|
| 1 | +"""The SQL point-in-time join templates identify each entity row by its join key |
| 2 | +values and event timestamp. Rows whose values concatenate to the same string, |
| 3 | +such as driver_id=1, customer_id=12 and driver_id=11, customer_id=2, must still |
| 4 | +get their own feature values.""" |
| 5 | + |
| 6 | +import re |
| 7 | + |
| 8 | +import duckdb |
| 9 | +import pytest |
| 10 | + |
| 11 | +from feast.infra.offline_stores import offline_utils |
| 12 | +from feast.infra.offline_stores.offline_utils import FeatureViewQueryContext |
| 13 | + |
| 14 | +# Templates whose rendered query DuckDB can run. |
| 15 | +DUCKDB_RUNNABLE_TEMPLATE_MODULES = [ |
| 16 | + "feast.infra.offline_stores.redshift", |
| 17 | + "feast.infra.offline_stores.contrib.athena_offline_store.athena", |
| 18 | + "feast.infra.offline_stores.contrib.postgres_offline_store.postgres", |
| 19 | + "feast.infra.offline_stores.contrib.trino_offline_store.trino", |
| 20 | + "feast.infra.offline_stores.contrib.clickhouse_offline_store.clickhouse", |
| 21 | +] |
| 22 | + |
| 23 | +# Templates that use SQL DuckDB does not support (ASOF ... MATCH_CONDITION, |
| 24 | +# CREATE TEMP TABLE with backticks, Spark temp views, N1QL). |
| 25 | +OTHER_TEMPLATE_MODULES = [ |
| 26 | + "feast.infra.offline_stores.bigquery", |
| 27 | + "feast.infra.offline_stores.snowflake", |
| 28 | + "feast.infra.offline_stores.contrib.spark_offline_store.spark", |
| 29 | + "feast.infra.offline_stores.contrib.couchbase_offline_store.couchbase", |
| 30 | +] |
| 31 | + |
| 32 | + |
| 33 | +def _render(query_template: str) -> str: |
| 34 | + context = FeatureViewQueryContext( |
| 35 | + name="trips", |
| 36 | + ttl=0, |
| 37 | + entities=["driver_id", "customer_id"], |
| 38 | + features=["trips"], |
| 39 | + field_mapping={}, |
| 40 | + timestamp_field="event_timestamp", |
| 41 | + created_timestamp_column=None, |
| 42 | + table_subquery="(SELECT * FROM feature_table)", |
| 43 | + entity_selections=["driver_id AS driver_id", "customer_id AS customer_id"], |
| 44 | + min_event_timestamp=None, |
| 45 | + max_event_timestamp="2025-01-03T00:00:00", |
| 46 | + date_partition_column=None, |
| 47 | + timestamp_field_type=None, |
| 48 | + ) |
| 49 | + return offline_utils.build_point_in_time_query( |
| 50 | + [context], |
| 51 | + left_table_query_string="entity_df", |
| 52 | + entity_df_event_timestamp_col="event_timestamp", |
| 53 | + entity_df_columns={ |
| 54 | + "driver_id": None, |
| 55 | + "customer_id": None, |
| 56 | + "event_timestamp": None, |
| 57 | + }.keys(), |
| 58 | + query_template=query_template, |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize("module_name", DUCKDB_RUNNABLE_TEMPLATE_MODULES) |
| 63 | +def test_entity_rows_with_colliding_key_strings_keep_their_own_features( |
| 64 | + module_name, |
| 65 | +): |
| 66 | + module = pytest.importorskip(module_name) |
| 67 | + con = duckdb.connect() |
| 68 | + # Athena and Trino parse the event timestamp bounds with this function. |
| 69 | + con.execute("CREATE MACRO from_iso8601_timestamp(s) AS CAST(s AS TIMESTAMP)") |
| 70 | + con.execute( |
| 71 | + "CREATE TABLE entity_df AS SELECT * FROM (VALUES " |
| 72 | + "(1, 12, TIMESTAMP '2025-01-02 00:00:00'), " |
| 73 | + "(11, 2, TIMESTAMP '2025-01-02 00:00:00')" |
| 74 | + ") AS t(driver_id, customer_id, event_timestamp)" |
| 75 | + ) |
| 76 | + con.execute( |
| 77 | + "CREATE TABLE feature_table AS SELECT * FROM (VALUES " |
| 78 | + "(1, 12, TIMESTAMP '2025-01-01 00:00:00', 100), " |
| 79 | + "(11, 2, TIMESTAMP '2025-01-01 00:00:00', 200)" |
| 80 | + ") AS t(driver_id, customer_id, event_timestamp, trips)" |
| 81 | + ) |
| 82 | + |
| 83 | + df = con.execute(_render(module.MULTIPLE_FEATURE_VIEW_POINT_IN_TIME_JOIN)).df() |
| 84 | + |
| 85 | + rows = sorted(zip(df["driver_id"], df["customer_id"], df["trips"])) |
| 86 | + assert rows == [(1, 12, 100), (11, 2, 200)] |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.parametrize("module_name", OTHER_TEMPLATE_MODULES) |
| 90 | +def test_entity_row_id_length_prefixes_each_join_key(module_name): |
| 91 | + module = pytest.importorskip(module_name) |
| 92 | + query = _render(module.MULTIPLE_FEATURE_VIEW_POINT_IN_TIME_JOIN) |
| 93 | + |
| 94 | + for entity in ("driver_id", "customer_id"): |
| 95 | + assert re.search(rf"LENGTH\((CAST|TOSTRING)\([^)]*\b{entity}\b", query) |
0 commit comments