Merge branch 'cassandra-3.0' into cassandra-3.11
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/f33cdcb8 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/f33cdcb8 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/f33cdcb8 Branch: refs/heads/cassandra-3.11 Commit: f33cdcb8aa500fdc21d3ee39d3d08f960fff6a36 Parents: 025d9e6 aa18192 Author: adelapena <a.penya.gar...@gmail.com> Authored: Wed May 10 11:44:43 2017 +0100 Committer: adelapena <a.penya.gar...@gmail.com> Committed: Wed May 10 11:44:43 2017 +0100 ---------------------------------------------------------------------- .../validation/entities/SecondaryIndexTest.java | 42 ++++++++++++++++++++ 1 file changed, 42 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/f33cdcb8/test/unit/org/apache/cassandra/cql3/validation/entities/SecondaryIndexTest.java ---------------------------------------------------------------------- diff --cc test/unit/org/apache/cassandra/cql3/validation/entities/SecondaryIndexTest.java index 4a90d16,64bd967..3aed07a --- a/test/unit/org/apache/cassandra/cql3/validation/entities/SecondaryIndexTest.java +++ b/test/unit/org/apache/cassandra/cql3/validation/entities/SecondaryIndexTest.java @@@ -1268,273 -1160,70 +1268,315 @@@ public class SecondaryIndexTest extend } @Test - public void testIndexOnPartitionKeyWithStaticColumnAndNoRows() throws Throwable + public void testPartitionKeyWithIndex() throws Throwable { - createTable("CREATE TABLE %s (pk1 int, pk2 int, c int, s int static, v int, PRIMARY KEY((pk1, pk2), c))"); - createIndex("CREATE INDEX ON %s (pk2)"); - execute("INSERT INTO %s (pk1, pk2, c, s, v) VALUES (?, ?, ?, ?, ?)", 1, 1, 1, 9, 1); - execute("INSERT INTO %s (pk1, pk2, c, s, v) VALUES (?, ?, ?, ?, ?)", 1, 1, 2, 9, 2); - execute("INSERT INTO %s (pk1, pk2, s) VALUES (?, ?, ?)", 2, 1, 9); - execute("INSERT INTO %s (pk1, pk2, c, s, v) VALUES (?, ?, ?, ?, ?)", 3, 1, 1, 9, 1); + createTable("CREATE TABLE %s (a int, b int, c int, PRIMARY KEY ((a, b)))"); + createIndex("CREATE INDEX ON %s (a);"); + createIndex("CREATE INDEX ON %s (b);"); - assertRows(execute("SELECT * FROM %s WHERE pk2 = ?", 1), - row(2, 1, null, 9, null), - row(1, 1, 1, 9, 1), - row(1, 1, 2, 9, 2), - row(3, 1, 1, 9, 1)); + execute("INSERT INTO %s (a, b, c) VALUES (1,2,3)"); + execute("INSERT INTO %s (a, b, c) VALUES (2,3,4)"); + execute("INSERT INTO %s (a, b, c) VALUES (5,6,7)"); - execute("UPDATE %s SET s=?, v=? WHERE pk1=? AND pk2=? AND c=?", 9, 1, 1, 10, 2); - assertRows(execute("SELECT * FROM %s WHERE pk2 = ?", 10), row(1, 10, 2, 9, 1)); + beforeAndAfterFlush(() -> { + assertRows(execute("SELECT * FROM %s WHERE a = 1"), + row(1, 2, 3)); + assertRows(execute("SELECT * FROM %s WHERE b = 3"), + row(2, 3, 4)); - execute("UPDATE %s SET s=? WHERE pk1=? AND pk2=?", 9, 1, 20); - assertRows(execute("SELECT * FROM %s WHERE pk2 = ?", 20), row(1, 20, null, 9, null)); + }); + } + + @Test + public void testAllowFilteringOnPartitionKeyWithSecondaryIndex() throws Throwable + { + createTable("CREATE TABLE %s (pk1 int, pk2 int, c1 int, c2 int, v int, " + + "PRIMARY KEY ((pk1, pk2), c1, c2))"); + createIndex("CREATE INDEX v_idx_1 ON %s (v);"); + + for (int i = 1; i <= 5; i++) + { + for (int j = 1; j <= 2; j++) + { + execute("INSERT INTO %s (pk1, pk2, c1, c2, v) VALUES (?, ?, ?, ?, ?)", j, 1, 1, 1, i); + execute("INSERT INTO %s (pk1, pk2, c1, c2, v) VALUES (?, ?, ?, ?, ?)", j, 1, 1, i, i); + execute("INSERT INTO %s (pk1, pk2, c1, c2, v) VALUES (?, ?, ?, ?, ?)", j, 1, i, i, i); + execute("INSERT INTO %s (pk1, pk2, c1, c2, v) VALUES (?, ?, ?, ?, ?)", j, i, i, i, i); + } + } + + beforeAndAfterFlush(() -> { + assertEmpty(execute("SELECT * FROM %s WHERE pk1 = 1 AND c1 > 0 AND c1 < 5 AND c2 = 1 AND v = 3 ALLOW FILTERING;")); + + assertRows(execute("SELECT * FROM %s WHERE pk1 = 1 AND c1 > 0 AND c1 < 5 AND c2 = 3 AND v = 3 ALLOW FILTERING;"), + row(1, 3, 3, 3, 3), + row(1, 1, 1, 3, 3), + row(1, 1, 3, 3, 3)); + + assertEmpty(execute("SELECT * FROM %s WHERE pk1 = 1 AND c2 > 1 AND c2 < 5 AND v = 1 ALLOW FILTERING;")); + + assertRows(execute("SELECT * FROM %s WHERE pk1 = 1 AND c1 > 1 AND c2 > 2 AND v = 3 ALLOW FILTERING;"), + row(1, 3, 3, 3, 3), + row(1, 1, 3, 3, 3)); + + assertRows(execute("SELECT * FROM %s WHERE pk1 = 1 AND pk2 > 1 AND c2 > 2 AND v = 3 ALLOW FILTERING;"), + row(1, 3, 3, 3, 3)); + + assertRowsIgnoringOrder(execute("SELECT * FROM %s WHERE pk2 > 1 AND c1 IN(0,1,2) AND v <= 3 ALLOW FILTERING;"), + row(1, 2, 2, 2, 2), + row(2, 2, 2, 2, 2)); + + assertRows(execute("SELECT * FROM %s WHERE pk1 >= 2 AND pk2 <=3 AND c1 IN(0,1,2) AND c2 IN(0,1,2) AND v < 3 ALLOW FILTERING;"), + row(2, 2, 2, 2, 2), + row(2, 1, 1, 2, 2), + row(2, 1, 2, 2, 2)); + + assertInvalidMessage(StatementRestrictions.REQUIRES_ALLOW_FILTERING_MESSAGE, + "SELECT * FROM %s WHERE pk1 >= 1 AND pk2 <=3 AND c1 IN(0,1,2) AND c2 IN(0,1,2) AND v = 3"); + }); + } + + @Test + public void testAllowFilteringOnPartitionKeyWithIndexForContains() 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)); + + beforeAndAfterFlush(() -> { + assertInvalidMessage(StatementRestrictions.REQUIRES_ALLOW_FILTERING_MESSAGE, + "SELECT * FROM %s WHERE k2 > ?", 0); + + assertRows(execute("SELECT * FROM %s WHERE k2 > ? ALLOW FILTERING", 0), + 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", 0, 7)); + }); + } + + @Test + public void testIndexOnStaticColumnWithPartitionWithoutRows() throws Throwable + { + createTable("CREATE TABLE %s (pk int, c int, s int static, v int, PRIMARY KEY(pk, c))"); + createIndex("CREATE INDEX ON %s (s)"); + + execute("INSERT INTO %s (pk, c, s, v) VALUES (?, ?, ?, ?)", 1, 1, 9, 1); + execute("INSERT INTO %s (pk, c, s, v) VALUES (?, ?, ?, ?)", 1, 2, 9, 2); + execute("INSERT INTO %s (pk, s) VALUES (?, ?)", 2, 9); + execute("INSERT INTO %s (pk, c, s, v) VALUES (?, ?, ?, ?)", 3, 1, 9, 1); + flush(); + + assertRows(execute("SELECT * FROM %s WHERE s = ?", 9), + row(1, 1, 9, 1), + row(1, 2, 9, 2), + row(2, null, 9, null), + row(3, 1, 9, 1)); + + execute("DELETE FROM %s WHERE pk = ?", 3); + + assertRows(execute("SELECT * FROM %s WHERE s = ?", 9), + row(1, 1, 9, 1), + row(1, 2, 9, 2), + row(2, null, 9, null)); + } + + @Test + public void testIndexOnRegularColumnWithPartitionWithoutRows() throws Throwable + { + createTable("CREATE TABLE %s (pk int, c int, s int static, v int, PRIMARY KEY(pk, c))"); + createIndex("CREATE INDEX ON %s (v)"); + + execute("INSERT INTO %s (pk, c, s, v) VALUES (?, ?, ?, ?)", 1, 1, 9, 1); + execute("INSERT INTO %s (pk, c, s, v) VALUES (?, ?, ?, ?)", 1, 2, 9, 2); + execute("INSERT INTO %s (pk, s) VALUES (?, ?)", 2, 9); + execute("INSERT INTO %s (pk, c, s, v) VALUES (?, ?, ?, ?)", 3, 1, 9, 1); + flush(); + + execute("DELETE FROM %s WHERE pk = ? and c = ?", 3, 1); + + assertRows(execute("SELECT * FROM %s WHERE v = ?", 1), + row(1, 1, 9, 1)); + } + + @Test + public void testIndexOnDurationColumn() throws Throwable + { + createTable("CREATE TABLE %s (k int PRIMARY KEY, d duration)"); + assertInvalidMessage("Secondary indexes are not supported on duration columns", + "CREATE INDEX ON %s (d)"); + + createTable("CREATE TABLE %s (k int PRIMARY KEY, l list<duration>)"); + assertInvalidMessage("Secondary indexes are not supported on collections containing durations", + "CREATE INDEX ON %s (l)"); + + createTable("CREATE TABLE %s (k int PRIMARY KEY, m map<int, duration>)"); + assertInvalidMessage("Secondary indexes are not supported on collections containing durations", + "CREATE INDEX ON %s (m)"); + + createTable("CREATE TABLE %s (k int PRIMARY KEY, t tuple<int, duration>)"); + assertInvalidMessage("Secondary indexes are not supported on tuples containing durations", + "CREATE INDEX ON %s (t)"); + + String udt = createType("CREATE TYPE %s (i int, d duration)"); + + createTable("CREATE TABLE %s (k int PRIMARY KEY, t " + udt + ")"); + assertInvalidMessage("Secondary indexes are not supported on UDTs containing durations", + "CREATE INDEX ON %s (t)"); + } + + @Test + public void testIndexOnFrozenUDT() throws Throwable + { + String type = createType("CREATE TYPE %s (a int)"); + String tableName = createTable("CREATE TABLE %s (k int PRIMARY KEY, v frozen<" + type + ">)"); + + Object udt1 = userType("a", 1); + Object udt2 = userType("a", 2); + + execute("INSERT INTO %s (k, v) VALUES (?, ?)", 0, udt1); + execute("CREATE INDEX idx ON %s (v)"); + execute("INSERT INTO %s (k, v) VALUES (?, ?)", 1, udt2); + execute("INSERT INTO %s (k, v) VALUES (?, ?)", 1, udt1); + assertTrue(waitForIndex(keyspace(), tableName, "idx")); + + assertRows(execute("SELECT * FROM %s WHERE v = ?", udt1), row(1, udt1), row(0, udt1)); + assertEmpty(execute("SELECT * FROM %s WHERE v = ?", udt2)); + + execute("DELETE FROM %s WHERE k = 0"); + assertRows(execute("SELECT * FROM %s WHERE v = ?", udt1), row(1, udt1)); + + dropIndex("DROP INDEX %s.idx"); + assertInvalidMessage("Index 'idx' could not be found", "DROP INDEX " + KEYSPACE + ".idx"); + assertInvalidMessage(StatementRestrictions.REQUIRES_ALLOW_FILTERING_MESSAGE, + "SELECT * FROM %s WHERE v = ?", udt1); + } + + @Test + public void testIndexOnFrozenCollectionOfUDT() throws Throwable + { + String type = createType("CREATE TYPE %s (a int)"); + String tableName = createTable("CREATE TABLE %s (k int PRIMARY KEY, v frozen<set<frozen<" + type + ">>>)"); + + Object udt1 = userType("a", 1); + Object udt2 = userType("a", 2); + + execute("INSERT INTO %s (k, v) VALUES (?, ?)", 1, set(udt1, udt2)); + assertInvalidMessage("Frozen collections only support full()", "CREATE INDEX idx ON %s (keys(v))"); + assertInvalidMessage("Frozen collections only support full()", "CREATE INDEX idx ON %s (values(v))"); + execute("CREATE INDEX idx ON %s (full(v))"); + + execute("INSERT INTO %s (k, v) VALUES (?, ?)", 2, set(udt2)); + assertTrue(waitForIndex(keyspace(), tableName, "idx")); + + assertInvalidMessage(StatementRestrictions.REQUIRES_ALLOW_FILTERING_MESSAGE, + "SELECT * FROM %s WHERE v CONTAINS ?", udt1); + + assertRows(execute("SELECT * FROM %s WHERE v = ?", set(udt1, udt2)), row(1, set(udt1, udt2))); + assertRows(execute("SELECT * FROM %s WHERE v = ?", set(udt2)), row(2, set(udt2))); + + execute("DELETE FROM %s WHERE k = 2"); + assertEmpty(execute("SELECT * FROM %s WHERE v = ?", set(udt2))); + + dropIndex("DROP INDEX %s.idx"); + assertInvalidMessage("Index 'idx' could not be found", "DROP INDEX " + KEYSPACE + ".idx"); + assertInvalidMessage(StatementRestrictions.REQUIRES_ALLOW_FILTERING_MESSAGE, + "SELECT * FROM %s WHERE v CONTAINS ?", udt1); + } + + @Test + public void testIndexOnNonFrozenCollectionOfFrozenUDT() throws Throwable + { + String type = createType("CREATE TYPE %s (a int)"); + String tableName = createTable("CREATE TABLE %s (k int PRIMARY KEY, v set<frozen<" + type + ">>)"); + + Object udt1 = userType("a", 1); + Object udt2 = userType("a", 2); + + execute("INSERT INTO %s (k, v) VALUES (?, ?)", 1, set(udt1)); + assertInvalidMessage("Cannot create index on keys of column v with non-map type", + "CREATE INDEX idx ON %s (keys(v))"); + assertInvalidMessage("full() indexes can only be created on frozen collections", + "CREATE INDEX idx ON %s (full(v))"); + execute("CREATE INDEX idx ON %s (values(v))"); + + execute("INSERT INTO %s (k, v) VALUES (?, ?)", 2, set(udt2)); + execute("UPDATE %s SET v = v + ? WHERE k = ?", set(udt2), 1); + assertTrue(waitForIndex(keyspace(), tableName, "idx")); + + assertRows(execute("SELECT * FROM %s WHERE v CONTAINS ?", udt1), row(1, set(udt1, udt2))); + assertRows(execute("SELECT * FROM %s WHERE v CONTAINS ?", udt2), row(1, set(udt1, udt2)), row(2, set(udt2))); + + execute("DELETE FROM %s WHERE k = 1"); + assertEmpty(execute("SELECT * FROM %s WHERE v CONTAINS ?", udt1)); + assertRows(execute("SELECT * FROM %s WHERE v CONTAINS ?", udt2), row(2, set(udt2))); + + dropIndex("DROP INDEX %s.idx"); + assertInvalidMessage("Index 'idx' could not be found", "DROP INDEX " + KEYSPACE + ".idx"); + assertInvalidMessage(StatementRestrictions.REQUIRES_ALLOW_FILTERING_MESSAGE, + "SELECT * FROM %s WHERE v CONTAINS ?", udt1); + } + + @Test + public void testIndexOnNonFrozenUDT() throws Throwable + { + String type = createType("CREATE TYPE %s (a int)"); + createTable("CREATE TABLE %s (k int PRIMARY KEY, v " + type + ")"); + assertInvalidMessage("Secondary indexes are not supported on non-frozen UDTs", "CREATE INDEX ON %s (v)"); + assertInvalidMessage("Non-collection columns support only simple indexes", "CREATE INDEX ON %s (keys(v))"); + assertInvalidMessage("Non-collection columns support only simple indexes", "CREATE INDEX ON %s (values(v))"); + assertInvalidMessage("full() indexes can only be created on frozen collections", "CREATE INDEX ON %s (full(v))"); } + @Test + public void testIndexOnPartitionKeyInsertExpiringColumn() throws Throwable + { + createTable("CREATE TABLE %s (k1 int, k2 int, a int, b int, PRIMARY KEY ((k1, k2)))"); + createIndex("CREATE INDEX on %s(k1)"); + execute("INSERT INTO %s (k1, k2, a, b) VALUES (1, 2, 3, 4)"); + assertRows(execute("SELECT * FROM %s WHERE k1 = 1"), row(1, 2, 3, 4)); + execute("UPDATE %s USING TTL 1 SET b = 10 WHERE k1 = 1 AND k2 = 2"); + Thread.sleep(1000); + assertRows(execute("SELECT * FROM %s WHERE k1 = 1"), row(1, 2, 3, null)); + } + + @Test + public void testIndexOnClusteringKeyInsertExpiringColumn() throws Throwable + { + createTable("CREATE TABLE %s (pk int, ck int, a int, b int, PRIMARY KEY (pk, ck))"); + createIndex("CREATE INDEX on %s(ck)"); + execute("INSERT INTO %s (pk, ck, a, b) VALUES (1, 2, 3, 4)"); + assertRows(execute("SELECT * FROM %s WHERE ck = 2"), row(1, 2, 3, 4)); + execute("UPDATE %s USING TTL 1 SET b = 10 WHERE pk = 1 AND ck = 2"); + Thread.sleep(1000); + assertRows(execute("SELECT * FROM %s WHERE ck = 2"), row(1, 2, 3, null)); + } + + @Test + public void testIndexOnRegularColumnInsertExpiringColumn() throws Throwable + { + createTable("CREATE TABLE %s (pk int, ck int, a int, b int, PRIMARY KEY (pk, ck))"); + createIndex("CREATE INDEX on %s(a)"); + execute("INSERT INTO %s (pk, ck, a, b) VALUES (1, 2, 3, 4)"); + assertRows(execute("SELECT * FROM %s WHERE a = 3"), row(1, 2, 3, 4)); + + execute("UPDATE %s USING TTL 1 SET b = 10 WHERE pk = 1 AND ck = 2"); + Thread.sleep(1000); + assertRows(execute("SELECT * FROM %s WHERE a = 3"), row(1, 2, 3, null)); + + execute("UPDATE %s USING TTL 1 SET a = 5 WHERE pk = 1 AND ck = 2"); + Thread.sleep(1000); + assertEmpty(execute("SELECT * FROM %s WHERE a = 3")); + assertEmpty(execute("SELECT * FROM %s WHERE a = 5")); + } + private ResultMessage.Prepared prepareStatement(String cql, boolean forThrift) { return QueryProcessor.prepare(String.format(cql, KEYSPACE, currentTable()), --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org For additional commands, e-mail: commits-h...@cassandra.apache.org