Nishant Kelkar
Nishant Kelkar

Reputation: 412

Multiple IF conditions in Cassandra lightweight transactions/CAS

So I'm trying to do a CAS (compare-and-set) type operation in Cassandra, where I want to do a data update only if a particular non-primary-key column is NULL or < ? where ? is supplied by client-side code.

How do I do this? Something like the following doesn't work:

UPDATE my_dbs.foo SET col1=5 WHERE id=1 IF (col1 = NULL OR col1 < 4);

The error I get is similar to the following:

SyntaxException: <ErrorMessage code=2000 [Syntax error in CQL query] message="line 1:149 no viable alternative at input '(' (...

How do I do this in Cassandra 2.0/2.1?

Upvotes: 2

Views: 2279

Answers (2)

Hocas
Hocas

Reputation: 96

Now you can do IF conditions with several "OR". Just use if field in (value1, value2)

UPDATE my_dbs.foo SET col1=5 WHERE id=1 IF col1 in (null, 1, 2, 3);

Upvotes: 1

whoisdan
whoisdan

Reputation: 86

The OR logical operator is not supported in the IF clause. From the documentation here:

Synopsis:

UPDATE keyspace_name.table_name

USING option AND option

SET assignment, assignment, ...

WHERE row_specification

IF column_name = literal AND column_name = literal . . .

IF EXISTS

Perhaps either do two separate updates to cover both cases, or populate col1 with a default non-null value at insertion given this doesn't compromise your application logic.

Upvotes: 3

Related Questions