|
18 | 18 |
|
19 | 19 | package org.apache.cassandra.index.sai.cql;
|
20 | 20 |
|
| 21 | +import org.apache.cassandra.exceptions.InvalidRequestException; |
| 22 | +import org.apache.cassandra.index.sai.SAITester; |
| 23 | +import org.apache.cassandra.index.sai.analyzer.NonTokenizingOptions; |
| 24 | +import org.assertj.core.api.Assertions; |
21 | 25 | import org.junit.Test;
|
22 | 26 |
|
23 |
| -public class AnalyzerTest extends VectorTester |
| 27 | +import javax.annotation.Nullable; |
| 28 | +import java.util.Arrays; |
| 29 | + |
| 30 | +public class AnalyzerTest extends SAITester |
24 | 31 | {
|
25 | 32 | @Test
|
26 | 33 | public void createAnalyzerWrongTypeTest()
|
@@ -49,4 +56,72 @@ public void createAnalyzerWrongTypeTest()
|
49 | 56 | execute("INSERT INTO %s (pk1, pk2, val) VALUES (0, 'c', -2)");
|
50 | 57 | execute("INSERT INTO %s (pk1, pk2, val) VALUES (1, 'c', -3)");
|
51 | 58 | }
|
| 59 | + |
| 60 | + /** |
| 61 | + * Test that we cannot use an analyzer, tokenizing or not, on a frozen collection. |
| 62 | + */ |
| 63 | + @Test |
| 64 | + public void analyzerOnFrozenCollectionTest() |
| 65 | + { |
| 66 | + createTable("CREATE TABLE %s (k int PRIMARY KEY, l frozen<list<text>>, s frozen<set<text>>, m frozen<map<text, text>>)"); |
| 67 | + |
| 68 | + for (String column : Arrays.asList("l", "s", "m")) |
| 69 | + { |
| 70 | + assertRejectsNonFullIndexCreationOnFrozenCollection(column); |
| 71 | + column = String.format("full(%s)", column); |
| 72 | + |
| 73 | + // non-tokenizing options that produce an analyzer should be rejected |
| 74 | + assertRejectsAnalyzerOnFrozenCollection(column, String.format("{'%s': %s}", NonTokenizingOptions.CASE_SENSITIVE, false)); |
| 75 | + assertRejectsAnalyzerOnFrozenCollection(column, String.format("{'%s': %s}", NonTokenizingOptions.NORMALIZE, true)); |
| 76 | + assertRejectsAnalyzerOnFrozenCollection(column, String.format("{'%s': %s}", NonTokenizingOptions.ASCII, true)); |
| 77 | + |
| 78 | + // non-tokenizing options that do not produce an analyzer should be accepted |
| 79 | + assertAcceptsIndexOptions(column, String.format("{'%s': %s}", NonTokenizingOptions.CASE_SENSITIVE, true)); |
| 80 | + assertAcceptsIndexOptions(column, String.format("{'%s': %s}", NonTokenizingOptions.NORMALIZE, false)); |
| 81 | + assertAcceptsIndexOptions(column, String.format("{'%s': %s}", NonTokenizingOptions.ASCII, false)); |
| 82 | + |
| 83 | + // Lucene analyzer should always be rejected |
| 84 | + assertRejectsAnalyzerOnFrozenCollection(column, "{'index_analyzer': 'standard'}"); |
| 85 | + assertRejectsAnalyzerOnFrozenCollection(column, |
| 86 | + "{'index_analyzer': " + |
| 87 | + " '{\"tokenizer\":{\"name\":\"ngram\", \"args\":{\"minGramSize\":\"2\", \"maxGramSize\":\"3\"}}," + |
| 88 | + " \"filters\":[{\"name\":\"lowercase\"}]}'}"); |
| 89 | + assertRejectsAnalyzerOnFrozenCollection(column, |
| 90 | + "{'index_analyzer':'\n" + |
| 91 | + " {\"tokenizer\":{\"name\" : \"whitespace\"},\n" + |
| 92 | + " \"filters\":[{\"name\":\"stop\", \"args\": {\"words\": \"the, test\", \"format\": \"wordset\"}}]}'}"); |
| 93 | + |
| 94 | + // no options should be accepted |
| 95 | + assertAcceptsIndexOptions(column, null); |
| 96 | + assertAcceptsIndexOptions(column, "{}"); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private void assertRejectsNonFullIndexCreationOnFrozenCollection(String column) |
| 101 | + { |
| 102 | + Assertions.assertThatThrownBy(() -> createSAIIndex(column, null)) |
| 103 | + .isInstanceOf(InvalidRequestException.class) |
| 104 | + .hasMessageContaining("Cannot create values() index on frozen column " + column); |
| 105 | + |
| 106 | + Assertions.assertThatThrownBy(() -> createSAIIndex("KEYS(" + column + ')', null)) |
| 107 | + .isInstanceOf(InvalidRequestException.class) |
| 108 | + .hasMessageContaining("Cannot create keys() index on frozen column " + column); |
| 109 | + |
| 110 | + Assertions.assertThatThrownBy(() -> createSAIIndex("VALUES(" + column + ')', null)) |
| 111 | + .isInstanceOf(InvalidRequestException.class) |
| 112 | + .hasMessageContaining("Cannot create values() index on frozen column " + column); |
| 113 | + } |
| 114 | + |
| 115 | + private void assertAcceptsIndexOptions(String column, @Nullable String options) |
| 116 | + { |
| 117 | + String index = createSAIIndex(column, options); |
| 118 | + dropIndex("DROP INDEX %s." + index); // clear for further tests |
| 119 | + } |
| 120 | + |
| 121 | + private void assertRejectsAnalyzerOnFrozenCollection(String column, String options) |
| 122 | + { |
| 123 | + Assertions.assertThatThrownBy(() -> createSAIIndex(column, options)) |
| 124 | + .isInstanceOf(InvalidRequestException.class) |
| 125 | + .hasMessageContaining("Cannot use an analyzer on " + column + " because it's a frozen collection."); |
| 126 | + } |
52 | 127 | }
|
0 commit comments