Search code examples
cassandracqlamazon-keyspaces

Cassandra(Amazon keyspace) Query Error on clustered columns


I am trying execute query on clustering columns on amazon keyspace, since I don't want to use ALLOW FILTERING with my native query I have created 4-5 clustering columns for better performance.

But while trying to filter it based on >= and <= with on 2 clustering columns, I am getting error with below message

message="Clustering column "start_date" cannot be restricted (preceding column "segment_id" is restricted by a non-EQ relation)"

I had also tried with multiple columns query but I am getting not supported error message="MultiColumn relation is not yet supported."

Query for the reference

select * from table_name where shard_id = 568 and division = '10' and customer_id = 568113 and (segment_id, start_date,end_date)>= (-1, '2022-05-16','2017-03-28') and flag = 1;

or

select * from table_name where shard_id = 568 and division = '10' and customer_id = 568113 and segment_id > -1 and start_date >='2022-05-16';


Solution

  • I am assuming that the your table has the following primary key:

    CREATE TABLE table_name (
        ...
        PRIMARY KEY(shard_id, division, customer_id, segment_id, start_date, end_date)
    )
    

    In any case, your CQL query is invalid because you can only apply an inequality operator on the last clustering column in your query. For example, these are valid queries based on your table schema:

    SELECT * FROM table_name
        WHERE shard_id = ? AND division = ?
        AND customer_id <= ?
    
    SELECT SELECT * FROM table_name \
        WHERE shard_id = ? AND division = ? \
        AND customer_id = ? AND segment_id > ?
    
    SELECT SELECT * FROM table_name \
        WHERE shard_id = ? AND division = ? \
        AND customer_id = ? AND segment_id = ? AND start_date >= ?
    

    All preceding columns must be filtered by an equality operator except for the very last clustering column in your query.

    If you require a complex predicate for your queries, you will need to index your Cassandra data with tools such as Elasticsearch or Apache Solr. They will allow you to run complex search parameters to retrieve data from your database. Cheers!