|
| 1 | +import json |
| 2 | +import sys |
| 3 | +import types |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from feast.infra.offline_stores.file_source import SavedDatasetFileStorage |
| 8 | +from feast.protos.feast.core.DataSource_pb2 import DataSource as DataSourceProto |
| 9 | +from feast.protos.feast.core.SavedDataset_pb2 import ( |
| 10 | + SavedDatasetStorage as SavedDatasetStorageProto, |
| 11 | +) |
| 12 | +from feast.saved_dataset import ( |
| 13 | + CUSTOM_STORAGE_CLASS_KEY, |
| 14 | + SavedDatasetStorage, |
| 15 | + _StorageRegistry, |
| 16 | +) |
| 17 | + |
| 18 | +SYNTHETIC_MODULE = "feast_test_synthetic_storage" |
| 19 | + |
| 20 | +# Two storage classes that share a proto field, in a real importable module so |
| 21 | +# that the class path recorded in a payload can actually be resolved. |
| 22 | +SYNTHETIC_SOURCE = """ |
| 23 | +import json |
| 24 | +
|
| 25 | +from feast.protos.feast.core.DataSource_pb2 import DataSource as DataSourceProto |
| 26 | +from feast.saved_dataset import SavedDatasetStorage |
| 27 | +
|
| 28 | +
|
| 29 | +def _options(**config): |
| 30 | + return DataSourceProto.CustomSourceOptions( |
| 31 | + configuration=json.dumps(config).encode() |
| 32 | + ) |
| 33 | +
|
| 34 | +
|
| 35 | +class FirstSharedStorage(SavedDatasetStorage): |
| 36 | + _proto_attr_name = "custom_storage" |
| 37 | +
|
| 38 | + def to_proto(self): |
| 39 | + return self._custom_storage_proto(_options(table="first")) |
| 40 | +
|
| 41 | + @staticmethod |
| 42 | + def from_proto(storage_proto): |
| 43 | + return FirstSharedStorage() |
| 44 | +
|
| 45 | + def to_data_source(self): |
| 46 | + raise NotImplementedError |
| 47 | +
|
| 48 | +
|
| 49 | +class SecondSharedStorage(SavedDatasetStorage): |
| 50 | + _proto_attr_name = "custom_storage" |
| 51 | +
|
| 52 | + def to_proto(self): |
| 53 | + return self._custom_storage_proto(_options(table="second")) |
| 54 | +
|
| 55 | + @staticmethod |
| 56 | + def from_proto(storage_proto): |
| 57 | + return SecondSharedStorage() |
| 58 | +
|
| 59 | + def to_data_source(self): |
| 60 | + raise NotImplementedError |
| 61 | +
|
| 62 | +
|
| 63 | +""" |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture |
| 67 | +def synthetic_storage(): |
| 68 | + """Register synthetic storage classes, then undo the registration. |
| 69 | +
|
| 70 | + The registry is process-global, so without the teardown these classes would |
| 71 | + remain claimants for every test that runs afterwards. |
| 72 | + """ |
| 73 | + by_name = dict(_StorageRegistry.classes_by_proto_attr_name) |
| 74 | + all_by_name = { |
| 75 | + key: list(value) |
| 76 | + for key, value in _StorageRegistry.all_classes_by_proto_attr_name.items() |
| 77 | + } |
| 78 | + |
| 79 | + module = types.ModuleType(SYNTHETIC_MODULE) |
| 80 | + sys.modules[SYNTHETIC_MODULE] = module |
| 81 | + exec(compile(SYNTHETIC_SOURCE, SYNTHETIC_MODULE, "exec"), module.__dict__) |
| 82 | + |
| 83 | + yield module |
| 84 | + |
| 85 | + sys.modules.pop(SYNTHETIC_MODULE, None) |
| 86 | + _StorageRegistry.classes_by_proto_attr_name.clear() |
| 87 | + _StorageRegistry.classes_by_proto_attr_name.update(by_name) |
| 88 | + _StorageRegistry.all_classes_by_proto_attr_name.clear() |
| 89 | + _StorageRegistry.all_classes_by_proto_attr_name.update(all_by_name) |
| 90 | + |
| 91 | + |
| 92 | +def _custom_options(**config) -> DataSourceProto.CustomSourceOptions: |
| 93 | + return DataSourceProto.CustomSourceOptions( |
| 94 | + configuration=json.dumps(config).encode() |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +def test_a_field_with_one_claimant_is_unaffected(): |
| 99 | + storage = SavedDatasetFileStorage(path="data/driver_stats.parquet") |
| 100 | + |
| 101 | + read_back = SavedDatasetStorage.from_proto(storage.to_proto()) |
| 102 | + |
| 103 | + assert isinstance(read_back, SavedDatasetFileStorage) |
| 104 | + assert read_back.file_options.uri == "data/driver_stats.parquet" |
| 105 | + |
| 106 | + |
| 107 | +def test_sharing_a_field_round_trips_to_the_class_that_wrote_it(synthetic_storage): |
| 108 | + first = synthetic_storage.FirstSharedStorage() |
| 109 | + second = synthetic_storage.SecondSharedStorage() |
| 110 | + |
| 111 | + # SecondSharedStorage was defined last, so the flat registry points at it. |
| 112 | + # Both must still read back as themselves. |
| 113 | + assert isinstance( |
| 114 | + SavedDatasetStorage.from_proto(first.to_proto()), |
| 115 | + synthetic_storage.FirstSharedStorage, |
| 116 | + ) |
| 117 | + assert isinstance( |
| 118 | + SavedDatasetStorage.from_proto(second.to_proto()), |
| 119 | + synthetic_storage.SecondSharedStorage, |
| 120 | + ) |
| 121 | + |
| 122 | + |
| 123 | +def test_the_written_proto_records_the_concrete_class(synthetic_storage): |
| 124 | + proto = synthetic_storage.FirstSharedStorage().to_proto() |
| 125 | + |
| 126 | + config = json.loads(proto.custom_storage.configuration.decode("utf8")) |
| 127 | + |
| 128 | + assert config[CUSTOM_STORAGE_CLASS_KEY] == ( |
| 129 | + f"{SYNTHETIC_MODULE}.FirstSharedStorage" |
| 130 | + ) |
| 131 | + # The payload the class itself wrote is preserved alongside the new key. |
| 132 | + assert config["table"] == "first" |
| 133 | + |
| 134 | + |
| 135 | +def test_an_untagged_payload_with_several_claimants_names_them(synthetic_storage): |
| 136 | + # What an older version wrote: no class recorded in the payload. |
| 137 | + legacy = _custom_options(table="first") |
| 138 | + assert CUSTOM_STORAGE_CLASS_KEY not in json.loads( |
| 139 | + legacy.configuration.decode("utf8") |
| 140 | + ) |
| 141 | + |
| 142 | + with pytest.raises(ValueError) as error: |
| 143 | + SavedDatasetStorage.from_proto(SavedDatasetStorageProto(custom_storage=legacy)) |
| 144 | + |
| 145 | + message = str(error.value) |
| 146 | + assert CUSTOM_STORAGE_CLASS_KEY in message |
| 147 | + # Every claimant is named so the reader knows which to call directly. |
| 148 | + assert f"{SYNTHETIC_MODULE}.FirstSharedStorage" in message |
| 149 | + assert f"{SYNTHETIC_MODULE}.SecondSharedStorage" in message |
| 150 | + |
| 151 | + |
| 152 | +def test_an_unresolvable_recorded_class_does_not_raise_an_import_error( |
| 153 | + synthetic_storage, |
| 154 | +): |
| 155 | + """A recorded class whose module is not installed must fall back. |
| 156 | +
|
| 157 | + The fallback still cannot disambiguate, so this reports the ambiguity |
| 158 | + rather than leaking an import failure from the recorded path. |
| 159 | + """ |
| 160 | + payload = _custom_options( |
| 161 | + table="t", **{CUSTOM_STORAGE_CLASS_KEY: "not_a_module.NotAClass"} |
| 162 | + ) |
| 163 | + |
| 164 | + with pytest.raises(ValueError) as error: |
| 165 | + SavedDatasetStorage.from_proto(SavedDatasetStorageProto(custom_storage=payload)) |
| 166 | + |
| 167 | + assert CUSTOM_STORAGE_CLASS_KEY in str(error.value) |
| 168 | + |
| 169 | + |
| 170 | +@pytest.mark.parametrize( |
| 171 | + "module_path, class_name, kwargs", |
| 172 | + [ |
| 173 | + ( |
| 174 | + "feast.infra.offline_stores.contrib.postgres_offline_store.postgres_source", |
| 175 | + "SavedDatasetPostgreSQLStorage", |
| 176 | + {"table_ref": "public.saved_dataset"}, |
| 177 | + ), |
| 178 | + ( |
| 179 | + "feast.infra.offline_stores.contrib.clickhouse_offline_store.clickhouse_source", |
| 180 | + "SavedDatasetClickhouseStorage", |
| 181 | + {"table_ref": "public.saved_dataset"}, |
| 182 | + ), |
| 183 | + ( |
| 184 | + "feast.infra.offline_stores.contrib.couchbase_offline_store.couchbase_source", |
| 185 | + "SavedDatasetCouchbaseColumnarStorage", |
| 186 | + { |
| 187 | + "database_ref": "db", |
| 188 | + "scope_ref": "scope", |
| 189 | + "collection_ref": "collection", |
| 190 | + }, |
| 191 | + ), |
| 192 | + ], |
| 193 | +) |
| 194 | +def test_the_real_custom_storage_classes_round_trip(module_path, class_name, kwargs): |
| 195 | + """The three in-tree classes that share ``custom_storage``. |
| 196 | +
|
| 197 | + Postgres and Clickhouse serialize identical payloads, so before the writing |
| 198 | + class was recorded these two could only be told apart by import order. |
| 199 | + """ |
| 200 | + module = pytest.importorskip(module_path) |
| 201 | + storage_class = getattr(module, class_name) |
| 202 | + |
| 203 | + written = storage_class(**kwargs) |
| 204 | + read_back = SavedDatasetStorage.from_proto(written.to_proto()) |
| 205 | + |
| 206 | + assert type(read_back) is storage_class |
0 commit comments