Skip to content

Commit

Permalink
feat(trino): add expression column
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyFBB committed Oct 12, 2024
1 parent 99a8d21 commit 7b95431
Show file tree
Hide file tree
Showing 10 changed files with 5,080 additions and 4,678 deletions.
86 changes: 49 additions & 37 deletions src/grammar/trino/TrinoSql.g4
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,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 +106,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 +167,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 +281,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 +323,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 +391,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 +510,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 +598,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 +773,7 @@ whenClause
;

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

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

columnName
: qualifiedName
;

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

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions src/lib/trino/TrinoSqlListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ import { InlineTableContext } from "./TrinoSqlParser.js";
import { SubqueryContext } from "./TrinoSqlParser.js";
import { SortItemContext } from "./TrinoSqlParser.js";
import { QuerySpecificationContext } from "./TrinoSqlParser.js";
import { WhereClauseContext } from "./TrinoSqlParser.js";
import { HavingClauseContext } from "./TrinoSqlParser.js";
import { GroupByContext } from "./TrinoSqlParser.js";
import { PartitionByContext } from "./TrinoSqlParser.js";
import { SingleGroupingSetContext } from "./TrinoSqlParser.js";
import { RollupContext } from "./TrinoSqlParser.js";
import { CubeContext } from "./TrinoSqlParser.js";
Expand Down Expand Up @@ -355,6 +358,7 @@ import { CatalogNameCreateContext } from "./TrinoSqlParser.js";
import { FunctionNameContext } from "./TrinoSqlParser.js";
import { FunctionNameCreateContext } from "./TrinoSqlParser.js";
import { ColumnRefContext } from "./TrinoSqlParser.js";
import { ColumnNameContext } from "./TrinoSqlParser.js";
import { ColumnNameCreateContext } from "./TrinoSqlParser.js";
import { QualifiedNameContext } from "./TrinoSqlParser.js";
import { QueryPeriodContext } from "./TrinoSqlParser.js";
Expand Down Expand Up @@ -1720,6 +1724,26 @@ export class TrinoSqlListener implements ParseTreeListener {
* @param ctx the parse tree
*/
exitQuerySpecification?: (ctx: QuerySpecificationContext) => void;
/**
* Enter a parse tree produced by `TrinoSqlParser.whereClause`.
* @param ctx the parse tree
*/
enterWhereClause?: (ctx: WhereClauseContext) => void;
/**
* Exit a parse tree produced by `TrinoSqlParser.whereClause`.
* @param ctx the parse tree
*/
exitWhereClause?: (ctx: WhereClauseContext) => void;
/**
* Enter a parse tree produced by `TrinoSqlParser.havingClause`.
* @param ctx the parse tree
*/
enterHavingClause?: (ctx: HavingClauseContext) => void;
/**
* Exit a parse tree produced by `TrinoSqlParser.havingClause`.
* @param ctx the parse tree
*/
exitHavingClause?: (ctx: HavingClauseContext) => void;
/**
* Enter a parse tree produced by `TrinoSqlParser.groupBy`.
* @param ctx the parse tree
Expand All @@ -1730,6 +1754,16 @@ export class TrinoSqlListener implements ParseTreeListener {
* @param ctx the parse tree
*/
exitGroupBy?: (ctx: GroupByContext) => void;
/**
* Enter a parse tree produced by `TrinoSqlParser.partitionBy`.
* @param ctx the parse tree
*/
enterPartitionBy?: (ctx: PartitionByContext) => void;
/**
* Exit a parse tree produced by `TrinoSqlParser.partitionBy`.
* @param ctx the parse tree
*/
exitPartitionBy?: (ctx: PartitionByContext) => void;
/**
* Enter a parse tree produced by the `singleGroupingSet`
* labeled alternative in `TrinoSqlParser.groupingElement`.
Expand Down Expand Up @@ -4332,6 +4366,16 @@ export class TrinoSqlListener implements ParseTreeListener {
* @param ctx the parse tree
*/
exitColumnRef?: (ctx: ColumnRefContext) => void;
/**
* Enter a parse tree produced by `TrinoSqlParser.columnName`.
* @param ctx the parse tree
*/
enterColumnName?: (ctx: ColumnNameContext) => void;
/**
* Exit a parse tree produced by `TrinoSqlParser.columnName`.
* @param ctx the parse tree
*/
exitColumnName?: (ctx: ColumnNameContext) => void;
/**
* Enter a parse tree produced by `TrinoSqlParser.columnNameCreate`.
* @param ctx the parse tree
Expand Down
Loading

0 comments on commit 7b95431

Please sign in to comment.