Repository: cassandra Updated Branches: refs/heads/trunk 7392fb96d -> bc9b0be32
http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/test/unit/org/apache/cassandra/cql3/validation/operations/SelectOrderedPartitionerTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/validation/operations/SelectOrderedPartitionerTest.java b/test/unit/org/apache/cassandra/cql3/validation/operations/SelectOrderedPartitionerTest.java new file mode 100644 index 0000000..a2ba71e --- /dev/null +++ b/test/unit/org/apache/cassandra/cql3/validation/operations/SelectOrderedPartitionerTest.java @@ -0,0 +1,325 @@ +package org.apache.cassandra.cql3.validation.operations; + +import org.junit.BeforeClass; +import org.junit.Test; + +import static junit.framework.Assert.assertNull; +import static org.junit.Assert.assertEquals; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.cql3.CQLTester; +import org.apache.cassandra.dht.ByteOrderedPartitioner; + +/** + * SELECT statement tests that require a ByteOrderedPartitioner + */ +public class SelectOrderedPartitionerTest extends CQLTester +{ + @BeforeClass + public static void setUp() + { + DatabaseDescriptor.setPartitioner(new ByteOrderedPartitioner()); + } + + @Test + public void testTokenFunctionWithSingleColumnPartitionKey() throws Throwable + { + 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 + public void testTokenFunctionWithPartitionKeyAndClusteringKeyArguments() throws Throwable + { + 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 testTokenFunctionWithMultiColumnPartitionKey() throws Throwable + { + 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')"); + } + + @Test + public void testTokenFunctionWithCompoundPartitionAndClusteringCols() throws Throwable + { + createTable("CREATE TABLE IF NOT EXISTS %s (a int, b int, c int, d int, PRIMARY KEY ((a, b), c, d))"); + // just test that the queries don't error + execute("SELECT * FROM %s WHERE token(a, b) > token(0, 0) AND c > 10 ALLOW FILTERING;"); + execute("SELECT * FROM %s WHERE c > 10 AND token(a, b) > token(0, 0) ALLOW FILTERING;"); + execute("SELECT * FROM %s WHERE token(a, b) > token(0, 0) AND (c, d) > (0, 0) ALLOW FILTERING;"); + execute("SELECT * FROM %s WHERE (c, d) > (0, 0) AND token(a, b) > token(0, 0) ALLOW FILTERING;"); + } + + /** + * Test undefined columns + * migrated from cql_tests.py:TestCQL.undefined_column_handling_test() + */ + @Test + public void testUndefinedColumns() throws Throwable + { + createTable("CREATE TABLE %s (k int PRIMARY KEY, v1 int, v2 int,)"); + + execute("INSERT INTO %s (k, v1, v2) VALUES (0, 0, 0)"); + execute("INSERT INTO %s (k, v1) VALUES (1, 1)"); + execute("INSERT INTO %s (k, v1, v2) VALUES (2, 2, 2)"); + + Object[][] rows = getRows(execute("SELECT v2 FROM %s")); + assertEquals(0, rows[0][0]); + assertEquals(null, rows[1][0]); + assertEquals(2, rows[2][0]); + + rows = getRows(execute("SELECT v2 FROM %s WHERE k = 1")); + assertEquals(1, rows.length); + assertNull(rows[0][0]); + } + + /** + * Check table with only a PK (#4361), + * migrated from cql_tests.py:TestCQL.only_pk_test() + */ + @Test + public void testPrimaryKeyOnly() throws Throwable + { + createTable("CREATE TABLE %s (k int, c int, PRIMARY KEY (k, c))"); + + for (int k = 0; k < 2; k++) + for (int c = 0; c < 2; c++) + execute("INSERT INTO %s (k, c) VALUES (?, ?)", k, c); + + assertRows(execute("SELECT * FROM %s"), + row(0, 0), + row(0, 1), + row(1, 0), + row(1, 1)); + + // Check for dense tables too + createTable(" CREATE TABLE %s (k int, c int, PRIMARY KEY (k, c)) WITH COMPACT STORAGE"); + + for (int k = 0; k < 2; k++) + for (int c = 0; c < 2; c++) + execute("INSERT INTO %s (k, c) VALUES (?, ?)", k, c); + + assertRows(execute("SELECT * FROM %s"), + row(0, 0), + row(0, 1), + row(1, 0), + row(1, 1)); + } + + /** + * Migrated from cql_tests.py:TestCQL.composite_index_with_pk_test() + */ + @Test + public void testCompositeIndexWithPK() throws Throwable + { + createTable("CREATE TABLE %s (blog_id int, time1 int, time2 int, author text, content text, PRIMARY KEY (blog_id, time1, time2))"); + + createIndex("CREATE INDEX ON %s(author)"); + + execute("INSERT INTO %s (blog_id, time1, time2, author, content) VALUES (?, ?, ?, ?, ?)", 1, 0, 0, "foo", "bar1"); + execute("INSERT INTO %s (blog_id, time1, time2, author, content) VALUES (?, ?, ?, ?, ?)", 1, 0, 1, "foo", "bar2"); + execute("INSERT INTO %s (blog_id, time1, time2, author, content) VALUES (?, ?, ?, ?, ?)", 2, 1, 0, "foo", "baz"); + execute("INSERT INTO %s (blog_id, time1, time2, author, content) VALUES (?, ?, ?, ?, ?)", 3, 0, 1, "gux", "qux"); + + assertRows(execute("SELECT blog_id, content FROM %s WHERE author='foo'"), + row(1, "bar1"), + row(1, "bar2"), + row(2, "baz")); + + assertRows(execute("SELECT blog_id, content FROM %s WHERE time1 > 0 AND author='foo' ALLOW FILTERING"), + row(2, "baz")); + + assertRows(execute("SELECT blog_id, content FROM %s WHERE time1 = 1 AND author='foo' ALLOW FILTERING"), + row(2, "baz")); + + assertRows(execute("SELECT blog_id, content FROM %s WHERE time1 = 1 AND time2 = 0 AND author='foo' ALLOW FILTERING"), + row(2, "baz")); + + assertEmpty(execute("SELECT content FROM %s WHERE time1 = 1 AND time2 = 1 AND author='foo' ALLOW FILTERING")); + + assertEmpty(execute("SELECT content FROM %s WHERE time1 = 1 AND time2 > 0 AND author='foo' ALLOW FILTERING")); + + assertInvalid("SELECT content FROM %s WHERE time2 >= 0 AND author='foo'"); + + execute("SELECT blog_id, content FROM %s WHERE time1 > 0 AND author='foo'"); + execute("SELECT blog_id, content FROM %s WHERE time1 = 1 AND author='foo'"); + execute("SELECT blog_id, content FROM %s WHERE time1 = 1 AND time2 = 0 AND author='foo'"); + execute("SELECT content FROM %s WHERE time1 = 1 AND time2 = 1 AND author='foo'"); + execute("SELECT content FROM %s WHERE time1 = 1 AND time2 > 0 AND author='foo'"); + } + + /** + * Test for LIMIT bugs from 4579, + * migrated from cql_tests.py:TestCQL.limit_bugs_test() + */ + @Test + public void testLimitBug() throws Throwable + { + createTable("CREATE TABLE %s (a int, b int, c int, d int, e int, PRIMARY KEY (a, b))"); + + execute("INSERT INTO %s (a, b, c, d, e) VALUES (1, 1, 1, 1, 1);"); + execute("INSERT INTO %s (a, b, c, d, e) VALUES (2, 2, 2, 2, 2);"); + execute("INSERT INTO %s (a, b, c, d, e) VALUES (3, 3, 3, 3, 3);"); + execute("INSERT INTO %s (a, b, c, d, e) VALUES (4, 4, 4, 4, 4);"); + + assertRows(execute("SELECT * FROM %s"), + row(1, 1, 1, 1, 1), + row(2, 2, 2, 2, 2), + row(3, 3, 3, 3, 3), + row(4, 4, 4, 4, 4)); + + assertRows(execute("SELECT * FROM %s LIMIT 1"), + row(1, 1, 1, 1, 1)); + + assertRows(execute("SELECT * FROM %s LIMIT 2"), + row(1, 1, 1, 1, 1), + row(2, 2, 2, 2, 2)); + + createTable("CREATE TABLE %s (a int primary key, b int, c int,)"); + + execute("INSERT INTO %s (a, b, c) VALUES (1, 1, 1)"); + execute("INSERT INTO %s (a, b, c) VALUES (2, 2, 2)"); + execute("INSERT INTO %s (a, b, c) VALUES (3, 3, 3)"); + execute("INSERT INTO %s (a, b, c) VALUES (4, 4, 4)"); + + assertRows(execute("SELECT * FROM %s"), + row(1, 1, 1), + row(2, 2, 2), + row(3, 3, 3), + row(4, 4, 4)); + + assertRows(execute("SELECT * FROM %s LIMIT 1"), + row(1, 1, 1)); + + assertRows(execute("SELECT * FROM %s LIMIT 2"), + row(1, 1, 1), + row(2, 2, 2)); + + assertRows(execute("SELECT * FROM %s LIMIT 3"), + row(1, 1, 1), + row(2, 2, 2), + row(3, 3, 3)); + + assertRows(execute("SELECT * FROM %s LIMIT 4"), + row(1, 1, 1), + row(2, 2, 2), + row(3, 3, 3), + row(4, 4, 4)); + + assertRows(execute("SELECT * FROM %s LIMIT 5"), + row(1, 1, 1), + row(2, 2, 2), + row(3, 3, 3), + row(4, 4, 4)); + } + + /** + * Test for #4612 bug and more generally order by when multiple C* rows are queried + * migrated from cql_tests.py:TestCQL.order_by_multikey_test() + */ + @Test + public void testOrderByMultikey() throws Throwable + { + createTable("CREATE TABLE %s (my_id varchar, col1 int, col2 int, value varchar, PRIMARY KEY (my_id, col1, col2))"); + + execute("INSERT INTO %s (my_id, col1, col2, value) VALUES ( 'key1', 1, 1, 'a');"); + execute("INSERT INTO %s (my_id, col1, col2, value) VALUES ( 'key2', 3, 3, 'a');"); + execute("INSERT INTO %s (my_id, col1, col2, value) VALUES ( 'key3', 2, 2, 'b');"); + execute("INSERT INTO %s (my_id, col1, col2, value) VALUES ( 'key4', 2, 1, 'b');"); + + assertRows(execute("SELECT col1 FROM %s WHERE my_id in('key1', 'key2', 'key3') ORDER BY col1"), + row(1), row(2), row(3)); + + assertRows(execute("SELECT col1, value, my_id, col2 FROM %s WHERE my_id in('key3', 'key4') ORDER BY col1, col2"), + row(2, "b", "key4", 1), row(2, "b", "key3", 2)); + + assertInvalid("SELECT col1 FROM %s ORDER BY col1"); + assertInvalid("SELECT col1 FROM %s WHERE my_id > 'key1' ORDER BY col1"); + } + + /** + * Migrated from cql_tests.py:TestCQL.composite_index_collections_test() + */ + @Test + public void testIndexOnCompositeWithCollections() throws Throwable + { + createTable("CREATE TABLE %s (blog_id int, time1 int, time2 int, author text, content set<text>, PRIMARY KEY (blog_id, time1, time2))"); + + createIndex("CREATE INDEX ON %s (author)"); + + execute("INSERT INTO %s (blog_id, time1, time2, author, content) VALUES (?, ?, ?, ?, { 'bar1', 'bar2' })", 1, 0, 0, "foo"); + execute("INSERT INTO %s (blog_id, time1, time2, author, content) VALUES (?, ?, ?, ?, { 'bar2', 'bar3' })", 1, 0, 1, "foo"); + execute("INSERT INTO %s (blog_id, time1, time2, author, content) VALUES (?, ?, ?, ?, { 'baz' })", 2, 1, 0, "foo"); + execute("INSERT INTO %s (blog_id, time1, time2, author, content) VALUES (?, ?, ?, ?, { 'qux' })", 3, 0, 1, "gux"); + + assertRows(execute("SELECT blog_id, content FROM %s WHERE author='foo'"), + row(1, set("bar1", "bar2")), + row(1, set("bar2", "bar3")), + row(2, set("baz"))); + } + + /** + * Migrated from cql_tests.py:TestCQL.truncate_clean_cache_test() + */ + @Test + public void testTruncateWithCaching() throws Throwable + { + createTable("CREATE TABLE %s (k int PRIMARY KEY, v1 int, v2 int,) WITH CACHING = ALL;"); + + for (int i = 0; i < 3; i++) + execute("INSERT INTO %s (k, v1, v2) VALUES (?, ?, ?)", i, i, i * 2); + + assertRows(execute("SELECT v1, v2 FROM %s WHERE k IN (0, 1, 2)"), + row(0, 0), + row(1, 2), + row(2, 4)); + + execute("TRUNCATE %s"); + + assertEmpty(execute("SELECT v1, v2 FROM %s WHERE k IN (0, 1, 2)")); + } + + /** + * Migrated from cql_tests.py:TestCQL.range_key_ordered_test() + */ + @Test + public void testRangeKey() throws Throwable + { + createTable("CREATE TABLE %s (k int PRIMARY KEY)"); + + execute("INSERT INTO %s (k) VALUES (-1)"); + execute("INSERT INTO %s (k) VALUES ( 0)"); + execute("INSERT INTO %s (k) VALUES ( 1)"); + + assertRows(execute("SELECT * FROM %s"), + row(0), + row(1), + row(-1)); + + assertInvalid("SELECT * FROM %s WHERE k >= -1 AND k < 1"); + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/test/unit/org/apache/cassandra/cql3/validation/operations/SelectSingleColumnRelationTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/validation/operations/SelectSingleColumnRelationTest.java b/test/unit/org/apache/cassandra/cql3/validation/operations/SelectSingleColumnRelationTest.java new file mode 100644 index 0000000..e950e7a --- /dev/null +++ b/test/unit/org/apache/cassandra/cql3/validation/operations/SelectSingleColumnRelationTest.java @@ -0,0 +1,109 @@ +/* + * 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.validation.operations; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.cassandra.cql3.CQLTester; + +public class SelectSingleColumnRelationTest extends CQLTester +{ + @Test + public void testInvalidCollectionEqualityRelation() throws Throwable + { + createTable("CREATE TABLE %s (a int PRIMARY KEY, b set<int>, c list<int>, d map<int, int>)"); + createIndex("CREATE INDEX ON %s (b)"); + createIndex("CREATE INDEX ON %s (c)"); + createIndex("CREATE INDEX ON %s (d)"); + + assertInvalid("SELECT * FROM %s WHERE a = 0 AND b=?", set(0)); + assertInvalid("SELECT * FROM %s WHERE a = 0 AND c=?", list(0)); + assertInvalid("SELECT * FROM %s WHERE a = 0 AND d=?", map(0, 0)); + } + + @Test + public void testInvalidCollectionNonEQRelation() throws Throwable + { + createTable("CREATE TABLE %s (a int PRIMARY KEY, b set<int>, c int)"); + createIndex("CREATE INDEX ON %s (c)"); + execute("INSERT INTO %s (a, b, c) VALUES (0, {0}, 0)"); + + // non-EQ operators + assertInvalid("SELECT * FROM %s WHERE c = 0 AND b > ?", set(0)); + assertInvalid("SELECT * FROM %s WHERE c = 0 AND b >= ?", set(0)); + assertInvalid("SELECT * FROM %s WHERE c = 0 AND b < ?", set(0)); + assertInvalid("SELECT * FROM %s WHERE c = 0 AND b <= ?", set(0)); + assertInvalid("SELECT * FROM %s WHERE c = 0 AND b IN (?)", set(0)); + } + + @Test + public void testLargeClusteringINValues() throws Throwable + { + createTable("CREATE TABLE %s (k int, c int, v int, PRIMARY KEY (k, c))"); + execute("INSERT INTO %s (k, c, v) VALUES (0, 0, 0)"); + List<Integer> inValues = new ArrayList<>(10000); + for (int i = 0; i < 10000; i++) + inValues.add(i); + assertRows(execute("SELECT * FROM %s WHERE k=? AND c IN ?", 0, inValues), + row(0, 0, 0) + ); + } + + @Test + public void testMultiplePartitionKeyWithIndex() throws Throwable + { + createTable("CREATE TABLE %s (a int, b int, c int, d int, e int, f int, PRIMARY KEY ((a, b), c, d, e))"); + createIndex("CREATE INDEX ON %s (c)"); + createIndex("CREATE INDEX ON %s (f)"); + + execute("INSERT INTO %s (a, b, c, d, e, f) VALUES (?, ?, ?, ?, ?, ?)", 0, 0, 0, 0, 0, 0); + execute("INSERT INTO %s (a, b, c, d, e, f) VALUES (?, ?, ?, ?, ?, ?)", 0, 0, 0, 1, 0, 1); + execute("INSERT INTO %s (a, b, c, d, e, f) VALUES (?, ?, ?, ?, ?, ?)", 0, 0, 0, 1, 1, 2); + + execute("INSERT INTO %s (a, b, c, d, e, f) VALUES (?, ?, ?, ?, ?, ?)", 0, 0, 1, 0, 0, 3); + execute("INSERT INTO %s (a, b, c, d, e, f) VALUES (?, ?, ?, ?, ?, ?)", 0, 0, 1, 1, 0, 4); + execute("INSERT INTO %s (a, b, c, d, e, f) VALUES (?, ?, ?, ?, ?, ?)", 0, 0, 1, 1, 1, 5); + + execute("INSERT INTO %s (a, b, c, d, e, f) VALUES (?, ?, ?, ?, ?, ?)", 0, 0, 2, 0, 0, 5); + + assertRows(execute("SELECT * FROM %s WHERE a = ? AND c = ? ALLOW FILTERING", 0, 1), + row(0, 0, 1, 0, 0, 3), + row(0, 0, 1, 1, 0, 4), + row(0, 0, 1, 1, 1, 5)); + + assertRows(execute("SELECT * FROM %s WHERE a = ? AND c = ? AND d = ? ALLOW FILTERING", 0, 1, 1), + row(0, 0, 1, 1, 0, 4), + row(0, 0, 1, 1, 1, 5)); + + assertInvalidMessage("Partition key part b must be restricted since preceding part is", + "SELECT * FROM %s WHERE a = ? AND c >= ? ALLOW FILTERING", 0, 1); + + assertRows(execute("SELECT * FROM %s WHERE a = ? AND c >= ? AND f = ? ALLOW FILTERING", 0, 1, 5), + row(0, 0, 1, 1, 1, 5), + row(0, 0, 2, 0, 0, 5)); + + assertRows(execute("SELECT * FROM %s WHERE a = ? AND c = ? AND d >= ? AND f = ? ALLOW FILTERING", 0, 1, 1, 5), + row(0, 0, 1, 1, 1, 5)); + + assertInvalidMessage("Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING", + "SELECT * FROM %s WHERE a = ? AND d >= ? AND f = ?", 0, 1, 5); + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/test/unit/org/apache/cassandra/cql3/validation/operations/SelectTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/validation/operations/SelectTest.java b/test/unit/org/apache/cassandra/cql3/validation/operations/SelectTest.java new file mode 100644 index 0000000..c8c1f1a --- /dev/null +++ b/test/unit/org/apache/cassandra/cql3/validation/operations/SelectTest.java @@ -0,0 +1,1238 @@ +/* + * 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.validation.operations; + +import java.nio.ByteBuffer; +import java.util.UUID; + +import org.junit.BeforeClass; +import org.junit.Test; + +import junit.framework.Assert; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.cql3.UntypedResultSet; +import org.apache.cassandra.cql3.CQLTester; +import org.apache.cassandra.dht.Murmur3Partitioner; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Test column ranges and ordering with static column in table + */ +public class SelectTest extends CQLTester +{ + @BeforeClass + public static void setUp() + { + DatabaseDescriptor.setPartitioner(new Murmur3Partitioner()); + } + + @Test + public void testSingleClustering() throws Throwable + { + createTable("CREATE TABLE %s (p text, c text, v text, s text static, PRIMARY KEY (p, c))"); + + execute("INSERT INTO %s(p, c, v, s) values (?, ?, ?, ?)", "p1", "k1", "v1", "sv1"); + execute("INSERT INTO %s(p, c, v) values (?, ?, ?)", "p1", "k2", "v2"); + execute("INSERT INTO %s(p, s) values (?, ?)", "p2", "sv2"); + + assertRows(execute("SELECT * FROM %s WHERE p=?", "p1"), + row("p1", "k1", "sv1", "v1"), + row("p1", "k2", "sv1", "v2") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=?", "p2"), + row("p2", null, "sv2", null) + ); + + // Ascending order + + assertRows(execute("SELECT * FROM %s WHERE p=? ORDER BY c ASC", "p1"), + row("p1", "k1", "sv1", "v1"), + row("p1", "k2", "sv1", "v2") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? ORDER BY c ASC", "p2"), + row("p2", null, "sv2", null) + ); + + // Descending order + + assertRows(execute("SELECT * FROM %s WHERE p=? ORDER BY c DESC", "p1"), + row("p1", "k2", "sv1", "v2"), + row("p1", "k1", "sv1", "v1") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? ORDER BY c DESC", "p2"), + row("p2", null, "sv2", null) + ); + + // No order with one relation + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c>=?", "p1", "k1"), + row("p1", "k1", "sv1", "v1"), + row("p1", "k2", "sv1", "v2") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c>=?", "p1", "k2"), + row("p1", "k2", "sv1", "v2") + ); + + assertEmpty(execute("SELECT * FROM %s WHERE p=? AND c>=?", "p1", "k3")); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c =?", "p1", "k1"), + row("p1", "k1", "sv1", "v1") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c<=?", "p1", "k1"), + row("p1", "k1", "sv1", "v1") + ); + + assertEmpty(execute("SELECT * FROM %s WHERE p=? AND c<=?", "p1", "k0")); + + // Ascending with one relation + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c>=? ORDER BY c ASC", "p1", "k1"), + row("p1", "k1", "sv1", "v1"), + row("p1", "k2", "sv1", "v2") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c>=? ORDER BY c ASC", "p1", "k2"), + row("p1", "k2", "sv1", "v2") + ); + + assertEmpty(execute("SELECT * FROM %s WHERE p=? AND c>=? ORDER BY c ASC", "p1", "k3")); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c =? ORDER BY c ASC", "p1", "k1"), + row("p1", "k1", "sv1", "v1") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c<=? ORDER BY c ASC", "p1", "k1"), + row("p1", "k1", "sv1", "v1") + ); + + assertEmpty(execute("SELECT * FROM %s WHERE p=? AND c<=? ORDER BY c ASC", "p1", "k0")); + + // Descending with one relation + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c>=? ORDER BY c DESC", "p1", "k1"), + row("p1", "k2", "sv1", "v2"), + row("p1", "k1", "sv1", "v1") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c>=? ORDER BY c DESC", "p1", "k2"), + row("p1", "k2", "sv1", "v2") + ); + + assertEmpty(execute("SELECT * FROM %s WHERE p=? AND c>=? ORDER BY c DESC", "p1", "k3")); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c =? ORDER BY c DESC", "p1", "k1"), + row("p1", "k1", "sv1", "v1") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c<=? ORDER BY c DESC", "p1", "k1"), + row("p1", "k1", "sv1", "v1") + ); + + assertEmpty(execute("SELECT * FROM %s WHERE p=? AND c<=? ORDER BY c DESC", "p1", "k0")); + + // IN + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c IN (?, ?)", "p1", "k1", "k2"), + row("p1", "k1", "sv1", "v1"), + row("p1", "k2", "sv1", "v2") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c IN (?, ?) ORDER BY c ASC", "p1", "k1", "k2"), + row("p1", "k1", "sv1", "v1"), + row("p1", "k2", "sv1", "v2") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c IN (?, ?) ORDER BY c DESC", "p1", "k1", "k2"), + row("p1", "k2", "sv1", "v2"), + row("p1", "k1", "sv1", "v1") + ); + } + + @Test + public void testSingleClusteringReversed() throws Throwable + { + createTable("CREATE TABLE %s (p text, c text, v text, s text static, PRIMARY KEY (p, c)) WITH CLUSTERING ORDER BY (c DESC)"); + + execute("INSERT INTO %s(p, c, v, s) values (?, ?, ?, ?)", "p1", "k1", "v1", "sv1"); + execute("INSERT INTO %s(p, c, v) values (?, ?, ?)", "p1", "k2", "v2"); + execute("INSERT INTO %s(p, s) values (?, ?)", "p2", "sv2"); + + assertRows(execute("SELECT * FROM %s WHERE p=?", "p1"), + row("p1", "k2", "sv1", "v2"), + row("p1", "k1", "sv1", "v1") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=?", "p2"), + row("p2", null, "sv2", null) + ); + + // Ascending order + + assertRows(execute("SELECT * FROM %s WHERE p=? ORDER BY c ASC", "p1"), + row("p1", "k1", "sv1", "v1"), + row("p1", "k2", "sv1", "v2") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? ORDER BY c ASC", "p2"), + row("p2", null, "sv2", null) + ); + + // Descending order + + assertRows(execute("SELECT * FROM %s WHERE p=? ORDER BY c DESC", "p1"), + row("p1", "k2", "sv1", "v2"), + row("p1", "k1", "sv1", "v1") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? ORDER BY c DESC", "p2"), + row("p2", null, "sv2", null) + ); + + // No order with one relation + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c>=?", "p1", "k1"), + row("p1", "k2", "sv1", "v2"), + row("p1", "k1", "sv1", "v1") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c>=?", "p1", "k2"), + row("p1", "k2", "sv1", "v2") + ); + + assertEmpty(execute("SELECT * FROM %s WHERE p=? AND c>=?", "p1", "k3")); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c=?", "p1", "k1"), + row("p1", "k1", "sv1", "v1") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c<=?", "p1", "k1"), + row("p1", "k1", "sv1", "v1") + ); + + assertEmpty(execute("SELECT * FROM %s WHERE p=? AND c<=?", "p1", "k0")); + + // Ascending with one relation + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c>=? ORDER BY c ASC", "p1", "k1"), + row("p1", "k1", "sv1", "v1"), + row("p1", "k2", "sv1", "v2") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c>=? ORDER BY c ASC", "p1", "k2"), + row("p1", "k2", "sv1", "v2") + ); + + assertEmpty(execute("SELECT * FROM %s WHERE p=? AND c>=? ORDER BY c ASC", "p1", "k3")); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c=? ORDER BY c ASC", "p1", "k1"), + row("p1", "k1", "sv1", "v1") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c<=? ORDER BY c ASC", "p1", "k1"), + row("p1", "k1", "sv1", "v1") + ); + + assertEmpty(execute("SELECT * FROM %s WHERE p=? AND c<=? ORDER BY c ASC", "p1", "k0")); + + // Descending with one relation + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c>=? ORDER BY c DESC", "p1", "k1"), + row("p1", "k2", "sv1", "v2"), + row("p1", "k1", "sv1", "v1") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c>=? ORDER BY c DESC", "p1", "k2"), + row("p1", "k2", "sv1", "v2") + ); + + assertEmpty(execute("SELECT * FROM %s WHERE p=? AND c>=? ORDER BY c DESC", "p1", "k3")); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c=? ORDER BY c DESC", "p1", "k1"), + row("p1", "k1", "sv1", "v1") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c<=? ORDER BY c DESC", "p1", "k1"), + row("p1", "k1", "sv1", "v1") + ); + + assertEmpty(execute("SELECT * FROM %s WHERE p=? AND c<=? ORDER BY c DESC", "p1", "k0")); + + // IN + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c IN (?, ?)", "p1", "k1", "k2"), + row("p1", "k2", "sv1", "v2"), + row("p1", "k1", "sv1", "v1") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c IN (?, ?) ORDER BY c ASC", "p1", "k1", "k2"), + row("p1", "k1", "sv1", "v1"), + row("p1", "k2", "sv1", "v2") + ); + + assertRows(execute("SELECT * FROM %s WHERE p=? AND c IN (?, ?) ORDER BY c DESC", "p1", "k1", "k2"), + row("p1", "k2", "sv1", "v2"), + row("p1", "k1", "sv1", "v1") + ); + } + + /** + * Check query with KEY IN clause + * migrated from cql_tests.py:TestCQL.select_key_in_test() + */ + @Test + public void testSelectKeyIn() throws Throwable + { + createTable("CREATE TABLE %s (userid uuid PRIMARY KEY, firstname text, lastname text, age int)"); + + UUID id1 = UUID.fromString("550e8400-e29b-41d4-a716-446655440000"); + UUID id2 = UUID.fromString("f47ac10b-58cc-4372-a567-0e02b2c3d479"); + + execute("INSERT INTO %s (userid, firstname, lastname, age) VALUES (?, 'Frodo', 'Baggins', 32)", id1); + execute("INSERT INTO %s (userid, firstname, lastname, age) VALUES (?, 'Samwise', 'Gamgee', 33)", id2); + + assertRowCount(execute("SELECT firstname, lastname FROM %s WHERE userid IN (?, ?)", id1, id2), 2); + } + + /** + * Check query with KEY IN clause for wide row tables + * migrated from cql_tests.py:TestCQL.in_clause_wide_rows_test() + */ + @Test + public void testSelectKeyInForWideRows() throws Throwable + { + createTable("CREATE TABLE %s (k int, c int, v int, PRIMARY KEY (k, c)) WITH COMPACT STORAGE"); + + for (int i = 0; i < 10; i++) + execute("INSERT INTO %s (k, c, v) VALUES (0, ?, ?)", i, i); + + assertRows(execute("SELECT v FROM %s WHERE k = 0 AND c IN (5, 2, 8)"), + row(2), row(5), row(8)); + + createTable("CREATE TABLE %s (k int, c1 int, c2 int, v int, PRIMARY KEY (k, c1, c2)) WITH COMPACT STORAGE"); + + for (int i = 0; i < 10; i++) + execute("INSERT INTO %s (k, c1, c2, v) VALUES (0, 0, ?, ?)", i, i); + + assertInvalid("SELECT v FROM %s WHERE k = 0 AND c1 IN (5, 2, 8) AND c2 = 3"); + + assertRows(execute("SELECT v FROM %s WHERE k = 0 AND c1 = 0 AND c2 IN (5, 2, 8)"), + row(2), row(5), row(8)); + } + + /** + * Check SELECT respects inclusive and exclusive bounds + * migrated from cql_tests.py:TestCQL.exclusive_slice_test() + */ + @Test + public void testSelectBounds() throws Throwable + { + createTable("CREATE TABLE %s (k int, c int, v int, PRIMARY KEY (k, c)) WITH COMPACT STORAGE"); + + for (int i = 0; i < 10; i++) + execute("INSERT INTO %s (k, c, v) VALUES (0, ?, ?)", i, i); + + assertRowCount(execute("SELECT v FROM %s WHERE k = 0"), 10); + + assertRows(execute("SELECT v FROM %s WHERE k = 0 AND c >= 2 AND c <= 6"), + row(2), row(3), row(4), row(5), row(6)); + + assertRows(execute("SELECT v FROM %s WHERE k = 0 AND c > 2 AND c <= 6"), + row(3), row(4), row(5), row(6)); + + assertRows(execute("SELECT v FROM %s WHERE k = 0 AND c >= 2 AND c < 6"), + row(2), row(3), row(4), row(5)); + + assertRows(execute("SELECT v FROM %s WHERE k = 0 AND c > 2 AND c < 6"), + row(3), row(4), row(5)); + + assertRows(execute("SELECT v FROM %s WHERE k = 0 AND c > 2 AND c <= 6 LIMIT 2"), + row(3), row(4)); + + assertRows(execute("SELECT v FROM %s WHERE k = 0 AND c >= 2 AND c < 6 ORDER BY c DESC LIMIT 2"), + row(5), row(4)); + } + + @Test + public void testSetContains() throws Throwable + { + createTable("CREATE TABLE %s (account text, id int, categories set<text>, PRIMARY KEY (account, id))"); + createIndex("CREATE INDEX ON %s(categories)"); + + execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 5, set("lmn")); + + assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?", "xyz", "lmn")); + + assertRows(execute("SELECT * FROM %s WHERE categories CONTAINS ?", "lmn"), + row("test", 5, set("lmn")) + ); + + assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?", "test", "lmn"), + row("test", 5, set("lmn")) + ); + + assertRows(execute("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS ?", "test", 5, "lmn"), + row("test", 5, set("lmn")) + ); + + assertInvalid("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ? AND categories CONTAINS ?", "xyz", "lmn", "notPresent"); + assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ? AND categories CONTAINS ? ALLOW FILTERING", "xyz", "lmn", "notPresent")); + } + + @Test + public void testListContains() throws Throwable + { + createTable("CREATE TABLE %s (account text, id int, categories list<text>, PRIMARY KEY (account, id))"); + createIndex("CREATE INDEX ON %s(categories)"); + + execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 5, list("lmn")); + + assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?", "xyz", "lmn")); + + assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?;", "test", "lmn"), + row("test", 5, list("lmn")) + ); + + assertRows(execute("SELECT * FROM %s WHERE categories CONTAINS ?", "lmn"), + row("test", 5, list("lmn")) + ); + + assertRows(execute("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS ?;", "test", 5, "lmn"), + row("test", 5, list("lmn")) + ); + + assertInvalid("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS ? AND categories CONTAINS ?", + "test", 5, "lmn", "notPresent"); + assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS ? AND categories CONTAINS ? ALLOW FILTERING", + "test", 5, "lmn", "notPresent")); + } + + @Test + public void testListContainsWithFiltering() throws Throwable + { + createTable("CREATE TABLE %s (e int PRIMARY KEY, f list<text>, s int)"); + createIndex("CREATE INDEX ON %s(f)"); + for(int i = 0; i < 3; i++) + { + execute("INSERT INTO %s (e, f, s) VALUES (?, ?, ?)", i, list("Dubai"), 4); + } + for(int i = 3; i < 5; i++) + { + execute("INSERT INTO %s (e, f, s) VALUES (?, ?, ?)", i, list("Dubai"), 3); + } + assertRows(execute("SELECT * FROM %s WHERE f CONTAINS ? AND s=? allow filtering", "Dubai", 3), + row(4, list("Dubai"), 3), + row(3, list("Dubai"), 3)); + } + + @Test + public void testMapKeyContains() throws Throwable + { + createTable("CREATE TABLE %s (account text, id int, categories map<text,text>, PRIMARY KEY (account, id))"); + createIndex("CREATE INDEX ON %s(keys(categories))"); + + execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 5, map("lmn", "foo")); + + assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS KEY ?", "xyz", "lmn")); + + assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS KEY ?", "test", "lmn"), + row("test", 5, map("lmn", "foo")) + ); + assertRows(execute("SELECT * FROM %s WHERE categories CONTAINS KEY ?", "lmn"), + row("test", 5, map("lmn", "foo")) + ); + + assertRows(execute("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS KEY ?", "test", 5, "lmn"), + row("test", 5, map("lmn", "foo")) + ); + + assertInvalid("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS KEY ? AND categories CONTAINS KEY ?", + "test", 5, "lmn", "notPresent"); + assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS KEY ? AND categories CONTAINS KEY ? ALLOW FILTERING", + "test", 5, "lmn", "notPresent")); + + assertInvalid("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS KEY ? AND categories CONTAINS ?", + "test", 5, "lmn", "foo"); + } + + @Test + public void testMapValueContains() throws Throwable + { + createTable("CREATE TABLE %s (account text, id int, categories map<text,text>, PRIMARY KEY (account, id))"); + createIndex("CREATE INDEX ON %s(categories)"); + + execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 5, map("lmn", "foo")); + + assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?", "xyz", "foo")); + + assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?", "test", "foo"), + row("test", 5, map("lmn", "foo")) + ); + + assertRows(execute("SELECT * FROM %s WHERE categories CONTAINS ?", "foo"), + row("test", 5, map("lmn", "foo")) + ); + + assertRows(execute("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS ?", "test", 5, "foo"), + row("test", 5, map("lmn", "foo")) + ); + + assertInvalid("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS ? AND categories CONTAINS ?" + , "test", 5, "foo", "notPresent"); + + assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS ? AND categories CONTAINS ? ALLOW FILTERING" + , "test", 5, "foo", "notPresent")); + } + + // See CASSANDRA-7525 + @Test + public void testQueryMultipleIndexTypes() throws Throwable + { + createTable("CREATE TABLE %s (account text, id int, categories map<text,text>, PRIMARY KEY (account, id))"); + + // create an index on + createIndex("CREATE INDEX id_index ON %s(id)"); + createIndex("CREATE INDEX categories_values_index ON %s(categories)"); + + execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 5, map("lmn", "foo")); + + assertRows(execute("SELECT * FROM %s WHERE categories CONTAINS ? AND id = ? ALLOW FILTERING", "foo", 5), + row("test", 5, map("lmn", "foo")) + ); + + assertRows( + execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ? AND id = ? ALLOW FILTERING", "test", "foo", 5), + row("test", 5, map("lmn", "foo")) + ); + } + + // See CASSANDRA-8033 + @Test + public void testFilterForContains() throws Throwable + { + createTable("CREATE TABLE %s (k1 int, k2 int, v set<int>, PRIMARY KEY ((k1, k2)))"); + createIndex("CREATE INDEX ON %s(k2)"); + + execute("INSERT INTO %s (k1, k2, v) VALUES (?, ?, ?)", 0, 0, set(1, 2, 3)); + execute("INSERT INTO %s (k1, k2, v) VALUES (?, ?, ?)", 0, 1, set(2, 3, 4)); + execute("INSERT INTO %s (k1, k2, v) VALUES (?, ?, ?)", 1, 0, set(3, 4, 5)); + execute("INSERT INTO %s (k1, k2, v) VALUES (?, ?, ?)", 1, 1, set(4, 5, 6)); + + assertRows(execute("SELECT * FROM %s WHERE k2 = ?", 1), + row(0, 1, set(2, 3, 4)), + row(1, 1, set(4, 5, 6)) + ); + + assertRows(execute("SELECT * FROM %s WHERE k2 = ? AND v CONTAINS ? ALLOW FILTERING", 1, 6), + row(1, 1, set(4, 5, 6)) + ); + + assertEmpty(execute("SELECT * FROM %s WHERE k2 = ? AND v CONTAINS ? ALLOW FILTERING", 1, 7)); + } + + // See CASSANDRA-8073 + @Test + public void testIndexLookupWithClusteringPrefix() throws Throwable + { + createTable("CREATE TABLE %s (a int, b int, c int, d set<int>, PRIMARY KEY (a, b, c))"); + createIndex("CREATE INDEX ON %s(d)"); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 0, set(1, 2, 3)); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 1, set(3, 4, 5)); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 0, set(1, 2, 3)); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 1, set(3, 4, 5)); + + assertRows(execute("SELECT * FROM %s WHERE a=? AND b=? AND d CONTAINS ?", 0, 1, 3), + row(0, 1, 0, set(1, 2, 3)), + row(0, 1, 1, set(3, 4, 5)) + ); + + assertRows(execute("SELECT * FROM %s WHERE a=? AND b=? AND d CONTAINS ?", 0, 1, 2), + row(0, 1, 0, set(1, 2, 3)) + ); + + assertRows(execute("SELECT * FROM %s WHERE a=? AND b=? AND d CONTAINS ?", 0, 1, 5), + row(0, 1, 1, set(3, 4, 5)) + ); + } + + @Test + public void testContainsKeyAndContainsWithIndexOnMapKey() throws Throwable + { + createTable("CREATE TABLE %s (account text, id int, categories map<text,text>, PRIMARY KEY (account, id))"); + createIndex("CREATE INDEX ON %s(keys(categories))"); + + execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 5, map("lmn", "foo")); + execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 6, map("lmn", "foo2")); + + assertInvalid("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?", "test", "foo"); + + assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS KEY ?", "test", "lmn"), + row("test", 5, map("lmn", "foo")), + row("test", 6, map("lmn", "foo2"))); + assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS KEY ? AND categories CONTAINS ? ALLOW FILTERING", + "test", "lmn", "foo"), + row("test", 5, map("lmn", "foo"))); + assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ? AND categories CONTAINS KEY ? ALLOW FILTERING", + "test", "foo", "lmn"), + row("test", 5, map("lmn", "foo"))); + } + + @Test + public void testContainsKeyAndContainsWithIndexOnMapValue() throws Throwable + { + createTable("CREATE TABLE %s (account text, id int, categories map<text,text>, PRIMARY KEY (account, id))"); + createIndex("CREATE INDEX ON %s(categories)"); + + execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 5, map("lmn", "foo")); + execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 6, map("lmn2", "foo")); + + assertInvalid("SELECT * FROM %s WHERE account = ? AND categories CONTAINS KEY ?", "test", "lmn"); + + assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?", "test", "foo"), + row("test", 5, map("lmn", "foo")), + row("test", 6, map("lmn2", "foo"))); + assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS KEY ? AND categories CONTAINS ? ALLOW FILTERING", + "test", "lmn", "foo"), + row("test", 5, map("lmn", "foo"))); + assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ? AND categories CONTAINS KEY ? ALLOW FILTERING", + "test", "foo", "lmn"), + row("test", 5, map("lmn", "foo"))); + } + + /** + * Test token ranges + * migrated from cql_tests.py:TestCQL.token_range_test() + */ + @Test + public void testTokenRange() throws Throwable + { + createTable(" CREATE TABLE %s (k int PRIMARY KEY, c int, v int)"); + + int c = 100; + for (int i = 0; i < c; i++) + execute("INSERT INTO %s (k, c, v) VALUES (?, ?, ?)", i, i, i); + + Object[][] res = getRows(execute("SELECT k FROM %s")); + assertEquals(c, res.length); + + Object[] inOrder = new Object[res.length]; + for (int i = 0; i < res.length; i++) + inOrder[i] = res[i][0]; + + Long min_token = Long.MIN_VALUE; + + res = getRows(execute(String.format("SELECT k FROM %s.%s WHERE token(k) >= %d", + keyspace(), currentTable(), min_token))); + assertEquals(c, res.length); + + res = getRows(execute(String.format("SELECT k FROM %s.%s WHERE token(k) >= token(%d) AND token(k) < token(%d)", + keyspace(), currentTable(), inOrder[32], inOrder[65]))); + + for (int i = 32; i < 65; i++) + Assert.assertEquals(inOrder[i], res[i - 32][0]); + } + + /** + * Test select count + * migrated from cql_tests.py:TestCQL.count_test() + */ + @Test + public void testSelectCount() throws Throwable + { + createTable(" CREATE TABLE %s (kind text, time int, value1 int, value2 int, PRIMARY KEY(kind, time))"); + + execute("INSERT INTO %s (kind, time, value1, value2) VALUES ('ev1', ?, ?, ?)", 0, 0, 0); + execute("INSERT INTO %s (kind, time, value1, value2) VALUES ('ev1', ?, ?, ?)", 1, 1, 1); + execute("INSERT INTO %s (kind, time, value1) VALUES ('ev1', ?, ?)", 2, 2); + execute("INSERT INTO %s (kind, time, value1, value2) VALUES ('ev1', ?, ?, ?)", 3, 3, 3); + execute("INSERT INTO %s (kind, time, value1) VALUES ('ev1', ?, ?)", 4, 4); + execute("INSERT INTO %s (kind, time, value1, value2) VALUES ('ev2', 0, 0, 0)"); + + assertRows(execute("SELECT COUNT(*) FROM %s WHERE kind = 'ev1'"), + row(5L)); + + assertRows(execute("SELECT COUNT(1) FROM %s WHERE kind IN ('ev1', 'ev2') AND time=0"), + row(2L)); + } + + /** + * Range test query from #4372 + * migrated from cql_tests.py:TestCQL.range_query_test() + */ + @Test + public void testRangeQuery() throws Throwable + { + createTable("CREATE TABLE %s (a int, b int, c int, d int, e int, f text, PRIMARY KEY (a, b, c, d, e) )"); + + execute("INSERT INTO %s (a, b, c, d, e, f) VALUES (1, 1, 1, 1, 2, '2')"); + execute("INSERT INTO %s (a, b, c, d, e, f) VALUES (1, 1, 1, 1, 1, '1')"); + execute("INSERT INTO %s (a, b, c, d, e, f) VALUES (1, 1, 1, 2, 1, '1')"); + execute("INSERT INTO %s (a, b, c, d, e, f) VALUES (1, 1, 1, 1, 3, '3')"); + execute("INSERT INTO %s (a, b, c, d, e, f) VALUES (1, 1, 1, 1, 5, '5')"); + + assertRows(execute("SELECT a, b, c, d, e, f FROM %s WHERE a = 1 AND b = 1 AND c = 1 AND d = 1 AND e >= 2"), + row(1, 1, 1, 1, 2, "2"), + row(1, 1, 1, 1, 3, "3"), + row(1, 1, 1, 1, 5, "5")); + } + + /** + * Migrated from cql_tests.py:TestCQL.composite_row_key_test() + */ + @Test + public void testCompositeRowKey() throws Throwable + { + createTable("CREATE TABLE %s (k1 int, k2 int, c int, v int, PRIMARY KEY ((k1, k2), c))"); + + for (int i = 0; i < 4; i++) + execute("INSERT INTO %s (k1, k2, c, v) VALUES (?, ?, ?, ?)", 0, i, i, i); + + assertRows(execute("SELECT * FROM %s"), + row(0, 2, 2, 2), + row(0, 3, 3, 3), + row(0, 0, 0, 0), + row(0, 1, 1, 1)); + + assertRows(execute("SELECT * FROM %s WHERE k1 = 0 and k2 IN (1, 3)"), + row(0, 1, 1, 1), + row(0, 3, 3, 3)); + + assertInvalid("SELECT * FROM %s WHERE k2 = 3"); + + assertRows(execute("SELECT * FROM %s WHERE token(k1, k2) = token(0, 1)"), + row(0, 1, 1, 1)); + + + assertRows(execute("SELECT * FROM %s WHERE token(k1, k2) > ?", Long.MIN_VALUE), + row(0, 2, 2, 2), + row(0, 3, 3, 3), + row(0, 0, 0, 0), + row(0, 1, 1, 1)); + } + + /** + * Test for #4532, NPE when trying to select a slice from a composite table + * migrated from cql_tests.py:TestCQL.bug_4532_test() + */ + @Test + public void testSelectSliceFromComposite() throws Throwable + { + createTable("CREATE TABLE %s (status ascii, ctime bigint, key ascii, nil ascii, PRIMARY KEY (status, ctime, key))"); + + execute("INSERT INTO %s (status,ctime,key,nil) VALUES ('C',12345678,'key1','')"); + execute("INSERT INTO %s (status,ctime,key,nil) VALUES ('C',12345678,'key2','')"); + execute("INSERT INTO %s (status,ctime,key,nil) VALUES ('C',12345679,'key3','')"); + execute("INSERT INTO %s (status,ctime,key,nil) VALUES ('C',12345679,'key4','')"); + execute("INSERT INTO %s (status,ctime,key,nil) VALUES ('C',12345679,'key5','')"); + execute("INSERT INTO %s (status,ctime,key,nil) VALUES ('C',12345680,'key6','')"); + + assertInvalid("SELECT * FROM %s WHERE ctime>=12345679 AND key='key3' AND ctime<=12345680 LIMIT 3;"); + assertInvalid("SELECT * FROM %s WHERE ctime=12345679 AND key='key3' AND ctime<=12345680 LIMIT 3"); + } + + /** + * Test for #4716 bug and more generally for good behavior of ordering, + * migrated from cql_tests.py:TestCQL.reversed_compact_test() + */ + @Test + public void testReverseCompact() throws Throwable + { + createTable("CREATE TABLE %s ( k text, c int, v int, PRIMARY KEY (k, c) ) WITH COMPACT STORAGE AND CLUSTERING ORDER BY (c DESC)"); + + for (int i = 0; i < 10; i++) + execute("INSERT INTO %s (k, c, v) VALUES ('foo', ?, ?)", i, i); + + assertRows(execute("SELECT c FROM %s WHERE c > 2 AND c < 6 AND k = 'foo'"), + row(5), row(4), row(3)); + + assertRows(execute("SELECT c FROM %s WHERE c >= 2 AND c <= 6 AND k = 'foo'"), + row(6), row(5), row(4), row(3), row(2)); + + assertRows(execute("SELECT c FROM %s WHERE c > 2 AND c < 6 AND k = 'foo' ORDER BY c ASC"), + row(3), row(4), row(5)); + + assertRows(execute("SELECT c FROM %s WHERE c >= 2 AND c <= 6 AND k = 'foo' ORDER BY c ASC"), + row(2), row(3), row(4), row(5), row(6)); + + assertRows(execute("SELECT c FROM %s WHERE c > 2 AND c < 6 AND k = 'foo' ORDER BY c DESC"), + row(5), row(4), row(3)); + + assertRows(execute("SELECT c FROM %s WHERE c >= 2 AND c <= 6 AND k = 'foo' ORDER BY c DESC"), + row(6), row(5), row(4), row(3), row(2)); + + createTable("CREATE TABLE %s ( k text, c int, v int, PRIMARY KEY (k, c) ) WITH COMPACT STORAGE"); + + for (int i = 0; i < 10; i++) + execute("INSERT INTO %s(k, c, v) VALUES ('foo', ?, ?)", i, i); + + assertRows(execute("SELECT c FROM %s WHERE c > 2 AND c < 6 AND k = 'foo'"), + row(3), row(4), row(5)); + + assertRows(execute("SELECT c FROM %s WHERE c >= 2 AND c <= 6 AND k = 'foo'"), + row(2), row(3), row(4), row(5), row(6)); + + assertRows(execute("SELECT c FROM %s WHERE c > 2 AND c < 6 AND k = 'foo' ORDER BY c ASC"), + row(3), row(4), row(5)); + + assertRows(execute("SELECT c FROM %s WHERE c >= 2 AND c <= 6 AND k = 'foo' ORDER BY c ASC"), + row(2), row(3), row(4), row(5), row(6)); + + assertRows(execute("SELECT c FROM %s WHERE c > 2 AND c < 6 AND k = 'foo' ORDER BY c DESC"), + row(5), row(4), row(3)); + + assertRows(execute("SELECT c FROM %s WHERE c >= 2 AND c <= 6 AND k = 'foo' ORDER BY c DESC"), + row(6), row(5), row(4), row(3), row(2)); + } + + /** + * Test for the bug from #4760 and #4759, + * migrated from cql_tests.py:TestCQL.reversed_compact_multikey_test() + */ + @Test + public void testReversedCompactMultikey() throws Throwable + { + createTable("CREATE TABLE %s (key text, c1 int, c2 int, value text, PRIMARY KEY(key, c1, c2) ) WITH COMPACT STORAGE AND CLUSTERING ORDER BY(c1 DESC, c2 DESC)"); + + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + execute("INSERT INTO %s (key, c1, c2, value) VALUES ('foo', ?, ?, 'bar')", i, j); + + // Equalities + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 = 1"), + row(1, 2), row(1, 1), row(1, 0)); + + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 = 1 ORDER BY c1 ASC, c2 ASC"), + row(1, 0), row(1, 1), row(1, 2)); + + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 = 1 ORDER BY c1 DESC, c2 DESC"), + row(1, 2), row(1, 1), row(1, 0)); + + // GT + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 > 1"), + row(2, 2), row(2, 1), row(2, 0)); + + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 > 1 ORDER BY c1 ASC, c2 ASC"), + row(2, 0), row(2, 1), row(2, 2)); + + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 > 1 ORDER BY c1 DESC, c2 DESC"), + row(2, 2), row(2, 1), row(2, 0)); + + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 >= 1"), + row(2, 2), row(2, 1), row(2, 0), row(1, 2), row(1, 1), row(1, 0)); + + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 >= 1 ORDER BY c1 ASC, c2 ASC"), + row(1, 0), row(1, 1), row(1, 2), row(2, 0), row(2, 1), row(2, 2)); + + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 >= 1 ORDER BY c1 ASC"), + row(1, 0), row(1, 1), row(1, 2), row(2, 0), row(2, 1), row(2, 2)); + + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 >= 1 ORDER BY c1 DESC, c2 DESC"), + row(2, 2), row(2, 1), row(2, 0), row(1, 2), row(1, 1), row(1, 0)); + + // LT + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 < 1"), + row(0, 2), row(0, 1), row(0, 0)); + + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 < 1 ORDER BY c1 ASC, c2 ASC"), + row(0, 0), row(0, 1), row(0, 2)); + + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 < 1 ORDER BY c1 DESC, c2 DESC"), + row(0, 2), row(0, 1), row(0, 0)); + + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 <= 1"), + row(1, 2), row(1, 1), row(1, 0), row(0, 2), row(0, 1), row(0, 0)); + + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 <= 1 ORDER BY c1 ASC, c2 ASC"), + row(0, 0), row(0, 1), row(0, 2), row(1, 0), row(1, 1), row(1, 2)); + + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 <= 1 ORDER BY c1 ASC"), + row(0, 0), row(0, 1), row(0, 2), row(1, 0), row(1, 1), row(1, 2)); + + assertRows(execute("SELECT c1, c2 FROM %s WHERE key='foo' AND c1 <= 1 ORDER BY c1 DESC, c2 DESC"), + row(1, 2), row(1, 1), row(1, 0), row(0, 2), row(0, 1), row(0, 0)); + } + + /** + * Migrated from cql_tests.py:TestCQL.bug_4882_test() + */ + @Test + public void testDifferentOrdering() throws Throwable + { + createTable(" CREATE TABLE %s ( k int, c1 int, c2 int, v int, PRIMARY KEY (k, c1, c2) ) WITH CLUSTERING ORDER BY (c1 ASC, c2 DESC)"); + + execute("INSERT INTO %s (k, c1, c2, v) VALUES (0, 0, 0, 0)"); + execute("INSERT INTO %s (k, c1, c2, v) VALUES (0, 1, 1, 1)"); + execute("INSERT INTO %s (k, c1, c2, v) VALUES (0, 0, 2, 2)"); + execute("INSERT INTO %s (k, c1, c2, v) VALUES (0, 1, 3, 3)"); + + assertRows(execute("select * from %s where k = 0 limit 1"), + row(0, 0, 2, 2)); + } + + /** + * Migrated from cql_tests.py:TestCQL.allow_filtering_test() + */ + @Test + public void testAllowFiltering() throws Throwable + { + createTable("CREATE TABLE %s (k int, c int, v int, PRIMARY KEY (k, c))"); + + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + execute("INSERT INTO %s (k, c, v) VALUES (?, ?, ?)", i, j, j); + + // Don't require filtering, always allowed + String[] queries = new String[] + { + "SELECT * FROM %s WHERE k = 1", + "SELECT * FROM %s WHERE k = 1 AND c > 2", + "SELECT * FROM %s WHERE k = 1 AND c = 2" + }; + + for (String q : queries) + { + execute(q); + execute(q + " ALLOW FILTERING"); + } + + // Require filtering, allowed only with ALLOW FILTERING + queries = new String[] + { + "SELECT * FROM %s WHERE c = 2", + "SELECT * FROM %s WHERE c > 2 AND c <= 4" + }; + + for (String q : queries) + { + assertInvalid(q); + execute(q + " ALLOW FILTERING"); + } + + createTable("CREATE TABLE %s (k int PRIMARY KEY, a int, b int,)"); + createIndex("CREATE INDEX ON %s (a)"); + + for (int i = 0; i < 5; i++) + execute("INSERT INTO %s (k, a, b) VALUES (?, ?, ?)", i, i * 10, i * 100); + + // Don't require filtering, always allowed + queries = new String[] + { + "SELECT * FROM %s WHERE k = 1", + "SELECT * FROM %s WHERE a = 20" + }; + + for (String q : queries) + { + execute(q); + execute(q + " ALLOW FILTERING"); + } + + // Require filtering, allowed only with ALLOW FILTERING + queries = new String[] + { + "SELECT * FROM %s WHERE a = 20 AND b = 200" + }; + + for (String q : queries) + { + assertInvalid(q); + execute(q + " ALLOW FILTERING"); + } + } + + /** + * Test for bug from #5122, + * migrated from cql_tests.py:TestCQL.composite_partition_key_validation_test() + */ + @Test + public void testSelectOnCompositeInvalid() throws Throwable + { + createTable("CREATE TABLE %s (a int, b text, c uuid, PRIMARY KEY ((a, b)))"); + + execute("INSERT INTO %s (a, b , c ) VALUES (1, 'aze', 4d481800-4c5f-11e1-82e0-3f484de45426)"); + execute("INSERT INTO %s (a, b , c ) VALUES (1, 'ert', 693f5800-8acb-11e3-82e0-3f484de45426)"); + execute("INSERT INTO %s (a, b , c ) VALUES (1, 'opl', d4815800-2d8d-11e0-82e0-3f484de45426)"); + + assertRowCount(execute("SELECT * FROM %s"), 3); + assertInvalid("SELECT * FROM %s WHERE a=1"); + } + + /** + * Migrated from cql_tests.py:TestCQL.multi_in_compact_non_composite_test() + */ + @Test + public void testMultiSelectsNonCompositeCompactStorage() throws Throwable + { + createTable("CREATE TABLE %s (key int, c int, v int, PRIMARY KEY (key, c)) WITH COMPACT STORAGE"); + + execute("INSERT INTO %s (key, c, v) VALUES (0, 0, 0)"); + execute("INSERT INTO %s (key, c, v) VALUES (0, 1, 1)"); + execute("INSERT INTO %s (key, c, v) VALUES (0, 2, 2)"); + + assertRows(execute("SELECT * FROM %s WHERE key=0 AND c IN (0, 2)"), + row(0, 0, 0), row(0, 2, 2)); + } + + /** + * Migrated from cql_tests.py:TestCQL.ticket_5230_test() + */ + @Test + public void testMultipleClausesOnPrimaryKey() throws Throwable + { + createTable("CREATE TABLE %s (key text, c text, v text, PRIMARY KEY(key, c))"); + + execute("INSERT INTO %s (key, c, v) VALUES ('foo', '1', '1')"); + execute("INSERT INTO %s(key, c, v) VALUES ('foo', '2', '2')"); + execute("INSERT INTO %s(key, c, v) VALUES ('foo', '3', '3')"); + + assertRows(execute("SELECT c FROM %s WHERE key = 'foo' AND c IN ('1', '2')"), + row("1"), row("2")); + } + + /** + * Migrated from cql_tests.py:TestCQL.bug_5404() + */ + @Test + public void testSelectWithToken() throws Throwable + { + createTable("CREATE TABLE %s (key text PRIMARY KEY)"); + + // We just want to make sure this doesn 't NPE server side + assertInvalid("select * from %s where token(key) > token(int(3030343330393233)) limit 1"); + } + + /** + * Migrated from cql_tests.py:TestCQL.clustering_order_and_functions_test() + */ + @Test + public void testFunctionsWithClusteringDesc() throws Throwable + { + createTable("CREATE TABLE %s ( k int, t timeuuid, PRIMARY KEY (k, t) ) WITH CLUSTERING ORDER BY (t DESC)"); + + for (int i = 0; i < 5; i++) + execute("INSERT INTO %s (k, t) VALUES (?, now())", i); + + execute("SELECT dateOf(t) FROM %s"); + } + + /** + * Migrated from cql_tests.py:TestCQL.select_with_alias_test() + */ + @Test + public void testSelectWithAlias() throws Throwable + { + createTable("CREATE TABLE %s (id int PRIMARY KEY, name text)"); + + for (int id = 0; id < 5; id++) + execute("INSERT INTO %s (id, name) VALUES (?, ?) USING TTL 10 AND TIMESTAMP 0", id, "name" + id); + + // test aliasing count( *) + UntypedResultSet rs = execute("SELECT count(*) AS user_count FROM %s"); + assertEquals("user_count", rs.metadata().get(0).name.toString()); + assertEquals(5L, rs.one().getLong(rs.metadata().get(0).name.toString())); + + // test aliasing regular value + rs = execute("SELECT name AS user_name FROM %s WHERE id = 0"); + assertEquals("user_name", rs.metadata().get(0).name.toString()); + assertEquals("name0", rs.one().getString(rs.metadata().get(0).name.toString())); + + // test aliasing writetime + rs = execute("SELECT writeTime(name) AS name_writetime FROM %s WHERE id = 0"); + assertEquals("name_writetime", rs.metadata().get(0).name.toString()); + assertEquals(0, rs.one().getInt(rs.metadata().get(0).name.toString())); + + // test aliasing ttl + rs = execute("SELECT ttl(name) AS name_ttl FROM %s WHERE id = 0"); + assertEquals("name_ttl", rs.metadata().get(0).name.toString()); + int ttl = rs.one().getInt(rs.metadata().get(0).name.toString()); + assertTrue(ttl == 9 || ttl == 10); + + // test aliasing a regular function + rs = execute("SELECT intAsBlob(id) AS id_blob FROM %s WHERE id = 0"); + assertEquals("id_blob", rs.metadata().get(0).name.toString()); + assertEquals(ByteBuffer.wrap(new byte[4]), rs.one().getBlob(rs.metadata().get(0).name.toString())); + + // test that select throws a meaningful exception for aliases in where clause + assertInvalidMessage("Aliases aren't allowed in the where clause", + "SELECT id AS user_id, name AS user_name FROM %s WHERE user_id = 0"); + + // test that select throws a meaningful exception for aliases in order by clause + assertInvalidMessage("Aliases are not allowed in order by clause", + "SELECT id AS user_id, name AS user_name FROM %s WHERE id IN (0) ORDER BY user_name"); + } + + /** + * Migrated from cql_tests.py:TestCQL.select_distinct_test() + */ + @Test + public void testSelectDistinct() throws Throwable + { + // Test a regular(CQL3) table. + createTable("CREATE TABLE %s (pk0 int, pk1 int, ck0 int, val int, PRIMARY KEY((pk0, pk1), ck0))"); + + for (int i = 0; i < 3; i++) + { + execute("INSERT INTO %s (pk0, pk1, ck0, val) VALUES (?, ?, 0, 0)", i, i); + execute("INSERT INTO %s (pk0, pk1, ck0, val) VALUES (?, ?, 1, 1)", i, i); + } + + assertRows(execute("SELECT DISTINCT pk0, pk1 FROM %s LIMIT 1"), + row(0, 0)); + + assertRows(execute("SELECT DISTINCT pk0, pk1 FROM %s LIMIT 3"), + row(0, 0), + row(2, 2), + row(1, 1)); + + // Test selection validation. + assertInvalidMessage("queries must request all the partition key columns", "SELECT DISTINCT pk0 FROM %s"); + assertInvalidMessage("queries must only request partition key columns", "SELECT DISTINCT pk0, pk1, ck0 FROM %s"); + + //Test a 'compact storage' table. + createTable("CREATE TABLE %s (pk0 int, pk1 int, val int, PRIMARY KEY((pk0, pk1))) WITH COMPACT STORAGE"); + + for (int i = 0; i < 3; i++) + execute("INSERT INTO %s (pk0, pk1, val) VALUES (?, ?, ?)", i, i, i); + + assertRows(execute("SELECT DISTINCT pk0, pk1 FROM %s LIMIT 1"), + row(0, 0)); + + assertRows(execute("SELECT DISTINCT pk0, pk1 FROM %s LIMIT 3"), + row(0, 0), + row(2, 2), + row(1, 1)); + + // Test a 'wide row' thrift table. + createTable("CREATE TABLE %s (pk int, name text, val int, PRIMARY KEY(pk, name)) WITH COMPACT STORAGE"); + + for (int i = 0; i < 3; i++) + { + execute("INSERT INTO %s (pk, name, val) VALUES (?, 'name0', 0)", i); + execute("INSERT INTO %s (pk, name, val) VALUES (?, 'name1', 1)", i); + } + + assertRows(execute("SELECT DISTINCT pk FROM %s LIMIT 1"), + row(1)); + + assertRows(execute("SELECT DISTINCT pk FROM %s LIMIT 3"), + row(1), + row(0), + row(2)); + } + + /** + * Migrated from cql_tests.py:TestCQL.select_distinct_with_deletions_test() + */ + @Test + public void testSelectDistinctWithDeletions() throws Throwable + { + createTable("CREATE TABLE %s (k int PRIMARY KEY, c int, v int)"); + + for (int i = 0; i < 10; i++) + execute("INSERT INTO %s (k, c, v) VALUES (?, ?, ?)", i, i, i); + + Object[][] rows = getRows(execute("SELECT DISTINCT k FROM %s")); + Assert.assertEquals(10, rows.length); + Object key_to_delete = rows[3][0]; + + execute("DELETE FROM %s WHERE k=?", key_to_delete); + + rows = getRows(execute("SELECT DISTINCT k FROM %s")); + Assert.assertEquals(9, rows.length); + + rows = getRows(execute("SELECT DISTINCT k FROM %s LIMIT 5")); + Assert.assertEquals(5, rows.length); + + rows = getRows(execute("SELECT DISTINCT k FROM %s")); + Assert.assertEquals(9, rows.length); + } + + /** + * Migrated from cql_tests.py:TestCQL.bug_6327_test() + */ + @Test + public void testSelectInClauseAtOne() throws Throwable + { + createTable("CREATE TABLE %s ( k int, v int, PRIMARY KEY (k, v))"); + + execute("INSERT INTO %s (k, v) VALUES (0, 0)"); + + flush(); + + assertRows(execute("SELECT v FROM %s WHERE k=0 AND v IN (1, 0)"), + row(0)); + } + + /** + * Test for the #6579 'select count' paging bug, + * migrated from cql_tests.py:TestCQL.select_count_paging_test() + */ + @Test + public void testSelectCountPaging() throws Throwable + { + createTable("create table %s (field1 text, field2 timeuuid, field3 boolean, primary key(field1, field2))"); + createIndex("create index test_index on %s (field3)"); + + execute("insert into %s (field1, field2, field3) values ('hola', now(), false)"); + execute("insert into %s (field1, field2, field3) values ('hola', now(), false)"); + + assertRows(execute("select count(*) from %s where field3 = false limit 1"), + row(1L)); + } + + /** + * Test for #7105 bug, + * migrated from cql_tests.py:TestCQL.clustering_order_in_test() + */ + @Test + public void testClusteringOrder() throws Throwable + { + createTable("CREATE TABLE %s (a int, b int, c int, PRIMARY KEY ((a, b), c) ) with clustering order by (c desc)"); + + execute("INSERT INTO %s (a, b, c) VALUES (1, 2, 3)"); + execute("INSERT INTO %s (a, b, c) VALUES (4, 5, 6)"); + + assertRows(execute("SELECT * FROM %s WHERE a=1 AND b=2 AND c IN (3)"), + row(1, 2, 3)); + assertRows(execute("SELECT * FROM %s WHERE a=1 AND b=2 AND c IN (3, 4)"), + row(1, 2, 3)); + } + + /** + * Test for #7105 bug, + * SELECT with IN on final column of composite and compound primary key fails + * migrated from cql_tests.py:TestCQL.bug7105_test() + */ + @Test + public void testSelectInFinalColumn() throws Throwable + { + createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY (a, b))"); + + execute("INSERT INTO %s (a, b, c, d) VALUES (1, 2, 3, 3)"); + execute("INSERT INTO %s (a, b, c, d) VALUES (1, 4, 6, 5)"); + + assertRows(execute("SELECT * FROM %s WHERE a=1 AND b=2 ORDER BY b DESC"), + row(1, 2, 3, 3)); + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/test/unit/org/apache/cassandra/cql3/validation/operations/UpdateTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/validation/operations/UpdateTest.java b/test/unit/org/apache/cassandra/cql3/validation/operations/UpdateTest.java new file mode 100644 index 0000000..19855d8 --- /dev/null +++ b/test/unit/org/apache/cassandra/cql3/validation/operations/UpdateTest.java @@ -0,0 +1,59 @@ +/* + * 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.validation.operations; + +import org.junit.Test; + +import org.apache.cassandra.cql3.CQLTester; +import org.apache.cassandra.utils.ByteBufferUtil; + +public class UpdateTest extends CQLTester +{ + /** + * Test altering the type of a column, including the one in the primary key (#4041) + * migrated from cql_tests.py:TestCQL.update_type_test() + */ + @Test + public void testUpdateColumnType() throws Throwable + { + createTable("CREATE TABLE %s (k text, c text, s set <text>, v text, PRIMARY KEY(k, c))"); + + // using utf8 character so that we can see the transition to BytesType + execute("INSERT INTO %s (k, c, v, s) VALUES ('ɸ', 'ɸ', 'ɸ', {'ɸ'})"); + + assertRows(execute("SELECT * FROM %s"), + row("ɸ", "ɸ", set("ɸ"), "ɸ")); + + execute("ALTER TABLE %s ALTER v TYPE blob"); + assertRows(execute("SELECT * FROM %s"), + row("ɸ", "ɸ", set("ɸ"), ByteBufferUtil.bytes("ɸ"))); + + execute("ALTER TABLE %s ALTER k TYPE blob"); + assertRows(execute("SELECT * FROM %s"), + row(ByteBufferUtil.bytes("ɸ"), "ɸ", set("ɸ"), ByteBufferUtil.bytes("ɸ"))); + + execute("ALTER TABLE %s ALTER c TYPE blob"); + assertRows(execute("SELECT * FROM %s"), + row(ByteBufferUtil.bytes("ɸ"), ByteBufferUtil.bytes("ɸ"), set("ɸ"), ByteBufferUtil.bytes("ɸ"))); + + execute("ALTER TABLE %s ALTER s TYPE set<blob>"); + assertRows(execute("SELECT * FROM %s"), + row(ByteBufferUtil.bytes("ɸ"), ByteBufferUtil.bytes("ɸ"), set(ByteBufferUtil.bytes("ɸ")), ByteBufferUtil.bytes("ɸ"))); + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/test/unit/org/apache/cassandra/cql3/validation/operations/UseTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/validation/operations/UseTest.java b/test/unit/org/apache/cassandra/cql3/validation/operations/UseTest.java new file mode 100644 index 0000000..e1498b6 --- /dev/null +++ b/test/unit/org/apache/cassandra/cql3/validation/operations/UseTest.java @@ -0,0 +1,31 @@ +/* + * 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.validation.operations; + +import org.junit.Test; + +import org.apache.cassandra.cql3.CQLTester; + +public class UseTest extends CQLTester +{ + @Test + public void testUseStatementWithBindVariable() throws Throwable + { + assertInvalidSyntaxMessage("Bind variables cannot be used for keyspace names", "USE ?"); + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/tools/stress/src/org/apache/cassandra/stress/generate/values/TimeUUIDs.java ---------------------------------------------------------------------- diff --git a/tools/stress/src/org/apache/cassandra/stress/generate/values/TimeUUIDs.java b/tools/stress/src/org/apache/cassandra/stress/generate/values/TimeUUIDs.java index 7bfabf5..537d54e 100644 --- a/tools/stress/src/org/apache/cassandra/stress/generate/values/TimeUUIDs.java +++ b/tools/stress/src/org/apache/cassandra/stress/generate/values/TimeUUIDs.java @@ -46,6 +46,6 @@ public class TimeUUIDs extends Generator<UUID> @Override public UUID generate() { - return UUIDGen.getTimeUUID(dateGen.generate().getTime(), clockSeqAndNode); + return UUIDGen.getTimeUUID(dateGen.generate().getTime(), 0L, clockSeqAndNode); } }