Skip to content

Commit 26219b8

Browse files
authored
feat(all sql): add all sql expression column (#358)
* feat(impala): add impala expression column * feat(trino): add expression column * feat(hive): add hive expression column * feat(spark): add spark expression column * feat(mysql): add mysql expression column unit test * feat(flink): add flink expression column * feat(postgresql): add pg expression column
1 parent 99b01e5 commit 26219b8

File tree

63 files changed

+33790
-30929
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+33790
-30929
lines changed

src/grammar/flink/FlinkSqlParser.g4

+11-7
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ columnName
189189
| {this.shouldMatchEmpty()}?
190190
;
191191

192+
columnNamePath
193+
: uid
194+
;
195+
192196
columnNameList
193197
: LR_BRACKET columnName (COMMA columnName)* RR_BRACKET
194198
;
@@ -452,9 +456,9 @@ queryStatement
452456
: valuesCaluse
453457
| withClause queryStatement
454458
| LR_BRACKET queryStatement RR_BRACKET
455-
| left=queryStatement operator=(KW_INTERSECT | KW_UNION | KW_EXCEPT) KW_ALL? right=queryStatement orderByCaluse? limitClause?
456-
| selectClause orderByCaluse? limitClause?
457-
| selectStatement orderByCaluse? limitClause?
459+
| left=queryStatement operator=(KW_INTERSECT | KW_UNION | KW_EXCEPT) KW_ALL? right=queryStatement orderByClause? limitClause?
460+
| selectClause orderByClause? limitClause?
461+
| selectStatement orderByClause? limitClause?
458462
;
459463

460464
valuesCaluse
@@ -627,15 +631,15 @@ namedWindow
627631
;
628632

629633
windowSpec
630-
: name=errorCapturingIdentifier? LR_BRACKET partitionByClause? orderByCaluse? windowFrame? RR_BRACKET
634+
: name=errorCapturingIdentifier? LR_BRACKET partitionByClause? orderByClause? windowFrame? RR_BRACKET
631635
;
632636

633637
matchRecognizeClause
634-
: KW_MATCH_RECOGNIZE LR_BRACKET partitionByClause? orderByCaluse? measuresClause? outputMode? afterMatchStrategy? patternDefination?
638+
: KW_MATCH_RECOGNIZE LR_BRACKET partitionByClause? orderByClause? measuresClause? outputMode? afterMatchStrategy? patternDefination?
635639
patternVariablesDefination RR_BRACKET (KW_AS? identifier)?
636640
;
637641

638-
orderByCaluse
642+
orderByClause
639643
: KW_ORDER KW_BY orderItemDefition (COMMA orderItemDefition)*
640644
;
641645

@@ -764,7 +768,7 @@ primaryExpression
764768
// | identifier '->' expression #lambda
765769
// | '(' identifier (',' identifier)+ ')' '->' expression #lambda
766770
| value=primaryExpression LS_BRACKET index=valueExpression RS_BRACKET # subscript
767-
| identifier # columnReference
771+
| columnNamePath # columnReference
768772
| dereferenceDefinition # dereference
769773
| LR_BRACKET expression RR_BRACKET # parenthesizedExpression
770774
| KW_CURRENT_TIMESTAMP # dateFunctionExpression

src/grammar/hive/HiveSqlParser.g4

+10-2
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,10 @@ columnName
758758
| {this.shouldMatchEmpty()}?
759759
;
760760

761+
columnNamePath
762+
: poolPath
763+
;
764+
761765
columnNameCreate
762766
: id_
763767
;
@@ -1437,7 +1441,7 @@ subQuerySource
14371441
Rules for parsing PTF clauses
14381442
*/
14391443
partitioningSpec
1440-
: KW_PARTITION KW_BY expressions orderByClause?
1444+
: partitionByClause orderByClause?
14411445
| orderByClause
14421446
| distributeByClause sortByClause?
14431447
| sortByClause
@@ -1613,6 +1617,10 @@ orderByClause
16131617
: KW_ORDER KW_BY columnRefOrder (COMMA columnRefOrder)*
16141618
;
16151619

1620+
partitionByClause
1621+
: KW_PARTITION KW_BY expressions
1622+
;
1623+
16161624
clusterByClause
16171625
: KW_CLUSTER KW_BY expressions
16181626
;
@@ -1714,7 +1722,7 @@ constant
17141722
| KW_FALSE
17151723
| KW_NULL
17161724
| p=QUESTION
1717-
| Identifier
1725+
| columnNamePath
17181726
;
17191727

17201728
intervalValue

src/grammar/impala/ImpalaSqlParser.g4

+24-10
Original file line numberDiff line numberDiff line change
@@ -344,18 +344,18 @@ deleteStatement
344344
;
345345

346346
delete
347-
: KW_DELETE KW_FROM? tableNamePath (KW_WHERE booleanExpression)?
347+
: KW_DELETE KW_FROM? tableNamePath (whereClause)?
348348
;
349349

350350
deleteTableRef
351351
: KW_DELETE tableNamePath (KW_AS? identifier)? KW_FROM (relation (COMMA relation)*)? (
352-
KW_WHERE booleanExpression
352+
whereClause
353353
)?
354354
;
355355

356356
updateStatement
357357
: KW_UPDATE tableNamePath KW_SET assignmentList (KW_FROM relation (COMMA relation)*)? (
358-
KW_WHERE booleanExpression
358+
whereClause
359359
)?
360360
;
361361

@@ -563,6 +563,10 @@ columnNamePath
563563
| {this.shouldMatchEmpty()}?
564564
;
565565

566+
columnName
567+
: qualifiedName
568+
;
569+
566570
tableOrViewPath
567571
: tableNamePath
568572
| viewNamePath
@@ -769,9 +773,15 @@ sortItem
769773
querySpecification
770774
: KW_SELECT setQuantifier? (KW_STRAIGHT_JOIN)? selectItem (COMMA selectItem)* (
771775
KW_FROM relation (COMMA relation)*
772-
)? (KW_WHERE where=booleanExpression)? (KW_GROUP KW_BY groupBy)? (
773-
KW_HAVING having=booleanExpression
774-
)?
776+
)? (whereClause)? (KW_GROUP KW_BY groupBy)? (havingClause)?
777+
;
778+
779+
whereClause
780+
: KW_WHERE where=booleanExpression
781+
;
782+
783+
havingClause
784+
: KW_HAVING having=booleanExpression
775785
;
776786

777787
groupBy
@@ -933,7 +943,7 @@ primaryExpression
933943
| KW_TRY_CAST LPAREN expression KW_AS type RPAREN # cast
934944
| KW_ARRAY LSQUARE (expression (COMMA expression)*)? RSQUARE # arrayConstructor
935945
| value=primaryExpression LSQUARE index=valueExpression RSQUARE # subscript
936-
| identifier # columnReference
946+
| columnName # columnReference
937947
| base=primaryExpression DOT fieldName=identifier # dereference
938948
| name=KW_CURRENT_DATE # specialDateTimeFunction
939949
| name=KW_CURRENT_TIME (LPAREN precision=INTEGER_VALUE RPAREN)? # specialDateTimeFunction
@@ -1050,11 +1060,15 @@ whenClause
10501060
;
10511061

10521062
filter
1053-
: KW_FILTER LPAREN KW_WHERE booleanExpression RPAREN
1063+
: KW_FILTER LPAREN whereClause RPAREN
1064+
;
1065+
1066+
partitionByClause
1067+
: partition+=expression (COMMA partition+=expression)*
10541068
;
10551069

10561070
over
1057-
: KW_OVER LPAREN (KW_PARTITION KW_BY partition+=expression (COMMA partition+=expression)*)? (
1071+
: KW_OVER LPAREN (KW_PARTITION KW_BY partitionByClause)? (
10581072
KW_ORDER KW_BY sortItem (COMMA sortItem)*
10591073
)? windowFrame? RPAREN
10601074
;
@@ -1093,7 +1107,7 @@ privilege
10931107
| KW_CREATE
10941108
| KW_INSERT
10951109
| KW_REFRESH
1096-
| KW_SELECT (LPAREN columnName=identifier RPAREN)?
1110+
| KW_SELECT (LPAREN name=identifier RPAREN)?
10971111
;
10981112

10991113
objectType

src/grammar/postgresql/PostgreSqlParser.g4

+29-26
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
/*
2-
* [The "MIT license"]
3-
* Copyright (C) 2014 Sam Harwell, Tunnel Vision Laboratories, LLC
4-
*
5-
* Permission is hereby granted, free of charge, to any person obtaining a copy
6-
* of this software and associated documentation files (the "Software"), to deal
7-
* in the Software without restriction, including without limitation the rights
8-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
* copies of the Software, and to permit persons to whom the Software is
2+
* [The "MIT license"] Copyright (C) 2014 Sam Harwell, Tunnel Vision Laboratories, LLC
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5+
* associated documentation files (the "Software"), to deal in the Software without restriction,
6+
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
7+
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
108
* furnished to do so, subject to the following conditions:
11-
*
12-
* 1. The above copyright notice and this permission notice shall be included in
13-
* all copies or substantial portions of the Software.
14-
* 2. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17-
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19-
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20-
* DEALINGS IN THE SOFTWARE.
21-
* 3. Except as contained in this notice, the name of Tunnel Vision
22-
* Laboratories, LLC. shall not be used in advertising or otherwise to
23-
* promote the sale, use or other dealings in this Software without prior
24-
* written authorization from Tunnel Vision Laboratories, LLC.
9+
*
10+
* 1. The above copyright notice and this permission notice shall be included in all copies or
11+
* substantial portions of the Software. 2. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
12+
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
14+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
15+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
16+
* DEALINGS IN THE SOFTWARE. 3. Except as contained in this notice, the name of Tunnel Vision
17+
* Laboratories, LLC. shall not be used in advertising or otherwise to promote the sale, use or
18+
* other dealings in this Software without prior written authorization from Tunnel Vision
19+
* Laboratories, LLC.
2520
*/
2621

2722
/**
28-
* This file is an adaptation of antlr's sql/postgresql/PostgreSQLParser.g4 grammar.
29-
* Reference: https://github.com/antlr/grammars-v4/blob/master/sql/postgresql/PostgreSQLParser.g4
23+
* This file is an adaptation of antlr's sql/postgresql/PostgreSQLParser.g4 grammar. Reference:
24+
* https://github.com/antlr/grammars-v4/blob/master/sql/postgresql/PostgreSQLParser.g4
3025
*/
3126

3227
/**
@@ -2407,7 +2402,7 @@ primaryExpression
24072402
| explicit_row
24082403
| OPEN_PAREN expression COMMA expr_list CLOSE_PAREN
24092404
| row KW_OVERLAPS row
2410-
| qualified_name
2405+
| column_name_path
24112406
| primaryExpression TYPECAST typename
24122407
| (PLUS | MINUS) primaryExpression
24132408
| primaryExpression qual_op primaryExpression?
@@ -2502,6 +2497,10 @@ window_clause
25022497
: KW_WINDOW window_definition (COMMA window_definition)*
25032498
;
25042499

2500+
having_clause
2501+
: KW_HAVING expression
2502+
;
2503+
25052504
window_definition
25062505
: colid KW_AS window_specification
25072506
;
@@ -2741,6 +2740,10 @@ column_name
27412740
| {this.shouldMatchEmpty()}? # columnNameMatch
27422741
;
27432742

2743+
column_name_path
2744+
: colid opt_indirection
2745+
;
2746+
27442747
column_name_create
27452748
: colid # columnNameCreate
27462749
;
@@ -3629,5 +3632,5 @@ any_identifier
36293632
;
36303633

36313634
sql_expression
3632-
: target_list? into_clause? from_clause? where_clause? group_clause? (KW_HAVING expression)? window_clause?
3635+
: target_list? into_clause? from_clause? where_clause? group_clause? having_clause? window_clause?
36333636
;

src/grammar/spark/SparkSqlParser.g4

+24-12
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ columnName
410410
| {this.shouldMatchEmpty()}?
411411
;
412412

413+
columnNamePath
414+
: multipartIdentifier
415+
;
416+
413417
columnNameSeq
414418
: columnName (COMMA columnName)*
415419
;
@@ -424,11 +428,23 @@ identifierReference
424428
;
425429

426430
queryOrganization
427-
: (KW_ORDER KW_BY order+=sortItem (COMMA order+=sortItem)*)? (
428-
KW_CLUSTER KW_BY clusterBy+=expression (COMMA clusterBy+=expression)*
429-
)? (KW_DISTRIBUTE KW_BY distributeBy+=expression (COMMA distributeBy+=expression)*)? (
430-
KW_SORT KW_BY sort+=sortItem (COMMA sort+=sortItem)*
431-
)? windowClause? (KW_LIMIT (KW_ALL | limit=expression))? (KW_OFFSET offset=expression)?
431+
: (KW_ORDER KW_BY orderOrSortByClause)? (KW_CLUSTER KW_BY clusterOrDistributeBy)? (
432+
KW_DISTRIBUTE KW_BY clusterOrDistributeBy
433+
)? (KW_SORT KW_BY orderOrSortByClause)? windowClause? limitClause? (
434+
KW_OFFSET offset=expression
435+
)?
436+
;
437+
438+
limitClause
439+
: KW_LIMIT (KW_ALL | limit=expression)
440+
;
441+
442+
orderOrSortByClause
443+
: sortItem (COMMA sortItem)*
444+
;
445+
446+
clusterOrDistributeBy
447+
: expression (COMMA expression)*
432448
;
433449

434450
queryTerm
@@ -722,11 +738,7 @@ tableArgumentPartitioning
722738
| partition+=expression
723739
)
724740
)
725-
) (
726-
(KW_ORDER | KW_SORT) KW_BY (
727-
((LEFT_PAREN sortItem (COMMA sortItem)* RIGHT_PAREN) | sortItem)
728-
)
729-
)?
741+
) ((KW_ORDER | KW_SORT) KW_BY ( ((LEFT_PAREN orderOrSortByClause RIGHT_PAREN) | sortItem)))?
730742
;
731743

732744
functionTableNamedArgumentExpression
@@ -906,7 +918,7 @@ primaryExpression
906918
| identifier ARROW expression
907919
| LEFT_PAREN identifier (COMMA identifier)+ RIGHT_PAREN ARROW expression
908920
| value=primaryExpression LEFT_BRACKET index=valueExpression RIGHT_BRACKET
909-
| identifier
921+
| columnNamePath
910922
| base=primaryExpression DOT fieldName=identifier
911923
| LEFT_PAREN expression RIGHT_PAREN
912924
| KW_EXTRACT LEFT_PAREN field=identifier KW_FROM source=valueExpression RIGHT_PAREN
@@ -1161,7 +1173,7 @@ windowSpec
11611173
(KW_PARTITION | KW_DISTRIBUTE) KW_BY partition+=expression (
11621174
COMMA partition+=expression
11631175
)*
1164-
)? ((KW_ORDER | KW_SORT) KW_BY sortItem (COMMA sortItem)*)?
1176+
)? ((KW_ORDER | KW_SORT) KW_BY orderOrSortByClause)?
11651177
) windowFrame? RIGHT_PAREN
11661178
;
11671179

0 commit comments

Comments
 (0)