Skip to content

Commit ebc4bc6

Browse files
authored
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 afda625 commit ebc4bc6

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
@@ -1164,9 +1164,9 @@ public final Index.Analyzer queryAnalyzer()
11641164
@Override
11651165
public int numFilteredValues()
11661166
{
1167-
return indexAnalyzer == null
1167+
return queryAnalyzer == null
11681168
? super.numFilteredValues()
1169-
: indexAnalyzer().analyze(value).size();
1169+
: queryAnalyzer().analyze(value).size();
11701170
}
11711171
}
11721172

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

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

205+
@Test
206+
public void testQueryFiltersWithIndexAndQueryAnalyzers() throws Throwable
207+
{
208+
createTable("CREATE TABLE %s (k int PRIMARY KEY, v text)");
209+
210+
createIndex("CREATE CUSTOM INDEX ON %s(v) USING 'StorageAttachedIndex' WITH OPTIONS = {" +
211+
"'index_analyzer': '{\n" +
212+
"\t\"tokenizer\":{\"name\":\"ngram\", \"args\":{\"minGramSize\":\"1\", \"maxGramSize\":\"10\"}}," +
213+
"\t\"filters\":[{\"name\":\"lowercase\"}]\n" +
214+
"}'," +
215+
"'query_analyzer': '{\n" +
216+
"\t\"tokenizer\":{\"name\":\"whitespace\"},\n" +
217+
"\t\"filters\":[{\"name\":\"porterstem\"}]\n" +
218+
"}'};");
219+
220+
// only the query analyzer should be used to calculate the number of filters
221+
assertValid("SELECT * FROM %s WHERE v = 'abcdef'");
222+
assertValid("SELECT * FROM %s WHERE v = 'abcdef ghijkl'");
223+
assertWarns("SELECT * FROM %s WHERE v = 'abcdef ghijkl mnopqr'", 3);
224+
assertWarns("SELECT * FROM %s WHERE v = 'abcdef ghijkl mnopqr stuvwx'", 4);
225+
assertFails("SELECT * FROM %s WHERE v = 'abcdef ghijkl mnopqr stuvwx xyz'", 5);
226+
}
227+
205228
@Test
206229
public void testExcludedUsers() throws Throwable
207230
{

0 commit comments

Comments
 (0)