Skip to content

Commit 234763f

Browse files
committed
Remove Accord CQL words from the reserved keywords list
patch by David Capwell; reviewed by Caleb Rackliffe for CASSANDRA-20613
1 parent 4971b4c commit 234763f

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

pylib/cqlshlib/cqlhandling.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
Hint = pylexotron.Hint
2626

2727
cql_keywords_reserved = {'add', 'allow', 'alter', 'and', 'apply', 'asc', 'authorize', 'batch', 'begin', 'by',
28-
'columnfamily', 'create', 'commit', 'delete', 'desc', 'describe', 'drop', 'end', 'entries', 'execute', 'from',
29-
'full', 'grant', 'if', 'in', 'index', 'infinity', 'insert', 'into', 'is', 'keyspace', 'let', 'limit',
28+
'columnfamily', 'create', 'delete', 'desc', 'describe', 'drop', 'entries', 'execute', 'from',
29+
'full', 'grant', 'if', 'in', 'index', 'infinity', 'insert', 'into', 'is', 'keyspace', 'limit',
3030
'materialized', 'modify', 'nan', 'norecursive', 'not', 'null', 'of', 'on', 'or', 'order',
31-
'primary', 'rename', 'revoke', 'schema', 'select', 'set', 'table', 'then', 'to', 'token', 'transaction', 'truncate',
31+
'primary', 'rename', 'revoke', 'schema', 'select', 'set', 'table', 'to', 'token', 'truncate',
3232
'unlogged', 'update', 'use', 'using', 'view', 'where', 'with'}
3333
"""
3434
Set of reserved keywords in CQL.

src/antlr/Parser.g

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,5 +2310,10 @@ basic_unreserved_keyword returns [String str]
23102310
| K_BETWEEN
23112311
| K_CHECK
23122312
| K_INDEXES
2313+
| K_COMMIT
2314+
| K_END
2315+
| K_LET
2316+
| K_THEN
2317+
| K_TRANSACTION
23132318
) { $str = $k.text; }
23142319
;

src/resources/org/apache/cassandra/cql3/reserved_keywords.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ BATCH
99
BEGIN
1010
BY
1111
COLUMNFAMILY
12-
COMMIT
1312
CREATE
1413
DELETE
1514
DESC
1615
DESCRIBE
1716
DROP
18-
END
1917
ENTRIES
2018
EXECUTE
2119
FROM
@@ -29,7 +27,6 @@ INSERT
2927
INTO
3028
IS
3129
KEYSPACE
32-
LET
3330
LIMIT
3431
MATERIALIZED
3532
MODIFY
@@ -48,10 +45,8 @@ SCHEMA
4845
SELECT
4946
SET
5047
TABLE
51-
THEN
5248
TO
5349
TOKEN
54-
TRANSACTION
5550
TRUNCATE
5651
UNLOGGED
5752
UPDATE

test/unit/org/apache/cassandra/cql3/ReservedKeywordsTest.java

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818

1919
package org.apache.cassandra.cql3;
2020

21+
import java.lang.reflect.Modifier;
22+
2123
import org.junit.Test;
2224

2325
import org.junit.Assert;
26+
2427
import org.apache.cassandra.exceptions.SyntaxException;
28+
import org.assertj.core.api.SoftAssertions;
2529

2630
public class ReservedKeywordsTest
2731
{
@@ -30,14 +34,51 @@ public void testReservedWordsForColumns()
3034
{
3135
for (String reservedWord : ReservedKeywords.reservedKeywords)
3236
{
33-
try
34-
{
35-
QueryProcessor.parseStatement(String.format("ALTER TABLE ks.t ADD %s TEXT", reservedWord));
37+
if (isAllowed(reservedWord))
3638
Assert.fail(String.format("Reserved keyword %s should not have parsed", reservedWord));
37-
}
38-
catch (SyntaxException ignore)
39-
{
40-
}
39+
}
40+
}
41+
42+
@Test
43+
public void parserAndTextFileMatch()
44+
{
45+
// If this test starts to fail that means that the lexer added a new keyword, and this keyword was not updated
46+
// to be unreserved.
47+
//
48+
// To mark a keyword as unreserved, open "Parser.g" and search for
49+
// basic_unreserved_keyword returns [String str]
50+
// or
51+
// unreserved_keyword returns [String str]
52+
// Add your keyword there and rebuild the jar (to generate the parser).
53+
//
54+
// If it is desired to make this keyword reserved, then you must first go to the mailing list and request a vote
55+
// on this change, if that vote passes then you can update "reserved_keywords.txt" (and pylib/cqlshlib/cqlhandling.py::cql_keywords_reserved).
56+
// Never update "reserved_keywords.txt" without a vote on the mailing list!
57+
SoftAssertions asserts = new SoftAssertions();
58+
for (var f : Cql_Lexer.class.getDeclaredFields())
59+
{
60+
if (!Modifier.isStatic(f.getModifiers())) continue;
61+
if (!f.getName().startsWith("K_")) continue;
62+
String name = f.getName();
63+
String keyword = name.replaceFirst("K_", "");
64+
65+
asserts.assertThat(ReservedKeywords.isReserved(keyword))
66+
.describedAs(keyword)
67+
.isEqualTo(!isAllowed(keyword));
68+
}
69+
asserts.assertAll();
70+
}
71+
72+
private static boolean isAllowed(String keyword)
73+
{
74+
try
75+
{
76+
QueryProcessor.parseStatement(String.format("ALTER TABLE ks.t ADD %s TEXT", keyword));
77+
return true;
78+
}
79+
catch (SyntaxException ignore)
80+
{
81+
return false;
4182
}
4283
}
4384
}

0 commit comments

Comments
 (0)