Kevin
Kevin

Reputation: 2697

Is this type of counter table definition valid?

I want to create a table with wide partitions (or, put another way, a table which has no value columns (non primary key columns)) that enables the number of rows in any of its partitions to be efficiently procured. Here is a simple definition of such a table

CREATE TABLE IF NOT EXISTS test_table
(
    partitionKeyCol         timestamp
    clusteringCol           timeuuid
    partitionRowCountCol    counter    static
    PRIMARY KEY             (partitionKeyCol, clusteringCol)
)

The problem with this definition, and others structured like it, is that their validity cannot be clearly deduced from the information contained in the docs.

What the docs do state (with regards to counters):

What the docs do not state (with regards to counters):

Given the information on this subject that is present in (and absent from) the docs, such a definition appears to be valid. However, I'm not sure how that is possible, given that the updates to partitionRowCountCol would require use of a write path different from that used to insert (partitionKeyCol, clusteringCol) tuples.

Is this type of counter table definition valid? If so, how are writes to the table carried out?

Upvotes: 1

Views: 623

Answers (1)

Adam Holmberg
Adam Holmberg

Reputation: 7365

It looks like a table with this structure can be defined, but I'm struggling to find a good use case for it. It seems there is no way to actually write to that clustering column.

CREATE TABLE test.test_table (
    a timestamp,
    b timeuuid,
    c counter static,
    PRIMARY KEY (a, b)
);

cassandra@cqlsh:test> insert into test_table (a,b,c) VALUES (unixtimestampof(now()), now(), 3);
InvalidRequest: code=2200 [Invalid query] message="INSERT statements are not allowed on counter tables, use UPDATE instead"
cassandra@cqlsh:test> update test_table set c = c + 1 where a=unixtimestampof(now());
cassandra@cqlsh:test> update test_table set c = c + 1 where a=unixtimestampof(now());
cassandra@cqlsh:test> select * from test_table;

 a                        | b    | c
--------------------------+------+---
 2016-03-24 15:04:31+0000 | null | 1
 2016-03-24 15:04:37+0000 | null | 1

(2 rows)
cassandra@cqlsh:test> update test_table set c = c + 1 where a=unixtimestampof(now()) and b=now();
InvalidRequest: code=2200 [Invalid query] message="Invalid restrictions on clustering columns since the UPDATE statement modifies only static columns"
cassandra@cqlsh:test> insert into test_table (a,b) VALUES (unixtimestampof(now()), now());
InvalidRequest: code=2200 [Invalid query] message="INSERT statements are not allowed on counter tables, use UPDATE instead"
cassandra@cqlsh:test> update test_table set b = now(), c = c + 1 where a=unixtimestampof(now());
InvalidRequest: code=2200 [Invalid query] message="PRIMARY KEY part b found in SET part"

What is it you're trying to model?

Upvotes: 1

Related Questions