Functions
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
SELECTstatement.
All functions in this topic are native hard-coded functions built into Lindorm CQL.
Function quick reference
| Category | Function | Description |
|---|---|---|
| Scalar | cast | Converts a value from one native CQL type to another. |
| Scalar | now | Generates a unique timeuuid on the coordinator node at statement execution time. |
| Scalar | maxTimeuuid | Returns the largest possible timeuuid for a given timestamp or date string. |
| Scalar | minTimeuuid | Returns the smallest possible timeuuid for a given timestamp or date string. |
| Scalar | currentTimestamp | Returns the current date and time as a Timestamp. |
| Scalar | currentDate | Returns the current date as a date. |
| Scalar | currentTime | Returns the current time as a time. |
| Scalar | currentTimeUUID | Returns the current time as a timeUUID. |
| Scalar | toDate | Converts timeuuid or timestamp to date. |
| Scalar | toTimestamp | Converts timeuuid or date to timestamp. |
| Scalar | toUnixTimestamp | Converts timeuuid, timestamp, or date to a Unix timestamp (bigint). |
| Scalar | typeAsBlob | Converts a native CQL type value to blob. |
| Scalar | blobAsType | Converts a 64-bit blob value to a native CQL type. |
| Native aggregate | count | Counts non-empty values in a column or counts the total number of returned rows. |
| Native aggregate | min | Returns the smallest value in a column. |
| Native aggregate | max | Returns the largest value in a column. |
| Native aggregate | sum | Calculates the sum of all values in a column. |
| Native aggregate | avg | Calculates 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 type | Destination types |
|---|---|
ascii | text |
bigint | tinyint, smallint, int, float, double, decimal, varint, text |
boolean | text |
counter | tinyint, smallint, int, bigint, float, double, decimal, varint, text |
date | timestamp |
decimal | tinyint, smallint, int, bigint, float, double, varint, text |
double | tinyint, smallint, int, bigint, float, decimal, varint, text |
float | tinyint, smallint, int, bigint, double, decimal, varint, text |
inet | text |
int | tinyint, smallint, bigint, float, double, decimal, varint, text |
time | text |
timestamp | date, text |
timeuuid | timestamp, date, text |
tinyint | tinyint, smallint, int, bigint, float, double, decimal, varint, text |
uuid | text |
varint | tinyint, 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.
Usingnow()in aWHEREclause 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:
maxTimeuuidreturns the largest possible TimeUUID for the given timestamp.minTimeuuidreturns 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.
| Function | Return type |
|---|---|
currentTimestamp | Timestamp |
currentDate | date |
currentTime | time |
currentTimeUUID | timeUUID |
Time conversion functions
These functions convert timeuuid, timestamp, or date values to other native CQL types.
| Function | Input type | Output type | Description |
|---|---|---|---|
toDate | timeuuid | date | Converts timeuuid to date |
toDate | timestamp | date | Converts timestamp to date |
toTimestamp | timeuuid | timestamp | Converts timeuuid to timestamp |
toTimestamp | date | timestamp | Converts date to timestamp |
toUnixTimestamp | timeuuid | bigint | Converts timeuuid to Unix timestamp (bigint) |
toUnixTimestamp | timestamp | bigint | Converts timestamp to Unix timestamp (bigint, raw) |
toUnixTimestamp | date | bigint | Converts 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 ablob. For example,bigintAsBlob(3)returns0x0000000000000003.blobAsType(blob)— takes a 64-bit binary value and converts it to the corresponding type. For example,blobAsBigint(0x0000000000000003)returns3.
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 ;