Functions

Updated at:

Lindorm Cassandra Query Language (CQL) provides two categories of built-in functions: scalar functions and native aggregate functions.

  • Scalar functions are used to obtain values and generate outputs.

  • Native aggregate functions aggregate values across multiple rows returned by a SELECT statement.

All functions in this topic are native hard-coded functions built into Lindorm CQL.

Function quick reference

CategoryFunctionDescription
ScalarcastConverts a value from one native CQL type to another.
ScalarnowGenerates a unique timeuuid on the coordinator node at statement execution time.
ScalarmaxTimeuuidReturns the largest possible timeuuid for a given timestamp or date string.
ScalarminTimeuuidReturns the smallest possible timeuuid for a given timestamp or date string.
ScalarcurrentTimestampReturns the current date and time as a Timestamp.
ScalarcurrentDateReturns the current date as a date.
ScalarcurrentTimeReturns the current time as a time.
ScalarcurrentTimeUUIDReturns the current time as a timeUUID.
ScalartoDateConverts timeuuid or timestamp to date.
ScalartoTimestampConverts timeuuid or date to timestamp.
ScalartoUnixTimestampConverts timeuuid, timestamp, or date to a Unix timestamp (bigint).
ScalartypeAsBlobConverts a native CQL type value to blob.
ScalarblobAsTypeConverts a 64-bit blob value to a native CQL type.
Native aggregatecountCounts non-empty values in a column or counts the total number of returned rows.
Native aggregateminReturns the smallest value in a column.
Native aggregatemaxReturns the largest value in a column.
Native aggregatesumCalculates the sum of all values in a column.
Native aggregateavgCalculates the mean of all values in a column.

Scalar functions

cast

Converts a value from one native data type to another. Conversions strictly rely on the semantics of Java.

Supported conversions

Source typeDestination types
asciitext
biginttinyint, smallint, int, float, double, decimal, varint, text
booleantext
countertinyint, smallint, int, bigint, float, double, decimal, varint, text
datetimestamp
decimaltinyint, smallint, int, bigint, float, double, varint, text
doubletinyint, smallint, int, bigint, float, decimal, varint, text
floattinyint, smallint, int, bigint, double, decimal, varint, text
inettext
inttinyint, smallint, bigint, float, double, decimal, varint, text
timetext
timestampdate, text
timeuuidtimestamp, date, text
tinyinttinyint, smallint, int, bigint, float, double, decimal, varint, text
uuidtext
varinttinyint, smallint, int, bigint, float, double, decimal, text

Example

CREATE TABLE persioninfo (c1 int PRIMARY KEY, c2 timeuuid);
SELECT avg(cast(c1 as double)) FROM persioninfo;

now

Generates a unique timeuuid value on the coordinator node at the time the statement executes. The returned value is guaranteed to be unique.

Using now() in a WHERE clause always returns an empty result set, because each call generates a new unique value that will never match an existing row.

Example

SELECT * FROM persioninfo WHERE c2 = now();

maxTimeuuid and minTimeuuid

These functions take a timestamp value (a timestamp or a date string) and return a boundary TimeUUID for use in range queries:

  • maxTimeuuid returns the largest possible TimeUUID for the given timestamp.

  • minTimeuuid returns the smallest possible TimeUUID for the given timestamp.

Example

SELECT * FROM persioninfo WHERE c2 > maxTimeuuid('2013-01-01 00:05+0000')
    AND c2 < minTimeuuid('2013-02-02 10:00+0000') ALLOW FILTERING ;

Datetime functions

These functions return the current date or time at the moment of the call.

FunctionReturn type
currentTimestampTimestamp
currentDatedate
currentTimetime
currentTimeUUIDtimeUUID

Time conversion functions

These functions convert timeuuid, timestamp, or date values to other native CQL types.

FunctionInput typeOutput typeDescription
toDatetimeuuiddateConverts timeuuid to date
toDatetimestampdateConverts timestamp to date
toTimestamptimeuuidtimestampConverts timeuuid to timestamp
toTimestampdatetimestampConverts date to timestamp
toUnixTimestamptimeuuidbigintConverts timeuuid to Unix timestamp (bigint)
toUnixTimestamptimestampbigintConverts timestamp to Unix timestamp (bigint, raw)
toUnixTimestampdatebigintConverts date to Unix timestamp (bigint, raw)

Blob conversion functions

These functions convert native CQL types to and from binary (blob) representation.

  • typeAsBlob(value) — takes a value of any native CQL type and returns it as a blob. For example, bigintAsBlob(3) returns 0x0000000000000003.

  • blobAsType(blob) — takes a 64-bit binary value and converts it to the corresponding type. For example, blobAsBigint(0x0000000000000003) returns 3.

Example

CREATE TABLE persioninfo (c1 text PRIMARY KEY, c2 bigint);
INSERT INTO persioninfo (c1, c2) VALUES ( '11', blobAsBigint(0x0000000000000003));

Native aggregate functions

Native aggregate functions operate on all values in a column across the rows returned by a SELECT statement.

count

Counts non-empty values in a column, or counts the total number of returned rows.

-- Count all returned rows
SELECT COUNT (*) FROM persioninfo;
SELECT COUNT (1) FROM persioninfo;

-- Count non-empty values in a specific column
SELECT COUNT (c2) FROM persioninfo;

min and max

min returns the smallest value in a column; max returns the largest.

CREATE TABLE persioninfo (c1 text PRIMARY KEY, c2 int);
INSERT INTO persioninfo (c1, c2) VALUES ('k1', 3);
INSERT INTO persioninfo (c1, c2) VALUES ('k2', 4);
SELECT min(c2), max(c2) FROM persioninfo;

sum

Calculates the total of all values in a column.

SELECT sum(c2) FROM persioninfo ;

avg

Calculates the mean of all values in a column.

SELECT avg(c2) FROM persioninfo ;