Skip to content

Commit b2e96eb

Browse files
committed
fix(thrift, kernel): report TIME logical type in cursor.description
DATATYPE-043: the Thrift SqlName remap covered only variant/geometry/geography, so TIME columns reported 'string'; the kernel type_mapping allow-list likewise omitted TIME, so SEA reported the raw Arrow spelling 'time64[ns]'. Add 'time' to both, map Arrow time32/time64 to the new SqlType.TIME, and honour a declared precision (TIME(6)) on either path. DATATYPE-044: the same remaps preserve the result-schema TIME logical type for NULL-only columns and zero-row results (including empty SEA manifests whose physical schema is Utf8). Fixes #1075 Signed-off-by: Atharva Chavhan <atharvachavhan18@gmail.com>
1 parent 543777a commit b2e96eb

5 files changed

Lines changed: 101 additions & 9 deletions

File tree

‎src/databricks/sql/backend/kernel/type_mapping.py‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,18 @@ def _arrow_type_to_dbapi_string(arrow_type: pyarrow.DataType) -> str:
7373
return SqlType.DATE
7474
if pyarrow.types.is_timestamp(arrow_type):
7575
return SqlType.TIMESTAMP
76+
if pyarrow.types.is_time32(arrow_type) or pyarrow.types.is_time64(arrow_type):
77+
return SqlType.TIME
7678
if pyarrow.types.is_list(arrow_type) or pyarrow.types.is_large_list(arrow_type):
7779
return SqlType.ARRAY
7880
if pyarrow.types.is_struct(arrow_type):
7981
return SqlType.STRUCT
8082
if pyarrow.types.is_map(arrow_type):
8183
return SqlType.MAP
8284
# Fallback for types the kernel hasn't been observed to emit yet
83-
# (time32/time64, unsigned ints, dictionary, string_view,
84-
# binary_view, fixed_size_*). ``str(arrow_type)`` produces shapes
85-
# like ``"fixed_size_binary[16]"`` — distinguishable from the
85+
# (unsigned ints, dictionary, string_view, binary_view,
86+
# fixed_size_*). ``str(arrow_type)`` produces shapes like
87+
# ``"fixed_size_binary[16]"`` — distinguishable from the
8688
# canonical slugs above, so callers can detect the unknown.
8789
return str(arrow_type)
8890

@@ -103,11 +105,13 @@ def description_from_arrow_schema(schema: pyarrow.Schema) -> List[Tuple]:
103105
``VARIANT`` → ``Utf8``). To recover the precise Databricks type
104106
name, we consult the field's metadata first — the kernel writes
105107
the server-reported type into ``databricks.type_name`` (see
106-
``databricks_sql_kernel::reader::metadata_keys``). Today only
107-
``VARIANT`` is special-cased here for parity with the Thrift
108-
backend's behaviour; other precise types (``INTERVAL_*``,
109-
``GEOMETRY``, ``GEOGRAPHY``) collapse to their Arrow shape on
110-
both backends and don't need a remap.
108+
``databricks_sql_kernel::reader::metadata_keys``). The names
109+
honoured there are ``VARIANT``, ``ARRAY`` / ``MAP`` / ``STRUCT``,
110+
``GEOGRAPHY`` / ``GEOMETRY`` and ``TIME``; other precise types
111+
(``INTERVAL_*``) collapse to their Arrow shape on both backends
112+
and don't need a remap. ``TIME`` needs the metadata path in
113+
particular for empty results, whose manifest physical schema is
114+
``Utf8`` and carries no TIME shape at all.
111115
112116
``precision`` / ``scale`` are extracted from ``Decimal128Type`` /
113117
``Decimal256Type`` so DECIMAL columns expose the same
@@ -163,6 +167,9 @@ def _databricks_type_for_field(field: pyarrow.Field) -> str:
163167
- ``GEOGRAPHY`` / ``GEOMETRY`` (also ``Utf8`` on the wire — the
164168
server returns WKT/WKB text; only the manifest metadata marks
165169
them as geospatial).
170+
- ``TIME`` (``time32`` / ``time64`` on the wire, or ``Utf8`` in
171+
an empty-result manifest whose physical schema carries no TIME
172+
shape — only the manifest metadata marks it as TIME).
166173
- The ``complex_types_as_json`` post-processor rewrites
167174
``ARRAY`` / ``MAP`` / ``STRUCT`` columns to ``Utf8`` carrying
168175
compact JSON text. The Thrift backend reports the original
@@ -185,13 +192,19 @@ def _databricks_type_for_field(field: pyarrow.Field) -> str:
185192
# types (e.g. ``DECIMAL`` arrives as ``decimal`` on the
186193
# Arrow side, which matches Thrift).
187194
decoded = type_name.decode("ascii", errors="replace").lower()
195+
# TIME may arrive with its declared precision (``"TIME(6)"``),
196+
# like the Thrift backend's SqlName remap — strip a
197+
# parenthesised suffix before the allow-list check so the
198+
# logical type survives at every precision.
199+
decoded = decoded.split("(", 1)[0]
188200
if decoded in {
189201
"variant",
190202
"array",
191203
"map",
192204
"struct",
193205
"geography",
194206
"geometry",
207+
"time",
195208
}:
196209
return decoded
197210
return _arrow_type_to_dbapi_string(field.type)

‎src/databricks/sql/backend/sea/utils/conversion.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class SqlType:
7171
# Date/Time types
7272
DATE = "date" # Maps to TTypeId.DATE_TYPE
7373
TIMESTAMP = "timestamp" # Maps to TTypeId.TIMESTAMP_TYPE
74+
TIME = "time" # No TTypeId; TIME arrives as STRING in Thrift and as time32/time64 in Arrow
7475
INTERVAL_YEAR_MONTH = (
7576
"interval_year_month" # Maps to TTypeId.INTERVAL_YEAR_MONTH_TYPE
7677
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ def _col_to_description(col, field=None, host_url=None):
826826
if field.metadata and b"Spark:DataType:SqlName" in field.metadata:
827827
sql_type = field.metadata.get(b"Spark:DataType:SqlName")
828828
logical_type = re.fullmatch(
829-
rb"(variant|geometry|geography)(?:\([^()]*\))?",
829+
rb"(variant|geometry|geography|time)(?:\([^()]*\))?",
830830
sql_type.strip(),
831831
re.IGNORECASE,
832832
)

‎tests/unit/test_kernel_type_mapping.py‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
(pa.large_binary(), "binary"),
3434
(pa.date32(), "date"),
3535
(pa.timestamp("us"), "timestamp"),
36+
(pa.time32("ms"), "time"),
37+
(pa.time64("us"), "time"),
38+
(pa.time64("ns"), "time"),
3639
(pa.list_(pa.int32()), "array"),
3740
(pa.large_list(pa.int32()), "array"),
3841
(pa.struct([("a", pa.int32())]), "struct"),
@@ -145,6 +148,74 @@ def test_description_recovers_complex_type_name_from_metadata(metadata_value, ex
145148
assert desc[0][1] == expected
146149

147150

151+
def test_description_recovers_time_type_name_from_metadata():
152+
"""TIME columns arrive over SEA as time32/time64, or as Utf8 in
153+
an empty-result manifest whose physical schema carries no TIME
154+
shape. The kernel annotates the field with
155+
``databricks.type_name=TIME``; ``description`` must report
156+
``time`` rather than the raw Arrow spelling (``time64[ns]``) or
157+
``string``. Mirrors the Thrift backend, which recovers TIME from
158+
the ``Spark:DataType:SqlName`` Arrow metadata."""
159+
schema = pa.schema(
160+
[
161+
pa.field(
162+
"t",
163+
pa.time64("ns"),
164+
metadata={b"databricks.type_name": b"TIME"},
165+
),
166+
# Empty-manifest shape: Utf8 physical type, logical TIME
167+
# in metadata — the DATATYPE-044 zero-row case.
168+
pa.field(
169+
"e",
170+
pa.string(),
171+
metadata={b"databricks.type_name": b"TIME"},
172+
),
173+
# Declared precision rides along in the metadata value.
174+
pa.field(
175+
"p",
176+
pa.string(),
177+
metadata={b"databricks.type_name": b"TIME(6)"},
178+
),
179+
# Plain time64 without metadata still reports ``time``
180+
# via the Arrow shape — the DATATYPE-044 NULL-only case
181+
# when no logical-type metadata is present.
182+
pa.field("n", pa.time64("us")),
183+
# Plain Utf8 without the metadata stays ``string``.
184+
pa.field("s", pa.string()),
185+
]
186+
)
187+
desc = description_from_arrow_schema(schema)
188+
assert [d[1] for d in desc] == ["time", "time", "time", "time", "string"]
189+
190+
191+
@pytest.mark.parametrize(
192+
"metadata_value, expected",
193+
[
194+
(b"TIME", "time"),
195+
(b"time", "time"),
196+
(b"Time", "time"),
197+
(b"TIME(6)", "time"),
198+
],
199+
)
200+
def test_description_recovers_time_type_name_case_and_precision(
201+
metadata_value, expected
202+
):
203+
"""``databricks.type_name`` matching is case-insensitive and
204+
tolerates a declared precision, matching the Thrift backend's
205+
SqlName remap (``TIME`` / ``TIME(6)`` / ``time(3)``)."""
206+
schema = pa.schema(
207+
[
208+
pa.field(
209+
"t",
210+
pa.string(),
211+
metadata={b"databricks.type_name": metadata_value},
212+
),
213+
]
214+
)
215+
desc = description_from_arrow_schema(schema)
216+
assert desc[0][1] == expected
217+
218+
148219
@pytest.mark.parametrize(
149220
"metadata_value, expected",
150221
[
@@ -227,6 +298,7 @@ def test_description_non_decimal_columns_have_none_precision_scale():
227298
("i", pa.int64()),
228299
("s", pa.string()),
229300
("ts", pa.timestamp("us")),
301+
("t", pa.time64("ns")),
230302
]
231303
)
232304
desc = description_from_arrow_schema(schema)

‎tests/unit/test_thrift_backend.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3094,6 +3094,9 @@ def test_col_to_description(self):
30943094
{b"Spark:DataType:SqlName": b"GEOGRAPHY"},
30953095
"geography",
30963096
),
3097+
("time_col", {b"Spark:DataType:SqlName": b"TIME(6)"}, "time"),
3098+
("time_no_precision_col", {b"Spark:DataType:SqlName": b"TIME"}, "time"),
3099+
("time_lower_col", {b"Spark:DataType:SqlName": b"time(3)"}, "time"),
30973100
("normal_col", {}, "string"),
30983101
(
30993102
"weird_field",
@@ -3142,6 +3145,7 @@ def test_hive_schema_to_description(self):
31423145
("variant_col", ttypes.TTypeId.STRING_TYPE),
31433146
("geometry_col", ttypes.TTypeId.STRING_TYPE),
31443147
("geography_col", ttypes.TTypeId.STRING_TYPE),
3148+
("time_col", ttypes.TTypeId.STRING_TYPE),
31453149
],
31463150
[
31473151
("regular_col", {}),
@@ -3154,12 +3158,14 @@ def test_hive_schema_to_description(self):
31543158
"geography_col",
31553159
{b"Spark:DataType:SqlName": b"GEOGRAPHY(4326)"},
31563160
),
3161+
("time_col", {b"Spark:DataType:SqlName": b"TIME(6)"}),
31573162
],
31583163
[
31593164
("regular_col", "string"),
31603165
("variant_col", "variant"),
31613166
("geometry_col", "geometry"),
31623167
("geography_col", "geography"),
3168+
("time_col", "time"),
31633169
],
31643170
),
31653171
(

0 commit comments

Comments
 (0)