RAQL Query Syntax Reference
This topic contains syntax definitions and corresponding syntax diagrams for the Real-Time Analytics Query Language (RAQL) in Presto.
SelectStatement
Definition:
SelectStatement
::= SelectWithOrder ';'?
Used In:
This is the starting definition of RAQL queries, which has no parent references.
SelectWithOrder
Definition:
SelectWithOrder
::= QueryExpressionBody OrderByClause? LimitClause?
Used In:
QueryExpressionBody
Definition:
QueryExpressionBody
::= QueryTerm ( ( 'UNION' | 'EXCEPT' | 'MINUS' ) ( 'ALL' | 'DISTINCT' )? QueryTerm )*
Used In:
QueryTerm
Definition:
QueryTerm
::= QueryPrimary ( 'INTERSECT' ( 'ALL' | 'DISTINCT' )? QueryPrimary )*
Used In:
QueryPrimary
Definition:
QueryPrimary
::= SelectWithoutOrder
| '(' QueryExpressionBody ')'
Used In:
SelectWithoutOrder
Definition:
SelectWithoutOrder
::= 'SELECT' ( 'ALL' | 'DISTINCT' )? SelectList FromClause WhereClause? GroupByClause? HavingClause?
Used In:
SelectList
Definition:
SelectList
::= SelectItem ( ',' SelectItem )*
Used In:
SelectItem
Definition:
SelectItem
::= '*'
| SelectStar
| SqlSimpleExpression Alias?
Used In:
SelectStar
Definition:
SelectStar
::= ObjectName '.' ( ObjectName '.' )? '*'
Used In:
ObjectName
Definition:
ObjectName
::= S_IDENTIFIER
| S_QUOTED_IDENTIFIER
Used In:
SqlSimpleExpression
Definition:
SqlSimpleExpression
::= SqlMultiplicativeExpression ( ( '+' | '-' | '||' ) SqlMultiplicativeExpression )*
Used In:
SqlMultiplicativeExpression
Definition:
SqlMultiplicativeExpression
::= SqlExponentExpression ( ( '*' | '/' | '%' ) SqlExponentExpression )*
Used In:
SqlExponentExpression
Definition:
SqlExponentExpression
::= SqlUnaryExpression ( '**' SqlUnaryExpression )*
Used In:
SqlUnaryExpression
Definition:
SqlUnaryExpression
::= ( '+' | '-' )? SqlPrimaryExpression
Used In:
SqlPrimaryExpression
Definition:
SqlPrimaryExpression
::= '(' SqlExpression ')'
| Constant
| FunctionCall
| ( AggregateFunctionCall | GenericFunctionCall ) WindowSpecification?
| GroupingOperation
| WindowFunctionCall
| ObjectName ( '.' ( GenericFunctionCall WindowSpecification? | ObjectName ( '.' ObjectName )? ArrayElementReference? ) | ArrayElementReference )?
| ArrayValueConstructor
Used In:
SqlExpression
Definition:
SqlExpression
::= SqlAndExpression ( 'OR' SqlAndExpression )*
Used In:
SqlAndExpression
Definition:
SqlAndExpression
::= SqlUnaryLogicalExpression ( 'AND' SqlUnaryLogicalExpression )*
Used In:
SqlUnaryLogicalExpression
Definition:
SqlUnaryLogicalExpression
::= 'NOT'? SqlRelationalExpression
Used In:
SqlRelationalExpression
Definition:
SqlRelationalExpression
::= ( '(' SqlExpressionList ')' | SqlSimpleExpression ) ( SqlRelationalOperatorExpression | SqlInClause | SqlBetweenClause | SqlLikeClause | IsNullClause )?
Used In:
SqlExpressionList
Definition:
SqlExpressionList
::= SqlExpression ( ',' SqlExpression )*
Used In:
SqlRelationalOperatorExpression
Definition:
SqlRelationalOperatorExpression
::= Relop ( '(' QueryExpressionBody ')' | SqlSimpleExpression )
Used In:
Relop
Definition:
Relop ::= '='
| '!='
| '#'
| '<>'
| '>'
| '>='
| '<'
| '<='
Used In:
SqlInClause
Definition:
SqlInClause
::= 'NOT'? 'IN' '(' SqlExpressionList ')'
Used In:
SqlBetweenClause
Definition:
SqlBetweenClause
::= 'NOT'? 'BETWEEN' SqlSimpleExpression 'AND' SqlSimpleExpression
Used In:
SqlLikeClause
Definition:
SqlLikeClause
::= 'NOT'? 'LIKE' SqlSimpleExpression
Used In:
IsNullClause
Definition:
IsNullClause
::= 'IS' 'NOT'? 'NULL'
Used In:
Constant
Definition:
Constant ::= 'NULL'
| ( '+' | '-' )? S_NUMBER
| S_CHAR_LITERAL
| 'TRUE'
| 'FALSE'
Used In:
FunctionCall
Definition:
FunctionCall
::= ( ( ( 'ABS' | 'CEIL' | 'CEILING' | 'CHAR_LENGTH' | 'CHARACTER_LENGTH' | 'EXP' | 'FLOOR' | 'LN' | 'LOWER' | 'SQRT' | 'UPPER' ) '(' | ( 'MOD' | 'POWER' ) '(' SqlExpression ',' | 'TRIM' '(' ( ( 'LEADING' | 'TRAILING' | 'BOTH' )? SqlExpression? 'FROM' )? | 'SUBSTRING' '(' SqlExpression 'FROM' ( SqlExpression 'FOR' )? | ( 'ROUND' | 'TRUNC' | 'TRUNCATE' ) '(' ( SqlExpression ',' )? ) SqlExpression | 'CAST' '(' SqlExpression 'AS' CastableDataTypeDeclaration ) ')'
| 'CASE' ( SimpleCaseCall | BooleanCaseCall )
Used In:
CastableDataTypeDeclaration
Definition:
CastableDataTypeDeclaration
::= ( 'BIG_DECIMAL' | 'BYTE' | 'INTEGER' | 'FLOAT' | 'LONG' | 'SHORT' | 'DOUBLE' | 'CHARACTER' | 'STRING' | 'SQL_DATE' | 'SQL_TIMESTAMP' | 'SQL_TIME' | 'DATE' | 'BOOLEAN' | 'BLOB' | 'CLOB' | 'REF' | 'STRUCT' | 'UNKNOWN' ) 'ARRAY'?
Used In:
SimpleCaseCall
Definition:
SimpleCaseCall
::= SqlSimpleExpression ( 'WHEN' SqlSimpleExpression 'THEN' SqlSimpleExpression )+ ( 'ELSE' SqlSimpleExpression )? 'END'
Used In:
BooleanCaseCall
Definition:
BooleanCaseCall
::= ( 'WHEN' SqlExpression 'THEN' SqlSimpleExpression )+ ( 'ELSE' SqlSimpleExpression )? 'END'
Used In:
AggregateFunctionCall
Definition:
AggregateFunctionCall
::= ( ( 'AVG' | 'MAX' | 'MIN' | 'STDDEV_POP' | 'STDDEV' | 'STDDEV_SAMP' | 'SUM' | 'VAR_POP' | 'VARIANCE' | 'VAR_SAMP' ) '(' ( ( 'ALL' | 'DISTINCT' ) SourceField | SqlExpression ) | 'COUNT' '(' ( '*' | ( 'ALL' | 'DISTINCT' ) SourceField | SqlExpression ) | ( 'CORR' | 'COVAR_POP' | 'COVAR_SAMP' | 'COVAR' | 'REGR_INTERCEPT' | 'REGR_R2' | 'REGR_SLOPE' ) '(' ( SqlExpression | SourceField ) ',' ( SqlExpression | SourceField ) ) ')'
Used In:
SourceField
Definition:
SourceField
::= ObjectName ( '.' ObjectName ( '.' ObjectName )? )?
Used In:
GenericFunctionCall
Definition:
GenericFunctionCall
::= ObjectName '(' SqlExpressionList? ( ';' ConstantList )? ')'
Used In:
ConstantList
Definition:
ConstantList
::= Constant ( ',' Constant )*
Used In:
WindowSpecification
Definition:
WindowSpecification
::= 'OVER' '(' ( 'PARTITION' 'BY' SqlExpressionList )? OrderByClause? WindowFrameClause? ')'
Used In:
OrderByClause
Definition:
OrderByClause
::= 'ORDER' 'BY' SqlExpression ( 'ASC' | 'DESC' )? ( 'NULLS' ( 'FIRST' | 'LAST' ) )? ( ',' SqlExpression ( 'ASC' | 'DESC' )? ( 'NULLS' ( 'FIRST' | 'LAST' ) )? )*
Used In:
WindowFrameClause
Definition:
WindowFrameClause
::= ( 'ROWS' | 'RANGE' ) ( WindowFrameStart | 'BETWEEN' WindowFrameBound 'AND' WindowFrameBound )
Used In:
WindowFrameStart
Definition:
WindowFrameStart
::= ( 'UNBOUNDED' | FrameBound ) 'PRECEDING'
| 'CURRENT' 'ROW'
Used In:
FrameBound
Definition:
FrameBound
::= S_NUMBER
Used In:
WindowFrameBound
Definition:
WindowFrameBound
::= ( 'UNBOUNDED' | FrameBound ) 'FOLLOWING'
| WindowFrameStart
Used In:
GroupingOperation
Definition:
GroupingOperation
::= 'GROUPING' '(' SqlExpressionList ')'
Used In:
WindowFunctionCall
Definition:
WindowFunctionCall
::= ( ( 'RANK' | 'DENSE_RANK' | 'PERCENT_RANK' | 'CUME_DIST' | 'ROW_NUMBER' ) '(' | 'NTILE' '(' S_NUMBER | ( 'LEAD' | 'LAG' ) '(' ( SqlExpression | SourceField ) ( ',' S_NUMBER ( ',' Constant )? )? | ( ( 'FIRST_VALUE' | 'LAST_VALUE' ) '(' ( SqlExpression | SourceField ) | 'NTH_VALUE' '(' ( SqlExpression | SourceField ) ',' S_NUMBER ) ( ',' Constant )? ) ')' WindowSpecification
Used In:
ArrayElementReference
Definition:
ArrayElementReference
::= '[' S_NUMBER ']'
Used In:
ArrayValueConstructor
Definition:
ArrayValueConstructor
::= 'ARRAY' '[' SqlExpressionList ']'
Used In:
Alias
Definition:
Alias ::= 'AS'? ObjectName
Used In:
FromClause
Definition:
FromClause
::= 'FROM' FromItem ( ',' FromItem )*
Used In:
FromItem
Definition:
FromItem ::= SourceReferenceOrSubquery JoinedTable*
Used In:
SourceReferenceOrSubquery
Definition:
SourceReferenceOrSubquery
::= ( SourceReference | '(' SelectWithOrder ')' ) Alias?
Used In:
SourceReference
Definition:
SourceReference
::= ObjectName ( '.' ObjectName )? ( '/' ObjectName ( '.' ObjectName )? )*
Used In:
JoinedTable
Definition:
JoinedTable
::= ( 'CROSS' | 'NATURAL' 'INNER'? ) 'JOIN' SourceReferenceOrSubquery
| 'INNER'? 'JOIN' SourceReferenceOrSubquery ( 'ON' SqlExpression | 'USING' '(' SourceFieldList ')' )?
Used In:
SourceFieldList
Definition:
SourceFieldList
::= SourceField ( ',' SourceField )*
Used In:
WhereClause
Definition:
WhereClause
::= 'WHERE' SqlExpression
Used In:
GroupByClause
Definition:
GroupByClause
::= 'GROUP' 'BY' ( 'ALL' | 'DISTINCT' )? GroupingElementList
Used In:
GroupingElementList
Definition:
GroupingElementList
::= GroupingElement ( ',' GroupingElement )*
Used In:
GroupingElement
Definition:
GroupingElement
::= EmptyGroupingSet
| OrdinaryGroupingSet
| RollupList
| CubeList
| GroupingSetsSpecification
Used In:
EmptyGroupingSet
Definition:
EmptyGroupingSet
::= '(' ')'
Used In:
OrdinaryGroupingSet
Definition:
OrdinaryGroupingSet
::= '(' GroupingExpressionReferenceList ')'
| GroupingExpressionReference
Used In:
GroupingExpressionReferenceList
Definition:
GroupingExpressionReferenceList
::= GroupingExpressionReference ( ',' GroupingExpressionReference )*
Used In:
GroupingExpressionReference
Definition:
GroupingExpressionReference
::= SqlExpression
Used In:
RollupList
Definition:
RollupList
::= 'ROLLUP' '(' OrdinaryGroupingSetList ')'
Used In:
OrdinaryGroupingSetList
Definition:
OrdinaryGroupingSetList
::= OrdinaryGroupingSet ( ',' OrdinaryGroupingSet )*
Used In:
CubeList
Definition:
CubeList ::= 'CUBE' '(' OrdinaryGroupingSetList ')'
Used In:
GroupingSetsSpecification
Definition:
GroupingSetsSpecification
::= 'GROUPING' 'SETS' '(' GroupingSetList ')'
Used In:
GroupingSetList
Definition:
GroupingSetList
::= GroupingSet ( ',' GroupingSet )*
Used In:
GroupingSet
Definition:
GroupingSet
::= EmptyGroupingSet
| OrdinaryGroupingSet
| RollupList
| CubeList
| GroupingSetsSpecification
Used In:
HavingClause
Definition:
HavingClause
::= 'HAVING' SqlExpression
Used In:
LimitClause
Definition:
LimitClause
::= 'LIMIT' S_NUMBER ( ',' S_NUMBER )?
Used In:
S_NUMBER
Definition:
S_NUMBER ::= FLOAT ( [Ee] [-+]? INTEGER )?
Used In:
FLOAT
Definition:
FLOAT ::= ( INTEGER? '.' )? INTEGER
Used In:
INTEGER
Definition:
INTEGER ::= DIGIT+
Used In:
DIGIT
Definition:
DIGIT ::= [0-9]
Used In:
S_CHAR_LITERAL
Definition:
S_CHAR_LITERAL
::= "'" ( "'" "'" | [^'] )* "'"
Used In:
S_IDENTIFIER
Definition:
S_IDENTIFIER
::= NAMESTARTCHAR NAMECHAR*
Used In:
NAMESTARTCHAR
Definition:
NAMESTARTCHAR
::= [:A-Z_a-z#x00C0-#x00D6#x00D8-#x00F6#x00F8-#x02FF#x0370-#x037D#x037F-#x1FFF#x200C-#x200D#x2070-#x218F#x2C00-#x2FEF#x3001-#xD7FF#xF900-#xFDCF#xFDF0-#xFFFD]
Used In:
NAMECHAR
Definition:
NAMECHAR ::= NAMESTARTCHAR
| [$0-9#x00B7#x0300-#x036F#x203F-#x2040]
Used In:
S_QUOTED_IDENTIFIER
Definition:
S_QUOTED_IDENTIFIER
::= '"' [^"#x000A#x000D]* '"'
Used In:
IgnorableWhitespace
Definition:
IgnorableWhitespace
::= [ #x0009#x000A#x000D]
| LINE_COMMENT
| MULTI_LINE_COMMENT
LINE_COMMENT
Definition:
LINE_COMMENT
::= '--' [^#x000A#x000D]*
Used In:
MULTI_LINE_COMMENT
Definition:
MULTI_LINE_COMMENT
::= '/' '*' ( [^*] | '*'+ [^*/] )* '*'+ '/'
Used In: