DML

Updated at:

Lindorm CQL supports three Data Manipulation Language (DML) statements: SELECT, UPDATE, and DELETE.

Syntax notation

The syntax definitions use the following conventions:

Symbol Meaning
::= Definition — the left side is defined by the right side
[...] Optional element
(...) Grouping
| Alternatives — choose one
* Repeatable zero or more times
+ Repeatable one or more times
'...' Literal string

SELECT

Reads one or more columns from rows in a table and returns a result set. Functions, including aggregate functions, can be applied to the results.

Lindorm CQL does not support joins or subqueries. Each SELECT statement applies to a single table.

Syntax

select_statement ::=  SELECT [ JSON | HOTDATA ] ( select_clause | '*' )
                      FROM table_name
                      [ WHERE where_clause ]
                      [ GROUP BY group_by_clause ]
                      [ ORDER BY ordering_clause ]
                      [ PER PARTITION LIMIT (integer | bind_marker) ]
                      [ LIMIT (integer | bind_marker) ]
                      [ ALLOW FILTERING ]
select_clause    ::=  selector [ AS identifier ] ( ',' selector [ AS identifier ] )
selector         ::=  column_name
                      | term
                      | CAST '(' selector AS cql_type ')'
                      | function_name '(' [ selector ( ',' selector )* ] ')'
                      | COUNT '(' '*' ')'
where_clause     ::=  relation ( AND relation )*
relation         ::=  column_name operator term
                      '(' column_name ( ',' column_name )* ')' operator tuple_literal
                      TOKEN '(' column_name ( ',' column_name )* ')' operator term
operator         ::=  '=' | '<' | '>' | '<=' | '>=' | '!=' | IN | CONTAINS | CONTAINS KEY
group_by_clause  ::=  column_name ( ',' column_name )*
ordering_clause  ::=  column_name [ ASC | DESC ] ( ',' column_name [ ASC | DESC ] )*

Examples

CREATE TABLE persioninfo (c1 int PRIMARY KEY, c2 text, c3 text);

-- Select specific columns with an IN condition
SELECT c1, c2 FROM persioninfo WHERE c1 IN (199, 200, 207);

-- Return results as JSON
SELECT JSON c1, c2 FROM persioninfo WHERE c1 = 199;

-- Use column aliases
SELECT c1 AS name, c2 AS address FROM persioninfo;
CREATE TABLE persioninfo (c1 int, c2 text, c3 text, PRIMARY KEY (c1, c2));

-- Filter on partition key and a range of clustering column values
SELECT *
FROM persioninfo
WHERE c1 = 12
  AND c2 > '2011-02-03'
  AND c2 <= '2012-01-01';

-- Count all rows
SELECT COUNT(*) AS user_count FROM persioninfo;

Select clause

The select clause determines which columns to return. It accepts a comma-separated list of selectors, or * to return all columns.

Selector types

Selector Description
column_name Returns the value of the named column.
term Returns the term's value for every row in the result set. Typically nested inside a function.
CAST(selector AS cql_type) Converts the nested selector to a compatible CQL type.
function_name(selector, ...) Invokes a function; the arguments are selectors.
COUNT(*) Returns the count of all non-empty results.

WRITETIME function

WRITETIME(column_name) retrieves the timestamp stored as metadata for the column — specifically, when the column's value was last written. WRITETIME takes exactly one argument and is valid only in the select clause.

WHERE clause

The WHERE clause specifies which rows to return. Relations must reference either primary key columns (partition key or clustering columns) or columns with a secondary index.

Restrictions on partition key columns

Non-equality relations on a partition key are not supported. IN is treated as an equality relation.

Restrictions on clustering columns

Within a partition, rows are sorted by their clustering columns. Relations on clustering columns must select a contiguous set of rows in that sort order.

Given this table:

CREATE TABLE posts (
    userid     text,
    blog_title text,
    posted_at  timestamp,
    entry_title text,
    content    text,
    category   int,
    PRIMARY KEY (userid, blog_title, posted_at)
);

This query is valid — it selects a contiguous range of posted_at values within a specific (userid, blog_title) partition:

-- Valid: all three primary key components are constrained
SELECT entry_title, content FROM posts
WHERE userid = 'john doe'
  AND blog_title = 'John''s Blog'
  AND posted_at >= '2012-01-01' AND posted_at < '2012-01-31';

This query is rejected — skipping blog_title breaks the contiguous-row requirement (assuming no secondary index on the table):

-- Rejected: blog_title is not constrained, so the result is not a contiguous range
SELECT entry_title, content FROM posts
WHERE userid = 'john doe'
  AND posted_at >= '2012-01-01' AND posted_at < '2012-01-31';

Tuple notation for clustering columns

Group clustering columns with tuple notation to express multi-column ordering conditions:

-- Returns all rows that come after (blog_title='John''s Blog', posted_at='2012-01-01')
-- in the clustering order. A row qualifies if blog_title > 'John''s Blog',
-- even if its posted_at is earlier.
SELECT * FROM posts
WHERE userid = 'john doe'
  AND (blog_title, posted_at) > ('John''s Blog', '2012-01-01');

This differs from the following query, which requires both conditions to hold independently:

SELECT * FROM posts
WHERE userid = 'john doe'
  AND blog_title > 'John''s Blog'
  AND posted_at > '2012-01-01';

Tuple notation also works with IN:

SELECT * FROM posts
WHERE userid = 'john doe'
  AND (blog_title, posted_at) IN (('John''s Blog', '2012-01-01'), ('Extreme Chess', '2014-06-01'));

IN operator restrictions

The IN operator is allowed only on:

  • The last column of the partition key

  • The last column of the primary key

GROUP BY

GROUP BY condenses rows that share identical values for the specified columns into a single row.

Constraints:

  • GROUP BY accepts only primary key column names, in primary key order.

  • Grouping is limited to the partition key level or the clustering column level.

  • A primary key column constrained by an equality condition in WHERE does not need to appear in GROUP BY.

GROUP BY pulls matching data to a single server node for processing. For large data volumes, this can be time-consuming.

ORDER BY

ORDER BY controls the sort order of returned rows. Accepts a list of column names, each with an optional direction (ASC or DESC; defaults to ASC).

The allowed sort orders depend on the table's clustering order definition:

  • No CLUSTERING ORDER defined: sort by the natural order of clustering columns, or its reverse.

  • CLUSTERING ORDER defined: sort by the order specified in CLUSTERING ORDER, or its reverse.

LIMIT and PER PARTITION LIMIT

LIMIT restricts the total number of rows returned by the query. PER PARTITION LIMIT restricts the number of rows returned per partition.

ALLOW FILTERING

By default, Lindorm CQL only executes SELECT queries that do not require server-side filtering. These queries have predictable performance: execution time is proportional to the amount of data returned, which you control with LIMIT.

ALLOW FILTERING explicitly enables queries that require server-side filtering. These queries may have unpredictable performance — even a query returning a small result can be slow if the cluster stores a large amount of data.

Example

Given this table and secondary index:

CREATE TABLE users (
    username   text PRIMARY KEY,
    firstname  text,
    lastname   text,
    birth_year int,
    country    text
);

CREATE INDEX ON users(birth_year);

Both of the following queries are valid — performance is proportional to the amount of data returned:

-- Valid: returns all users
SELECT * FROM users;

-- Valid: returns users born in 1981; uses the secondary index
SELECT * FROM users WHERE birth_year = 1981;

Note that due to how secondary indexes are implemented, the second query may still depend on the number of nodes in the cluster and, indirectly, on the total amount of data stored in the cluster.

This query is rejected because Lindorm CQL cannot guarantee bounded scan time:

-- Rejected: filtering on 'country' requires a full scan of the birth_year index
SELECT * FROM users WHERE birth_year = 1981 AND country = 'FR';

Adding ALLOW FILTERING makes the query valid:

-- Valid with ALLOW FILTERING, but performance depends on total data volume
SELECT * FROM users WHERE birth_year = 1981 AND country = 'FR' ALLOW FILTERING;

HOTDATA

When a table is created with hot/cold data separation enabled, use the HOTDATA keyword to query only hot data:

SELECT HOTDATA * FROM persioninfo;

Without HOTDATA, the query searches both hot and cold data and merges the results.

SEARCH INDEX SELECT

Columns with a SEARCH INDEX support fuzzy match queries, multi-dimensional queries, and sorting.

Fuzzy match with LIKE

The % wildcard matches zero or more characters. The following match types are supported:

-- Exact match: column value is exactly 'v2'
SELECT * FROM persioninfo WHERE c2 LIKE 'v2';

-- Prefix match: value starts with 'v2'
SELECT * FROM kss.tb WHERE c2 LIKE 'v2%';

-- Suffix match: value ends with 'v2'
SELECT * FROM kss.tb WHERE c2 LIKE '%v2';

-- Contains match: value contains 'v2'
SELECT * FROM kss.tb WHERE c2 LIKE '%v2%';

Native Solr queries with search_query

The search_query keyword runs any native Solr query within a SELECT statement. This supports complex query and retrieval scenarios beyond what LIKE can express.

SELECT selectors
  FROM table
  WHERE (indexed_column_expression | search_query = 'search_expression')
  [ LIMIT n ]
  [ ORDER BY column_name ];

The search_query value is a JSON object. For character restrictions in the search expression, see Local Parameters in Queries.

Commonly used options:

Option Type Description
q string Main query expression
fq string or array Filter query expression
facet object Facet query expression
sort string Sort expression
start number Start index for pagination
timeAllowed number Maximum search time in milliseconds
TZ string Time zone ID (any valid Java TimeZone value)
paging string Paging mode
distrib.singlePass boolean Single-pass distributed query
shards.failover boolean Fail over to other shards on error. Default: true
shards.tolerant boolean Tolerate partial shard failures. Default: false
commit boolean Commit after search
route.partition array of strings Partition routing expression
route.range array of strings Range routing expression
query.name string Query name

Example

CREATE TABLE persioninfo (
    name text PRIMARY KEY,
    city text,
    id   int
) WITH extensions = {'CONSISTENCY_TYPE': 'strong', 'MUTABILITY': 'IMMUTABLE'};

CREATE SEARCH INDEX sidx ON persioninfo WITH COLUMNS (id, city);

INSERT INTO persioninfo (name, city, id) VALUES ('MY', 'hangzhou', 18);

-- Query using a native Solr expression
SELECT name, id FROM persioninfo WHERE search_query = 'city:hangzhou';
search_query cannot be combined with other WHERE conditions. For example, WHERE search_query = 'c1:name' AND column = 'a' is not supported.

UPDATE

Updates one or more columns in the rows specified by the WHERE clause.

Syntax

update_statement ::=  UPDATE table_name
                      [ USING update_parameter ( AND update_parameter )* ]
                      SET assignment ( ',' assignment )*
                      WHERE where_clause
update_parameter ::=  ( integer | bind_marker )
assignment       ::=  simple_selection '=' term
                     | column_name '=' column_name ( '+' | '-' ) term
                     | column_name '=' list_literal '+' column_name
simple_selection ::=  column_name
                     | column_name '[' term ']'
                     | column_name '.' field_name
condition        ::=  simple_selection operator term

Usage notes

  • The WHERE clause must include all columns that form the primary key.

  • SET assigns values to non-primary-key columns.

  • UPDATE has upsert semantics: if the specified row does not exist, it is created; if it exists, it is updated. There is no way to distinguish a creation from an update.

  • All updates within the same partition key are applied atomically and in isolation.

  • Counter columns can coexist with non-counter columns. This differs from Apache Cassandra CQL.

  • To increment or decrement a counter, use the form c = c + 3. The column name on both sides of = must match. Increment and decrement are the only permitted operations on counter columns.

Example

CREATE TABLE product (productname text PRIMARY KEY, company text, subdepartment text);

UPDATE product
   SET company       = 'aliyun cloud',
       subdepartment = 'database nosql'
 WHERE productname = 'lindorm';

DELETE

Removes columns or entire rows from a table.

Syntax

delete_statement ::=  DELETE [ simple_selection ( ',' simple_selection ) ]
                      FROM table_name
                      [ USING update_parameter ( AND update_parameter )* ]
                      WHERE where_clause

Usage notes

  • Delete specific columns: list the column names immediately after DELETE. Only those columns are removed from the rows matched by WHERE.

  • Delete entire rows: omit column names. All rows matched by WHERE are deleted.

  • Use IN in the WHERE clause to delete multiple rows in a single statement.

  • Deleting a range of rows is not currently supported.

  • All delete operations on rows with the same partition key are applied atomically and in isolation.

Example

INSERT INTO t1 (c1, c2, c3) VALUES ('11', '22', '33');

-- Delete only column c2 from the row where c1 = '11'
DELETE c2 FROM t1 WHERE c1 IN ('11');