Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,12 @@ def translate_compound(self, f: CompoundFilter) -> Tuple[sql.Composable, List[An
all_params: List[Any] = []
for sub in f.filters:
sub_clause, sub_params = self._dispatch(sub)
if sub_clause == sql.SQL(""):
continue
parts.append(sub_clause)
all_params.extend(sub_params)
if not parts:
return sql.SQL(""), []
joiner = sql.SQL(" AND " if f.type == "and" else " OR ")
combined = sql.SQL("(") + joiner.join(parts) + sql.SQL(")")
return combined, all_params
Expand Down
4 changes: 4 additions & 0 deletions sdk/python/feast/infra/online_stores/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,12 @@ def translate_compound(self, f: CompoundFilter) -> Tuple[str, List[Any]]:
all_params: List[Any] = []
for sub in f.filters:
sub_clause, sub_params = self._dispatch(sub)
if not sub_clause:
continue
parts.append(sub_clause)
all_params.extend(sub_params)
if not parts:
return "", []
joiner = " AND " if f.type == "and" else " OR "
combined = "(" + joiner.join(parts) + ")"
return combined, all_params
Expand Down
60 changes: 60 additions & 0 deletions sdk/python/tests/unit/test_filter_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sqlite3

import pytest
from pydantic import ValidationError

Expand All @@ -6,6 +8,10 @@
CompoundFilter,
convert_dict_to_filter,
)
from feast.infra.online_stores.postgres_online_store.postgres import (
PostgresFilterTranslator,
)
from feast.infra.online_stores.sqlite import SqliteFilterTranslator


class TestComparisonFilter:
Expand Down Expand Up @@ -158,3 +164,57 @@ def test_in_operator_dict(self):
assert isinstance(result, ComparisonFilter)
assert result.type == "in"
assert result.value == ["active", "pending"]


class TestSqlFilterTranslatorEmptyCompound:
def _filter_with_empty_group(self):
return CompoundFilter(
type="and",
filters=[
ComparisonFilter(type="eq", key="title", value="Title 1"),
CompoundFilter(type="or", filters=[]),
],
)

def test_sqlite_skips_empty_nested_group(self):
clause, params = SqliteFilterTranslator("t", alias="fv").translate(
self._filter_with_empty_group()
)
assert params == ["title", "Title 1"]
assert clause == (
'(fv.entity_key IN (SELECT entity_key FROM "t" '
"WHERE feature_name = ? AND value_text = ?))"
)

conn = sqlite3.connect(":memory:")
conn.execute("CREATE TABLE t (entity_key, feature_name, value_text, value_num)")
conn.execute(f"SELECT * FROM t fv WHERE {clause}", params).fetchall()

def test_sqlite_only_empty_groups(self):
f = CompoundFilter(
type="and",
filters=[
CompoundFilter(type="or", filters=[]),
CompoundFilter(type="and", filters=[]),
],
)
assert SqliteFilterTranslator("t").translate(f) == ("", [])

def test_postgres_skips_empty_nested_group(self):
clause, params = PostgresFilterTranslator("t", alias="t1").translate(
self._filter_with_empty_group()
)
assert params == ["title", "Title 1"]
assert clause.as_string(None) == (
'(t1.entity_key IN (SELECT entity_key FROM "t" '
'WHERE feature_name = %s AND "value_text" = %s))'
)

def test_postgres_only_empty_groups(self):
f = CompoundFilter(
type="or",
filters=[CompoundFilter(type="and", filters=[])],
)
clause, params = PostgresFilterTranslator("t").translate(f)
assert clause.as_string(None) == ""
assert params == []