Overview
DQL (Data Query Language) supports different types of functions, depending on how many inputs (operands) they take.
Unary Functions
A unary function applies a function to a single operand. Typical use cases: math, transformation, logic.
JSON form:{
"$unary": {
"$fn": <string>,
"$operand": Object ( Expression )
}
}
- JSON
- SQL
{
"$unary": {
"$fn": "sum",
"$operand": {
"$qualifier": {
"$table": "vs"
},
"$identifier": "nb_users"
}
}
}
SUM("vs"."nb_users")
Binary Functions
A binary function applies a function to exactly two operands. Typical use cases: arithmetic, comparison, logic.
JSON form:{
"$binary": {
"$fn": <string>,
"$left": Object ( Expression ),
"$right": Object ( Expression )
}
}
- JSON
- SQL
{
"$binary": {
"$fn": "add",
"$left": {
"$scalar": 5
},
"$right": {
"$scalar": 3
}
}
}
add(5, 3)
Variadic Functions
A variadic function accepts two or more operands. Typical use cases: fallback, greatest/least, string concatenation.
JSON form:{
"$variadic": {
"$fn": <string>,
"$operands": Array ( Object ( Expression ) )
}
}
- JSON
- SQL
{
"$variadic": {
"$fn": "coalesce",
"$operands": [
{
"$unary": {
"$fn": "sum",
"$operand": {
"$qualifier": {
"$table": "vs"
},
"$identifier": "nb_users"
}
}
},
{
"$scalar": 0
}
]
}
}
COALESCE(SUM("vs"."nb_users"), 0)
Aggregate Functions
Aggregate functions perform a computation over a set of rows and return a single result. Typical use cases: count, average, sum, etc.
{
"$agg": {
"$fn": <string>,
"$distinct": <boolean>,
"$filter": Object ( LogicalExpression ),
"$orderBy": [ Object ( OrderBy ) ],
"$operands": [ Object ( Expression ) ]
}
}
- JSON
- SQL
{
"$agg": {
"$fn": "avg",
"$distinct": false,
"$operands": [
{
"$identifier": "revenue"
}
]
}
}
AVG(revenue)
Window Functions
Window functions compute values across rows that are related to the current row. They do not collapse rows like aggregate functions.
{
"$window": {
"$fn": <string>,
"$orderBy": [ Object ( OrderBy ) ],
"$expressions": [ Object ( Expression ) ],
"$partitionBy": [ Object ( Expression ) ]
}
}
- JSON
- SQL
{
"$window": {
"$fn": "rank",
"$orderBy": [
{
"$expression": {
"$identifier": "score"
},
"$direction": "desc"
}
],
"$partitionBy": [
{
"$identifier": "department"
}
]
}
}
RANK() OVER (PARTITION BY department ORDER BY score DESC)
Example: Embedded Functions
Sometimes, functions can be nested, where one function serves as an operand to another. For instance, a coalesce (variadic) function might use an aggregate like last(...) as one of its arguments.
JSON form:{
"$as": "value",
"$variadic": {
"$fn": "coalesce",
"$operands": [
{
"$agg": {
"$fn": "last",
"$orderBy": [
{
"$expr": {
"$qualifier": {
"$table": "t1"
},
"$identifier": "timestamp"
}
}
],
"$operands": [
{
"$qualifier": {
"$table": "t1"
},
"$identifier": "data"
}
]
}
},
{
"$scalar": 0
}
]
},
"$shortCast": [
"INT"
]
}