diff --git a/CHANGES.txt b/CHANGES.txt index 7778c73ad5..61c57a3f7e 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -142,6 +142,7 @@ * Fix sstableupgrade throws exception (CASSANDRA-8688) * Fix hang when repairing empty keyspace (CASSANDRA-8694) Merged from 2.0: + * Add support for UPDATE ... IF EXISTS (CASSANDRA-8610) * Fix reversal of list prepends (CASSANDRA-8733) * Prevent non-zero default_time_to_live on tables with counters (CASSANDRA-8678) diff --git a/pylib/cqlshlib/cql3handling.py b/pylib/cqlshlib/cql3handling.py index 552f5c1ebe..ae2d50ab18 100644 --- a/pylib/cqlshlib/cql3handling.py +++ b/pylib/cqlshlib/cql3handling.py @@ -766,7 +766,7 @@ syntax_rules += r''' ( "AND" [updateopt]= )* )? "SET" ( "," )* "WHERE" - ( "IF" )? + ( "IF" ( "EXISTS" | ))? ; ::= updatecol= ( "=" update_rhs=( | ) diff --git a/pylib/cqlshlib/helptopics.py b/pylib/cqlshlib/helptopics.py index c4f65b0f66..6e2fd6b57d 100644 --- a/pylib/cqlshlib/helptopics.py +++ b/pylib/cqlshlib/helptopics.py @@ -562,7 +562,8 @@ class CQL3HelpTopics(CQLHelpTopics): UPDATE [.] [USING [TIMESTAMP ] [AND TTL ]] - SET name1 = value1, name2 = value2 WHERE = keyval; + SET name1 = value1, name2 = value2 WHERE = keyval + [IF EXISTS]; An UPDATE is used to write one or more columns to a record in a table. No results are returned. The record's primary key must be completely diff --git a/src/java/org/apache/cassandra/cql3/Cql.g b/src/java/org/apache/cassandra/cql3/Cql.g index c39da63662..08436abbfa 100644 --- a/src/java/org/apache/cassandra/cql3/Cql.g +++ b/src/java/org/apache/cassandra/cql3/Cql.g @@ -387,18 +387,20 @@ updateStatement returns [UpdateStatement.ParsedUpdate expr] @init { Attributes.Raw attrs = new Attributes.Raw(); List> operations = new ArrayList>(); + boolean ifExists = false; } : K_UPDATE cf=columnFamilyName ( usingClause[attrs] )? K_SET columnOperation[operations] (',' columnOperation[operations])* K_WHERE wclause=whereClause - ( K_IF conditions=updateConditions )? + ( K_IF ( K_EXISTS { ifExists = true; } | conditions=updateConditions ))? { return new UpdateStatement.ParsedUpdate(cf, attrs, operations, wclause, - conditions == null ? Collections.>emptyList() : conditions); + conditions == null ? Collections.>emptyList() : conditions, + ifExists); } ; diff --git a/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java b/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java index 4c7660f42f..b7156a081d 100644 --- a/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java +++ b/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java @@ -198,14 +198,16 @@ public class UpdateStatement extends ModificationStatement * @param attrs additional attributes for statement (timestamp, timeToLive) * @param updates a map of column operations to perform * @param whereClause the where clause - */ + * @param ifExists flag to check if row exists + * */ public ParsedUpdate(CFName name, Attributes.Raw attrs, List> updates, List whereClause, - List> conditions) + List> conditions, + boolean ifExists) { - super(name, attrs, conditions, false, false); + super(name, attrs, conditions, false, ifExists); this.updates = updates; this.whereClause = whereClause; }