Skip to content

Commit 76bb73a

Browse files
committed
fix(thrift): report geospatial cursor metadata
Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
1 parent 011cc14 commit 76bb73a

2 files changed

Lines changed: 51 additions & 6 deletions

File tree

‎src/databricks/sql/backend/thrift_backend.py‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import errno
44
import logging
55
import math
6+
import re
67
import time
78
import threading
89
from typing import Dict, List, Optional, Union, Any, TYPE_CHECKING
@@ -788,16 +789,20 @@ def _col_to_description(col, field=None, host_url=None):
788789
else:
789790
precision, scale = None, None
790791

791-
# Extract variant type from field if available
792+
# Extract logical types that Thrift surfaces as strings from Arrow metadata.
792793
if field is not None:
793794
try:
794-
# Check for variant type in metadata
795795
if field.metadata and b"Spark:DataType:SqlName" in field.metadata:
796796
sql_type = field.metadata.get(b"Spark:DataType:SqlName")
797-
if sql_type == b"VARIANT":
798-
cleaned_type = "variant"
797+
logical_type = re.fullmatch(
798+
rb"(variant|geometry|geography)(?:\([^()]*\))?",
799+
sql_type.strip(),
800+
re.IGNORECASE,
801+
)
802+
if logical_type:
803+
cleaned_type = logical_type.group(1).decode("ascii").lower()
799804
except Exception as e:
800-
logger.debug(f"Could not extract variant type from field: {e}")
805+
logger.debug(f"Could not extract logical type from field: {e}")
801806

802807
return col.columnName, cleaned_type, None, None, precision, scale, None
803808

‎tests/unit/test_thrift_backend.py‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2534,12 +2534,37 @@ def test_execute_command_sets_complex_type_fields_correctly(
25342534
def test_col_to_description(self):
25352535
test_cases = [
25362536
("variant_col", {b"Spark:DataType:SqlName": b"VARIANT"}, "variant"),
2537+
(
2538+
"geometry_col",
2539+
{b"Spark:DataType:SqlName": b"GEOMETRY(0)"},
2540+
"geometry",
2541+
),
2542+
(
2543+
"geography_col",
2544+
{b"Spark:DataType:SqlName": b"GEOGRAPHY(4326)"},
2545+
"geography",
2546+
),
2547+
(
2548+
"geometry_any_col",
2549+
{b"Spark:DataType:SqlName": b"geometry(ANY)"},
2550+
"geometry",
2551+
),
2552+
(
2553+
"unqualified_geography_col",
2554+
{b"Spark:DataType:SqlName": b"GEOGRAPHY"},
2555+
"geography",
2556+
),
25372557
("normal_col", {}, "string"),
25382558
(
25392559
"weird_field",
25402560
{b"Spark:DataType:SqlName": b"Some unexpected value"},
25412561
"string",
25422562
),
2563+
(
2564+
"malformed_geometry_col",
2565+
{b"Spark:DataType:SqlName": b"GEOMETRY("},
2566+
"string",
2567+
),
25432568
("missing_field", None, "string"), # None field case
25442569
]
25452570

@@ -2575,12 +2600,27 @@ def test_hive_schema_to_description(self):
25752600
[
25762601
("regular_col", ttypes.TTypeId.STRING_TYPE),
25772602
("variant_col", ttypes.TTypeId.STRING_TYPE),
2603+
("geometry_col", ttypes.TTypeId.STRING_TYPE),
2604+
("geography_col", ttypes.TTypeId.STRING_TYPE),
25782605
],
25792606
[
25802607
("regular_col", {}),
25812608
("variant_col", {b"Spark:DataType:SqlName": b"VARIANT"}),
2609+
(
2610+
"geometry_col",
2611+
{b"Spark:DataType:SqlName": b"GEOMETRY(0)"},
2612+
),
2613+
(
2614+
"geography_col",
2615+
{b"Spark:DataType:SqlName": b"GEOGRAPHY(4326)"},
2616+
),
2617+
],
2618+
[
2619+
("regular_col", "string"),
2620+
("variant_col", "variant"),
2621+
("geometry_col", "geometry"),
2622+
("geography_col", "geography"),
25822623
],
2583-
[("regular_col", "string"), ("variant_col", "variant")],
25842624
),
25852625
(
25862626
[("regular_col", ttypes.TTypeId.STRING_TYPE)],

0 commit comments

Comments
 (0)