-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathvalue_wrapper.py
More file actions
386 lines (284 loc) · 9.6 KB
/
value_wrapper.py
File metadata and controls
386 lines (284 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# Copyright 2025 vesoft-inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from datetime import date, datetime, time
from decimal import Decimal
from typing import Any, List, Optional, Type, TypeVar, Union, overload
from nebulagraph_python.decoder.data_types import ColumnType
from nebulagraph_python.py_data_types import (
ColumnToPy,
CompositeDataObject,
Edge,
NDuration,
Node,
NRecord,
NVector,
Path,
TargetType,
)
logger = logging.getLogger(__name__)
T = TypeVar("T", bound=TargetType)
class ValueWrapper:
def __init__(self, value: Any, data_type: ColumnType):
self.value = value
self.data_type = data_type
def is_null(self) -> bool:
"""If the value is null
Returns
-------
bool: true if value is null
"""
return self.value is None
def is_bool(self) -> bool:
"""Check if the Value is Boolean type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_BOOL
"""
return self.data_type == ColumnType.BOOL
def is_long(self) -> bool:
"""Check if the Value is Long type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_UINT64 or COLUMN_TYPE_INT64
"""
return self.data_type in [ColumnType.UINT64, ColumnType.INT64]
def is_int(self) -> bool:
"""Check if the Value is Int type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_UINT8 or COLUMN_TYPE_INT8 or COLUMN_TYPE_UINT16
or COLUMN_TYPE_INT16 or COLUMN_TYPE_UINT32 or COLUMN_TYPE_INT32
"""
return self.data_type in [
ColumnType.UINT8,
ColumnType.INT8,
ColumnType.UINT16,
ColumnType.INT16,
ColumnType.UINT32,
ColumnType.INT32,
]
def is_float(self) -> bool:
"""Check if the Value is Float type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_FLOAT32
"""
return self.data_type == ColumnType.FLOAT32
def is_double(self) -> bool:
"""Check if the Value is Double type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_FLOAT64
"""
return self.data_type == ColumnType.FLOAT64
def is_string(self) -> bool:
"""Check if the Value is String type.
Returns
-------
bool: true if Value's type is COLUMN_TYPE_STRING
"""
return self.data_type == ColumnType.STRING
def is_list(self) -> bool:
"""Check if the Value is List type.
Returns
-------
bool: true if Value's type is COLUMN_TYPE_LIST
"""
return self.data_type == ColumnType.LIST
def is_node(self) -> bool:
"""Check if the Value is Node type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_NODE
"""
return self.data_type == ColumnType.NODE
def is_edge(self) -> bool:
"""Check if the Value is Edge type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_EDGE
"""
return self.data_type == ColumnType.EDGE
def is_local_time(self) -> bool:
"""Check if the Value is Local Time type.
Returns
-------
bool: true if Value's type is COLUMN_TYPE_LOCALTIME
"""
return self.data_type == ColumnType.LOCALTIME
def is_zoned_time(self) -> bool:
"""Check if the Value is Zoned Time type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_ZONEDTIME
"""
return self.data_type == ColumnType.ZONEDTIME
def is_local_datetime(self) -> bool:
"""Check if the Value is Local Datetime type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_LOCALDATETIME
"""
return self.data_type == ColumnType.LOCALDATETIME
def is_zoned_datetime(self) -> bool:
"""Check if the Value is Zoned Datetime type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_ZONEDDATETIME
"""
return self.data_type == ColumnType.ZONEDDATETIME
def is_date(self) -> bool:
"""Check if the Value is Date type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_DATE
"""
return self.data_type == ColumnType.DATE
def is_record(self) -> bool:
"""Check if the Value is Record type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_RECORD
"""
return self.data_type == ColumnType.RECORD
def is_duration(self) -> bool:
"""Check if the Value is Duration type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_DURATION
"""
return self.data_type == ColumnType.DURATION
def is_path(self) -> bool:
"""Check if the Value is Path type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_PATH
"""
return self.data_type == ColumnType.PATH
def is_decimal(self) -> bool:
"""Check if the Value is Decimal type
Returns
-------
bool: true if Value's type is COLUMN_TYPE_DECIMAL
"""
return self.data_type == ColumnType.DECIMAL
def as_bool(self):
return self.cast(bool)
def as_int(self):
return self.cast(int)
def as_long(self):
return self.cast(int)
def as_string(self):
return self.cast(str)
def as_float(self):
return self.cast(float)
def as_double(self):
return self.cast(float)
def as_list(self):
return self.cast(list)
def as_node(self):
return self.cast(Node)
def as_edge(self):
return self.cast(Edge)
def as_local_time(self):
return self.cast(time)
def as_zoned_time(self):
return self.cast(time)
def as_date(self):
return self.cast(date)
def as_local_datetime(self):
return self.cast(datetime)
def as_zoned_datetime(self):
return self.cast(datetime)
def as_duration(self):
return self.cast(NDuration)
def as_record(self):
return self.cast(NRecord)
def as_path(self):
return self.cast(Path)
def as_decimal(self):
return self.cast(Decimal)
def as_embedding_vector(self):
return self.cast(NVector)
@overload
def cast(self) -> TargetType:
"""Get self.value"""
...
@overload
def cast(self, target_type: Type[T]) -> T:
"""Get self.value, with target_type check"""
...
def cast(
self,
target_type: Optional[Type[T]] = None,
) -> Union[T, TargetType]:
if self.value is None and target_type is None:
return None
target_type = ColumnToPy[self.data_type] if target_type is None else target_type # type: ignore
if target_type is not None:
if isinstance(self.value, target_type):
return self.value
else:
raise TypeError(
f"Cannot cast {type(self.value)} to {target_type}. ColumnType and value:",
self.data_type,
self.value,
)
def cast_primitive(self) -> Any:
"""Convert the wrapped value to primitive Python types recursively.
For basic types, uses cast()
For composite types (Path, Node, Edge), calls cast_primitive()
For containers (Map, List, Record), recursively calls cast_primitive() on elements
"""
outer = self.cast()
# Handle composite types
if isinstance(outer, CompositeDataObject):
return outer.cast_primitive()
# Handle list recursively
if isinstance(outer, list):
ans = []
for i, v in enumerate(outer):
if isinstance(v, ValueWrapper):
ans.append(v.cast_primitive())
else:
raise TypeError(
f"Cannot cast list, where list[{i}] have type {type(v)}, not ValueWrapper. list[{i}]: ",
v,
)
return ans
return outer
def __eq__(self, other):
if not isinstance(other, ValueWrapper):
return False
return self.value == other.value and self.data_type == other.data_type
def __hash__(self) -> int:
return hash((self.value, self.data_type))
class Row:
def __init__(self, values: Optional[List[ValueWrapper]] = None):
self.values = values if values is not None else []
def add_value(self, value: ValueWrapper):
"""Append one value into row.
Args:
----
value (ValueWrapper): one value of the row
"""
self.values.append(value)
def get_values(self) -> list[ValueWrapper]:
"""Get the values of this row.
Returns
-------
List[ValueWrapper]: list of values in this row
"""
return self.values