Return-Path: X-Original-To: apmail-cassandra-commits-archive@www.apache.org Delivered-To: apmail-cassandra-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D0CBE19108 for ; Thu, 14 Apr 2016 10:38:32 +0000 (UTC) Received: (qmail 75177 invoked by uid 500); 14 Apr 2016 10:38:32 -0000 Delivered-To: apmail-cassandra-commits-archive@cassandra.apache.org Received: (qmail 75141 invoked by uid 500); 14 Apr 2016 10:38:32 -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 75130 invoked by uid 99); 14 Apr 2016 10:38:32 -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; Thu, 14 Apr 2016 10:38:32 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 7AA86DFE5F; Thu, 14 Apr 2016 10:38:32 +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 Date: Thu, 14 Apr 2016 10:38:32 -0000 Message-Id: <109be7b113e94817874e975a4a6f3052@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/4] cassandra git commit: Allow only DISTINCT queries with partition keys restrictions Repository: cassandra Updated Branches: refs/heads/trunk 9a0eb9a31 -> ccacf7d1a Allow only DISTINCT queries with partition keys restrictions patch by Alex Petrov; reviewed by Benjamin Lerer for CASSANDRA-11339 Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/69edeaa4 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/69edeaa4 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/69edeaa4 Branch: refs/heads/trunk Commit: 69edeaa46b78bb168f7e9d0b1c991c07b90f41ca Parents: 19b4b63 Author: Alex Petrov Authored: Thu Apr 14 12:26:52 2016 +0200 Committer: Benjamin Lerer Committed: Thu Apr 14 12:26:52 2016 +0200 ---------------------------------------------------------------------- CHANGES.txt | 1 + .../restrictions/StatementRestrictions.java | 9 ++++ .../cql3/statements/SelectStatement.java | 3 ++ .../cql3/validation/operations/SelectTest.java | 45 ++++++++++++++++++++ 4 files changed, 58 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/69edeaa4/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt index 54013a3..c72b6cb 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 2.2.6 + * Allow only DISTINCT queries with partition keys restrictions (CASSANDRA-11339) * CqlConfigHelper no longer requires both a keystore and truststore to work (CASSANDRA-11532) * Make deprecated repair methods backward-compatible with previous notification service (CASSANDRA-11430) * IncomingStreamingConnection version check message wrong (CASSANDRA-11462) http://git-wip-us.apache.org/repos/asf/cassandra/blob/69edeaa4/src/java/org/apache/cassandra/cql3/restrictions/StatementRestrictions.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cql3/restrictions/StatementRestrictions.java b/src/java/org/apache/cassandra/cql3/restrictions/StatementRestrictions.java index e0cf743..3934f33 100644 --- a/src/java/org/apache/cassandra/cql3/restrictions/StatementRestrictions.java +++ b/src/java/org/apache/cassandra/cql3/restrictions/StatementRestrictions.java @@ -279,6 +279,15 @@ public final class StatementRestrictions } /** + * Checks if the restrictions contain any non-primary key restrictions + * @return true if the restrictions contain any non-primary key restrictions, false otherwise. + */ + public boolean hasNonPrimaryKeyRestrictions() + { + return !nonPrimaryKeyRestrictions.isEmpty(); + } + + /** * Returns the partition key components that are not restricted. * @return the partition key components that are not restricted. */ http://git-wip-us.apache.org/repos/asf/cassandra/blob/69edeaa4/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java index 291e3e4..7bba330 100644 --- a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java +++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java @@ -885,6 +885,9 @@ public class SelectStatement implements CQLStatement StatementRestrictions restrictions) throws InvalidRequestException { + checkFalse(restrictions.hasClusteringColumnsRestriction() || restrictions.hasNonPrimaryKeyRestrictions(), + "SELECT DISTINCT with WHERE clause only supports restriction by partition key."); + Collection requestedColumns = selection.getColumns(); for (ColumnDefinition def : requestedColumns) checkFalse(!def.isPartitionKey() && !def.isStatic(), http://git-wip-us.apache.org/repos/asf/cassandra/blob/69edeaa4/test/unit/org/apache/cassandra/cql3/validation/operations/SelectTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/validation/operations/SelectTest.java b/test/unit/org/apache/cassandra/cql3/validation/operations/SelectTest.java index d8cd3c3..d444fde 100644 --- a/test/unit/org/apache/cassandra/cql3/validation/operations/SelectTest.java +++ b/test/unit/org/apache/cassandra/cql3/validation/operations/SelectTest.java @@ -1253,6 +1253,51 @@ public class SelectTest extends CQLTester Assert.assertEquals(9, rows.length); } + @Test + public void testSelectDistinctWithWhereClause() throws Throwable { + createTable("CREATE TABLE %s (k int, a int, b int, PRIMARY KEY (k, a))"); + createIndex("CREATE INDEX ON %s (b)"); + + for (int i = 0; i < 10; i++) + { + execute("INSERT INTO %s (k, a, b) VALUES (?, ?, ?)", i, i, i); + execute("INSERT INTO %s (k, a, b) VALUES (?, ?, ?)", i, i * 10, i * 10); + } + + String distinctQueryErrorMsg = "SELECT DISTINCT with WHERE clause only supports restriction by partition key."; + assertInvalidMessage(distinctQueryErrorMsg, + "SELECT DISTINCT k FROM %s WHERE a >= 80 ALLOW FILTERING"); + + assertInvalidMessage(distinctQueryErrorMsg, + "SELECT DISTINCT k FROM %s WHERE k IN (1, 2, 3) AND a = 10"); + + assertInvalidMessage(distinctQueryErrorMsg, + "SELECT DISTINCT k FROM %s WHERE b = 5"); + + assertRows(execute("SELECT DISTINCT k FROM %s WHERE k = 1"), + row(1)); + assertRows(execute("SELECT DISTINCT k FROM %s WHERE k IN (5, 6, 7)"), + row(5), + row(6), + row(7)); + + // With static columns + createTable("CREATE TABLE %s (k int, a int, s int static, b int, PRIMARY KEY (k, a))"); + createIndex("CREATE INDEX ON %s (b)"); + for (int i = 0; i < 10; i++) + { + execute("INSERT INTO %s (k, a, b, s) VALUES (?, ?, ?, ?)", i, i, i, i); + execute("INSERT INTO %s (k, a, b, s) VALUES (?, ?, ?, ?)", i, i * 10, i * 10, i * 10); + } + + assertRows(execute("SELECT DISTINCT s FROM %s WHERE k = 5"), + row(50)); + assertRows(execute("SELECT DISTINCT s FROM %s WHERE k IN (5, 6, 7)"), + row(50), + row(60), + row(70)); + } + /** * Migrated from cql_tests.py:TestCQL.bug_6327_test() */