Skip to content

Commit f90ba59

Browse files
authored
SNOW-1867961: Fix from_json not working the TimestampType that contains tzinfo (#2825)
1 parent 31b5c8f commit f90ba59

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
- Updated README.md to include instructions on how to verify package signatures using `cosign`.
2222

23+
#### Bug Fixes
24+
25+
- Fixed a bug in StructField.from_json that prevented TimestampTypes with tzinfo from being parsed correctly.
26+
2327
### Snowpark pandas API Updates
2428

2529
#### New Features

src/snowflake/snowpark/types.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,10 +943,20 @@ def _fill_ast(self, ast: proto.SpDataType) -> None:
943943
IntegerType,
944944
LongType,
945945
DateType,
946-
TimestampType,
947946
NullType,
948947
]
948+
949+
_timestamp_types: List[DataType] = [
950+
TimestampType(),
951+
TimestampType(timezone=TimestampTimeZone.NTZ),
952+
TimestampType(timezone=TimestampTimeZone.LTZ),
953+
TimestampType(timezone=TimestampTimeZone.TZ),
954+
]
955+
949956
_all_atomic_types: Dict[str, Type[DataType]] = {t.typeName(): t for t in _atomic_types}
957+
_all_timestamp_types: Dict[str, DataType] = {
958+
t.json_value(): t for t in _timestamp_types
959+
}
950960

951961
_complex_types: List[Type[Union[ArrayType, MapType, StructType]]] = [
952962
ArrayType,
@@ -964,8 +974,10 @@ def _fill_ast(self, ast: proto.SpDataType) -> None:
964974

965975
def _parse_datatype_json_value(json_value: Union[dict, str]) -> DataType:
966976
if not isinstance(json_value, dict):
967-
if json_value in _all_atomic_types.keys():
977+
if json_value in _all_atomic_types:
968978
return _all_atomic_types[json_value]()
979+
if json_value in _all_timestamp_types:
980+
return TimestampType(timezone=_all_timestamp_types[json_value].tz)
969981
elif json_value == "decimal":
970982
return DecimalType()
971983
elif _FIXED_DECIMAL_PATTERN.match(json_value):

tests/unit/test_types.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,23 @@ def test_from_json_wrong_data_type():
14311431
StructField.from_json(wrong_json)
14321432

14331433

1434+
def test_timestamp_json_round_trip():
1435+
timestamp_types = [
1436+
"timestamp",
1437+
"timestamp_tz",
1438+
"timestamp_ntz",
1439+
"timestamp_ltz",
1440+
]
1441+
1442+
for ts in timestamp_types:
1443+
assert (
1444+
StructField.from_json(
1445+
{"name": "TS", "type": ts, "nullable": True}
1446+
).json_value()["type"]
1447+
== ts
1448+
)
1449+
1450+
14341451
def test_maptype_alias():
14351452
expected_key = StringType()
14361453
expected_value = IntegerType()

0 commit comments

Comments
 (0)