This page describes the expressions in Lenses SQL Processors.
Expressions are the parts of a Lenses SQL query that will be evaluated to single values.
Below is the complete list of expressions that Lenses SQL supports.
A literal is an expression that represents a concrete value of a given type. This means that there is no resolution needed for evaluating a literal and its value is simply what is specified in the query.
Integer numbers can be introduced in a Lenses SQL query using integer literals:
In the above query 1
, 2
are integer literals.
Decimal number literals can be used to express constant floating-point numbers:
To express strings, string literals can be used. Single quotes ('
) and double quotes ("
) are both supported as delimiters:
In the example above, "hello "
and 'world!'
are string literals.
Boolean constant values can be expressed using the false
and true
boolean literals:
Sometimes it is necessary to the NULL
literal in a query, for example to test that something is or is not null, or to put a NULL
the value facet, useful to delete records in a compacted topic:
An array is a collection of elements of the same type.
A new array can be defined with the familiar [...]
syntax:
You can use more complex expressions inside the array:
and nested arrays as well:
Note: empty array literals like []
are currently not supported by Lenses SQL. That will change in future versions.
An element of an array can be extracted appending, to the array expression, a pair of square brackets containing the index of the element.
Some examples:
Note how the expression on the left of the brackets can be of arbitrary complexity, like in complexExpression[0].inner[1]
or [1, 2, 3][myIndex]
.
A Struct is a value that is composed by fields and sub-values assigned to those fields. It is similar to what an object is in JSON.
In Lenses SQL there are two ways of building new structs.
In a SELECT
projection, it is possible to use nested aliases to denote the fields of a struct.
In the next example, we are building a struct field called user
, with two subfields, one that is a string, and another one that is a struct:
When the projection will be evaluated, a new struct user
will be built.
The result will be a struct with a name
field, and a nested struct assigned to the contact
field, containing type
and value
subfields.
While nested aliases are a quick way to define new structs, they have some limitations: they can only be used in the projection section of a SELECT
, and they do not cover all the cases where a struct can potentially be used.
Struct expressions overcome these limitations.
With struct expressions one can explicitly build complex structs, specifying the name and the values of the fields, one by one, and as any other expression, they can be used inside other expressions and in any other part of the query where an expression is allowed.
The syntax is similar to the one used to define JSON objects:
Note how the first projection
is equivalent to the three projections used in the previous paragraph:
while the second projection userWithContacts
is not representable with nested aliases, because it defines structs inside an array.
A selection is an explicit reference to a field within a struct. The syntax for a selection is:
Selections can be used to directly access a field of a facet, optionally specifying the topic and the facet:
It is also possible to select a field from more complex expressions. Here we use selections to select fields from array elements, or to directly access a nested field of a struct expression:
In general, a field selection can be used on any expression that returns a struct.
If there are special characters in the field names, backticks (`
) can be used:
A binary expression is an expression that is composed of the left-hand side and right-hand side sub-expressions and an operator that describes how the results of the sub-expressions are to be combined into a single result.
Currently, supported operators are:
Logical operators: AND
, OR
Arithmetic operators: +
, -
, *
, /
, %
(mod)
Ordering operators: >
, >=
, <
, <=
Equality operators: =
, !=
String operators: LIKE
, NOT LIKE
Inclusion operators: IN
, NOT IN
A binary expression is the main way to compose expressions into more complex ones.
For example, 1 + field1
and LENGTH(field2) > 5
are binary expressions, using the +
and the >=
operator respectively.
CASE
expressions return conditional values, depending on the evaluation of sub-expressions present in each of the CASE
’s branches. This expression is Lenses SQL version of what other languages call a switch-statement or if-elseif-else construct.
A function is a predefined named operation that takes a number of input arguments and is evaluated into a result. Functions usually accept the result of other expressions as input arguments, so functions can be nested.