Search code examples
cassandracqlcql3

"Invalid operator IN for PRIMARY KEY" when updating multiple clustering columns using IN clause


CREATE TABLE IF NOT EXISTS my_counters (
  partition_key text, 
  clustering_key text,
  count counter,
  PRIMARY KEY ((partition_key), clustering_key)
);

I now want to increment counters under two clustering keys. According to Update spec, this should be possible: http://docs.datastax.com/en/cql/3.1/cql/cql_reference/update_r.html

But I am getting "Invalid operator IN for PRIMARY KEY..." error

UPDATE my_counters SET count = count + 1 WHERE partition_key = ? AND clustering_key IN (?, ?)

Is this a specific limitation for counters? I know i can write a counter batch using one clustering key per query, but I would rather not.


Solution

  • From http://docs.datastax.com/en/cql/3.3/cql/cql_reference/update_r.html The IN relation is supported only for the last column of the partition key.

    Also an update cannot be done without specifying the complete PRIMARY KEY (partition key + clustering key)

    create table spending_by_country_state (country text,state text,amount int, primary key ((country,state)));
    
    select * from spending_by_country_state;
    
     country | state     | amount
    ---------+-----------+--------
       India | Karnataka |  20000
       India |    Kerala |  10000
    
    cqlsh:test> update spending_by_country_state set amount = 10001 where country = 'India' and state in ('Karnataka','Kerala');
    cqlsh:test> select * from spending_by_country_state;
    
     country | state     | amount
    ---------+-----------+--------
       India | Karnataka |  10001
       India |    Kerala |  10001
    
    (2 rows)