Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions src/grammar/impala/ImpalaSqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -344,18 +344,18 @@ deleteStatement
;

delete
: KW_DELETE KW_FROM? tableNamePath (KW_WHERE booleanExpression)?
: KW_DELETE KW_FROM? tableNamePath (whereClause)?
;

deleteTableRef
: KW_DELETE tableNamePath (KW_AS? identifier)? KW_FROM (relation (COMMA relation)*)? (
KW_WHERE booleanExpression
whereClause
)?
;

updateStatement
: KW_UPDATE tableNamePath KW_SET assignmentList (KW_FROM relation (COMMA relation)*)? (
KW_WHERE booleanExpression
whereClause
)?
;

Expand Down Expand Up @@ -563,6 +563,10 @@ columnNamePath
| {this.shouldMatchEmpty()}?
;

columnName
: qualifiedName
;

tableOrViewPath
: tableNamePath
| viewNamePath
Expand Down Expand Up @@ -769,9 +773,15 @@ sortItem
querySpecification
: KW_SELECT setQuantifier? (KW_STRAIGHT_JOIN)? selectItem (COMMA selectItem)* (
KW_FROM relation (COMMA relation)*
)? (KW_WHERE where=booleanExpression)? (KW_GROUP KW_BY groupBy)? (
KW_HAVING having=booleanExpression
)?
)? (whereClause)? (KW_GROUP KW_BY groupBy)? (havingClause)?
;

whereClause
: KW_WHERE where=booleanExpression
;

havingClause
: KW_HAVING having=booleanExpression
;

groupBy
Expand Down Expand Up @@ -933,7 +943,7 @@ primaryExpression
| KW_TRY_CAST LPAREN expression KW_AS type RPAREN # cast
| KW_ARRAY LSQUARE (expression (COMMA expression)*)? RSQUARE # arrayConstructor
| value=primaryExpression LSQUARE index=valueExpression RSQUARE # subscript
| identifier # columnReference
| columnName # columnReference
| base=primaryExpression DOT fieldName=identifier # dereference
| name=KW_CURRENT_DATE # specialDateTimeFunction
| name=KW_CURRENT_TIME (LPAREN precision=INTEGER_VALUE RPAREN)? # specialDateTimeFunction
Expand Down Expand Up @@ -1050,11 +1060,15 @@ whenClause
;

filter
: KW_FILTER LPAREN KW_WHERE booleanExpression RPAREN
: KW_FILTER LPAREN whereClause RPAREN
;

partitionByClause
: partition+=expression (COMMA partition+=expression)*
;

over
: KW_OVER LPAREN (KW_PARTITION KW_BY partition+=expression (COMMA partition+=expression)*)? (
: KW_OVER LPAREN (KW_PARTITION KW_BY partitionByClause)? (
KW_ORDER KW_BY sortItem (COMMA sortItem)*
)? windowFrame? RPAREN
;
Expand Down Expand Up @@ -1093,7 +1107,7 @@ privilege
| KW_CREATE
| KW_INSERT
| KW_REFRESH
| KW_SELECT (LPAREN columnName=identifier RPAREN)?
| KW_SELECT (LPAREN name=identifier RPAREN)?
;

objectType
Expand Down
107 changes: 61 additions & 46 deletions src/grammar/trino/TrinoSql.g4
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

/**
* This file is an adaptation of trino's trino/core/trino-parser/src/main/antlr4/io/trino/sql/parser/SqlBase.g4 grammar.
* Reference: https://github.com/trinodb/trino/blob/385/core/trino-parser/src/main/antlr4/io/trino/sql/parser/SqlBase.g4
* This file is an adaptation of trino's
* trino/core/trino-parser/src/main/antlr4/io/trino/sql/parser/SqlBase.g4 grammar. Reference:
* https://github.com/trinodb/trino/blob/385/core/trino-parser/src/main/antlr4/io/trino/sql/parser/SqlBase.g4
* current version 450
*/

Expand Down Expand Up @@ -91,7 +94,7 @@ statement
)* ')' (KW_COMMENT comment=string)? (KW_WITH properties)? # createTable
| KW_DROP KW_TABLE (KW_IF KW_EXISTS)? tableRef # dropTable
| KW_INSERT KW_INTO tableRef columnList? rootQuery # insertInto
| KW_DELETE KW_FROM tableRef (KW_WHERE booleanExpression)? # delete
| KW_DELETE KW_FROM tableRef (whereClause)? # delete
| KW_TRUNCATE KW_TABLE tableRef # truncateTable
| KW_COMMENT KW_ON KW_TABLE tableRef KW_IS (string | KW_NULL) # commentTable
| KW_COMMENT KW_ON KW_VIEW viewRef KW_IS (string | KW_NULL) # commentView
Expand All @@ -106,7 +109,7 @@ statement
| KW_ALTER KW_TABLE tableName=tableRef KW_SET KW_PROPERTIES propertyAssignments # setTableProperties
| KW_ALTER KW_TABLE tableName=tableRef KW_EXECUTE procedureName=functionName (
'(' (callArgument (',' callArgument)*)? ')'
)? (KW_WHERE where=booleanExpression)? # tableExecute
)? (whereClause)? # tableExecute
| KW_ANALYZE tableRef (KW_WITH properties)? # analyze
| KW_CREATE (KW_OR KW_REPLACE)? KW_MATERIALIZED KW_VIEW (KW_IF KW_NOT KW_EXISTS)? viewNameCreate (
KW_GRACE KW_PERIOD interval
Expand Down Expand Up @@ -167,26 +170,24 @@ statement
| KW_DESC tableOrViewName # showColumns
| KW_SHOW KW_FUNCTIONS ((KW_FROM | KW_IN) schemaRef)? (
KW_LIKE pattern=string (KW_ESCAPE escape=string)?
)? # showFunctions
| KW_SHOW KW_SESSION (KW_LIKE pattern=string (KW_ESCAPE escape=string)?)? # showSession
| KW_SET KW_SESSION KW_AUTHORIZATION authorizationUser # setSessionAuthorization
| KW_RESET KW_SESSION KW_AUTHORIZATION # resetSessionAuthorization
| KW_SET KW_SESSION qualifiedName EQ expression # setSession
| KW_RESET KW_SESSION qualifiedName # resetSession
| KW_START KW_TRANSACTION (transactionMode (',' transactionMode)*)? # startTransaction
| KW_COMMIT KW_WORK? # commit
| KW_ROLLBACK KW_WORK? # rollback
| KW_PREPARE identifier KW_FROM statement # prepare
| KW_DEALLOCATE KW_PREPARE identifier # deallocate
| KW_EXECUTE identifier (KW_USING expression (',' expression)*)? # execute
| KW_EXECUTE KW_IMMEDIATE string (KW_USING expression (',' expression)*)? # executeImmediate
| KW_DESCRIBE KW_INPUT identifier # describeInput
| KW_DESCRIBE KW_OUTPUT identifier # describeOutput
| KW_SET KW_PATH pathSpecification # setPath
| KW_SET KW_TIME KW_ZONE ( KW_LOCAL | expression) # setTimeZone
| KW_UPDATE tableRef KW_SET updateAssignment (',' updateAssignment)* (
KW_WHERE where=booleanExpression
)? # update
)? # showFunctions
| KW_SHOW KW_SESSION (KW_LIKE pattern=string (KW_ESCAPE escape=string)?)? # showSession
| KW_SET KW_SESSION KW_AUTHORIZATION authorizationUser # setSessionAuthorization
| KW_RESET KW_SESSION KW_AUTHORIZATION # resetSessionAuthorization
| KW_SET KW_SESSION qualifiedName EQ expression # setSession
| KW_RESET KW_SESSION qualifiedName # resetSession
| KW_START KW_TRANSACTION (transactionMode (',' transactionMode)*)? # startTransaction
| KW_COMMIT KW_WORK? # commit
| KW_ROLLBACK KW_WORK? # rollback
| KW_PREPARE identifier KW_FROM statement # prepare
| KW_DEALLOCATE KW_PREPARE identifier # deallocate
| KW_EXECUTE identifier (KW_USING expression (',' expression)*)? # execute
| KW_EXECUTE KW_IMMEDIATE string (KW_USING expression (',' expression)*)? # executeImmediate
| KW_DESCRIBE KW_INPUT identifier # describeInput
| KW_DESCRIBE KW_OUTPUT identifier # describeOutput
| KW_SET KW_PATH pathSpecification # setPath
| KW_SET KW_TIME KW_ZONE ( KW_LOCAL | expression) # setTimeZone
| KW_UPDATE tableRef KW_SET updateAssignment (',' updateAssignment)* (whereClause)? # update
| KW_MERGE KW_INTO tableRef (KW_AS? identifier)? KW_USING relation KW_ON expression mergeCase+ # merge
| KW_SHOW KW_COMMENT KW_ON KW_TABLE tableRef # showTableComment // dtstack
| KW_SHOW KW_COMMENT KW_ON KW_COLUMN columnRef # showColumnComment // dtstack
Expand Down Expand Up @@ -283,16 +284,26 @@ sortItem

querySpecification
: KW_SELECT setQuantifier? selectItem (',' selectItem)* (KW_FROM relation (',' relation)*)? (
KW_WHERE where=booleanExpression
)? (KW_GROUP KW_BY groupBy)? (KW_HAVING having=booleanExpression)? (
KW_WINDOW windowDefinition (',' windowDefinition)*
)?
whereClause
)? (KW_GROUP KW_BY groupBy)? (havingClause)? (KW_WINDOW windowDefinition (',' windowDefinition)*)?
;

whereClause
: KW_WHERE where=booleanExpression
;

havingClause
: KW_HAVING having=booleanExpression
;

groupBy
: setQuantifier? groupingElement (',' groupingElement)*
;

partitionBy
: expression (',' expression)*
;

groupingElement
: groupingSet # singleGroupingSet
| KW_ROLLUP '(' (groupingSet (',' groupingSet)*)? ')' # rollup
Expand All @@ -315,9 +326,9 @@ windowDefinition
;

windowSpecification
: (existingWindowName=identifier)? (
KW_PARTITION KW_BY partition+=expression (',' partition+=expression)*
)? (KW_ORDER KW_BY sortItem (',' sortItem)*)? windowFrame?
: (existingWindowName=identifier)? (KW_PARTITION KW_BY partitionBy)? (
KW_ORDER KW_BY sortItem (',' sortItem)*
)? windowFrame?
;

namedQuery
Expand Down Expand Up @@ -383,11 +394,11 @@ listaggCountIndication

patternRecognition
: aliasedRelation (
KW_MATCH_RECOGNIZE '(' (
KW_PARTITION KW_BY partition+=expression (',' partition+=expression)*
)? (KW_ORDER KW_BY sortItem (',' sortItem)*)? (
KW_MEASURES measureDefinition (',' measureDefinition)*
)? rowsPerMatch? (KW_AFTER KW_MATCH skipTo)? (KW_INITIAL | KW_SEEK)? KW_PATTERN '(' rowPattern ')' (
KW_MATCH_RECOGNIZE '(' (KW_PARTITION KW_BY partitionBy)? (
KW_ORDER KW_BY sortItem (',' sortItem)*
)? (KW_MEASURES measureDefinition (',' measureDefinition)*)? rowsPerMatch? (
KW_AFTER KW_MATCH skipTo
)? (KW_INITIAL | KW_SEEK)? KW_PATTERN '(' rowPattern ')' (
KW_SUBSET subsetDefinition (',' subsetDefinition)*
)? KW_DEFINE variableDefinition (',' variableDefinition)* ')' (KW_AS? identifier columnAliases?)?
)?
Expand Down Expand Up @@ -502,7 +513,7 @@ tableFunctionArgument
;

tableArgument
: tableArgumentRelation (KW_PARTITION KW_BY ('(' (expression (',' expression)*)? ')' | expression))? (
: tableArgumentRelation (KW_PARTITION KW_BY ('(' partitionBy? ')' | expression))? (
KW_PRUNE KW_WHEN KW_EMPTY
| KW_KEEP KW_WHEN KW_EMPTY
)? (KW_ORDER KW_BY ('(' sortItem (',' sortItem)* ')' | sortItem))?
Expand Down Expand Up @@ -590,7 +601,7 @@ primaryExpression
| KW_TRY_CAST '(' expression KW_AS type ')' # cast
| KW_ARRAY '[' (expression (',' expression)*)? ']' # arrayConstructor
| value=primaryExpression '[' index=valueExpression ']' # subscript
| identifier # columnReference
| columnName # columnReference
| base=primaryExpression '.' fieldName=identifier # dereference
| name=KW_CURRENT_DATE # currentDate
| name=KW_CURRENT_TIME ('(' precision=INTEGER_VALUE ')')? # currentTime
Expand Down Expand Up @@ -765,7 +776,7 @@ whenClause
;

filter
: KW_FILTER '(' KW_WHERE booleanExpression ')'
: KW_FILTER '(' whereClause ')'
;

mergeCase
Expand Down Expand Up @@ -1008,6 +1019,10 @@ columnRef
| {this.shouldMatchEmpty()}?
;

columnName
: qualifiedName
;

columnNameCreate
: identifier
;
Expand Down
6 changes: 5 additions & 1 deletion src/lib/impala/ImpalaSqlParser.interp

Large diffs are not rendered by default.

Loading