Skip to content

Commit 1de82da

Browse files
adelapenadjatnieks
authored andcommitted
CNDB-13171: Fix query_filters guardrail using the index analyzer instead of the query analyzer (#1615)
Fix `RowFilter.AnalyzableExpression#numFilteredValues`, which is used by the `query_filters` guardrail, to use the query analyzer, instead of the index analyzer. That prevents wrong triggerings of the guardrail.
1 parent 8287a2e commit 1de82da

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/java/org/apache/cassandra/db/filter/RowFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,9 +1293,9 @@ public final Index.Analyzer queryAnalyzer()
12931293
@Override
12941294
public int numFilteredValues()
12951295
{
1296-
return indexAnalyzer == null
1296+
return queryAnalyzer == null
12971297
? super.numFilteredValues()
1298-
: indexAnalyzer().analyze(value).size();
1298+
: queryAnalyzer().analyze(value).size();
12991299
}
13001300
}
13011301

test/unit/org/apache/cassandra/db/guardrails/GuardrailQueryFiltersTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,29 @@ public void testQueryFilters() throws Throwable
175175
assertWarns("SELECT * FROM %s WHERE x = '1 2 3' AND y = '4 5 6' AND z = '7 8 9' ALLOW FILTERING", 3);
176176
}
177177

178+
@Test
179+
public void testQueryFiltersWithIndexAndQueryAnalyzers() throws Throwable
180+
{
181+
createTable("CREATE TABLE %s (k int PRIMARY KEY, v text)");
182+
183+
createIndex("CREATE CUSTOM INDEX ON %s(v) USING 'StorageAttachedIndex' WITH OPTIONS = {" +
184+
"'index_analyzer': '{\n" +
185+
"\t\"tokenizer\":{\"name\":\"ngram\", \"args\":{\"minGramSize\":\"1\", \"maxGramSize\":\"10\"}}," +
186+
"\t\"filters\":[{\"name\":\"lowercase\"}]\n" +
187+
"}'," +
188+
"'query_analyzer': '{\n" +
189+
"\t\"tokenizer\":{\"name\":\"whitespace\"},\n" +
190+
"\t\"filters\":[{\"name\":\"porterstem\"}]\n" +
191+
"}'};");
192+
193+
// only the query analyzer should be used to calculate the number of filters
194+
assertValid("SELECT * FROM %s WHERE v : 'abcdef'");
195+
assertValid("SELECT * FROM %s WHERE v : 'abcdef ghijkl'");
196+
assertWarns("SELECT * FROM %s WHERE v : 'abcdef ghijkl mnopqr'", 3);
197+
assertWarns("SELECT * FROM %s WHERE v : 'abcdef ghijkl mnopqr stuvwx'", 4);
198+
assertFails("SELECT * FROM %s WHERE v : 'abcdef ghijkl mnopqr stuvwx xyz'", 5);
199+
}
200+
178201
@Test
179202
public void testExcludedUsers() throws Throwable
180203
{

0 commit comments

Comments
 (0)