Search code examples
cqlscylla

Can I select a list of Blob with CQL?


I store all event_id in blob type and would like to select multiple event_id at once.

It works well if I send the query one by one. Like this one:

SELECT JSON event_id, user FROM demo.events WHERE customer_id=1234 AND event_id=0x123123;

But I would like to use IN clause to query multiple event_id at the same time. I've tried the following CQL but failed:

SELECT JSON event_id, user FROM demo.events WHERE customer_id=1234 AND event_id in [0x123123, 0x456456, 0x789789];

An error message is given

SyntaxException: line 1:106 no viable alternative at input 'event_id'

Can anyone tell me if it's possible to do it? Thank you.


Solution

  • IN operator requires tuble_literal on the right side of the expression. Tuples can be defined with (). In your case IN (x, y, z) would work:

    $ create table test.a (customer_id int, event_id blob, user text, PRIMARY KEY (customer_id, event_id));
    
    $ insert into test.a (customer_id, event_id, user) values (1, 0x123123, 'a');
    $ insert into test.a (customer_id, event_id, user) values (1, 0x123124, 'a');
    $ insert into test.a (customer_id, event_id, user) values (1, 0x123125, 'a');
    
    $ SELECT JSON event_id, user FROM test.a WHERE customer_id=1 AND event_id in (0x123123, 0x123124);
    
    [json]
    ---------------------------------------
     {"event_id": "0x123123", "user": "a"}
     {"event_id": "0x123124", "user": "b"}
    
    (2 rows)