Does Cassandra's CQL support the modulo operator for filtering queries?

Viewed 35

I need to make a selection by the value of the remainder of the division:


cqlsh> SELECT * FROM table WHERE key%10=1;
Invalid syntax at line 1, char 39
  SELECT * FROM table WHERE key%10=1;
                               ^

Does CQL allow such queries?

2 Answers

CQL does not support modulo operations on the partition key.

You can only use the absolute value of the partition to filter in CQL queries. Cheers!

So I went to try this out with a simple table:

CREATE TABLE stackoverflow.keys (
    month int,
    id uuid,
    key int,
    PRIMARY KEY (month, id));

I was able to get this to work:

> SELECT month,month%10,id,key,key%10 AS "key mod 10"
  FROM keys2 WHERE month=202208;

 month  | month % 10 | id                                   | key  | key mod 10
--------+------------+--------------------------------------+------+------------
 202208 |          8 | 2fe7e98f-d1e2-45df-91f6-fa1430995fdc |   12 |          2
 202208 |          8 | 59d04401-d11f-472d-a606-a33d380dc017 |  800 |          0
 202208 |          8 | 92d3fa01-3b1e-4649-9280-786d75e2b9dc | 1157 |          7
 202208 |          8 | 02612042-a7de-49ce-b958-ee60853ba51c | 2660 |          0

However, I was not able to get the modulus operator to work in the WHERE clause.

Related