Skip to content

Commit 2ddee28

Browse files
authored
test(semantic-layer): cover grouped exclusion filters in conformance kit (#46549)
1 parent 6fc327e commit 2ddee28

7 files changed

Lines changed: 394 additions & 44 deletions

File tree

‎UPDATING.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ assists people when migrating to a new version.
2424

2525
## Next
2626

27+
- Provider tests inheriting `SemanticProviderConformance` must supply four
28+
additional `semantic_query_cases`: `or_not_in`, `or_not_equals`, `or_not_like`,
29+
and `or_not_ilike`. These check the existing normalized exclusion-filter
30+
contract, including NULL retention and outer AND predicates. Update any
31+
explicit conformance-case allowlist when adopting the kit; see the
32+
[provider testing guide](docs/developer_docs/semantic-layer-provider-testing.md).
33+
2734
- Dashboard history records related chart archives using the new
2835
`version_chart_archive_scope` table. Run `superset db upgrade` before deploying
2936
new archive writes with versioning capture enabled. During a rollout, activity

‎docs/developer_docs/semantic-layer-provider-testing.md‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ separate integration tests.
5959
| `semantic_change_catalog` | No-argument callable changing the controlled source catalog: add a visible member on every call, without clearing provider caches. |
6060
| `semantic_provider_class` | `(importable_module_name, class_name)` of the layer for an isolated import and static-schema check. Dependencies must already be installed. |
6161

62-
Required query cases are `identity`, `grain`, `null`, `not_null`, `or`, and
63-
`order_limit`. Use member names distinct from private IDs and display labels.
62+
Required query cases are `identity`, `grain`, `null`, `not_null`, `or`,
63+
`or_not_in`, `or_not_equals`, `or_not_like`, `or_not_ilike`, and `order_limit`.
64+
Use member names distinct from private IDs and display labels.
6465
For `grain`, use a grain-bearing dimension with data whose aggregation distinguishes the selected grain from raw values. Multiple grain variants for one name are allowed, not required. When the catalog declares no grain-bearing dimension, the case skips with an explicit reason before looking up its fixture entry; such providers may omit the `grain` entry. This reports unexercised grain coverage, not a passing grain check.
6566
NULL and grouped-OR fixtures must select NULL and non-NULL branches observably;
6667
the `or` case must use the host-normalized shape: one `OrFilter` containing
@@ -74,6 +75,22 @@ The SQLite example uses a February lower bound, with A and NULL rows on both sid
7475
of that bound: the correct totals are NULL=1, A=5, B=8. This distinguishes either
7576
ordering of the outer AND and the OR group. A February C row with amount 6 must
7677
be excluded; dropping the OR group incorrectly adds C=6 to the result.
78+
The four exclusion cases use the same grouped shape, replacing `IN` with
79+
`NOT IN`, `!=`, `NOT LIKE`, or `NOT ILIKE`, respectively. The `NOT IN` operand
80+
must contain at least two non-NULL values; pattern operands must contain a
81+
wildcard. Keep an outer AND comparison and group by the filtered dimension,
82+
with both NULL and non-NULL groups in the expected result. Seed rows on both
83+
sides of the outer comparison and rows excluded by the negated leaf, so dropping
84+
the NULL branch, the group, its parentheses or the outer predicate changes the
85+
answer. A plain non-empty negated leaf still excludes NULL: it is the explicit
86+
`IS_NULL` branch that retains NULL rows in a user exclusion.
87+
The SQLite example expects NULL=1 and C=6 for `NOT IN {A, B}`; NULL=1, C=6,
88+
B=8 for `!= A`; and NULL=1, A=5, C=6 for `NOT LIKE 'B%'` and `NOT ILIKE 'b%'`.
89+
These are required conformance cases, not optional advertised capabilities.
90+
The example also has a January C row, so a lost pair of parentheses is observable
91+
whether the outer AND predicate appears before or after the OR group.
92+
Consumers upgrading the kit must provide the four fixture entries and review
93+
the new case IDs in any explicit case allowlist before certifying providers.
7794
Seed more result rows than the limit, in an order that differs from the requested
7895
sort. The suite compares exact Arrow column names, types, values and row order,
7996
without prescribing column order or field nullability.

‎superset-core/src/superset_core/semantic_layers/testing.py‎

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,19 @@ def test_catalog_identity(
159159
), "preferred_temporal_dimension must remain stable on equivalent reload"
160160

161161
@pytest.mark.parametrize(
162-
"case", ["identity", "grain", "null", "not_null", "or", "order_limit"]
162+
"case",
163+
[
164+
"identity",
165+
"grain",
166+
"null",
167+
"not_null",
168+
"or",
169+
"or_not_in",
170+
"or_not_equals",
171+
"or_not_like",
172+
"or_not_ilike",
173+
"order_limit",
174+
],
163175
)
164176
def test_query_outputs(
165177
self,
@@ -195,6 +207,25 @@ def test_query_outputs(
195207
)
196208
elif case == "or":
197209
_assert_normalized_or_witness(query)
210+
elif case in {"or_not_in", "or_not_equals", "or_not_like", "or_not_ilike"}:
211+
exclusion: Operator = {
212+
"or_not_in": Operator.NOT_IN,
213+
"or_not_equals": Operator.NOT_EQUALS,
214+
"or_not_like": Operator.NOT_LIKE,
215+
"or_not_ilike": Operator.NOT_ILIKE,
216+
}[case]
217+
_assert_normalized_or_witness(query, exclusion)
218+
group: OrFilter = next(
219+
item for item in query.filters or set() if isinstance(item, OrFilter)
220+
)
221+
member: Dimension | Metric | None = next(iter(group.filters)).column
222+
assert member is not None, "exclusion fixture requires a member"
223+
assert member in query.dimensions, (
224+
"exclusion fixture must group by the filtered dimension"
225+
)
226+
assert 0 < expected.column(member.name).null_count < expected.num_rows, (
227+
"exclusion fixture must retain NULL and non-NULL groups"
228+
)
198229
elif case == "order_limit":
199230
assert query.order
200231
assert query.limit is not None
@@ -437,34 +468,53 @@ def test_imports_and_static_schema(
437468
)
438469

439470

440-
def _assert_normalized_or_witness(query: SemanticQuery) -> None:
441-
"""Require the host's mixed-IN/NULL group and an outer comparison."""
471+
def _assert_normalized_or_witness(
472+
query: SemanticQuery, operator: Operator = Operator.IN
473+
) -> None:
474+
"""Require a normalized comparison/NULL group and an outer comparison."""
442475
groups: list[OrFilter] = [
443476
item for item in query.filters or set() if isinstance(item, OrFilter)
444477
]
445-
assert len(groups) == 1, "or fixture requires one normalized IN/NULL group"
478+
assert len(groups) == 1, (
479+
f"or fixture requires one normalized {operator.value}/NULL group"
480+
)
446481
group: OrFilter = groups[0]
447482
membership: list[Filter] = [
448-
leaf for leaf in group.filters if leaf.operator == Operator.IN
483+
leaf for leaf in group.filters if leaf.operator == operator
449484
]
450485
nulls: list[Filter] = [
451486
leaf for leaf in group.filters if leaf.operator == Operator.IS_NULL
452487
]
453-
assert len(group.filters) == 2, "or fixture requires IN and IS_NULL leaves"
454-
assert len(membership) == 1, "or fixture requires IN and IS_NULL leaves"
455-
assert len(nulls) == 1, "or fixture requires IN and IS_NULL leaves"
488+
message: str = f"or fixture requires {operator.value} and IS_NULL leaves"
489+
assert len(group.filters) == 2, message
490+
assert len(membership) == 1, message
491+
assert len(nulls) == 1, message
456492
selected: Filter = membership[0]
457493
assert selected.column is not None, "or fixture leaves must select the same member"
458494
assert selected.column == nulls[0].column, (
459495
"or fixture leaves must select the same member"
460496
)
461-
assert isinstance(selected.value, frozenset), "or fixture IN requires a frozenset"
462-
assert len(selected.value) >= 2, (
463-
"or fixture IN requires at least two non-NULL values"
464-
)
465-
assert None not in selected.value, (
466-
"or fixture IN requires at least two non-NULL values"
467-
)
497+
if operator in {Operator.IN, Operator.NOT_IN}:
498+
assert isinstance(selected.value, frozenset), (
499+
"or fixture membership requires a frozenset"
500+
)
501+
assert len(selected.value) >= 2, (
502+
"or fixture membership requires at least two non-NULL values"
503+
)
504+
assert None not in selected.value, (
505+
"or fixture membership requires at least two non-NULL values"
506+
)
507+
else:
508+
assert selected.value is not None, (
509+
"or fixture comparison requires a non-NULL value"
510+
)
511+
if operator in {Operator.NOT_LIKE, Operator.NOT_ILIKE}:
512+
assert isinstance(selected.value, str), (
513+
"or fixture pattern requires a string"
514+
)
515+
assert any(char in selected.value for char in "%_"), (
516+
"or fixture pattern requires a wildcard"
517+
)
468518
assert any(
469519
isinstance(item, Filter)
470520
and item.type == selected.type

‎superset-core/tests/semantic_layers/conformance_provider.py‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def execute(self, sql: str, parameters: list[object]) -> list[tuple[Any, ...]]:
6060
raise ConnectionError("test backend unavailable")
6161
connection: sqlite3.Connection
6262
with closing(sqlite3.connect(":memory:")) as connection:
63+
connection.execute("PRAGMA case_sensitive_like = ON")
6364
connection.execute(
6465
"CREATE TABLE records(category TEXT, occurred TEXT, amount INT)"
6566
)
@@ -68,6 +69,7 @@ def execute(self, sql: str, parameters: list[object]) -> list[tuple[Any, ...]]:
6869
[
6970
("B", "2026-02-04", 8),
7071
("C", "2026-02-07", 6),
72+
("C", "2026-01-04", 4),
7173
(None, "2026-01-03", 3),
7274
(None, "2026-02-06", 1),
7375
("A", "2026-02-05", 5),
@@ -213,6 +215,11 @@ def _predicate(expression: FilterExpression, parameters: list[object]) -> str:
213215
raise SemanticQueryRejectedError(SemanticQueryErrorCode.INVALID_FILTER)
214216
if expression.column is None or expression.column.name != "category":
215217
raise SemanticQueryRejectedError(SemanticQueryErrorCode.INVALID_FILTER)
218+
return _category_predicate(expression, parameters)
219+
220+
221+
def _category_predicate(expression: Filter, parameters: list[object]) -> str:
222+
"""Render category predicates with SQL NULL semantics and bound operands."""
216223
if expression.operator == Operator.IS_NULL:
217224
return "category IS NULL"
218225
if expression.operator == Operator.IS_NOT_NULL:
@@ -222,11 +229,20 @@ def _predicate(expression: FilterExpression, parameters: list[object]) -> str:
222229
if expression.operator == Operator.EQUALS and expression.value is not None:
223230
parameters.append(expression.value)
224231
return "category = ?"
232+
if expression.operator in {
233+
Operator.NOT_EQUALS,
234+
Operator.NOT_LIKE,
235+
Operator.NOT_ILIKE,
236+
} and isinstance(expression.value, str):
237+
parameters.append(expression.value)
238+
if expression.operator == Operator.NOT_ILIKE:
239+
return "LOWER(category) NOT LIKE LOWER(?)"
240+
return f"category {expression.operator.value} ?"
225241
raise SemanticQueryRejectedError(SemanticQueryErrorCode.INVALID_FILTER)
226242

227243

228244
def _membership(expression: Filter, parameters: list[object]) -> str:
229-
"""Render IN values; empty IN is FALSE and empty NOT IN is TRUE."""
245+
"""Render bound membership values, including empty-set constants."""
230246
if not isinstance(expression.value, frozenset):
231247
raise SemanticQueryRejectedError(SemanticQueryErrorCode.INVALID_FILTER)
232248
if not expression.value:
@@ -235,10 +251,12 @@ def _membership(expression: Filter, parameters: list[object]) -> str:
235251
values: list[str] = sorted(
236252
value for value in expression.value if isinstance(value, str)
237253
)
238-
if expression.operator != Operator.IN or len(values) != len(expression.value):
254+
if len(values) != len(expression.value):
239255
raise SemanticQueryRejectedError(SemanticQueryErrorCode.INVALID_FILTER)
240256
parameters.extend(values)
241-
return "category IN (" + ", ".join("?" for _ in expression.value) + ")"
257+
return (
258+
f"category {expression.operator.value} (" + ", ".join("?" for _ in values) + ")"
259+
)
242260

243261

244262
def _result(

0 commit comments

Comments
 (0)