Skip to main content

Functions and Operators

Functions

Numeric

FunctionInput TypeReturn TypeDescription
ABS(V)NUMERICNUMERICCalculates the absolute value of V
ROUND(V, D)FLOAT or DECIMALFLOAT or DECIMALRounds a number V with D number with decimals

Aggregation

FunctionInput TypeReturn TypeDescription
AVG(V)NUMERICReturns FLOAT if V is FLOAT otherwise DECIMALCalculates cumulative V executed over multiple input values
COUNT(V)ANYUINTCalculates the cumulative count over multiple values
MAX(V)NUMERIC or DATETIMENUMERIC or DATETIMECalculates maximum over a list of values
MIN(V)NUMERIC or DATETIMENUMERIC or DATETIMECalculates minimum over a list of values
SUM(V)NUMERICNUMERICCalculates the sum over a list of values

String

FunctionInput TypeReturn TypeDesctiption
TRIM([[LEADING or TRAILING or BOTH] char FROM] value)C is STRING, V is TEXT or STRINGTEXT or STRINGTrims multiple leading or trailing characters C from a string V
LEADING Removes characters from the beginning of V
TRAILING Removes characters from the end of V
BOTH Removes characters from the beginning and the end of V
UCASE(V)TEXT or STRINGTEXT or STRINGTransform V into uppercase characters
V is TEXT or STRING
Returns the same type as V

Geospatial

FunctionInput TypeReturn TypeDescription
DISTANCE(A, B, [GEODESIC or HAVERSINE or VINCENTY])A and B are POINTReturns a FLOAT, if A or B are NULL returns NULLGets distance between A and B. By default GEODESIC algorithm is used.

JSON

FunctionInput TypeReturn TypeDescription
JSON_VALUE(expression , path)JSON, STRINGSCALARExtracts a scalar value from a JSON string. The expression is a JSON object and path specifies the item to extract.
JSON_QUERY(expression [ , path ])JSON, STRINGOBJECT or ARRAYExtracts an object or an array from a JSON string. Default path for the query function is $ which means the original JSON.

Here's how you can use these functions in SQL. Given the following JSON:

{
"test_jsonb": {"bar": {"1": 1, "2": 2, "3": 3}, "baz": null, "foo": [1, 2, 3]},
"test_json": [{"digit": 30, "letter": "A"}, {"digit": 31, "letter": "B"}]
}

You can write the following SQL query:

select
JSON_QUERY(test_jsonb,'$.bar') AS bar,
JSON_VALUE(test_jsonb,'$.baz') AS baz,
JSON_QUERY(test_jsonb,'$.foo') AS foo,
JSON_VALUE(test_json,'$[0].letter') AS first_letter,
JSON_QUERY(test_json) AS all
into test_uuid_test
from test_uuid;

select
JSON_QUERY(test_json,'$[*].digit') AS all_digits
into test_uuid_res
from test_uuid;

This would return:

Details
"bar": {
"struct_value": {
"fields": [
{
"key": "1",
"value": {
"number_value": 1
}
},
{
"key": "2",
"value": {
"number_value": 2
}
},
{
"key": "3",
"value": {
"number_value": 3
}
}
]
}
},
"baz": {
"null_value": "NULL_VALUE"
},
"foo": {
"list_value": {
"values": [
{
"number_value": 1
},
{
"number_value": 2
},
{
"number_value": 3
}
]
}
},
"first_letter": {
"list_value": {
"values": [
{
"string_value": "A"
}
]
}
},
"all": {
"list_value": {
"values": [
{
"list_value": {
"values": [
{
"struct_value": {
"fields": [
{
"key": "letter",
"value": {
"string_value": "A"
}
},
{
"key": "digit",
"value": {
"number_value": 30
}
}
]
}
},
{
"struct_value": {
"fields": [
{
"key": "letter",
"value": {
"string_value": "B"
}
},
{
"key": "digit",
"value": {
"number_value": 31
}
}
]
}
}
]
}
}
]
}
}

"all_digits": {
"list_value": {
"values": [
{
"number_value": 30
},
{
"number_value": 31
}
]
}
}

Operators

Comparison

OperatorData Types & UsageDescription
<NUMERIC < NUMERICBOOLEAN
TEXTUAL < TEXTUALBOOLEAN
DATE/TIME < DATE/TIMEBOOLEAN
Returns TRUE if the first operand is smaller than the second operand, otherwise FALSE
>NUMERIC < NUMERICBOOLEAN
TEXTUAL < TEXTUALBOOLEAN
DATE/TIME < DATE/TIMEBOOLEAN
Returns TRUE if the first operand is bigger than the second operand, otherwise FALSE
=ANY = ANYBOOLEANReturns TRUE if the first operand is equal to the second operand, otherwise FALSE
!=ANY != ANYBOOLEANReturns TRUE if teh first operand is not equal to the second operand, otherwise FALSE
<=NUMERIC < NUMERICBOOLEAN
TEXTUAL < TEXTUALBOOLEAN
DATE/TIME < DATE/TIMEBOOLEAN
Returns TRUE if the first operand is smaller or equal than the second operand, otherwise FALSE
>=NUMERIC < NUMERICBOOLEAN
TEXTUAL < TEXTUALBOOLEAN
DATE/TIME < DATE/TIMEBOOLEAN
Returns TRUE if the first operand is bigger or equal than the second operand, otherwise FALSE

Logical

OperatorData Types & UsageDescription
ANDBOOLEAN AND BOOLEANBOOLEANReturns TRUE if both operans are TRUE, otherwise FALSE
ORBOOLEAN OR BOOLEANBOOLEANReturns TRUE any of the two oiperants is TRUE, otherwise FALSE
NOTNOT BOOLEANBOOLEANReturns TRUE if operand is FALSE, otherwise FALSE

Mathematical

OperatorData TypesDescription
+NUMERIC + NUMERICNUMERICSums two operand
-NUMERIC - NUMERICNUMERICSubstracts the second operand from the first one and returns the difference
*NUMERIC * NUMERICNUMERICMultiplies two operands
/UINT / NUMERICFLOAT
INT / NUMERICFLOAT
FLOAT / NUMERICFLOAT
DECIMAL / NUMERICDECIMAL
Divides the first operands by the second operand and returns teh result
%NUMERIC % NUMERICNUMERICCalculate the modulo between two operands