Skip to content

Commit 8c8b3ba

Browse files
committed
feat(trino): change groupBy/orderBy/where expression to expressionCol
1 parent dc2acf9 commit 8c8b3ba

File tree

8 files changed

+4565
-4449
lines changed

8 files changed

+4565
-4449
lines changed

src/grammar/trino/TrinoSql.g4

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/*
2-
* Licensed under the Apache License, Version 2.0 (the "License");
3-
* you may not use this file except in compliance with the License.
4-
* You may obtain a copy of the License at
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
3+
* in compliance with the License. You may obtain a copy of the License at
4+
*
55
*
6-
* http://www.apache.org/licenses/LICENSE-2.0
7-
*
8-
* Unless required by applicable law or agreed to in writing, software
9-
* distributed under the License is distributed on an "AS IS" BASIS,
10-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
* See the License for the specific language governing permissions and
12-
* limitations under the License.
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software distributed under the License
9+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10+
* or implied. See the License for the specific language governing permissions and limitations under
11+
* the License.
1312
*/
1413

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

@@ -91,7 +91,7 @@ statement
9191
)* ')' (KW_COMMENT comment=string)? (KW_WITH properties)? # createTable
9292
| KW_DROP KW_TABLE (KW_IF KW_EXISTS)? tableRef # dropTable
9393
| KW_INSERT KW_INTO tableRef columnList? rootQuery # insertInto
94-
| KW_DELETE KW_FROM tableRef (KW_WHERE booleanExpression)? # delete
94+
| KW_DELETE KW_FROM tableRef (KW_WHERE expressionWithCol)? # delete
9595
| KW_TRUNCATE KW_TABLE tableRef # truncateTable
9696
| KW_COMMENT KW_ON KW_TABLE tableRef KW_IS (string | KW_NULL) # commentTable
9797
| KW_COMMENT KW_ON KW_VIEW viewRef KW_IS (string | KW_NULL) # commentView
@@ -106,7 +106,7 @@ statement
106106
| KW_ALTER KW_TABLE tableName=tableRef KW_SET KW_PROPERTIES propertyAssignments # setTableProperties
107107
| KW_ALTER KW_TABLE tableName=tableRef KW_EXECUTE procedureName=functionName (
108108
'(' (callArgument (',' callArgument)*)? ')'
109-
)? (KW_WHERE where=booleanExpression)? # tableExecute
109+
)? (KW_WHERE where=expressionWithCol)? # tableExecute
110110
| KW_ANALYZE tableRef (KW_WITH properties)? # analyze
111111
| KW_CREATE (KW_OR KW_REPLACE)? KW_MATERIALIZED KW_VIEW (KW_IF KW_NOT KW_EXISTS)? viewNameCreate (
112112
KW_GRACE KW_PERIOD interval
@@ -185,7 +185,7 @@ statement
185185
| KW_SET KW_PATH pathSpecification # setPath
186186
| KW_SET KW_TIME KW_ZONE ( KW_LOCAL | expression) # setTimeZone
187187
| KW_UPDATE tableRef KW_SET updateAssignment (',' updateAssignment)* (
188-
KW_WHERE where=booleanExpression
188+
KW_WHERE where=expressionWithCol
189189
)? # update
190190
| KW_MERGE KW_INTO tableRef (KW_AS? identifier)? KW_USING relation KW_ON expression mergeCase+ # merge
191191
| KW_SHOW KW_COMMENT KW_ON KW_TABLE tableRef # showTableComment // dtstack
@@ -276,14 +276,14 @@ queryPrimary
276276
;
277277

278278
sortItem
279-
: (columnRef | expression) ordering=(KW_ASC | KW_DESC)? (
279+
: (columnRef | expressionWithCol) ordering=(KW_ASC | KW_DESC)? (
280280
KW_NULLS nullOrdering=(KW_FIRST | KW_LAST)
281281
)?
282282
;
283283

284284
querySpecification
285285
: KW_SELECT setQuantifier? selectItem (',' selectItem)* (KW_FROM relation (',' relation)*)? (
286-
KW_WHERE where=booleanExpression
286+
KW_WHERE where=expressionWithCol
287287
)? (KW_GROUP KW_BY groupBy)? (KW_HAVING having=booleanExpression)? (
288288
KW_WINDOW windowDefinition (',' windowDefinition)*
289289
)?
@@ -307,7 +307,7 @@ groupingSet
307307

308308
groupingTerm
309309
: columnRef
310-
| expression
310+
| expressionWithCol
311311
;
312312

313313
windowDefinition
@@ -526,6 +526,11 @@ copartitionTables
526526
: '(' qualifiedName ',' qualifiedName (',' qualifiedName)* ')'
527527
;
528528

529+
expressionWithCol
530+
: columnRef
531+
| booleanExpression
532+
;
533+
529534
expression
530535
: booleanExpression
531536
;
@@ -765,7 +770,7 @@ whenClause
765770
;
766771

767772
filter
768-
: KW_FILTER '(' KW_WHERE booleanExpression ')'
773+
: KW_FILTER '(' KW_WHERE expressionWithCol ')'
769774
;
770775

771776
mergeCase

src/lib/trino/TrinoSql.interp

Lines changed: 2 additions & 1 deletion
Large diffs are not rendered by default.

src/lib/trino/TrinoSqlListener.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ import { TableArgumentQueryContext } from "./TrinoSqlParser.js";
182182
import { DescriptorArgumentContext } from "./TrinoSqlParser.js";
183183
import { DescriptorFieldContext } from "./TrinoSqlParser.js";
184184
import { CopartitionTablesContext } from "./TrinoSqlParser.js";
185+
import { ExpressionWithColContext } from "./TrinoSqlParser.js";
185186
import { ExpressionContext } from "./TrinoSqlParser.js";
186187
import { LogicalNotContext } from "./TrinoSqlParser.js";
187188
import { PredicatedContext } from "./TrinoSqlParser.js";
@@ -2360,6 +2361,16 @@ export class TrinoSqlListener implements ParseTreeListener {
23602361
* @param ctx the parse tree
23612362
*/
23622363
exitCopartitionTables?: (ctx: CopartitionTablesContext) => void;
2364+
/**
2365+
* Enter a parse tree produced by `TrinoSqlParser.expressionWithCol`.
2366+
* @param ctx the parse tree
2367+
*/
2368+
enterExpressionWithCol?: (ctx: ExpressionWithColContext) => void;
2369+
/**
2370+
* Exit a parse tree produced by `TrinoSqlParser.expressionWithCol`.
2371+
* @param ctx the parse tree
2372+
*/
2373+
exitExpressionWithCol?: (ctx: ExpressionWithColContext) => void;
23632374
/**
23642375
* Enter a parse tree produced by `TrinoSqlParser.expression`.
23652376
* @param ctx the parse tree

0 commit comments

Comments
 (0)