Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/antlr/Parser.g
Original file line number Diff line number Diff line change
Expand Up @@ -2363,12 +2363,14 @@ vector_type returns [CQL3Type.Raw vt]
username
: IDENT
| STRING_LITERAL
| unreserved_keyword
| QUOTED_NAME { addRecognitionError("Quoted strings are are not supported for user names and USER is deprecated, please use ROLE");}
;

identity
: IDENT
| STRING_LITERAL
| unreserved_keyword
| QUOTED_NAME { addRecognitionError("Quoted strings are are not supported for identity");}
;

Expand Down
33 changes: 32 additions & 1 deletion test/unit/org/apache/cassandra/cql3/ReservedKeywordsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,42 @@ public void parserAndTextFileMatch()
asserts.assertAll();
}

/**
* Legacy USER and IDENTITY statements must accept unreserved keywords as names, just like
* role statements do ({@code roleName} accepts {@code unreserved_keyword}). Otherwise adding
* a new keyword to the grammar breaks existing deployments with a user or identity of that name.
*/
@Test
public void testUnreservedKeywordsAsUserNameAndIdentity()
{
SoftAssertions asserts = new SoftAssertions();
for (var f : Cql_Lexer.class.getDeclaredFields())
{
if (!Modifier.isStatic(f.getModifiers())) continue;
if (!f.getName().startsWith("K_")) continue;
String keyword = f.getName().replaceFirst("K_", "");
if (ReservedKeywords.isReserved(keyword)) continue;

for (String statement : new String[]{ "DROP USER %s", "DROP IDENTITY %s" })
{
asserts.assertThat(parses(String.format(statement, keyword)))
.describedAs(String.format(statement, keyword))
.isTrue();
}
}
asserts.assertAll();
}

private static boolean isAllowed(String keyword)
{
return parses(String.format("ALTER TABLE ks.t ADD %s TEXT", keyword));
}

private static boolean parses(String cql)
{
try
{
QueryProcessor.parseStatement(String.format("ALTER TABLE ks.t ADD %s TEXT", keyword));
QueryProcessor.parseStatement(cql);
return true;
}
catch (SyntaxException ignore)
Expand Down