FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
feast/sdk/python/feast/type_map.py at master · feast-dev/feast · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
feast-dev
/
feast
Public
Notifications
You must be signed in to change notification settings
Fork
1.4k
Star
7.2k
Code
Issues
214
Pull requests
189
Discussions
Actions
Security and quality
1
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
feast
/
sdk
/
python
/
feast
/
type_map.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
2430 lines (2156 loc) · 87.3 KB
Breadcrumbs
feast
/
sdk
/
python
/
feast
/
type_map.py
Copy path
File metadata and controls
2430 lines (2156 loc) · 87.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2019 The Feast Authors
#
# 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
#
# https://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
decimal
import
json
import
logging
import
re
import
uuid
as
uuid_module
from
collections
import
defaultdict
from
datetime
import
datetime
,
timedelta
,
timezone
from
typing
import
(
TYPE_CHECKING
,
Any
,
Dict
,
Iterator
,
List
,
Optional
,
Sequence
,
Set
,
Sized
,
Tuple
,
Type
,
Union
,
cast
,
)
import
numpy
as
np
import
pandas
as
pd
from
google
.
protobuf
.
timestamp_pb2
import
Timestamp
from
feast
.
protos
.
feast
.
types
.
Value_pb2
import
(
BoolList
,
BoolSet
,
BytesList
,
BytesSet
,
DoubleList
,
DoubleSet
,
FloatList
,
FloatSet
,
Int32List
,
Int32Set
,
Int64List
,
Int64Set
,
Map
,
MapKey
,
MapList
,
RepeatedValue
,
ScalarMap
,
ScalarMapEntry
,
StringList
,
StringSet
,
ZonedTimestamp
,
)
from
feast
.
protos
.
feast
.
types
.
Value_pb2
import
Value
as
ProtoValue
from
feast
.
value_type
import
ListType
,
SetType
,
ValueType
if
TYPE_CHECKING
:
import
pyarrow
# null timestamps get converted to -9223372036854775808
NULL_TIMESTAMP_INT_VALUE
:
int
=
np
.
datetime64
(
"NaT"
).
astype
(
int
)
logger
=
logging
.
getLogger
(
__name__
)
def
_zone_name
(
tzinfo
:
Optional
[
Any
])
->
str
:
"""Return a storable zone string for a datetime's tzinfo.
Prefers the IANA name (e.g. ``zoneinfo.ZoneInfo`` key) so DST is preserved;
falls back to a fixed-offset string (e.g. ``-07:00``). A naive datetime
(``tzinfo is None``) yields ``""``, which decodes back as UTC.
"""
if
tzinfo
is
None
:
return
""
key
=
getattr
(
tzinfo
,
"key"
,
None
)
# zoneinfo.ZoneInfo
if
key
:
return
key
name
=
str
(
tzinfo
)
# zoneinfo prints as the key; pytz prints the name; offsets print as "UTC-07:00"
return
name
def
_zone_from_name
(
zone
:
str
):
"""Resolve a stored zone string back to a tzinfo. Empty → UTC."""
if
not
zone
:
return
timezone
.
utc
try
:
from
zoneinfo
import
ZoneInfo
return
ZoneInfo
(
zone
)
except
Exception
:
# Not an IANA name. It may be a fixed-offset string produced by
# ``_zone_name`` (e.g. "UTC", "UTC-07:00", "+05:30"); parse it so the
# original offset is preserved on round trips rather than silently
# shifting the wall-clock time to UTC.
offset
=
_fixed_offset_from_name
(
zone
)
if
offset
is
not
None
:
return
offset
logger
.
warning
(
"Could not resolve zone %r; decoding as UTC"
,
zone
)
return
timezone
.
utc
def
_fixed_offset_from_name
(
zone
:
str
)
->
Optional
[
timezone
]:
"""Parse a fixed-offset zone string into a ``timezone``.
Accepts the forms ``_zone_name`` emits for offset-only tzinfos: a bare
``UTC``/``GMT``, or an offset like ``UTC-07:00``, ``-07:00``, ``+05:30`` or
``+0530``. Returns ``None`` if the string is not a recognizable offset.
"""
text
=
zone
.
strip
()
if
text
in
(
"UTC"
,
"GMT"
):
return
timezone
.
utc
match
=
re
.
fullmatch
(
r"(?:UTC|GMT)?([+-])(\d{2}):?(\d{2})"
,
text
,
)
if
not
match
:
return
None
sign
,
hours
,
minutes
=
match
.
groups
()
delta
=
timedelta
(
hours
=
int
(
hours
),
minutes
=
int
(
minutes
))
if
sign
==
"-"
:
delta
=
-
delta
return
timezone
(
delta
)
def
feast_value_type_to_python_type
(
field_value_proto
:
ProtoValue
,
feature_type
:
Optional
[
ValueType
]
=
None
,
)
->
Any
:
"""
Converts field value Proto to Dict and returns each field's Feast Value Type value
in their respective Python value.
Args:
field_value_proto: Field value Proto
Returns:
Python native type representation/version of the given field_value_proto
"""
val_attr
=
field_value_proto
.
WhichOneof
(
"val"
)
if
val_attr
is
None
:
return
None
val
=
getattr
(
field_value_proto
,
val_attr
)
# Handle JSON types — stored as strings but returned as parsed Python objects
if
val_attr
==
"json_val"
:
try
:
return
json
.
loads
(
val
)
except
(
json
.
JSONDecodeError
,
TypeError
):
return
val
elif
val_attr
==
"json_list_val"
:
result
=
[]
for
v
in
val
.
val
:
if
isinstance
(
v
,
str
):
try
:
result
.
append
(
json
.
loads
(
v
))
except
(
json
.
JSONDecodeError
,
TypeError
):
result
.
append
(
v
)
else
:
result
.
append
(
v
)
return
result
# Handle nested collection types (list_val, set_val)
if
val_attr
in
(
"list_val"
,
"set_val"
):
return
_handle_nested_collection_value
(
val
)
# Handle Struct types — stored using Map proto, returned as dicts
if
val_attr
==
"struct_val"
:
return
_handle_map_value
(
val
)
elif
val_attr
==
"struct_list_val"
:
return
_handle_map_list_value
(
val
)
# Handle Map and MapList types FIRST (before generic list processing)
if
val_attr
==
"map_val"
:
return
_handle_map_value
(
val
)
elif
val_attr
==
"map_list_val"
:
return
_handle_map_list_value
(
val
)
elif
val_attr
==
"scalar_map_val"
:
return
_handle_scalar_map_value
(
val
)
# Zoned timestamp: a (instant, zone) message → tz-aware datetime in its own zone.
if
val_attr
==
"zoned_timestamp_val"
:
if
val
.
unix_timestamp
==
NULL_TIMESTAMP_INT_VALUE
:
return
None
tz
=
_zone_from_name
(
val
.
zone
)
return
datetime
.
fromtimestamp
(
val
.
unix_timestamp
,
tz
=
tz
)
# If it's a _LIST or _SET type extract the values.
if
hasattr
(
val
,
"val"
):
val
=
list
(
val
.
val
)
# Convert UNIX_TIMESTAMP values to `datetime`
if
val_attr
==
"unix_timestamp_list_val"
:
val
=
[
(
datetime
.
fromtimestamp
(
v
,
tz
=
timezone
.
utc
)
if
v
!=
NULL_TIMESTAMP_INT_VALUE
else
None
)
for
v
in
val
]
elif
val_attr
==
"unix_timestamp_set_val"
:
val
=
set
(
[
(
datetime
.
fromtimestamp
(
v
,
tz
=
timezone
.
utc
)
if
v
!=
NULL_TIMESTAMP_INT_VALUE
else
None
)
for
v
in
val
]
)
elif
val_attr
==
"unix_timestamp_val"
:
val
=
(
datetime
.
fromtimestamp
(
val
,
tz
=
timezone
.
utc
)
if
val
!=
NULL_TIMESTAMP_INT_VALUE
else
None
)
# Convert _SET types to Python sets
elif
val_attr
.
endswith
(
"_set_val"
)
and
val_attr
!=
"unix_timestamp_set_val"
:
val
=
set
(
val
)
# Convert UUID values to uuid.UUID objects
if
val_attr
in
(
"uuid_val"
,
"time_uuid_val"
):
return
uuid_module
.
UUID
(
val
)
if
isinstance
(
val
,
str
)
else
val
if
val_attr
in
(
"uuid_list_val"
,
"time_uuid_list_val"
):
return
[
uuid_module
.
UUID
(
v
)
if
isinstance
(
v
,
str
)
else
v
for
v
in
val
]
if
val_attr
in
(
"uuid_set_val"
,
"time_uuid_set_val"
):
return
{
uuid_module
.
UUID
(
v
)
if
isinstance
(
v
,
str
)
else
v
for
v
in
val
}
# Convert DECIMAL values to decimal.Decimal objects
if
val_attr
==
"decimal_val"
:
return
decimal
.
Decimal
(
val
)
if
isinstance
(
val
,
str
)
else
val
if
val_attr
==
"decimal_list_val"
:
return
[
decimal
.
Decimal
(
v
)
if
isinstance
(
v
,
str
)
else
v
for
v
in
val
]
if
val_attr
==
"decimal_set_val"
:
return
{
decimal
.
Decimal
(
v
)
if
isinstance
(
v
,
str
)
else
v
for
v
in
val
}
# Backward compatibility: handle UUIDs stored as string_val/string_list_val with feature_type hint
if
feature_type
in
(
ValueType
.
UUID
,
ValueType
.
TIME_UUID
)
and
isinstance
(
val
,
str
):
return
uuid_module
.
UUID
(
val
)
if
feature_type
in
(
ValueType
.
UUID_LIST
,
ValueType
.
TIME_UUID_LIST
)
and
isinstance
(
val
,
list
):
return
[
uuid_module
.
UUID
(
v
)
if
isinstance
(
v
,
str
)
else
v
for
v
in
val
]
if
feature_type
in
(
ValueType
.
UUID_SET
,
ValueType
.
TIME_UUID_SET
)
and
isinstance
(
val
,
set
):
return
{
uuid_module
.
UUID
(
v
)
if
isinstance
(
v
,
str
)
else
v
for
v
in
val
}
return
val
def
_handle_map_value
(
map_message
)
->
Dict
[
str
,
Any
]:
"""Handle Map proto message containing map<string, Value> val."""
result
=
{}
for
key
,
value
in
map_message
.
val
.
items
():
# Recursively handle the Value message
result
[
key
]
=
feast_value_type_to_python_type
(
value
)
return
result
def
_handle_map_list_value
(
map_list_message
)
->
List
[
Dict
[
str
,
Any
]]:
"""Handle MapList proto message containing repeated Map val."""
result
=
[]
for
map_item
in
map_list_message
.
val
:
# Handle each Map in the list
processed_map
=
_handle_map_value
(
map_item
)
result
.
append
(
processed_map
)
return
result
def
_handle_nested_collection_value
(
repeated_value
)
->
List
[
Any
]:
"""Handle nested collection proto (RepeatedValue containing Values).
Each inner Value is itself a list/set proto. We recursively convert
each inner Value to a Python list/set via feast_value_type_to_python_type.
"""
result
=
[]
for
inner_value
in
repeated_value
.
val
:
result
.
append
(
feast_value_type_to_python_type
(
inner_value
))
return
result
def
_map_key_to_python_value
(
map_key
:
MapKey
)
->
Any
:
"""Convert a MapKey proto to its Python equivalent."""
key_attr
=
map_key
.
WhichOneof
(
"key"
)
if
key_attr
is
None
:
return
None
val
=
getattr
(
map_key
,
key_attr
)
if
key_attr
in
(
"int32_key"
,
"int64_key"
):
return
int
(
val
)
if
key_attr
in
(
"float_key"
,
"double_key"
):
return
float
(
val
)
if
key_attr
==
"bool_key"
:
return
bool
(
val
)
if
key_attr
==
"unix_timestamp_key"
:
return
(
datetime
.
fromtimestamp
(
val
,
tz
=
timezone
.
utc
)
if
val
!=
NULL_TIMESTAMP_INT_VALUE
else
None
)
if
key_attr
==
"bytes_key"
:
return
bytes
(
val
)
if
key_attr
in
(
"uuid_key"
,
"time_uuid_key"
):
return
uuid_module
.
UUID
(
val
)
if
key_attr
==
"decimal_key"
:
return
decimal
.
Decimal
(
val
)
return
val
def
_handle_scalar_map_value
(
value_map_message
:
ScalarMap
)
->
Dict
[
Any
,
Any
]:
"""Handle ScalarMap proto message (repeated ScalarMapEntry) → Python dict."""
result
:
Dict
[
Any
,
Any
]
=
{}
for
entry
in
value_map_message
.
val
:
key
=
_map_key_to_python_value
(
entry
.
key
)
value
=
feast_value_type_to_python_type
(
entry
.
value
)
result
[
key
]
=
value
return
result
def
feast_value_type_to_pandas_type
(
value_type
:
ValueType
)
->
Any
:
value_type_to_pandas_type
:
Dict
[
ValueType
,
str
]
=
{
ValueType
.
FLOAT
:
"float"
,
ValueType
.
INT32
:
"int"
,
ValueType
.
INT64
:
"int"
,
ValueType
.
STRING
:
"str"
,
ValueType
.
DOUBLE
:
"float"
,
ValueType
.
BYTES
:
"bytes"
,
ValueType
.
BOOL
:
"bool"
,
ValueType
.
UNIX_TIMESTAMP
:
"datetime64[ns]"
,
ValueType
.
UUID
:
"str"
,
ValueType
.
TIME_UUID
:
"str"
,
ValueType
.
DECIMAL
:
"object"
,
}
if
(
value_type
.
name
in
(
"MAP"
,
"JSON"
,
"STRUCT"
,
"VALUE_LIST"
,
"VALUE_SET"
)
or
value_type
.
name
.
endswith
(
"_LIST"
)
or
value_type
.
name
.
endswith
(
"_SET"
)
):
return
"object"
if
value_type
in
value_type_to_pandas_type
:
return
value_type_to_pandas_type
[
value_type
]
raise
TypeError
(
f"Casting to pandas type for type
{
value_type
}
failed. "
f"Type
{
value_type
}
not found"
)
def
python_type_to_feast_value_type
(
name
:
str
,
value
:
Optional
[
Any
]
=
None
,
recurse
:
bool
=
True
,
type_name
:
Optional
[
str
]
=
None
,
)
->
ValueType
:
"""
Finds the equivalent Feast Value Type for a Python value. Both native
and Pandas types are supported. This function will recursively look
for nested types when arrays are detected. All types must be homogenous.
Args:
name: Name of the value or field
value: Value that will be inspected
recurse: Whether to recursively look for nested types in arrays
Returns:
Feast Value Type
"""
type_name
=
(
type_name
or
type
(
value
).
__name__
).
lower
()
type_map
=
{
"int"
:
ValueType
.
INT64
,
"str"
:
ValueType
.
STRING
,
"string"
:
ValueType
.
STRING
,
# pandas.StringDtype
"float"
:
ValueType
.
DOUBLE
,
"bytes"
:
ValueType
.
BYTES
,
"float64"
:
ValueType
.
DOUBLE
,
"float32"
:
ValueType
.
FLOAT
,
"int64"
:
ValueType
.
INT64
,
"uint64"
:
ValueType
.
INT64
,
"int32"
:
ValueType
.
INT32
,
"uint32"
:
ValueType
.
INT32
,
"int16"
:
ValueType
.
INT32
,
"uint16"
:
ValueType
.
INT32
,
"uint8"
:
ValueType
.
INT32
,
"int8"
:
ValueType
.
INT32
,
"bool_"
:
ValueType
.
BOOL
,
# np.bool_
"bool"
:
ValueType
.
BOOL
,
"boolean"
:
ValueType
.
BOOL
,
"timedelta"
:
ValueType
.
UNIX_TIMESTAMP
,
"timestamp"
:
ValueType
.
UNIX_TIMESTAMP
,
"datetime"
:
ValueType
.
UNIX_TIMESTAMP
,
"datetime64[ns]"
:
ValueType
.
UNIX_TIMESTAMP
,
"datetime64[ns, tz]"
:
ValueType
.
UNIX_TIMESTAMP
,
# special dtype of pandas
"datetime64[ns, utc]"
:
ValueType
.
UNIX_TIMESTAMP
,
"date"
:
ValueType
.
UNIX_TIMESTAMP
,
"category"
:
ValueType
.
STRING
,
"uuid"
:
ValueType
.
UUID
,
"decimal"
:
ValueType
.
DECIMAL
,
}
if
type_name
in
type_map
:
return
type_map
[
type_name
]
# Handle pandas "object" dtype by inspecting the actual value
if
type_name
==
"object"
and
value
is
not
None
:
# Check the actual type of the value
actual_type
=
type
(
value
).
__name__
.
lower
()
if
actual_type
==
"str"
:
return
ValueType
.
STRING
# Check if it's a dictionary (could be a Map)
elif
actual_type
==
"dict"
:
return
ValueType
.
MAP
# If it's a different type wrapped in object, try to infer from the value
elif
actual_type
in
type_map
:
return
type_map
[
actual_type
]
if
isinstance
(
value
,
np
.
ndarray
)
and
str
(
value
.
dtype
)
in
type_map
:
item_type
=
type_map
[
str
(
value
.
dtype
)]
return
ValueType
[
item_type
.
name
+
"_LIST"
]
if
isinstance
(
value
, (
list
,
np
.
ndarray
)):
# Check if it's a list of maps
if
value
and
isinstance
(
value
[
0
],
dict
):
return
ValueType
.
MAP_LIST
# if the value's type is "ndarray" and we couldn't infer from "value.dtype"
# this is most probably array of "object",
# so we need to iterate over objects and try to infer type of each item
if
not
recurse
:
raise
ValueError
(
f"Value type for field
{
name
}
is
{
type
(
value
)
}
but "
f"recursion is not allowed. Nested collection types cannot be "
f"inferred automatically; use an explicit Field dtype instead "
f"(e.g., dtype=Array(Array(Int32)))."
)
# This is the final type which we infer from the list
common_item_value_type
=
None
for
item
in
value
:
if
isinstance
(
item
,
ProtoValue
):
current_item_value_type
:
ValueType
=
_proto_value_to_value_type
(
item
)
else
:
# Get the type from the current item, only one level deep
current_item_value_type
=
python_type_to_feast_value_type
(
name
=
name
,
value
=
item
,
recurse
=
False
)
# Validate whether the type stays consistent
if
(
common_item_value_type
and
not
common_item_value_type
==
current_item_value_type
):
raise
ValueError
(
f"List value type for field
{
name
}
is inconsistent. "
f"
{
common_item_value_type
}
different from "
f"
{
current_item_value_type
}
."
)
common_item_value_type
=
current_item_value_type
if
common_item_value_type
is
None
:
return
ValueType
.
UNKNOWN
return
ValueType
[
common_item_value_type
.
name
+
"_LIST"
]
# Check if it's a set (Set type)
if
isinstance
(
value
,
set
):
if
not
recurse
:
raise
ValueError
(
f"Value type for field
{
name
}
is
{
type
(
value
)
}
but "
f"recursion is not allowed. Set types can only be one level "
f"deep."
)
# Infer the type from set elements
common_set_item_type
=
None
for
item
in
value
:
if
isinstance
(
item
,
ProtoValue
):
current_set_item_type
:
ValueType
=
_proto_value_to_value_type
(
item
)
else
:
# Get the type from the current item, only one level deep
current_set_item_type
=
python_type_to_feast_value_type
(
name
=
name
,
value
=
item
,
recurse
=
False
)
# Validate whether the type stays consistent
if
(
common_set_item_type
and
not
common_set_item_type
==
current_set_item_type
):
raise
ValueError
(
f"Set value type for field
{
name
}
is inconsistent. "
f"
{
common_set_item_type
}
different from "
f"
{
current_set_item_type
}
."
)
common_set_item_type
=
current_set_item_type
if
common_set_item_type
is
None
:
return
ValueType
.
UNKNOWN
return
ValueType
[
common_set_item_type
.
name
+
"_SET"
]
# Check if it's a dictionary (Map type)
if
isinstance
(
value
,
dict
):
# Non-string keys require ScalarMap; string keys (or empty dict) use Map
if
value
and
not
isinstance
(
next
(
iter
(
value
)),
str
):
return
ValueType
.
SCALAR_MAP
return
ValueType
.
MAP
raise
ValueError
(
f"Value with native type
{
type_name
}
cannot be converted into Feast value type"
)
def
python_values_to_feast_value_type
(
name
:
str
,
values
:
Any
,
recurse
:
bool
=
True
)
->
ValueType
:
inferred_dtype
=
ValueType
.
UNKNOWN
for
row
in
values
:
current_dtype
=
python_type_to_feast_value_type
(
name
,
value
=
row
,
recurse
=
recurse
)
if
inferred_dtype
is
ValueType
.
UNKNOWN
:
inferred_dtype
=
current_dtype
else
:
if
current_dtype
!=
inferred_dtype
and
current_dtype
not
in
(
ValueType
.
UNKNOWN
,
ValueType
.
NULL
,
):
raise
TypeError
(
f"Input entity
{
name
}
has mixed types,
{
current_dtype
}
and
{
inferred_dtype
}
. That is not allowed. "
)
if
inferred_dtype
in
(
ValueType
.
UNKNOWN
,
ValueType
.
NULL
):
raise
ValueError
(
f"field
{
name
}
cannot have all null values for type inference."
)
return
inferred_dtype
def
_convert_value_type_str_to_value_type
(
type_str
:
str
)
->
ValueType
:
type_map
=
{
"UNKNOWN"
:
ValueType
.
UNKNOWN
,
"BYTES"
:
ValueType
.
BYTES
,
"STRING"
:
ValueType
.
STRING
,
"INT32"
:
ValueType
.
INT32
,
"INT64"
:
ValueType
.
INT64
,
"DOUBLE"
:
ValueType
.
DOUBLE
,
"FLOAT"
:
ValueType
.
FLOAT
,
"FLOAT32"
:
ValueType
.
FLOAT
,
"BOOL"
:
ValueType
.
BOOL
,
"NULL"
:
ValueType
.
NULL
,
"UNIX_TIMESTAMP"
:
ValueType
.
UNIX_TIMESTAMP
,
"BYTES_LIST"
:
ValueType
.
BYTES_LIST
,
"STRING_LIST"
:
ValueType
.
STRING_LIST
,
"INT32_LIST "
:
ValueType
.
INT32_LIST
,
"INT64_LIST"
:
ValueType
.
INT64_LIST
,
"DOUBLE_LIST"
:
ValueType
.
DOUBLE_LIST
,
"FLOAT_LIST"
:
ValueType
.
FLOAT_LIST
,
"BOOL_LIST"
:
ValueType
.
BOOL_LIST
,
"UNIX_TIMESTAMP_LIST"
:
ValueType
.
UNIX_TIMESTAMP_LIST
,
"MAP"
:
ValueType
.
MAP
,
"MAP_LIST"
:
ValueType
.
MAP_LIST
,
"JSON"
:
ValueType
.
JSON
,
"JSON_LIST"
:
ValueType
.
JSON_LIST
,
"STRUCT"
:
ValueType
.
STRUCT
,
"STRUCT_LIST"
:
ValueType
.
STRUCT_LIST
,
"BYTES_SET"
:
ValueType
.
BYTES_SET
,
"STRING_SET"
:
ValueType
.
STRING_SET
,
"INT32_SET"
:
ValueType
.
INT32_SET
,
"INT64_SET"
:
ValueType
.
INT64_SET
,
"DOUBLE_SET"
:
ValueType
.
DOUBLE_SET
,
"FLOAT_SET"
:
ValueType
.
FLOAT_SET
,
"BOOL_SET"
:
ValueType
.
BOOL_SET
,
"UNIX_TIMESTAMP_SET"
:
ValueType
.
UNIX_TIMESTAMP_SET
,
"UUID"
:
ValueType
.
UUID
,
"TIME_UUID"
:
ValueType
.
TIME_UUID
,
"UUID_LIST"
:
ValueType
.
UUID_LIST
,
"TIME_UUID_LIST"
:
ValueType
.
TIME_UUID_LIST
,
"UUID_SET"
:
ValueType
.
UUID_SET
,
"TIME_UUID_SET"
:
ValueType
.
TIME_UUID_SET
,
"VALUE_LIST"
:
ValueType
.
VALUE_LIST
,
"VALUE_SET"
:
ValueType
.
VALUE_SET
,
"DECIMAL"
:
ValueType
.
DECIMAL
,
"DECIMAL_LIST"
:
ValueType
.
DECIMAL_LIST
,
"DECIMAL_SET"
:
ValueType
.
DECIMAL_SET
,
"SCALAR_MAP"
:
ValueType
.
SCALAR_MAP
,
"ZONED_TIMESTAMP"
:
ValueType
.
ZONED_TIMESTAMP
,
}
return
type_map
.
get
(
type_str
,
ValueType
.
STRING
)
def
_type_err
(
item
,
dtype
):
raise
TypeError
(
f'Value "
{
item
}
" is of type
{
type
(
item
)
}
not of type
{
dtype
}
'
)
PYTHON_LIST_VALUE_TYPE_TO_PROTO_VALUE
:
Dict
[
ValueType
,
Tuple
[
ListType
,
str
,
List
[
Type
]]
]
=
{
ValueType
.
FLOAT_LIST
: (
FloatList
,
"float_list_val"
,
[
np
.
float32
,
np
.
float64
,
float
],
),
ValueType
.
DOUBLE_LIST
: (
DoubleList
,
"double_list_val"
,
[
np
.
float64
,
np
.
float32
,
float
],
),
ValueType
.
INT32_LIST
: (
Int32List
,
"int32_list_val"
, [
np
.
int64
,
np
.
int32
,
int
]),
ValueType
.
INT64_LIST
: (
Int64List
,
"int64_list_val"
, [
np
.
int64
,
np
.
int32
,
int
]),
ValueType
.
UNIX_TIMESTAMP_LIST
: (
Int64List
,
"int64_list_val"
,
[
np
.
datetime64
,
np
.
int64
,
np
.
int32
,
int
,
datetime
,
Timestamp
],
),
ValueType
.
STRING_LIST
: (
StringList
,
"string_list_val"
, [
np
.
str_
,
str
]),
ValueType
.
BOOL_LIST
: (
BoolList
,
"bool_list_val"
, [
np
.
bool_
,
bool
]),
ValueType
.
BYTES_LIST
: (
BytesList
,
"bytes_list_val"
, [
np
.
bytes_
,
bytes
]),
ValueType
.
UUID_LIST
: (
StringList
,
"uuid_list_val"
,
[
np
.
str_
,
str
,
uuid_module
.
UUID
],
),
ValueType
.
TIME_UUID_LIST
: (
StringList
,
"time_uuid_list_val"
,
[
np
.
str_
,
str
,
uuid_module
.
UUID
],
),
ValueType
.
DECIMAL_LIST
: (
StringList
,
"decimal_list_val"
,
[
np
.
str_
,
str
,
decimal
.
Decimal
],
),
}
PYTHON_SET_VALUE_TYPE_TO_PROTO_VALUE
:
Dict
[
ValueType
,
Tuple
[
SetType
,
str
,
List
[
Type
]]
]
=
{
ValueType
.
FLOAT_SET
: (
FloatSet
,
"float_set_val"
,
[
np
.
float32
,
np
.
float64
,
float
],
),
ValueType
.
DOUBLE_SET
: (
DoubleSet
,
"double_set_val"
,
[
np
.
float64
,
np
.
float32
,
float
],
),
ValueType
.
INT32_SET
: (
Int32Set
,
"int32_set_val"
, [
np
.
int64
,
np
.
int32
,
int
]),
ValueType
.
INT64_SET
: (
Int64Set
,
"int64_set_val"
, [
np
.
int64
,
np
.
int32
,
int
]),
ValueType
.
UNIX_TIMESTAMP_SET
: (
Int64Set
,
"unix_timestamp_set_val"
,
[
np
.
datetime64
,
np
.
int64
,
np
.
int32
,
int
,
datetime
,
Timestamp
],
),
ValueType
.
STRING_SET
: (
StringSet
,
"string_set_val"
, [
np
.
str_
,
str
]),
ValueType
.
BOOL_SET
: (
BoolSet
,
"bool_set_val"
, [
np
.
bool_
,
bool
]),
ValueType
.
BYTES_SET
: (
BytesSet
,
"bytes_set_val"
, [
np
.
bytes_
,
bytes
]),
ValueType
.
UUID_SET
: (
StringSet
,
"uuid_set_val"
, [
np
.
str_
,
str
,
uuid_module
.
UUID
]),
ValueType
.
TIME_UUID_SET
: (
StringSet
,
"time_uuid_set_val"
,
[
np
.
str_
,
str
,
uuid_module
.
UUID
],
),
ValueType
.
DECIMAL_SET
: (
StringSet
,
"decimal_set_val"
,
[
np
.
str_
,
str
,
decimal
.
Decimal
],
),
}
PYTHON_SCALAR_VALUE_TYPE_TO_PROTO_VALUE
:
Dict
[
ValueType
,
Tuple
[
str
,
Any
,
Optional
[
Set
[
Type
]]]
]
=
{
ValueType
.
INT32
: (
"int32_val"
,
lambda
x
:
int
(
x
),
None
),
ValueType
.
INT64
: (
"int64_val"
,
lambda
x
: (
int
(
x
.
timestamp
())
if
isinstance
(
x
,
pd
.
_libs
.
tslibs
.
timestamps
.
Timestamp
)
else
int
(
x
)
),
None
,
),
ValueType
.
FLOAT
: (
"float_val"
,
lambda
x
:
float
(
x
),
None
),
ValueType
.
DOUBLE
: (
"double_val"
,
lambda
x
:
x
,
{
float
,
np
.
float64
,
int
,
np
.
int_
,
decimal
.
Decimal
},
),
ValueType
.
STRING
: (
"string_val"
,
lambda
x
:
str
(
x
),
None
),
ValueType
.
BYTES
: (
"bytes_val"
,
lambda
x
:
x
, {
bytes
}),
ValueType
.
IMAGE_BYTES
: (
"bytes_val"
,
lambda
x
:
x
, {
bytes
}),
ValueType
.
BOOL
: (
"bool_val"
,
lambda
x
:
x
, {
bool
,
np
.
bool_
,
int
,
np
.
int_
}),
ValueType
.
UUID
: (
"uuid_val"
,
lambda
x
:
str
(
x
), {
str
,
uuid_module
.
UUID
}),
ValueType
.
TIME_UUID
: (
"time_uuid_val"
,
lambda
x
:
str
(
x
), {
str
,
uuid_module
.
UUID
}),
ValueType
.
DECIMAL
: (
"decimal_val"
,
lambda
x
:
str
(
x
), {
decimal
.
Decimal
,
str
}),
}
def
_python_datetime_to_int_timestamp
(
values
:
Sequence
[
Any
],
)
->
Sequence
[
Union
[
int
,
np
.
int_
]]:
# Fast path for Numpy array.
if
isinstance
(
values
,
np
.
ndarray
)
and
isinstance
(
values
.
dtype
,
np
.
datetime64
):
if
values
.
ndim
!=
1
:
raise
ValueError
(
"Only 1 dimensional arrays are supported."
)
return
cast
(
Sequence
[
np
.
int_
],
values
.
astype
(
"datetime64[s]"
).
astype
(
np
.
int_
))
int_timestamps
=
[]
for
value
in
values
:
if
isinstance
(
value
,
datetime
):
int_timestamps
.
append
(
int
(
value
.
timestamp
()))
elif
isinstance
(
value
,
Timestamp
):
int_timestamps
.
append
(
int
(
value
.
ToSeconds
()))
elif
isinstance
(
value
,
np
.
datetime64
):
int_timestamps
.
append
(
value
.
astype
(
"datetime64[s]"
).
astype
(
np
.
int_
))
# type: ignore[attr-defined]
elif
isinstance
(
value
,
type
(
np
.
nan
)):
int_timestamps
.
append
(
NULL_TIMESTAMP_INT_VALUE
)
else
:
int_timestamps
.
append
(
int
(
value
))
return
int_timestamps
def
_convert_timestamp_collection_to_proto
(
values
:
List
[
Any
],
proto_field
:
str
,
proto_type
:
type
,
)
->
List
[
ProtoValue
]:
"""Convert timestamp collection values (list or set) to proto.
Args:
values: List of timestamp collections to convert.
proto_field: The proto field name (e.g., 'unix_timestamp_list_val').
proto_type: The proto type class (e.g., Int64List).
Returns:
List of ProtoValue with converted timestamps.
"""
result
=
[]
for
value
in
values
:
if
value
is
not
None
:
result
.
append
(
ProtoValue
(
**
{
proto_field
:
proto_type
(
val
=
_python_datetime_to_int_timestamp
(
value
)
)
}
# type: ignore
)
)
else
:
result
.
append
(
ProtoValue
())
return
result
def
_convert_bool_collection_to_proto
(
values
:
List
[
Any
],
proto_field
:
str
,
proto_type
:
type
,
)
->
List
[
ProtoValue
]:
"""Convert boolean collection values (list or set) to proto.
ProtoValue does not support direct conversion of np.bool_, so we need to
explicitly convert each element to Python bool.
Args:
values: List of boolean collections to convert.
proto_field: The proto field name (e.g., 'bool_list_val').
proto_type: The proto type class (e.g., BoolList).
Returns:
List of ProtoValue with converted booleans.
"""
result
=
[]
for
value
in
values
:
if
value
is
not
None
:
result
.
append
(
ProtoValue
(
**
{
proto_field
:
proto_type
(
val
=
[
bool
(
e
)
for
e
in
value
])})
# type: ignore
)
else
:
result
.
append
(
ProtoValue
())
return
result
def
_validate_collection_item_types
(
sample
:
Any
,
valid_types
:
List
[
Type
],
feast_value_type
:
ValueType
,
)
->
None
:
"""Validate that collection items match expected types.
Args:
sample: A sample collection value to check.
valid_types: List of valid Python types for items.
feast_value_type: The Feast value type for error messages.
Raises:
TypeError: If any item in sample is not a valid type.
"""
if
sample
is
None
:
return
if
all
(
type
(
item
)
in
valid_types
for
item
in
sample
if
item
is
not
None
):
return
# to_numpy() upcasts INT32/INT64 with NULL to Float64 automatically
int_collection_types
=
[
ValueType
.
INT32_LIST
,
ValueType
.
INT64_LIST
,
ValueType
.
INT32_SET
,
ValueType
.
INT64_SET
,
]
for
item
in
sample
:
if
item
is
None
:
continue
# None elements in STRING_LIST are replaced with ""; for other types they are dropped
if
type
(
item
)
not
in
valid_types
:
if
feast_value_type
in
int_collection_types
:
# Check if the float values are due to NULL upcast
if
not
any
(
np
.
isnan
(
i
)
for
i
in
sample
if
isinstance
(
i
,
float
)):
logger
.
error
(
f"
{
feast_value_type
.
name
}
has NULL values. to_numpy() upcasts to Float64 automatically."
)
raise
_type_err
(
item
,
valid_types
[
0
])
def
_python_set_to_proto_values
(
feast_value_type
:
ValueType
,
values
:
List
[
Any
]
)
->
List
[
ProtoValue
]:
"""
Converts Python set values to Feast Proto Values.
Args:
feast_value_type: The target set value type
values: List of set values that will be converted
Returns:
List of Feast Value Proto
"""
# Feature can be set but None is still valid
if
feast_value_type
not
in
PYTHON_SET_VALUE_TYPE_TO_PROTO_VALUE
:
return
[]
set_proto_type
,
set_field_name
,
set_valid_types
=
(
PYTHON_SET_VALUE_TYPE_TO_PROTO_VALUE
[
feast_value_type
]
)
# Convert set to list for proto (proto doesn't have native set type)
def
convert_set_to_list
(
value
:
Any
)
->
Any
:
if
value
is
None
:
return
None
if
isinstance
(
value
,
set
):
return
list
(
value
)
if
isinstance
(
value
, (
list
,
tuple
,
np
.
ndarray
)):
return
list
(
set
(
value
))
return
value
converted_values
=
[
convert_set_to_list
(
v
)
for
v
in
values
]
sample
=
next
(
filter
(
_non_empty_value
,
converted_values
),
None
)
# Bytes to array type conversion
if
isinstance
(
sample
, (
bytes
,
bytearray
)):
if
feast_value_type
==
ValueType
.
BYTES_SET
:
raise
_type_err
(
sample
,
ValueType
.
BYTES_SET
)
json_sample
=
json
.
loads
(
sample
)
if
isinstance
(
json_sample
,
list
):
json_values
=
[
json
.
loads
(
value
)
if
value
is
not
None
else
None
for
value
in
converted_values
]
if
feast_value_type
==
ValueType
.
BOOL_SET
:
json_values
=
[
[
bool
(
item
)
for
item
in
list_item
]
if
list_item
is
not
None
else
None
for
list_item
in
json_values
]
return
[
ProtoValue
(
**
{
set_field_name
:
set_proto_type
(
val
=
v
)})
# type: ignore[arg-type]
if
v
is
not
None
else
ProtoValue
()
for
v
in
json_values
]
raise
_type_err
(
sample
,
set_valid_types
[
0
])
# Validate item types using shared helper
_validate_collection_item_types
(
sample
,
set_valid_types
,
feast_value_type
)
# Handle special types using shared helpers
if
feast_value_type
==
ValueType
.
UNIX_TIMESTAMP_SET
:
return
_convert_timestamp_collection_to_proto
(
converted_values
,
"unix_timestamp_set_val"
,
Int64Set
)
if
feast_value_type
==
ValueType
.
BOOL_SET
:
return
_convert_bool_collection_to_proto
(
converted_values
,
set_field_name
,
set_proto_type
)
if
feast_value_type
in
(
ValueType
.
UUID_SET
,
ValueType
.
TIME_UUID_SET
):
# uuid.UUID objects must be converted to str for StringSet proto.
return
[
(
ProtoValue
(
**
{
set_field_name
:
set_proto_type
(
val
=
[
str
(
e
)
for
e
in
value
])}
# type: ignore[arg-type, misc]
)
if
value
is
not
None
else
ProtoValue
()
)
for
value
in
converted_values
]
if
feast_value_type
==
ValueType
.
DECIMAL_SET
:
# decimal.Decimal objects must be converted to str for StringSet proto.
return
[
(
ProtoValue
(
**
{
set_field_name
:
set_proto_type
(
val
=
[
str
(
e
)
for
e
in
value
])}
# type: ignore[arg-type, misc]
)
if
value
is
not
None
else
ProtoValue
()
)
for
value
in
converted_values
]
# Generic set conversion
return
[
ProtoValue
(
**
{
set_field_name
:
set_proto_type
(
val
=
value
)})
# type: ignore[arg-type]
if
value
is
not
None
else
ProtoValue
()
for
value
in
converted_values
]
# Per-type default values substituted for None elements inside list columns.
# Protobuf repeated fields do not accept None, so we replace with a
# type-appropriate zero/empty value.
_LIST_NONE_DEFAULTS
:
Dict
[
ValueType
,
Any
]
=
{
ValueType
.
STRING_LIST
:
""
,
ValueType
.
BYTES_LIST
:
b""
,
ValueType
.
INT32_LIST
:
0
,
ValueType
.
INT64_LIST
:
0
,
ValueType
.
FLOAT_LIST
:
0.0
,
ValueType
.
DOUBLE_LIST
:
0.0
,
ValueType
.
BOOL_LIST
:
False
,
ValueType
.
UNIX_TIMESTAMP_LIST
:
NULL_TIMESTAMP_INT_VALUE
,
ValueType
.
UUID_LIST
:
""
,
ValueType
.
TIME_UUID_LIST
:
""
,
ValueType
.
DECIMAL_LIST
:
""
,
}
def
_sanitize_list_value
(
value
:
Any
,
feast_value_type
:
ValueType
)
->
Any
:
"""Convert ndarray to list and replace None elements with a type-appropriate default.
Arrow/Athena may deserialize array columns as numpy.ndarray with object dtype
instead of plain Python lists. Protobuf repeated fields do not accept ndarrays
or None elements, so we normalise here before building proto messages.
"""
if
isinstance
(
value
,
np
.
ndarray
):
value
=
value
.
tolist
()
none_default
=
_LIST_NONE_DEFAULTS
.
get
(
feast_value_type
)
if
none_default
is
not
None
and
isinstance
(
value
,
list
):
value
=
[
none_default
if
v
is
None
else
v
for
v
in
value
]
return
value
def
_convert_list_values_to_proto
(
feast_value_type
:
ValueType
,
values
:
List
[
Any
],
sample
:
Any
,
)
->
List
[
ProtoValue
]:
"""Convert list-type values to proto.
Args:
feast_value_type: The target list value type.
values: List of list values to convert.
sample: First non-empty value for type checking.
Returns:
List of ProtoValue.
"""
if
feast_value_type
not
in
PYTHON_LIST_VALUE_TYPE_TO_PROTO_VALUE
:
raise
Exception
(
f"Unsupported list type:
{
feast_value_type
}
"
)
proto_type
,
field_name
,
valid_types
=
PYTHON_LIST_VALUE_TYPE_TO_PROTO_VALUE
[
feast_value_type
]
values
=
[
_sanitize_list_value
(
v
,
feast_value_type
)
if
v
is
not
None
else
v
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL