Search code examples
cassandracqlscylla

How do I get the last value of a CQL list column?


I'm using scylladb lists ( https://docs.scylladb.com/stable/cql/types.html#lists ). I created a same table from documentation:

CREATE TABLE plays (
    id text PRIMARY KEY,
    game text,
    players int,
    scores list<int> // A list of integers
);

After creating the table, I inserted the data using query:

INSERT INTO plays (id, game, players, scores)
           VALUES ('123-afde', 'quake', 3, [17, 4, 24241414]);

How do i get only last score 24241414 from table, instead of getting full list of scores? I need CQL-query (operator).

Query SELECT scores FROM plays WHERE id = '123-afde' returns full list. It's not what I need.


Solution

  • Selecting an element by index (at a particular position) of a CQL list is not supported by the CQL grammar because its performance can be unpredictable as discussed in CASSANDRA-7396.

    You will need to implement it in your app by getting the size/length of the list then use that value to calculate the last index position. Cheers!


    👉 Please support the Apache Cassandra community by hovering over then click on the Watch tag button. 🙏 Thanks!