Optiq SQL language reference

SQL constructs

query:
  {
      select
  |   query UNION [ ALL ] query
  |   query EXCEPT query
  |   query INTERSECT query
  }
  [ ORDER BY orderItem [, orderItem ]* ]
  [ LIMIT { count | ALL } ]
  [ OFFSET start { ROW | ROWS } ]
  [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ]

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

select:
  SELECT [ ALL | DISTINCT ]
      { * | projectItem [, projectItem ]* }
  FROM tableExpression
  [ WHERE booleanExpression ]
  [ GROUP BY { () | expression [, expression]* } ]
  [ HAVING booleanExpression ]
  [ WINDOW windowName AS windowSpec [, windowName AS windowSpec ]* ]

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

tableExpression:
      tableReference [, tableReference ]*
  |   tableExpression [ NATURAL ] [ LEFT | RIGHT | FULL ] JOIN tableExpression [ joinCondition ]

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

tableReference:
  tablePrimary [ [ AS ] alias [ ( columnAlias [, columnAlias ]* ) ] ]

tablePrimary:
      [ TABLE ] [ [ catalogName . ] schemaName . ] tableName
  |   ( query )
  |   VALUES expression [, expression ]*
  |   ( TABLE expression )

windowRef:
      windowName
  |   windowSpec

windowSpec:
  [ windowName ]
  (
      [ ORDER BY orderItem [, orderItem ]* ]
      [ PARTITION BY expression [, expression ]* ]
      {
          RANGE numericOrInterval { PRECEDING | FOLLOWING }
      |
          ROWS numeric { PRECEDING | FOLLOWING }
      }
  )

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

A scalar sub-query is a sub-query used as an expression. It can occur in most places where an expression can occur (such as the SELECT clause, WHERE clause, or as an argument to an aggregate function). If the sub-query returns no rows, the value is NULL; if it returns more than one row, it is an error.

A sub-query can occur in the FROM clause of a query and also in IN and EXISTS expressions. A sub-query that occurs in IN and EXISTS expressions may be correlated; that is, refer to tables in the FROM clause of an enclosing query.

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 Optiq, 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 examples
BOOLEANLogical valuesValues: TRUE, FALSE, UNKNOWN
TINYINT1 byte signed integerRange is -255 to 256
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 is a DECIMAL(5, 2) value.
NUMERICFixed point
REAL, FLOAT4 byte floating point6 decimal digits precision
DOUBLE8 byte floating point15 decimal digits precision
CHAR(n), CHARACTER(n)Fixed-width character string‘Hello’, '' (empty string), _latin1‘Hello’, n‘Hello’, _UTF16‘Hello’, ‘Hello’ ‘there’ (literal split into multiple parts)
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’
TIMESTAMPDate and timeExample: TIMESTAMP ‘1969-07-20 20:17:40’
INTERVAL timeUnit [ TO timeUnit ]Date time intervalExamples: INTERVAL ‘1:5’ YEAR TO MONTH, INTERVAL ‘45’ DAY
Anchored intervalDate time intervalExample: (DATE ‘1969-07-20’, DATE ‘1972-08-29’)

Where:

timeUnit:
  YEAR | MONTH | DAY | HOUR | MINUTE | SECOND

Note:

  • DATE, TIME and TIMESTAMP have no time zone. 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.

Non-scalar types

TypeDescription
ANYA value of an unknown type
ROWRow with 1 or more columns
MAPCollection of keys mapped to values
MULTISETUnordered collection that may contain duplicates
ARRAYOrdered, contiguous collection that may contain duplicates
CURSORCursor over the result of executing a query

Operators and functions

Comparison operators

Operator syntaxDescription
value = value
value <> value
value > value
value >= value
value < value
value <= value
value IS NULL
value IS NOT NULL
value IS DISTINCT FROM value
value IS NOT DISTINCT FROM value
value BETWEEN value AND value
value NOT BETWEEN value AND value
string LIKE string
string NOT LIKE string
string SIMILAR TO string
string NOT SIMILAR TO string
string SIMILAR TO string ESCAPE string
string NOT SIMILAR TO string ESCAPE string
value IN (value [, value]* )
value NOT IN (value [, value]* )
value IN (sub-query)
value NOT IN (sub-query)
EXISTS (sub-query)

Logical operators

Operator syntaxDescription
boolean OR boolean
boolean AND boolean
NOT boolean
boolean IS FALSE
boolean IS NOT FALSE
boolean IS TRUE
boolean IS NOT TRUE
boolean IS UNKNOWN
boolean IS NOT UNKNOWN

Arithmetic operators and functions

Operator syntaxDescription
+ numeric
- numeric
numeric + numeric
numeric - numeric
numeric * numeric
numeric / numeric
POWER(numeric, numeric)
ABS(numeric)
MOD(numeric, numeric)
SQRT(numeric)
LN(numeric)
LOG10(numeric)
EXP(numeric)
CEIL(numeric)
FLOOR(numeric)

String operators and functions

Operator syntaxDescription
string || string
CHAR_LENGTH(string)
CHARACTER_LENGTH(string)
UPPER(string)
LOWER(string)
POSITION(string IN string)
TRIM( { BOTH ;| LEADING ;| TRAILING } string FROM string)
OVERLAY(string PLACING string FROM string)
OVERLAY(string PLACING string FROM integer)
SUBSTRING(string FROM integer)
SUBSTRING(string FROM integer FOR integer)
INITCAP(string)

Not implemented:

  • SUBSTRING(string FROM regexp FOR regexp)

Binary string operators and functions

Operator syntaxDescription
binary || binary
POSITION(binary IN binary)
SUBSTRING(binary FROM integer FOR integer)

Date/time functions

Operator syntaxDescription
LOCALTIME
LOCALTIME(n)
LOCALDATE
LOCALTIMESTAMP
LOCALTIMESTAMP(n)
CURRENT_TIME
CURRENT_DATE
CURRENT_TIMESTAMP
EXTRACT(timeUnit FROM datetime)

Not implemented:

  • EXTRACT(timeUnit FROM interval)
  • CEIL(interval)
  • FLOOR(interval)
  • datetime - datetime timeUnit [ TO timeUnit ]
  • interval OVERLAPS interval
    • interval
    • interval
  • interval + interval
  • interval - interval
  • interval / interval
  • datetime + interval
  • datetime - interval

System functions

Operator syntaxDescription
USER
CURRENT_USER
SESSION_USER
SYSTEM_USER
CURRENT_PATH
CURRENT_ROLE

Conditional functions and operators

Operator syntaxDescription
CASE value
WHEN value1 THEN result1
[ WHEN valueN THEN resultN ]*
[ ELSE resultZ ]
END
Simple case
CASE WHEN condition1 THEN result1 [ WHEN conditionN THEN resultN ]* [ ELSE resultZ ] ENDSearched 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.

Note implemented:

  • CASE value
    WHEN value11 [, value1M ]* THEN result1
    [ WHEN valueN1 [ , value NM ]* THEN resultN ]*
    [ ELSE resultZ ]
    END | Simple case with multiple values per THEN clause

Type conversion

Operator syntaxDescription
CAST(value AS type)

Value constructors

Operator syntaxDescription
ROW (value [, value]* )
(value [, value]* )
map [ key ]
array [ index ]
ARRAY [ value [, value ]* ]
MAP [ key, value [, key, value ]* ]

JDBC function escape

Numeric

Operator syntaxDescription
{fn LOG10(numeric)}
{fn POWER(numeric, numeric)}

Not implemented:

  • {fn ABS(numeric)}
  • {fn ACOS(numeric)}
  • {fn ASIN(numeric)}
  • {fn ATAN(numeric)}
  • {fn ATAN2(numeric, numeric)}
  • {fn CEILING(numeric)}
  • {fn COS(numeric)}
  • {fn COT(numeric)}
  • {fn DEGREES(numeric)}
  • {fn EXP(numeric)}
  • {fn FLOOR(numeric)}
  • {fn LOG(numeric)}
  • {fn LOG10(numeric)}
  • {fn MOD(numeric, numeric)}
  • {fn PI()}
  • {fn RADIANS(numeric)}
  • {fn RAND(numeric)}
  • {fn ROUND(numeric, numeric)}
  • {fn SIGN(numeric)}
  • {fn SIN(numeric)}
  • {fn SQRT(numeric)}
  • {fn TAN(numeric)}
  • {fn TRUNCATE(numeric, numeric)}

String

Operator syntaxDescription
{fn LOCATE(string, string)}
{fn INSERT(string, integer, integer, string)}
{fn LCASE(string)}

Not implemented:

  • {fn ASCII(string)}
  • {fn CHAR(string)}
  • {fn CONCAT(string, string)}
  • {fn DIFFERENCE(string, string)}
  • {fn LEFT(string, integer)}
  • {fn LENGTH(string)}
  • {fn LOCATE(string, string, integer)}
  • {fn LTRIM(string)}
  • {fn REPEAT(string, integer)}
  • {fn REPLACE(string, string, string)}
  • {fn RIGHT(string, integer)}
  • {fn RTRIM(string)}
  • {fn SOUNDEX(string)}
  • {fn SPACE(integer)}
  • {fn SUBSTRING(string, integer, integer)}
  • {fn UCASE(string)}

Date/time

Not implemented:

  • {fn CURDATE()}
  • {fn CURTIME()}
  • {fn DAYNAME(date)}
  • {fn DAYOFMONTH(date)}
  • {fn DAYOFWEEK(date)}
  • {fn DAYOFYEAR(date)}
  • {fn HOUR(time)}
  • {fn MINUTE(time)}
  • {fn MONTH(date)}
  • {fn MONTHNAME(date)}
  • {fn NOW()}
  • {fn QUARTER(date)}
  • {fn SECOND(time)}
  • {fn TIMESTAMPADD(interval, count, timestamp)}
  • {fn TIMESTAMPDIFF(interval, timestamp, timestamp)}
  • {fn WEEK(date)}
  • {fn YEAR(date)}

System

Not implemented:

  • {fn DATABASE()}
  • {fn IFNULL(value, value)}
  • {fn USER(value, value)}
  • {fn CONVERT(value, type)}

Aggregate functions

Operator syntaxDescription
COUNT( [ DISTINCT ] value)
COUNT(*)
AVG( [ DISTINCT ] numeric)
SUM( [ DISTINCT ] numeric)
MAX( [ DISTINCT ] value)
MIN( [ DISTINCT ] value)
STDDEV_POP( [ DISTINCT ] value)
STDDEV_SAMP( [ DISTINCT ] value)
VAR( [ DISTINCT ] value)
VAR_POP( [ DISTINCT ] value)

Window functions

Operator syntaxDescription
COUNT(value) OVER window
COUNT(*) OVER window
AVG(numeric) OVER window
SUM(numeric) OVER window
MAX(value) OVER window
MIN(value) OVER window
RANK() OVER window

Not implemented:

  • FIRST_VALUE() OVER window
  • LAST_VALUE() OVER window
  • DENSE_RANK(value) OVER window
  • PERCENT_RANK(value) OVER window
  • CUME_DIST(value) OVER window
  • ROW_NUMBER() OVER window