Skip to content

Commit cc17aad

Browse files
authored
fix: Keep entity rows apart in the SQL point-in-time joins (#7124)
* fix: Keep entity rows apart in the SQL point-in-time joins The point-in-time join templates of the Redshift, Snowflake, BigQuery, Postgres, Athena, Spark, Trino, ClickHouse and Couchbase offline stores identify each entity row by concatenating the string values of its join keys and event timestamp. Different rows could produce the same id, e.g. driver_id=1, customer_id=12 and driver_id=11, customer_id=2 at the same timestamp. Such rows got the other row's feature value, or, when both feature rows had the same event timestamp, each came back twice, once with each value. Length-prefix each join key value in the id, as the ibis join does since #6884, so the id is only shared by rows with equal values. Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com> * test: pass a subquery as the feature source so DuckDB 1.5 parses the Trino template Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com> --------- Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com>
1 parent ce02644 commit cc17aad

10 files changed

Lines changed: 104 additions & 9 deletions

File tree

‎sdk/python/feast/infra/offline_stores/bigquery.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,7 @@ def arrow_schema_to_bq_schema(arrow_schema: pyarrow.Schema) -> List[SchemaField]
16931693
{% if featureview.entities %}
16941694
,CONCAT(
16951695
{% for entity in featureview.entities %}
1696-
CAST({{entity}} AS STRING),
1696+
CAST(LENGTH(CAST({{entity}} AS STRING)) AS STRING), ':', CAST({{entity}} AS STRING),
16971697
{% endfor %}
16981698
CAST({{entity_df_event_timestamp_col}} AS STRING)
16991699
) AS {{featureview.name}}__entity_row_unique_id

‎sdk/python/feast/infra/offline_stores/contrib/athena_offline_store/athena.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ def _get_entity_df_event_timestamp_range(
576576
{% if featureview.entities %}
577577
,(
578578
{% for entity in featureview.entities %}
579-
CAST({{entity}} as VARCHAR) ||
579+
CAST(LENGTH(CAST({{entity}} AS VARCHAR)) AS VARCHAR) || ':' || CAST({{entity}} AS VARCHAR) ||
580580
{% endfor %}
581581
CAST({{entity_df_event_timestamp_col}} AS VARCHAR)
582582
) AS {{featureview.name}}__entity_row_unique_id

‎sdk/python/feast/infra/offline_stores/contrib/clickhouse_offline_store/clickhouse.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ def _append_alias(field_names: List[str], alias: str) -> List[str]:
469469
{% if featureview.entities %}
470470
,(
471471
{% for entity in featureview.entities %}
472-
CAST("{{entity}}" as VARCHAR) ||
472+
CAST(LENGTH(CAST("{{entity}}" AS VARCHAR)) AS VARCHAR) || ':' || CAST("{{entity}}" AS VARCHAR) ||
473473
{% endfor %}
474474
CAST("{{entity_df_event_timestamp_col}}" AS VARCHAR)
475475
) AS "{{featureview.name}}__entity_row_unique_id"

‎sdk/python/feast/infra/offline_stores/contrib/couchbase_offline_store/couchbase.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def _get_entity_schema(
554554
{% if featureview.entities -%}
555555
,CONCAT(
556556
{% for entity in featureview.entities -%}
557-
TOSTRING(e.`{{entity}}`),
557+
TOSTRING(LENGTH(TOSTRING(e.`{{entity}}`))), ':', TOSTRING(e.`{{entity}}`),
558558
{% endfor -%}
559559
TOSTRING(e.`{{entity_df_event_timestamp_col}}`)
560560
) AS `{{featureview.name}}__entity_row_unique_id`

‎sdk/python/feast/infra/offline_stores/contrib/postgres_offline_store/postgres.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ def _get_entity_schema(
929929
{% if featureview.entities %}
930930
,(
931931
{% for entity in featureview.entities %}
932-
CAST("{{entity}}" as VARCHAR) ||
932+
CAST(LENGTH(CAST("{{entity}}" AS VARCHAR)) AS VARCHAR) || ':' || CAST("{{entity}}" AS VARCHAR) ||
933933
{% endfor %}
934934
CAST("{{entity_df_event_timestamp_col}}" AS VARCHAR)
935935
) AS "{{featureview.name}}__entity_row_unique_id"

‎sdk/python/feast/infra/offline_stores/contrib/spark_offline_store/spark.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,7 @@ def _cast_data_frame(
19081908
{% for featureview in featureviews %}
19091909
,CONCAT(
19101910
{% for entity in featureview.entities %}
1911-
CAST({{entity}} AS STRING),
1911+
CAST(LENGTH(CAST({{entity}} AS STRING)) AS STRING), ':', CAST({{entity}} AS STRING),
19121912
{% endfor %}
19131913
CAST({{entity_df_event_timestamp_col}} AS STRING)
19141914
) AS {{featureview.name}}__entity_row_unique_id

‎sdk/python/feast/infra/offline_stores/contrib/trino_offline_store/trino.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ def _get_entity_df_event_timestamp_range(
13651365
{% if featureview.entities %}
13661366
,CONCAT(
13671367
{% for entity in featureview.entities %}
1368-
CAST({{entity}} AS VARCHAR),
1368+
CAST(LENGTH(CAST({{entity}} AS VARCHAR)) AS VARCHAR), ':', CAST({{entity}} AS VARCHAR),
13691369
{% endfor %}
13701370
CAST({{entity_df_event_timestamp_col}} AS VARCHAR)
13711371
) AS {{featureview.name}}__entity_row_unique_id

‎sdk/python/feast/infra/offline_stores/redshift.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ def _get_entity_df_event_timestamp_range(
12701270
{% if featureview.entities %}
12711271
,(
12721272
{% for entity in featureview.entities %}
1273-
CAST({{entity}} as VARCHAR) ||
1273+
CAST(LENGTH(CAST({{entity}} AS VARCHAR)) AS VARCHAR) || ':' || CAST({{entity}} AS VARCHAR) ||
12741274
{% endfor %}
12751275
CAST({{entity_df_event_timestamp_col}} AS VARCHAR)
12761276
) AS {{featureview.name}}__entity_row_unique_id

‎sdk/python/feast/infra/offline_stores/snowflake.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ def _get_entity_df_event_timestamp_range(
13691369
{% if featureview.entities %}
13701370
,(
13711371
{% for entity in featureview.entities %}
1372-
CAST("{{entity}}" AS VARCHAR) ||
1372+
CAST(LENGTH(CAST("{{entity}}" AS VARCHAR)) AS VARCHAR) || ':' || CAST("{{entity}}" AS VARCHAR) ||
13731373
{% endfor %}
13741374
CAST("{{entity_df_event_timestamp_col}}" AS VARCHAR)
13751375
) AS "{{featureview.name}}__entity_row_unique_id"
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)