diff --git a/sdk/python/feast/infra/offline_stores/contrib/trino_offline_store/trino_source.py b/sdk/python/feast/infra/offline_stores/contrib/trino_offline_store/trino_source.py index 21554244f86..9f20fad8926 100644 --- a/sdk/python/feast/infra/offline_stores/contrib/trino_offline_store/trino_source.py +++ b/sdk/python/feast/infra/offline_stores/contrib/trino_offline_store/trino_source.py @@ -225,7 +225,9 @@ def validate(self, config: RepoConfig): def get_table_query_string(self) -> str: """Returns a string that can directly be used to reference this table in SQL""" - return self.table or self.query + if self.table: + return self.table + return f"({self.query})" @staticmethod def source_datatype_to_feast_value_type() -> Callable[[str], ValueType]: diff --git a/sdk/python/tests/unit/infra/compute_engines/trino/test_trino_compute_engine.py b/sdk/python/tests/unit/infra/compute_engines/trino/test_trino_compute_engine.py index ad2f2910938..a06657f2f67 100644 --- a/sdk/python/tests/unit/infra/compute_engines/trino/test_trino_compute_engine.py +++ b/sdk/python/tests/unit/infra/compute_engines/trino/test_trino_compute_engine.py @@ -342,6 +342,40 @@ def test_trino_read_node_generates_sql(self): assert "TIMESTAMP '2025-01-02 00:00:00.000000'" in sql assert "FROM iceberg.feast.driver_stats" in sql + def test_trino_read_node_with_query_source_generates_parenthesized_sql(self): + from feast.infra.offline_stores.contrib.trino_offline_store.trino_source import ( + TrinoSource, + ) + + source = TrinoSource( + name="user_source", + query="SELECT user_id, event_timestamp, val FROM catalog.schema.table", + timestamp_field="event_timestamp", + ) + + col_info = ColumnInfo( + join_keys=["user_id"], + feature_cols=["val"], + ts_col="event_timestamp", + created_ts_col=None, + ) + + node = TrinoReadNode( + name="test_query_read", + source=source, + column_info=col_info, + client=MagicMock(), + ) + + val = node.execute(MagicMock()) + plan: TrinoQueryPlan = val.data + sql = plan.to_sql() + + assert ( + "FROM (SELECT user_id, event_timestamp, val FROM catalog.schema.table)" + in sql + ) + def test_trino_filter_node_generates_sql(self): plan = TrinoQueryPlan( ctes=[("_source", 'SELECT * FROM "iceberg"."feast"."driver_stats"')], diff --git a/sdk/python/tests/unit/infra/offline_stores/contrib/trino_offline_store/test_trino_source.py b/sdk/python/tests/unit/infra/offline_stores/contrib/trino_offline_store/test_trino_source.py new file mode 100644 index 00000000000..cf57f43fb61 --- /dev/null +++ b/sdk/python/tests/unit/infra/offline_stores/contrib/trino_offline_store/test_trino_source.py @@ -0,0 +1,49 @@ +from feast.infra.offline_stores.contrib.trino_offline_store.trino_source import ( + TrinoSource, +) + + +def test_trino_source_get_table_query_string_with_table(): + source = TrinoSource( + table="my_catalog.my_schema.my_table", + timestamp_field="event_timestamp", + ) + assert source.get_table_query_string() == "my_catalog.my_schema.my_table" + + +def test_trino_source_get_table_query_string_with_query(): + query = "SELECT user_id, event_timestamp, val FROM catalog.schema.table" + source = TrinoSource( + name="user_source", + query=query, + timestamp_field="event_timestamp", + ) + assert source.get_table_query_string() == f"({query})" + + +def test_trino_source_proto_roundtrip_query(): + query = "SELECT user_id, event_timestamp, val FROM catalog.schema.table" + source = TrinoSource( + name="user_source", + query=query, + timestamp_field="event_timestamp", + ) + proto = source.to_proto() + deserialized = TrinoSource.from_proto(proto) + + assert deserialized.name == source.name + assert deserialized.query == source.query + assert deserialized.get_table_query_string() == f"({query})" + + +def test_trino_source_proto_roundtrip_table(): + source = TrinoSource( + table="my_catalog.my_schema.my_table", + timestamp_field="event_timestamp", + ) + proto = source.to_proto() + deserialized = TrinoSource.from_proto(proto) + + assert deserialized.name == source.name + assert deserialized.table == source.table + assert deserialized.get_table_query_string() == "my_catalog.my_schema.my_table"