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 <oleksandr.pet...@gmail.com>
Authored: Thu Apr 14 12:26:52 2016 +0200
Committer: Benjamin Lerer <b.le...@gmail.com>
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 <code>true</code> if the restrictions contain any non-primary 
key restrictions, <code>false</code> 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<ColumnDefinition> 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()
      */

Reply via email to