-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_type_map.py
More file actions
1417 lines (1092 loc) · 55.1 KB
/
test_type_map.py
File metadata and controls
1417 lines (1092 loc) · 55.1 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
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
import numpy as np
import pandas as pd
import pyarrow
import pytest
from feast.protos.feast.types.Value_pb2 import Map, MapList
from feast.type_map import (
_convert_value_type_str_to_value_type,
_python_dict_to_map_proto,
_python_list_to_map_list_proto,
arrow_to_pg_type,
feast_value_type_to_pa,
feast_value_type_to_python_type,
pa_to_feast_value_type,
pa_to_redshift_value_type,
pg_type_to_feast_value_type,
python_type_to_feast_value_type,
python_values_to_proto_values,
redshift_to_feast_value_type,
snowflake_type_to_feast_value_type,
spark_to_feast_value_type,
)
from feast.types import Array, from_feast_to_pyarrow_type
from feast.types import Map as FeastMap
from feast.value_type import ValueType
def test_null_unix_timestamp():
"""Test that null UnixTimestamps get converted from proto correctly."""
data = np.array(["NaT"], dtype="datetime64")
protos = python_values_to_proto_values(data, ValueType.UNIX_TIMESTAMP)
converted = feast_value_type_to_python_type(protos[0])
assert converted is None
def test_null_unix_timestamp_list():
"""Test that UnixTimestamp lists with a null get converted from proto
correctly."""
data = np.array([["NaT"]], dtype="datetime64")
protos = python_values_to_proto_values(data, ValueType.UNIX_TIMESTAMP_LIST)
converted = feast_value_type_to_python_type(protos[0])
assert converted[0] is None
@pytest.mark.parametrize(
"values",
(
np.array([True]),
np.array([False]),
np.array([0]),
np.array([1]),
[True],
[False],
[0],
[1],
),
)
def test_python_values_to_proto_values_bool(values):
protos = python_values_to_proto_values(values, ValueType.BOOL)
converted = feast_value_type_to_python_type(protos[0])
assert converted is bool(values[0])
@pytest.mark.parametrize(
"values, value_type, expected",
(
(np.array([b"[1,2,3]"]), ValueType.INT64_LIST, [1, 2, 3]),
(np.array([b"[1,2,3]"]), ValueType.INT32_LIST, [1, 2, 3]),
(np.array([b"[1.5,2.5,3.5]"]), ValueType.FLOAT_LIST, [1.5, 2.5, 3.5]),
(np.array([b"[1.5,2.5,3.5]"]), ValueType.DOUBLE_LIST, [1.5, 2.5, 3.5]),
(np.array([b'["a","b","c"]']), ValueType.STRING_LIST, ["a", "b", "c"]),
(np.array([b"[true,false]"]), ValueType.BOOL_LIST, [True, False]),
(np.array([b"[1,0]"]), ValueType.BOOL_LIST, [True, False]),
(np.array([None]), ValueType.INT32_LIST, None),
(np.array([None]), ValueType.INT64_LIST, None),
(np.array([None]), ValueType.FLOAT_LIST, None),
(np.array([None]), ValueType.DOUBLE_LIST, None),
(np.array([None]), ValueType.BOOL_LIST, None),
(np.array([None]), ValueType.BYTES_LIST, None),
(np.array([None]), ValueType.STRING_LIST, None),
(np.array([None]), ValueType.UNIX_TIMESTAMP_LIST, None),
([b"[1,2,3]"], ValueType.INT64_LIST, [1, 2, 3]),
([b"[1,2,3]"], ValueType.INT32_LIST, [1, 2, 3]),
([b"[1.5,2.5,3.5]"], ValueType.FLOAT_LIST, [1.5, 2.5, 3.5]),
([b"[1.5,2.5,3.5]"], ValueType.DOUBLE_LIST, [1.5, 2.5, 3.5]),
([b'["a","b","c"]'], ValueType.STRING_LIST, ["a", "b", "c"]),
([b"[true,false]"], ValueType.BOOL_LIST, [True, False]),
([b"[1,0]"], ValueType.BOOL_LIST, [True, False]),
([None], ValueType.STRING_LIST, None),
),
)
def test_python_values_to_proto_values_bytes_to_list(values, value_type, expected):
protos = python_values_to_proto_values(values, value_type)
converted = feast_value_type_to_python_type(protos[0])
assert converted == expected
def test_python_values_to_proto_values_bytes_to_list_not_supported():
with pytest.raises(TypeError):
_ = python_values_to_proto_values([b"[]"], ValueType.BYTES_LIST)
def test_python_values_to_proto_values_int_list_with_null_not_supported():
df = pd.DataFrame({"column": [1, 2, None]})
arr = df["column"].to_numpy()
with pytest.raises(TypeError):
_ = python_values_to_proto_values(arr, ValueType.INT32_LIST)
class TestMapTypes:
"""Test cases for MAP and MAP_LIST value types."""
def test_simple_map_conversion(self):
"""Test basic MAP type conversion from Python dict to proto and back."""
test_dict = {"key1": "value1", "key2": "value2", "key3": 123}
protos = python_values_to_proto_values([test_dict], ValueType.MAP)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, dict)
assert converted["key1"] == "value1"
assert converted["key2"] == "value2"
assert converted["key3"] == 123
def test_nested_map_conversion(self):
"""Test nested MAP type conversion."""
test_dict = {
"level1": {
"level2": {"key": "deep_value", "number": 42},
"simple": "value",
},
"top_level": "top_value",
}
protos = python_values_to_proto_values([test_dict], ValueType.MAP)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, dict)
assert converted["level1"]["level2"]["key"] == "deep_value"
assert converted["level1"]["level2"]["number"] == 42
assert converted["level1"]["simple"] == "value"
assert converted["top_level"] == "top_value"
def test_map_with_different_value_types(self):
"""Test MAP with various value types."""
test_dict = {
"string_val": "hello",
"int_val": 42,
"float_val": 3.14,
"bool_val": True,
"list_val": [1, 2, 3],
"string_list_val": ["a", "b", "c"],
}
protos = python_values_to_proto_values([test_dict], ValueType.MAP)
converted = feast_value_type_to_python_type(protos[0])
assert converted["string_val"] == "hello"
assert converted["int_val"] == 42
assert converted["float_val"] == 3.14
assert converted["bool_val"] is True
assert converted["list_val"] == [1, 2, 3]
assert converted["string_list_val"] == ["a", "b", "c"]
def test_map_with_none_values(self):
"""Test MAP with None values."""
test_dict = {"key1": "value1", "key2": None, "key3": "value3"}
protos = python_values_to_proto_values([test_dict], ValueType.MAP)
converted = feast_value_type_to_python_type(protos[0])
assert converted["key1"] == "value1"
assert converted["key2"] is None
assert converted["key3"] == "value3"
def test_empty_map(self):
"""Test empty MAP conversion."""
test_dict = {}
protos = python_values_to_proto_values([test_dict], ValueType.MAP)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, dict)
assert len(converted) == 0
def test_null_map(self):
"""Test None MAP conversion."""
protos = python_values_to_proto_values([None], ValueType.MAP)
converted = feast_value_type_to_python_type(protos[0])
assert converted is None
def test_map_list_conversion(self):
"""Test basic MAP_LIST type conversion."""
test_list = [
{"name": "John", "age": 30},
{"name": "Jane", "age": 25},
{"name": "Bob", "score": 85.5},
]
protos = python_values_to_proto_values([test_list], ValueType.MAP_LIST)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, list)
assert len(converted) == 3
assert converted[0]["name"] == "John"
assert converted[0]["age"] == 30
assert converted[1]["name"] == "Jane"
assert converted[1]["age"] == 25
assert converted[2]["name"] == "Bob"
assert converted[2]["score"] == 85.5
def test_map_list_with_nested_maps(self):
"""Test MAP_LIST with nested maps."""
test_list = [
{"user": {"name": "John", "details": {"city": "NYC"}}, "score": 100},
{"user": {"name": "Jane", "details": {"city": "SF"}}, "score": 95},
]
protos = python_values_to_proto_values([test_list], ValueType.MAP_LIST)
converted = feast_value_type_to_python_type(protos[0])
assert len(converted) == 2
assert converted[0]["user"]["name"] == "John"
assert converted[0]["user"]["details"]["city"] == "NYC"
assert converted[1]["user"]["name"] == "Jane"
assert converted[1]["user"]["details"]["city"] == "SF"
def test_map_list_with_lists_in_maps(self):
"""Test MAP_LIST where maps contain lists."""
test_list = [
{"name": "John", "hobbies": ["reading", "swimming"]},
{"name": "Jane", "scores": [95, 87, 92]},
]
protos = python_values_to_proto_values([test_list], ValueType.MAP_LIST)
converted = feast_value_type_to_python_type(protos[0])
assert converted[0]["name"] == "John"
assert converted[0]["hobbies"] == ["reading", "swimming"]
assert converted[1]["name"] == "Jane"
assert converted[1]["scores"] == [95, 87, 92]
def test_empty_map_list(self):
"""Test empty MAP_LIST conversion."""
test_list = []
protos = python_values_to_proto_values([test_list], ValueType.MAP_LIST)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, list)
assert len(converted) == 0
def test_null_map_list(self):
"""Test None MAP_LIST conversion."""
protos = python_values_to_proto_values([None], ValueType.MAP_LIST)
converted = feast_value_type_to_python_type(protos[0])
assert converted is None
def test_map_list_with_empty_maps(self):
"""Test MAP_LIST containing empty maps."""
test_list = [{}, {"key": "value"}, {}]
protos = python_values_to_proto_values([test_list], ValueType.MAP_LIST)
converted = feast_value_type_to_python_type(protos[0])
assert len(converted) == 3
assert len(converted[0]) == 0
assert converted[1]["key"] == "value"
assert len(converted[2]) == 0
def test_python_type_inference_for_map(self):
"""Test that dictionaries are correctly inferred as MAP type."""
test_dict = {"key1": "value1", "key2": 123}
inferred_type = python_type_to_feast_value_type("test_field", test_dict)
assert inferred_type == ValueType.MAP
def test_python_type_inference_for_map_list(self):
"""Test that list of dictionaries is correctly inferred as MAP_LIST type."""
test_list = [{"key1": "value1"}, {"key2": "value2"}]
inferred_type = python_type_to_feast_value_type("test_field", test_list)
assert inferred_type == ValueType.MAP_LIST
def test_multiple_map_values(self):
"""Test conversion of multiple MAP values."""
test_dicts = [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
{"name": "Charlie", "city": "NYC"},
]
protos = python_values_to_proto_values(test_dicts, ValueType.MAP)
converted_values = [feast_value_type_to_python_type(proto) for proto in protos]
assert len(converted_values) == 3
assert converted_values[0]["name"] == "Alice"
assert converted_values[1]["name"] == "Bob"
assert converted_values[2]["city"] == "NYC"
def test_multiple_map_list_values(self):
"""Test conversion of multiple MAP_LIST values."""
test_lists = [[{"id": 1}, {"id": 2}], [{"id": 3}, {"id": 4}], []]
protos = python_values_to_proto_values(test_lists, ValueType.MAP_LIST)
converted_values = [feast_value_type_to_python_type(proto) for proto in protos]
assert len(converted_values) == 3
assert len(converted_values[0]) == 2
assert converted_values[0][0]["id"] == 1
assert len(converted_values[2]) == 0
def test_map_with_map_list_value(self):
"""Test MAP containing MAP_LIST as a value."""
test_dict = {
"metadata": {"version": "1.0"},
"items": [{"name": "item1", "count": 5}, {"name": "item2", "count": 3}],
}
protos = python_values_to_proto_values([test_dict], ValueType.MAP)
converted = feast_value_type_to_python_type(protos[0])
assert converted["metadata"]["version"] == "1.0"
assert len(converted["items"]) == 2
assert converted["items"][0]["name"] == "item1"
assert converted["items"][1]["count"] == 3
@pytest.mark.parametrize(
"invalid_value",
[
[{"key": "value"}, "not_a_dict", {"another": "dict"}],
["string1", "string2"],
[1, 2, 3],
],
)
def test_map_list_with_invalid_items(self, invalid_value):
"""Test that MAP_LIST with non-dict items raises appropriate errors."""
with pytest.raises((ValueError, TypeError)):
python_values_to_proto_values([invalid_value], ValueType.MAP_LIST)
def test_direct_proto_construction(self):
"""Test direct construction of Map and MapList proto messages."""
# Test Map proto construction
test_dict = {"key1": "value1", "key2": 42}
map_proto = _python_dict_to_map_proto(test_dict)
assert isinstance(map_proto, Map)
assert len(map_proto.val) == 2
# Test MapList proto construction
test_list = [{"a": 1}, {"b": 2}]
map_list_proto = _python_list_to_map_list_proto(test_list)
assert isinstance(map_list_proto, MapList)
assert len(map_list_proto.val) == 2
def test_roundtrip_conversion_consistency(self):
"""Test that roundtrip conversion maintains data integrity."""
original_map = {
"string": "hello",
"integer": 42,
"float": 3.14159,
"boolean": True,
"nested": {"inner_string": "world", "inner_list": [1, 2, 3]},
"list_of_maps": [{"item": "first"}, {"item": "second"}],
}
# Convert to proto and back
protos = python_values_to_proto_values([original_map], ValueType.MAP)
converted = feast_value_type_to_python_type(protos[0])
# Verify all data is preserved
assert converted["string"] == original_map["string"]
assert converted["integer"] == original_map["integer"]
assert converted["float"] == original_map["float"]
assert converted["boolean"] == original_map["boolean"]
assert (
converted["nested"]["inner_string"]
== original_map["nested"]["inner_string"]
)
assert converted["nested"]["inner_list"] == original_map["nested"]["inner_list"]
assert len(converted["list_of_maps"]) == len(original_map["list_of_maps"])
assert converted["list_of_maps"][0]["item"] == "first"
assert converted["list_of_maps"][1]["item"] == "second"
class TestSetTypes:
"""Test cases for SET value types."""
def test_simple_set_conversion(self):
"""Test basic SET type conversion from Python set to proto and back."""
test_set = {1, 2, 3, 4, 5}
protos = python_values_to_proto_values([test_set], ValueType.INT32_SET)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, set)
assert converted == test_set
def test_string_set_conversion(self):
"""Test STRING_SET type conversion."""
test_set = {"apple", "banana", "cherry"}
protos = python_values_to_proto_values([test_set], ValueType.STRING_SET)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, set)
assert converted == test_set
def test_float_set_conversion(self):
"""Test FLOAT_SET type conversion."""
test_set = {1.5, 2.5, 3.5}
protos = python_values_to_proto_values([test_set], ValueType.FLOAT_SET)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, set)
assert converted == test_set
def test_bool_set_conversion(self):
"""Test BOOL_SET type conversion."""
test_set = {True, False}
protos = python_values_to_proto_values([test_set], ValueType.BOOL_SET)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, set)
assert converted == test_set
def test_set_from_list_with_duplicates(self):
"""Test that duplicate values in lists are removed when converted to sets."""
test_list = [1, 2, 2, 3, 3, 3, 4, 5, 5]
protos = python_values_to_proto_values([test_list], ValueType.INT32_SET)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, set)
assert converted == {1, 2, 3, 4, 5}
def test_empty_set(self):
"""Test empty SET conversion."""
test_set = set()
protos = python_values_to_proto_values([test_set], ValueType.STRING_SET)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, set)
assert len(converted) == 0
def test_null_set(self):
"""Test None SET conversion."""
protos = python_values_to_proto_values([None], ValueType.INT32_SET)
converted = feast_value_type_to_python_type(protos[0])
assert converted is None
def test_multiple_set_values(self):
"""Test conversion of multiple set values."""
test_sets = [{1, 2, 3}, {4, 5}, {6}]
protos = python_values_to_proto_values(test_sets, ValueType.INT32_SET)
assert len(protos) == 3
assert feast_value_type_to_python_type(protos[0]) == {1, 2, 3}
assert feast_value_type_to_python_type(protos[1]) == {4, 5}
assert feast_value_type_to_python_type(protos[2]) == {6}
class TestMapArrowTypeSupport:
"""Test cases for MAP and MAP_LIST Arrow type conversions."""
def test_feast_value_type_to_pa_map(self):
"""Test that ValueType.MAP converts to a PyArrow map type."""
pa_type = feast_value_type_to_pa(ValueType.MAP)
assert isinstance(pa_type, pyarrow.MapType)
assert pa_type.key_type == pyarrow.string()
def test_feast_value_type_to_pa_map_list(self):
"""Test that ValueType.MAP_LIST converts to a PyArrow list of maps."""
pa_type = feast_value_type_to_pa(ValueType.MAP_LIST)
assert isinstance(pa_type, pyarrow.ListType)
assert isinstance(pa_type.value_type, pyarrow.MapType)
def test_pa_to_feast_value_type_map(self):
"""Test that PyArrow map type string converts to ValueType.MAP."""
result = pa_to_feast_value_type("map<string, string>")
assert result == ValueType.MAP
def test_pa_to_feast_value_type_map_various_value_types(self):
"""Test that various PyArrow map type strings all convert to MAP."""
assert pa_to_feast_value_type("map<string, int64>") == ValueType.MAP
assert pa_to_feast_value_type("map<string, double>") == ValueType.MAP
assert pa_to_feast_value_type("map<string, binary>") == ValueType.MAP
def test_from_feast_to_pyarrow_type_map(self):
"""Test that Feast Map type converts to PyArrow map type."""
pa_type = from_feast_to_pyarrow_type(FeastMap)
assert isinstance(pa_type, pyarrow.MapType)
def test_from_feast_to_pyarrow_type_array_map(self):
"""Test that Feast Array(Map) converts to PyArrow list of maps."""
pa_type = from_feast_to_pyarrow_type(Array(FeastMap))
assert isinstance(pa_type, pyarrow.ListType)
assert isinstance(pa_type.value_type, pyarrow.MapType)
def test_convert_value_type_str_map(self):
"""Test that 'MAP' string converts to ValueType.MAP."""
assert _convert_value_type_str_to_value_type("MAP") == ValueType.MAP
def test_convert_value_type_str_map_list(self):
"""Test that 'MAP_LIST' string converts to ValueType.MAP_LIST."""
assert _convert_value_type_str_to_value_type("MAP_LIST") == ValueType.MAP_LIST
def test_arrow_to_pg_type_map(self):
"""Test that Arrow map type converts to Postgres jsonb."""
assert arrow_to_pg_type("map<string, string>") == "jsonb"
assert arrow_to_pg_type("map<string, int64>") == "jsonb"
def test_pg_type_to_feast_value_type_json(self):
"""Test that Postgres json/jsonb types convert to ValueType.MAP."""
assert pg_type_to_feast_value_type("json") == ValueType.MAP
assert pg_type_to_feast_value_type("jsonb") == ValueType.MAP
def test_pg_type_to_feast_value_type_json_array(self):
"""Test that Postgres json[]/jsonb[] types convert to ValueType.MAP_LIST."""
assert pg_type_to_feast_value_type("json[]") == ValueType.MAP_LIST
assert pg_type_to_feast_value_type("jsonb[]") == ValueType.MAP_LIST
def test_snowflake_variant_to_map(self):
"""Test that Snowflake VARIANT/OBJECT types convert to ValueType.MAP."""
assert snowflake_type_to_feast_value_type("VARIANT") == ValueType.MAP
assert snowflake_type_to_feast_value_type("OBJECT") == ValueType.MAP
def test_redshift_super_to_map(self):
"""Test that Redshift super type converts to ValueType.MAP."""
assert redshift_to_feast_value_type("super") == ValueType.MAP
def test_map_roundtrip_proto_to_arrow_type(self):
"""Test that MAP type survives a full conversion roundtrip."""
pa_type = feast_value_type_to_pa(ValueType.MAP)
pa_type_str = str(pa_type)
roundtrip = pa_to_feast_value_type(pa_type_str)
assert roundtrip == ValueType.MAP
def test_spark_map_to_feast(self):
"""Test that Spark map types convert to ValueType.MAP."""
assert spark_to_feast_value_type("map<string,string>") == ValueType.MAP
assert spark_to_feast_value_type("map<string,int>") == ValueType.MAP
assert spark_to_feast_value_type("MAP<STRING,DOUBLE>") == ValueType.MAP
def test_spark_array_map_to_feast(self):
"""Test that Spark array<map<...>> types convert to ValueType.MAP_LIST."""
assert (
spark_to_feast_value_type("array<map<string,string>>") == ValueType.MAP_LIST
)
def test_spark_unknown_still_returns_null(self):
"""Test that unrecognized Spark types still return NULL."""
assert spark_to_feast_value_type("interval") == ValueType.NULL
def test_spark_struct_to_feast_struct(self):
"""Test that Spark struct types now convert to ValueType.STRUCT."""
assert spark_to_feast_value_type("struct<a:int>") == ValueType.STRUCT
class TestEnableValidationOnFeatureView:
"""Test that enable_validation is a real parameter on FeatureView."""
def test_feature_view_has_enable_validation_default_false(self):
"""Test that FeatureView has enable_validation defaulting to False."""
import inspect
from feast.feature_view import FeatureView
sig = inspect.signature(FeatureView.__init__)
assert "enable_validation" in sig.parameters
assert sig.parameters["enable_validation"].default is False
def test_batch_feature_view_has_enable_validation(self):
"""Test that BatchFeatureView has enable_validation parameter."""
import inspect
from feast.batch_feature_view import BatchFeatureView
sig = inspect.signature(BatchFeatureView.__init__)
assert "enable_validation" in sig.parameters
assert sig.parameters["enable_validation"].default is False
def test_stream_feature_view_has_enable_validation(self):
"""Test that StreamFeatureView has enable_validation parameter."""
import inspect
from feast.stream_feature_view import StreamFeatureView
sig = inspect.signature(StreamFeatureView.__init__)
assert "enable_validation" in sig.parameters
assert sig.parameters["enable_validation"].default is False
class TestRedshiftDynamoDBMapSupport:
"""Test cases for DynamoDB + Redshift map type round-trips."""
def test_pa_to_redshift_value_type_map(self):
"""Test that Arrow map type maps to Redshift 'super' type."""
pa_type = feast_value_type_to_pa(ValueType.MAP)
assert pa_to_redshift_value_type(pa_type) == "super"
def test_pa_to_redshift_value_type_map_list(self):
"""Test that Arrow list-of-map type maps to Redshift 'super' type."""
pa_type = feast_value_type_to_pa(ValueType.MAP_LIST)
assert pa_to_redshift_value_type(pa_type) == "super"
def test_json_string_to_map_proto(self):
"""Test that JSON strings are parsed to MAP protos during materialization."""
json_str = '{"key1": "value1", "key2": "value2"}'
protos = python_values_to_proto_values([json_str], ValueType.MAP)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, dict)
assert converted["key1"] == "value1"
assert converted["key2"] == "value2"
def test_json_string_to_map_list_proto(self):
"""Test that JSON strings are parsed to MAP_LIST protos during materialization."""
json_str = '[{"a": "1"}, {"b": "2"}]'
protos = python_values_to_proto_values([json_str], ValueType.MAP_LIST)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, list)
assert len(converted) == 2
assert converted[0]["a"] == "1"
def test_dict_still_works_for_map(self):
"""Test that regular Python dicts still work for MAP (no regression)."""
test_dict = {"x": "y", "a": 1}
protos = python_values_to_proto_values([test_dict], ValueType.MAP)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, dict)
assert converted["x"] == "y"
def test_none_map_still_works(self):
"""Test that None MAP values still produce empty proto (no regression)."""
protos = python_values_to_proto_values([None], ValueType.MAP)
converted = feast_value_type_to_python_type(protos[0])
assert converted is None
def test_redshift_super_roundtrip(self):
"""Test full type conversion roundtrip: Redshift super → Feast MAP → Arrow → Redshift super."""
feast_type = redshift_to_feast_value_type("super")
assert feast_type == ValueType.MAP
pa_type = feast_value_type_to_pa(feast_type)
redshift_type = pa_to_redshift_value_type(pa_type)
assert redshift_type == "super"
class TestJsonTypeSupport:
"""Test cases for JSON value type."""
def test_simple_json_conversion(self):
"""Test basic JSON type conversion: Python dict -> proto (json_val) -> Python."""
test_data = {"name": "Alice", "age": 30, "active": True}
protos = python_values_to_proto_values([test_data], ValueType.JSON)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, dict)
assert converted["name"] == "Alice"
assert converted["age"] == 30
assert converted["active"] is True
def test_json_string_passthrough(self):
"""Test that a raw JSON string is stored and returned correctly."""
json_str = '{"key": "value", "count": 42}'
protos = python_values_to_proto_values([json_str], ValueType.JSON)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, dict)
assert converted["key"] == "value"
assert converted["count"] == 42
def test_json_array_value(self):
"""Test JSON type with an array as the top-level value."""
test_data = [1, 2, 3, "four"]
protos = python_values_to_proto_values([test_data], ValueType.JSON)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, list)
assert converted == [1, 2, 3, "four"]
def test_json_nested(self):
"""Test deeply nested JSON structures."""
test_data = {
"level1": {"level2": {"level3": {"value": "deep"}}},
"array": [{"a": 1}, {"b": 2}],
}
protos = python_values_to_proto_values([test_data], ValueType.JSON)
converted = feast_value_type_to_python_type(protos[0])
assert converted["level1"]["level2"]["level3"]["value"] == "deep"
assert converted["array"][0]["a"] == 1
def test_null_json(self):
"""Test None JSON conversion."""
protos = python_values_to_proto_values([None], ValueType.JSON)
converted = feast_value_type_to_python_type(protos[0])
assert converted is None
def test_json_list_conversion(self):
"""Test JSON_LIST type conversion."""
test_data = [
{"name": "Alice"},
'{"name": "Bob"}',
{"count": 42},
]
protos = python_values_to_proto_values([test_data], ValueType.JSON_LIST)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, list)
assert len(converted) == 3
assert converted[0] == {"name": "Alice"}
assert converted[1] == {"name": "Bob"}
assert converted[2] == {"count": 42}
def test_null_json_list(self):
"""Test None JSON_LIST conversion."""
protos = python_values_to_proto_values([None], ValueType.JSON_LIST)
converted = feast_value_type_to_python_type(protos[0])
assert converted is None
def test_multiple_json_values(self):
"""Test conversion of multiple JSON values."""
test_values = [
{"x": 1},
{"y": 2},
None,
{"z": 3},
]
protos = python_values_to_proto_values(test_values, ValueType.JSON)
converted = [feast_value_type_to_python_type(p) for p in protos]
assert converted[0] == {"x": 1}
assert converted[1] == {"y": 2}
assert converted[2] is None
assert converted[3] == {"z": 3}
def test_feast_value_type_to_pa_json(self):
"""Test that ValueType.JSON converts to PyArrow large_string."""
pa_type = feast_value_type_to_pa(ValueType.JSON)
assert pa_type == pyarrow.large_string()
def test_feast_value_type_to_pa_json_list(self):
"""Test that ValueType.JSON_LIST converts to PyArrow list of large_string."""
pa_type = feast_value_type_to_pa(ValueType.JSON_LIST)
assert isinstance(pa_type, pyarrow.ListType)
assert pa_type.value_type == pyarrow.large_string()
def test_convert_value_type_str_json(self):
"""Test that 'JSON' string converts to ValueType.JSON."""
assert _convert_value_type_str_to_value_type("JSON") == ValueType.JSON
assert _convert_value_type_str_to_value_type("JSON_LIST") == ValueType.JSON_LIST
def test_arrow_to_pg_type_json(self):
"""Test that Arrow large_string converts to Postgres jsonb."""
assert arrow_to_pg_type("large_string") == "jsonb"
def test_bq_json_to_feast(self):
"""Test that BigQuery JSON type converts to ValueType.JSON."""
from feast.type_map import bq_to_feast_value_type
assert bq_to_feast_value_type("JSON") == ValueType.JSON
def test_spark_struct_not_json(self):
"""Test that Spark struct types map to STRUCT not JSON."""
assert spark_to_feast_value_type("struct<a:int,b:string>") == ValueType.STRUCT
def test_snowflake_json_to_feast(self):
"""Test that Snowflake JSON type converts to ValueType.JSON."""
assert snowflake_type_to_feast_value_type("JSON") == ValueType.JSON
def test_json_feast_type_aliases(self):
"""Test Json FeastType alias and conversions."""
from feast.types import Json, from_feast_to_pyarrow_type
pa_type = from_feast_to_pyarrow_type(Json)
assert pa_type == pyarrow.large_string()
def test_json_value_types_mapping(self):
"""Test JSON types in VALUE_TYPES_TO_FEAST_TYPES."""
from feast.types import VALUE_TYPES_TO_FEAST_TYPES, Json
assert VALUE_TYPES_TO_FEAST_TYPES[ValueType.JSON] == Json
def test_pa_to_feast_value_type_large_string(self):
"""Test that large_string arrow type converts to ValueType.JSON."""
result = pa_to_feast_value_type("large_string")
assert result == ValueType.JSON
class TestStructTypeSupport:
"""Test cases for STRUCT value type."""
def test_simple_struct_conversion(self):
"""Test basic STRUCT type conversion: Python dict -> proto (struct_val) -> Python dict."""
test_data = {"name": "Alice", "age": 30}
protos = python_values_to_proto_values([test_data], ValueType.STRUCT)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, dict)
assert converted["name"] == "Alice"
assert converted["age"] == 30
def test_nested_struct_conversion(self):
"""Test nested STRUCT type conversion."""
test_data = {
"address": {"street": "123 Main St", "city": "NYC"},
"name": "Alice",
}
protos = python_values_to_proto_values([test_data], ValueType.STRUCT)
converted = feast_value_type_to_python_type(protos[0])
assert converted["address"]["street"] == "123 Main St"
assert converted["address"]["city"] == "NYC"
assert converted["name"] == "Alice"
def test_null_struct(self):
"""Test None STRUCT conversion."""
protos = python_values_to_proto_values([None], ValueType.STRUCT)
converted = feast_value_type_to_python_type(protos[0])
assert converted is None
def test_struct_list_conversion(self):
"""Test STRUCT_LIST type conversion."""
test_data = [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
]
protos = python_values_to_proto_values([test_data], ValueType.STRUCT_LIST)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, list)
assert len(converted) == 2
assert converted[0]["name"] == "Alice"
assert converted[1]["age"] == 25
def test_null_struct_list(self):
"""Test None STRUCT_LIST conversion."""
protos = python_values_to_proto_values([None], ValueType.STRUCT_LIST)
converted = feast_value_type_to_python_type(protos[0])
assert converted is None
def test_multiple_struct_values(self):
"""Test conversion of multiple STRUCT values."""
test_values = [
{"x": 1},
None,
{"y": 2, "z": 3},
]
protos = python_values_to_proto_values(test_values, ValueType.STRUCT)
converted = [feast_value_type_to_python_type(p) for p in protos]
assert converted[0] == {"x": 1}
assert converted[1] is None
assert converted[2] == {"y": 2, "z": 3}
def test_struct_class_creation(self):
"""Test Struct FeastType creation and validation."""
from feast.types import Int32, String, Struct
struct_type = Struct({"name": String, "age": Int32})
assert struct_type.to_value_type() == ValueType.STRUCT
assert "name" in struct_type.fields
assert struct_type.fields["name"] == String
assert struct_type.fields["age"] == Int32
def test_struct_empty_raises(self):
"""Test that empty Struct raises ValueError."""
from feast.types import Struct
with pytest.raises(ValueError, match="at least one field"):
Struct({})
def test_struct_to_pyarrow(self):
"""Test Struct type converts to PyArrow struct."""
from feast.types import Int32, String, Struct
struct_type = Struct({"name": String, "age": Int32})
pa_type = struct_type.to_pyarrow_type()
assert pyarrow.types.is_struct(pa_type)
assert pa_type.get_field_index("name") >= 0
assert pa_type.get_field_index("age") >= 0
def test_struct_from_feast_to_pyarrow(self):
"""Test from_feast_to_pyarrow_type handles Struct."""
from feast.types import Int32, String, Struct
struct_type = Struct({"name": String, "age": Int32})
pa_type = from_feast_to_pyarrow_type(struct_type)
assert pyarrow.types.is_struct(pa_type)
def test_array_of_struct(self):
"""Test Array(Struct(...)) works."""
from feast.types import Array, Int32, String, Struct
struct_type = Struct({"name": String, "value": Int32})
array_type = Array(struct_type)
assert array_type.to_value_type() == ValueType.STRUCT_LIST
pa_type = from_feast_to_pyarrow_type(array_type)
assert isinstance(pa_type, pyarrow.ListType)
assert pyarrow.types.is_struct(pa_type.value_type)
def test_feast_value_type_to_pa_struct(self):
"""Test that ValueType.STRUCT converts to PyArrow struct (empty default)."""
pa_type = feast_value_type_to_pa(ValueType.STRUCT)
assert pyarrow.types.is_struct(pa_type)
def test_feast_value_type_to_pa_struct_list(self):
"""Test that ValueType.STRUCT_LIST converts to PyArrow list of struct."""
pa_type = feast_value_type_to_pa(ValueType.STRUCT_LIST)
assert isinstance(pa_type, pyarrow.ListType)
assert pyarrow.types.is_struct(pa_type.value_type)
def test_convert_value_type_str_struct(self):
"""Test that 'STRUCT' string converts to ValueType.STRUCT."""
assert _convert_value_type_str_to_value_type("STRUCT") == ValueType.STRUCT
assert (
_convert_value_type_str_to_value_type("STRUCT_LIST")
== ValueType.STRUCT_LIST
)
def test_spark_struct_to_feast(self):
"""Test that Spark struct types convert to ValueType.STRUCT."""
assert spark_to_feast_value_type("struct<a:int,b:string>") == ValueType.STRUCT
assert spark_to_feast_value_type("STRUCT<name:string>") == ValueType.STRUCT
def test_spark_array_struct_to_feast(self):
"""Test that Spark array<struct<...>> types convert to STRUCT_LIST."""
assert (
spark_to_feast_value_type("array<struct<a:int>>") == ValueType.STRUCT_LIST
)
def test_bq_struct_to_feast(self):
"""Test that BigQuery STRUCT/RECORD types convert to ValueType.STRUCT."""
from feast.type_map import bq_to_feast_value_type
assert bq_to_feast_value_type("STRUCT") == ValueType.STRUCT
assert bq_to_feast_value_type("RECORD") == ValueType.STRUCT
def test_pa_to_feast_value_type_struct(self):
"""Test that struct arrow type string converts to ValueType.STRUCT."""
result = pa_to_feast_value_type("struct<name: string, age: int32>")
assert result == ValueType.STRUCT
def test_struct_schema_persistence(self):
"""Test that Struct schema is preserved through Field serialization/deserialization."""
from feast.field import Field
from feast.types import Int32, String, Struct
struct_type = Struct({"street": String, "zip": Int32})
field = Field(name="address", dtype=struct_type)
proto = field.to_proto()
restored = Field.from_proto(proto)
assert isinstance(restored.dtype, Struct)
assert "street" in restored.dtype.fields
assert "zip" in restored.dtype.fields
assert restored.dtype.fields["street"] == String
assert restored.dtype.fields["zip"] == Int32
def test_struct_json_string_parsing(self):
"""Test that JSON string input is parsed for STRUCT type."""
json_str = '{"name": "Alice", "score": 95}'
protos = python_values_to_proto_values([json_str], ValueType.STRUCT)
converted = feast_value_type_to_python_type(protos[0])
assert isinstance(converted, dict)
assert converted["name"] == "Alice"
assert converted["score"] == 95
def test_struct_equality(self):
"""Test Struct type equality."""
from feast.types import Int32, String, Struct
s1 = Struct({"name": String, "age": Int32})
s2 = Struct({"name": String, "age": Int32})
s3 = Struct({"name": String})
assert s1 == s2
assert s1 != s3
def test_from_feast_type_struct(self):