Uploaded image for project: 'Cassandra'
  1. Cassandra
  2. CASSANDRA-8610

Allow IF EXISTS for UPDATE statements

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Low
    • Resolution: Fixed
    • 2.0.13, 2.1.3
    • Legacy/CQL
    • 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;

      Attachments

        1. 8610.patch
          4 kB
          Prajakta Bhosale

        Activity

          People

            pbhosale Prajakta Bhosale
            doanduyhai DuyHai Doan
            Prajakta Bhosale
            Tom Hobbs
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: