This is an automated email from the ASF dual-hosted git repository. bereng pushed a commit to branch cassandra-4.0 in repository https://gitbox.apache.org/repos/asf/cassandra.git
The following commit(s) were added to refs/heads/cassandra-4.0 by this push: new 0764273 ViewTests flaky on timeouts 0764273 is described below commit 0764273608f501036f1f68e51185067856b934db Author: Bereng <berenguerbl...@gmail.com> AuthorDate: Mon Nov 22 11:45:33 2021 +0100 ViewTests flaky on timeouts patch by Berenguer Blasi; reviewed by Andres de la Peña for CASSANDRA-17167 --- .../cql3/ViewComplexDeletionsPartialTest.java | 227 +++++++++ .../cassandra/cql3/ViewComplexDeletionsTest.java | 191 +------- .../cql3/ViewComplexLivenessLimitTest.java | 101 ++++ .../cassandra/cql3/ViewComplexLivenessTest.java | 67 +-- .../apache/cassandra/cql3/ViewComplexTTLTest.java | 4 +- .../org/apache/cassandra/cql3/ViewComplexTest.java | 80 +--- .../apache/cassandra/cql3/ViewComplexTester.java | 4 +- .../cassandra/cql3/ViewComplexTombstoneTest.java | 116 +++++ .../cassandra/cql3/ViewComplexUpdatesTest.java | 4 +- .../apache/cassandra/cql3/ViewFiltering2Test.java | 452 +++++++++++++++++ .../cql3/ViewFilteringClustering1Test.java | 58 +-- .../cql3/ViewFilteringClustering2Test.java | 58 +-- ...g2Test.java => ViewFilteringComplexPKTest.java} | 240 +++++----- ...gPKTest.java => ViewFilteringSimplePKTest.java} | 328 +------------ .../apache/cassandra/cql3/ViewFilteringTest.java | 532 +-------------------- ...ComplexTester.java => ViewFilteringTester.java} | 54 +-- 16 files changed, 1071 insertions(+), 1445 deletions(-) diff --git a/test/unit/org/apache/cassandra/cql3/ViewComplexDeletionsPartialTest.java b/test/unit/org/apache/cassandra/cql3/ViewComplexDeletionsPartialTest.java new file mode 100644 index 0000000..377621e --- /dev/null +++ b/test/unit/org/apache/cassandra/cql3/ViewComplexDeletionsPartialTest.java @@ -0,0 +1,227 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.cql3; + +import java.util.concurrent.TimeUnit; + +import org.junit.Ignore; +import org.junit.Test; + +import org.apache.cassandra.db.Keyspace; +import org.apache.cassandra.utils.FBUtilities; + +/* ViewComplexTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670, CASSANDRA-17167) + * Any changes here check if they apply to the other classes: + * - ViewComplexUpdatesTest + * - ViewComplexDeletionsTest + * - ViewComplexTTLTest + * - ViewComplexTest + * - ViewComplexLivenessTest + * - ... + * - ViewComplex*Test + */ +public class ViewComplexDeletionsPartialTest extends ViewComplexTester +{ + // for now, unselected column cannot be fully supported, SEE CASSANDRA-11500 + @Ignore + @Test + public void testPartialDeleteUnselectedColumn() throws Throwable + { + boolean flush = true; + execute("USE " + keyspace()); + executeNet(version, "USE " + keyspace()); + createTable("CREATE TABLE %s (k int, c int, a int, b int, PRIMARY KEY (k, c))"); + String mv = createView("CREATE MATERIALIZED VIEW %s " + + "AS SELECT k,c FROM %%s WHERE k IS NOT NULL AND c IS NOT NULL PRIMARY KEY (k,c)"); + Keyspace ks = Keyspace.open(keyspace()); + ks.getColumnFamilyStore(mv).disableAutoCompaction(); + + updateView("UPDATE %s USING TIMESTAMP 10 SET b=1 WHERE k=1 AND c=1"); + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + assertRows(execute("SELECT * from %s"), row(1, 1, null, 1)); + assertRows(execute("SELECT * from " + mv), row(1, 1)); + updateView("DELETE b FROM %s USING TIMESTAMP 11 WHERE k=1 AND c=1"); + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + assertEmpty(execute("SELECT * from %s")); + assertEmpty(execute("SELECT * from " + mv)); + updateView("UPDATE %s USING TIMESTAMP 1 SET a=1 WHERE k=1 AND c=1"); + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + assertRows(execute("SELECT * from %s"), row(1, 1, 1, null)); + assertRows(execute("SELECT * from " + mv), row(1, 1)); + + execute("truncate %s;"); + + // removal generated by unselected column should not shadow PK update with smaller timestamp + updateViewWithFlush("UPDATE %s USING TIMESTAMP 18 SET a=1 WHERE k=1 AND c=1", flush); + assertRows(execute("SELECT * from %s"), row(1, 1, 1, null)); + assertRows(execute("SELECT * from " + mv), row(1, 1)); + + updateViewWithFlush("UPDATE %s USING TIMESTAMP 20 SET a=null WHERE k=1 AND c=1", flush); + assertRows(execute("SELECT * from %s")); + assertRows(execute("SELECT * from " + mv)); + + updateViewWithFlush("INSERT INTO %s(k,c) VALUES(1,1) USING TIMESTAMP 15", flush); + assertRows(execute("SELECT * from %s"), row(1, 1, null, null)); + assertRows(execute("SELECT * from " + mv), row(1, 1)); + } + + @Test + public void testPartialDeleteSelectedColumnWithFlush() throws Throwable + { + testPartialDeleteSelectedColumn(true); + } + + @Test + public void testPartialDeleteSelectedColumnWithoutFlush() throws Throwable + { + testPartialDeleteSelectedColumn(false); + } + + private void testPartialDeleteSelectedColumn(boolean flush) throws Throwable + { + execute("USE " + keyspace()); + executeNet(version, "USE " + keyspace()); + createTable("CREATE TABLE %s (k int, c int, a int, b int, e int, f int, PRIMARY KEY (k, c))"); + String mv = createView("CREATE MATERIALIZED VIEW %s AS SELECT a, b, c, k FROM %%s " + + "WHERE k IS NOT NULL AND c IS NOT NULL PRIMARY KEY (k,c)"); + Keyspace ks = Keyspace.open(keyspace()); + ks.getColumnFamilyStore(mv).disableAutoCompaction(); + + updateViewWithFlush("UPDATE %s USING TIMESTAMP 10 SET b=1 WHERE k=1 AND c=1", flush); + assertRows(execute("SELECT * from %s"), row(1, 1, null, 1, null, null)); + assertRows(execute("SELECT * from " + mv), row(1, 1, null, 1)); + + updateViewWithFlush("DELETE b FROM %s USING TIMESTAMP 11 WHERE k=1 AND c=1", flush); + assertEmpty(execute("SELECT * from %s")); + assertEmpty(execute("SELECT * from " + mv)); + + updateViewWithFlush("UPDATE %s USING TIMESTAMP 1 SET a=1 WHERE k=1 AND c=1", flush); + assertRows(execute("SELECT * from %s"), row(1, 1, 1, null, null, null)); + assertRows(execute("SELECT * from " + mv), row(1, 1, 1, null)); + + updateViewWithFlush("DELETE a FROM %s USING TIMESTAMP 1 WHERE k=1 AND c=1", flush); + assertEmpty(execute("SELECT * from %s")); + assertEmpty(execute("SELECT * from " + mv)); + + // view livenessInfo should not be affected by selected column ts or tb + updateViewWithFlush("INSERT INTO %s(k,c) VALUES(1,1) USING TIMESTAMP 0", flush); + assertRows(execute("SELECT * from %s"), row(1, 1, null, null, null, null)); + assertRows(execute("SELECT * from " + mv), row(1, 1, null, null)); + + updateViewWithFlush("UPDATE %s USING TIMESTAMP 12 SET b=1 WHERE k=1 AND c=1", flush); + assertRows(execute("SELECT * from %s"), row(1, 1, null, 1, null, null)); + assertRows(execute("SELECT * from " + mv), row(1, 1, null, 1)); + + updateViewWithFlush("DELETE b FROM %s USING TIMESTAMP 13 WHERE k=1 AND c=1", flush); + assertRows(execute("SELECT * from %s"), row(1, 1, null, null, null, null)); + assertRows(execute("SELECT * from " + mv), row(1, 1, null, null)); + + updateViewWithFlush("DELETE FROM %s USING TIMESTAMP 14 WHERE k=1 AND c=1", flush); + assertEmpty(execute("SELECT * from %s")); + assertEmpty(execute("SELECT * from " + mv)); + + updateViewWithFlush("INSERT INTO %s(k,c) VALUES(1,1) USING TIMESTAMP 15", flush); + assertRows(execute("SELECT * from %s"), row(1, 1, null, null, null, null)); + assertRows(execute("SELECT * from " + mv), row(1, 1, null, null)); + + updateViewWithFlush("UPDATE %s USING TTL 3 SET b=1 WHERE k=1 AND c=1", flush); + assertRows(execute("SELECT * from %s"), row(1, 1, null, 1, null, null)); + assertRows(execute("SELECT * from " + mv), row(1, 1, null, 1)); + + TimeUnit.SECONDS.sleep(4); + + assertRows(execute("SELECT * from %s"), row(1, 1, null, null, null, null)); + assertRows(execute("SELECT * from " + mv), row(1, 1, null, null)); + + updateViewWithFlush("DELETE FROM %s USING TIMESTAMP 15 WHERE k=1 AND c=1", flush); + assertEmpty(execute("SELECT * from %s")); + assertEmpty(execute("SELECT * from " + mv)); + + execute("truncate %s;"); + + // removal generated by unselected column should not shadow selected column with smaller timestamp + updateViewWithFlush("UPDATE %s USING TIMESTAMP 18 SET e=1 WHERE k=1 AND c=1", flush); + assertRows(execute("SELECT * from %s"), row(1, 1, null, null, 1, null)); + assertRows(execute("SELECT * from " + mv), row(1, 1, null, null)); + + updateViewWithFlush("UPDATE %s USING TIMESTAMP 18 SET e=null WHERE k=1 AND c=1", flush); + assertRows(execute("SELECT * from %s")); + assertRows(execute("SELECT * from " + mv)); + + updateViewWithFlush("UPDATE %s USING TIMESTAMP 16 SET a=1 WHERE k=1 AND c=1", flush); + assertRows(execute("SELECT * from %s"), row(1, 1, 1, null, null, null)); + assertRows(execute("SELECT * from " + mv), row(1, 1, 1, null)); + } + + @Test + public void testRangeDeletionWithFlush() throws Throwable + { + testRangeDeletion(true); + } + + @Test + public void testRangeDeletionWithoutFlush() throws Throwable + { + testRangeDeletion(false); + } + + private void testRangeDeletion(boolean flush) throws Throwable + { + // for partition range deletion, need to know that existing row is shadowed instead of not existed. + createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY (a))"); + + execute("USE " + keyspace()); + executeNet(version, "USE " + keyspace()); + + String mv = createView("CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s " + + "WHERE a IS NOT NULL AND b IS NOT NULL PRIMARY KEY (a, b)"); + + Keyspace ks = Keyspace.open(keyspace()); + ks.getColumnFamilyStore(mv).disableAutoCompaction(); + + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?) using timestamp 0", 1, 1, 1, 1); + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + + assertRowsIgnoringOrder(execute("SELECT * FROM " + mv), row(1, 1, 1, 1)); + + // remove view row + updateView("UPDATE %s using timestamp 1 set b = null WHERE a=1"); + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + + assertRowsIgnoringOrder(execute("SELECT * FROM " + mv)); + // remove base row, no view updated generated. + updateView("DELETE FROM %s using timestamp 2 where a=1"); + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + + assertRowsIgnoringOrder(execute("SELECT * FROM " + mv)); + + // restor view row with b,c column. d is still tombstone + updateView("UPDATE %s using timestamp 3 set b = 1,c = 1 where a=1"); // upsert + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + + assertRowsIgnoringOrder(execute("SELECT * FROM " + mv), row(1, 1, 1, null)); + } +} diff --git a/test/unit/org/apache/cassandra/cql3/ViewComplexDeletionsTest.java b/test/unit/org/apache/cassandra/cql3/ViewComplexDeletionsTest.java index 33fa6d9..24d76d4 100644 --- a/test/unit/org/apache/cassandra/cql3/ViewComplexDeletionsTest.java +++ b/test/unit/org/apache/cassandra/cql3/ViewComplexDeletionsTest.java @@ -21,10 +21,8 @@ package org.apache.cassandra.cql3; import java.util.Arrays; import java.util.Comparator; import java.util.List; -import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; -import org.junit.Ignore; import org.junit.Test; import org.apache.cassandra.db.ColumnFamilyStore; @@ -44,196 +42,11 @@ import static org.junit.Assert.assertEquals; * - ViewComplexTTLTest * - ViewComplexTest * - ViewComplexLivenessTest + * - ... + * - ViewComplex*Test */ public class ViewComplexDeletionsTest extends ViewComplexTester { - // for now, unselected column cannot be fully supported, SEE CASSANDRA-11500 - @Ignore - @Test - public void testPartialDeleteUnselectedColumn() throws Throwable - { - boolean flush = true; - execute("USE " + keyspace()); - executeNet(version, "USE " + keyspace()); - createTable("CREATE TABLE %s (k int, c int, a int, b int, PRIMARY KEY (k, c))"); - String mv = createView("CREATE MATERIALIZED VIEW %s " + - "AS SELECT k,c FROM %%s WHERE k IS NOT NULL AND c IS NOT NULL PRIMARY KEY (k,c)"); - Keyspace ks = Keyspace.open(keyspace()); - ks.getColumnFamilyStore(mv).disableAutoCompaction(); - - updateView("UPDATE %s USING TIMESTAMP 10 SET b=1 WHERE k=1 AND c=1"); - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - assertRows(execute("SELECT * from %s"), row(1, 1, null, 1)); - assertRows(execute("SELECT * from " + mv), row(1, 1)); - updateView("DELETE b FROM %s USING TIMESTAMP 11 WHERE k=1 AND c=1"); - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - assertEmpty(execute("SELECT * from %s")); - assertEmpty(execute("SELECT * from " + mv)); - updateView("UPDATE %s USING TIMESTAMP 1 SET a=1 WHERE k=1 AND c=1"); - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - assertRows(execute("SELECT * from %s"), row(1, 1, 1, null)); - assertRows(execute("SELECT * from " + mv), row(1, 1)); - - execute("truncate %s;"); - - // removal generated by unselected column should not shadow PK update with smaller timestamp - updateViewWithFlush("UPDATE %s USING TIMESTAMP 18 SET a=1 WHERE k=1 AND c=1", flush); - assertRows(execute("SELECT * from %s"), row(1, 1, 1, null)); - assertRows(execute("SELECT * from " + mv), row(1, 1)); - - updateViewWithFlush("UPDATE %s USING TIMESTAMP 20 SET a=null WHERE k=1 AND c=1", flush); - assertRows(execute("SELECT * from %s")); - assertRows(execute("SELECT * from " + mv)); - - updateViewWithFlush("INSERT INTO %s(k,c) VALUES(1,1) USING TIMESTAMP 15", flush); - assertRows(execute("SELECT * from %s"), row(1, 1, null, null)); - assertRows(execute("SELECT * from " + mv), row(1, 1)); - } - - @Test - public void testPartialDeleteSelectedColumnWithFlush() throws Throwable - { - testPartialDeleteSelectedColumn(true); - } - - @Test - public void testPartialDeleteSelectedColumnWithoutFlush() throws Throwable - { - testPartialDeleteSelectedColumn(false); - } - - private void testPartialDeleteSelectedColumn(boolean flush) throws Throwable - { - execute("USE " + keyspace()); - executeNet(version, "USE " + keyspace()); - createTable("CREATE TABLE %s (k int, c int, a int, b int, e int, f int, PRIMARY KEY (k, c))"); - String mv = createView("CREATE MATERIALIZED VIEW %s AS SELECT a, b, c, k FROM %%s " + - "WHERE k IS NOT NULL AND c IS NOT NULL PRIMARY KEY (k,c)"); - Keyspace ks = Keyspace.open(keyspace()); - ks.getColumnFamilyStore(mv).disableAutoCompaction(); - - updateViewWithFlush("UPDATE %s USING TIMESTAMP 10 SET b=1 WHERE k=1 AND c=1", flush); - assertRows(execute("SELECT * from %s"), row(1, 1, null, 1, null, null)); - assertRows(execute("SELECT * from " + mv), row(1, 1, null, 1)); - - updateViewWithFlush("DELETE b FROM %s USING TIMESTAMP 11 WHERE k=1 AND c=1", flush); - assertEmpty(execute("SELECT * from %s")); - assertEmpty(execute("SELECT * from " + mv)); - - updateViewWithFlush("UPDATE %s USING TIMESTAMP 1 SET a=1 WHERE k=1 AND c=1", flush); - assertRows(execute("SELECT * from %s"), row(1, 1, 1, null, null, null)); - assertRows(execute("SELECT * from " + mv), row(1, 1, 1, null)); - - updateViewWithFlush("DELETE a FROM %s USING TIMESTAMP 1 WHERE k=1 AND c=1", flush); - assertEmpty(execute("SELECT * from %s")); - assertEmpty(execute("SELECT * from " + mv)); - - // view livenessInfo should not be affected by selected column ts or tb - updateViewWithFlush("INSERT INTO %s(k,c) VALUES(1,1) USING TIMESTAMP 0", flush); - assertRows(execute("SELECT * from %s"), row(1, 1, null, null, null, null)); - assertRows(execute("SELECT * from " + mv), row(1, 1, null, null)); - - updateViewWithFlush("UPDATE %s USING TIMESTAMP 12 SET b=1 WHERE k=1 AND c=1", flush); - assertRows(execute("SELECT * from %s"), row(1, 1, null, 1, null, null)); - assertRows(execute("SELECT * from " + mv), row(1, 1, null, 1)); - - updateViewWithFlush("DELETE b FROM %s USING TIMESTAMP 13 WHERE k=1 AND c=1", flush); - assertRows(execute("SELECT * from %s"), row(1, 1, null, null, null, null)); - assertRows(execute("SELECT * from " + mv), row(1, 1, null, null)); - - updateViewWithFlush("DELETE FROM %s USING TIMESTAMP 14 WHERE k=1 AND c=1", flush); - assertEmpty(execute("SELECT * from %s")); - assertEmpty(execute("SELECT * from " + mv)); - - updateViewWithFlush("INSERT INTO %s(k,c) VALUES(1,1) USING TIMESTAMP 15", flush); - assertRows(execute("SELECT * from %s"), row(1, 1, null, null, null, null)); - assertRows(execute("SELECT * from " + mv), row(1, 1, null, null)); - - updateViewWithFlush("UPDATE %s USING TTL 3 SET b=1 WHERE k=1 AND c=1", flush); - assertRows(execute("SELECT * from %s"), row(1, 1, null, 1, null, null)); - assertRows(execute("SELECT * from " + mv), row(1, 1, null, 1)); - - TimeUnit.SECONDS.sleep(4); - - assertRows(execute("SELECT * from %s"), row(1, 1, null, null, null, null)); - assertRows(execute("SELECT * from " + mv), row(1, 1, null, null)); - - updateViewWithFlush("DELETE FROM %s USING TIMESTAMP 15 WHERE k=1 AND c=1", flush); - assertEmpty(execute("SELECT * from %s")); - assertEmpty(execute("SELECT * from " + mv)); - - execute("truncate %s;"); - - // removal generated by unselected column should not shadow selected column with smaller timestamp - updateViewWithFlush("UPDATE %s USING TIMESTAMP 18 SET e=1 WHERE k=1 AND c=1", flush); - assertRows(execute("SELECT * from %s"), row(1, 1, null, null, 1, null)); - assertRows(execute("SELECT * from " + mv), row(1, 1, null, null)); - - updateViewWithFlush("UPDATE %s USING TIMESTAMP 18 SET e=null WHERE k=1 AND c=1", flush); - assertRows(execute("SELECT * from %s")); - assertRows(execute("SELECT * from " + mv)); - - updateViewWithFlush("UPDATE %s USING TIMESTAMP 16 SET a=1 WHERE k=1 AND c=1", flush); - assertRows(execute("SELECT * from %s"), row(1, 1, 1, null, null, null)); - assertRows(execute("SELECT * from " + mv), row(1, 1, 1, null)); - } - - @Test - public void testRangeDeletionWithFlush() throws Throwable - { - testRangeDeletion(true); - } - - @Test - public void testRangeDeletionWithoutFlush() throws Throwable - { - testRangeDeletion(false); - } - - private void testRangeDeletion(boolean flush) throws Throwable - { - // for partition range deletion, need to know that existing row is shadowed instead of not existed. - createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY (a))"); - - execute("USE " + keyspace()); - executeNet(version, "USE " + keyspace()); - - String mv = createView("CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s " + - "WHERE a IS NOT NULL AND b IS NOT NULL PRIMARY KEY (a, b)"); - - Keyspace ks = Keyspace.open(keyspace()); - ks.getColumnFamilyStore(mv).disableAutoCompaction(); - - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?) using timestamp 0", 1, 1, 1, 1); - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - - assertRowsIgnoringOrder(execute("SELECT * FROM " + mv), row(1, 1, 1, 1)); - - // remove view row - updateView("UPDATE %s using timestamp 1 set b = null WHERE a=1"); - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - - assertRowsIgnoringOrder(execute("SELECT * FROM " + mv)); - // remove base row, no view updated generated. - updateView("DELETE FROM %s using timestamp 2 where a=1"); - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - - assertRowsIgnoringOrder(execute("SELECT * FROM " + mv)); - - // restor view row with b,c column. d is still tombstone - updateView("UPDATE %s using timestamp 3 set b = 1,c = 1 where a=1"); // upsert - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - - assertRowsIgnoringOrder(execute("SELECT * FROM " + mv), row(1, 1, 1, null)); - } - @Test public void testCommutativeRowDeletionFlush() throws Throwable { diff --git a/test/unit/org/apache/cassandra/cql3/ViewComplexLivenessLimitTest.java b/test/unit/org/apache/cassandra/cql3/ViewComplexLivenessLimitTest.java new file mode 100644 index 0000000..2be826f --- /dev/null +++ b/test/unit/org/apache/cassandra/cql3/ViewComplexLivenessLimitTest.java @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.cql3; + +import java.util.Arrays; + +import org.junit.Test; + +import org.apache.cassandra.db.Keyspace; + +import static org.junit.Assert.assertEquals; + +/* ViewComplexTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670, CASSANDRA-17167) + * Any changes here check if they apply to the other classes: + * - ViewComplexUpdatesTest + * - ViewComplexDeletionsTest + * - ViewComplexTTLTest + * - ViewComplexTest + * - ViewComplexLivenessTest + * - ... + * - ViewComplex*Test + */ +public class ViewComplexLivenessLimitTest extends ViewComplexTester +{ + @Test + public void testExpiredLivenessLimitWithFlush() throws Throwable + { + // CASSANDRA-13883 + testExpiredLivenessLimit(true); + } + + @Test + public void testExpiredLivenessLimitWithoutFlush() throws Throwable + { + // CASSANDRA-13883 + testExpiredLivenessLimit(false); + } + + private void testExpiredLivenessLimit(boolean flush) throws Throwable + { + createTable("CREATE TABLE %s (k int PRIMARY KEY, a int, b int);"); + + execute("USE " + keyspace()); + executeNet(version, "USE " + keyspace()); + Keyspace ks = Keyspace.open(keyspace()); + + String mv1 = createView("CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s " + + "WHERE k IS NOT NULL AND a IS NOT NULL PRIMARY KEY (k, a)"); + String mv2 = createView("CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s " + + "WHERE k IS NOT NULL AND a IS NOT NULL PRIMARY KEY (a, k)"); + ks.getColumnFamilyStore(mv1).disableAutoCompaction(); + ks.getColumnFamilyStore(mv2).disableAutoCompaction(); + + for (int i = 1; i <= 100; i++) + updateView("INSERT INTO %s(k, a, b) VALUES (?, ?, ?);", i, i, i); + for (int i = 1; i <= 100; i++) + { + if (i % 50 == 0) + continue; + // create expired liveness + updateView("DELETE a FROM %s WHERE k = ?;", i); + } + if (flush) + { + ks.getColumnFamilyStore(mv1).forceBlockingFlush(); + ks.getColumnFamilyStore(mv2).forceBlockingFlush(); + } + + for (String view : Arrays.asList(mv1, mv2)) + { + // paging + assertEquals(1, executeNetWithPaging(version, String.format("SELECT k,a,b FROM %s limit 1", view), 1).all().size()); + assertEquals(2, executeNetWithPaging(version, String.format("SELECT k,a,b FROM %s limit 2", view), 1).all().size()); + assertEquals(2, executeNetWithPaging(version, String.format("SELECT k,a,b FROM %s", view), 1).all().size()); + assertRowsNet(version, executeNetWithPaging(version, String.format("SELECT k,a,b FROM %s ", view), 1), + row(50, 50, 50), + row(100, 100, 100)); + // limit + assertEquals(1, execute(String.format("SELECT k,a,b FROM %s limit 1", view)).size()); + assertRowsIgnoringOrder(execute(String.format("SELECT k,a,b FROM %s limit 2", view)), + row(50, 50, 50), + row(100, 100, 100)); + } + } +} diff --git a/test/unit/org/apache/cassandra/cql3/ViewComplexLivenessTest.java b/test/unit/org/apache/cassandra/cql3/ViewComplexLivenessTest.java index 7b4aa1a..1e327e2 100644 --- a/test/unit/org/apache/cassandra/cql3/ViewComplexLivenessTest.java +++ b/test/unit/org/apache/cassandra/cql3/ViewComplexLivenessTest.java @@ -18,8 +18,6 @@ package org.apache.cassandra.cql3; -import java.util.Arrays; - import org.junit.Test; import org.apache.cassandra.db.ColumnFamilyStore; @@ -28,13 +26,15 @@ import org.apache.cassandra.utils.FBUtilities; import static org.junit.Assert.assertEquals; -/* ViewComplexTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670) +/* ViewComplexTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670, CASSANDRA-17167) * Any changes here check if they apply to the other classes: * - ViewComplexUpdatesTest * - ViewComplexDeletionsTest * - ViewComplexTTLTest * - ViewComplexTest * - ViewComplexLivenessTest + * - ... + * - ViewComplex*Test */ public class ViewComplexLivenessTest extends ViewComplexTester { @@ -91,67 +91,6 @@ public class ViewComplexLivenessTest extends ViewComplexTester } @Test - public void testExpiredLivenessLimitWithFlush() throws Throwable - { - // CASSANDRA-13883 - testExpiredLivenessLimit(true); - } - - @Test - public void testExpiredLivenessLimitWithoutFlush() throws Throwable - { - // CASSANDRA-13883 - testExpiredLivenessLimit(false); - } - - private void testExpiredLivenessLimit(boolean flush) throws Throwable - { - createTable("CREATE TABLE %s (k int PRIMARY KEY, a int, b int);"); - - execute("USE " + keyspace()); - executeNet(version, "USE " + keyspace()); - Keyspace ks = Keyspace.open(keyspace()); - - String mv1 = createView("CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s " + - "WHERE k IS NOT NULL AND a IS NOT NULL PRIMARY KEY (k, a)"); - String mv2 = createView("CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s " + - "WHERE k IS NOT NULL AND a IS NOT NULL PRIMARY KEY (a, k)"); - ks.getColumnFamilyStore(mv1).disableAutoCompaction(); - ks.getColumnFamilyStore(mv2).disableAutoCompaction(); - - for (int i = 1; i <= 100; i++) - updateView("INSERT INTO %s(k, a, b) VALUES (?, ?, ?);", i, i, i); - for (int i = 1; i <= 100; i++) - { - if (i % 50 == 0) - continue; - // create expired liveness - updateView("DELETE a FROM %s WHERE k = ?;", i); - } - if (flush) - { - ks.getColumnFamilyStore(mv1).forceBlockingFlush(); - ks.getColumnFamilyStore(mv2).forceBlockingFlush(); - } - - for (String view : Arrays.asList(mv1, mv2)) - { - // paging - assertEquals(1, executeNetWithPaging(version, String.format("SELECT k,a,b FROM %s limit 1", view), 1).all().size()); - assertEquals(2, executeNetWithPaging(version, String.format("SELECT k,a,b FROM %s limit 2", view), 1).all().size()); - assertEquals(2, executeNetWithPaging(version, String.format("SELECT k,a,b FROM %s", view), 1).all().size()); - assertRowsNet(version, executeNetWithPaging(version, String.format("SELECT k,a,b FROM %s ", view), 1), - row(50, 50, 50), - row(100, 100, 100)); - // limit - assertEquals(1, execute(String.format("SELECT k,a,b FROM %s limit 1", view)).size()); - assertRowsIgnoringOrder(execute(String.format("SELECT k,a,b FROM %s limit 2", view)), - row(50, 50, 50), - row(100, 100, 100)); - } - } - - @Test public void testStrictLivenessTombstone() throws Throwable { createTable("create table %s (p int primary key, v1 int, v2 int)"); diff --git a/test/unit/org/apache/cassandra/cql3/ViewComplexTTLTest.java b/test/unit/org/apache/cassandra/cql3/ViewComplexTTLTest.java index 7c49ffa..b44c8d0 100644 --- a/test/unit/org/apache/cassandra/cql3/ViewComplexTTLTest.java +++ b/test/unit/org/apache/cassandra/cql3/ViewComplexTTLTest.java @@ -26,13 +26,15 @@ import org.apache.cassandra.utils.FBUtilities; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -/* ViewComplexTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670) +/* ViewComplexTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670, CASSANDRA-17167) * Any changes here check if they apply to the other classes: * - ViewComplexUpdatesTest * - ViewComplexDeletionsTest * - ViewComplexTTLTest * - ViewComplexTest * - ViewComplexLivenessTest + * - ... + * - ViewComplex*Test */ public class ViewComplexTTLTest extends ViewComplexTester { diff --git a/test/unit/org/apache/cassandra/cql3/ViewComplexTest.java b/test/unit/org/apache/cassandra/cql3/ViewComplexTest.java index 27cbf07..8098dcf 100644 --- a/test/unit/org/apache/cassandra/cql3/ViewComplexTest.java +++ b/test/unit/org/apache/cassandra/cql3/ViewComplexTest.java @@ -21,31 +21,30 @@ package org.apache.cassandra.cql3; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; -import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import com.google.common.base.Objects; + import org.junit.Test; import org.apache.cassandra.db.ColumnFamilyStore; import org.apache.cassandra.db.Keyspace; -import org.apache.cassandra.db.compaction.CompactionManager; -import org.apache.cassandra.io.sstable.format.SSTableReader; import org.apache.cassandra.utils.FBUtilities; import static org.junit.Assert.fail; -/* ViewComplexTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670) +/* ViewComplexTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670, CASSANDRA-17167) * Any changes here check if they apply to the other classes: * - ViewComplexUpdatesTest * - ViewComplexDeletionsTest * - ViewComplexTTLTest * - ViewComplexTest * - ViewComplexLivenessTest + * - ... + * - ViewComplex*Test */ public class ViewComplexTest extends ViewComplexTester { @@ -113,77 +112,6 @@ public class ViewComplexTest extends ViewComplexTester } @Test - public void testCellTombstoneAndShadowableTombstonesWithFlush() throws Throwable - { - testCellTombstoneAndShadowableTombstones(true); - } - - @Test - public void testCellTombstoneAndShadowableTombstonesWithoutFlush() throws Throwable - { - testCellTombstoneAndShadowableTombstones(false); - } - - private void testCellTombstoneAndShadowableTombstones(boolean flush) throws Throwable - { - createTable("create table %s (p int primary key, v1 int, v2 int)"); - - execute("USE " + keyspace()); - executeNet(version, "USE " + keyspace()); - Keyspace ks = Keyspace.open(keyspace()); - - String mv = createView("create materialized view %s as select * from %%s " + - "where p is not null and v1 is not null primary key (v1, p)"); - ks.getColumnFamilyStore(mv).disableAutoCompaction(); - - // sstable 1, Set initial values TS=1 - updateView("Insert into %s (p, v1, v2) values (3, 1, 3) using timestamp 1;"); - - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - - assertRowsIgnoringOrder(execute("SELECT v2, WRITETIME(v2) from " + mv + " WHERE v1 = ? AND p = ?", 1, 3), row(3, 1L)); - // sstable 2 - updateView("UPdate %s using timestamp 2 set v2 = null where p = 3"); - - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - - assertRowsIgnoringOrder(execute("SELECT v2, WRITETIME(v2) from " + mv + " WHERE v1 = ? AND p = ?", 1, 3), - row(null, null)); - // sstable 3 - updateView("UPdate %s using timestamp 3 set v1 = 2 where p = 3"); - - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - - assertRowsIgnoringOrder(execute("SELECT v1, p, v2, WRITETIME(v2) from " + mv), row(2, 3, null, null)); - // sstable 4 - updateView("UPdate %s using timestamp 4 set v1 = 1 where p = 3"); - - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - - assertRowsIgnoringOrder(execute("SELECT v1, p, v2, WRITETIME(v2) from " + mv), row(1, 3, null, null)); - - if (flush) - { - // compact sstable 2 and 3; - ColumnFamilyStore cfs = ks.getColumnFamilyStore(mv); - List<String> sstables = cfs.getLiveSSTables() - .stream() - .sorted(Comparator.comparingInt(s -> s.descriptor.generation)) - .map(SSTableReader::getFilename) - .collect(Collectors.toList()); - String dataFiles = String.join(",", Arrays.asList(sstables.get(1), sstables.get(2))); - CompactionManager.instance.forceUserDefinedCompaction(dataFiles); - } - // cell-tombstone in sstable 4 is not compacted away, because the shadowable tombstone is shadowed by new row. - assertRowsIgnoringOrder(execute("SELECT v1, p, v2, WRITETIME(v2) from " + mv), row(1, 3, null, null)); - assertRowsIgnoringOrder(execute("SELECT v1, p, v2, WRITETIME(v2) from " + mv + " limit 1"), row(1, 3, null, null)); - } - - @Test public void testMVWithDifferentColumnsWithFlush() throws Throwable { testMVWithDifferentColumns(true); diff --git a/test/unit/org/apache/cassandra/cql3/ViewComplexTester.java b/test/unit/org/apache/cassandra/cql3/ViewComplexTester.java index a934649..de30eec 100644 --- a/test/unit/org/apache/cassandra/cql3/ViewComplexTester.java +++ b/test/unit/org/apache/cassandra/cql3/ViewComplexTester.java @@ -35,13 +35,15 @@ import org.apache.cassandra.concurrent.Stage; import org.apache.cassandra.db.Keyspace; import org.apache.cassandra.transport.ProtocolVersion; -/* ViewComplexTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670) +/* ViewComplexTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670, CASSANDRA-17167) * Any changes here check if they apply to the other classes: * - ViewComplexUpdatesTest * - ViewComplexDeletionsTest * - ViewComplexTTLTest * - ViewComplexTest * - ViewComplexLivenessTest + * - ... + * - ViewComplex*Test */ @RunWith(Parameterized.class) public abstract class ViewComplexTester extends CQLTester diff --git a/test/unit/org/apache/cassandra/cql3/ViewComplexTombstoneTest.java b/test/unit/org/apache/cassandra/cql3/ViewComplexTombstoneTest.java new file mode 100644 index 0000000..3c484ac --- /dev/null +++ b/test/unit/org/apache/cassandra/cql3/ViewComplexTombstoneTest.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.cql3; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +import org.junit.Test; + +import org.apache.cassandra.db.ColumnFamilyStore; +import org.apache.cassandra.db.Keyspace; +import org.apache.cassandra.db.compaction.CompactionManager; +import org.apache.cassandra.io.sstable.format.SSTableReader; +import org.apache.cassandra.utils.FBUtilities; + +/* ViewComplexTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670, CASSANDRA-17167) + * Any changes here check if they apply to the other classes: + * - ViewComplexUpdatesTest + * - ViewComplexDeletionsTest + * - ViewComplexTTLTest + * - ViewComplexTest + * - ViewComplexLivenessTest + * - ... + * - ViewComplex*Test + */ +public class ViewComplexTombstoneTest extends ViewComplexTester +{ + @Test + public void testCellTombstoneAndShadowableTombstonesWithFlush() throws Throwable + { + testCellTombstoneAndShadowableTombstones(true); + } + + @Test + public void testCellTombstoneAndShadowableTombstonesWithoutFlush() throws Throwable + { + testCellTombstoneAndShadowableTombstones(false); + } + + private void testCellTombstoneAndShadowableTombstones(boolean flush) throws Throwable + { + createTable("create table %s (p int primary key, v1 int, v2 int)"); + + execute("USE " + keyspace()); + executeNet(version, "USE " + keyspace()); + Keyspace ks = Keyspace.open(keyspace()); + + String mv = createView("create materialized view %s as select * from %%s " + + "where p is not null and v1 is not null primary key (v1, p)"); + ks.getColumnFamilyStore(mv).disableAutoCompaction(); + + // sstable 1, Set initial values TS=1 + updateView("Insert into %s (p, v1, v2) values (3, 1, 3) using timestamp 1;"); + + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + + assertRowsIgnoringOrder(execute("SELECT v2, WRITETIME(v2) from " + mv + " WHERE v1 = ? AND p = ?", 1, 3), row(3, 1L)); + // sstable 2 + updateView("UPdate %s using timestamp 2 set v2 = null where p = 3"); + + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + + assertRowsIgnoringOrder(execute("SELECT v2, WRITETIME(v2) from " + mv + " WHERE v1 = ? AND p = ?", 1, 3), + row(null, null)); + // sstable 3 + updateView("UPdate %s using timestamp 3 set v1 = 2 where p = 3"); + + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + + assertRowsIgnoringOrder(execute("SELECT v1, p, v2, WRITETIME(v2) from " + mv), row(2, 3, null, null)); + // sstable 4 + updateView("UPdate %s using timestamp 4 set v1 = 1 where p = 3"); + + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + + assertRowsIgnoringOrder(execute("SELECT v1, p, v2, WRITETIME(v2) from " + mv), row(1, 3, null, null)); + + if (flush) + { + // compact sstable 2 and 3; + ColumnFamilyStore cfs = ks.getColumnFamilyStore(mv); + List<String> sstables = cfs.getLiveSSTables() + .stream() + .sorted(Comparator.comparingInt(s -> s.descriptor.generation)) + .map(SSTableReader::getFilename) + .collect(Collectors.toList()); + String dataFiles = String.join(",", Arrays.asList(sstables.get(1), sstables.get(2))); + CompactionManager.instance.forceUserDefinedCompaction(dataFiles); + } + // cell-tombstone in sstable 4 is not compacted away, because the shadowable tombstone is shadowed by new row. + assertRowsIgnoringOrder(execute("SELECT v1, p, v2, WRITETIME(v2) from " + mv), row(1, 3, null, null)); + assertRowsIgnoringOrder(execute("SELECT v1, p, v2, WRITETIME(v2) from " + mv + " limit 1"), row(1, 3, null, null)); + } +} diff --git a/test/unit/org/apache/cassandra/cql3/ViewComplexUpdatesTest.java b/test/unit/org/apache/cassandra/cql3/ViewComplexUpdatesTest.java index 76ae361..c930fa0 100644 --- a/test/unit/org/apache/cassandra/cql3/ViewComplexUpdatesTest.java +++ b/test/unit/org/apache/cassandra/cql3/ViewComplexUpdatesTest.java @@ -25,13 +25,15 @@ import org.junit.Test; import org.apache.cassandra.db.Keyspace; import org.apache.cassandra.utils.FBUtilities; -/* ViewComplexTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670) +/* ViewComplexTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670, CASSANDRA-17167) * Any changes here check if they apply to the other classes: * - ViewComplexUpdatesTest * - ViewComplexDeletionsTest * - ViewComplexTTLTest * - ViewComplexTest * - ViewComplexLivenessTest + * - ... + * - ViewComplex*Test */ public class ViewComplexUpdatesTest extends ViewComplexTester { diff --git a/test/unit/org/apache/cassandra/cql3/ViewFiltering2Test.java b/test/unit/org/apache/cassandra/cql3/ViewFiltering2Test.java new file mode 100644 index 0000000..2eb0eca --- /dev/null +++ b/test/unit/org/apache/cassandra/cql3/ViewFiltering2Test.java @@ -0,0 +1,452 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.cql3; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import org.apache.cassandra.db.Keyspace; +import org.apache.cassandra.db.SystemKeyspace; +import org.apache.cassandra.utils.FBUtilities; + +/* ViewFilteringTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670, CASSANDRA-17167) + * Any changes here check if they apply to the other classes + * - ViewFilteringPKTest + * - ViewFilteringClustering1Test + * - ViewFilteringClustering2Test + * - ViewFilteringTest + * - ... + * - ViewFiltering*Test + */ +@RunWith(Parameterized.class) +public class ViewFiltering2Test extends ViewFilteringTester +{ + @Test + public void testAllTypes() throws Throwable + { + String myType = createType("CREATE TYPE %s (a int, b uuid, c set<text>)"); + String columnNames = "asciival, " + + "bigintval, " + + "blobval, " + + "booleanval, " + + "dateval, " + + "decimalval, " + + "doubleval, " + + "floatval, " + + "inetval, " + + "intval, " + + "textval, " + + "timeval, " + + "timestampval, " + + "timeuuidval, " + + "uuidval," + + "varcharval, " + + "varintval, " + + "frozenlistval, " + + "frozensetval, " + + "frozenmapval, " + + "tupleval, " + + "udtval"; + + createTable( + "CREATE TABLE %s (" + + "asciival ascii, " + + "bigintval bigint, " + + "blobval blob, " + + "booleanval boolean, " + + "dateval date, " + + "decimalval decimal, " + + "doubleval double, " + + "floatval float, " + + "inetval inet, " + + "intval int, " + + "textval text, " + + "timeval time, " + + "timestampval timestamp, " + + "timeuuidval timeuuid, " + + "uuidval uuid," + + "varcharval varchar, " + + "varintval varint, " + + "frozenlistval frozen<list<int>>, " + + "frozensetval frozen<set<uuid>>, " + + "frozenmapval frozen<map<ascii, int>>," + + "tupleval frozen<tuple<int, ascii, uuid>>," + + "udtval frozen<" + myType + ">, " + + "PRIMARY KEY (" + columnNames + "))"); + + execute("USE " + keyspace()); + executeNet(version, "USE " + keyspace()); + + createView( + "mv_test", + "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE " + + "asciival = 'abc' AND " + + "bigintval = 123 AND " + + "blobval = 0xfeed AND " + + "booleanval = true AND " + + "dateval = '1987-03-23' AND " + + "decimalval = 123.123 AND " + + "doubleval = 123.123 AND " + + "floatval = 123.123 AND " + + "inetval = '127.0.0.1' AND " + + "intval = 123 AND " + + "textval = 'abc' AND " + + "timeval = '07:35:07.000111222' AND " + + "timestampval = 123123123 AND " + + "timeuuidval = 6BDDC89A-5644-11E4-97FC-56847AFE9799 AND " + + "uuidval = 6BDDC89A-5644-11E4-97FC-56847AFE9799 AND " + + "varcharval = 'abc' AND " + + "varintval = 123123123 AND " + + "frozenlistval = [1, 2, 3] AND " + + "frozensetval = {6BDDC89A-5644-11E4-97FC-56847AFE9799} AND " + + "frozenmapval = {'a': 1, 'b': 2} AND " + + "tupleval = (1, 'foobar', 6BDDC89A-5644-11E4-97FC-56847AFE9799) AND " + + "udtval = {a: 1, b: 6BDDC89A-5644-11E4-97FC-56847AFE9799, c: {'foo', 'bar'}} " + + "PRIMARY KEY (" + columnNames + ")"); + + execute("INSERT INTO %s (" + columnNames + ") VALUES (" + + "'abc'," + + "123," + + "0xfeed," + + "true," + + "'1987-03-23'," + + "123.123," + + "123.123," + + "123.123," + + "'127.0.0.1'," + + "123," + + "'abc'," + + "'07:35:07.000111222'," + + "123123123," + + "6BDDC89A-5644-11E4-97FC-56847AFE9799," + + "6BDDC89A-5644-11E4-97FC-56847AFE9799," + + "'abc'," + + "123123123," + + "[1, 2, 3]," + + "{6BDDC89A-5644-11E4-97FC-56847AFE9799}," + + "{'a': 1, 'b': 2}," + + "(1, 'foobar', 6BDDC89A-5644-11E4-97FC-56847AFE9799)," + + "{a: 1, b: 6BDDC89A-5644-11E4-97FC-56847AFE9799, c: {'foo', 'bar'}})"); + + assert !execute("SELECT * FROM mv_test").isEmpty(); + + executeNet(version, "ALTER TABLE %s RENAME inetval TO foo"); + assert !execute("SELECT * FROM mv_test").isEmpty(); + } + + @Test + public void testMVCreationWithNonPrimaryRestrictions() throws Throwable + { + createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY (a, b))"); + + execute("USE " + keyspace()); + executeNet(version, "USE " + keyspace()); + + try { + createView("mv_test", "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL AND d = 1 PRIMARY KEY (a, b, c)"); + dropView("mv_test"); + } catch(Exception e) { + throw new RuntimeException("MV creation with non primary column restrictions failed.", e); + } + + dropTable("DROP TABLE %s"); + } + + @Test + public void testNonPrimaryRestrictions() throws Throwable + { + createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY (a, b))"); + + execute("USE " + keyspace()); + executeNet(version, "USE " + keyspace()); + + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 0, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 1, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 0, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 1, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 0, 0, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 0, 1, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 0, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 1, 0); + + // only accept rows where c = 1 + createView("mv_test", "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL AND c = 1 PRIMARY KEY (a, b, c)"); + + while (!SystemKeyspace.isViewBuilt(keyspace(), "mv_test")) + Thread.sleep(10); + + assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), + row(0, 0, 1, 0), + row(0, 1, 1, 0), + row(1, 0, 1, 0), + row(1, 1, 1, 0) + ); + + // insert new rows that do not match the filter + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 0, 0, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 1, 2, 0); + assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), + row(0, 0, 1, 0), + row(0, 1, 1, 0), + row(1, 0, 1, 0), + row(1, 1, 1, 0) + ); + + // insert new row that does match the filter + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 2, 1, 0); + assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), + row(0, 0, 1, 0), + row(0, 1, 1, 0), + row(1, 0, 1, 0), + row(1, 1, 1, 0), + row(1, 2, 1, 0) + ); + + // update rows that don't match the filter + execute("UPDATE %s SET d = ? WHERE a = ? AND b = ?", 2, 2, 0); + execute("UPDATE %s SET d = ? WHERE a = ? AND b = ?", 1, 2, 1); + assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), + row(0, 0, 1, 0), + row(0, 1, 1, 0), + row(1, 0, 1, 0), + row(1, 1, 1, 0), + row(1, 2, 1, 0) + ); + + // update a row that does match the filter + execute("UPDATE %s SET d = ? WHERE a = ? AND b = ?", 1, 1, 0); + assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), + row(0, 0, 1, 0), + row(0, 1, 1, 0), + row(1, 0, 1, 1), + row(1, 1, 1, 0), + row(1, 2, 1, 0) + ); + + // delete rows that don't match the filter + execute("DELETE FROM %s WHERE a = ? AND b = ?", 2, 0); + assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), + row(0, 0, 1, 0), + row(0, 1, 1, 0), + row(1, 0, 1, 1), + row(1, 1, 1, 0), + row(1, 2, 1, 0) + ); + + // delete a row that does match the filter + execute("DELETE FROM %s WHERE a = ? AND b = ?", 1, 2); + assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), + row(0, 0, 1, 0), + row(0, 1, 1, 0), + row(1, 0, 1, 1), + row(1, 1, 1, 0) + ); + + // delete a partition that matches the filter + execute("DELETE FROM %s WHERE a = ?", 1); + assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), + row(0, 0, 1, 0), + row(0, 1, 1, 0) + ); + + dropView("mv_test"); + dropTable("DROP TABLE %s"); + } + + @Test + public void complexRestrictedTimestampUpdateTestWithFlush() throws Throwable + { + complexRestrictedTimestampUpdateTest(true); + } + + @Test + public void complexRestrictedTimestampUpdateTestWithoutFlush() throws Throwable + { + complexRestrictedTimestampUpdateTest(false); + } + + public void complexRestrictedTimestampUpdateTest(boolean flush) throws Throwable + { + createTable("CREATE TABLE %s (a int, b int, c int, d int, e int, PRIMARY KEY (a, b))"); + + execute("USE " + keyspace()); + executeNet(version, "USE " + keyspace()); + Keyspace ks = Keyspace.open(keyspace()); + + createView("mv", "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL AND c = 1 PRIMARY KEY (c, a, b)"); + ks.getColumnFamilyStore("mv").disableAutoCompaction(); + + //Set initial values TS=0, matching the restriction and verify view + executeNet(version, "INSERT INTO %s (a, b, c, d) VALUES (0, 0, 1, 0) USING TIMESTAMP 0"); + assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(0)); + + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + + //update c's timestamp TS=2 + executeNet(version, "UPDATE %s USING TIMESTAMP 2 SET c = ? WHERE a = ? and b = ? ", 1, 0, 0); + assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(0)); + + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + + //change c's value and TS=3, tombstones c=1 and adds c=0 record + executeNet(version, "UPDATE %s USING TIMESTAMP 3 SET c = ? WHERE a = ? and b = ? ", 0, 0, 0); + assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 0, 0, 0)); + + if(flush) + { + ks.getColumnFamilyStore("mv").forceMajorCompaction(); + FBUtilities.waitOnFutures(ks.flush()); + } + + //change c's value back to 1 with TS=4, check we can see d + executeNet(version, "UPDATE %s USING TIMESTAMP 4 SET c = ? WHERE a = ? and b = ? ", 1, 0, 0); + if (flush) + { + ks.getColumnFamilyStore("mv").forceMajorCompaction(); + FBUtilities.waitOnFutures(ks.flush()); + } + + assertRows(execute("SELECT d, e from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(0, null)); + + //Add e value @ TS=1 + executeNet(version, "UPDATE %s USING TIMESTAMP 1 SET e = ? WHERE a = ? and b = ? ", 1, 0, 0); + assertRows(execute("SELECT d, e from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(0, 1)); + + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + + //Change d value @ TS=2 + executeNet(version, "UPDATE %s USING TIMESTAMP 2 SET d = ? WHERE a = ? and b = ? ", 2, 0, 0); + assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(2)); + + if (flush) + FBUtilities.waitOnFutures(ks.flush()); + + //Change d value @ TS=3 + executeNet(version, "UPDATE %s USING TIMESTAMP 3 SET d = ? WHERE a = ? and b = ? ", 1, 0, 0); + assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(1)); + + //Tombstone c + executeNet(version, "DELETE FROM %s WHERE a = ? and b = ?", 0, 0); + assertRowsIgnoringOrder(execute("SELECT d from mv")); + assertRows(execute("SELECT d from mv")); + + //Add back without D + executeNet(version, "INSERT INTO %s (a, b, c) VALUES (0, 0, 1)"); + + //Make sure D doesn't pop back in. + assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row((Object) null)); + + //New partition + // insert a row with timestamp 0 + executeNet(version, "INSERT INTO %s (a, b, c, d, e) VALUES (?, ?, ?, ?, ?) USING TIMESTAMP 0", 1, 0, 1, 0, 0); + + // overwrite pk and e with timestamp 1, but don't overwrite d + executeNet(version, "INSERT INTO %s (a, b, c, e) VALUES (?, ?, ?, ?) USING TIMESTAMP 1", 1, 0, 1, 0); + + // delete with timestamp 0 (which should only delete d) + executeNet(version, "DELETE FROM %s USING TIMESTAMP 0 WHERE a = ? AND b = ?", 1, 0); + assertRows(execute("SELECT a, b, c, d, e from mv WHERE c = ? and a = ? and b = ?", 1, 1, 0), + row(1, 0, 1, null, 0) + ); + + executeNet(version, "UPDATE %s USING TIMESTAMP 2 SET c = ? WHERE a = ? AND b = ?", 1, 1, 1); + executeNet(version, "UPDATE %s USING TIMESTAMP 3 SET c = ? WHERE a = ? AND b = ?", 1, 1, 0); + assertRows(execute("SELECT a, b, c, d, e from mv WHERE c = ? and a = ? and b = ?", 1, 1, 0), + row(1, 0, 1, null, 0) + ); + + executeNet(version, "UPDATE %s USING TIMESTAMP 3 SET d = ? WHERE a = ? AND b = ?", 0, 1, 0); + assertRows(execute("SELECT a, b, c, d, e from mv WHERE c = ? and a = ? and b = ?", 1, 1, 0), + row(1, 0, 1, 0, 0) + ); + } + + @Test + public void testRestrictedRegularColumnTimestampUpdates() throws Throwable + { + // Regression test for CASSANDRA-10910 + + createTable("CREATE TABLE %s (" + + "k int PRIMARY KEY, " + + "c int, " + + "val int)"); + + execute("USE " + keyspace()); + executeNet(version, "USE " + keyspace()); + + createView("mv_rctstest", "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE k IS NOT NULL AND c IS NOT NULL AND c = 1 PRIMARY KEY (k,c)"); + + updateView("UPDATE %s SET c = ?, val = ? WHERE k = ?", 0, 0, 0); + updateView("UPDATE %s SET val = ? WHERE k = ?", 1, 0); + updateView("UPDATE %s SET c = ? WHERE k = ?", 1, 0); + assertRows(execute("SELECT c, k, val FROM mv_rctstest"), row(1, 0, 1)); + + updateView("TRUNCATE %s"); + + updateView("UPDATE %s USING TIMESTAMP 1 SET c = ?, val = ? WHERE k = ?", 0, 0, 0); + updateView("UPDATE %s USING TIMESTAMP 3 SET c = ? WHERE k = ?", 1, 0); + updateView("UPDATE %s USING TIMESTAMP 2 SET val = ? WHERE k = ?", 1, 0); + updateView("UPDATE %s USING TIMESTAMP 4 SET c = ? WHERE k = ?", 1, 0); + updateView("UPDATE %s USING TIMESTAMP 3 SET val = ? WHERE k = ?", 2, 0); + assertRows(execute("SELECT c, k, val FROM mv_rctstest"), row(1, 0, 2)); + } + + @Test + public void testOldTimestampsWithRestrictions() throws Throwable + { + createTable("CREATE TABLE %s (" + + "k int, " + + "c int, " + + "val text, " + "" + + "PRIMARY KEY(k, c))"); + + execute("USE " + keyspace()); + executeNet(version, "USE " + keyspace()); + + createView("mv_tstest", "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE val IS NOT NULL AND k IS NOT NULL AND c IS NOT NULL AND val = 'baz' PRIMARY KEY (val,k,c)"); + + for (int i = 0; i < 100; i++) + updateView("INSERT into %s (k,c,val)VALUES(?,?,?)", 0, i % 2, "baz"); + + Keyspace.open(keyspace()).getColumnFamilyStore(currentTable()).forceBlockingFlush(); + + Assert.assertEquals(2, execute("select * from %s").size()); + Assert.assertEquals(2, execute("select * from mv_tstest").size()); + + assertRows(execute("SELECT val from %s where k = 0 and c = 0"), row("baz")); + assertRows(execute("SELECT c from mv_tstest where k = 0 and val = ?", "baz"), row(0), row(1)); + + //Make sure an old TS does nothing + updateView("UPDATE %s USING TIMESTAMP 100 SET val = ? where k = ? AND c = ?", "bar", 0, 1); + assertRows(execute("SELECT val from %s where k = 0 and c = 1"), row("baz")); + assertRows(execute("SELECT c from mv_tstest where k = 0 and val = ?", "baz"), row(0), row(1)); + assertRows(execute("SELECT c from mv_tstest where k = 0 and val = ?", "bar")); + + //Latest TS + updateView("UPDATE %s SET val = ? where k = ? AND c = ?", "bar", 0, 1); + assertRows(execute("SELECT val from %s where k = 0 and c = 1"), row("bar")); + assertRows(execute("SELECT c from mv_tstest where k = 0 and val = ?", "bar")); + assertRows(execute("SELECT c from mv_tstest where k = 0 and val = ?", "baz"), row(0)); + } +} diff --git a/test/unit/org/apache/cassandra/cql3/ViewFilteringClustering1Test.java b/test/unit/org/apache/cassandra/cql3/ViewFilteringClustering1Test.java index 6d4e487..841eb91 100644 --- a/test/unit/org/apache/cassandra/cql3/ViewFilteringClustering1Test.java +++ b/test/unit/org/apache/cassandra/cql3/ViewFilteringClustering1Test.java @@ -18,77 +18,27 @@ package org.apache.cassandra.cql3; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.List; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.apache.cassandra.db.SystemKeyspace; -import org.apache.cassandra.transport.ProtocolVersion; -/* ViewFilteringTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670) +/* ViewFilteringTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670, CASSANDRA-17167) * Any changes here check if they apply to the other classes * - ViewFilteringPKTest * - ViewFilteringClustering1Test * - ViewFilteringClustering2Test * - ViewFilteringTest + * - ... + * - ViewFiltering*Test */ @RunWith(Parameterized.class) -public class ViewFilteringClustering1Test extends CQLTester +public class ViewFilteringClustering1Test extends ViewFilteringTester { - @Parameterized.Parameter - public ProtocolVersion version; - - @Parameterized.Parameters() - public static Collection<Object[]> versions() - { - return ViewFilteringTest.versions(); - } - - private final List<String> views = new ArrayList<>(); - - @BeforeClass - public static void startup() - { - ViewFilteringTest.startup(); - } - - @AfterClass - public static void tearDown() - { - ViewFilteringTest.tearDown(); - } - - @Before - public void begin() - { - ViewFilteringTest.beginSetup(views); - } - - @After - public void end() throws Throwable - { - ViewFilteringTest.endSetup(views, version, this); - } - - private void createView(String name, String query) throws Throwable - { - ViewFilteringTest.createView(name, query, views, version, this); - } - - private void dropView(String name) throws Throwable - { - ViewFilteringTest.dropView(name, views, version, this); - } - @Test public void testClusteringKeyEQRestrictions() throws Throwable { diff --git a/test/unit/org/apache/cassandra/cql3/ViewFilteringClustering2Test.java b/test/unit/org/apache/cassandra/cql3/ViewFilteringClustering2Test.java index d1ba842..219a807 100644 --- a/test/unit/org/apache/cassandra/cql3/ViewFilteringClustering2Test.java +++ b/test/unit/org/apache/cassandra/cql3/ViewFilteringClustering2Test.java @@ -18,77 +18,27 @@ package org.apache.cassandra.cql3; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.List; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.apache.cassandra.db.SystemKeyspace; -import org.apache.cassandra.transport.ProtocolVersion; -/* ViewFilteringTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670) +/* ViewFilteringTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670, CASSANDRA-17167) * Any changes here check if they apply to the other classes * - ViewFilteringPKTest * - ViewFilteringClustering1Test * - ViewFilteringClustering2Test * - ViewFilteringTest + * - ... + * - ViewFiltering*Test */ @RunWith(Parameterized.class) -public class ViewFilteringClustering2Test extends CQLTester +public class ViewFilteringClustering2Test extends ViewFilteringTester { - @Parameterized.Parameter - public ProtocolVersion version; - - @Parameterized.Parameters() - public static Collection<Object[]> versions() - { - return ViewFilteringTest.versions(); - } - - private final List<String> views = new ArrayList<>(); - - @BeforeClass - public static void startup() - { - ViewFilteringTest.startup(); - } - - @AfterClass - public static void tearDown() - { - ViewFilteringTest.tearDown(); - } - - @Before - public void begin() - { - ViewFilteringTest.beginSetup(views); - } - - @After - public void end() throws Throwable - { - ViewFilteringTest.endSetup(views, version, this); - } - - private void createView(String name, String query) throws Throwable - { - ViewFilteringTest.createView(name, query, views, version, this); - } - - private void dropView(String name) throws Throwable - { - ViewFilteringTest.dropView(name, views, version, this); - } - @Test public void testClusteringKeyMultiColumnRestrictions() throws Throwable { diff --git a/test/unit/org/apache/cassandra/cql3/ViewFilteringClustering2Test.java b/test/unit/org/apache/cassandra/cql3/ViewFilteringComplexPKTest.java similarity index 66% copy from test/unit/org/apache/cassandra/cql3/ViewFilteringClustering2Test.java copy to test/unit/org/apache/cassandra/cql3/ViewFilteringComplexPKTest.java index d1ba842..da089ca 100644 --- a/test/unit/org/apache/cassandra/cql3/ViewFilteringClustering2Test.java +++ b/test/unit/org/apache/cassandra/cql3/ViewFilteringComplexPKTest.java @@ -18,84 +18,34 @@ package org.apache.cassandra.cql3; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.List; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.apache.cassandra.db.SystemKeyspace; -import org.apache.cassandra.transport.ProtocolVersion; -/* ViewFilteringTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670) +/* ViewFilteringTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670, CASSANDRA-17167) * Any changes here check if they apply to the other classes * - ViewFilteringPKTest * - ViewFilteringClustering1Test * - ViewFilteringClustering2Test * - ViewFilteringTest + * - ... + * - ViewFiltering*Test */ @RunWith(Parameterized.class) -public class ViewFilteringClustering2Test extends CQLTester +public class ViewFilteringComplexPKTest extends ViewFilteringTester { - @Parameterized.Parameter - public ProtocolVersion version; - - @Parameterized.Parameters() - public static Collection<Object[]> versions() - { - return ViewFilteringTest.versions(); - } - - private final List<String> views = new ArrayList<>(); - - @BeforeClass - public static void startup() - { - ViewFilteringTest.startup(); - } - - @AfterClass - public static void tearDown() - { - ViewFilteringTest.tearDown(); - } - - @Before - public void begin() - { - ViewFilteringTest.beginSetup(views); - } - - @After - public void end() throws Throwable - { - ViewFilteringTest.endSetup(views, version, this); - } - - private void createView(String name, String query) throws Throwable - { - ViewFilteringTest.createView(name, query, views, version, this); - } - - private void dropView(String name) throws Throwable - { - ViewFilteringTest.dropView(name, views, version, this); - } - @Test - public void testClusteringKeyMultiColumnRestrictions() throws Throwable + public void testCompoundPartitionKeyRestrictions() throws Throwable { List<String> mvPrimaryKeys = Arrays.asList("((a, b), c)", "((b, a), c)", "(a, b, c)", "(c, b, a)", "((c, a), b)"); for (int i = 0; i < mvPrimaryKeys.size(); i++) { - createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY (a, b, c))"); + createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY ((a, b), c))"); execute("USE " + keyspace()); executeNet(version, "USE " + keyspace()); @@ -106,32 +56,27 @@ public class ViewFilteringClustering2Test extends CQLTester execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 1, 0); execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 0, 0, 0); execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 0, 1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, -1, 0); execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 0, 0); execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 1, 0); logger.info("Testing MV primary key: {}", mvPrimaryKeys.get(i)); - // only accept rows where b = 1 - createView("mv_test" + i, "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE a IS NOT NULL AND (b, c) >= (1, 0) PRIMARY KEY " + mvPrimaryKeys.get(i)); + // only accept rows where a = 1 and b = 1 + createView("mv_test" + i, "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE a = 1 AND b = 1 AND c IS NOT NULL PRIMARY KEY " + mvPrimaryKeys.get(i)); while (!SystemKeyspace.isViewBuilt(keyspace(), "mv_test" + i)) Thread.sleep(10); assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 1, 0, 0), - row(0, 1, 1, 0), row(1, 1, 0, 0), row(1, 1, 1, 0) ); // insert new rows that do not match the filter - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, -1, 0, 0); execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 0, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 1, -1, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 1, 0, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 2, 0, 0); assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 1, 0, 0), - row(0, 1, 1, 0), row(1, 1, 0, 0), row(1, 1, 1, 0) ); @@ -139,20 +84,16 @@ public class ViewFilteringClustering2Test extends CQLTester // insert new row that does match the filter execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 2, 0); assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 1, 0, 0), - row(0, 1, 1, 0), row(1, 1, 0, 0), row(1, 1, 1, 0), row(1, 1, 2, 0) ); // update rows that don't match the filter - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 1, -1, 0); - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 2, -1, 0); - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 2, 0, 0); + execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 0, 0, 0); + execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 1, 0, 0); + execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 0, 1, 0); assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 1, 0, 0), - row(0, 1, 1, 0), row(1, 1, 0, 0), row(1, 1, 1, 0), row(1, 1, 2, 0) @@ -161,21 +102,17 @@ public class ViewFilteringClustering2Test extends CQLTester // update a row that does match the filter execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 1, 1, 0); assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 1, 0, 0), - row(0, 1, 1, 0), row(1, 1, 0, 1), row(1, 1, 1, 0), row(1, 1, 2, 0) ); // delete rows that don't match the filter - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 1, 1, -1); - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 2, -1, 0); - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 2, 0, 0); + execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 0, 0, 0); + execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 1, 0, 0); + execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 0, 1, 0); execute("DELETE FROM %s WHERE a = ? AND b = ?", 0, 0); assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 1, 0, 0), - row(0, 1, 1, 0), row(1, 1, 0, 1), row(1, 1, 1, 0), row(1, 1, 2, 0) @@ -184,26 +121,103 @@ public class ViewFilteringClustering2Test extends CQLTester // delete a row that does match the filter execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 1, 1, 0); assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 1, 0, 0), - row(0, 1, 1, 0), row(1, 1, 1, 0), row(1, 1, 2, 0) ); // delete a partition that matches the filter - execute("DELETE FROM %s WHERE a = ?", 1); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 1, 0, 0), - row(0, 1, 1, 0) - ); - - dropView("mv_test" + i); - dropTable("DROP TABLE %s"); + execute("DELETE FROM %s WHERE a = ? AND b = ?", 1, 1); + assertEmpty(execute("SELECT * FROM mv_test" + i)); } } @Test - public void testClusteringKeyFilteringRestrictions() throws Throwable + public void testCompoundPartitionKeyRestrictionsNotIncludeAll() throws Throwable + { + createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY ((a, b), c))"); + execute("USE " + keyspace()); + executeNet(version, "USE " + keyspace()); + + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 0, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 1, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 0, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 1, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 0, 0, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 0, 1, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 0, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 1, 0); + + // only accept rows where a = 1 and b = 1, don't include column d in the selection + createView("mv_test", "CREATE MATERIALIZED VIEW %s AS SELECT a, b, c FROM %%s WHERE a = 1 AND b = 1 AND c IS NOT NULL PRIMARY KEY ((a, b), c)"); + + while (!SystemKeyspace.isViewBuilt(keyspace(), "mv_test")) + Thread.sleep(10); + + assertRows(execute("SELECT * FROM mv_test"), + row(1, 1, 0), + row(1, 1, 1) + ); + + // insert new rows that do not match the filter + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 0, 0, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 1, 0, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 2, 0, 0); + assertRows(execute("SELECT * FROM mv_test"), + row(1, 1, 0), + row(1, 1, 1) + ); + + // insert new row that does match the filter + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 2, 0); + assertRows(execute("SELECT * FROM mv_test"), + row(1, 1, 0), + row(1, 1, 1), + row(1, 1, 2) + ); + + // update rows that don't match the filter + execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 0, 0, 0); + execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 1, 0, 0); + execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 0, 1, 0); + assertRows(execute("SELECT * FROM mv_test"), + row(1, 1, 0), + row(1, 1, 1), + row(1, 1, 2) + ); + + // update a row that does match the filter + execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 1, 1, 0); + assertRows(execute("SELECT * FROM mv_test"), + row(1, 1, 0), + row(1, 1, 1), + row(1, 1, 2) + ); + + // delete rows that don't match the filter + execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 0, 0, 0); + execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 1, 0, 0); + execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 0, 1, 0); + execute("DELETE FROM %s WHERE a = ? AND b = ?", 0, 0); + assertRows(execute("SELECT * FROM mv_test"), + row(1, 1, 0), + row(1, 1, 1), + row(1, 1, 2) + ); + + // delete a row that does match the filter + execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 1, 1, 0); + assertRows(execute("SELECT * FROM mv_test"), + row(1, 1, 1), + row(1, 1, 2) + ); + + // delete a partition that matches the filter + execute("DELETE FROM %s WHERE a = ? AND b = ?", 1, 1); + assertEmpty(execute("SELECT * FROM mv_test")); + } + + @Test + public void testPartitionKeyAndClusteringKeyFilteringRestrictions() throws Throwable { List<String> mvPrimaryKeys = Arrays.asList("((a, b), c)", "((b, a), c)", "(a, b, c)", "(c, b, a)", "((c, a), b)"); for (int i = 0; i < mvPrimaryKeys.size(); i++) @@ -226,24 +240,20 @@ public class ViewFilteringClustering2Test extends CQLTester logger.info("Testing MV primary key: {}", mvPrimaryKeys.get(i)); // only accept rows where b = 1 - createView("mv_test" + i, "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE a IS NOT NULL AND b IS NOT NULL AND c = 1 PRIMARY KEY " + mvPrimaryKeys.get(i)); + createView("mv_test" + i, "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE a = 1 AND b IS NOT NULL AND c = 1 PRIMARY KEY " + mvPrimaryKeys.get(i)); while (!SystemKeyspace.isViewBuilt(keyspace(), "mv_test" + i)) Thread.sleep(10); assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 0, 1, 0), - row(0, 1, 1, 0), row(1, 0, 1, 0), row(1, 1, 1, 0) ); // insert new rows that do not match the filter - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 0, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 1, -1, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 1, 0); + execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 0, 0); assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 0, 1, 0), - row(0, 1, 1, 0), row(1, 0, 1, 0), row(1, 1, 1, 0) ); @@ -251,8 +261,6 @@ public class ViewFilteringClustering2Test extends CQLTester // insert new row that does match the filter execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 2, 1, 0); assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 0, 1, 0), - row(0, 1, 1, 0), row(1, 0, 1, 0), row(1, 1, 1, 0), row(1, 2, 1, 0) @@ -260,10 +268,8 @@ public class ViewFilteringClustering2Test extends CQLTester // update rows that don't match the filter execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 1, -1, 0); - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 2, 0, 0); + execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 0, 1, 1, 0); assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 0, 1, 0), - row(0, 1, 1, 0), row(1, 0, 1, 0), row(1, 1, 1, 0), row(1, 2, 1, 0) @@ -272,8 +278,6 @@ public class ViewFilteringClustering2Test extends CQLTester // update a row that does match the filter execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 2, 1, 1, 1); assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 0, 1, 0), - row(0, 1, 1, 0), row(1, 0, 1, 0), row(1, 1, 1, 2), row(1, 2, 1, 0) @@ -281,12 +285,9 @@ public class ViewFilteringClustering2Test extends CQLTester // delete rows that don't match the filter execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 1, 1, -1); - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 2, -1, 0); - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 2, 0, 0); - execute("DELETE FROM %s WHERE a = ? AND b = ?", 0, -1); + execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 2, 0, 1); + execute("DELETE FROM %s WHERE a = ?", 0); assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 0, 1, 0), - row(0, 1, 1, 0), row(1, 0, 1, 0), row(1, 1, 1, 2), row(1, 2, 1, 0) @@ -295,32 +296,13 @@ public class ViewFilteringClustering2Test extends CQLTester // delete a row that does match the filter execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 1, 1, 1); assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 0, 1, 0), - row(0, 1, 1, 0), row(1, 0, 1, 0), row(1, 2, 1, 0) ); // delete a partition that matches the filter execute("DELETE FROM %s WHERE a = ?", 1); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 0, 1, 0), - row(0, 1, 1, 0) - ); - - // insert a partition with one matching and one non-matching row using a batch (CASSANDRA-10614) - String tableName = KEYSPACE + "." + currentTable(); - execute("BEGIN BATCH " + - "INSERT INTO " + tableName + " (a, b, c, d) VALUES (?, ?, ?, ?); " + - "INSERT INTO " + tableName + " (a, b, c, d) VALUES (?, ?, ?, ?); " + - "APPLY BATCH", - 4, 4, 0, 0, - 4, 4, 1, 1); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(0, 0, 1, 0), - row(0, 1, 1, 0), - row(4, 4, 1, 1) - ); + assertEmpty(execute("SELECT a, b, c, d FROM mv_test" + i)); dropView("mv_test" + i); dropTable("DROP TABLE %s"); diff --git a/test/unit/org/apache/cassandra/cql3/ViewFilteringPKTest.java b/test/unit/org/apache/cassandra/cql3/ViewFilteringSimplePKTest.java similarity index 52% rename from test/unit/org/apache/cassandra/cql3/ViewFilteringPKTest.java rename to test/unit/org/apache/cassandra/cql3/ViewFilteringSimplePKTest.java index 09d220d..d3b3e4a 100644 --- a/test/unit/org/apache/cassandra/cql3/ViewFilteringPKTest.java +++ b/test/unit/org/apache/cassandra/cql3/ViewFilteringSimplePKTest.java @@ -18,77 +18,27 @@ package org.apache.cassandra.cql3; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.List; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.apache.cassandra.db.SystemKeyspace; -import org.apache.cassandra.transport.ProtocolVersion; -/* ViewFilteringTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670) +/* ViewFilteringTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670, CASSANDRA-17167) * Any changes here check if they apply to the other classes * - ViewFilteringPKTest * - ViewFilteringClustering1Test * - ViewFilteringClustering2Test * - ViewFilteringTest + * - ... + * - ViewFiltering*Test */ @RunWith(Parameterized.class) -public class ViewFilteringPKTest extends CQLTester +public class ViewFilteringSimplePKTest extends ViewFilteringTester { - @Parameterized.Parameter - public ProtocolVersion version; - - @Parameterized.Parameters() - public static Collection<Object[]> versions() - { - return ViewFilteringTest.versions(); - } - - private final List<String> views = new ArrayList<>(); - - @BeforeClass - public static void startup() - { - ViewFilteringTest.startup(); - } - - @AfterClass - public static void tearDown() - { - ViewFilteringTest.tearDown(); - } - - @Before - public void begin() - { - ViewFilteringTest.beginSetup(views); - } - - @After - public void end() throws Throwable - { - ViewFilteringTest.endSetup(views, version, this); - } - - private void createView(String name, String query) throws Throwable - { - ViewFilteringTest.createView(name, query, views, version, this); - } - - private void dropView(String name) throws Throwable - { - ViewFilteringTest.dropView(name, views, version, this); - } - @Test public void testPartitionKeyFilteringUnrestrictedPart() throws Throwable { @@ -384,274 +334,4 @@ public class ViewFilteringPKTest extends CQLTester assertEmpty(execute("SELECT * FROM mv_test" + i)); } } - - @Test - public void testCompoundPartitionKeyRestrictions() throws Throwable - { - List<String> mvPrimaryKeys = Arrays.asList("((a, b), c)", "((b, a), c)", "(a, b, c)", "(c, b, a)", "((c, a), b)"); - for (int i = 0; i < mvPrimaryKeys.size(); i++) - { - createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY ((a, b), c))"); - - execute("USE " + keyspace()); - executeNet(version, "USE " + keyspace()); - - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 0, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 0, 1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 1, 0); - - logger.info("Testing MV primary key: {}", mvPrimaryKeys.get(i)); - - // only accept rows where a = 1 and b = 1 - createView("mv_test" + i, "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE a = 1 AND b = 1 AND c IS NOT NULL PRIMARY KEY " + mvPrimaryKeys.get(i)); - - while (!SystemKeyspace.isViewBuilt(keyspace(), "mv_test" + i)) - Thread.sleep(10); - - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(1, 1, 0, 0), - row(1, 1, 1, 0) - ); - - // insert new rows that do not match the filter - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 0, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 1, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 2, 0, 0); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(1, 1, 0, 0), - row(1, 1, 1, 0) - ); - - // insert new row that does match the filter - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 2, 0); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(1, 1, 0, 0), - row(1, 1, 1, 0), - row(1, 1, 2, 0) - ); - - // update rows that don't match the filter - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 0, 0, 0); - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 1, 0, 0); - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 0, 1, 0); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(1, 1, 0, 0), - row(1, 1, 1, 0), - row(1, 1, 2, 0) - ); - - // update a row that does match the filter - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 1, 1, 0); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(1, 1, 0, 1), - row(1, 1, 1, 0), - row(1, 1, 2, 0) - ); - - // delete rows that don't match the filter - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 0, 0, 0); - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 1, 0, 0); - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 0, 1, 0); - execute("DELETE FROM %s WHERE a = ? AND b = ?", 0, 0); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(1, 1, 0, 1), - row(1, 1, 1, 0), - row(1, 1, 2, 0) - ); - - // delete a row that does match the filter - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 1, 1, 0); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(1, 1, 1, 0), - row(1, 1, 2, 0) - ); - - // delete a partition that matches the filter - execute("DELETE FROM %s WHERE a = ? AND b = ?", 1, 1); - assertEmpty(execute("SELECT * FROM mv_test" + i)); - } - } - - @Test - public void testCompoundPartitionKeyRestrictionsNotIncludeAll() throws Throwable - { - createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY ((a, b), c))"); - execute("USE " + keyspace()); - executeNet(version, "USE " + keyspace()); - - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 0, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 0, 1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 1, 0); - - // only accept rows where a = 1 and b = 1, don't include column d in the selection - createView("mv_test", "CREATE MATERIALIZED VIEW %s AS SELECT a, b, c FROM %%s WHERE a = 1 AND b = 1 AND c IS NOT NULL PRIMARY KEY ((a, b), c)"); - - while (!SystemKeyspace.isViewBuilt(keyspace(), "mv_test")) - Thread.sleep(10); - - assertRows(execute("SELECT * FROM mv_test"), - row(1, 1, 0), - row(1, 1, 1) - ); - - // insert new rows that do not match the filter - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 0, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 1, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 2, 0, 0); - assertRows(execute("SELECT * FROM mv_test"), - row(1, 1, 0), - row(1, 1, 1) - ); - - // insert new row that does match the filter - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 2, 0); - assertRows(execute("SELECT * FROM mv_test"), - row(1, 1, 0), - row(1, 1, 1), - row(1, 1, 2) - ); - - // update rows that don't match the filter - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 0, 0, 0); - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 1, 0, 0); - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 0, 1, 0); - assertRows(execute("SELECT * FROM mv_test"), - row(1, 1, 0), - row(1, 1, 1), - row(1, 1, 2) - ); - - // update a row that does match the filter - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 1, 1, 0); - assertRows(execute("SELECT * FROM mv_test"), - row(1, 1, 0), - row(1, 1, 1), - row(1, 1, 2) - ); - - // delete rows that don't match the filter - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 0, 0, 0); - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 1, 0, 0); - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 0, 1, 0); - execute("DELETE FROM %s WHERE a = ? AND b = ?", 0, 0); - assertRows(execute("SELECT * FROM mv_test"), - row(1, 1, 0), - row(1, 1, 1), - row(1, 1, 2) - ); - - // delete a row that does match the filter - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 1, 1, 0); - assertRows(execute("SELECT * FROM mv_test"), - row(1, 1, 1), - row(1, 1, 2) - ); - - // delete a partition that matches the filter - execute("DELETE FROM %s WHERE a = ? AND b = ?", 1, 1); - assertEmpty(execute("SELECT * FROM mv_test")); - } - - @Test - public void testPartitionKeyAndClusteringKeyFilteringRestrictions() throws Throwable - { - List<String> mvPrimaryKeys = Arrays.asList("((a, b), c)", "((b, a), c)", "(a, b, c)", "(c, b, a)", "((c, a), b)"); - for (int i = 0; i < mvPrimaryKeys.size(); i++) - { - createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY (a, b, c))"); - - execute("USE " + keyspace()); - executeNet(version, "USE " + keyspace()); - - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 0, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 0, 1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, -1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 1, 0); - - logger.info("Testing MV primary key: {}", mvPrimaryKeys.get(i)); - - // only accept rows where b = 1 - createView("mv_test" + i, "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE a = 1 AND b IS NOT NULL AND c = 1 PRIMARY KEY " + mvPrimaryKeys.get(i)); - - while (!SystemKeyspace.isViewBuilt(keyspace(), "mv_test" + i)) - Thread.sleep(10); - - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(1, 0, 1, 0), - row(1, 1, 1, 0) - ); - - // insert new rows that do not match the filter - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 0, 0); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(1, 0, 1, 0), - row(1, 1, 1, 0) - ); - - // insert new row that does match the filter - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 2, 1, 0); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(1, 0, 1, 0), - row(1, 1, 1, 0), - row(1, 2, 1, 0) - ); - - // update rows that don't match the filter - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 1, 1, -1, 0); - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 0, 1, 1, 0); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(1, 0, 1, 0), - row(1, 1, 1, 0), - row(1, 2, 1, 0) - ); - - // update a row that does match the filter - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ? AND c = ?", 2, 1, 1, 1); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(1, 0, 1, 0), - row(1, 1, 1, 2), - row(1, 2, 1, 0) - ); - - // delete rows that don't match the filter - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 1, 1, -1); - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 2, 0, 1); - execute("DELETE FROM %s WHERE a = ?", 0); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(1, 0, 1, 0), - row(1, 1, 1, 2), - row(1, 2, 1, 0) - ); - - // delete a row that does match the filter - execute("DELETE FROM %s WHERE a = ? AND b = ? AND c = ?", 1, 1, 1); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test" + i), - row(1, 0, 1, 0), - row(1, 2, 1, 0) - ); - - // delete a partition that matches the filter - execute("DELETE FROM %s WHERE a = ?", 1); - assertEmpty(execute("SELECT a, b, c, d FROM mv_test" + i)); - - dropView("mv_test" + i); - dropTable("DROP TABLE %s"); - } - } } diff --git a/test/unit/org/apache/cassandra/cql3/ViewFilteringTest.java b/test/unit/org/apache/cassandra/cql3/ViewFilteringTest.java index 374f79f..ed2d140 100644 --- a/test/unit/org/apache/cassandra/cql3/ViewFilteringTest.java +++ b/test/unit/org/apache/cassandra/cql3/ViewFilteringTest.java @@ -18,137 +18,32 @@ package org.apache.cassandra.cql3; -import java.util.*; -import java.util.stream.Collectors; +import java.util.Arrays; +import java.util.List; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; +import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; - -import com.datastax.driver.core.exceptions.InvalidQueryException; -import org.junit.Assert; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import com.datastax.driver.core.exceptions.OperationTimedOutException; -import org.apache.cassandra.concurrent.SEPExecutor; -import org.apache.cassandra.concurrent.Stage; +import com.datastax.driver.core.exceptions.InvalidQueryException; import org.apache.cassandra.db.Keyspace; import org.apache.cassandra.db.SystemKeyspace; -import org.apache.cassandra.transport.ProtocolVersion; import org.apache.cassandra.utils.FBUtilities; -/* ViewFilteringTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670) +/* ViewFilteringTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670, CASSANDRA-17167) * Any changes here check if they apply to the other classes * - ViewFilteringPKTest * - ViewFilteringClustering1Test * - ViewFilteringClustering2Test * - ViewFilteringTest + * - ... + * - ViewFiltering*Test */ @RunWith(Parameterized.class) -public class ViewFilteringTest extends CQLTester +public class ViewFilteringTest extends ViewFilteringTester { - @Parameterized.Parameter - public ProtocolVersion version; - - @Parameterized.Parameters() - public static Collection<Object[]> versions() - { - return ProtocolVersion.SUPPORTED.stream() - .map(v -> new Object[]{v}) - .collect(Collectors.toList()); - } - - private final List<String> views = new ArrayList<>(); - - @BeforeClass - public static void startup() - { - requireNetwork(); - System.setProperty("cassandra.mv.allow_filtering_nonkey_columns_unsafe", "true"); - } - - @AfterClass - public static void tearDown() - { - System.setProperty("cassandra.mv.allow_filtering_nonkey_columns_unsafe", "false"); - } - - @Before - public void begin() - { - beginSetup(views); - } - - public static void beginSetup(List<String> views) - { - views.clear(); - } - - @After - public void end() throws Throwable - { - endSetup(views, version, this); - } - - public static void endSetup(List<String> views, ProtocolVersion version, CQLTester tester) throws Throwable - { - for (String viewName : views) - tester.executeNet(version, "DROP MATERIALIZED VIEW " + viewName); - } - - private void createView(String name, String query) throws Throwable - { - createView(name, query, views, version, this); - } - - public static void createView(String name, String query, List<String> views, ProtocolVersion version, CQLTester tester) throws Throwable - { - try - { - tester.executeNet(version, String.format(query, name)); - // If exception is thrown, the view will not be added to the list; since it shouldn't have been created, this is - // the desired behavior - views.add(name); - } - catch (OperationTimedOutException ex) - { - // ... except for timeout, when we actually do not know whether the view was created or not - views.add(name); - throw ex; - } - } - - private void updateView(String query, Object... params) throws Throwable - { - executeNet(version, query, params); - while (!(((SEPExecutor) Stage.VIEW_MUTATION.executor()).getPendingTaskCount() == 0 - && ((SEPExecutor) Stage.VIEW_MUTATION.executor()).getActiveTaskCount() == 0)) - { - Thread.sleep(1); - } - } - - private void dropView(String name) throws Throwable - { - dropView(name, views, version, this); - } - - public static void dropView(String name, List<String> views, ProtocolVersion version, CQLTester tester) throws Throwable - { - tester.executeNet(version, "DROP MATERIALIZED VIEW " + name); - views.remove(name); - } - - public static void waitForView(String keyspace, String view) throws InterruptedException - { - while (!SystemKeyspace.isViewBuilt(keyspace, view)) - Thread.sleep(10); - } - // TODO will revise the non-pk filter condition in MV, see CASSANDRA-11500 @Ignore @Test @@ -631,415 +526,4 @@ public class ViewFilteringTest extends CQLTester row(1, 1, 3) ); } - - @Test - public void testAllTypes() throws Throwable - { - String myType = createType("CREATE TYPE %s (a int, b uuid, c set<text>)"); - String columnNames = "asciival, " + - "bigintval, " + - "blobval, " + - "booleanval, " + - "dateval, " + - "decimalval, " + - "doubleval, " + - "floatval, " + - "inetval, " + - "intval, " + - "textval, " + - "timeval, " + - "timestampval, " + - "timeuuidval, " + - "uuidval," + - "varcharval, " + - "varintval, " + - "frozenlistval, " + - "frozensetval, " + - "frozenmapval, " + - "tupleval, " + - "udtval"; - - createTable( - "CREATE TABLE %s (" + - "asciival ascii, " + - "bigintval bigint, " + - "blobval blob, " + - "booleanval boolean, " + - "dateval date, " + - "decimalval decimal, " + - "doubleval double, " + - "floatval float, " + - "inetval inet, " + - "intval int, " + - "textval text, " + - "timeval time, " + - "timestampval timestamp, " + - "timeuuidval timeuuid, " + - "uuidval uuid," + - "varcharval varchar, " + - "varintval varint, " + - "frozenlistval frozen<list<int>>, " + - "frozensetval frozen<set<uuid>>, " + - "frozenmapval frozen<map<ascii, int>>," + - "tupleval frozen<tuple<int, ascii, uuid>>," + - "udtval frozen<" + myType + ">, " + - "PRIMARY KEY (" + columnNames + "))"); - - execute("USE " + keyspace()); - executeNet(version, "USE " + keyspace()); - - createView( - "mv_test", - "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE " + - "asciival = 'abc' AND " + - "bigintval = 123 AND " + - "blobval = 0xfeed AND " + - "booleanval = true AND " + - "dateval = '1987-03-23' AND " + - "decimalval = 123.123 AND " + - "doubleval = 123.123 AND " + - "floatval = 123.123 AND " + - "inetval = '127.0.0.1' AND " + - "intval = 123 AND " + - "textval = 'abc' AND " + - "timeval = '07:35:07.000111222' AND " + - "timestampval = 123123123 AND " + - "timeuuidval = 6BDDC89A-5644-11E4-97FC-56847AFE9799 AND " + - "uuidval = 6BDDC89A-5644-11E4-97FC-56847AFE9799 AND " + - "varcharval = 'abc' AND " + - "varintval = 123123123 AND " + - "frozenlistval = [1, 2, 3] AND " + - "frozensetval = {6BDDC89A-5644-11E4-97FC-56847AFE9799} AND " + - "frozenmapval = {'a': 1, 'b': 2} AND " + - "tupleval = (1, 'foobar', 6BDDC89A-5644-11E4-97FC-56847AFE9799) AND " + - "udtval = {a: 1, b: 6BDDC89A-5644-11E4-97FC-56847AFE9799, c: {'foo', 'bar'}} " + - "PRIMARY KEY (" + columnNames + ")"); - - execute("INSERT INTO %s (" + columnNames + ") VALUES (" + - "'abc'," + - "123," + - "0xfeed," + - "true," + - "'1987-03-23'," + - "123.123," + - "123.123," + - "123.123," + - "'127.0.0.1'," + - "123," + - "'abc'," + - "'07:35:07.000111222'," + - "123123123," + - "6BDDC89A-5644-11E4-97FC-56847AFE9799," + - "6BDDC89A-5644-11E4-97FC-56847AFE9799," + - "'abc'," + - "123123123," + - "[1, 2, 3]," + - "{6BDDC89A-5644-11E4-97FC-56847AFE9799}," + - "{'a': 1, 'b': 2}," + - "(1, 'foobar', 6BDDC89A-5644-11E4-97FC-56847AFE9799)," + - "{a: 1, b: 6BDDC89A-5644-11E4-97FC-56847AFE9799, c: {'foo', 'bar'}})"); - - assert !execute("SELECT * FROM mv_test").isEmpty(); - - executeNet(version, "ALTER TABLE %s RENAME inetval TO foo"); - assert !execute("SELECT * FROM mv_test").isEmpty(); - } - - @Test - public void testMVCreationWithNonPrimaryRestrictions() throws Throwable - { - createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY (a, b))"); - - execute("USE " + keyspace()); - executeNet(version, "USE " + keyspace()); - - try { - createView("mv_test", "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL AND d = 1 PRIMARY KEY (a, b, c)"); - dropView("mv_test"); - } catch(Exception e) { - throw new RuntimeException("MV creation with non primary column restrictions failed.", e); - } - - dropTable("DROP TABLE %s"); - } - - @Test - public void testNonPrimaryRestrictions() throws Throwable - { - createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY (a, b))"); - - execute("USE " + keyspace()); - executeNet(version, "USE " + keyspace()); - - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 0, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 0, 1, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 1, 1, 0); - - // only accept rows where c = 1 - createView("mv_test", "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL AND c = 1 PRIMARY KEY (a, b, c)"); - - while (!SystemKeyspace.isViewBuilt(keyspace(), "mv_test")) - Thread.sleep(10); - - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), - row(0, 0, 1, 0), - row(0, 1, 1, 0), - row(1, 0, 1, 0), - row(1, 1, 1, 0) - ); - - // insert new rows that do not match the filter - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 0, 0, 0); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 2, 1, 2, 0); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), - row(0, 0, 1, 0), - row(0, 1, 1, 0), - row(1, 0, 1, 0), - row(1, 1, 1, 0) - ); - - // insert new row that does match the filter - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 1, 2, 1, 0); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), - row(0, 0, 1, 0), - row(0, 1, 1, 0), - row(1, 0, 1, 0), - row(1, 1, 1, 0), - row(1, 2, 1, 0) - ); - - // update rows that don't match the filter - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ?", 2, 2, 0); - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ?", 1, 2, 1); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), - row(0, 0, 1, 0), - row(0, 1, 1, 0), - row(1, 0, 1, 0), - row(1, 1, 1, 0), - row(1, 2, 1, 0) - ); - - // update a row that does match the filter - execute("UPDATE %s SET d = ? WHERE a = ? AND b = ?", 1, 1, 0); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), - row(0, 0, 1, 0), - row(0, 1, 1, 0), - row(1, 0, 1, 1), - row(1, 1, 1, 0), - row(1, 2, 1, 0) - ); - - // delete rows that don't match the filter - execute("DELETE FROM %s WHERE a = ? AND b = ?", 2, 0); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), - row(0, 0, 1, 0), - row(0, 1, 1, 0), - row(1, 0, 1, 1), - row(1, 1, 1, 0), - row(1, 2, 1, 0) - ); - - // delete a row that does match the filter - execute("DELETE FROM %s WHERE a = ? AND b = ?", 1, 2); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), - row(0, 0, 1, 0), - row(0, 1, 1, 0), - row(1, 0, 1, 1), - row(1, 1, 1, 0) - ); - - // delete a partition that matches the filter - execute("DELETE FROM %s WHERE a = ?", 1); - assertRowsIgnoringOrder(execute("SELECT a, b, c, d FROM mv_test"), - row(0, 0, 1, 0), - row(0, 1, 1, 0) - ); - - dropView("mv_test"); - dropTable("DROP TABLE %s"); - } - - @Test - public void complexRestrictedTimestampUpdateTestWithFlush() throws Throwable - { - complexRestrictedTimestampUpdateTest(true); - } - - @Test - public void complexRestrictedTimestampUpdateTestWithoutFlush() throws Throwable - { - complexRestrictedTimestampUpdateTest(false); - } - - public void complexRestrictedTimestampUpdateTest(boolean flush) throws Throwable - { - createTable("CREATE TABLE %s (a int, b int, c int, d int, e int, PRIMARY KEY (a, b))"); - - execute("USE " + keyspace()); - executeNet(version, "USE " + keyspace()); - Keyspace ks = Keyspace.open(keyspace()); - - createView("mv", "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL AND c = 1 PRIMARY KEY (c, a, b)"); - ks.getColumnFamilyStore("mv").disableAutoCompaction(); - - //Set initial values TS=0, matching the restriction and verify view - executeNet(version, "INSERT INTO %s (a, b, c, d) VALUES (0, 0, 1, 0) USING TIMESTAMP 0"); - assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(0)); - - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - - //update c's timestamp TS=2 - executeNet(version, "UPDATE %s USING TIMESTAMP 2 SET c = ? WHERE a = ? and b = ? ", 1, 0, 0); - assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(0)); - - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - - //change c's value and TS=3, tombstones c=1 and adds c=0 record - executeNet(version, "UPDATE %s USING TIMESTAMP 3 SET c = ? WHERE a = ? and b = ? ", 0, 0, 0); - assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 0, 0, 0)); - - if(flush) - { - ks.getColumnFamilyStore("mv").forceMajorCompaction(); - FBUtilities.waitOnFutures(ks.flush()); - } - - //change c's value back to 1 with TS=4, check we can see d - executeNet(version, "UPDATE %s USING TIMESTAMP 4 SET c = ? WHERE a = ? and b = ? ", 1, 0, 0); - if (flush) - { - ks.getColumnFamilyStore("mv").forceMajorCompaction(); - FBUtilities.waitOnFutures(ks.flush()); - } - - assertRows(execute("SELECT d, e from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(0, null)); - - //Add e value @ TS=1 - executeNet(version, "UPDATE %s USING TIMESTAMP 1 SET e = ? WHERE a = ? and b = ? ", 1, 0, 0); - assertRows(execute("SELECT d, e from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(0, 1)); - - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - - //Change d value @ TS=2 - executeNet(version, "UPDATE %s USING TIMESTAMP 2 SET d = ? WHERE a = ? and b = ? ", 2, 0, 0); - assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(2)); - - if (flush) - FBUtilities.waitOnFutures(ks.flush()); - - //Change d value @ TS=3 - executeNet(version, "UPDATE %s USING TIMESTAMP 3 SET d = ? WHERE a = ? and b = ? ", 1, 0, 0); - assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(1)); - - //Tombstone c - executeNet(version, "DELETE FROM %s WHERE a = ? and b = ?", 0, 0); - assertRowsIgnoringOrder(execute("SELECT d from mv")); - assertRows(execute("SELECT d from mv")); - - //Add back without D - executeNet(version, "INSERT INTO %s (a, b, c) VALUES (0, 0, 1)"); - - //Make sure D doesn't pop back in. - assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row((Object) null)); - - //New partition - // insert a row with timestamp 0 - executeNet(version, "INSERT INTO %s (a, b, c, d, e) VALUES (?, ?, ?, ?, ?) USING TIMESTAMP 0", 1, 0, 1, 0, 0); - - // overwrite pk and e with timestamp 1, but don't overwrite d - executeNet(version, "INSERT INTO %s (a, b, c, e) VALUES (?, ?, ?, ?) USING TIMESTAMP 1", 1, 0, 1, 0); - - // delete with timestamp 0 (which should only delete d) - executeNet(version, "DELETE FROM %s USING TIMESTAMP 0 WHERE a = ? AND b = ?", 1, 0); - assertRows(execute("SELECT a, b, c, d, e from mv WHERE c = ? and a = ? and b = ?", 1, 1, 0), - row(1, 0, 1, null, 0) - ); - - executeNet(version, "UPDATE %s USING TIMESTAMP 2 SET c = ? WHERE a = ? AND b = ?", 1, 1, 1); - executeNet(version, "UPDATE %s USING TIMESTAMP 3 SET c = ? WHERE a = ? AND b = ?", 1, 1, 0); - assertRows(execute("SELECT a, b, c, d, e from mv WHERE c = ? and a = ? and b = ?", 1, 1, 0), - row(1, 0, 1, null, 0) - ); - - executeNet(version, "UPDATE %s USING TIMESTAMP 3 SET d = ? WHERE a = ? AND b = ?", 0, 1, 0); - assertRows(execute("SELECT a, b, c, d, e from mv WHERE c = ? and a = ? and b = ?", 1, 1, 0), - row(1, 0, 1, 0, 0) - ); - } - - @Test - public void testRestrictedRegularColumnTimestampUpdates() throws Throwable - { - // Regression test for CASSANDRA-10910 - - createTable("CREATE TABLE %s (" + - "k int PRIMARY KEY, " + - "c int, " + - "val int)"); - - execute("USE " + keyspace()); - executeNet(version, "USE " + keyspace()); - - createView("mv_rctstest", "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE k IS NOT NULL AND c IS NOT NULL AND c = 1 PRIMARY KEY (k,c)"); - - updateView("UPDATE %s SET c = ?, val = ? WHERE k = ?", 0, 0, 0); - updateView("UPDATE %s SET val = ? WHERE k = ?", 1, 0); - updateView("UPDATE %s SET c = ? WHERE k = ?", 1, 0); - assertRows(execute("SELECT c, k, val FROM mv_rctstest"), row(1, 0, 1)); - - updateView("TRUNCATE %s"); - - updateView("UPDATE %s USING TIMESTAMP 1 SET c = ?, val = ? WHERE k = ?", 0, 0, 0); - updateView("UPDATE %s USING TIMESTAMP 3 SET c = ? WHERE k = ?", 1, 0); - updateView("UPDATE %s USING TIMESTAMP 2 SET val = ? WHERE k = ?", 1, 0); - updateView("UPDATE %s USING TIMESTAMP 4 SET c = ? WHERE k = ?", 1, 0); - updateView("UPDATE %s USING TIMESTAMP 3 SET val = ? WHERE k = ?", 2, 0); - assertRows(execute("SELECT c, k, val FROM mv_rctstest"), row(1, 0, 2)); - } - - @Test - public void testOldTimestampsWithRestrictions() throws Throwable - { - createTable("CREATE TABLE %s (" + - "k int, " + - "c int, " + - "val text, " + "" + - "PRIMARY KEY(k, c))"); - - execute("USE " + keyspace()); - executeNet(version, "USE " + keyspace()); - - createView("mv_tstest", "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE val IS NOT NULL AND k IS NOT NULL AND c IS NOT NULL AND val = 'baz' PRIMARY KEY (val,k,c)"); - - for (int i = 0; i < 100; i++) - updateView("INSERT into %s (k,c,val)VALUES(?,?,?)", 0, i % 2, "baz"); - - Keyspace.open(keyspace()).getColumnFamilyStore(currentTable()).forceBlockingFlush(); - - Assert.assertEquals(2, execute("select * from %s").size()); - Assert.assertEquals(2, execute("select * from mv_tstest").size()); - - assertRows(execute("SELECT val from %s where k = 0 and c = 0"), row("baz")); - assertRows(execute("SELECT c from mv_tstest where k = 0 and val = ?", "baz"), row(0), row(1)); - - //Make sure an old TS does nothing - updateView("UPDATE %s USING TIMESTAMP 100 SET val = ? where k = ? AND c = ?", "bar", 0, 1); - assertRows(execute("SELECT val from %s where k = 0 and c = 1"), row("baz")); - assertRows(execute("SELECT c from mv_tstest where k = 0 and val = ?", "baz"), row(0), row(1)); - assertRows(execute("SELECT c from mv_tstest where k = 0 and val = ?", "bar")); - - //Latest TS - updateView("UPDATE %s SET val = ? where k = ? AND c = ?", "bar", 0, 1); - assertRows(execute("SELECT val from %s where k = 0 and c = 1"), row("bar")); - assertRows(execute("SELECT c from mv_tstest where k = 0 and val = ?", "bar")); - assertRows(execute("SELECT c from mv_tstest where k = 0 and val = ?", "baz"), row(0)); - } } diff --git a/test/unit/org/apache/cassandra/cql3/ViewComplexTester.java b/test/unit/org/apache/cassandra/cql3/ViewFilteringTester.java similarity index 79% copy from test/unit/org/apache/cassandra/cql3/ViewComplexTester.java copy to test/unit/org/apache/cassandra/cql3/ViewFilteringTester.java index a934649..782a241 100644 --- a/test/unit/org/apache/cassandra/cql3/ViewComplexTester.java +++ b/test/unit/org/apache/cassandra/cql3/ViewFilteringTester.java @@ -21,10 +21,10 @@ package org.apache.cassandra.cql3; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.runner.RunWith; @@ -32,7 +32,7 @@ import org.junit.runners.Parameterized; import com.datastax.driver.core.exceptions.OperationTimedOutException; import org.apache.cassandra.concurrent.Stage; -import org.apache.cassandra.db.Keyspace; +import org.apache.cassandra.db.SystemKeyspace; import org.apache.cassandra.transport.ProtocolVersion; /* ViewComplexTest class has been split into multiple ones because of timeout issues (CASSANDRA-16670) @@ -42,12 +42,12 @@ import org.apache.cassandra.transport.ProtocolVersion; * - ViewComplexTTLTest * - ViewComplexTest * - ViewComplexLivenessTest + * - ... + * - ViewComplex*Test */ @RunWith(Parameterized.class) -public abstract class ViewComplexTester extends CQLTester +public abstract class ViewFilteringTester extends CQLTester { - private static final AtomicInteger seqNumber = new AtomicInteger(); - @Parameterized.Parameter public ProtocolVersion version; @@ -65,10 +65,17 @@ public abstract class ViewComplexTester extends CQLTester public static void startup() { requireNetwork(); + System.setProperty("cassandra.mv.allow_filtering_nonkey_columns_unsafe", "true"); + } + + @AfterClass + public static void tearDown() + { + System.setProperty("cassandra.mv.allow_filtering_nonkey_columns_unsafe", "false"); } @Before - public void begin() throws Throwable + public void begin() { views.clear(); } @@ -76,19 +83,12 @@ public abstract class ViewComplexTester extends CQLTester @After public void end() throws Throwable { - dropMViews(); - } - - protected void dropMViews() throws Throwable - { for (String viewName : views) executeNet(version, "DROP MATERIALIZED VIEW " + viewName); } - protected String createView(String query) throws Throwable + protected void createView(String name, String query) throws Throwable { - String name = createViewName(); - try { executeNet(version, String.format(query, name)); @@ -102,29 +102,27 @@ public abstract class ViewComplexTester extends CQLTester views.add(name); throw ex; } - - return name; - } - - protected static String createViewName() - { - return "mv" + seqNumber.getAndIncrement(); } protected void updateView(String query, Object... params) throws Throwable { - updateViewWithFlush(query, false, params); - } - - protected void updateViewWithFlush(String query, boolean flush, Object... params) throws Throwable - { executeNet(version, query, params); while (!(Stage.VIEW_MUTATION.executor().getPendingTaskCount() == 0 && Stage.VIEW_MUTATION.executor().getActiveTaskCount() == 0)) { Thread.sleep(1); } - if (flush) - Keyspace.open(keyspace()).flush(); + } + + protected void dropView(String name) throws Throwable + { + executeNet(version, "DROP MATERIALIZED VIEW " + name); + views.remove(name); + } + + protected static void waitForView(String keyspace, String view) throws InterruptedException + { + while (!SystemKeyspace.isViewBuilt(keyspace, view)) + Thread.sleep(10); } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org For additional commands, e-mail: commits-h...@cassandra.apache.org