Skip to content

Commit 4d9ce06

Browse files
committed
feat(trino): add havingClause/partitionBy for expression column
1 parent 3c7382b commit 4d9ce06

File tree

10 files changed

+4486
-4318
lines changed

10 files changed

+4486
-4318
lines changed

src/grammar/trino/TrinoSql.g4

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
* Licensed under the Apache License, Version 2.0 (the "License");
33
* you may not use this file except in compliance with the License.
44
* You may obtain a copy of the License at
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License. You may obtain a copy of the License at
7+
*
58
*
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.
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software distributed under the License
12+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13+
* or implied. See the License for the specific language governing permissions and limitations under
14+
* the License.
1315
*/
1416

1517
/**
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
18+
* This file is an adaptation of trino's
19+
* trino/core/trino-parser/src/main/antlr4/io/trino/sql/parser/SqlBase.g4 grammar. Reference:
20+
* https://github.com/trinodb/trino/blob/385/core/trino-parser/src/main/antlr4/io/trino/sql/parser/SqlBase.g4
1821
* current version 450
1922
*/
2023

@@ -282,19 +285,25 @@ sortItem
282285
querySpecification
283286
: KW_SELECT setQuantifier? selectItem (',' selectItem)* (KW_FROM relation (',' relation)*)? (
284287
whereClause
285-
)? (KW_GROUP KW_BY groupBy)? (KW_HAVING having=booleanExpression)? (
286-
KW_WINDOW windowDefinition (',' windowDefinition)*
287-
)?
288+
)? (KW_GROUP KW_BY groupBy)? (havingClause)? (KW_WINDOW windowDefinition (',' windowDefinition)*)?
288289
;
289290

290291
whereClause
291292
: KW_WHERE where=booleanExpression
292293
;
293294

295+
havingClause
296+
: KW_HAVING having=booleanExpression
297+
;
298+
294299
groupBy
295300
: setQuantifier? groupingElement (',' groupingElement)*
296301
;
297302

303+
partitionBy
304+
: expression (',' expression)*
305+
;
306+
298307
groupingElement
299308
: groupingSet # singleGroupingSet
300309
| KW_ROLLUP '(' (groupingSet (',' groupingSet)*)? ')' # rollup
@@ -317,9 +326,9 @@ windowDefinition
317326
;
318327

319328
windowSpecification
320-
: (existingWindowName=identifier)? (
321-
KW_PARTITION KW_BY partition+=expression (',' partition+=expression)*
322-
)? (KW_ORDER KW_BY sortItem (',' sortItem)*)? windowFrame?
329+
: (existingWindowName=identifier)? (KW_PARTITION KW_BY partitionBy)? (
330+
KW_ORDER KW_BY sortItem (',' sortItem)*
331+
)? windowFrame?
323332
;
324333

325334
namedQuery
@@ -385,11 +394,11 @@ listaggCountIndication
385394

386395
patternRecognition
387396
: aliasedRelation (
388-
KW_MATCH_RECOGNIZE '(' (
389-
KW_PARTITION KW_BY partition+=expression (',' partition+=expression)*
390-
)? (KW_ORDER KW_BY sortItem (',' sortItem)*)? (
391-
KW_MEASURES measureDefinition (',' measureDefinition)*
392-
)? rowsPerMatch? (KW_AFTER KW_MATCH skipTo)? (KW_INITIAL | KW_SEEK)? KW_PATTERN '(' rowPattern ')' (
397+
KW_MATCH_RECOGNIZE '(' (KW_PARTITION KW_BY partitionBy)? (
398+
KW_ORDER KW_BY sortItem (',' sortItem)*
399+
)? (KW_MEASURES measureDefinition (',' measureDefinition)*)? rowsPerMatch? (
400+
KW_AFTER KW_MATCH skipTo
401+
)? (KW_INITIAL | KW_SEEK)? KW_PATTERN '(' rowPattern ')' (
393402
KW_SUBSET subsetDefinition (',' subsetDefinition)*
394403
)? KW_DEFINE variableDefinition (',' variableDefinition)* ')' (KW_AS? identifier columnAliases?)?
395404
)?
@@ -504,7 +513,7 @@ tableFunctionArgument
504513
;
505514

506515
tableArgument
507-
: tableArgumentRelation (KW_PARTITION KW_BY ('(' (expression (',' expression)*)? ')' | expression))? (
516+
: tableArgumentRelation (KW_PARTITION KW_BY ('(' partitionBy? ')' | expression))? (
508517
KW_PRUNE KW_WHEN KW_EMPTY
509518
| KW_KEEP KW_WHEN KW_EMPTY
510519
)? (KW_ORDER KW_BY ('(' sortItem (',' sortItem)* ')' | sortItem))?

src/lib/trino/TrinoSql.interp

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

src/lib/trino/TrinoSqlListener.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ import { SubqueryContext } from "./TrinoSqlParser.js";
124124
import { SortItemContext } from "./TrinoSqlParser.js";
125125
import { QuerySpecificationContext } from "./TrinoSqlParser.js";
126126
import { WhereClauseContext } from "./TrinoSqlParser.js";
127+
import { HavingClauseContext } from "./TrinoSqlParser.js";
127128
import { GroupByContext } from "./TrinoSqlParser.js";
129+
import { PartitionByContext } from "./TrinoSqlParser.js";
128130
import { SingleGroupingSetContext } from "./TrinoSqlParser.js";
129131
import { RollupContext } from "./TrinoSqlParser.js";
130132
import { CubeContext } from "./TrinoSqlParser.js";
@@ -1732,6 +1734,16 @@ export class TrinoSqlListener implements ParseTreeListener {
17321734
* @param ctx the parse tree
17331735
*/
17341736
exitWhereClause?: (ctx: WhereClauseContext) => void;
1737+
/**
1738+
* Enter a parse tree produced by `TrinoSqlParser.havingClause`.
1739+
* @param ctx the parse tree
1740+
*/
1741+
enterHavingClause?: (ctx: HavingClauseContext) => void;
1742+
/**
1743+
* Exit a parse tree produced by `TrinoSqlParser.havingClause`.
1744+
* @param ctx the parse tree
1745+
*/
1746+
exitHavingClause?: (ctx: HavingClauseContext) => void;
17351747
/**
17361748
* Enter a parse tree produced by `TrinoSqlParser.groupBy`.
17371749
* @param ctx the parse tree
@@ -1742,6 +1754,16 @@ export class TrinoSqlListener implements ParseTreeListener {
17421754
* @param ctx the parse tree
17431755
*/
17441756
exitGroupBy?: (ctx: GroupByContext) => void;
1757+
/**
1758+
* Enter a parse tree produced by `TrinoSqlParser.partitionBy`.
1759+
* @param ctx the parse tree
1760+
*/
1761+
enterPartitionBy?: (ctx: PartitionByContext) => void;
1762+
/**
1763+
* Exit a parse tree produced by `TrinoSqlParser.partitionBy`.
1764+
* @param ctx the parse tree
1765+
*/
1766+
exitPartitionBy?: (ctx: PartitionByContext) => void;
17451767
/**
17461768
* Enter a parse tree produced by the `singleGroupingSet`
17471769
* labeled alternative in `TrinoSqlParser.groupingElement`.

0 commit comments

Comments
 (0)