cqlcassandra-2.1paxos

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?


Solution

  • 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.