|
40 | 40 | SemanticCacheState, |
41 | 41 | ) |
42 | 42 | from superset.semantic_layers.cache_identity import SemanticCacheIdentityFactory |
43 | | -from superset.semantic_layers.cache_policy import ContainmentCapabilities |
| 43 | +from superset.semantic_layers.cache_policy import ( |
| 44 | + ContainmentCapabilities, |
| 45 | + PatternSemantics, |
| 46 | +) |
44 | 47 | from superset.semantic_layers.cache_repository import SemanticCacheRepository |
| 48 | +from superset.semantic_layers.mapper import _convert_query_object_filter |
45 | 49 | from tests.unit_tests.semantic_layers.cache_repository_test import ( |
46 | 50 | _Backend, |
47 | 51 | _Coordinator, |
@@ -88,6 +92,94 @@ def test_group_identity_is_order_independent_and_not_flattened() -> None: |
88 | 92 | assert ":v5:" in identity |
89 | 93 |
|
90 | 94 |
|
| 95 | +@pytest.mark.parametrize( |
| 96 | + "operator,value", |
| 97 | + [ |
| 98 | + ("NOT IN", frozenset({"GB"})), |
| 99 | + ("!=", "GB"), |
| 100 | + ("NOT LIKE", "G%"), |
| 101 | + ("NOT ILIKE", "g%"), |
| 102 | + ], |
| 103 | +) |
| 104 | +def test_user_exclusion_cannot_reuse_old_null_dropping_results( |
| 105 | + operator: str, |
| 106 | + value: FilterValues | frozenset[FilterValues], |
| 107 | +) -> None: |
| 108 | + """Normalized user exclusion is distinct from its NULL-dropping SDK leaf.""" |
| 109 | + query: SemanticQuery = build_semantic_query() |
| 110 | + leaf: Filter = Filter( |
| 111 | + PredicateType.WHERE, query.dimensions[0], Operator(operator), value |
| 112 | + ) |
| 113 | + old: SemanticQuery = replace(query, filters={leaf}) |
| 114 | + normalized: set[FilterExpression] | None = _convert_query_object_filter( |
| 115 | + { |
| 116 | + "col": query.dimensions[0].name, |
| 117 | + "op": operator, |
| 118 | + "val": list(value) if isinstance(value, frozenset) else value, |
| 119 | + }, |
| 120 | + {query.dimensions[0].name: query.dimensions[0]}, |
| 121 | + ) |
| 122 | + requested: SemanticQuery = replace(query, filters=normalized) |
| 123 | + assert SemanticCacheIdentityFactory.query( |
| 124 | + old |
| 125 | + ) != SemanticCacheIdentityFactory.query(requested) |
| 126 | + result: SemanticResult = SemanticResult( |
| 127 | + requests=[], results=pa.table({"Country": ["US"], "Revenue": [42.0]}) |
| 128 | + ) |
| 129 | + stored: SemanticQuery |
| 130 | + lookup: SemanticQuery |
| 131 | + for stored, lookup in ((old, requested), (requested, old)): |
| 132 | + repository: SemanticCacheRepository = SemanticCacheRepository( |
| 133 | + _Backend(), _Coordinator() |
| 134 | + ) |
| 135 | + assert repository.store(build_view_meta(), stored, result) |
| 136 | + assert not repository.lookup( |
| 137 | + build_view_meta(), |
| 138 | + lookup, |
| 139 | + ContainmentCapabilities(comparisons=True, membership=True, nulls=True), |
| 140 | + ).candidates |
| 141 | + |
| 142 | + |
| 143 | +@pytest.mark.parametrize("operator", ["NOT LIKE", "NOT ILIKE", "!="]) |
| 144 | +def test_negated_group_reuse_requires_exact_pattern(operator: str) -> None: |
| 145 | + """A broader pattern must not become a cache containment or leftover filter.""" |
| 146 | + query: SemanticQuery = build_semantic_query() |
| 147 | + dimension_name: str = query.dimensions[0].name |
| 148 | + queries: list[SemanticQuery] = [ |
| 149 | + replace( |
| 150 | + query, |
| 151 | + filters=_convert_query_object_filter( |
| 152 | + {"col": dimension_name, "op": operator, "val": pattern}, |
| 153 | + {dimension_name: query.dimensions[0]}, |
| 154 | + ), |
| 155 | + ) |
| 156 | + for pattern in ("G%", "GB%") |
| 157 | + ] |
| 158 | + result: SemanticResult = SemanticResult( |
| 159 | + requests=[], results=pa.table({"Country": [None, "US"], "Revenue": [7.0, 42.0]}) |
| 160 | + ) |
| 161 | + stored: SemanticQuery |
| 162 | + lookup: SemanticQuery |
| 163 | + for stored, lookup in (queries, list(reversed(queries))): |
| 164 | + repository: SemanticCacheRepository = SemanticCacheRepository( |
| 165 | + _Backend(), _Coordinator() |
| 166 | + ) |
| 167 | + assert repository.store(build_view_meta(), stored, result) |
| 168 | + capabilities: ContainmentCapabilities = ContainmentCapabilities( |
| 169 | + comparisons=True, |
| 170 | + membership=True, |
| 171 | + nulls=True, |
| 172 | + pattern_semantics=PatternSemantics.sql_like(escape="\\"), |
| 173 | + ) |
| 174 | + assert repository.lookup(build_view_meta(), stored, capabilities).candidates |
| 175 | + assert ( |
| 176 | + not repository.lookup(build_view_meta(), stored, capabilities) |
| 177 | + .candidates[0] |
| 178 | + .decision.leftover_filters |
| 179 | + ) |
| 180 | + assert not repository.lookup(build_view_meta(), lookup, capabilities).candidates |
| 181 | + |
| 182 | + |
91 | 183 | @pytest.mark.parametrize("location", ["query", "group_limit", "both"]) |
92 | 184 | def test_grouped_query_is_stored_and_served_without_cache_failures( |
93 | 185 | location: str, |
|
0 commit comments