cassandradatastax-enterprise

Contradicting information on CQL counter type in docs


I have been looking for some information on counters and it seems like there is some rather contradicting info regarding to what you can do with them.

According to the official DataStax Documentation (https://docs.datastax.com/en/cql-oss/3.x/cql/cql_reference/counter_type.html) "You cannot set the value of a counter, which supports two operations: increment and decrement.".

However, if we look into the BATCH CQL documentation (https://docs.datastax.com/en/dse/6.0/cql/cql/cql_reference/cql_commands/cqlBatch.html#cqlBatch__batch-updates), the bottom page example includes setting, adding, and subtracting a counter variable within a batch.

This example is likely also breaking the rule that having a counter in a table should only have counters for the rest of the table.

So what really are the limitations / usability for counters in cassandra DataStax? There does not seem to be a clear definition.


Solution

  • I think it's just a misunderstanding. Those pages do not contradict each other.

    The CQL Counter type page correctly states that it is not possible to set the value of a counter column. For example, this is NOT valid:

    UPDATE ks.counter_table
        SET count = 10
        WHERE pk = ?
    

    The only valid operations on a counter column are increment and decrement. Here are some examples:

    UPDATE ks.counter_table
      SET count = count + 1
      WHERE pk = ?
    
    UPDATE ks.counter_table
      SET count = count - 1
      WHERE pk = ?
    

    In the BATCH command page, the first 2 examples are increment operations:

    UPDATE cycling.popular_count
      SET popularity = popularity + 1
      WHERE id = 6ab09bec-e68e-48d9-a5f8-97e6fb4c9b47;
    
    UPDATE cycling.popular_count
      SET popularity = popularity + 125
      WHERE id = 6ab09bec-e68e-48d9-a5f8-97e6fb4c9b47;
    

    The last example is a decrement operation:

    UPDATE cycling.popular_count
      SET popularity = popularity - 64
      WHERE id = 6ab09bec-e68e-48d9-a5f8-97e6fb4c9b47;
    

    I can't see how the examples would break the rule about only having counters in a counter table. From the examples, I would infer that the table schema is:

    CREATE TABLE cycling.popular_count
        id uuid,
        popularity counter,
        PRIMARY KEY(id)
    )
    

    You can have as many non-counter columns in the table as long as they are part of the PRIMARY KEY.

    As a side note, it is not correct to refer to the software as either "Cassandra DataStax" or "DataStax Cassandra" so I've updated the title accordingly. DataStax (the company) does not own Cassandra. The more appropriate reference is "Apache Cassandra" or just plain "Cassandra". Cheers!