37
37
import org .apache .cassandra .service .QueryState ;
38
38
import org .apache .cassandra .transport .messages .ResultMessage ;
39
39
40
+ import static org .apache .cassandra .cql3 .restrictions .StatementRestrictions .NON_CLUSTER_ORDERING_REQUIRES_INDEX_MESSAGE ;
40
41
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
41
42
import static org .junit .Assert .assertEquals ;
42
43
import static org .junit .Assert .assertNull ;
@@ -57,6 +58,27 @@ public void cannotOrderVarintColumn()
57
58
executeOrderByAndAssertInvalidRequestException ("varint" );
58
59
}
59
60
61
+ @ Test
62
+ public void cannotOrderWithAnalyzedIndex ()
63
+ {
64
+ createTable ("CREATE TABLE %s (pk int primary key, val text)" );
65
+ createIndex ("CREATE CUSTOM INDEX test_v1_idx ON %s(val) USING 'StorageAttachedIndex'" +
66
+ " WITH OPTIONS = {'index_analyzer': '{\" tokenizer\" : { \" name\" : \" whitespace\" , \" args\" : {} }}'}" );
67
+
68
+ execute ("INSERT INTO %s (pk, val) VALUES (1, 'ciao amico')" );
69
+ execute ("INSERT INTO %s (pk, val) VALUES (2, 'ciao amico')" );
70
+ assertRows (execute ("SELECT * FROM %s" ), row (1 , "ciao amico" ), row (2 , "ciao amico" ));
71
+
72
+ // Verify ORDER BY fails with analyzed index
73
+ assertThatThrownBy (() -> execute ("SELECT * FROM %s ORDER BY val LIMIT 10" ))
74
+ .isInstanceOf (InvalidRequestException .class )
75
+ .hasMessage (String .format (NON_CLUSTER_ORDERING_REQUIRES_INDEX_MESSAGE , "val" ));
76
+
77
+ // Verify ORDER BY works with non-analyzed index
78
+ createIndex ("CREATE CUSTOM INDEX ON %s(val) USING 'StorageAttachedIndex'" );
79
+ assertRows (execute ("SELECT * FROM %s ORDER BY val LIMIT 10" ), row (1 , "ciao amico" ), row (2 , "ciao amico" ));
80
+ }
81
+
60
82
@ Test
61
83
public void cannotOrderDecimalColumn ()
62
84
{
@@ -77,10 +99,10 @@ public void cannotOrderTextColumnWithoutIndex()
77
99
{
78
100
createTable ("CREATE TABLE %s (pk int, val text, PRIMARY KEY(pk))" );
79
101
80
- assertInvalidMessage (String .format (StatementRestrictions . NON_CLUSTER_ORDERING_REQUIRES_INDEX_MESSAGE , "val" ),
102
+ assertInvalidMessage (String .format (NON_CLUSTER_ORDERING_REQUIRES_INDEX_MESSAGE , "val" ),
81
103
"SELECT * FROM %s ORDER BY val ASC LIMIT 1" );
82
104
// Also confirm filtering does not make it work.
83
- assertInvalidMessage (String .format (StatementRestrictions . NON_CLUSTER_ORDERING_REQUIRES_INDEX_MESSAGE , "val" ),
105
+ assertInvalidMessage (String .format (NON_CLUSTER_ORDERING_REQUIRES_INDEX_MESSAGE , "val" ),
84
106
"SELECT * FROM %s ORDER BY val LIMIT 5 ALLOW FILTERING" );
85
107
}
86
108
@@ -109,7 +131,7 @@ public void textOrderingMustHaveLimit()
109
131
public void testInvalidColumnName ()
110
132
{
111
133
String table = createTable (KEYSPACE , "CREATE TABLE %s (k int, c int, v int, primary key (k, c))" );
112
- assertInvalidMessage (String .format ("Undefined column name bad_col in table %s" , KEYSPACE + "." + table ),
134
+ assertInvalidMessage (String .format ("Undefined column name bad_col in table %s" , KEYSPACE + '.' + table ),
113
135
"SELECT k from %s ORDER BY bad_col LIMIT 1" );
114
136
}
115
137
0 commit comments