Skip to content

Commit 7b95431

Browse files
committed
feat(trino): add expression column
1 parent 99a8d21 commit 7b95431

File tree

10 files changed

+5080
-4678
lines changed

10 files changed

+5080
-4678
lines changed

src/grammar/trino/TrinoSql.g4

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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 (whereClause)? # 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+
)? (whereClause)? # 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
@@ -167,26 +167,24 @@ statement
167167
| KW_DESC tableOrViewName # showColumns
168168
| KW_SHOW KW_FUNCTIONS ((KW_FROM | KW_IN) schemaRef)? (
169169
KW_LIKE pattern=string (KW_ESCAPE escape=string)?
170-
)? # showFunctions
171-
| KW_SHOW KW_SESSION (KW_LIKE pattern=string (KW_ESCAPE escape=string)?)? # showSession
172-
| KW_SET KW_SESSION KW_AUTHORIZATION authorizationUser # setSessionAuthorization
173-
| KW_RESET KW_SESSION KW_AUTHORIZATION # resetSessionAuthorization
174-
| KW_SET KW_SESSION qualifiedName EQ expression # setSession
175-
| KW_RESET KW_SESSION qualifiedName # resetSession
176-
| KW_START KW_TRANSACTION (transactionMode (',' transactionMode)*)? # startTransaction
177-
| KW_COMMIT KW_WORK? # commit
178-
| KW_ROLLBACK KW_WORK? # rollback
179-
| KW_PREPARE identifier KW_FROM statement # prepare
180-
| KW_DEALLOCATE KW_PREPARE identifier # deallocate
181-
| KW_EXECUTE identifier (KW_USING expression (',' expression)*)? # execute
182-
| KW_EXECUTE KW_IMMEDIATE string (KW_USING expression (',' expression)*)? # executeImmediate
183-
| KW_DESCRIBE KW_INPUT identifier # describeInput
184-
| KW_DESCRIBE KW_OUTPUT identifier # describeOutput
185-
| KW_SET KW_PATH pathSpecification # setPath
186-
| KW_SET KW_TIME KW_ZONE ( KW_LOCAL | expression) # setTimeZone
187-
| KW_UPDATE tableRef KW_SET updateAssignment (',' updateAssignment)* (
188-
KW_WHERE where=booleanExpression
189-
)? # update
170+
)? # showFunctions
171+
| KW_SHOW KW_SESSION (KW_LIKE pattern=string (KW_ESCAPE escape=string)?)? # showSession
172+
| KW_SET KW_SESSION KW_AUTHORIZATION authorizationUser # setSessionAuthorization
173+
| KW_RESET KW_SESSION KW_AUTHORIZATION # resetSessionAuthorization
174+
| KW_SET KW_SESSION qualifiedName EQ expression # setSession
175+
| KW_RESET KW_SESSION qualifiedName # resetSession
176+
| KW_START KW_TRANSACTION (transactionMode (',' transactionMode)*)? # startTransaction
177+
| KW_COMMIT KW_WORK? # commit
178+
| KW_ROLLBACK KW_WORK? # rollback
179+
| KW_PREPARE identifier KW_FROM statement # prepare
180+
| KW_DEALLOCATE KW_PREPARE identifier # deallocate
181+
| KW_EXECUTE identifier (KW_USING expression (',' expression)*)? # execute
182+
| KW_EXECUTE KW_IMMEDIATE string (KW_USING expression (',' expression)*)? # executeImmediate
183+
| KW_DESCRIBE KW_INPUT identifier # describeInput
184+
| KW_DESCRIBE KW_OUTPUT identifier # describeOutput
185+
| KW_SET KW_PATH pathSpecification # setPath
186+
| KW_SET KW_TIME KW_ZONE ( KW_LOCAL | expression) # setTimeZone
187+
| KW_UPDATE tableRef KW_SET updateAssignment (',' updateAssignment)* (whereClause)? # update
190188
| KW_MERGE KW_INTO tableRef (KW_AS? identifier)? KW_USING relation KW_ON expression mergeCase+ # merge
191189
| KW_SHOW KW_COMMENT KW_ON KW_TABLE tableRef # showTableComment // dtstack
192190
| KW_SHOW KW_COMMENT KW_ON KW_COLUMN columnRef # showColumnComment // dtstack
@@ -283,16 +281,26 @@ sortItem
283281

284282
querySpecification
285283
: KW_SELECT setQuantifier? selectItem (',' selectItem)* (KW_FROM relation (',' relation)*)? (
286-
KW_WHERE where=booleanExpression
287-
)? (KW_GROUP KW_BY groupBy)? (KW_HAVING having=booleanExpression)? (
288-
KW_WINDOW windowDefinition (',' windowDefinition)*
289-
)?
284+
whereClause
285+
)? (KW_GROUP KW_BY groupBy)? (havingClause)? (KW_WINDOW windowDefinition (',' windowDefinition)*)?
286+
;
287+
288+
whereClause
289+
: KW_WHERE where=booleanExpression
290+
;
291+
292+
havingClause
293+
: KW_HAVING having=booleanExpression
290294
;
291295

292296
groupBy
293297
: setQuantifier? groupingElement (',' groupingElement)*
294298
;
295299

300+
partitionBy
301+
: expression (',' expression)*
302+
;
303+
296304
groupingElement
297305
: groupingSet # singleGroupingSet
298306
| KW_ROLLUP '(' (groupingSet (',' groupingSet)*)? ')' # rollup
@@ -315,9 +323,9 @@ windowDefinition
315323
;
316324

317325
windowSpecification
318-
: (existingWindowName=identifier)? (
319-
KW_PARTITION KW_BY partition+=expression (',' partition+=expression)*
320-
)? (KW_ORDER KW_BY sortItem (',' sortItem)*)? windowFrame?
326+
: (existingWindowName=identifier)? (KW_PARTITION KW_BY partitionBy)? (
327+
KW_ORDER KW_BY sortItem (',' sortItem)*
328+
)? windowFrame?
321329
;
322330

323331
namedQuery
@@ -383,11 +391,11 @@ listaggCountIndication
383391

384392
patternRecognition
385393
: aliasedRelation (
386-
KW_MATCH_RECOGNIZE '(' (
387-
KW_PARTITION KW_BY partition+=expression (',' partition+=expression)*
388-
)? (KW_ORDER KW_BY sortItem (',' sortItem)*)? (
389-
KW_MEASURES measureDefinition (',' measureDefinition)*
390-
)? rowsPerMatch? (KW_AFTER KW_MATCH skipTo)? (KW_INITIAL | KW_SEEK)? KW_PATTERN '(' rowPattern ')' (
394+
KW_MATCH_RECOGNIZE '(' (KW_PARTITION KW_BY partitionBy)? (
395+
KW_ORDER KW_BY sortItem (',' sortItem)*
396+
)? (KW_MEASURES measureDefinition (',' measureDefinition)*)? rowsPerMatch? (
397+
KW_AFTER KW_MATCH skipTo
398+
)? (KW_INITIAL | KW_SEEK)? KW_PATTERN '(' rowPattern ')' (
391399
KW_SUBSET subsetDefinition (',' subsetDefinition)*
392400
)? KW_DEFINE variableDefinition (',' variableDefinition)* ')' (KW_AS? identifier columnAliases?)?
393401
)?
@@ -502,7 +510,7 @@ tableFunctionArgument
502510
;
503511

504512
tableArgument
505-
: tableArgumentRelation (KW_PARTITION KW_BY ('(' (expression (',' expression)*)? ')' | expression))? (
513+
: tableArgumentRelation (KW_PARTITION KW_BY ('(' partitionBy? ')' | expression))? (
506514
KW_PRUNE KW_WHEN KW_EMPTY
507515
| KW_KEEP KW_WHEN KW_EMPTY
508516
)? (KW_ORDER KW_BY ('(' sortItem (',' sortItem)* ')' | sortItem))?
@@ -590,7 +598,7 @@ primaryExpression
590598
| KW_TRY_CAST '(' expression KW_AS type ')' # cast
591599
| KW_ARRAY '[' (expression (',' expression)*)? ']' # arrayConstructor
592600
| value=primaryExpression '[' index=valueExpression ']' # subscript
593-
| identifier # columnReference
601+
| columnName # columnReference
594602
| base=primaryExpression '.' fieldName=identifier # dereference
595603
| name=KW_CURRENT_DATE # currentDate
596604
| name=KW_CURRENT_TIME ('(' precision=INTEGER_VALUE ')')? # currentTime
@@ -765,7 +773,7 @@ whenClause
765773
;
766774

767775
filter
768-
: KW_FILTER '(' KW_WHERE booleanExpression ')'
776+
: KW_FILTER '(' whereClause ')'
769777
;
770778

771779
mergeCase
@@ -1008,6 +1016,10 @@ columnRef
10081016
| {this.shouldMatchEmpty()}?
10091017
;
10101018

1019+
columnName
1020+
: qualifiedName
1021+
;
1022+
10111023
columnNameCreate
10121024
: identifier
10131025
;

src/lib/trino/TrinoSql.interp

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

src/lib/trino/TrinoSqlListener.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ import { InlineTableContext } from "./TrinoSqlParser.js";
123123
import { SubqueryContext } from "./TrinoSqlParser.js";
124124
import { SortItemContext } from "./TrinoSqlParser.js";
125125
import { QuerySpecificationContext } from "./TrinoSqlParser.js";
126+
import { WhereClauseContext } from "./TrinoSqlParser.js";
127+
import { HavingClauseContext } from "./TrinoSqlParser.js";
126128
import { GroupByContext } from "./TrinoSqlParser.js";
129+
import { PartitionByContext } from "./TrinoSqlParser.js";
127130
import { SingleGroupingSetContext } from "./TrinoSqlParser.js";
128131
import { RollupContext } from "./TrinoSqlParser.js";
129132
import { CubeContext } from "./TrinoSqlParser.js";
@@ -355,6 +358,7 @@ import { CatalogNameCreateContext } from "./TrinoSqlParser.js";
355358
import { FunctionNameContext } from "./TrinoSqlParser.js";
356359
import { FunctionNameCreateContext } from "./TrinoSqlParser.js";
357360
import { ColumnRefContext } from "./TrinoSqlParser.js";
361+
import { ColumnNameContext } from "./TrinoSqlParser.js";
358362
import { ColumnNameCreateContext } from "./TrinoSqlParser.js";
359363
import { QualifiedNameContext } from "./TrinoSqlParser.js";
360364
import { QueryPeriodContext } from "./TrinoSqlParser.js";
@@ -1720,6 +1724,26 @@ export class TrinoSqlListener implements ParseTreeListener {
17201724
* @param ctx the parse tree
17211725
*/
17221726
exitQuerySpecification?: (ctx: QuerySpecificationContext) => void;
1727+
/**
1728+
* Enter a parse tree produced by `TrinoSqlParser.whereClause`.
1729+
* @param ctx the parse tree
1730+
*/
1731+
enterWhereClause?: (ctx: WhereClauseContext) => void;
1732+
/**
1733+
* Exit a parse tree produced by `TrinoSqlParser.whereClause`.
1734+
* @param ctx the parse tree
1735+
*/
1736+
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;
17231747
/**
17241748
* Enter a parse tree produced by `TrinoSqlParser.groupBy`.
17251749
* @param ctx the parse tree
@@ -1730,6 +1754,16 @@ export class TrinoSqlListener implements ParseTreeListener {
17301754
* @param ctx the parse tree
17311755
*/
17321756
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;
17331767
/**
17341768
* Enter a parse tree produced by the `singleGroupingSet`
17351769
* labeled alternative in `TrinoSqlParser.groupingElement`.
@@ -4332,6 +4366,16 @@ export class TrinoSqlListener implements ParseTreeListener {
43324366
* @param ctx the parse tree
43334367
*/
43344368
exitColumnRef?: (ctx: ColumnRefContext) => void;
4369+
/**
4370+
* Enter a parse tree produced by `TrinoSqlParser.columnName`.
4371+
* @param ctx the parse tree
4372+
*/
4373+
enterColumnName?: (ctx: ColumnNameContext) => void;
4374+
/**
4375+
* Exit a parse tree produced by `TrinoSqlParser.columnName`.
4376+
* @param ctx the parse tree
4377+
*/
4378+
exitColumnName?: (ctx: ColumnNameContext) => void;
43354379
/**
43364380
* Enter a parse tree produced by `TrinoSqlParser.columnNameCreate`.
43374381
* @param ctx the parse tree

0 commit comments

Comments
 (0)