Merge branch 'cassandra-2.0' into cassandra-2.1 Conflicts: src/java/org/apache/cassandra/cql3/statements/SelectStatement.java test/unit/org/apache/cassandra/cql3/SelectWithTokenFunctionTest.java
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/c65ef9af Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/c65ef9af Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/c65ef9af Branch: refs/heads/cassandra-2.1 Commit: c65ef9af6529086ef1793d24b3f387012bf59091 Parents: c2c9835 b1166c0 Author: Aleksey Yeschenko <alek...@apache.org> Authored: Tue Sep 23 18:59:00 2014 -0700 Committer: Aleksey Yeschenko <alek...@apache.org> Committed: Tue Sep 23 18:59:00 2014 -0700 ---------------------------------------------------------------------- .../cassandra/cql3/statements/SelectStatement.java | 4 ++-- .../cassandra/cql3/SelectWithTokenFunctionTest.java | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/c65ef9af/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java ---------------------------------------------------------------------- diff --cc src/java/org/apache/cassandra/cql3/statements/SelectStatement.java index 22c8468,aadd0bd..ccda356 --- a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java +++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java @@@ -1821,22 -1813,22 +1821,22 @@@ public class SelectStatement implement // index with filtering, we'll need to handle it though. throw new InvalidRequestException("Only EQ and IN relation are supported on the partition key (unless you use the token() function)"); } - previous = cname; + previous = cdef; } - if (stmt.onToken && cfm.partitionKeyColumns().size() > 0) + if (stmt.onToken) - checkTokenFunctionArgumentsOrder(cfDef); + checkTokenFunctionArgumentsOrder(cfm); } /** * Checks that the column identifiers used as argument for the token function have been specified in the * partition key order. - * @param cfDef the Column Family Definition + * @param cfm the Column Family MetaData * @throws InvalidRequestException if the arguments have not been provided in the proper order. */ - private void checkTokenFunctionArgumentsOrder(CFDefinition cfDef) throws InvalidRequestException + private void checkTokenFunctionArgumentsOrder(CFMetaData cfm) throws InvalidRequestException { - Iterator<ColumnDefinition> iter = cfm.partitionKeyColumns().iterator(); - Iterator<Name> iter = Iterators.cycle(cfDef.partitionKeys()); ++ Iterator<ColumnDefinition> iter = Iterators.cycle(cfm.partitionKeyColumns()); for (Relation relation : whereClause) { SingleColumnRelation singleColumnRelation = (SingleColumnRelation) relation; http://git-wip-us.apache.org/repos/asf/cassandra/blob/c65ef9af/test/unit/org/apache/cassandra/cql3/SelectWithTokenFunctionTest.java ---------------------------------------------------------------------- diff --cc test/unit/org/apache/cassandra/cql3/SelectWithTokenFunctionTest.java index 73a7209,9199862..6f9f5e2 --- a/test/unit/org/apache/cassandra/cql3/SelectWithTokenFunctionTest.java +++ b/test/unit/org/apache/cassandra/cql3/SelectWithTokenFunctionTest.java @@@ -17,39 -17,155 +17,50 @@@ */ package org.apache.cassandra.cql3; -import org.apache.cassandra.SchemaLoader; -import org.apache.cassandra.db.ConsistencyLevel; -import org.apache.cassandra.exceptions.InvalidRequestException; -import org.apache.cassandra.exceptions.SyntaxException; -import org.apache.cassandra.gms.Gossiper; -import org.apache.cassandra.service.ClientState; -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import static org.apache.cassandra.cql3.QueryProcessor.process; -import static org.apache.cassandra.cql3.QueryProcessor.processInternal; -import static org.junit.Assert.assertEquals; - -public class SelectWithTokenFunctionTest +public class SelectWithTokenFunctionTest extends CQLTester { - private static final Logger logger = LoggerFactory.getLogger(SelectWithTokenFunctionTest.class); - static ClientState clientState; - static String keyspace = "token_function_test"; - - @BeforeClass - public static void setUpClass() throws Throwable - { - SchemaLoader.loadSchema(); - executeSchemaChange("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}"); - executeSchemaChange("CREATE TABLE IF NOT EXISTS %s.single_partition (a int PRIMARY KEY, b text)"); - executeSchemaChange("CREATE TABLE IF NOT EXISTS %s.compound_partition (a int, b text, PRIMARY KEY ((a, b)))"); - executeSchemaChange("CREATE TABLE IF NOT EXISTS %s.single_clustering (a int, b text, PRIMARY KEY (a, b))"); - clientState = ClientState.forInternalCalls(); - } - - @AfterClass - public static void stopGossiper() - { - Gossiper.instance.stop(); - } - - private static void executeSchemaChange(String query) throws Throwable - { - try - { - process(String.format(query, keyspace), ConsistencyLevel.ONE); - } - catch (RuntimeException exc) - { - throw exc.getCause(); - } - } - - private static UntypedResultSet execute(String query) throws Throwable - { - try - { - return processInternal(String.format(query, keyspace)); - } - catch (RuntimeException exc) - { - if (exc.getCause() != null) - throw exc.getCause(); - throw exc; - } - } - @Test - public void testTokenFunctionWithSinglePartitionArgument() throws Throwable - { - execute("INSERT INTO %s.single_partition (a, b) VALUES (0, 'a')"); - - try - { - UntypedResultSet results = execute("SELECT * FROM %s.single_partition WHERE token(a) >= token(0)"); - assertEquals(1, results.size()); - results = execute("SELECT * FROM %s.single_partition WHERE token(a) >= token(0) and token(a) < token(1)"); - assertEquals(1, results.size()); - } - finally - { - execute("DELETE FROM %s.single_partition WHERE a = 0"); - } - } - - @Test(expected = InvalidRequestException.class) - public void testTokenFunctionWithWrongLiteralArgument() throws Throwable - { - execute("SELECT * FROM %s.single_partition WHERE token(a) > token('a')"); - } - - @Test(expected = InvalidRequestException.class) - public void testTokenFunctionWithTwoGreaterThan() throws Throwable - { - execute("SELECT * FROM %s.single_clustering WHERE token(a) >= token(0) and token(a) >= token(1)"); - } - - @Test(expected = InvalidRequestException.class) - public void testTokenFunctionWithGreaterThanAndEquals() throws Throwable + public void testTokenFunctionWithSingleColumnPartitionKey() throws Throwable { - execute("SELECT * FROM %s.single_clustering WHERE token(a) >= token(0) and token(a) = token(1)"); + createTable("CREATE TABLE IF NOT EXISTS %s (a int PRIMARY KEY, b text)"); + execute("INSERT INTO %s (a, b) VALUES (0, 'a')"); + + assertRows(execute("SELECT * FROM %s WHERE token(a) >= token(?)", 0), row(0, "a")); ++ assertRows(execute("SELECT * FROM %s WHERE token(a) >= token(?) and token(a) < token(?)", 0, 1), row(0, "a")); + assertInvalid("SELECT * FROM %s WHERE token(a) > token(?)", "a"); + assertInvalid("SELECT * FROM %s WHERE token(a, b) >= token(?, ?)", "b", 0); ++ assertInvalid("SELECT * FROM %s WHERE token(a) >= token(?) and token(a) >= token(?)", 0, 1); ++ assertInvalid("SELECT * FROM %s WHERE token(a) >= token(?) and token(a) = token(?)", 0, 1); ++ assertInvalidSyntax("SELECT * FROM %s WHERE token(a) = token(?) and token(a) IN (token(?))", 0, 1); } - @Test(expected = SyntaxException.class) - public void testTokenFunctionWithGreaterThanAndIn() throws Throwable - { - execute("SELECT * FROM %s.single_clustering WHERE token(a) >= token(0) and token(a) in (token(1))"); - } - - @Test(expected = InvalidRequestException.class) + @Test public void testTokenFunctionWithPartitionKeyAndClusteringKeyArguments() throws Throwable { - execute("SELECT * FROM %s.single_clustering WHERE token(a, b) > token(0, 'c')"); - } - - @Test(expected = InvalidRequestException.class) - public void testTokenFunctionWithCompoundPartitionKeyAndWrongLiteralArgument() throws Throwable - { - execute("SELECT * FROM %s.single_partition WHERE token(a, b) >= token('c', 0)"); + createTable("CREATE TABLE IF NOT EXISTS %s (a int, b text, PRIMARY KEY (a, b))"); + assertInvalid("SELECT * FROM %s WHERE token(a, b) > token(0, 'c')"); } @Test - public void testTokenFunctionWithCompoundPartition() throws Throwable - { - execute("INSERT INTO %s.compound_partition (a, b) VALUES (0, 'a')"); - execute("INSERT INTO %s.compound_partition (a, b) VALUES (0, 'b')"); - execute("INSERT INTO %s.compound_partition (a, b) VALUES (0, 'c')"); - - try - { - UntypedResultSet results = execute("SELECT * FROM %s.compound_partition WHERE token(a, b) > token(0, 'a')"); - assertEquals(2, results.size()); - results = execute("SELECT * FROM %s.compound_partition WHERE token(a, b) > token(0, 'a') " - + "and token(a, b) < token(0, 'd')"); - assertEquals(2, results.size()); - } - finally - { - execute("DELETE FROM %s.compound_partition WHERE a = 0 and b in ('a', 'b', 'c')"); - } - } - - @Test(expected = InvalidRequestException.class) - public void testTokenFunctionWithCompoundPartitionKeyAndColumnIdentifierInWrongOrder() throws Throwable - { - execute("SELECT * FROM %s.compound_partition WHERE token(b, a) > token(0, 'c')"); - } - - @Test(expected = InvalidRequestException.class) - public void testTokenFunctionOnEachPartitionKeyColumns() throws Throwable + public void testTokenFunctionWithMultiColumnPartitionKey() throws Throwable { - execute("SELECT * FROM %s.compound_partition WHERE token(a) > token(0) and token(b) > token('c')"); + createTable("CREATE TABLE IF NOT EXISTS %s (a int, b text, PRIMARY KEY ((a, b)))"); + execute("INSERT INTO %s (a, b) VALUES (0, 'a')"); + execute("INSERT INTO %s (a, b) VALUES (0, 'b')"); + execute("INSERT INTO %s (a, b) VALUES (0, 'c')"); + + assertRows(execute("SELECT * FROM %s WHERE token(a, b) > token(?, ?)", 0, "a"), + row(0, "b"), + row(0, "c")); ++ assertRows(execute("SELECT * FROM %s WHERE token(a, b) > token(?, ?) and token(a, b) < token(?, ?)", ++ 0, "a", ++ 0, "d"), ++ row(0, "b"), ++ row(0, "c")); ++ assertInvalid("SELECT * FROM %s WHERE token(a) > token(?) and token(b) > token(?)", 0, "a"); ++ assertInvalid("SELECT * FROM %s WHERE token(a) > token(?, ?) and token(a) < token(?, ?) and token(b) > token(?, ?) ", 0, "a", 0, "d", 0, "a"); + assertInvalid("SELECT * FROM %s WHERE token(b, a) > token(0, 'c')"); } }