Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 9CDCF200BAD for ; Tue, 25 Oct 2016 17:14:29 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 9B7F6160AF3; Tue, 25 Oct 2016 15:14:29 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id BA5F2160AE6 for ; Tue, 25 Oct 2016 17:14:28 +0200 (CEST) Received: (qmail 38842 invoked by uid 500); 25 Oct 2016 15:14:22 -0000 Mailing-List: contact commits-help@cassandra.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cassandra.apache.org Delivered-To: mailing list commits@cassandra.apache.org Received: (qmail 38831 invoked by uid 99); 25 Oct 2016 15:14:22 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 25 Oct 2016 15:14:22 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A6C74DFE80; Tue, 25 Oct 2016 15:14:22 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: blerer@apache.org To: commits@cassandra.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: cassandra git commit: Use non-token restrictions for bounds when token restrictions are overridden Date: Tue, 25 Oct 2016 15:14:22 +0000 (UTC) archived-at: Tue, 25 Oct 2016 15:14:29 -0000 Repository: cassandra Updated Branches: refs/heads/cassandra-3.X 8644aa9b8 -> b98a40605 Use non-token restrictions for bounds when token restrictions are overridden patch by Alex Petrov; reviewed by Benjamin Lerer for CASSANDRA-12149 Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/b98a4060 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/b98a4060 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/b98a4060 Branch: refs/heads/cassandra-3.X Commit: b98a40605a4eeaf9347401b8f9ed9f0fe297c745 Parents: 8644aa9 Author: Alex Petrov Authored: Tue Oct 25 17:12:06 2016 +0200 Committer: Benjamin Lerer Committed: Tue Oct 25 17:12:06 2016 +0200 ---------------------------------------------------------------------- CHANGES.txt | 1 + .../cql3/restrictions/TokenFilter.java | 21 ++++---- .../SelectOrderedPartitionerTest.java | 50 ++++++++++++++++++++ 3 files changed, 63 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/b98a4060/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt index e6477d7..dea2003 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 3.10 + * Use non-token restrictions for bounds when token restrictions are overridden (CASSANDRA-12419) * Fix CQLSH auto completion for PER PARTITION LIMIT (CASSANDRA-12803) * Use different build directories for Eclipse and Ant (CASSANDRA-12466) * Avoid potential AttributeError in cqlsh due to no table metadata (CASSANDRA-12815) http://git-wip-us.apache.org/repos/asf/cassandra/blob/b98a4060/src/java/org/apache/cassandra/cql3/restrictions/TokenFilter.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cql3/restrictions/TokenFilter.java b/src/java/org/apache/cassandra/cql3/restrictions/TokenFilter.java index 2611d19..400706b 100644 --- a/src/java/org/apache/cassandra/cql3/restrictions/TokenFilter.java +++ b/src/java/org/apache/cassandra/cql3/restrictions/TokenFilter.java @@ -41,18 +41,21 @@ import static org.apache.cassandra.cql3.statements.Bound.START; /** * Restriction decorator used to merge non-token restriction and token restriction on partition keys. + * + *

If all partition key columns have non-token restrictions and do not need filtering, they take precedence + * when calculating bounds, incusiveness etc (see CASSANDRA-12149).

*/ final class TokenFilter implements PartitionKeyRestrictions { /** * The decorated restriction */ - private PartitionKeyRestrictions restrictions; + private final PartitionKeyRestrictions restrictions; /** * The restriction on the token */ - private TokenRestriction tokenRestriction; + private final TokenRestriction tokenRestriction; /** * Partitioner to manage tokens, extracted from tokenRestriction metadata. @@ -81,9 +84,9 @@ final class TokenFilter implements PartitionKeyRestrictions @Override public boolean isOnToken() { - // if all partition key columns have non-token restrictions, we can simply use the token range to filter - // those restrictions and then ignore the token range - return restrictions.size() < tokenRestriction.size(); + // if all partition key columns have non-token restrictions and do not need filtering, + // we can simply use the token range to filter those restrictions and then ignore the token range + return needFiltering(tokenRestriction.metadata) || restrictions.size() < tokenRestriction.size(); } public TokenFilter(PartitionKeyRestrictions restrictions, TokenRestriction tokenRestriction) @@ -111,19 +114,19 @@ final class TokenFilter implements PartitionKeyRestrictions @Override public boolean isInclusive(Bound bound) { - return tokenRestriction.isInclusive(bound); + return isOnToken() ? tokenRestriction.isInclusive(bound) : restrictions.isInclusive(bound); } @Override - public boolean hasBound(Bound b) + public boolean hasBound(Bound bound) { - return tokenRestriction.hasBound(b); + return isOnToken() ? tokenRestriction.hasBound(bound) : restrictions.hasBound(bound); } @Override public List bounds(Bound bound, QueryOptions options) throws InvalidRequestException { - return tokenRestriction.bounds(bound, options); + return isOnToken() ? tokenRestriction.bounds(bound, options) : restrictions.bounds(bound, options); } /** http://git-wip-us.apache.org/repos/asf/cassandra/blob/b98a4060/test/unit/org/apache/cassandra/cql3/validation/operations/SelectOrderedPartitionerTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/validation/operations/SelectOrderedPartitionerTest.java b/test/unit/org/apache/cassandra/cql3/validation/operations/SelectOrderedPartitionerTest.java index 0e3a342..3957305 100644 --- a/test/unit/org/apache/cassandra/cql3/validation/operations/SelectOrderedPartitionerTest.java +++ b/test/unit/org/apache/cassandra/cql3/validation/operations/SelectOrderedPartitionerTest.java @@ -46,9 +46,50 @@ public class SelectOrderedPartitionerTest extends CQLTester } @Test + public void testTokenAndIndex() throws Throwable + { + createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY (a, b, c))"); + createIndex("CREATE INDEX ON %s(c)"); + + for (int i = 0; i < 10; i++) + { + execute("INSERT INTO %s (a,b,c,d) VALUES (?, ?, ?, ?)", i, i, i, i); + execute("INSERT INTO %s (a,b,c,d) VALUES (?, ?, ?, ?)", i, i + 10, i + 10, i + 10); + } + + beforeAndAfterFlush(() -> { + assertRows(execute("SELECT * FROM %s WHERE token(a) > token(8) AND a = 9 AND c = 9 ALLOW FILTERING"), + row(9, 9, 9, 9)); + + assertRows(execute("SELECT * FROM %s WHERE token(a) > token(8) AND a > 8 AND c = 9 ALLOW FILTERING"), + row(9, 9, 9, 9)); + }); + } + + + @Test + public void testFilteringOnAllPartitionKeysWithTokenRestriction() throws Throwable + { + createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY ((a, b), c))"); + + for (int i = 0; i < 10; i++) + { + execute("INSERT INTO %s (a,b,c,d) VALUES (?, ?, ?, ?)", i, i, i, i); + execute("INSERT INTO %s (a,b,c,d) VALUES (?, ?, ?, ?)", i, i + 10, i + 10, i + 10); + } + + assertEmpty(execute("SELECT * FROM %s WHERE token(a, b) > token(10, 10)")); + assertEmpty(execute("SELECT * FROM %s WHERE token(a, b) > token(10, 10) AND a < 8 AND b < 8 ALLOW FILTERING")); + assertRows(execute("SELECT * FROM %s WHERE token(a, b) > token(5, 5) AND a < 8 AND b < 8 ALLOW FILTERING"), + row(6, 6, 6, 6), + row(7, 7, 7, 7)); + } + + @Test public void testFilteringOnPartitionKeyWithToken() throws Throwable { createTable("CREATE TABLE %s (a int, b int, c int, d int, PRIMARY KEY ((a, b), c))"); + createIndex("CREATE INDEX ON %s(d)"); for (int i = 0; i < 10; i++) { @@ -65,6 +106,15 @@ public class SelectOrderedPartitionerTest extends CQLTester row(9, 9, 9, 9), row(9, 19, 19, 19)); + assertRows(execute("SELECT * FROM %s WHERE token(a, b) > token(8, 10) AND a = 9 AND d = 9 ALLOW FILTERING"), + row(9, 9, 9, 9)); + + assertRows(execute("SELECT * FROM %s WHERE token(a, b) > token(8, 10) AND a > 8 AND b > 8 AND d = 9 ALLOW FILTERING"), + row(9, 9, 9, 9)); + + assertRows(execute("SELECT * FROM %s WHERE a = 9 AND b = 9 AND token(a, b) > token(8, 10) AND d = 9 ALLOW FILTERING"), + row(9, 9, 9, 9)); + assertRows(execute("SELECT * FROM %s WHERE token(a, b) > token(8, 10) AND a = 9 AND c = 19 ALLOW FILTERING"), row(9, 19, 19, 19));