Skip to content

Commit 764deb8

Browse files
Cythia828JackWang032mumiaoliuxy0551
authored
feat: match empty column when in entityCollecting context (#457) (#472)
* chore(release): 4.3.0 * fix(common): #424 allTokens slice when caretTokenIndex use tokenIndexOffset (#426) * test: #424 syntax after comments * fix(common): #424 allTokens slice when caretTokenIndex use tokenIndexOffset * chore(release): 4.3.1 * fix(postgresql): #432 remove error rule * test: #432 validate unComplete sql * fix: #432 remove error rule * feat: mark as entityCollecting in getAllEntities context to allow empty column * chore: update jest.config.js to hide console.log * fix(flink): #442 fix flink's insert values() can't support function problem * feat: remove noReserved keywords in completions * test: add filter keywords test case * test: #438 sync suggestion no duplicate syntaxContextType * fix: #438 syntaxContextType not duplicate * chore(release): 4.4.0-beta.0 * chore(release): 4.4.0 * feat: support query result and derived table entity collecting (#434) * feat: support queryResult and derived table entities collecting * feat: support query result and derived table entity collecting * test: enhance hive and spark entity collect test case * fix: remove _ctx and add tokenIndex into position * fix: rename declareType COMMON to LITERAL * fix: optimize entity collector and update grammar * test: add derived table and query result entities test case * fix: remove isCaretInDerivedTableStmt and set default isAccessible to null * fix: update _caretStmt docs * test: add isAccessible test case * fix: skip _caretStmt ts check * docs: update README to include additional entity information * test: fix create view test case * fix: import from error sql module * test: update entity collection tests * fix: remove unused type * feat: match empty column when in entityCollecting context * feat: optimize collecting entity when match empty column in entityCollecting context (#467) Co-authored-by: Cythia828 <942884029@qq.com> --------- Co-authored-by: Cythia828 <942884029@qq.com> Co-authored-by: JackWang032 <64318393+JackWang032@users.noreply.github.com> Co-authored-by: mumiao <1270865802zl@gmail.com> Co-authored-by: 琉易 <liuxy0551@qq.com> Co-authored-by: zhaoge <942884029@qq.com>
1 parent 107ba2d commit 764deb8

47 files changed

Lines changed: 45642 additions & 40063 deletions

Some content is hidden

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dt-sql-parser",
3-
"version": "4.5.0-beta.0",
3+
"version": "4.5.0-beta.1",
44
"authors": "DTStack Corporation",
55
"description": "SQL Parsers for BigData, built with antlr4",
66
"keywords": [

src/grammar/flink/FlinkSqlParser.g4

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,24 @@ columnNameCreate
184184
| expression
185185
;
186186

187+
emptyColumn
188+
:
189+
;
190+
187191
columnName
188-
: uid
189-
| {this.shouldMatchEmpty()}?
192+
: uidAllowEmpty
193+
| {this.shouldMatchEmpty()}? emptyColumn
190194
;
191195

192196
columnNamePath
193197
: uid
194198
;
195199

200+
columnNamePathAllowEmpty
201+
: uidAllowEmpty
202+
| {this.shouldMatchEmpty()}? emptyColumn
203+
;
204+
196205
columnNameList
197206
: LR_BRACKET columnName (COMMA columnName)* RR_BRACKET
198207
;
@@ -500,6 +509,7 @@ columnProjectItem
500509
| selectLiteralColumnName (columnAlias | KW_AS? expression)?
501510
| tableAllColumns columnAlias?
502511
| selectExpressionColumnName (columnAlias | KW_AS? columnName)?
512+
| {this.shouldMatchEmpty()}? emptyColumn
503513
;
504514

505515
selectWindowItemColumnName
@@ -614,12 +624,12 @@ columnDescriptor
614624
;
615625

616626
joinCondition
617-
: KW_ON booleanExpression
627+
: KW_ON (booleanExpression | columnNamePathAllowEmpty (EQUAL_SYMBOL columnNamePathAllowEmpty)?)
618628
| KW_USING columnNameList
619629
;
620630

621631
whereClause
622-
: KW_WHERE booleanExpression
632+
: KW_WHERE (booleanExpression | columnNamePathAllowEmpty)
623633
;
624634

625635
groupByClause
@@ -1037,6 +1047,11 @@ uid
10371047
: identifier (DOT identifier)*
10381048
;
10391049

1050+
uidAllowEmpty
1051+
: identifier (DOT identifier)*
1052+
| {this.shouldMatchEmpty()}? identifier DOT emptyColumn
1053+
;
1054+
10401055
withOption
10411056
: KW_WITH tablePropertyList
10421057
;

src/grammar/hive/HiveSqlParser.g4

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -754,14 +754,19 @@ columnNameList
754754
;
755755

756756
columnName
757-
: poolPath
758-
| {this.shouldMatchEmpty()}?
757+
: poolPathAllowEmpty
758+
| {this.shouldMatchEmpty()}? emptyColumn
759759
;
760760

761761
columnNamePath
762762
: poolPath
763763
;
764764

765+
columnNamePathAllowEmpty
766+
: poolPathAllowEmpty
767+
| {this.shouldMatchEmpty()}? emptyColumn
768+
;
769+
765770
columnNameCreate
766771
: id_
767772
;
@@ -1346,7 +1351,10 @@ atomjoinSource
13461351

13471352
joinSource
13481353
: atomjoinSource (
1349-
joinToken joinSourcePart (KW_ON expression | KW_USING columnParenthesesList)?
1354+
joinToken joinSourcePart (
1355+
KW_ON (expression | columnNamePathAllowEmpty (EQUAL columnNamePathAllowEmpty)?)
1356+
| KW_USING columnParenthesesList
1357+
)?
13501358
)*
13511359
;
13521360

@@ -1469,7 +1477,7 @@ Rules for parsing whereClause
14691477
where a=b and ...
14701478
*/
14711479
whereClause
1472-
: KW_WHERE expression
1480+
: KW_WHERE (expression | columnNamePathAllowEmpty)
14731481
;
14741482

14751483
/**
@@ -1528,6 +1536,7 @@ selectItem
15281536
| KW_AS LPAREN alias=id_ (COMMA alias=id_)* RPAREN
15291537
)?
15301538
)
1539+
| {this.shouldMatchEmpty()}? emptyColumn
15311540
;
15321541

15331542
selectLiteralColumnName
@@ -1600,7 +1609,7 @@ groupingSetExpression
16001609
;
16011610

16021611
havingClause
1603-
: KW_HAVING expression
1612+
: KW_HAVING (expression | columnNamePathAllowEmpty)
16041613
;
16051614

16061615
qualifyClause
@@ -2425,10 +2434,19 @@ decimal
24252434
| KW_NUMERIC
24262435
;
24272436

2437+
emptyColumn
2438+
:
2439+
;
2440+
24282441
poolPath
24292442
: id_ (DOT id_)*
24302443
;
24312444

2445+
poolPathAllowEmpty
2446+
: id_ (DOT id_)*
2447+
| {this.shouldMatchEmpty()}? id_ (DOT emptyColumn)*
2448+
;
2449+
24322450
triggerAtomExpression
24332451
: id_ GREATERTHAN (Number | StringLiteral)
24342452
;

src/grammar/impala/ImpalaSqlParser.g4

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -558,15 +558,24 @@ functionNamePath
558558
| qualifiedName
559559
;
560560

561+
emptyColumn
562+
:
563+
;
564+
561565
columnNamePath
562-
: qualifiedName
563-
| {this.shouldMatchEmpty()}?
566+
: {this.shouldMatchEmpty()}? emptyColumn
567+
| qualifiedNameAllowEmpty
564568
;
565569

566570
columnName
567571
: qualifiedName
568572
;
569573

574+
columnNameAllowEmpty
575+
: {this.shouldMatchEmpty()}? emptyColumn
576+
| qualifiedNameAllowEmpty
577+
;
578+
570579
tableOrViewPath
571580
: tableNamePath
572581
| viewNamePath
@@ -781,11 +790,11 @@ selectList
781790
;
782791

783792
whereClause
784-
: KW_WHERE where=booleanExpression
793+
: KW_WHERE (where=booleanExpression | columnNameAllowEmpty)
785794
;
786795

787796
havingClause
788-
: KW_HAVING having=booleanExpression
797+
: KW_HAVING (having=booleanExpression | columnNameAllowEmpty)
789798
;
790799

791800
groupBy
@@ -814,6 +823,7 @@ selectItem
814823
: selectLiteralColumnName columnAlias?
815824
| selectExpressionColumnName columnAlias?
816825
| tableAllColumns
826+
| {this.shouldMatchEmpty()}? emptyColumn
817827
;
818828

819829
columnAlias
@@ -854,7 +864,7 @@ joinType
854864
;
855865

856866
joinCriteria
857-
: KW_ON booleanExpression
867+
: KW_ON (booleanExpression | columnNameAllowEmpty)
858868
| KW_USING LPAREN identifier (COMMA identifier)* RPAREN
859869
;
860870

@@ -1149,6 +1159,11 @@ qualifiedName
11491159
: identifier (DOT identifier)*
11501160
;
11511161

1162+
qualifiedNameAllowEmpty
1163+
: {this.shouldMatchEmpty()}? (identifier DOT emptyColumn | emptyColumn)
1164+
| identifier (DOT identifier)*
1165+
;
1166+
11521167
principal
11531168
: KW_ROLE identifier # rolePrincipal
11541169
| KW_USER identifier # userPrincipal

src/grammar/mysql/MySqlParser.g4

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ joinPart
11221122
;
11231123

11241124
joinSpec
1125-
: (KW_ON expression)
1125+
: (KW_ON (expression | columnNamePathAllowEmpty))
11261126
| KW_USING '(' columnNames ')'
11271127
;
11281128

@@ -1205,9 +1205,10 @@ selectElements
12051205
;
12061206

12071207
selectElement
1208-
: tableAllColumns
1209-
| selectLiteralColumnName (KW_AS? alias=uid)?
1210-
| selectExpressionColumnName (KW_AS? alias=uid)?
1208+
: tableAllColumns # selectElement_star
1209+
| selectLiteralColumnName (KW_AS? alias=uid)? # selectElement_label
1210+
| selectExpressionColumnName (KW_AS? alias=uid)? # selectElement_expr
1211+
| uid DOT {this.shouldMatchEmpty()}? emptyColumn # selectElement_dot_empty
12111212
;
12121213

12131214
tableAllColumns
@@ -1249,7 +1250,7 @@ selectLinesInto
12491250
;
12501251

12511252
fromClause
1252-
: (KW_FROM tableSources)? (KW_WHERE whereExpr=expression)?
1253+
: (KW_FROM tableSources)? (KW_WHERE (whereExpr=expression | columnNamePathAllowEmpty))?
12531254
;
12541255

12551256
groupByClause
@@ -2419,10 +2420,24 @@ columnNames
24192420
: columnName (',' columnName)*
24202421
;
24212422

2423+
emptyColumn
2424+
:
2425+
;
2426+
24222427
columnName
24232428
: uid (dottedId dottedId?)?
24242429
| .? dottedId dottedId?
2425-
| {this.shouldMatchEmpty()}?
2430+
| {this.shouldMatchEmpty()}? emptyColumn
2431+
;
2432+
2433+
columnNamePath
2434+
: uid (dottedId dottedId?)?
2435+
| .? dottedId dottedId?
2436+
;
2437+
2438+
columnNamePathAllowEmpty
2439+
: {this.shouldMatchEmpty()}? emptyColumn
2440+
| uid (dottedId dottedId?)?
24262441
;
24272442

24282443
tableSpaceNameCreate
@@ -2998,7 +3013,7 @@ expressionAtom
29983013
| left=expressionAtom jsonOperator right=expressionAtom # jsonExpressionAtom
29993014
| left=expressionAtom bitOperator right=expressionAtom # bitExpressionAtom
30003015
| left=expressionAtom mathOperator right=expressionAtom # mathExpressionAtom
3001-
| columnName # columnNameExpressionAtom
3016+
| columnNamePath # columnNameExpressionAtom
30023017
;
30033018

30043019
unaryOperator

src/grammar/postgresql/PostgreSqlParser.g4

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2615,7 +2615,8 @@ when_clause
26152615
;
26162616

26172617
indirectionEl
2618-
: DOT (colLabel | STAR)
2618+
: DOT indirectionLabel
2619+
| DOT STAR
26192620
| OPEN_BRACKET (expression | expression? COLON expression?) CLOSE_BRACKET
26202621
;
26212622

@@ -2634,6 +2635,8 @@ targetList
26342635
targetEl
26352636
: tableAllColumns # target_star
26362637
| (selectLiteralColumnName | selectExpressionColumnName) (KW_AS? alias=identifier |) # target_label
2638+
| colId DOT {this.entityCollecting}? emptyColumn # target_dot_empty
2639+
| {this.entityCollecting}? emptyColumn # target_empty
26372640
;
26382641

26392642
tableAllColumns
@@ -2722,9 +2725,13 @@ procedureNameCreate
27222725
| colId indirection
27232726
;
27242727

2728+
// Empty column rule for entity collection
2729+
emptyColumn
2730+
:
2731+
;
2732+
27252733
columnName
27262734
: colId optIndirection
2727-
| {this.shouldMatchEmpty()}?
27282735
;
27292736

27302737
columnNamePath
@@ -2795,6 +2802,12 @@ colLabel
27952802
| reservedKeyword
27962803
;
27972804

2805+
indirectionLabel
2806+
: identifier
2807+
| colNameKeyword
2808+
| typeFuncNameKeyword
2809+
;
2810+
27982811
identifier
27992812
: Identifier (KW_UESCAPE anysconst)?
28002813
| stringConst

src/grammar/spark/SparkSqlParser.g4

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,15 +413,24 @@ viewName
413413
: viewIdentifier
414414
;
415415

416+
emptyColumn
417+
:
418+
;
419+
416420
columnName
417-
: multipartIdentifier
418-
| {this.shouldMatchEmpty()}?
421+
: multipartIdentifierAllowEmpty
422+
| {this.shouldMatchEmpty()}? emptyColumn
419423
;
420424

421425
columnNamePath
422426
: multipartIdentifier
423427
;
424428

429+
columnNamePathAllowEmpty
430+
: multipartIdentifierAllowEmpty
431+
| {this.shouldMatchEmpty()}? emptyColumn
432+
;
433+
425434
columnNameSeq
426435
: columnName (COMMA columnName)*
427436
;
@@ -680,7 +689,7 @@ joinType
680689
;
681690

682691
joinCriteria
683-
: KW_ON booleanExpression
692+
: KW_ON (booleanExpression | columnNamePathAllowEmpty (EQ columnNamePathAllowEmpty)?)
684693
| KW_USING identifierList
685694
;
686695

@@ -804,6 +813,11 @@ multipartIdentifier
804813
: parts+=errorCapturingIdentifier (DOT parts+=errorCapturingIdentifier)*
805814
;
806815

816+
multipartIdentifierAllowEmpty
817+
: multipartIdentifier
818+
| {this.shouldMatchEmpty()}? multipartIdentifier DOT emptyColumn
819+
;
820+
807821
multipartIdentifierPropertyList
808822
: multipartIdentifierProperty (COMMA multipartIdentifierProperty)*
809823
;
@@ -836,6 +850,7 @@ namedExpression
836850
: (tableAllColumns | selectLiteralColumnName | selectExpressionColumnName) (
837851
KW_AS? (alias=errorCapturingIdentifier | identifierList)
838852
)?
853+
| {this.shouldMatchEmpty()}? emptyColumn
839854
;
840855

841856
namedExpressionSeq

0 commit comments

Comments
 (0)