Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6b9c8a2

Browse files
committedApr 23, 2025·
Support new YDB types
1 parent 0fff143 commit 6b9c8a2

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed
 

‎ydb/types.py‎

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ def _to_date(pb: ydb_value_pb2.Value, value: typing.Union[date, int]) -> None:
4040
pb.uint32_value = value
4141

4242

43+
def _from_date32(x: ydb_value_pb2.Value, table_client_settings: table.TableClientSettings) -> typing.Union[date, int]:
44+
if table_client_settings is not None and table_client_settings._native_date_in_result_sets:
45+
return _EPOCH.date() + timedelta(days=x.int32_value)
46+
return x.int32_value
47+
48+
49+
def _to_date32(pb: ydb_value_pb2.Value, value: typing.Union[date, int]) -> None:
50+
if isinstance(value, date):
51+
pb.int32_value = (value - _EPOCH.date()).days
52+
else:
53+
pb.int32_value = value
54+
55+
4356
def _from_datetime_number(
4457
x: typing.Union[float, datetime], table_client_settings: table.TableClientSettings
4558
) -> datetime:
@@ -63,6 +76,10 @@ def _from_uuid(pb: ydb_value_pb2.Value, value: uuid.UUID):
6376
pb.high_128 = struct.unpack("Q", value.bytes_le[8:16])[0]
6477

6578

79+
def _timedelta_to_microseconds(value: timedelta) -> int:
80+
return (value.days * _SECONDS_IN_DAY + value.seconds) * 1000000 + value.microseconds
81+
82+
6683
def _from_interval(
6784
value_pb: ydb_value_pb2.Value, table_client_settings: table.TableClientSettings
6885
) -> typing.Union[timedelta, int]:
@@ -71,10 +88,6 @@ def _from_interval(
7188
return value_pb.int64_value
7289

7390

74-
def _timedelta_to_microseconds(value: timedelta) -> int:
75-
return (value.days * _SECONDS_IN_DAY + value.seconds) * 1000000 + value.microseconds
76-
77-
7891
def _to_interval(pb: ydb_value_pb2.Value, value: typing.Union[timedelta, int]):
7992
if isinstance(value, timedelta):
8093
pb.int64_value = _timedelta_to_microseconds(value)
@@ -101,6 +114,25 @@ def _to_timestamp(pb: ydb_value_pb2.Value, value: typing.Union[datetime, int]):
101114
pb.uint64_value = value
102115

103116

117+
def _from_timestamp64(
118+
value_pb: ydb_value_pb2.Value, table_client_settings: table.TableClientSettings
119+
) -> typing.Union[datetime, int]:
120+
if table_client_settings is not None and table_client_settings._native_timestamp_in_result_sets:
121+
return _EPOCH + timedelta(microseconds=value_pb.int64_value)
122+
return value_pb.int64_value
123+
124+
125+
def _to_timestamp64(pb: ydb_value_pb2.Value, value: typing.Union[datetime, int]):
126+
if isinstance(value, datetime):
127+
if value.tzinfo:
128+
epoch = _EPOCH_UTC
129+
else:
130+
epoch = _EPOCH
131+
pb.int64_value = _timedelta_to_microseconds(value - epoch)
132+
else:
133+
pb.int64_value = value
134+
135+
104136
@enum.unique
105137
class PrimitiveType(enum.Enum):
106138
"""
@@ -133,23 +165,46 @@ class PrimitiveType(enum.Enum):
133165
_from_date,
134166
_to_date,
135167
)
168+
Date32 = (
169+
_apis.primitive_types.DATE32,
170+
None,
171+
_from_date32,
172+
_to_date32,
173+
)
136174
Datetime = (
137175
_apis.primitive_types.DATETIME,
138176
"uint32_value",
139177
_from_datetime_number,
140178
)
179+
Datetime64 = (
180+
_apis.primitive_types.DATETIME64,
181+
"int32_value",
182+
_from_datetime_number,
183+
)
141184
Timestamp = (
142185
_apis.primitive_types.TIMESTAMP,
143186
None,
144187
_from_timestamp,
145188
_to_timestamp,
146189
)
190+
Timestamp64 = (
191+
_apis.primitive_types.TIMESTAMP64,
192+
None,
193+
_from_timestamp,
194+
_to_timestamp,
195+
)
147196
Interval = (
148197
_apis.primitive_types.INTERVAL,
149198
None,
150199
_from_interval,
151200
_to_interval,
152201
)
202+
Interval64 = (
203+
_apis.primitive_types.INTERVAL64,
204+
None,
205+
_from_interval,
206+
_to_interval,
207+
)
153208

154209
DyNumber = _apis.primitive_types.DYNUMBER, "text_value"
155210

0 commit comments

Comments
 (0)
Please sign in to comment.