jonmeredith commented on a change in pull request #893: URL: https://github.com/apache/cassandra/pull/893#discussion_r574731117
########## File path: test/unit/org/apache/cassandra/cql3/KeywordTest.java ########## @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.cql3; + +import java.util.Arrays; +import java.util.Collection; +import java.util.stream.Collectors; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import org.apache.cassandra.exceptions.SyntaxException; + +@RunWith(Parameterized.class) +public class KeywordTest extends CQLTester +{ + @Parameterized.Parameters(name = "keyword {0} isReserved {1}") + public static Collection<Object[]> keywords() { + return Arrays.stream(CqlParser.tokenNames) + .filter(k -> k.startsWith("K_")) + .map(k -> { + String keyword = k.substring(2); + return new Object[]{ keyword, ReservedKeywords.isReserved(keyword) }; + }) + .collect(Collectors.toSet()); + } + + String keyword; + boolean isReserved; + public KeywordTest(String keyword, boolean isReserved) + { + this.keyword = keyword; + this.isReserved = isReserved; + } + + @Test + public void test() throws Throwable + { + String createStatement = String.format("CREATE TABLE %s.%s (c text, %s text, PRIMARY KEY (c, %s))", + KEYSPACE, keyword, keyword, keyword, keyword); + logger.info(createStatement); + if (isReserved) + { + try + { + schemaChange(createStatement); + Assert.fail(String.format("Reserved keyword %s should not have parsed", keyword)); + } + catch (RuntimeException ignore) // SyntaxException is wrapped by CQLTester + { + Assert.assertEquals(SyntaxException.class, ignore.getCause().getClass()); + } + } + else // not a reserved word + { + /* Create the table using the keyword as a tablename and column name, then call describe and re-create it + * using the create_statement result. + */ + schemaChange(createStatement); + String describeStatement = String.format("DESCRIBE TABLE %s.%s", KEYSPACE, keyword); + UntypedResultSet rs = execute(describeStatement); + UntypedResultSet.Row row = rs.one(); + String describedCreateStatement = row.getString("create_statement"); + + String recreateStatement = describedCreateStatement.replace(KEYSPACE, KEYSPACE_PER_TEST); + logger.info(recreateStatement); + schemaChange(recreateStatement); + + /* Check it is possible to insert and select from the table/column, with the keyword used in the + * where clause + */ + String insertStatement = String.format("INSERT INTO %s.%s(c,%s) VALUES ('x', '%s')", + KEYSPACE, keyword, keyword, keyword); + logger.info(insertStatement); + execute(insertStatement); + + String selectStatement = String.format("SELECT c, %s FROM %s.%s WHERE c = 'x' AND %s >= ''", + keyword, KEYSPACE, keyword, keyword); + logger.info(selectStatement); + rs = execute(selectStatement); + row = rs.one(); + String value = row.getString(keyword.toLowerCase()); + Assert.assertEquals(keyword, value); + + /* Make a materialized view using the fields (cannot re-use the name as MV must be in same keyspace). + * Added as CASSANDRA-11803 motivated adding the reserved/unreserved distinction + */ + String createMV = String.format("CREATE MATERIALIZED VIEW %s.mv_%s AS %s PRIMARY KEY (c, %s)", + KEYSPACE, keyword, selectStatement, keyword); + logger.info(createMV); + schemaChange(createMV); + } + } +} Review comment: Will do, sorry I didn't realize you added it or I would have extended it - though I like that the parameterized test checks all the keywords. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

