Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/databricks/sql/backend/kernel/type_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,18 @@ def _arrow_type_to_dbapi_string(arrow_type: pyarrow.DataType) -> str:
return SqlType.DATE
if pyarrow.types.is_timestamp(arrow_type):
return SqlType.TIMESTAMP
if pyarrow.types.is_time32(arrow_type) or pyarrow.types.is_time64(arrow_type):
return SqlType.TIME
if pyarrow.types.is_list(arrow_type) or pyarrow.types.is_large_list(arrow_type):
return SqlType.ARRAY
if pyarrow.types.is_struct(arrow_type):
return SqlType.STRUCT
if pyarrow.types.is_map(arrow_type):
return SqlType.MAP
# Fallback for types the kernel hasn't been observed to emit yet
# (time32/time64, unsigned ints, dictionary, string_view,
# binary_view, fixed_size_*). ``str(arrow_type)`` produces shapes
# like ``"fixed_size_binary[16]"`` — distinguishable from the
# (unsigned ints, dictionary, string_view, binary_view,
# fixed_size_*). ``str(arrow_type)`` produces shapes like
# ``"fixed_size_binary[16]"`` — distinguishable from the
# canonical slugs above, so callers can detect the unknown.
return str(arrow_type)

Expand All @@ -103,11 +105,13 @@ def description_from_arrow_schema(schema: pyarrow.Schema) -> List[Tuple]:
``VARIANT`` → ``Utf8``). To recover the precise Databricks type
name, we consult the field's metadata first — the kernel writes
the server-reported type into ``databricks.type_name`` (see
``databricks_sql_kernel::reader::metadata_keys``). Today only
``VARIANT`` is special-cased here for parity with the Thrift
backend's behaviour; other precise types (``INTERVAL_*``,
``GEOMETRY``, ``GEOGRAPHY``) collapse to their Arrow shape on
both backends and don't need a remap.
``databricks_sql_kernel::reader::metadata_keys``). The names
honoured there are ``VARIANT``, ``ARRAY`` / ``MAP`` / ``STRUCT``,
``GEOGRAPHY`` / ``GEOMETRY`` and ``TIME``; other precise types
(``INTERVAL_*``) collapse to their Arrow shape on both backends
and don't need a remap. ``TIME`` needs the metadata path in
particular for empty results, whose manifest physical schema is
``Utf8`` and carries no TIME shape at all.

``precision`` / ``scale`` are extracted from ``Decimal128Type`` /
``Decimal256Type`` so DECIMAL columns expose the same
Expand Down Expand Up @@ -163,6 +167,9 @@ def _databricks_type_for_field(field: pyarrow.Field) -> str:
- ``GEOGRAPHY`` / ``GEOMETRY`` (also ``Utf8`` on the wire — the
server returns WKT/WKB text; only the manifest metadata marks
them as geospatial).
- ``TIME`` (``time32`` / ``time64`` on the wire, or ``Utf8`` in
an empty-result manifest whose physical schema carries no TIME
shape — only the manifest metadata marks it as TIME).
- The ``complex_types_as_json`` post-processor rewrites
``ARRAY`` / ``MAP`` / ``STRUCT`` columns to ``Utf8`` carrying
compact JSON text. The Thrift backend reports the original
Expand All @@ -185,13 +192,19 @@ def _databricks_type_for_field(field: pyarrow.Field) -> str:
# types (e.g. ``DECIMAL`` arrives as ``decimal`` on the
# Arrow side, which matches Thrift).
decoded = type_name.decode("ascii", errors="replace").lower()
# TIME may arrive with its declared precision (``"TIME(6)"``),
# like the Thrift backend's SqlName remap — strip a
# parenthesised suffix before the allow-list check so the
# logical type survives at every precision.
decoded = decoded.split("(", 1)[0]
if decoded in {
"variant",
"array",
"map",
"struct",
"geography",
"geometry",
"time",
}:
return decoded
return _arrow_type_to_dbapi_string(field.type)
Expand Down
1 change: 1 addition & 0 deletions src/databricks/sql/backend/sea/utils/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class SqlType:
# Date/Time types
DATE = "date" # Maps to TTypeId.DATE_TYPE
TIMESTAMP = "timestamp" # Maps to TTypeId.TIMESTAMP_TYPE
TIME = "time" # No TTypeId; TIME arrives as STRING in Thrift and as time32/time64 in Arrow
INTERVAL_YEAR_MONTH = (
"interval_year_month" # Maps to TTypeId.INTERVAL_YEAR_MONTH_TYPE
)
Expand Down
2 changes: 1 addition & 1 deletion src/databricks/sql/backend/thrift_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ def _col_to_description(col, field=None, host_url=None):
if field.metadata and b"Spark:DataType:SqlName" in field.metadata:
sql_type = field.metadata.get(b"Spark:DataType:SqlName")
logical_type = re.fullmatch(
rb"(variant|geometry|geography)(?:\([^()]*\))?",
rb"(variant|geometry|geography|time)(?:\([^()]*\))?",
sql_type.strip(),
re.IGNORECASE,
)
Expand Down
72 changes: 72 additions & 0 deletions tests/unit/test_kernel_type_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
(pa.large_binary(), "binary"),
(pa.date32(), "date"),
(pa.timestamp("us"), "timestamp"),
(pa.time32("ms"), "time"),
(pa.time64("us"), "time"),
(pa.time64("ns"), "time"),
(pa.list_(pa.int32()), "array"),
(pa.large_list(pa.int32()), "array"),
(pa.struct([("a", pa.int32())]), "struct"),
Expand Down Expand Up @@ -145,6 +148,74 @@ def test_description_recovers_complex_type_name_from_metadata(metadata_value, ex
assert desc[0][1] == expected


def test_description_recovers_time_type_name_from_metadata():
"""TIME columns arrive over SEA as time32/time64, or as Utf8 in
an empty-result manifest whose physical schema carries no TIME
shape. The kernel annotates the field with
``databricks.type_name=TIME``; ``description`` must report
``time`` rather than the raw Arrow spelling (``time64[ns]``) or
``string``. Mirrors the Thrift backend, which recovers TIME from
the ``Spark:DataType:SqlName`` Arrow metadata."""
schema = pa.schema(
[
pa.field(
"t",
pa.time64("ns"),
metadata={b"databricks.type_name": b"TIME"},
),
# Empty-manifest shape: Utf8 physical type, logical TIME
# in metadata — the DATATYPE-044 zero-row case.
pa.field(
"e",
pa.string(),
metadata={b"databricks.type_name": b"TIME"},
),
# Declared precision rides along in the metadata value.
pa.field(
"p",
pa.string(),
metadata={b"databricks.type_name": b"TIME(6)"},
),
# Plain time64 without metadata still reports ``time``
# via the Arrow shape — the DATATYPE-044 NULL-only case
# when no logical-type metadata is present.
pa.field("n", pa.time64("us")),
# Plain Utf8 without the metadata stays ``string``.
pa.field("s", pa.string()),
]
)
desc = description_from_arrow_schema(schema)
assert [d[1] for d in desc] == ["time", "time", "time", "time", "string"]


@pytest.mark.parametrize(
"metadata_value, expected",
[
(b"TIME", "time"),
(b"time", "time"),
(b"Time", "time"),
(b"TIME(6)", "time"),
],
)
def test_description_recovers_time_type_name_case_and_precision(
metadata_value, expected
):
"""``databricks.type_name`` matching is case-insensitive and
tolerates a declared precision, matching the Thrift backend's
SqlName remap (``TIME`` / ``TIME(6)`` / ``time(3)``)."""
schema = pa.schema(
[
pa.field(
"t",
pa.string(),
metadata={b"databricks.type_name": metadata_value},
),
]
)
desc = description_from_arrow_schema(schema)
assert desc[0][1] == expected


@pytest.mark.parametrize(
"metadata_value, expected",
[
Expand Down Expand Up @@ -227,6 +298,7 @@ def test_description_non_decimal_columns_have_none_precision_scale():
("i", pa.int64()),
("s", pa.string()),
("ts", pa.timestamp("us")),
("t", pa.time64("ns")),
]
)
desc = description_from_arrow_schema(schema)
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_thrift_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3094,6 +3094,9 @@ def test_col_to_description(self):
{b"Spark:DataType:SqlName": b"GEOGRAPHY"},
"geography",
),
("time_col", {b"Spark:DataType:SqlName": b"TIME(6)"}, "time"),
("time_no_precision_col", {b"Spark:DataType:SqlName": b"TIME"}, "time"),
("time_lower_col", {b"Spark:DataType:SqlName": b"time(3)"}, "time"),
("normal_col", {}, "string"),
(
"weird_field",
Expand Down Expand Up @@ -3142,6 +3145,7 @@ def test_hive_schema_to_description(self):
("variant_col", ttypes.TTypeId.STRING_TYPE),
("geometry_col", ttypes.TTypeId.STRING_TYPE),
("geography_col", ttypes.TTypeId.STRING_TYPE),
("time_col", ttypes.TTypeId.STRING_TYPE),
],
[
("regular_col", {}),
Expand All @@ -3154,12 +3158,14 @@ def test_hive_schema_to_description(self):
"geography_col",
{b"Spark:DataType:SqlName": b"GEOGRAPHY(4326)"},
),
("time_col", {b"Spark:DataType:SqlName": b"TIME(6)"}),
],
[
("regular_col", "string"),
("variant_col", "variant"),
("geometry_col", "geometry"),
("geography_col", "geography"),
("time_col", "time"),
],
),
(
Expand Down