diff --git a/sdk/python/feast/infra/offline_stores/ibis.py b/sdk/python/feast/infra/offline_stores/ibis.py index 67fdca8bb24..79be8bdc607 100644 --- a/sdk/python/feast/infra/offline_stores/ibis.py +++ b/sdk/python/feast/infra/offline_stores/ibis.py @@ -548,18 +548,49 @@ def point_in_time_join( select_cols.extend(feature_refs) feature_table = feature_table.select(select_cols) - # Track the feature columns we're adding - all_feature_cols.extend(feature_refs) - + # Select each side's columns explicitly instead of relying on the join's + # name resolution: a feature name that clashes with an entity column is + # renamed to "{name}_yyyy" on the feature side, and the final select + # below would then fail to find it. A feature ref that clashes with an + # entity column wins (consistent with the inner join above, where the + # entity's clashing copy is not selected), so the entity column it + # shadows is not carried forward and the result cannot contain + # duplicate column names. Columns accumulated by earlier feature tables + # are re-selected from acc_table; refs new to this table come from + # feature_table. + shadowed = set(feature_refs) + entity_cols_out = [ + c for c in entity_columns if c != "entity_row_id" and c not in shadowed + ] + current_new_cols = [ + c for c in dict.fromkeys(feature_refs) if c not in all_feature_cols + ] + # refs earlier feature tables contributed, except those the current + # table's refs shadow: already present in acc_table + carried_cols = [c for c in all_feature_cols if c not in shadowed] acc_table = acc_table.left_join( feature_table, predicates=[feature_table.entity_row_id == acc_table.entity_row_id], lname="", rname="{name}_yyyy", + ).select( + *[acc_table[c] for c in entity_cols_out], + acc_table["entity_row_id"], + *[acc_table[c] for c in carried_cols], + *[feature_table[c] for c in current_new_cols], ) - # Select only the columns we want: entity columns (minus entity_row_id) + all feature columns - final_cols = [c for c in entity_columns if c != "entity_row_id"] + all_feature_cols + all_feature_cols.extend(current_new_cols) + + # Select only the columns we want: entity columns (minus entity_row_id, + # minus any column a feature ref shadows) + all feature columns. The + # feature refs are already deduplicated per table; deduplicate them across + # tables too, so two feature views contributing the same bare feature name + # (full_feature_names=False) do not produce a duplicate column. + entity_cols_out = [ + c for c in entity_columns if c != "entity_row_id" and c not in set(all_feature_cols) + ] + final_cols = entity_cols_out + list(dict.fromkeys(all_feature_cols)) acc_table = acc_table.select(final_cols) return acc_table diff --git a/sdk/python/tests/unit/infra/offline_stores/test_ibis.py b/sdk/python/tests/unit/infra/offline_stores/test_ibis.py index 54d68eaea26..0a82fe965fc 100644 --- a/sdk/python/tests/unit/infra/offline_stores/test_ibis.py +++ b/sdk/python/tests/unit/infra/offline_stores/test_ibis.py @@ -286,6 +286,221 @@ def test_point_in_time_join_keeps_features_whose_name_ends_in_y(): assert rows == {1: (2.0, 20.0), 2: (3.0, 30.0)} +def test_point_in_time_join_with_entity_column_clashing_with_a_feature_name(): + """An entity dataframe column that shares its name with a requested feature + does not crash the join with a duplicate column; the feature value wins and + shadows the entity column (issue #8853 family, DuckDB/ibis path).""" + entity_table = pa.table( + { + "driver_id": [1, 2], + "event_timestamp": [pa_datetime(2024, 1, 3)] * 2, + "trips": [5, 6], + } + ) + positions = pa.table( + { + "driver_id": [1, 1, 2], + "event_timestamp": [ + pa_datetime(2024, 1, 1), + pa_datetime(2024, 1, 2), + pa_datetime(2024, 1, 1), + ], + "created": [ + pa_datetime(2024, 1, 1), + pa_datetime(2024, 1, 2), + pa_datetime(2024, 1, 1), + ], + "trips": [10.0, 20.0, 30.0], + } + ) + + actual = point_in_time_join( + ibis.memtable(entity_table), + feature_tables=[ + ( + ibis.memtable(positions), + "event_timestamp", + "created", + {"driver_id": "driver_id"}, + ["trips"], + timedelta(days=10), + ) + ], + ).to_pyarrow() + + rows = {row["driver_id"]: row["trips"] for row in actual.to_pylist()} + assert rows == {1: 20.0, 2: 30.0} + + +def test_point_in_time_join_full_feature_names_no_collision_with_entity_columns(): + """With full feature names the generated refs are prefixed with the feature + view name, so an entity column carrying the bare feature name cannot clash + and both columns come back.""" + entity_table = pa.table( + { + "driver_id": [1, 2], + "event_timestamp": [pa_datetime(2024, 1, 3)] * 2, + "trips": [5, 6], + } + ) + positions = pa.table( + { + "driver_id": [1, 1, 2], + "event_timestamp": [ + pa_datetime(2024, 1, 1), + pa_datetime(2024, 1, 2), + pa_datetime(2024, 1, 1), + ], + "created": [ + pa_datetime(2024, 1, 1), + pa_datetime(2024, 1, 2), + pa_datetime(2024, 1, 1), + ], + "trips": [10.0, 20.0, 30.0], + } + ) + + actual = point_in_time_join( + ibis.memtable(entity_table), + feature_tables=[ + ( + ibis.memtable(positions), + "event_timestamp", + "created", + {"driver_id": "driver_id"}, + ["positions__trips"], + timedelta(days=10), + ) + ], + ).to_pyarrow() + + rows = { + row["driver_id"]: (row["trips"], row["positions__trips"]) + for row in actual.to_pylist() + } + assert rows == {1: (5, 20.0), 2: (6, 30.0)} + + +def test_point_in_time_join_full_feature_names_no_collision_with_entity_columns(): + """With full feature names the generated refs are prefixed with the feature + view name, so an entity column carrying the bare feature name cannot clash + and both columns come back.""" + entity_table = pa.table( + { + "driver_id": [1, 2], + "event_timestamp": [pa_datetime(2024, 1, 3)] * 2, + "trips": [5, 6], + } + ) + # read_fv renames the feature columns to "__" when + # full_feature_names is set; simulate that here. + positions = pa.table( + { + "driver_id": [1, 1, 2], + "event_timestamp": [ + pa_datetime(2024, 1, 1), + pa_datetime(2024, 1, 2), + pa_datetime(2024, 1, 1), + ], + "created": [ + pa_datetime(2024, 1, 1), + pa_datetime(2024, 1, 2), + pa_datetime(2024, 1, 1), + ], + "positions__trips": [10.0, 20.0, 30.0], + } + ) + + actual = point_in_time_join( + ibis.memtable(entity_table), + feature_tables=[ + ( + ibis.memtable(positions), + "event_timestamp", + "created", + {"driver_id": "driver_id"}, + ["positions__trips"], + timedelta(days=10), + ) + ], + ).to_pyarrow() + + rows = { + row["driver_id"]: (row["trips"], row["positions__trips"]) + for row in actual.to_pylist() + } + assert rows == {1: (5, 20.0), 2: (6, 30.0)} + + +def test_point_in_time_join_accumulates_features_from_multiple_tables(): + """Feature columns accumulated from earlier feature tables survive the + left joins of later ones.""" + entity_table = pa.table( + { + "driver_id": [1, 2], + "event_timestamp": [pa_datetime(2024, 1, 3)] * 2, + } + ) + positions = pa.table( + { + "driver_id": [1, 1, 2], + "event_timestamp": [ + pa_datetime(2024, 1, 1), + pa_datetime(2024, 1, 2), + pa_datetime(2024, 1, 1), + ], + "created": [ + pa_datetime(2024, 1, 1), + pa_datetime(2024, 1, 2), + pa_datetime(2024, 1, 1), + ], + "trips": [10.0, 20.0, 30.0], + } + ) + rates = pa.table( + { + "driver_id": [1, 2], + "event_timestamp": [ + pa_datetime(2024, 1, 1), + pa_datetime(2024, 1, 1), + ], + "created": [ + pa_datetime(2024, 1, 1), + pa_datetime(2024, 1, 1), + ], + "conv_rate": [0.5, 0.75], + } + ) + + actual = point_in_time_join( + ibis.memtable(entity_table), + feature_tables=[ + ( + ibis.memtable(positions), + "event_timestamp", + "created", + {"driver_id": "driver_id"}, + ["trips"], + timedelta(days=10), + ), + ( + ibis.memtable(rates), + "event_timestamp", + "created", + {"driver_id": "driver_id"}, + ["conv_rate"], + timedelta(days=10), + ), + ], + ).to_pyarrow() + + rows = { + row["driver_id"]: (row["trips"], row["conv_rate"]) + for row in actual.to_pylist() + } + assert rows == {1: (20.0, 0.5), 2: (30.0, 0.75)} + + def test_point_in_time_join_allows_columns_named_like_the_y_copies(): """Feature-table columns named like the "_y" copies the join makes of clashing entity columns ("driver_id_y", "event_timestamp_y") do not clash