cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ble...@apache.org
Subject cassandra git commit: Add +=/-= shortcut syntax for update queries
Date Mon, 26 Sep 2016 09:53:03 GMT
Repository: cassandra
Updated Branches:
  refs/heads/trunk bddfd643b -> cfa90c065


Add +=/-= shortcut syntax for update queries

patch by Alex Petrov; reviewed by Benjamin Lerer for CASSANDRA-12232


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/cfa90c06
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/cfa90c06
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/cfa90c06

Branch: refs/heads/trunk
Commit: cfa90c065f649ea5fac5507cf6c06c9a3d3254de
Parents: bddfd64
Author: Alex Petrov <oleksandr.petrov@gmail.com>
Authored: Mon Sep 26 11:48:13 2016 +0200
Committer: Benjamin Lerer <b.lerer@gmail.com>
Committed: Mon Sep 26 11:48:13 2016 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 NEWS.txt                                        |  1 +
 src/antlr/Parser.g                              |  8 +++++
 .../validation/entities/CollectionsTest.java    | 35 +++++++++++++++++++-
 .../cql3/validation/entities/CountersTest.java  | 16 +++++++++
 5 files changed, 60 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/cfa90c06/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 8d96050..f9fed90 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 3.10
+ * Add +=/-= shortcut syntax for update queries (CASSANDRA-12232)
  * Include repair session IDs in repair start message (CASSANDRA-12532)
  * Add a blocking task to Index, run before joining the ring (CASSANDRA-12039)
  * Fix NPE when using CQLSSTableWriter (CASSANDRA-12667)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/cfa90c06/NEWS.txt
----------------------------------------------------------------------
diff --git a/NEWS.txt b/NEWS.txt
index 708e839..4bcb197 100644
--- a/NEWS.txt
+++ b/NEWS.txt
@@ -18,6 +18,7 @@ using the provided 'sstableupgrade' tool.
 
 New features
 ------------
+   - Support for the assignment operators +=/-= has been added for update queries.
    - An Index implementation may now provide a task which runs prior to joining
      the ring. See CASSANDRA-12039
    - Filtering on partition key columns is now also supported for queries without

http://git-wip-us.apache.org/repos/asf/cassandra/blob/cfa90c06/src/antlr/Parser.g
----------------------------------------------------------------------
diff --git a/src/antlr/Parser.g b/src/antlr/Parser.g
index 7273286..4fa27a0 100644
--- a/src/antlr/Parser.g
+++ b/src/antlr/Parser.g
@@ -1332,6 +1332,7 @@ columnOperation[List<Pair<ColumnDefinition.Raw, Operation.RawUpdate>>
operations
 
 columnOperationDifferentiator[List<Pair<ColumnDefinition.Raw, Operation.RawUpdate>>
operations, ColumnDefinition.Raw key]
     : '=' normalColumnOperation[operations, key]
+    | shorthandColumnOperation[operations, key]
     | '[' k=term ']' collectionColumnOperation[operations, key, k]
     | '.' field=fident udtColumnOperation[operations, key, field]
     ;
@@ -1366,6 +1367,13 @@ normalColumnOperation[List<Pair<ColumnDefinition.Raw, Operation.RawUpdate>>
oper
       }
     ;
 
+shorthandColumnOperation[List<Pair<ColumnDefinition.Raw, Operation.RawUpdate>>
operations, ColumnDefinition.Raw key]
+    : sig=('+=' | '-=') t=term
+      {
+          addRawUpdate(operations, key, $sig.text.equals("+=") ? new Operation.Addition(t)
: new Operation.Substraction(t));
+      }
+    ;
+
 collectionColumnOperation[List<Pair<ColumnDefinition.Raw, Operation.RawUpdate>>
operations, ColumnDefinition.Raw key, Term.Raw k]
     : '=' t=term
       {

http://git-wip-us.apache.org/repos/asf/cassandra/blob/cfa90c06/test/unit/org/apache/cassandra/cql3/validation/entities/CollectionsTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/validation/entities/CollectionsTest.java
b/test/unit/org/apache/cassandra/cql3/validation/entities/CollectionsTest.java
index a0a6e73..4a4c1df 100644
--- a/test/unit/org/apache/cassandra/cql3/validation/entities/CollectionsTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/entities/CollectionsTest.java
@@ -118,6 +118,19 @@ public class CollectionsTest extends CQLTester
                    row(set("v7"))
         );
 
+        execute("UPDATE %s SET s += ? WHERE k = 0", set("v5"));
+        execute("UPDATE %s SET s += ? WHERE k = 0", set("v6"));
+
+        assertRows(execute("SELECT s FROM %s WHERE k = 0"),
+                   row(set("v5", "v6", "v7"))
+        );
+
+        execute("UPDATE %s SET s -= ? WHERE k = 0", set("v6", "v5"));
+
+        assertRows(execute("SELECT s FROM %s WHERE k = 0"),
+                   row(set("v7"))
+        );
+
         execute("DELETE s[?] FROM %s WHERE k = 0", set("v7"));
 
         // Deleting an element that does not exist will succeed
@@ -166,7 +179,19 @@ public class CollectionsTest extends CQLTester
                    row(map("v5", 5, "v6", 6, "v7", 7))
         );
 
-        execute("DELETE m[?] FROM %s WHERE k = 0", "v7");
+        execute("UPDATE %s SET m = m - ? WHERE k = 0", set("v7"));
+
+        assertRows(execute("SELECT m FROM %s WHERE k = 0"),
+                   row(map("v5", 5, "v6", 6))
+        );
+
+        execute("UPDATE %s SET m += ? WHERE k = 0", map("v7", 7));
+
+        assertRows(execute("SELECT m FROM %s WHERE k = 0"),
+                   row(map("v5", 5, "v6", 6, "v7", 7))
+        );
+
+        execute("UPDATE %s SET m -= ? WHERE k = 0", set("v7"));
 
         assertRows(execute("SELECT m FROM %s WHERE k = 0"),
                    row(map("v5", 5, "v6", 6))
@@ -234,6 +259,14 @@ public class CollectionsTest extends CQLTester
 
         assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v9", "v6", "v7")));
 
+        execute("UPDATE %s SET l += ? WHERE k = 0", list("v8"));
+
+        assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v9", "v6", "v7", "v8")));
+
+        execute("UPDATE %s SET l -= ? WHERE k = 0", list("v6", "v8"));
+
+        assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v9", "v7")));
+
         execute("DELETE l FROM %s WHERE k = 0");
 
         assertRows(execute("SELECT l FROM %s WHERE k = 0"), row((Object) null));

http://git-wip-us.apache.org/repos/asf/cassandra/blob/cfa90c06/test/unit/org/apache/cassandra/cql3/validation/entities/CountersTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/validation/entities/CountersTest.java b/test/unit/org/apache/cassandra/cql3/validation/entities/CountersTest.java
index 866fcbf..94e1c52 100644
--- a/test/unit/org/apache/cassandra/cql3/validation/entities/CountersTest.java
+++ b/test/unit/org/apache/cassandra/cql3/validation/entities/CountersTest.java
@@ -49,6 +49,22 @@ public class CountersTest extends CQLTester
         execute("UPDATE %s SET total = total -2 WHERE userid = 1 AND url = 'http://foo.com'");
         assertRows(execute("SELECT total FROM %s WHERE userid = 1 AND url = 'http://foo.com'"),
                    row(-4L));
+
+        execute("UPDATE %s SET total += 6 WHERE userid = 1 AND url = 'http://foo.com'");
+        assertRows(execute("SELECT total FROM %s WHERE userid = 1 AND url = 'http://foo.com'"),
+                   row(2L));
+
+        execute("UPDATE %s SET total -= 1 WHERE userid = 1 AND url = 'http://foo.com'");
+        assertRows(execute("SELECT total FROM %s WHERE userid = 1 AND url = 'http://foo.com'"),
+                   row(1L));
+
+        execute("UPDATE %s SET total += -2 WHERE userid = 1 AND url = 'http://foo.com'");
+        assertRows(execute("SELECT total FROM %s WHERE userid = 1 AND url = 'http://foo.com'"),
+                   row(-1L));
+
+        execute("UPDATE %s SET total -= -2 WHERE userid = 1 AND url = 'http://foo.com'");
+        assertRows(execute("SELECT total FROM %s WHERE userid = 1 AND url = 'http://foo.com'"),
+                   row(1L));
     }
 
     /**


Mime
View raw message