Details
-
Improvement
-
Status: Resolved
-
Low
-
Resolution: Fixed
-
None
-
Cassandra 2.1.2
Description
While creating a hands-on exercice for Cassandra, I was facing a quite annoying limitation.
Let's take this table:
CREATE TABLE killrchat.chat_rooms( room_name text, creation_date timestamp, banner text, creator text, participants set<text>, PRIMARY KEY(room_name));
Upon a new participant joining the room, to be concurrency-proof (avoiding mutating the participants set if the room is deleted concurrently), I would like to issue this query:
UPDATE chat_rooms SET participants = participants + {'johnny'} WHERE room_name = 'games' IF EXISTS;
Unfortunately the clause IF EXISTS is not allowed for UPDATE statements. Similarly I tried
UPDATE chat_rooms SET participants = participants + {'johnny'} WHERE room_name = 'games' IF room_name='games';
It doesn't work either, it is not allowed to use one column of the primary key as condition column for LWT (why ? mystery).
So far, the only work-around I found is:
UPDATE chat_rooms SET participants = participants + {'johnny'} WHERE room_name = 'games' IF name='games';
I added an extra column called name which is just the duplicate of the partition key room_name. It does work but is not very elegant.
I believe there are legit use cases for UPDATE ... IF EXISTS;