layout: docs title: SQL language permalink: /docs/reference.html

The page describes the SQL dialect recognized by Calcite's default SQL parser.

Grammar

SQL grammar in BNF-like form.

{% highlight sql %} statement: setStatement | resetStatement | explain | describe | insert | update | merge | delete | query

statementList: statement [ ‘;’ statement ]* [ ‘;’ ]

setStatement: [ ALTER { SYSTEM | SESSION } ] SET identifier ‘=’ expression

resetStatement: [ ALTER { SYSTEM | SESSION } ] RESET identifier | [ ALTER { SYSTEM | SESSION } ] RESET ALL

explain: EXPLAIN PLAN [ WITH TYPE | WITH IMPLEMENTATION | WITHOUT IMPLEMENTATION ] [ EXCLUDING ATTRIBUTES | INCLUDING [ ALL ] ATTRIBUTES ] [ AS JSON | AS XML | AS DOT ] FOR { query | insert | update | merge | delete }

describe: DESCRIBE DATABASE databaseName | DESCRIBE CATALOG [ databaseName . ] catalogName | DESCRIBE SCHEMA [ [ databaseName . ] catalogName ] . schemaName | DESCRIBE [ TABLE ] [ [ [ databaseName . ] catalogName . ] schemaName . ] tableName [ columnName ] | DESCRIBE [ STATEMENT ] { query | insert | update | merge | delete }

insert: { INSERT | UPSERT } INTO tablePrimary [ ‘(’ column [, column ]* ‘)’ ] query

update: UPDATE tablePrimary SET assign [, assign ]* [ WHERE booleanExpression ]

assign: identifier ‘=’ expression

merge: MERGE INTO tablePrimary [ [ AS ] alias ] USING tablePrimary ON booleanExpression [ WHEN MATCHED THEN UPDATE SET assign [, assign ]* ] [ WHEN NOT MATCHED THEN INSERT VALUES ‘(’ value [ , value ]* ‘)’ ]

delete: DELETE FROM tablePrimary [ [ AS ] alias ] [ WHERE booleanExpression ]

query: values | WITH [ RECURSIVE ] withItem [ , withItem ]* query | { select | selectWithoutFrom | query UNION [ ALL | DISTINCT ] query | query EXCEPT [ ALL | DISTINCT ] query | query MINUS [ ALL | DISTINCT ] query | query INTERSECT [ ALL | DISTINCT ] query } [ ORDER BY orderItem [, orderItem ]* ] [ LIMIT [ start, ] { count | ALL } ] [ OFFSET start { ROW | ROWS } ] [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]

withItem: name [ ‘(’ column [, column ]* ‘)’ ] AS ‘(’ query ‘)’

orderItem: expression [ ASC | DESC ] [ NULLS FIRST | NULLS LAST ]

select: SELECT [ hintComment ] [ STREAM ] [ ALL | DISTINCT ] { * | projectItem [, projectItem ]* } FROM tableExpression [ WHERE booleanExpression ] [ GROUP BY [ ALL | DISTINCT ] { groupItem [, groupItem ]* } ] [ HAVING booleanExpression ] [ WINDOW windowName AS windowSpec [, windowName AS windowSpec ]* ] [ QUALIFY booleanExpression ]

selectWithoutFrom: SELECT [ ALL | DISTINCT ] { * | projectItem [, projectItem ]* }

projectItem: expression [ [ AS ] columnAlias ] | tableAlias . *

tableExpression: tableReference [, tableReference ]* | tableExpression [ NATURAL ] [ { LEFT | RIGHT | FULL } [ OUTER ] ] JOIN tableExpression [ joinCondition ] | tableExpression CROSS JOIN tableExpression | tableExpression [ CROSS | OUTER ] APPLY tableExpression

joinCondition: ON booleanExpression | USING ‘(’ column [, column ]* ‘)’

tableReference: tablePrimary [ FOR SYSTEM_TIME AS OF expression ] [ pivot ] [ unpivot ] [ matchRecognize ] [ [ AS ] alias [ ‘(’ columnAlias [, columnAlias ]* ‘)’ ] ]

tablePrimary: [ [ catalogName . ] schemaName . ] tableName ‘(’ TABLE [ [ catalogName . ] schemaName . ] tableName ‘)’ | tablePrimary [ hintComment ] [ EXTEND ] ‘(’ columnDecl [, columnDecl ]* ‘)’ | [ LATERAL ] ‘(’ query ‘)’ | UNNEST ‘(’ expression ‘)’ [ WITH ORDINALITY ] | [ LATERAL ] TABLE ‘(’ [ SPECIFIC ] functionName ‘(’ expression [, expression ]* ‘)’ ‘)’

columnDecl: column type [ NOT NULL ]

hint: hintName | hintName ‘(’ hintOptions ‘)’

hintOptions: hintKVOption [, hintKVOption ]* | optionName [, optionName ]* | optionValue [, optionValue ]*

hintKVOption: optionName ‘=’ stringLiteral | stringLiteral ‘=’ stringLiteral

optionValue: stringLiteral | numericLiteral

columnOrList: column | ‘(’ column [, column ]* ‘)’

exprOrList: expr | ‘(’ expr [, expr ]* ‘)’

pivot: PIVOT ‘(’ pivotAgg [, pivotAgg ]* FOR pivotList IN ‘(’ pivotExpr [, pivotExpr ]* ‘)’ ‘)’

pivotAgg: agg ‘(’ [ ALL | DISTINCT ] value [, value ]* ‘)’ [ [ AS ] alias ]

pivotList: columnOrList

pivotExpr: exprOrList [ [ AS ] alias ]

unpivot: UNPIVOT [ INCLUDING NULLS | EXCLUDING NULLS ] ‘(’ unpivotMeasureList FOR unpivotAxisList IN ‘(’ unpivotValue [, unpivotValue ]* ‘)’ ‘)’

unpivotMeasureList: columnOrList

unpivotAxisList: columnOrList

unpivotValue: column [ AS literal ] | ‘(’ column [, column ]* ‘)’ [ AS ‘(’ literal [, literal ]* ‘)’ ]

values: { VALUES | VALUE } expression [, expression ]*

groupItem: expression | ‘(’ ‘)’ | ‘(’ expression [, expression ]* ‘)’ | CUBE ‘(’ expression [, expression ]* ‘)’ | ROLLUP ‘(’ expression [, expression ]* ‘)’ | GROUPING SETS ‘(’ groupItem [, groupItem ]* ‘)’

window: windowName | windowSpec

windowSpec: ‘(’ [ windowName ] [ ORDER BY orderItem [, orderItem ]* ] [ PARTITION BY expression [, expression ]* ] [ RANGE numericOrIntervalExpression { PRECEDING | FOLLOWING } | ROWS numericExpression { PRECEDING | FOLLOWING } ] ‘)’ {% endhighlight %}

In insert, if the INSERT or UPSERT statement does not specify a list of target columns, the query must have the same number of columns as the target table, except in certain [conformance levels]({{ site.apiRoot }}/org/apache/calcite/sql/validate/SqlConformance.html#isInsertSubsetColumnsAllowed--).

In merge, at least one of the WHEN MATCHED and WHEN NOT MATCHED clauses must be present.

tablePrimary may only contain an EXTEND clause in certain [conformance levels]({{ site.apiRoot }}/org/apache/calcite/sql/validate/SqlConformance.html#allowExtend--); in those same conformance levels, any column in insert may be replaced by columnDecl, which has a similar effect to including it in an EXTEND clause.

In orderItem, if expression is a positive integer n, it denotes the nth item in the SELECT clause.

In query, count and start may each be either an unsigned integer literal or a dynamic parameter whose value is an integer.

An aggregate query is a query that contains a GROUP BY or a HAVING clause, or aggregate functions in the SELECT clause. In the SELECT, HAVING and ORDER BY clauses of an aggregate query, all expressions must be constant within the current group (that is, grouping constants as defined by the GROUP BY clause, or constants), or aggregate functions, or a combination of constants and aggregate functions. Aggregate and grouping functions may only appear in an aggregate query, and only in a SELECT, HAVING or ORDER BY clause.

A scalar sub-query is a sub-query used as an expression. If the sub-query returns no rows, the value is NULL; if it returns more than one row, it is an error.

IN, EXISTS, UNIQUE and scalar sub-queries can occur in any place where an expression can occur (such as the SELECT clause, WHERE clause, ON clause of a JOIN, or as an argument to an aggregate function).

An IN, EXISTS, UNIQUE or scalar sub-query may be correlated; that is, it may refer to tables in the FROM clause of an enclosing query.

GROUP BY DISTINCT removes duplicate grouping sets (for example, “GROUP BY DISTINCT GROUPING SETS ((a), (a, b), (a))” is equivalent to “GROUP BY GROUPING SETS ((a), (a, b))”); GROUP BY ALL is equivalent to GROUP BY.

selectWithoutFrom is equivalent to VALUES, but is not standard SQL and is only allowed in certain [conformance levels]({{ site.apiRoot }}/org/apache/calcite/sql/validate/SqlConformance.html#isFromRequired--).

MINUS is equivalent to EXCEPT, but is not standard SQL and is only allowed in certain [conformance levels]({{ site.apiRoot }}/org/apache/calcite/sql/validate/SqlConformance.html#isMinusAllowed--).

CROSS APPLY and OUTER APPLY are only allowed in certain [conformance levels]({{ site.apiRoot }}/org/apache/calcite/sql/validate/SqlConformance.html#isApplyAllowed--).

“LIMIT start, count” is equivalent to “LIMIT count OFFSET start” but is only allowed in certain [conformance levels]({{ site.apiRoot }}/org/apache/calcite/sql/validate/SqlConformance.html#isLimitStartCountAllowed--).

“OFFSET start” may occur before “LIMIT count” in certain [conformance levels]({{ site.apiRoot }}/org/apache/calcite/sql/validate/SqlConformance.html#isOffsetLimitAllowed--).

VALUE is equivalent to VALUES, but is not standard SQL and is only allowed in certain [conformance levels]({{ site.apiRoot }}/org/apache/calcite/sql/validate/SqlConformance.html#isValueAllowed--).

Keywords

The following is a list of SQL keywords. Reserved keywords are bold.

{% comment %} start {% endcomment %} A, ABS, ABSENT, ABSOLUTE, ACTION, ADA, ADD, ADMIN, AFTER, ALL, ALLOCATE, ALLOW, ALTER, ALWAYS, AND, ANY, APPLY, ARE, ARRAY, ARRAY_AGG, ARRAY_CONCAT_AGG, ARRAY_MAX_CARDINALITY, AS, ASC, ASENSITIVE, ASSERTION, ASSIGNMENT, ASYMMETRIC, AT, ATOMIC, ATTRIBUTE, ATTRIBUTES, AUTHORIZATION, AVG, BEFORE, BEGIN, BEGIN_FRAME, BEGIN_PARTITION, BERNOULLI, BETWEEN, BIGINT, BINARY, BIT, BLOB, BOOLEAN, BOTH, BREADTH, BY, C, CALL, CALLED, CARDINALITY, CASCADE, CASCADED, CASE, CAST, CATALOG, CATALOG_NAME, CEIL, CEILING, CENTURY, CHAIN, CHAR, CHARACTER, CHARACTERISTICS, CHARACTERS, CHARACTER_LENGTH, CHARACTER_SET_CATALOG, CHARACTER_SET_NAME, CHARACTER_SET_SCHEMA, CHAR_LENGTH, CHECK, CLASSIFIER, CLASS_ORIGIN, CLOB, CLOSE, COALESCE, COBOL, COLLATE, COLLATION, COLLATION_CATALOG, COLLATION_NAME, COLLATION_SCHEMA, COLLECT, COLUMN, COLUMN_NAME, COMMAND_FUNCTION, COMMAND_FUNCTION_CODE, COMMIT, COMMITTED, CONDITION, CONDITIONAL, CONDITION_NUMBER, CONNECT, CONNECTION, CONNECTION_NAME, CONSTRAINT, CONSTRAINTS, CONSTRAINT_CATALOG, CONSTRAINT_NAME, CONSTRAINT_SCHEMA, CONSTRUCTOR, CONTAINS, CONTAINS_SUBSTR, CONTINUE, CONVERT, CORR, CORRESPONDING, COUNT, COVAR_POP, COVAR_SAMP, CREATE, CROSS, CUBE, CUME_DIST, CURRENT, CURRENT_CATALOG, CURRENT_DATE, CURRENT_DEFAULT_TRANSFORM_GROUP, CURRENT_PATH, CURRENT_ROLE, CURRENT_ROW, CURRENT_SCHEMA, CURRENT_TIME, CURRENT_TIMESTAMP, CURRENT_TRANSFORM_GROUP_FOR_TYPE, CURRENT_USER, CURSOR, CURSOR_NAME, CYCLE, DATA, DATABASE, DATE, DATETIME, DATETIME_DIFF, DATETIME_INTERVAL_CODE, DATETIME_INTERVAL_PRECISION, DATETIME_TRUNC, DATE_DIFF, DATE_TRUNC, DAY, DAYOFWEEK, DAYOFYEAR, DAYS, DEALLOCATE, DEC, DECADE, DECIMAL, DECLARE, DEFAULT, DEFAULTS, DEFERRABLE, DEFERRED, DEFINE, DEFINED, DEFINER, DEGREE, DELETE, DENSE_RANK, DEPTH, DEREF, DERIVED, DESC, DESCRIBE, DESCRIPTION, DESCRIPTOR, DETERMINISTIC, DIAGNOSTICS, DISALLOW, DISCONNECT, DISPATCH, DISTINCT, DOMAIN, DOT, DOUBLE, DOW, DOY, DROP, DYNAMIC, DYNAMIC_FUNCTION, DYNAMIC_FUNCTION_CODE, EACH, ELEMENT, ELSE, EMPTY, ENCODING, END, END-EXEC, END_FRAME, END_PARTITION, EPOCH, EQUALS, ERROR, ESCAPE, EVERY, EXCEPT, EXCEPTION, EXCLUDE, EXCLUDING, EXEC, EXECUTE, EXISTS, EXP, EXPLAIN, EXTEND, EXTERNAL, EXTRACT, FALSE, FETCH, FILTER, FINAL, FIRST, FIRST_VALUE, FLOAT, FLOOR, FOLLOWING, FOR, FOREIGN, FORMAT, FORTRAN, FOUND, FRAC_SECOND, FRAME_ROW, FREE, FRIDAY, FROM, FULL, FUNCTION, FUSION, G, GENERAL, GENERATED, GEOMETRY, GET, GLOBAL, GO, GOTO, GRANT, GRANTED, GROUP, GROUPING, GROUPS, GROUP_CONCAT, HAVING, HIERARCHY, HOLD, HOP, HOUR, HOURS, IDENTITY, IGNORE, ILIKE, IMMEDIATE, IMMEDIATELY, IMPLEMENTATION, IMPORT, IN, INCLUDE, INCLUDING, INCREMENT, INDICATOR, INITIAL, INITIALLY, INNER, INOUT, INPUT, INSENSITIVE, INSERT, INSTANCE, INSTANTIABLE, INT, INTEGER, INTERSECT, INTERSECTION, INTERVAL, INTO, INVOKER, IS, ISODOW, ISOLATION, ISOYEAR, JAVA, JOIN, JSON, JSON_ARRAY, JSON_ARRAYAGG, JSON_EXISTS, JSON_OBJECT, JSON_OBJECTAGG, JSON_QUERY, JSON_SCOPE, JSON_VALUE, K, KEY, KEY_MEMBER, KEY_TYPE, LABEL, LAG, LANGUAGE, LARGE, LAST, LAST_VALUE, LATERAL, LEAD, LEADING, LEFT, LENGTH, LEVEL, LIBRARY, LIKE, LIKE_REGEX, LIMIT, LN, LOCAL, LOCALTIME, LOCALTIMESTAMP, LOCATOR, LOWER, M, MAP, MATCH, MATCHED, MATCHES, MATCH_NUMBER, MATCH_RECOGNIZE, MAX, MAXVALUE, MEASURES, MEMBER, MERGE, MESSAGE_LENGTH, MESSAGE_OCTET_LENGTH, MESSAGE_TEXT, METHOD, MICROSECOND, MILLENNIUM, MILLISECOND, MIN, MINUS, MINUTE, MINUTES, MINVALUE, MOD, MODIFIES, MODULE, MONDAY, MONTH, MONTHS, MORE, MULTISET, MUMPS, NAME, NAMES, NANOSECOND, NATIONAL, NATURAL, NCHAR, NCLOB, NESTING, NEW, NEXT, NO, NONE, NORMALIZE, NORMALIZED, NOT, NTH_VALUE, NTILE, NULL, NULLABLE, NULLIF, NULLS, NUMBER, NUMERIC, OBJECT, OCCURRENCES_REGEX, OCTETS, OCTET_LENGTH, OF, OFFSET, OLD, OMIT, ON, ONE, ONLY, OPEN, OPTION, OPTIONS, OR, ORDER, ORDERING, ORDINAL, ORDINALITY, OTHERS, OUT, OUTER, OUTPUT, OVER, OVERLAPS, OVERLAY, OVERRIDING, PAD, PARAMETER, PARAMETER_MODE, PARAMETER_NAME, PARAMETER_ORDINAL_POSITION, PARAMETER_SPECIFIC_CATALOG, PARAMETER_SPECIFIC_NAME, PARAMETER_SPECIFIC_SCHEMA, PARTIAL, PARTITION, PASCAL, PASSING, PASSTHROUGH, PAST, PATH, PATTERN, PER, PERCENT, PERCENTILE_CONT, PERCENTILE_DISC, PERCENT_RANK, PERIOD, PERMUTE, PIVOT, PLACING, PLAN, PLI, PORTION, POSITION, POSITION_REGEX, POWER, PRECEDES, PRECEDING, PRECISION, PREPARE, PRESERVE, PREV, PRIMARY, PRIOR, PRIVILEGES, PROCEDURE, PUBLIC, QUALIFY, QUARTER, QUARTERS, RANGE, RANK, READ, READS, REAL, RECURSIVE, REF, REFERENCES, REFERENCING, REGR_AVGX, REGR_AVGY, REGR_COUNT, REGR_INTERCEPT, REGR_R2, REGR_SLOPE, REGR_SXX, REGR_SXY, REGR_SYY, RELATIVE, RELEASE, REPEATABLE, REPLACE, RESET, RESPECT, RESTART, RESTRICT, RESULT, RETURN, RETURNED_CARDINALITY, RETURNED_LENGTH, RETURNED_OCTET_LENGTH, RETURNED_SQLSTATE, RETURNING, RETURNS, REVOKE, RIGHT, RLIKE, ROLE, ROLLBACK, ROLLUP, ROUTINE, ROUTINE_CATALOG, ROUTINE_NAME, ROUTINE_SCHEMA, ROW, ROWS, ROW_COUNT, ROW_NUMBER, RUNNING, SAFE_CAST, SAFE_OFFSET, SAFE_ORDINAL, SATURDAY, SAVEPOINT, SCALAR, SCALE, SCHEMA, SCHEMA_NAME, SCOPE, SCOPE_CATALOGS, SCOPE_NAME, SCOPE_SCHEMA, SCROLL, SEARCH, SECOND, SECONDS, SECTION, SECURITY, SEEK, SELECT, SELF, SENSITIVE, SEPARATOR, SEQUENCE, SERIALIZABLE, SERVER, SERVER_NAME, SESSION, SESSION_USER, SET, SETS, SHOW, SIMILAR, SIMPLE, SIZE, SKIP, SMALLINT, SOME, SOURCE, SPACE, SPECIFIC, SPECIFICTYPE, SPECIFIC_NAME, SQL, SQLEXCEPTION, SQLSTATE, SQLWARNING, SQL_BIGINT, SQL_BINARY, SQL_BIT, SQL_BLOB, SQL_BOOLEAN, SQL_CHAR, SQL_CLOB, SQL_DATE, SQL_DECIMAL, SQL_DOUBLE, SQL_FLOAT, SQL_INTEGER, SQL_INTERVAL_DAY, SQL_INTERVAL_DAY_TO_HOUR, SQL_INTERVAL_DAY_TO_MINUTE, SQL_INTERVAL_DAY_TO_SECOND, SQL_INTERVAL_HOUR, SQL_INTERVAL_HOUR_TO_MINUTE, SQL_INTERVAL_HOUR_TO_SECOND, SQL_INTERVAL_MINUTE, SQL_INTERVAL_MINUTE_TO_SECOND, SQL_INTERVAL_MONTH, SQL_INTERVAL_SECOND, SQL_INTERVAL_YEAR, SQL_INTERVAL_YEAR_TO_MONTH, SQL_LONGVARBINARY, SQL_LONGVARCHAR, SQL_LONGVARNCHAR, SQL_NCHAR, SQL_NCLOB, SQL_NUMERIC, SQL_NVARCHAR, SQL_REAL, SQL_SMALLINT, SQL_TIME, SQL_TIMESTAMP, SQL_TINYINT, SQL_TSI_DAY, SQL_TSI_FRAC_SECOND, SQL_TSI_HOUR, SQL_TSI_MICROSECOND, SQL_TSI_MINUTE, SQL_TSI_MONTH, SQL_TSI_QUARTER, SQL_TSI_SECOND, SQL_TSI_WEEK, SQL_TSI_YEAR, SQL_VARBINARY, SQL_VARCHAR, SQRT, START, STATE, STATEMENT, STATIC, STDDEV_POP, STDDEV_SAMP, STREAM, STRING_AGG, STRUCTURE, STYLE, SUBCLASS_ORIGIN, SUBMULTISET, SUBSET, SUBSTITUTE, SUBSTRING, SUBSTRING_REGEX, SUCCEEDS, SUM, SUNDAY, SYMMETRIC, SYSTEM, SYSTEM_TIME, SYSTEM_USER, TABLE, TABLESAMPLE, TABLE_NAME, TEMPORARY, THEN, THURSDAY, TIES, TIME, TIMESTAMP, TIMESTAMPADD, TIMESTAMPDIFF, TIMESTAMP_DIFF, TIMESTAMP_TRUNC, TIMEZONE_HOUR, TIMEZONE_MINUTE, TIME_DIFF, TIME_TRUNC, TINYINT, TO, TOP_LEVEL_COUNT, TRAILING, TRANSACTION, TRANSACTIONS_ACTIVE, TRANSACTIONS_COMMITTED, TRANSACTIONS_ROLLED_BACK, TRANSFORM, TRANSFORMS, TRANSLATE, TRANSLATE_REGEX, TRANSLATION, TREAT, TRIGGER, TRIGGER_CATALOG, TRIGGER_NAME, TRIGGER_SCHEMA, TRIM, TRIM_ARRAY, TRUE, TRUNCATE, TRY_CAST, TUESDAY, TUMBLE, TYPE, UESCAPE, UNBOUNDED, UNCOMMITTED, UNCONDITIONAL, UNDER, UNION, UNIQUE, UNKNOWN, UNNAMED, UNNEST, UNPIVOT, UPDATE, UPPER, UPSERT, USAGE, USER, USER_DEFINED_TYPE_CATALOG, USER_DEFINED_TYPE_CODE, USER_DEFINED_TYPE_NAME, USER_DEFINED_TYPE_SCHEMA, USING, UTF16, UTF32, UTF8, VALUE, VALUES, VALUE_OF, VARBINARY, VARCHAR, VARYING, VAR_POP, VAR_SAMP, VERSION, VERSIONING, VIEW, WEDNESDAY, WEEK, WEEKS, WHEN, WHENEVER, WHERE, WIDTH_BUCKET, WINDOW, WITH, WITHIN, WITHOUT, WORK, WRAPPER, WRITE, XML, YEAR, YEARS, ZONE. {% comment %} end {% endcomment %}

Identifiers

Identifiers are the names of tables, columns and other metadata elements used in a SQL query.

Unquoted identifiers, such as emp, must start with a letter and can only contain letters, digits, and underscores. They are implicitly converted to upper case.

Quoted identifiers, such as "Employee Name", start and end with double quotes. They may contain virtually any character, including spaces and other punctuation. If you wish to include a double quote in an identifier, use another double quote to escape it, like this: "An employee called ""Fred"".".

In Calcite, matching identifiers to the name of the referenced object is case-sensitive. But remember that unquoted identifiers are implicitly converted to upper case before matching, and if the object it refers to was created using an unquoted identifier for its name, then its name will have been converted to upper case also.

Data types

Scalar types

Data typeDescriptionRange and example literals
BOOLEANLogical valuesValues: TRUE, FALSE, UNKNOWN
TINYINT1 byte signed integerRange is -128 to 127
SMALLINT2 byte signed integerRange is -32768 to 32767
INTEGER, INT4 byte signed integerRange is -2147483648 to 2147483647
BIGINT8 byte signed integerRange is -9223372036854775808 to 9223372036854775807
DECIMAL(p, s)Fixed pointExample: 123.45 and DECIMAL ‘123.45’ are identical values, and have type DECIMAL(5, 2)
NUMERIC(p, s)Fixed pointA synonym for DECIMAL
REAL4 byte floating point6 decimal digits precision; examples: CAST(1.2 AS REAL), CAST(‘Infinity’ AS REAL)
DOUBLE8 byte floating point15 decimal digits precision; examples: 1.4E2, CAST(‘-Infinity’ AS DOUBLE), CAST(‘NaN’ AS DOUBLE)
FLOAT8 byte floating pointA synonym for DOUBLE
CHAR(n), CHARACTER(n)Fixed-width character string‘Hello’, '' (empty string), _latin1‘Hello’, n‘Hello’, _UTF16‘Hello’, ‘Hello’ ‘there’ (literal split into multiple parts), e‘Hello\nthere’ (literal containing C-style escapes)
VARCHAR(n), CHARACTER VARYING(n)Variable-length character stringAs CHAR(n)
BINARY(n)Fixed-width binary stringx‘45F0AB’, x'' (empty binary string), x‘AB’ ‘CD’ (multi-part binary string literal)
VARBINARY(n), BINARY VARYING(n)Variable-length binary stringAs BINARY(n)
DATEDateExample: DATE ‘1969-07-20’
TIMETime of dayExample: TIME ‘20:17:40’
TIME WITH LOCAL TIME ZONETime of day with local time zoneExample: TIME WITH LOCAL TIME ZONE ‘20:17:40’
TIME WITH TIME ZONETime of day with time zoneExample: TIME ‘20:17:40 GMT+08’
TIMESTAMP [ WITHOUT TIME ZONE ]Date and timeExample: TIMESTAMP ‘1969-07-20 20:17:40’
TIMESTAMP WITH LOCAL TIME ZONEDate and time with local time zoneExample: TIMESTAMP WITH LOCAL TIME ZONE ‘1969-07-20 20:17:40’
TIMESTAMP WITH TIME ZONEDate and time with time zoneExample: TIMESTAMP WITH TIME ZONE ‘1969-07-20 20:17:40 America/Los Angeles’
INTERVAL timeUnit [ TO timeUnit ]Date time intervalExamples: INTERVAL ‘1-5’ YEAR TO MONTH, INTERVAL ‘45’ DAY, INTERVAL ‘1 2:34:56.789’ DAY TO SECOND
GEOMETRYGeometryExamples: ST_GeomFromText(‘POINT (30 10)’)

Where:

{% highlight sql %} timeUnit: MILLENNIUM | CENTURY | DECADE | YEAR | QUARTER | MONTH | WEEK | DOY | DOW | DAY | HOUR | MINUTE | SECOND | EPOCH {% endhighlight %}

Note:

  • DATE, TIME and TIMESTAMP have no time zone. For those types, there is not even an implicit time zone, such as UTC (as in Java) or the local time zone. It is left to the user or application to supply a time zone. In turn, TIMESTAMP WITH LOCAL TIME ZONE does not store the time zone internally, but it will rely on the supplied time zone to provide correct semantics.
  • GEOMETRY is allowed only in certain [conformance levels]({{ site.apiRoot }}/org/apache/calcite/sql/validate/SqlConformance.html#allowGeometry--).
  • Interval literals may only use time units YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE and SECOND. In certain [conformance levels]({{ site.apiRoot }}/org/apache/calcite/sql/validate/SqlConformance.html#allowPluralTimeUnits--), we also allow their plurals, YEARS, QUARTERS, MONTHS, WEEKS, DAYS, HOURS, MINUTES and SECONDS.

Non-scalar types

TypeDescriptionExample literals
ANYThe union of all types
UNKNOWNA value of an unknown type; used as a placeholder
ROWRow with 1 or more columnsExample: row(f0 int null, f1 varchar)
MAPCollection of keys mapped to valuesExample: (int, varchar) map
MULTISETUnordered collection that may contain duplicatesExample: int multiset
ARRAYOrdered, contiguous collection that may contain duplicatesExample: varchar(10) array
CURSORCursor over the result of executing a query
FUNCTIONA function definition that is not bound to an identifier, it is not fully supported in CAST or DDLExample FUNCTION(INTEGER, VARCHAR(30)) -> INTEGER

Note:

  • Every ROW column type can have an optional [ NULL | NOT NULL ] suffix to indicate if this column type is nullable, default is not nullable.

Spatial types

Spatial data is represented as character strings encoded as well-known text (WKT) or binary strings encoded as well-known binary (WKB).

Where you would use a literal, apply the ST_GeomFromText function, for example ST_GeomFromText('POINT (30 10)').

Data typeType codeExamples in WKT
GEOMETRY0generalization of Point, Curve, Surface, GEOMETRYCOLLECTION
POINT1ST_GeomFromText(​‘POINT (30 10)’) is a point in 2D space; ST_GeomFromText(​‘POINT Z(30 10 2)’) is point in 3D space
CURVE13generalization of LINESTRING
LINESTRING2ST_GeomFromText(​‘LINESTRING (30 10, 10 30, 40 40)’)
SURFACE14generalization of Polygon, PolyhedralSurface
POLYGON3ST_GeomFromText(​‘POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))’) is a pentagon; ST_GeomFromText(​‘POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10), (20 30, 35 35, 30 20, 20 30))’) is a pentagon with a quadrilateral hole
POLYHEDRALSURFACE15
GEOMETRYCOLLECTION7a collection of zero or more GEOMETRY instances; a generalization of MULTIPOINT, MULTILINESTRING, MULTIPOLYGON
MULTIPOINT4ST_GeomFromText(​‘MULTIPOINT ((10 40), (40 30), (20 20), (30 10))’) is equivalent to ST_GeomFromText(​‘MULTIPOINT (10 40, 40 30, 20 20, 30 10)’)
MULTICURVE-generalization of MULTILINESTRING
MULTILINESTRING5ST_GeomFromText(​‘MULTILINESTRING ((10 10, 20 20, 10 40), (40 40, 30 30, 40 20, 30 10))’)
MULTISURFACE-generalization of MULTIPOLYGON
MULTIPOLYGON6ST_GeomFromText(​‘MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5)))’)

Operators and functions

Operator precedence

The operator precedence and associativity, highest to lowest.

OperatorAssociativity
.left
::left
[ ] (collection element)left
+ - (unary plus, minus)right
* / % ||left
+ -left
BETWEEN, IN, LIKE, SIMILAR, OVERLAPS, CONTAINS etc.-
< > = <= >= <> != <=>left
IS NULL, IS FALSE, IS NOT TRUE etc.-
NOTright
ANDleft
ORleft

Note that ::,<=> is dialect-specific, but is shown in this table for completeness.

Comparison operators

Operator syntaxDescription
value1 = value2Equals
value1 <> value2Not equal
value1 != value2Not equal (only in certain [conformance levels]({{ site.apiRoot }}/org/apache/calcite/sql/validate/SqlConformance.html#isBangEqualAllowed--))
value1 > value2Greater than
value1 >= value2Greater than or equal
value1 < value2Less than
value1 <= value2Less than or equal
value1 <=> value2Whether two values are equal, treating null values as the same
value IS NULLWhether value is null
value IS NOT NULLWhether value is not null
value1 IS DISTINCT FROM value2Whether two values are not equal, treating null values as the same
value1 IS NOT DISTINCT FROM value2Whether two values are equal, treating null values as the same
value1 BETWEEN value2 AND value3Whether value1 is greater than or equal to value2 and less than or equal to value3
value1 NOT BETWEEN value2 AND value3Whether value1 is less than value2 or greater than value3
string1 LIKE string2 [ ESCAPE string3 ]Whether string1 matches pattern string2
string1 NOT LIKE string2 [ ESCAPE string3 ]Whether string1 does not match pattern string2
string1 SIMILAR TO string2 [ ESCAPE string3 ]Whether string1 matches regular expression string2
string1 NOT SIMILAR TO string2 [ ESCAPE string3 ]Whether string1 does not match regular expression string2
value IN (value [, value ]*)Whether value is equal to a value in a list
value NOT IN (value [, value ]*)Whether value is not equal to every value in a list
value IN (sub-query)Whether value is equal to a row returned by sub-query
value NOT IN (sub-query)Whether value is not equal to every row returned by sub-query
value comparison SOME (sub-query or collection)Whether value comparison at least one row returned by sub-query or collection
value comparison ANY (sub-query or collection)Synonym for SOME
value comparison ALL (sub-query or collection)Whether value comparison every row returned by sub-query or collection
EXISTS (sub-query)Whether sub-query returns at least one row
UNIQUE (sub-query)Whether the rows returned by sub-query are unique (ignoring null values)

{% highlight sql %} comp: = | <> | > | >= | < | <= | <=> {% endhighlight %}

Logical operators

Operator syntaxDescription
boolean1 OR boolean2Whether boolean1 is TRUE or boolean2 is TRUE
boolean1 AND boolean2Whether boolean1 and boolean2 are both TRUE
NOT booleanWhether boolean is not TRUE; returns UNKNOWN if boolean is UNKNOWN
boolean IS FALSEWhether boolean is FALSE; returns FALSE if boolean is UNKNOWN
boolean IS NOT FALSEWhether boolean is not FALSE; returns TRUE if boolean is UNKNOWN
boolean IS TRUEWhether boolean is TRUE; returns FALSE if boolean is UNKNOWN
boolean IS NOT TRUEWhether boolean is not TRUE; returns TRUE if boolean is UNKNOWN
boolean IS UNKNOWNWhether boolean is UNKNOWN
boolean IS NOT UNKNOWNWhether boolean is not UNKNOWN

Arithmetic operators and functions

Operator syntaxDescription
+ numericReturns numeric
- numericReturns negative numeric
numeric1 + numeric2Returns numeric1 plus numeric2
numeric1 - numeric2Returns numeric1 minus numeric2
numeric1 * numeric2Returns numeric1 multiplied by numeric2
numeric1 / numeric2Returns numeric1 divided by numeric2
numeric1 % numeric2As MOD(numeric1, numeric2) (only in certain [conformance levels]({{ site.apiRoot }}/org/apache/calcite/sql/validate/SqlConformance.html#isPercentRemainderAllowed--))
POWER(numeric1, numeric2)Returns numeric1 raised to the power of numeric2
ABS(numeric)Returns the absolute value of numeric
MOD(numeric1, numeric2)Returns the remainder (modulus) of numeric1 divided by numeric2. The result is negative only if numeric1 is negative
SQRT(numeric)Returns the square root of numeric
LN(numeric)Returns the natural logarithm (base e) of numeric
LOG10(numeric)Returns the base 10 logarithm of numeric
EXP(numeric)Returns e raised to the power of numeric
CEIL(numeric)Rounds numeric up, returning the smallest integer that is greater than or equal to numeric
FLOOR(numeric)Rounds numeric down, returning the largest integer that is less than or equal to numeric
RAND([seed])Generates a random double between 0 and 1 inclusive, optionally initializing the random number generator with seed
RAND_INTEGER([seed, ] numeric)Generates a random integer between 0 and numeric - 1 inclusive, optionally initializing the random number generator with seed
ACOS(numeric)Returns the arc cosine of numeric
ASIN(numeric)Returns the arc sine of numeric
ATAN(numeric)Returns the arc tangent of numeric
ATAN2(numeric, numeric)Returns the arc tangent of the numeric coordinates
CBRT(numeric)Returns the cube root of numeric
COS(numeric)Returns the cosine of numeric
COT(numeric)Returns the cotangent of numeric
DEGREES(numeric)Converts numeric from radians to degrees
PI()Returns a value that is closer than any other value to pi
RADIANS(numeric)Converts numeric from degrees to radians
ROUND(numeric1 [, integer2])Rounds numeric1 to optionally integer2 (if not specified 0) places right to the decimal point
SIGN(numeric)Returns the signum of numeric
SIN(numeric)Returns the sine of numeric
TAN(numeric)Returns the tangent of numeric
TRUNCATE(numeric1 [, integer2])Truncates numeric1 to optionally integer2 (if not specified 0) places right to the decimal point

Character string operators and functions

Operator syntaxDescription
string || stringConcatenates two character strings
CHAR_LENGTH(string)Returns the number of characters in a character string
CHARACTER_LENGTH(string)As CHAR_LENGTH(string)
UPPER(string)Returns a character string converted to upper case
LOWER(string)Returns a character string converted to lower case
POSITION(substring IN string)Returns the position of the first occurrence of substring in string
POSITION(substring IN string FROM integer)Returns the position of the first occurrence of substring in string starting at a given point (not standard SQL)
TRIM( { BOTH | LEADING | TRAILING } string1 FROM string2)Removes the longest string containing only the characters in string1 from the start/end/both ends of string1
OVERLAY(string1 PLACING string2 FROM integer [ FOR integer2 ])Replaces a substring of string1 with string2
SUBSTRING(string FROM integer)Returns a substring of a character string starting at a given point
SUBSTRING(string FROM integer FOR integer)Returns a substring of a character string starting at a given point with a given length
INITCAP(string)Returns string with the first letter of each word converter to upper case and the rest to lower case. Words are sequences of alphanumeric characters separated by non-alphanumeric characters.

Not implemented:

  • SUBSTRING(string FROM regexp FOR regexp)

Binary string operators and functions

Operator syntaxDescription
binary || binaryConcatenates two binary strings
OCTET_LENGTH(binary)Returns the number of bytes in binary
POSITION(binary1 IN binary2)Returns the position of the first occurrence of binary1 in binary2
POSITION(binary1 IN binary2 FROM integer)Returns the position of the first occurrence of binary1 in binary2 starting at a given point (not standard SQL)
OVERLAY(binary1 PLACING binary2 FROM integer [ FOR integer2 ])Replaces a substring of binary1 with binary2
SUBSTRING(binary FROM integer)Returns a substring of binary starting at a given point
SUBSTRING(binary FROM integer FOR integer)Returns a substring of binary starting at a given point with a given length

Date/time functions

Operator syntaxDescription
LOCALTIMEReturns the current date and time in the session time zone in a value of datatype TIME
LOCALTIME(precision)Returns the current date and time in the session time zone in a value of datatype TIME, with precision digits of precision
LOCALTIMESTAMPReturns the current date and time in the session time zone in a value of datatype TIMESTAMP
LOCALTIMESTAMP(precision)Returns the current date and time in the session time zone in a value of datatype TIMESTAMP, with precision digits of precision
CURRENT_TIMEReturns the current time in the session time zone, in a value of datatype TIMESTAMP WITH TIME ZONE
CURRENT_DATEReturns the current date in the session time zone, in a value of datatype DATE
CURRENT_TIMESTAMPReturns the current date and time in the session time zone, in a value of datatype TIMESTAMP WITH TIME ZONE
EXTRACT(timeUnit FROM datetime)Extracts and returns the value of a specified datetime field from a datetime value expression
FLOOR(datetime TO timeUnit)Rounds datetime down to timeUnit
CEIL(datetime TO timeUnit)Rounds datetime up to timeUnit
YEAR(date)Equivalent to EXTRACT(YEAR FROM date). Returns an integer.
QUARTER(date)Equivalent to EXTRACT(QUARTER FROM date). Returns an integer between 1 and 4.
MONTH(date)Equivalent to EXTRACT(MONTH FROM date). Returns an integer between 1 and 12.
WEEK(date)Equivalent to EXTRACT(WEEK FROM date). Returns an integer between 1 and 53.
DAYOFYEAR(date)Equivalent to EXTRACT(DOY FROM date). Returns an integer between 1 and 366.
DAYOFMONTH(date)Equivalent to EXTRACT(DAY FROM date). Returns an integer between 1 and 31.
DAYOFWEEK(date)Equivalent to EXTRACT(DOW FROM date). Returns an integer between 1 and 7.
HOUR(date)Equivalent to EXTRACT(HOUR FROM date). Returns an integer between 0 and 23.
MINUTE(date)Equivalent to EXTRACT(MINUTE FROM date). Returns an integer between 0 and 59.
SECOND(date)Equivalent to EXTRACT(SECOND FROM date). Returns an integer between 0 and 59.
TIMESTAMPADD(timeUnit, integer, datetime)Returns datetime with an interval of (signed) integer timeUnits added. Equivalent to datetime + INTERVAL 'integer' timeUnit
TIMESTAMPDIFF(timeUnit, datetime, datetime2)Returns the (signed) number of timeUnit intervals between datetime and datetime2. Equivalent to (datetime2 - datetime) timeUnit
LAST_DAY(date)Returns the date of the last day of the month in a value of datatype DATE; For example, it returns DATE‘2020-02-29’ for both DATE‘2020-02-10’ and TIMESTAMP‘2020-02-10 10:10:10’

Calls to niladic functions such as CURRENT_DATE do not accept parentheses in standard SQL. Calls with parentheses, such as CURRENT_DATE() are accepted in certain [conformance levels]({{ site.apiRoot }}/org/apache/calcite/sql/validate/SqlConformance.html#allowNiladicParentheses--).

Not implemented:

  • CEIL(interval)
  • FLOOR(interval)
  • + interval
  • - interval
  • interval + interval
  • interval - interval
  • interval / interval

System functions

Operator syntaxDescription
USEREquivalent to CURRENT_USER
CURRENT_USERUser name of current execution context
SESSION_USERSession user name
SYSTEM_USERReturns the name of the current data store user as identified by the operating system
CURRENT_PATHReturns a character string representing the current lookup scope for references to user-defined routines and types
CURRENT_ROLEReturns the current active role
CURRENT_SCHEMAReturns the current schema

Conditional functions and operators

Operator syntaxDescription
CASE value
WHEN value1 [, value11 ]* THEN result1
[ WHEN valueN [, valueN1 ]* THEN resultN ]*
[ ELSE resultZ ]
END
Simple case
CASE
WHEN condition1 THEN result1
[ WHEN conditionN THEN resultN ]*
[ ELSE resultZ ]
END
Searched case
NULLIF(value, value)Returns NULL if the values are the same.

For example, NULLIF(5, 5) returns NULL; NULLIF(5, 0) returns 5.
COALESCE(value, value [, value ]*)Provides a value if the first value is null.

For example, COALESCE(NULL, 5) returns 5.

Type conversion

Generally an expression cannot contain values of different datatypes. For example, an expression cannot multiply 5 by 10 and then add ‘JULIAN’. However, Calcite supports both implicit and explicit conversion of values from one datatype to another.

Implicit and Explicit Type Conversion

Calcite recommends that you specify explicit conversions, rather than rely on implicit or automatic conversions, for these reasons:

  • SQL statements are easier to understand when you use explicit datatype conversion functions.
  • Implicit datatype conversion can have a negative impact on performance, especially if the datatype of a column value is converted to that of a constant rather than the other way around.
  • Implicit conversion depends on the context in which it occurs and may not work the same way in every case. For example, implicit conversion from a datetime value to a VARCHAR value may return an unexpected format.

Algorithms for implicit conversion are subject to change across Calcite releases. Behavior of explicit conversions is more predictable.

Explicit Type Conversion

Operator syntaxDescription
CAST(value AS type)Converts a value to a given type. Casts between integer types truncate towards 0
CONVERT(string, charSet1, charSet2)Converts string from charSet1 to charSet2
CONVERT(value USING transcodingName)Alter value from one base character set to transcodingName
TRANSLATE(value USING transcodingName)Alter value from one base character set to transcodingName

Supported data types syntax:

{% highlight sql %} type: typeName [ collectionsTypeName ]*

typeName: sqlTypeName | rowTypeName | compoundIdentifier

sqlTypeName: char [ precision ] [ charSet ] | varchar [ precision ] [ charSet ] | DATE | time | timestamp | GEOMETRY | decimal [ precision [, scale] ] | BOOLEAN | integer | BINARY [ precision ] | varbinary [ precision ] | TINYINT | SMALLINT | BIGINT | REAL | double | FLOAT | ANY [ precision [, scale] ]

collectionsTypeName: ARRAY | MULTISET

rowTypeName: ROW ‘(’ fieldName1 fieldType1 [ NULL | NOT NULL ] [ , fieldName2 fieldType2 [ NULL | NOT NULL ] ]* ‘)’

char: CHARACTER | CHAR

varchar: char VARYING | VARCHAR

decimal: DECIMAL | DEC | NUMERIC

integer: INTEGER | INT

varbinary: BINARY VARYING | VARBINARY

double: DOUBLE [ PRECISION ]

time: TIME [ precision ] [ timeZone ]

timestamp: TIMESTAMP [ precision ] [ timeZone ]

charSet: CHARACTER SET charSetName

timeZone: WITHOUT TIME ZONE | WITH LOCAL TIME ZONE {% endhighlight %}

Implicit Type Conversion

Calcite automatically converts a value from one datatype to another when such a conversion makes sense. The table below is a matrix of Calcite type conversions. The table shows all possible conversions, without regard to the context in which it is made. The rules governing these details follow the table.

FROM - TONULLBOOLEANTINYINTSMALLINTINTBIGINTDECIMALFLOAT or REALDOUBLEINTERVALDATETIMETIMESTAMPCHAR or VARCHARBINARY or VARBINARYGEOMETRYARRAY
NULLiiiiiiiiiiiiiiiix
BOOLEANxixxxxxxxxxxxixxx
TINYINTxeiiiiiiiexxeixxx
SMALLINTxeiiiiiiiexxeixxx
INTxeiiiiiiiexxeixxx
BIGINTxeiiiiiiiexxeixxx
DECIMALxeiiiiiiiexxeixxx
FLOAT/REALxeiiiiiiixxxeixxx
DOUBLExeiiiiiiixxxeixxx
INTERVALxxeeeeexxixxxexxx
DATExxxxxxxxxxixiixxx
TIMExxxxxxxxxxxieixxx
TIMESTAMPxxeeeeeeexieiixxx
CHAR or VARCHARxeiiiiiiiiiiiiiii
BINARY or VARBINARYxxxxxxxxxxeeeiixx
GEOMETRYxxxxxxxxxxxxxixix
ARRAYxxxxxxxxxxxxxxxxi

i: implicit cast / e: explicit cast / x: not allowed

Conversion Contexts and Strategies
  • Set operation (UNION, EXCEPT, INTERSECT): compare every branch row data type and find the common type of each fields pair;
  • Binary arithmetic expression (+, -, &, ^, /, %): promote string operand to data type of the other numeric operand;
  • Binary comparison (=, <, <=, <>, >, >=): if operands are STRING and TIMESTAMP, promote to TIMESTAMP; make 1 = true and 0 = false always evaluate to TRUE; if there is numeric type operand, find common type for both operands.
  • IN sub-query: compare type of LHS and RHS, and find the common type; if it is struct type, find wider type for every field;
  • IN expression list: compare every expression to find the common type;
  • CASE WHEN expression or COALESCE: find the common wider type of the THEN and ELSE operands;
  • Character + INTERVAL or character - INTERVAL: promote character to TIMESTAMP;
  • Built-in function: look up the type families registered in the checker, find the family default type if checker rules allow it;
  • User-defined function (UDF): coerce based on the declared argument types of the eval() method;
  • INSERT and UPDATE: coerce a source field to counterpart target table field's type if the two fields differ with type name or precision(scale).

Note:

Implicit type coercion of following cases are ignored:

  • One of the type is ANY;
  • Type coercion within CHARACTER types are always ignored, i.e. from CHAR(20) to VARCHAR(30);
  • Type coercion from a numeric to another with higher precedence is ignored, i.e. from INT to LONG.
Strategies for Finding Common Type
  • If the operator has expected data types, just take them as the desired one. (e.g. the UDF would have eval() method which has reflection argument types);
  • If there is no expected data type but the data type families are registered, try to coerce the arguments to the family's default data type, i.e. the String family will have a VARCHAR type;
  • If neither expected data type nor families are specified, try to find the tightest common type of the node types, i.e. INTEGER and DOUBLE will return DOUBLE, the numeric precision does not lose for this case;
  • If no tightest common type is found, try to find a wider type, i.e. VARCHAR and INTEGER will return INTEGER, we allow some precision loss when widening decimal to fractional, or promote to VARCHAR type.

Value constructors

Operator syntaxDescription
ROW (value [, value ]*)Creates a row from a list of values.
(value [, value ]* )Creates a row from a list of values.
row ‘[’ index ‘]’Returns the element at a particular location in a row (1-based index).
row ‘[’ name ‘]’Returns the element of a row with a particular name.
map ‘[’ key ‘]’Returns the element of a map with a particular key.
array ‘[’ index ‘]’Returns the element at a particular location in an array (1-based index).
ARRAY ‘[’ value [, value ]* ‘]’Creates an array from a list of values.
MAP ‘[’ key, value [, key, value ]* ‘]’Creates a map from a list of key-value pairs.

Value constructors by query

Operator syntaxDescription
ARRAY (sub-query)Creates an array from the result of a sub-query. Example: ARRAY(SELECT empno FROM emp ORDER BY empno)
MAP (sub-query)Creates a map from the result of a key-value pair sub-query. Example: MAP(SELECT empno, deptno FROM emp)
MULTISET (sub-query)Creates a multiset from the result of a sub-query. Example: MULTISET(SELECT empno FROM emp)

Collection functions

Operator syntaxDescription
ELEMENT(value)Returns the sole element of an array or multiset; null if the collection is empty; throws if it has more than one element.
CARDINALITY(value)Returns the number of elements in an array or multiset.
value MEMBER OF multisetReturns whether the value is a member of multiset.
multiset IS A SETWhether multiset is a set (has no duplicates).
multiset IS NOT A SETWhether multiset is not a set (has duplicates).
multiset IS EMPTYWhether multiset contains zero elements.
multiset IS NOT EMPTYWhether multiset contains one or more elements.
multiset SUBMULTISET OF multiset2Whether multiset is a submultiset of multiset2.
multiset NOT SUBMULTISET OF multiset2Whether multiset is not a submultiset of multiset2.
multiset MULTISET UNION [ ALL | DISTINCT ] multiset2Returns the union multiset and multiset2, eliminating duplicates if DISTINCT is specified (ALL is the default).
multiset MULTISET INTERSECT [ ALL | DISTINCT ] multiset2Returns the intersection of multiset and multiset2, eliminating duplicates if DISTINCT is specified (ALL is the default).
multiset MULTISET EXCEPT [ ALL | DISTINCT ] multiset2Returns the difference of multiset and multiset2, eliminating duplicates if DISTINCT is specified (ALL is the default).

See also: the UNNEST relational operator converts a collection to a relation.

Period predicates

Where period1 and period2 are period expressions:

{% highlight sql %} period: (datetime, datetime) | (datetime, interval) | PERIOD (datetime, datetime) | PERIOD (datetime, interval) {% endhighlight %}

JDBC function escape

Numeric

Operator syntaxDescription
{fn ABS(numeric)}Returns the absolute value of numeric
{fn ACOS(numeric)}Returns the arc cosine of numeric
{fn ASIN(numeric)}Returns the arc sine of numeric
{fn ATAN(numeric)}Returns the arc tangent of numeric
{fn ATAN2(numeric, numeric)}Returns the arc tangent of the numeric coordinates
{fn CBRT(numeric)}Returns the cube root of numeric
{fn CEILING(numeric)}Rounds numeric up, and returns the smallest number that is greater than or equal to numeric
{fn COS(numeric)}Returns the cosine of numeric
{fn COT(numeric)}Returns the cotangent of numeric
{fn DEGREES(numeric)}Converts numeric from radians to degrees
{fn EXP(numeric)}Returns e raised to the power of numeric
{fn FLOOR(numeric)}Rounds numeric down, and returns the largest number that is less than or equal to numeric
{fn LOG(numeric)}Returns the natural logarithm (base e) of numeric
{fn LOG10(numeric)}Returns the base-10 logarithm of numeric
{fn MOD(numeric1, numeric2)}Returns the remainder (modulus) of numeric1 divided by numeric2. The result is negative only if numeric1 is negative
{fn PI()}Returns a value that is closer than any other value to pi
{fn POWER(numeric1, numeric2)}Returns numeric1 raised to the power of numeric2
{fn RADIANS(numeric)}Converts numeric from degrees to radians
{fn RAND(numeric)}Returns a random double using numeric as the seed value
{fn ROUND(numeric1, integer2)}Rounds numeric1 to integer2 places right to the decimal point
{fn SIGN(numeric)}Returns the signum of numeric
{fn SIN(numeric)}Returns the sine of numeric
{fn SQRT(numeric)}Returns the square root of numeric
{fn TAN(numeric)}Returns the tangent of numeric
{fn TRUNCATE(numeric1, integer2)}Truncates numeric1 to integer2 places right to the decimal point

String

Operator syntaxDescription
{fn ASCII(string)}Returns the ASCII code of the first character of string; if the first character is a non-ASCII character, returns its Unicode code point; returns 0 if string is empty
{fn CHAR(integer)}Returns the character whose ASCII code is integer % 256, or null if integer < 0
{fn CONCAT(character, character)}Returns the concatenation of character strings
{fn INSERT(string1, start, length, string2)}Inserts string2 into a slot in string1
{fn LCASE(string)}Returns a string in which all alphabetic characters in string have been converted to lower case
{fn LENGTH(string)}Returns the number of characters in a string
{fn LOCATE(string1, string2 [, integer])}Returns the position in string2 of the first occurrence of string1. Searches from the beginning of string2, unless integer is specified.
{fn LEFT(string, length)}Returns the leftmost length characters from string
{fn LTRIM(string)}Returns string with leading space characters removed
{fn REPLACE(string, search, replacement)}Returns a string in which all the occurrences of search in string are replaced with replacement; if replacement is the empty string, the occurrences of search are removed
{fn REVERSE(string)}Returns string with the order of the characters reversed
{fn RIGHT(string, length)}Returns the rightmost length characters from string
{fn RTRIM(string)}Returns string with trailing space characters removed
{fn SUBSTRING(string, offset, length)}Returns a character string that consists of length characters from string starting at the offset position
{fn UCASE(string)}Returns a string in which all alphabetic characters in string have been converted to upper case

Date/time

Operator syntaxDescription
{fn CURDATE()}Equivalent to CURRENT_DATE
{fn CURTIME()}Equivalent to LOCALTIME
{fn NOW()}Equivalent to LOCALTIMESTAMP
{fn YEAR(date)}Equivalent to EXTRACT(YEAR FROM date). Returns an integer.
{fn QUARTER(date)}Equivalent to EXTRACT(QUARTER FROM date). Returns an integer between 1 and 4.
{fn MONTH(date)}Equivalent to EXTRACT(MONTH FROM date). Returns an integer between 1 and 12.
{fn WEEK(date)}Equivalent to EXTRACT(WEEK FROM date). Returns an integer between 1 and 53.
{fn DAYOFYEAR(date)}Equivalent to EXTRACT(DOY FROM date). Returns an integer between 1 and 366.
{fn DAYOFMONTH(date)}Equivalent to EXTRACT(DAY FROM date). Returns an integer between 1 and 31.
{fn DAYOFWEEK(date)}Equivalent to EXTRACT(DOW FROM date). Returns an integer between 1 and 7.
{fn HOUR(date)}Equivalent to EXTRACT(HOUR FROM date). Returns an integer between 0 and 23.
{fn MINUTE(date)}Equivalent to EXTRACT(MINUTE FROM date). Returns an integer between 0 and 59.
{fn SECOND(date)}Equivalent to EXTRACT(SECOND FROM date). Returns an integer between 0 and 59.
{fn TIMESTAMPADD(timeUnit, count, datetime)}Adds an interval of count timeUnits to a datetime
{fn TIMESTAMPDIFF(timeUnit, timestamp1, timestamp2)}Subtracts timestamp1 from timestamp2 and returns the result in timeUnits

System

Operator syntaxDescription
{fn DATABASE()}Equivalent to CURRENT_CATALOG
{fn IFNULL(value1, value2)}Returns value2 if value1 is null
{fn USER()}Equivalent to CURRENT_USER

Conversion

Operator syntaxDescription
{fn CONVERT(value, type)}Cast value into type

Aggregate functions

Syntax:

{% highlight sql %} aggregateCall: agg ‘(’ [ ALL | DISTINCT ] value [, value ]* ‘)’ [ WITHIN DISTINCT ‘(’ expression [, expression ]* ‘)’ ] [ WITHIN GROUP ‘(’ ORDER BY orderItem [, orderItem ]* ‘)’ ] [ FILTER ‘(’ WHERE condition ‘)’ ] | agg ‘(’ ‘*’ ‘)’ [ FILTER (WHERE condition) ] {% endhighlight %}

where agg is one of the operators in the following table, or a user-defined aggregate function.

If FILTER is present, the aggregate function only considers rows for which condition evaluates to TRUE.

If DISTINCT is present, duplicate argument values are eliminated before being passed to the aggregate function.

If WITHIN DISTINCT is present, argument values are made distinct within each value of specified keys before being passed to the aggregate function.

If WITHIN GROUP is present, the aggregate function sorts the input rows according to the ORDER BY clause inside WITHIN GROUP before aggregating values. WITHIN GROUP is only allowed for hypothetical set functions (RANK, DENSE_RANK, PERCENT_RANK and CUME_DIST), inverse distribution functions (PERCENTILE_CONT and PERCENTILE_DISC) and collection functions (COLLECT and LISTAGG).

Operator syntaxDescription
ANY_VALUE( [ ALL | DISTINCT ] value)Returns one of the values of value across all input values; this is NOT specified in the SQL standard
ARG_MAX(value, comp)Returns value for the maximum value of comp in the group
ARG_MIN(value, comp)Returns value for the minimum value of comp in the group
APPROX_COUNT_DISTINCT(value [, value ]*)Returns the approximate number of distinct values of value; the database is allowed to use an approximation but is not required to
AVG( [ ALL | DISTINCT ] numeric)Returns the average (arithmetic mean) of numeric across all input values
BIT_AND( [ ALL | DISTINCT ] value)Returns the bitwise AND of all non-null input values, or null if none; integer and binary types are supported
BIT_OR( [ ALL | DISTINCT ] value)Returns the bitwise OR of all non-null input values, or null if none; integer and binary types are supported
BIT_XOR( [ ALL | DISTINCT ] value)Returns the bitwise XOR of all non-null input values, or null if none; integer and binary types are supported
COLLECT( [ ALL | DISTINCT ] value)Returns a multiset of the values
COUNT(*)Returns the number of input rows
COUNT( [ ALL | DISTINCT ] value [, value ]*)Returns the number of input rows for which value is not null (wholly not null if value is composite)
COVAR_POP(numeric1, numeric2)Returns the population covariance of the pair (numeric1, numeric2) across all input values
COVAR_SAMP(numeric1, numeric2)Returns the sample covariance of the pair (numeric1, numeric2) across all input values
EVERY(condition)Returns TRUE if all of the values of condition are TRUE
FUSION(multiset)Returns the multiset union of multiset across all input values
INTERSECTION(multiset)Returns the multiset intersection of multiset across all input values
LISTAGG( [ ALL | DISTINCT ] value [, separator])Returns values concatenated into a string, delimited by separator (default ‘,’)
MAX( [ ALL | DISTINCT ] value)Returns the maximum value of value across all input values
MIN( [ ALL | DISTINCT ] value)Returns the minimum value of value across all input values
MODE(value)Returns the most frequent value of value across all input values
REGR_COUNT(numeric1, numeric2)Returns the number of rows where both dependent and independent expressions are not null
REGR_SXX(numeric1, numeric2)Returns the sum of squares of the dependent expression in a linear regression model
REGR_SYY(numeric1, numeric2)Returns the sum of squares of the independent expression in a linear regression model
SOME(condition)Returns TRUE if one or more of the values of condition is TRUE
STDDEV( [ ALL | DISTINCT ] numeric)Synonym for STDDEV_SAMP
STDDEV_POP( [ ALL | DISTINCT ] numeric)Returns the population standard deviation of numeric across all input values
STDDEV_SAMP( [ ALL | DISTINCT ] numeric)Returns the sample standard deviation of numeric across all input values
SUM( [ ALL | DISTINCT ] numeric)Returns the sum of numeric across all input values
VAR_POP( [ ALL | DISTINCT ] value)Returns the population variance (square of the population standard deviation) of numeric across all input values
VAR_SAMP( [ ALL | DISTINCT ] numeric)Returns the sample variance (square of the sample standard deviation) of numeric across all input values

Not implemented:

  • REGR_AVGX(numeric1, numeric2)
  • REGR_AVGY(numeric1, numeric2)
  • REGR_INTERCEPT(numeric1, numeric2)
  • REGR_R2(numeric1, numeric2)
  • REGR_SLOPE(numeric1, numeric2)
  • REGR_SXY(numeric1, numeric2)

Ordered-Set Aggregate Functions

The syntax is as for aggregateCall, except that WITHIN GROUP is required.

In the following:

  • fraction is a numeric literal between 0 and 1, inclusive, and represents a percentage
Operator syntaxDescription
PERCENTILE_CONT(fraction) WITHIN GROUP (ORDER BY orderItem)Returns a percentile based on a continuous distribution of the column values, interpolating between adjacent input items if needed
PERCENTILE_DISC(fraction) WITHIN GROUP (ORDER BY orderItem [, orderItem ]*)Returns a percentile based on a discrete distribution of the column values returning the first input value whose position in the ordering equals or exceeds the specified fraction

Window functions

Syntax:

{% highlight sql %} windowedAggregateCall: agg ‘(’ [ ALL | DISTINCT ] value [, value ]* ‘)’ [ RESPECT NULLS | IGNORE NULLS ] [ WITHIN GROUP ‘(’ ORDER BY orderItem [, orderItem ]* ‘)’ ] [ FILTER ‘(’ WHERE condition ‘)’ ] OVER window | agg ‘(’ ‘*’ ‘)’ [ FILTER ‘(’ WHERE condition ‘)’ ] OVER window {% endhighlight %}

where agg is one of the operators in the following table, or a user-defined aggregate function.

DISTINCT, FILTER and WITHIN GROUP are as described for aggregate functions.

Operator syntaxDescription
COUNT(value [, value ]*) OVER windowReturns the number of rows in window for which value is not null (wholly not null if value is composite)
COUNT(*) OVER windowReturns the number of rows in window
AVG(numeric) OVER windowReturns the average (arithmetic mean) of numeric across all values in window
SUM(numeric) OVER windowReturns the sum of numeric across all values in window
MAX(value) OVER windowReturns the maximum value of value across all values in window
MIN(value) OVER windowReturns the minimum value of value across all values in window
RANK() OVER windowReturns the rank of the current row with gaps; same as ROW_NUMBER of its first peer
DENSE_RANK() OVER windowReturns the rank of the current row without gaps; this function counts peer groups
ROW_NUMBER() OVER windowReturns the number of the current row within its partition, counting from 1
FIRST_VALUE(value) OVER windowReturns value evaluated at the row that is the first row of the window frame
LAST_VALUE(value) OVER windowReturns value evaluated at the row that is the last row of the window frame
LEAD(value, offset, default) OVER windowReturns value evaluated at the row that is offset rows after the current row within the partition; if there is no such row, instead returns default. Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to NULL
LAG(value, offset, default) OVER windowReturns value evaluated at the row that is offset rows before the current row within the partition; if there is no such row, instead returns default. Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to NULL
NTH_VALUE(value, nth) OVER windowReturns value evaluated at the row that is the nth row of the window frame
NTILE(value) OVER windowReturns an integer ranging from 1 to value, dividing the partition as equally as possible

Note:

  • You may specify null treatment (IGNORE NULLS, RESPECT NULLS) for FIRST_VALUE, LAST_VALUE, NTH_VALUE, LEAD and LAG functions. The syntax handled by the parser, but only RESPECT NULLS is implemented at runtime.

Not implemented:

  • COUNT(DISTINCT value [, value ]*) OVER window
  • APPROX_COUNT_DISTINCT(value [, value ]*) OVER window
  • PERCENT_RANK(value) OVER window
  • CUME_DIST(value) OVER window

Grouping functions

Operator syntaxDescription
GROUPING(expression [, expression ]*)Returns a bit vector of the given grouping expressions
GROUP_ID()Returns an integer that uniquely identifies the combination of grouping keys
GROUPING_ID(expression [, expression ]*)Synonym for GROUPING

DESCRIPTOR

Operator syntaxDescription
DESCRIPTOR(name [, name ]*)DESCRIPTOR appears as an argument in a function to indicate a list of names. The interpretation of names is left to the function.

Table functions

Table functions occur in the FROM clause.

Table functions may have generic table parameters (i.e., no row type is declared when the table function is created), and the row type of the result might depend on the row type(s) of the input tables. Besides, input tables are classified by three characteristics. The first characteristic is semantics. Input tables have either row semantics or set semantics, as follows:

  • Row semantics means that the result of the table function depends on a row-by-row basis.
  • Set semantics means that the outcome of the function depends on how the data is partitioned.

The second characteristic, which applies only to input tables with set semantics, is whether the table function can generate a result row even if the input table is empty.

  • If the table function can generate a result row on empty input, the table is said to be “keep when empty”.
  • The alternative is called “prune when empty”, meaning that the result would be pruned out if the input table is empty.

The third characteristic is whether the input table supports pass-through columns or not. Pass-through columns is a mechanism enabling the table function to copy every column of an input row into columns of an output row.

The input tables with set semantics may be partitioned on one or more columns. The input tables with set semantics may be ordered on one or more columns.

Note:

  • The input tables with row semantics may not be partitioned or ordered.
  • A polymorphic table function may have multiple input tables. However, at most one input table could have row semantics.

TUMBLE

In streaming queries, TUMBLE assigns a window for each row of a relation based on a timestamp column. An assigned window is specified by its beginning and ending. All assigned windows have the same length, and that's why tumbling sometimes is named as “fixed windowing”. The first parameter of the TUMBLE table function is a generic table parameter. The input table has row semantics and supports pass-through columns.

Operator syntaxDescription
TUMBLE(data, DESCRIPTOR(timecol), size [, offset ])Indicates a tumbling window of size interval for timecol, optionally aligned at offset.

Here is an example:

{% highlight sql %} SELECT * FROM TABLE( TUMBLE( TABLE orders, DESCRIPTOR(rowtime), INTERVAL ‘1’ MINUTE));

-- or with the named params -- note: the DATA param must be the first SELECT * FROM TABLE( TUMBLE( DATA => TABLE orders, TIMECOL => DESCRIPTOR(rowtime), SIZE => INTERVAL ‘1’ MINUTE)); {% endhighlight %}

applies a tumbling window with a one minute range to rows from the orders table. rowtime is the watermarked column of the orders table that informs whether data is complete.

HOP

In streaming queries, HOP assigns windows that cover rows within the interval of size and shifting every slide based on a timestamp column. Windows assigned could have overlapping so hopping sometime is named as “sliding windowing”. The first parameter of the HOP table function is a generic table parameter. The input table has row semantics and supports pass-through columns.

Operator syntaxDescription
HOP(data, DESCRIPTOR(timecol), slide, size [, offset ])Indicates a hopping window for timecol, covering rows within the interval of size, shifting every slide and optionally aligned at offset.

Here is an example:

{% highlight sql %} SELECT * FROM TABLE( HOP( TABLE orders, DESCRIPTOR(rowtime), INTERVAL ‘2’ MINUTE, INTERVAL ‘5’ MINUTE));

-- or with the named params -- note: the DATA param must be the first SELECT * FROM TABLE( HOP( DATA => TABLE orders, TIMECOL => DESCRIPTOR(rowtime), SLIDE => INTERVAL ‘2’ MINUTE, SIZE => INTERVAL ‘5’ MINUTE)); {% endhighlight %}

applies hopping with 5-minute interval size on rows from table orders and shifting every 2 minutes. rowtime is the watermarked column of table orders that tells data completeness.

SESSION

In streaming queries, SESSION assigns windows that cover rows based on datetime. Within a session window, distances of rows are less than interval. Session window is applied per key. The first parameter of the SESSION table function is a generic table parameter. The input table has set semantics and supports pass-through columns. Besides, the SESSION table function would not generate a result row if the input table is empty.

Operator syntaxDescription
session(data, DESCRIPTOR(timecol), DESCRIPTOR(key), size)Indicates a session window of size interval for timecol. Session window is applied per key.

Here is an example:

{% highlight sql %} SELECT * FROM TABLE( SESSION( TABLE orders PARTITION BY product, DESCRIPTOR(rowtime), INTERVAL ‘20’ MINUTE));

-- or with the named params -- note: the DATA param must be the first SELECT * FROM TABLE( SESSION( DATA => TABLE orders PARTITION BY product, TIMECOL => DESCRIPTOR(rowtime), SIZE => INTERVAL ‘20’ MINUTE)); {% endhighlight %}

applies a session with 20-minute inactive gap on rows from table orders. rowtime is the watermarked column of table orders that tells data completeness. Session is applied per product.

Note: The Tumble, Hop and Session window table functions assign each row in the original table to a window. The output table has all the same columns as the original table plus two additional columns window_start and window_end, which represent the start and end of the window interval, respectively.

Grouped window functions

warning: grouped window functions are deprecated.

Grouped window functions occur in the GROUP BY clause and define a key value that represents a window containing several rows.

In some window functions, a row may belong to more than one window. For example, if a query is grouped using HOP(t, INTERVAL '2' HOUR, INTERVAL '1' HOUR), a row with timestamp ‘10:15:00’ will occur in both the 10:00 - 11:00 and 11:00 - 12:00 totals.

Operator syntaxDescription
HOP(datetime, slide, size [, time ])Indicates a hopping window for datetime, covering rows within the interval of size, shifting every slide, and optionally aligned at time
SESSION(datetime, interval [, time ])Indicates a session window of interval for datetime, optionally aligned at time
TUMBLE(datetime, interval [, time ])Indicates a tumbling window of interval for datetime, optionally aligned at time

Grouped auxiliary functions

Grouped auxiliary functions allow you to access properties of a window defined by a grouped window function.

Operator syntaxDescription
HOP_END(expression, slide, size [, time ])Returns the value of expression at the end of the window defined by a HOP function call
HOP_START(expression, slide, size [, time ])Returns the value of expression at the beginning of the window defined by a HOP function call
SESSION_END(expression, interval [, time])Returns the value of expression at the end of the window defined by a SESSION function call
SESSION_START(expression, interval [, time])Returns the value of expression at the beginning of the window defined by a SESSION function call
TUMBLE_END(expression, interval [, time ])Returns the value of expression at the end of the window defined by a TUMBLE function call
TUMBLE_START(expression, interval [, time ])Returns the value of expression at the beginning of the window defined by a TUMBLE function call

Spatial functions

In the following:

In the “C” (for “compatibility”) column, “o” indicates that the function implements the OpenGIS Simple Features Implementation Specification for SQL, version 1.2.1; “p” indicates that the function is a PostGIS extension to OpenGIS; “h” indicates that the function is an H2GIS extension.

Geometry conversion functions (2D)

COperator syntaxDescription
pST_AsBinary(geom)Synonym for ST_AsWKB
pST_AsEWKB(geom)Synonym for ST_AsWKB
pST_AsEWKT(geom)Converts GEOMETRY → EWKT
pST_AsGeoJSON(geom)Converts GEOMETRY → GeoJSON
pST_AsGML(geom)Converts GEOMETRY → GML
pST_AsText(geom)Synonym for ST_AsWKT
oST_AsWKB(geom)Converts GEOMETRY → WKB
oST_AsWKT(geom)Converts GEOMETRY → WKT
oST_Force2D(geom)3D GEOMETRY → 2D GEOMETRY
oST_GeomFromEWKB(wkb [, srid ])Synonym for ST_GeomFromWKB
oST_GeomFromEWKT(wkb [, srid ])Converts EWKT → GEOMETRY
oST_GeomFromGeoJSON(json)Converts GeoJSON → GEOMETRY
oST_GeomFromGML(wkb [, srid ])Converts GML → GEOMETRY
oST_GeomFromText(wkt [, srid ])Synonym for ST_GeomFromWKT
oST_GeomFromWKB(wkb [, srid ])Converts WKB → GEOMETRY
oST_GeomFromWKT(wkb [, srid ])Converts WKT → GEOMETRY
oST_LineFromText(wkt [, srid ])Converts WKT → LINESTRING
oST_LineFromWKB(wkt [, srid ])Converts WKT → LINESTRING
oST_MLineFromText(wkt [, srid ])Converts WKT → MULTILINESTRING
oST_MPointFromText(wkt [, srid ])Converts WKT → MULTIPOINT
oST_MPolyFromText(wkt [, srid ]) Converts WKT → MULTIPOLYGON
oST_PointFromText(wkt [, srid ])Converts WKT → POINT
oST_PointFromWKB(wkt [, srid ])Converts WKB → POINT
oST_PolyFromText(wkt [, srid ])Converts WKT → POLYGON
oST_PolyFromWKB(wkt [, srid ])Converts WKB → POLYGON
pST_ReducePrecision(geom, gridSize)Reduces the precision of a geom to the provided gridSize
hST_ToMultiPoint(geom)Converts the coordinates of geom (which may be a GEOMETRYCOLLECTION) into a MULTIPOINT
hST_ToMultiLine(geom)Converts the coordinates of geom (which may be a GEOMETRYCOLLECTION) into a MULTILINESTRING
hST_ToMultiSegments(geom)Converts geom (which may be a GEOMETRYCOLLECTION) into a set of distinct segments stored in a MULTILINESTRING

Not implemented:

  • ST_GoogleMapLink(geom [, layerType [, zoom ]]) GEOMETRY → Google map link
  • ST_OSMMapLink(geom [, marker ]) GEOMETRY → OSM map link

Geometry conversion functions (3D)

COperator syntaxDescription
oST_Force3D(geom)2D GEOMETRY → 3D GEOMETRY

Geometry creation functions (2D)

COperator syntaxDescription
hST_BoundingCircle(geom)Returns the minimum bounding circle of geom
hST_Expand(geom, distance)Expands geom's envelope
hST_Expand(geom, deltaX, deltaY)Expands geom's envelope
hST_MakeEllipse(point, width, height)Constructs an ellipse
pST_MakeEnvelope(xMin, yMin, xMax, yMax [, srid ])Creates a rectangular POLYGON
hST_MakeGrid(geom, deltaX, deltaY)Calculates a regular grid of POLYGONs based on geom
hST_MakeGridPoints(geom, deltaX, deltaY)Calculates a regular grid of points based on geom
oST_MakeLine(point1 [, point ]*)Creates a line-string from the given POINTs (or MULTIPOINTs)
pST_MakePoint(x, y [, z ])Synonym for ST_Point
pST_MakePolygon(lineString [, hole ]*)Creates a POLYGON from lineString with the given holes (which are required to be closed LINESTRINGs)
hST_MinimumDiameter(geom)Returns the minimum diameter of geom
hST_MinimumRectangle(geom)Returns the minimum rectangle enclosing geom
hST_OctogonalEnvelope(geom)Returns the octogonal envelope of geom
oST_Point(x, y [, z ])Constructs a point from two or three coordinates

Not implemented:

  • ST_RingBuffer(geom, distance, bufferCount [, endCapStyle [, doDifference]]) Returns a MULTIPOLYGON of buffers centered at geom and of increasing buffer size

Geometry creation functions (3D)

Not implemented:

  • ST_Extrude(geom, height [, flag]) Extrudes a GEOMETRY
  • ST_GeometryShadow(geom, point, height) Computes the shadow footprint of geom
  • ST_GeometryShadow(geom, azimuth, altitude, height [, unify ]) Computes the shadow footprint of geom

Geometry properties (2D)

COperator syntaxDescription
oST_Boundary(geom [, srid ])Returns the boundary of geom
oST_Centroid(geom)Returns the centroid of geom
oST_CoordDim(geom)Returns the dimension of the coordinates of geom
oST_Dimension(geom)Returns the dimension of geom
oST_Distance(geom1, geom2)Returns the distance between geom1 and geom2
hST_ExteriorRing(geom)Returns the exterior ring of geom, or null if geom is not a polygon
oST_GeometryType(geom)Returns the type of geom
oST_GeometryTypeCode(geom)Returns the OGC SFS type code of geom
pST_EndPoint(lineString)Returns the last coordinate of geom
oST_Envelope(geom [, srid ])Returns the envelope of geom (which may be a GEOMETRYCOLLECTION) as a GEOMETRY
oST_Extent(geom)Returns the minimum bounding box of geom (which may be a GEOMETRYCOLLECTION)
hST_GeometryN(geomCollection, n)Returns the nth GEOMETRY of geomCollection
hST_InteriorRingN(geom)Returns the nth interior ring of geom, or null if geom is not a polygon
hST_IsClosed(geom)Returns whether geom is a closed LINESTRING or MULTILINESTRING
oST_IsEmpty(geom)Returns whether geom is empty
oST_IsRectangle(geom)Returns whether geom is a rectangle
hST_IsRing(geom)Returns whether geom is a closed and simple line-string or MULTILINESTRING
oST_IsSimple(geom)Returns whether geom is simple
oST_IsValid(geom)Returns whether geom is valid
hST_NPoints(geom)Returns the number of points in geom
hST_NumGeometries(geom)Returns the number of geometries in geom (1 if it is not a GEOMETRYCOLLECTION)
hST_NumInteriorRing(geom)Synonym for ST_NumInteriorRings
hST_NumInteriorRings(geom)Returns the number of interior rings of geom
hST_NumPoints(geom)Returns the number of points in geom
pST_PointN(geom, n)Returns the nth point of a geom
pST_PointOnSurface(geom)Returns an interior or boundary point of geom
oST_SRID(geom)Returns SRID value of geom or 0 if it does not have one
pST_StartPoint(geom)Returns the first point of geom
oST_X(geom)Returns the x-value of the first coordinate of geom
oST_XMax(geom)Returns the maximum x-value of geom
oST_XMin(geom)Returns the minimum x-value of geom
oST_Y(geom)Returns the y-value of the first coordinate of geom
oST_YMax(geom)Returns the maximum y-value of geom
oST_YMin(geom)Returns the minimum y-value of geom

Not implemented:

  • ST_CompactnessRatio(polygon) Returns the square root of polygon's area divided by the area of the circle with circumference equal to its perimeter
  • ST_Explode(query [, fieldName]) Explodes the GEOMETRYCOLLECTIONs in the fieldName column of a query into multiple geometries
  • ST_IsValidDetail(geom [, selfTouchValid ]) Returns a valid detail as an array of objects
  • ST_IsValidReason(geom [, selfTouchValid ]) Returns text stating whether geom is valid, and if not valid, a reason why

Geometry properties (3D)

COperator syntaxDescription
pST_Is3D(s)Returns whether geom has at least one z-coordinate
oST_Z(geom)Returns the z-value of the first coordinate of geom
oST_ZMax(geom)Returns the maximum z-value of geom
oST_ZMin(geom)Returns the minimum z-value of geom

Geometry predicates

COperator syntaxDescription
oST_Contains(geom1, geom2)Returns whether geom1 contains geom2
pST_ContainsProperly(geom1, geom2)Returns whether geom1 contains geom2 but does not intersect its boundary
pST_CoveredBy(geom1, geom2)Returns whether no point in geom1 is outside geom2.
pST_Covers(geom1, geom2)Returns whether no point in geom2 is outside geom1
oST_Crosses(geom1, geom2)Returns whether geom1 crosses geom2
oST_Disjoint(geom1, geom2)Returns whether geom1 and geom2 are disjoint
pST_DWithin(geom1, geom2, distance)Returns whether geom1 and geom are within distance of one another
oST_EnvelopesIntersect(geom1, geom2)Returns whether the envelope of geom1 intersects the envelope of geom2
oST_Equals(geom1, geom2)Returns whether geom1 equals geom2
oST_Intersects(geom1, geom2)Returns whether geom1 intersects geom2
oST_Overlaps(geom1, geom2)Returns whether geom1 overlaps geom2
oST_Relate(geom1, geom2)Returns the DE-9IM intersection matrix of geom1 and geom2
oST_Relate(geom1, geom2, iMatrix)Returns whether geom1 and geom2 are related by the given intersection matrix iMatrix
oST_Touches(geom1, geom2)Returns whether geom1 touches geom2
oST_Within(geom1, geom2)Returns whether geom1 is within geom2

Not implemented:

  • ST_OrderingEquals(geom1, geom2) Returns whether geom1 equals geom2 and their coordinates and component Geometries are listed in the same order

Geometry operators (2D)

The following functions combine 2D geometries.

COperator syntaxDescription
pST_Buffer(geom, distance [, quadSegs, endCapStyle ])Computes a buffer around geom
pST_Buffer(geom, distance [, bufferStyle ])Computes a buffer around geom
oST_ConvexHull(geom)Computes the smallest convex polygon that contains all the points in geom
oST_Difference(geom1, geom2)Computes the difference between two geometries
oST_SymDifference(geom1, geom2)Computes the symmetric difference between two geometries
oST_Intersection(geom1, geom2)Computes the intersection of geom1 and geom2
pST_OffsetCurve(geom, distance, bufferStyle)Computes an offset line for linestring
oST_Union(geom1, geom2)Computes the union of geom1 and geom2
oST_Union(geomCollection)Computes the union of the geometries in geomCollection

See also: the ST_Union aggregate function.

Affine transformation functions (3D and 2D)

The following functions transform 2D geometries.

COperator syntaxDescription
oST_Rotate(geom, angle [, origin | x, y])Rotates a geom counter-clockwise by angle (in radians) about origin (or the point (x, y))
oST_Scale(geom, xFactor, yFactor)Scales geom by multiplying the ordinates by the indicated scale factors
oST_Translate(geom, x, y)Translates geom by the vector (x, y)

Not implemented:

  • ST_Scale(geom, xFactor, yFactor [, zFactor ]) Scales geom by multiplying the ordinates by the indicated scale factors
  • ST_Translate(geom, x, y, [, z]) Translates geom

Geometry editing functions (2D)

The following functions modify 2D geometries.

COperator syntaxDescription
pST_AddPoint(linestring, point [, index])Adds point to linestring at a given index (or at the end if index is not specified)
hST_Densify(geom, tolerance)Densifies a geom by inserting extra vertices along the line segments
hST_FlipCoordinates(geom)Flips the X and Y coordinates of the geom
hST_Holes(geom)Returns the holes in the geom (which may be a GEOMETRYCOLLECTION)
hST_Normalize(geom)Converts the geom to normal form
pST_RemoveRepeatedPoints(geom [, tolerance])Removes duplicated coordinates from the geom
hST_RemoveHoles(geom)Removes the holes of the geom
pST_RemovePoint(linestring, index)Remove point at given index in linestring
hST_Reverse(geom)Reverses the order of the coordinates of the geom

Not implemented:

  • ST_CollectionExtract(geom, dimension) Filters geom, returning a multi-geometry of those members with a given dimension (1 = point, 2 = line-string, 3 = polygon)

Geometry editing functions (3D)

The following functions modify 3D geometries.

COperator syntaxDescription
hST_AddZ(geom, zToAdd)Adds zToAdd to the z-coordinate of geom

Not implemented:

  • ST_Interpolate3DLine(geom) Returns geom with an interpolation of z values, or null if it is not a line-string or MULTILINESTRING
  • ST_MultiplyZ(geom, zFactor) Returns geom with its z-values multiplied by zFactor
  • ST_Reverse3DLine(geom [, sortOrder ]) Potentially reverses geom according to the z-values of its first and last coordinates
  • ST_UpdateZ(geom, newZ [, updateCondition ]) Updates the z-values of geom
  • ST_ZUpdateLineExtremities(geom, startZ, endZ [, interpolate ]) Updates the start and end z-values of geom

Geometry measurement functions (2D)

The following functions measure geometries.

COperator syntaxDescription
oST_Area(geom)Returns the area of geom (which may be a GEOMETRYCOLLECTION)
hST_ClosestCoordinate(point, geom)Returns the coordinate(s) of geom closest to point
hST_ClosestPoint(geom1, geom2)Returns the point of geom1 closest to geom2
hST_FurthestCoordinate(geom, point)Returns the coordinate(s) of geom that are furthest from point
hST_Length(geom)Returns the length of geom
hST_LocateAlong(geom, segmentLengthFraction, offsetDistance)Returns a MULTIPOINT containing points along the line segments of geom at segmentLengthFraction and offsetDistance
hST_LongestLine(geom1, geom2)Returns the 2-dimensional longest line-string between the points of geom1 and geom2
hST_MaxDistance(geom1, geom2)Computes the maximum distance between geom1 and geom2
hST_Perimeter(polygon)Returns the length of the perimeter of polygon (which may be a MULTIPOLYGON)
hST_ProjectPoint(point, lineString)Projects point onto a lineString (which may be a MULTILINESTRING)

Geometry measurement functions (3D)

Not implemented:

  • ST_3DArea(geom) Return a polygon's 3D area
  • ST_3DLength(geom) Returns the 3D length of a line-string
  • ST_3DPerimeter(geom) Returns the 3D perimeter of a polygon or MULTIPOLYGON
  • ST_SunPosition(point [, timestamp ]) Computes the sun position at point and timestamp (now by default)

Geometry processing functions (2D)

The following functions process geometries.

COperator syntaxDescription
oST_LineMerge(geom)Merges a collection of linear components to form a line-string of maximal length
oST_MakeValid(geom)Makes a valid geometry of a given invalid geometry
oST_Polygonize(geom)Creates a MULTIPOLYGON from edges of geom
oST_PrecisionReducer(geom, n)Reduces geom's precision to n decimal places
oST_Simplify(geom, distance)Simplifies geom using the Douglas-Peuker algorithm with a distance tolerance
oST_SimplifyPreserveTopology(geom, distance)Simplifies geom, preserving its topology
oST_Snap(geom1, geom2, tolerance)Snaps geom1 and geom2 together
pST_Split(geom, blade)Splits geom by blade

Not implemented:

  • ST_LineIntersector(geom1, geom2) Splits geom1 (a line-string) with geom2
  • ST_LineMerge(geom) Merges a collection of linear components to form a line-string of maximal length
  • ST_MakeValid(geom [, preserveGeomDim [, preserveDuplicateCoord [, preserveCoordDim]]]) Makes geom valid
  • ST_RingSideBuffer(geom, distance, bufferCount [, endCapStyle [, doDifference]]) Computes a ring buffer on one side
  • ST_SideBuffer(geom, distance [, bufferStyle ]) Compute a single buffer on one side

Geometry projection functions

The EPSG dataset is released separately from Proj4J due to its restrictive terms of use. In order to use the projection functions in Apache Calcite, users must include the EPSG dataset in their dependencies.

COperator syntaxDescription
oST_SetSRID(geom, srid)Returns a copy of geom with a new SRID
oST_Transform(geom, srid)Transforms geom from one coordinate reference system (CRS) to the CRS specified by srid

Trigonometry functions

Not implemented:

  • ST_Azimuth(point1, point2) Return the azimuth of the segment from point1 to point2

Topography functions

Not implemented:

  • ST_TriangleAspect(geom) Returns the aspect of a triangle
  • ST_TriangleContouring(query [, z1, z2, z3 ][, varArgs ]*) Splits triangles into smaller triangles according to classes
  • ST_TriangleDirection(geom) Computes the direction of steepest ascent of a triangle and returns it as a line-string
  • ST_TriangleSlope(geom) Computes the slope of a triangle as a percentage
  • ST_Voronoi(geom [, outDimension [, envelopePolygon ]]) Creates a Voronoi diagram

Triangulation functions

COperator syntaxDescription
hST_ConstrainedDelaunay(geom [, flag])Computes a constrained Delaunay triangulation based on geom
hST_Delaunay(geom [, flag])Computes a Delaunay triangulation based on points in geom

Not implemented:

  • ST_Tessellate(polygon) Tessellates polygon (may be MULTIPOLYGON) with adaptive triangles

Geometry aggregate functions

COperator syntaxDescription
hST_Accum(geom)Accumulates geom into an array
hST_Collect(geom)Collects geom into a GeometryCollection
hST_Union(geom)Computes the union of the geometries in geom

JSON Functions

In the following:

  • jsonValue is a character string containing a JSON value;
  • path is a character string containing a JSON path expression; mode flag strict or lax should be specified in the beginning of path.

Query Functions

Operator syntaxDescription
JSON_EXISTS(jsonValue, path [ { TRUE | FALSE | UNKNOWN | ERROR } ON ERROR ] )Whether a jsonValue satisfies a search criterion described using JSON path expression path
JSON_VALUE(jsonValue, path [ RETURNING type ] [ { ERROR | NULL | DEFAULT expr } ON EMPTY ] [ { ERROR | NULL | DEFAULT expr } ON ERROR ] )Extract an SQL scalar from a jsonValue using JSON path expression path
JSON_QUERY(jsonValue, path [ { WITHOUT [ ARRAY ] | WITH [ CONDITIONAL | UNCONDITIONAL ] [ ARRAY ] } WRAPPER ] [ { ERROR | NULL | EMPTY ARRAY | EMPTY OBJECT } ON EMPTY ] [ { ERROR | NULL | EMPTY ARRAY | EMPTY OBJECT } ON ERROR ] )Extract a JSON object or JSON array from jsonValue using the path JSON path expression

Note:

  • The ON ERROR and ON EMPTY clauses define the fallback behavior of the function when an error is thrown or a null value is about to be returned.
  • The ARRAY WRAPPER clause defines how to represent a JSON array result in JSON_QUERY function. The following examples compare the wrapper behaviors.

Example Data:

{% highlight json %} {“a”: “[1,2]”, “b”: [1,2], “c”: “hi”} {% endhighlight json %}

Comparison:

Operator$.a$.b$.c
JSON_VALUE[1, 2]errorhi
JSON QUERY WITHOUT ARRAY WRAPPERerror[1, 2]error
JSON QUERY WITH UNCONDITIONAL ARRAY WRAPPER[ “[1,2]” ][ [1,2] ][ “hi” ]
JSON QUERY WITH CONDITIONAL ARRAY WRAPPER[ “[1,2]” ][1,2][ “hi” ]

Not implemented:

  • JSON_TABLE

Constructor Functions

Operator syntaxDescription
JSON_OBJECT( jsonKeyVal [, jsonKeyVal ]* [ nullBehavior ] )Construct JSON object using a series of key-value pairs
JSON_OBJECTAGG( jsonKeyVal [ nullBehavior ] )Aggregate function to construct a JSON object using a key-value pair
JSON_ARRAY( [ jsonVal [, jsonVal ]* ] [ nullBehavior ] )Construct a JSON array using a series of values
JSON_ARRAYAGG( jsonVal [ ORDER BY orderItem [, orderItem ]* ] [ nullBehavior ] )Aggregate function to construct a JSON array using a value

{% highlight sql %} jsonKeyVal: [ KEY ] name VALUE value [ FORMAT JSON ] | name : value [ FORMAT JSON ]

jsonVal: value [ FORMAT JSON ]

nullBehavior: NULL ON NULL | ABSENT ON NULL {% endhighlight %}

Note:

  • The flag FORMAT JSON indicates the value is formatted as JSON character string. When FORMAT JSON is used, the value should be de-parse from JSON character string to a SQL structured value.
  • ON NULL clause defines how the JSON output represents null values. The default null behavior of JSON_OBJECT and JSON_OBJECTAGG is NULL ON NULL, and for JSON_ARRAY and JSON_ARRAYAGG it is ABSENT ON NULL.
  • If ORDER BY clause is provided, JSON_ARRAYAGG sorts the input rows into the specified order before performing aggregation.

Comparison Operators

Operator syntaxDescription
jsonValue IS JSON [ VALUE ]Whether jsonValue is a JSON value
jsonValue IS NOT JSON [ VALUE ]Whether jsonValue is not a JSON value
jsonValue IS JSON SCALARWhether jsonValue is a JSON scalar value
jsonValue IS NOT JSON SCALARWhether jsonValue is not a JSON scalar value
jsonValue IS JSON OBJECTWhether jsonValue is a JSON object
jsonValue IS NOT JSON OBJECTWhether jsonValue is not a JSON object
jsonValue IS JSON ARRAYWhether jsonValue is a JSON array
jsonValue IS NOT JSON ARRAYWhether jsonValue is not a JSON array

Dialect-specific Operators

The following operators are not in the SQL standard, and are not enabled in Calcite's default operator table. They are only available for use in queries if your session has enabled an extra operator table.

To enable an operator table, set the [fun]({{ site.baseurl }}/docs/adapter.html#jdbc-connect-string-parameters) connect string parameter.

The ‘C’ (compatibility) column contains value:

  • ‘*’ for all libraries,
  • ‘b’ for Google BigQuery (‘fun=bigquery’ in the connect string),
  • ‘c’ for Apache Calcite (‘fun=calcite’ in the connect string),
  • ‘f’ for Snowflake (‘fun=snowflake’ in the connect string),
  • ‘h’ for Apache Hive (‘fun=hive’ in the connect string),
  • ‘m’ for MySQL (‘fun=mysql’ in the connect string),
  • ‘q’ for Microsoft SQL Server (‘fun=mssql’ in the connect string),
  • ‘o’ for Oracle (‘fun=oracle’ in the connect string),
  • ‘p’ for PostgreSQL (‘fun=postgresql’ in the connect string),
  • ‘s’ for Apache Spark (‘fun=spark’ in the connect string).

One operator name may correspond to multiple SQL dialects, but with different semantics.

BigQuery's type system uses confusingly different names for types and functions:

  • BigQuery‘s DATETIME type represents a local date time, and corresponds to Calcite’s TIMESTAMP type;
  • BigQuery‘s TIMESTAMP type represents an instant, and corresponds to Calcite’s TIMESTAMP WITH LOCAL TIME ZONE type;
  • The timestampLtz parameter, for instance in DATE(timestampLtz), has Calcite type TIMESTAMP WITH LOCAL TIME ZONE;
  • The TIMESTAMP(string) function, designed to be compatible the BigQuery function, return a Calcite TIMESTAMP WITH LOCAL TIME ZONE;
  • Similarly, DATETIME(string) returns a Calcite TIMESTAMP.

In the following:

  • func is a lambda argument.
COperator syntaxDescription
pexpr :: typeCasts expr to type
mexpr1 <=> expr2Whether two values are equal, treating null values as the same, and it's similar to IS NOT DISTINCT FROM
*ACOSH(numeric)Returns the inverse hyperbolic cosine of numeric
sARRAY([expr [, expr ]*])Construct an array in Apache Spark. The function allows users to use ARRAY() to create an empty array
sARRAY_APPEND(array, element)Appends an element to the end of the array and returns the result. Type of element should be similar to type of the elements of the array. If the array is null, the function will return null. If an element that is null, the null element will be added to the end of the array
sARRAY_COMPACT(array)Removes null values from the array
bARRAY_CONCAT(array [, array ]*)Concatenates one or more arrays. If any input argument is NULL the function returns NULL
sARRAY_CONTAINS(array, element)Returns true if the array contains the element
sARRAY_DISTINCT(array)Removes duplicate values from the array that keeps ordering of elements
sARRAY_EXCEPT(array1, array2)Returns an array of the elements in array1 but not in array2, without duplicates
sARRAY_INSERT(array, pos, element)Places element into index pos of array. Array index start at 1, or start from the end if index is negative. Index above array size appends the array, or prepends the array if index is negative, with NULL elements.
sARRAY_INTERSECT(array1, array2)Returns an array of the elements in the intersection of array1 and array2, without duplicates
sARRAY_JOIN(array, delimiter [, nullText ])Synonym for ARRAY_TO_STRING
bARRAY_LENGTH(array)Synonym for CARDINALITY
sARRAY_MAX(array)Returns the maximum value in the array
sARRAY_MIN(array)Returns the minimum value in the array
sARRAY_POSITION(array, element)Returns the (1-based) index of the first element of the array as long
sARRAY_REMOVE(array, element)Remove all elements that equal to element from the array
sARRAY_PREPEND(array, element)Appends an element to the beginning of the array and returns the result. Type of element should be similar to type of the elements of the array. If the array is null, the function will return null. If an element that is null, the null element will be added to the beginning of the array
sARRAY_REPEAT(element, count)Returns the array containing element count times.
bARRAY_REVERSE(array)Reverses elements of array
sARRAY_SIZE(array)Synonym for CARDINALITY
bARRAY_TO_STRING(array, delimiter [, nullText ])Returns a concatenation of the elements in array as a STRING and take delimiter as the delimiter. If the nullText parameter is used, the function replaces any NULL values in the array with the value of nullText. If the nullText parameter is not used, the function omits the NULL value and its preceding delimiter. Returns NULL if any argument is NULL
sARRAY_UNION(array1, array2)Returns an array of the elements in the union of array1 and array2, without duplicates
sARRAYS_OVERLAP(array1, array2)Returns true if *array1 contains at least a non-null element present also in array2. If the arrays have no common element and they are both non-empty and either of them contains a null element null is returned, false otherwise
sARRAYS_ZIP(array [, array ]*)Returns a merged array of structs in which the N-th struct contains all N-th values of input arrays
sSORT_ARRAY(array [, ascendingOrder])Sorts the array in ascending or descending order according to the natural ordering of the array elements. The default order is ascending if ascendingOrder is not specified. Null elements will be placed at the beginning of the returned array in ascending order or at the end of the returned array in descending order
*ASINH(numeric)Returns the inverse hyperbolic sine of numeric
*ATANH(numeric)Returns the inverse hyperbolic tangent of numeric
fBITAND_AGG(value)Equivalent to BIT_AND(value)
fBITOR_AGG(value)Equivalent to BIT_OR(value)
sBIT_LENGTH(binary)Returns the bit length of binary
sBIT_LENGTH(string)Returns the bit length of string
sBIT_GET(value, position)Returns the bit (0 or 1) value at the specified position of numeric value. The positions are numbered from right to left, starting at zero. The position argument cannot be negative
bCEIL(value)Similar to standard CEIL(value) except if value is an integer type, the return type is a double
m sCHAR(integer)Returns the character whose ASCII code is integer % 256, or null if integer < 0
b o pCHR(integer)Returns the character whose UTF-8 code is integer
bCODE_POINTS_TO_BYTES(integers)Converts integers, an array of integers between 0 and 255 inclusive, into bytes; throws error if any element is out of range
bCODE_POINTS_TO_STRING(integers)Converts integers, an array of integers between 0 and 0xD7FF or between 0xE000 and 0x10FFFF inclusive, into string; throws error if any element is out of range
oCONCAT(string, string)Concatenates two strings, returns null only when both string arguments are null, otherwise treats null as empty string
b mCONCAT(string [, string ]*)Concatenates one or more strings, returns null if any of the arguments is null
p qCONCAT(string [, string ]*)Concatenates one or more strings, null is treated as empty string
m pCONCAT_WS(separator, str1 [, string ]*)Concatenates one or more strings, returns null only when separator is null, otherwise treats null arguments as empty strings
qCONCAT_WS(separator, str1, str2 [, string ]*)Concatenates two or more strings, requires at least 3 arguments (up to 254), treats null arguments as empty strings
mCOMPRESS(string)Compresses a string using zlib compression and returns the result as a binary string
bCONTAINS_SUBSTR(expression, string [ , json_scope => json_scope_value ])Returns whether string exists as a substring in expression. Optional json_scope argument specifies what scope to search if expression is in JSON format. Returns NULL if a NULL exists in expression that does not result in a match
qCONVERT(type, expression [ , style ])Equivalent to CAST(expression AS type); ignores the style operand
pCONVERT_TIMEZONE(tz1, tz2, datetime)Converts the timezone of datetime from tz1 to tz2
*COSH(numeric)Returns the hyperbolic cosine of numeric
*COTH(numeric)Returns the hyperbolic cotangent of numeric
*CSC(numeric)Returns the cosecant of numeric in radians
*CSCH(numeric)Returns the hyperbolic cosecant of numeric
bCURRENT_DATETIME([ timeZone ])Returns the current time as a TIMESTAMP from timezone
mDAYNAME(datetime)Returns the name, in the connection's locale, of the weekday in datetime; for example, it returns ‘星期日’ for both DATE ‘2020-02-10’ and TIMESTAMP ‘2020-02-10 10:10:10’
bDATE(timestamp)Extracts the DATE from a timestamp
bDATE(timestampLtz)Extracts the DATE from timestampLtz (an instant; BigQuery's TIMESTAMP type), assuming UTC
bDATE(timestampLtz, timeZone)Extracts the DATE from timestampLtz (an instant; BigQuery's TIMESTAMP type) in timeZone
bDATE(string)Equivalent to CAST(string AS DATE)
bDATE(year, month, day)Returns a DATE value for year, month, and day (all of type INTEGER)
p qDATEADD(timeUnit, integer, datetime)Equivalent to TIMESTAMPADD(timeUnit, integer, datetime)
p qDATEDIFF(timeUnit, datetime, datetime2)Equivalent to TIMESTAMPDIFF(timeUnit, datetime, datetime2)
qDATEPART(timeUnit, datetime)Equivalent to EXTRACT(timeUnit FROM datetime)
bDATETIME(date, time)Converts date and time to a TIMESTAMP
bDATETIME(date)Converts date to a TIMESTAMP value (at midnight)
bDATETIME(date, timeZone)Converts date to a TIMESTAMP value (at midnight), in timeZone
bDATETIME(year, month, day, hour, minute, second)Creates a TIMESTAMP for year, month, day, hour, minute, second (all of type INTEGER)
bDATETIME_ADD(timestamp, interval)Returns the TIMESTAMP value that occurs interval after timestamp
bDATETIME_DIFF(timestamp, timestamp2, timeUnit)Returns the whole number of timeUnit between timestamp and timestamp2
bDATETIME_SUB(timestamp, interval)Returns the TIMESTAMP that occurs interval before timestamp
bDATETIME_TRUNC(timestamp, timeUnit)Truncates timestamp to the granularity of timeUnit, rounding to the beginning of the unit
b sDATE_FROM_UNIX_DATE(integer)Returns the DATE that is integer days after 1970-01-01
pDATE_PART(timeUnit, datetime)Equivalent to EXTRACT(timeUnit FROM datetime)
bDATE_ADD(date, interval)Returns the DATE value that occurs interval after date
bDATE_DIFF(date, date2, timeUnit)Returns the whole number of timeUnit between date and date2
bDATE_SUB(date, interval)Returns the DATE value that occurs interval before date
bDATE_TRUNC(date, timeUnit)Truncates date to the granularity of timeUnit, rounding to the beginning of the unit
o sDECODE(value, value1, result1 [, valueN, resultN ]* [, default ])Compares value to each valueN value one by one; if value is equal to a valueN, returns the corresponding resultN, else returns default, or NULL if default is not specified
pDIFFERENCE(string, string)Returns a measure of the similarity of two strings, namely the number of character positions that their SOUNDEX values have in common: 4 if the SOUNDEX values are same and 0 if the SOUNDEX values are totally different
f sENDSWITH(string1, string2)Returns whether string2 is a suffix of string1
b pENDS_WITH(string1, string2)Equivalent to ENDSWITH(string1, string2)
sEXISTS(array, func)Returns whether a predicate func holds for one or more elements in the array
oEXISTSNODE(xml, xpath, [, namespaces ])Determines whether traversal of a XML document using a specified xpath results in any nodes. Returns 0 if no nodes remain after applying the XPath traversal on the document fragment of the element or elements matched by the XPath expression. Returns 1 if any nodes remain. The optional namespace value that specifies a default mapping or namespace mapping for prefixes, which is used when evaluating the XPath expression.
oEXTRACT(xml, xpath, [, namespaces ])Returns the XML fragment of the element or elements matched by the XPath expression. The optional namespace value that specifies a default mapping or namespace mapping for prefixes, which is used when evaluating the XPath expression
mEXTRACTVALUE(xml, xpathExpr))Returns the text of the first text node which is a child of the element or elements matched by the XPath expression.
h sFACTORIAL(integer)Returns the factorial of integer, the range of integer is [0, 20]. Otherwise, returns NULL
h sFIND_IN_SET(matchStr, textStr)Returns the index (1-based) of the given matchStr in the comma-delimited textStr. Returns 0, if the given matchStr is not found or if the matchStr contains a comma. For example, FIND_IN_SET(‘bc’, ‘a,bc,def’) returns 2
bFLOOR(value)Similar to standard FLOOR(value) except if value is an integer type, the return type is a double
bFORMAT_DATE(string, date)Formats date according to the specified format string
bFORMAT_DATETIME(string, timestamp)Formats timestamp according to the specified format string
h sFORMAT_NUMBER(value, decimalVal)Formats the number value like ‘#,###,###.##’, rounded to decimal places decimalVal. If decimalVal is 0, the result has no decimal point or fractional part
h sFORMAT_NUMBER(value, format)Formats the number value to MySQL's FORMAT format, like ‘#,###,###.##0.00’
bFORMAT_TIME(string, time)Formats time according to the specified format string
bFORMAT_TIMESTAMP(string timestamp)Formats timestamp according to the specified format string
sGETBIT(value, position)Equivalent to BIT_GET(value, position)
b o sGREATEST(expr [, expr ]*)Returns the greatest of the expressions
b h sIF(condition, value1, value2)Returns value1 if condition is TRUE, value2 otherwise
b sIFNULL(value1, value2)Equivalent to NVL(value1, value2)
pstring1 ILIKE string2 [ ESCAPE string3 ]Whether string1 matches pattern string2, ignoring case (similar to LIKE)
pstring1 NOT ILIKE string2 [ ESCAPE string3 ]Whether string1 does not match pattern string2, ignoring case (similar to NOT LIKE)
b oINSTR(string, substring [, from [, occurrence ] ])Returns the position of substring in string, searching starting at from (default 1), and until locating the nth occurrence (default 1) of substring
mINSTR(string, substring)Equivalent to POSITION(substring IN string)
bIS_INF(value)Returns whether value is infinite
bIS_NAN(value)Returns whether value is NaN
mJSON_TYPE(jsonValue)Returns a string value indicating the type of jsonValue
mJSON_DEPTH(jsonValue)Returns an integer value indicating the depth of jsonValue
mJSON_PRETTY(jsonValue)Returns a pretty-printing of jsonValue
mJSON_LENGTH(jsonValue [, path ])Returns a integer indicating the length of jsonValue
mJSON_INSERT(jsonValue, path, val [, path, val ]*)Returns a JSON document insert a data of jsonValue, path, val
mJSON_KEYS(jsonValue [, path ])Returns a string indicating the keys of a JSON jsonValue
mJSON_REMOVE(jsonValue, path [, path ])Removes data from jsonValue using a series of path expressions and returns the result
mJSON_REPLACE(jsonValue, path, val [, path, val ]*)Returns a JSON document replace a data of jsonValue, path, val
mJSON_SET(jsonValue, path, val [, path, val ]*)Returns a JSON document set a data of jsonValue, path, val
mJSON_STORAGE_SIZE(jsonValue)Returns the number of bytes used to store the binary representation of jsonValue
b o sLEAST(expr [, expr ]* )Returns the least of the expressions
b m p sLEFT(string, length)Returns the leftmost length characters from the string
f sLEN(string)Equivalent to CHAR_LENGTH(string)
b f sLENGTH(string)Equivalent to CHAR_LENGTH(string)
h sLEVENSHTEIN(string1, string2)Returns the Levenshtein distance between string1 and string2
bLOG(numeric1 [, numeric2 ])Returns the logarithm of numeric1 to base numeric2, or base e if numeric2 is not present
m sLOG2(numeric)Returns the base 2 logarithm of numeric
b o sLPAD(string, length [, pattern ])Returns a string or bytes value that consists of string prepended to length with pattern
bTO_BASE32(string)Converts the string to base-32 encoded form and returns an encoded string
bFROM_BASE32(string)Returns the decoded result of a base-32 string as a string
mTO_BASE64(string)Converts the string to base-64 encoded form and returns a encoded string
b mFROM_BASE64(string)Returns the decoded result of a base-64 string as a string
bTO_HEX(binary)Converts binary into a hexadecimal varchar
bFROM_HEX(varchar)Converts a hexadecimal-encoded varchar into bytes
b o sLTRIM(string)Returns string with all blanks removed from the start
sMAP()Returns an empty map
sMAP(key, value [, key, value]*)Returns a map with the given key/value pairs
sMAP_CONCAT(map [, map]*)Concatenates one or more maps. If any input argument is NULL the function returns NULL. Note that calcite is using the LAST_WIN strategy
sMAP_CONTAINS_KEY(map, key)Returns whether map contains key
sMAP_ENTRIES(map)Returns the entries of the map as an array, the order of the entries is not defined
sMAP_KEYS(map)Returns the keys of the map as an array, the order of the entries is not defined
sMAP_VALUES(map)Returns the values of the map as an array, the order of the entries is not defined
sMAP_FROM_ARRAYS(array1, array2)Returns a map created from an array1 and array2. Note that the lengths of two arrays should be the same and calcite is using the LAST_WIN strategy
sMAP_FROM_ENTRIES(arrayOfRows)Returns a map created from an arrays of row with two fields. Note that the number of fields in a row must be 2. Note that calcite is using the LAST_WIN strategy
sSTR_TO_MAP(string [, stringDelimiter [, keyValueDelimiter]])Returns a map after splitting the string into key/value pairs using delimiters. Default delimiters are ‘,’ for stringDelimiter and ‘:’ for keyValueDelimiter. Note that calcite is using the LAST_WIN strategy
b m p sMD5(string)Calculates an MD5 128-bit checksum of string and returns it as a hex string
mMONTHNAME(date)Returns the name, in the connection's locale, of the month in datetime; for example, it returns ‘二月’ for both DATE ‘2020-02-10’ and TIMESTAMP ‘2020-02-10 10:10:10’
o sNVL(value1, value2)Returns value1 if value1 is not null, otherwise value2
bOFFSET(index)When indexing an array, wrapping index in OFFSET returns the value at the 0-based index; throws error if index is out of bounds
bORDINAL(index)Similar to OFFSET except index begins at 1
bPARSE_DATE(format, string)Uses format specified by format to convert string representation of date to a DATE value
bPARSE_DATETIME(format, string)Uses format specified by format to convert string representation of datetime to a TIMESTAMP value
bPARSE_TIME(format, string)Uses format specified by format to convert string representation of time to a TIME value
bPARSE_TIMESTAMP(format, string[, timeZone])Uses format specified by format to convert string representation of timestamp to a TIMESTAMP WITH LOCAL TIME ZONE value in timeZone
h sPARSE_URL(urlString, partToExtract [, keyToExtract] )Returns the specified partToExtract from the urlString. Valid values for partToExtract include HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO. keyToExtract specifies which query to extract
b sPOW(numeric1, numeric2)Returns numeric1 raised to the power numeric2
pRANDOM()Generates a random double between 0 and 1 inclusive
sREGEXP(string, regexp)Equivalent to string1 RLIKE string2
bREGEXP_CONTAINS(string, regexp)Returns whether string is a partial match for the regexp
bREGEXP_EXTRACT(string, regexp [, position [, occurrence]])Returns the substring in string that matches the regexp, starting search at position (default 1), and until locating the nth occurrence (default 1). Returns NULL if there is no match
bREGEXP_EXTRACT_ALL(string, regexp)Returns an array of all substrings in string that matches the regexp. Returns an empty array if there is no match
bREGEXP_INSTR(string, regexp [, position [, occurrence [, occurrence_position]]])Returns the lowest 1-based position of the substring in string that matches the regexp, starting search at position (default 1), and until locating the nth occurrence (default 1). Setting occurrence_position (default 0) to 1 returns the end position of substring + 1. Returns 0 if there is no match
m o p sREGEXP_LIKE(string, regexp [, flags])Equivalent to string1 RLIKE string2 with an optional parameter for search flags. Supported flags are: i: case-insensitive matchingc: case-sensitive matchingn: newline-sensitive matchings: non-newline-sensitive matchingm: multi-line
b m oREGEXP_REPLACE(string, regexp, rep [, pos [, occurrence [, matchType]]])Replaces all substrings of string that match regexp with rep at the starting pos in expr (if omitted, the default is 1), occurrence specifies which occurrence of a match to search for (if omitted, the default is 1), matchType specifies how to perform matching
bREGEXP_SUBSTR(string, regexp [, position [, occurrence]])Synonym for REGEXP_EXTRACT
b m p sREPEAT(string, integer)Returns a string consisting of string repeated of integer times; returns an empty string if integer is less than 1
b mREVERSE(string)Returns string with the order of the characters reversed
b m p sRIGHT(string, length)Returns the rightmost length characters from the string
h sstring1 RLIKE string2Whether string1 matches regex pattern string2 (similar to LIKE, but uses Java regex)
h sstring1 NOT RLIKE string2Whether string1 does not match regex pattern string2 (similar to NOT LIKE, but uses Java regex)
b o sRPAD(string, length[, pattern ])Returns a string or bytes value that consists of string appended to length with pattern
b o sRTRIM(string)Returns string with all blanks removed from the end
bSAFE_ADD(numeric1, numeric2)Returns numeric1 + numeric2, or NULL on overflow. Arguments are implicitly cast to one of the types BIGINT, DOUBLE, or DECIMAL
bSAFE_CAST(value AS type)Converts value to type, returning NULL if conversion fails
bSAFE_DIVIDE(numeric1, numeric2)Returns numeric1 / numeric2, or NULL on overflow or if numeric2 is zero. Arguments implicitly are cast to one of the types BIGINT, DOUBLE, or DECIMAL
bSAFE_MULTIPLY(numeric1, numeric2)Returns numeric1 * numeric2, or NULL on overflow. Arguments are implicitly cast to one of the types BIGINT, DOUBLE, or DECIMAL
bSAFE_NEGATE(numeric)Returns numeric * -1, or NULL on overflow. Arguments are implicitly cast to one of the types BIGINT, DOUBLE, or DECIMAL
bSAFE_OFFSET(index)Similar to OFFSET except null is returned if index is out of bounds
bSAFE_ORDINAL(index)Similar to OFFSET except index begins at 1 and null is returned if index is out of bounds
bSAFE_SUBTRACT(numeric1, numeric2)Returns numeric1 - numeric2, or NULL on overflow. Arguments are implicitly cast to one of the types BIGINT, DOUBLE, or DECIMAL
*SEC(numeric)Returns the secant of numeric in radians
*SECH(numeric)Returns the hyperbolic secant of numeric
b m p sSHA1(string)Calculates a SHA-1 hash value of string and returns it as a hex string
b pSHA256(string)Calculates a SHA-256 hash value of string and returns it as a hex string
b pSHA512(string)Calculates a SHA-512 hash value of string and returns it as a hex string
*SINH(numeric)Returns the hyperbolic sine of numeric
b m o pSOUNDEX(string)Returns the phonetic representation of string; throws if string is encoded with multi-byte encoding such as UTF-8
sSOUNDEX(string)Returns the phonetic representation of string; return original string if string is encoded with multi-byte encoding such as UTF-8
m sSPACE(integer)Returns a string of integer spaces; returns an empty string if integer is less than 1
bSPLIT(string [, delimiter ])Returns the string array of string split at delimiter (if omitted, default is comma). If the string is empty it returns an empty array, otherwise, if the delimiter is empty, it returns an array containing the original string.
f sSTARTSWITH(string1, string2)Returns whether string2 is a prefix of string1
b pSTARTS_WITH(string1, string2)Equivalent to STARTSWITH(string1, string2)
mSTRCMP(string, string)Returns 0 if both of the strings are same and returns -1 when the first argument is smaller than the second and 1 when the second one is smaller than the first one
b pSTRPOS(string, substring)Equivalent to POSITION(substring IN string)
b m o pSUBSTR(string, position [, substringLength ])Returns a portion of string, beginning at character position, substringLength characters long. SUBSTR calculates lengths using characters as defined by the input character set
*TANH(numeric)Returns the hyperbolic tangent of numeric
bTIME(hour, minute, second)Returns a TIME value hour, minute, second (all of type INTEGER)
bTIME(timestamp)Extracts the TIME from timestamp (a local time; BigQuery's DATETIME type)
bTIME(instant)Extracts the TIME from timestampLtz (an instant; BigQuery's TIMESTAMP type), assuming UTC
bTIME(instant, timeZone)Extracts the time from timestampLtz (an instant; BigQuery's TIMESTAMP type), in timeZone
bTIMESTAMP(string)Equivalent to CAST(string AS TIMESTAMP WITH LOCAL TIME ZONE)
bTIMESTAMP(string, timeZone)Equivalent to CAST(string AS TIMESTAMP WITH LOCAL TIME ZONE), converted to timeZone
bTIMESTAMP(date)Converts date to a TIMESTAMP WITH LOCAL TIME ZONE value (at midnight)
bTIMESTAMP(date, timeZone)Converts date to a TIMESTAMP WITH LOCAL TIME ZONE value (at midnight), in timeZone
bTIMESTAMP(timestamp)Converts timestamp to a TIMESTAMP WITH LOCAL TIME ZONE, assuming a UTC
bTIMESTAMP(timestamp, timeZone)Converts timestamp to a TIMESTAMP WITH LOCAL TIME ZONE, in timeZone
bTIMESTAMP_ADD(timestamp, interval)Returns the TIMESTAMP value that occurs interval after timestamp
bTIMESTAMP_DIFF(timestamp, timestamp2, timeUnit)Returns the whole number of timeUnit between timestamp and timestamp2. Equivalent to TIMESTAMPDIFF(timeUnit, timestamp2, timestamp) and (timestamp - timestamp2) timeUnit
b sTIMESTAMP_MICROS(integer)Returns the TIMESTAMP that is integer microseconds after 1970-01-01 00:00:00
b sTIMESTAMP_MILLIS(integer)Returns the TIMESTAMP that is integer milliseconds after 1970-01-01 00:00:00
b sTIMESTAMP_SECONDS(integer)Returns the TIMESTAMP that is integer seconds after 1970-01-01 00:00:00
bTIMESTAMP_SUB(timestamp, interval)Returns the TIMESTAMP value that is interval before timestamp
bTIMESTAMP_TRUNC(timestamp, timeUnit)Truncates timestamp to the granularity of timeUnit, rounding to the beginning of the unit
bTIME_ADD(time, interval)Adds interval to time, independent of any time zone
bTIME_DIFF(time, time2, timeUnit)Returns the whole number of timeUnit between time and time2
bTIME_SUB(time, interval)Returns the TIME value that is interval before time
bTIME_TRUNC(time, timeUnit)Truncates time to the granularity of timeUnit, rounding to the beginning of the unit
m o pTO_CHAR(timestamp, format)Converts timestamp to a string using the format format
bTO_CODE_POINTS(string)Converts string to an array of integers that represent code points or extended ASCII character values
o pTO_DATE(string, format)Converts string to a date using the format format
o pTO_TIMESTAMP(string, format)Converts string to a timestamp using the format format
b o p sTRANSLATE(expr, fromString, toString)Returns expr with all occurrences of each character in fromString replaced by its corresponding character in toString. Characters in expr that are not in fromString are not replaced
bTRUNC(numeric1 [, integer2 ])Truncates numeric1 to optionally integer2 (if not specified 0) places right to the decimal point
qTRY_CAST(value AS type)Converts value to type, returning NULL if conversion fails
b sUNIX_MICROS(timestamp)Returns the number of microseconds since 1970-01-01 00:00:00
b sUNIX_MILLIS(timestamp)Returns the number of milliseconds since 1970-01-01 00:00:00
b sUNIX_SECONDS(timestamp)Returns the number of seconds since 1970-01-01 00:00:00
b sUNIX_DATE(date)Returns the number of days since 1970-01-01
sURL_DECODE(string)Decodes a string in ‘application/x-www-form-urlencoded’ format using a specific encoding scheme, returns original string when decoded error
sURL_ENCODE(string)Translates a string into ‘application/x-www-form-urlencoded’ format using a specific encoding scheme
oXMLTRANSFORM(xml, xslt)Applies XSLT transform xslt to XML string xml and returns the result

Note:

  • Calcite has no Redshift library, so the Postgres library is used instead. The functions DATEADD, DATEDIFF are implemented in Redshift and not Postgres but nevertheless appear in Calcite's Postgres library
  • Functions DATEADD, DATEDIFF, DATE_PART require the Babel parser
  • JSON_TYPE / JSON_DEPTH / JSON_PRETTY / JSON_STORAGE_SIZE return null if the argument is null
  • JSON_LENGTH / JSON_KEYS / JSON_REMOVE return null if the first argument is null
  • JSON_TYPE generally returns an upper-case string flag indicating the type of the JSON input. Currently supported supported type flags are:
    • INTEGER
    • STRING
    • FLOAT
    • DOUBLE
    • LONG
    • BOOLEAN
    • DATE
    • OBJECT
    • ARRAY
    • NULL
  • JSON_DEPTH defines a JSON value's depth as follows:
    • An empty array, empty object, or scalar value has depth 1;
    • A non-empty array containing only elements of depth 1 or non-empty object containing only member values of depth 1 has depth 2;
    • Otherwise, a JSON document has depth greater than 2.
  • JSON_LENGTH defines a JSON value's length as follows:
    • A scalar value has length 1;
    • The length of array or object is the number of elements is contains.

Dialect-specific aggregate functions.

COperator syntaxDescription
cAGGREGATE(m)Computes measure m in the context of the current GROUP BY key
b pARRAY_AGG( [ ALL | DISTINCT ] value [ RESPECT NULLS | IGNORE NULLS ] [ ORDER BY orderItem [, orderItem ]* ] )Gathers values into arrays
b pARRAY_CONCAT_AGG( [ ALL | DISTINCT ] value [ ORDER BY orderItem [, orderItem ]* ] )Concatenates arrays into arrays
p sBOOL_AND(condition)Synonym for EVERY
p sBOOL_OR(condition)Synonym for SOME
bCOUNTIF(condition)Returns the number of rows for which condition is TRUE; equivalent to COUNT(*) FILTER (WHERE condition)
mGROUP_CONCAT( [ ALL | DISTINCT ] value [, value ]* [ ORDER BY orderItem [, orderItem ]* ] [ SEPARATOR separator ] )MySQL-specific variant of LISTAGG
bLOGICAL_AND(condition)Synonym for EVERY
bLOGICAL_OR(condition)Synonym for SOME
sMAX_BY(value, comp)Synonym for ARG_MAX
sMIN_BY(value, comp)Synonym for ARG_MIN
bPERCENTILE_CONT(value, fraction [ RESPECT NULLS | IGNORE NULLS ] ) OVER windowSpecSynonym for standard PERCENTILE_CONT where PERCENTILE_CONT(value, fraction) OVER (ORDER BY value) is equivalent to standard PERCENTILE_CONT(fraction) WITHIN GROUP (ORDER BY value)
bPERCENTILE_DISC(value, fraction [ RESPECT NULLS | IGNORE NULLS ] ) OVER windowSpecSynonym for standard PERCENTILE_DISC where PERCENTILE_DISC(value, fraction) OVER (ORDER BY value) is equivalent to standard PERCENTILE_DISC(fraction) WITHIN GROUP (ORDER BY value)
b pSTRING_AGG( [ ALL | DISTINCT ] value [, separator] [ ORDER BY orderItem [, orderItem ]* ] )Synonym for LISTAGG

Usage Examples:

JSON_TYPE example

SQL

{% highlight sql %} SELECT JSON_TYPE(v) AS c1, JSON_TYPE(JSON_VALUE(v, ‘lax $.b’ ERROR ON ERROR)) AS c2, JSON_TYPE(JSON_VALUE(v, ‘strict $.a[0]’ ERROR ON ERROR)) AS c3, JSON_TYPE(JSON_VALUE(v, ‘strict $.a[1]’ ERROR ON ERROR)) AS c4 FROM (VALUES (‘{“a”: [10, true],“b”: “[10, true]”}’)) AS t(v) LIMIT 10; {% endhighlight %}

Result

c1c2c3c4
OBJECTARRAYINTEGERBOOLEAN
JSON_DEPTH example

SQL

{% highlight sql %} SELECT JSON_DEPTH(v) AS c1, JSON_DEPTH(JSON_VALUE(v, ‘lax $.b’ ERROR ON ERROR)) AS c2, JSON_DEPTH(JSON_VALUE(v, ‘strict $.a[0]’ ERROR ON ERROR)) AS c3, JSON_DEPTH(JSON_VALUE(v, ‘strict $.a[1]’ ERROR ON ERROR)) AS c4 FROM (VALUES (‘{“a”: [10, true],“b”: “[10, true]”}’)) AS t(v) LIMIT 10; {% endhighlight %}

Result

c1c2c3c4
3211
JSON_LENGTH example

SQL

{% highlight sql %} SELECT JSON_LENGTH(v) AS c1, JSON_LENGTH(v, ‘lax $.a’) AS c2, JSON_LENGTH(v, ‘strict $.a[0]’) AS c3, JSON_LENGTH(v, ‘strict $.a[1]’) AS c4 FROM (VALUES (‘{“a”: [10, true]}’)) AS t(v) LIMIT 10; {% endhighlight %}

Result

c1c2c3c4
1211
JSON_INSERT example

SQL

SELECT JSON_INSERT(v, '$.a', 10, '$.c', '[1]') AS c1,
  JSON_INSERT(v, '$', 10, '$.c', '[1]') AS c2
FROM (VALUES ('{"a": [10, true]}')) AS t(v)
LIMIT 10;

Result

c1c2
{“a”:1 , “b”:[2] , “c”:“[1]”}{“a”:1 , “b”:[2] , “c”:“[1]”}
JSON_KEYS example

SQL

{% highlight sql %} SELECT JSON_KEYS(v) AS c1, JSON_KEYS(v, ‘lax $.a’) AS c2, JSON_KEYS(v, ‘lax $.b’) AS c2, JSON_KEYS(v, ‘strict $.a[0]’) AS c3, JSON_KEYS(v, ‘strict $.a[1]’) AS c4 FROM (VALUES (‘{“a”: [10, true],“b”: {“c”: 30}}’)) AS t(v) LIMIT 10; {% endhighlight %}

Result

c1c2c3c4c5
[“a”, “b”]NULL[“c”]NULLNULL
JSON_REMOVE example

SQL

{% highlight sql %} SELECT JSON_REMOVE(v, ‘$[1]’) AS c1 FROM (VALUES (‘[“a”, [“b”, “c”], “d”]’)) AS t(v) LIMIT 10; {% endhighlight %}

Result

c1
[“a”, “d”]
JSON_REPLACE example

SQL

SELECT
JSON_REPLACE(v, '$.a', 10, '$.c', '[1]') AS c1,
JSON_REPLACE(v, '$', 10, '$.c', '[1]') AS c2
FROM (VALUES ('{\"a\": 1,\"b\":[2]}')) AS t(v)
limit 10;

Result

c1c2
{“a”:1 , “b”:[2] , “c”:“[1]”}{“a”:1 , “b”:[2] , “c”:“[1]”}")
JSON_SET example

SQL

SELECT
JSON_SET(v, '$.a', 10, '$.c', '[1]') AS c1,
JSON_SET(v, '$', 10, '$.c', '[1]') AS c2
FROM (VALUES ('{\"a\": 1,\"b\":[2]}')) AS t(v)
limit 10;

Result

c1c2
{“a”:10, “b”:[2]}10
JSON_STORAGE_SIZE example

SQL

{% highlight sql %} SELECT JSON_STORAGE_SIZE(‘[100, "sakila", [1, 3, 5], 425.05]’) AS c1, JSON_STORAGE_SIZE(‘{"a": 10, "b": "a", "c": "[1, 3, 5, 7]"}’) AS c2, JSON_STORAGE_SIZE(‘{"a": 10, "b": "xyz", "c": "[1, 3, 5, 7]"}’) AS c3, JSON_STORAGE_SIZE(‘[100, "json", [[10, 20, 30], 3, 5], 425.05]’) AS c4 limit 10; {% endhighlight %}

Result

c1c2c3c4
29353736

DECODE example

SQL

{% highlight sql %} SELECT DECODE(f1, 1, ‘aa’, 2, ‘bb’, 3, ‘cc’, 4, ‘dd’, ‘ee’) as c1, DECODE(f2, 1, ‘aa’, 2, ‘bb’, 3, ‘cc’, 4, ‘dd’, ‘ee’) as c2, DECODE(f3, 1, ‘aa’, 2, ‘bb’, 3, ‘cc’, 4, ‘dd’, ‘ee’) as c3, DECODE(f4, 1, ‘aa’, 2, ‘bb’, 3, ‘cc’, 4, ‘dd’, ‘ee’) as c4, DECODE(f5, 1, ‘aa’, 2, ‘bb’, 3, ‘cc’, 4, ‘dd’, ‘ee’) as c5 FROM (VALUES (1, 2, 3, 4, 5)) AS t(f1, f2, f3, f4, f5); {% endhighlight %}

Result

c1c2c3c4c5
aabbccddee

TRANSLATE example

SQL

{% highlight sql %} SELECT TRANSLATE(‘AaBbCc’‘D*d’, ' */‘‘%’, ‘_’) as c1, TRANSLATE(‘Aa/Bb/Cc’’D/d', ' */‘‘%’, ‘_’) as c2, TRANSLATE(‘Aa Bb Cc’’D d', ' */‘‘%’, ‘_’) as c3, TRANSLATE(‘Aa%Bb%Cc’’D%d', ' */'‘%’, ‘_’) as c4 FROM (VALUES (true)) AS t(f0); {% endhighlight %}

Result

c1c2c3c4
Aa_Bb_CcD_dAa_Bb_CcD_dAa_Bb_CcD_dAa_Bb_CcD_d

Higher-order Functions

A higher-order function takes one or more lambda expressions as arguments.

Lambda Expression Syntax: {% highlight sql %} lambdaExpression: parameters ‘->’ expression

parameters: ‘(’ [ identifier [, identifier ] ] ‘)’ | identifier {% endhighlight %}

Higher-order functions are not included in the SQL standard, so all the functions will be listed in the [Dialect-specific OperatorsPermalink]({{ site.baseurl }}/docs/reference.html#dialect-specific-operators) as well.

Examples of functions with a lambda argument are EXISTS.

User-defined functions

Calcite is extensible. You can define each kind of function using user code. For each kind of function there are often several ways to define a function, varying from convenient to efficient.

To implement a scalar function, there are 3 options:

  • Create a class with a public static eval method, and register the class;
  • Create a class with a public non-static eval method, and a public constructor with no arguments, and register the class;
  • Create a class with one or more public static methods, and register each class/method combination.

To implement an aggregate function, there are 2 options:

  • Create a class with public static init, add and result methods, and register the class;
  • Create a class with public non-static init, add and result methods, and a public constructor with no arguments, and register the class.

Optionally, add a public merge method to the class; this allows Calcite to generate code that merges sub-totals.

Optionally, make your class implement the [SqlSplittableAggFunction]({{ site.apiRoot }}/org/apache/calcite/sql/SqlSplittableAggFunction.html) interface; this allows Calcite to decompose the function across several stages of aggregation, roll up from summary tables, and push it through joins.

To implement a table function, there are 3 options:

  • Create a class with a static eval method that returns [ScannableTable]({{ site.apiRoot }}/org/apache/calcite/schema/ScannableTable.html) or [QueryableTable]({{ site.apiRoot }}/org/apache/calcite/schema/QueryableTable.html), and register the class;
  • Create a class with a non-static eval method that returns [ScannableTable]({{ site.apiRoot }}/org/apache/calcite/schema/ScannableTable.html) or [QueryableTable]({{ site.apiRoot }}/org/apache/calcite/schema/QueryableTable.html), and register the class;
  • Create a class with one or more public static methods that return [ScannableTable]({{ site.apiRoot }}/org/apache/calcite/schema/ScannableTable.html) or [QueryableTable]({{ site.apiRoot }}/org/apache/calcite/schema/QueryableTable.html), and register each class/method combination.

To implement a table macro, there are 3 options:

  • Create a class with a static eval method that returns [TranslatableTable]({{ site.apiRoot }}/org/apache/calcite/schema/TranslatableTable.html), and register the class;
  • Create a class with a non-static eval method that returns [TranslatableTable]({{ site.apiRoot }}/org/apache/calcite/schema/TranslatableTable.html), and register the class;
  • Create a class with one or more public static methods that return [TranslatableTable]({{ site.apiRoot }}/org/apache/calcite/schema/TranslatableTable.html), and register each class/method combination.

Calcite deduces the parameter types and result type of a function from the parameter and return types of the Java method that implements it. Further, you can specify the name and optionality of each parameter using the [Parameter]({{ site.apiRoot }}/org/apache/calcite/linq4j/function/Parameter.html) annotation.

Calling functions with named and optional parameters

Usually when you call a function, you need to specify all of its parameters, in order. But that can be a problem if a function has a lot of parameters, and especially if you want to add more parameters over time.

To solve this problem, the SQL standard allows you to pass parameters by name, and to define parameters which are optional (that is, have a default value that is used if they are not specified).

Suppose you have a function f, declared as in the following pseudo syntax:

{% highlight sql %} FUNCTION f( INTEGER a, INTEGER b DEFAULT NULL, INTEGER c, INTEGER d DEFAULT NULL, INTEGER e DEFAULT NULL) RETURNS INTEGER {% endhighlight %}

All of the function's parameters have names, and parameters b, d and e have a default value of NULL and are therefore optional. (In Calcite, NULL is the only allowable default value for optional parameters; this may change in future.)

When calling a function with optional parameters, you can omit optional arguments at the end of the list, or use the DEFAULT keyword for any optional arguments. Here are some examples:

  • f(1, 2, 3, 4, 5) provides a value to each parameter, in order;
  • f(1, 2, 3, 4) omits e, which gets its default value, NULL;
  • f(1, DEFAULT, 3) omits d and e, and specifies to use the default value of b;
  • f(1, DEFAULT, 3, DEFAULT, DEFAULT) has the same effect as the previous example;
  • f(1, 2) is not legal, because c is not optional;
  • f(1, 2, DEFAULT, 4) is not legal, because c is not optional.

You can specify arguments by name using the => syntax. If one argument is named, they all must be. Arguments may be in any other, but must not specify any argument more than once, and you need to provide a value for every parameter which is not optional. Here are some examples:

  • f(c => 3, d => 1, a => 0) is equivalent to f(0, NULL, 3, 1, NULL);
  • f(c => 3, d => 1) is not legal, because you have not specified a value for a and a is not optional.

SQL Hints

A hint is an instruction to the optimizer. When writing SQL, you may know information about the data unknown to the optimizer. Hints enable you to make decisions normally made by the optimizer.

  • Planner enforcers: there's no perfect planner, so it makes sense to implement hints to allow user better control the execution. For instance: “never merge this subquery with others” (/*+ no_merge */); “treat those tables as leading ones" (/*+ leading */) to affect join ordering, etc;
  • Append meta data/statistics: some statistics like “table index for scan” or “skew info of some shuffle keys” are somehow dynamic for the query, it would be very convenient to config them with hints because our planning metadata from the planner is very often not very accurate;
  • Operator resource constraints: for many cases, we would give a default resource configuration for the execution operators, i.e. min parallelism, memory (resource consuming UDF), special resource requirement (GPU or SSD disk) ... It would be very flexible to profile the resource with hints per query (not the Job).

Syntax

Calcite supports hints in two locations:

  • Query Hint: right after the SELECT keyword;
  • Table Hint: right after the referenced table name.

For example: {% highlight sql %} SELECT /*+ hint1, hint2(a=1, b=2) / ... FROM tableName /+ hint3(5, ‘x’) / JOIN tableName /+ hint4(c=id), hint5 */ ... {% endhighlight %}

The syntax is as follows:

{% highlight sql %} hintComment: ‘/*+’ hint [, hint ]* ‘*/’

hint: hintName | hintName ‘(’ optionKey ‘=’ optionVal [, optionKey ‘=’ optionVal ]* ‘)’ | hintName ‘(’ hintOption [, hintOption ]* ‘)’

optionKey: simpleIdentifier | stringLiteral

optionVal: stringLiteral

hintOption: simpleIdentifier | numericLiteral | stringLiteral {% endhighlight %}

It is experimental in Calcite, and yet not fully implemented, what we have implemented are:

  • The parser support for the syntax above;
  • RelHint to represent a hint item;
  • Mechanism to propagate the hints, during sql-to-rel conversion and planner planning.

We do not add any builtin hint items yet, would introduce more if we think the hints is stable enough.

MATCH_RECOGNIZE

MATCH_RECOGNIZE is a SQL extension for recognizing sequences of events in complex event processing (CEP).

It is experimental in Calcite, and yet not fully implemented.

Syntax

{% highlight sql %} matchRecognize: MATCH_RECOGNIZE ‘(’ [ PARTITION BY expression [, expression ]* ] [ ORDER BY orderItem [, orderItem ]* ] [ MEASURES measureColumn [, measureColumn ]* ] [ ONE ROW PER MATCH | ALL ROWS PER MATCH ] [ AFTER MATCH skip ] PATTERN ‘(’ pattern ‘)’ [ WITHIN intervalLiteral ] [ SUBSET subsetItem [, subsetItem ]* ] DEFINE variable AS condition [, variable AS condition ]* ‘)’

skip: SKIP TO NEXT ROW | SKIP PAST LAST ROW | SKIP TO FIRST variable | SKIP TO LAST variable | SKIP TO variable

subsetItem: variable = ‘(’ variable [, variable ]* ‘)’

measureColumn: expression AS alias

pattern: patternTerm [ ‘|’ patternTerm ]*

patternTerm: patternFactor [ patternFactor ]*

patternFactor: patternPrimary [ patternQuantifier ]

patternPrimary: variable | ‘$’ | ‘^’ | ‘(’ [ pattern ] ‘)’ | ‘{-’ pattern ‘-}’ | PERMUTE ‘(’ pattern [, pattern ]* ‘)’

patternQuantifier: ‘*’ | ‘*?’ | ‘+’ | ‘+?’ | ‘?’ | ‘??’ | ‘{’ { [ minRepeat ], [ maxRepeat ] } ‘}’ [‘?’] | ‘{’ repeat ‘}’

intervalLiteral: INTERVAL ‘string’ timeUnit [ TO timeUnit ] {% endhighlight %}

In patternQuantifier, repeat is a positive integer, and minRepeat and maxRepeat are non-negative integers.

DDL Extensions

DDL extensions are only available in the calcite-server module. To enable, include calcite-server.jar in your class path, and add parserFactory=org.apache.calcite.sql.parser.ddl.SqlDdlParserImpl#FACTORY to the JDBC connect string (see connect string property [parserFactory]({{ site.apiRoot }}/org/apache/calcite/config/CalciteConnectionProperty.html#PARSER_FACTORY)).

{% highlight sql %} ddlStatement: createSchemaStatement | createForeignSchemaStatement | createTableStatement | createTableLikeStatement | createViewStatement | createMaterializedViewStatement | createTypeStatement | createFunctionStatement | dropSchemaStatement | dropForeignSchemaStatement | dropTableStatement | dropViewStatement | dropMaterializedViewStatement | dropTypeStatement | dropFunctionStatement

createSchemaStatement: CREATE [ OR REPLACE ] SCHEMA [ IF NOT EXISTS ] name

createForeignSchemaStatement: CREATE [ OR REPLACE ] FOREIGN SCHEMA [ IF NOT EXISTS ] name ( TYPE ‘type’ | LIBRARY ‘com.example.calcite.ExampleSchemaFactory’ ) [ OPTIONS ‘(’ option [, option ]* ‘)’ ]

option: name literal

createTableStatement: CREATE TABLE [ IF NOT EXISTS ] name [ ‘(’ tableElement [, tableElement ]* ‘)’ ] [ AS query ]

createTableLikeStatement: CREATE TABLE [ IF NOT EXISTS ] name LIKE sourceTable [ likeOption [, likeOption ]* ]

likeOption: { INCLUDING | EXCLUDING } { DEFAULTS | GENERATED | ALL }

createTypeStatement: CREATE [ OR REPLACE ] TYPE name AS { baseType | ‘(’ attributeDef [, attributeDef ]* ‘)’ }

attributeDef: attributeName type [ COLLATE collation ] [ NULL | NOT NULL ] [ DEFAULT expression ]

tableElement: columnName type [ columnGenerator ] [ columnConstraint ] | columnName | tableConstraint

columnGenerator: DEFAULT expression | [ GENERATED ALWAYS ] AS ‘(’ expression ‘)’ { VIRTUAL | STORED }

columnConstraint: [ CONSTRAINT name ] [ NOT ] NULL

tableConstraint: [ CONSTRAINT name ] { CHECK ‘(’ expression ‘)’ | PRIMARY KEY ‘(’ columnName [, columnName ]* ‘)’ | UNIQUE ‘(’ columnName [, columnName ]* ‘)’ }

createViewStatement: CREATE [ OR REPLACE ] VIEW name [ ‘(’ columnName [, columnName ]* ‘)’ ] AS query

createMaterializedViewStatement: CREATE MATERIALIZED VIEW [ IF NOT EXISTS ] name [ ‘(’ columnName [, columnName ]* ‘)’ ] AS query

createFunctionStatement: CREATE [ OR REPLACE ] FUNCTION [ IF NOT EXISTS ] name AS classNameLiteral [ USING usingFile [, usingFile ]* ]

usingFile: { JAR | FILE | ARCHIVE } filePathLiteral

dropSchemaStatement: DROP SCHEMA [ IF EXISTS ] name

dropForeignSchemaStatement: DROP FOREIGN SCHEMA [ IF EXISTS ] name

dropTableStatement: DROP TABLE [ IF EXISTS ] name

dropViewStatement: DROP VIEW [ IF EXISTS ] name

dropMaterializedViewStatement: DROP MATERIALIZED VIEW [ IF EXISTS ] name

dropTypeStatement: DROP TYPE [ IF EXISTS ] name

dropFunctionStatement: DROP FUNCTION [ IF EXISTS ] name

truncateTableStatement: TRUNCATE TABLE name [ CONTINUE IDENTITY | RESTART IDENTITY ] {% endhighlight %}

In createTableStatement, if you specify AS query, you may omit the list of tableElements, or you can omit the data type of any tableElement, in which case it just renames the underlying column.

In columnGenerator, if you do not specify VIRTUAL or STORED for a generated column, VIRTUAL is the default.

In createFunctionStatement and usingFile, classNameLiteral and filePathLiteral are character literals.

Declaring objects for user-defined types

After an object type is defined and installed in the schema, you can use it to declare objects in any SQL block. For example, you can use the object type to specify the datatype of an attribute, column, variable, bind variable, record field, table element, formal parameter, or function result. At run time, instances of the object type are created; that is, objects of that type are instantiated. Each object can hold different values.

For example, we can declare types address_typ and employee_typ:

{% highlight sql %} CREATE TYPE address_typ AS ( street VARCHAR(30), city VARCHAR(20), state CHAR(2), postal_code VARCHAR(6));

CREATE TYPE employee_typ AS ( employee_id DECIMAL(6), first_name VARCHAR(20), last_name VARCHAR(25), email VARCHAR(25), phone_number VARCHAR(20), hire_date DATE, job_id VARCHAR(10), salary DECIMAL(8,2), commission_pct DECIMAL(2,2), manager_id DECIMAL(6), department_id DECIMAL(4), address address_typ); {% endhighlight %}

Using these types, you can instantiate objects as follows:

{% highlight sql %} employee_typ(315, ‘Francis’, ‘Logan’, ‘FLOGAN’, ‘555.777.2222’, DATE ‘2004-05-01’, ‘SA_MAN’, 11000, .15, 101, 110, address_typ(‘376 Mission’, ‘San Francisco’, ‘CA’, ‘94222’)) {% endhighlight %}