Skip to content

Commit 7dc1d46

Browse files
authored
feat: collect entity's attribute(#333)
* feat(trinosql): collect trino sql's attribute(comment,alias,colType) * feat(hivesql): collect hive sql's attribute(comment,alias,colType) * feat(impalasql): collect attribute(comment, colType, alias) * feat(sparksql): collect entity's attribute (comment,alias, colType) * feat: update endContextList of collect attribute * feat(postgresql): collect hive sql's attribute(alias,colType) * feat: update interface of attrInfo and alter entitycollect ts file * feat(mysql): collect entity's attribute(comment,colType,alias) * ci: fix check-types problem --------- Co-authored-by: zhaoge <>
1 parent 46da6ec commit 7dc1d46

39 files changed

+6506
-5363
lines changed

src/grammar/hive/HiveSqlParser.g4

+8-4
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,9 @@ tableConstraint
887887
;
888888

889889
columnNameTypeConstraint
890-
: colName=columnNameCreate columnType columnConstraint? (KW_COMMENT comment=StringLiteral)?
890+
: colName=columnNameCreate colType=columnType columnConstraint? (
891+
KW_COMMENT comment=StringLiteral
892+
)?
891893
;
892894

893895
columnConstraint
@@ -1363,10 +1365,12 @@ joinToken
13631365
;
13641366

13651367
lateralView
1366-
: KW_LATERAL KW_VIEW KW_OUTER function_ tableAlias (KW_AS id_ (COMMA id_)*)?
1368+
: KW_LATERAL KW_VIEW KW_OUTER function_ alias=tableAlias (KW_AS id_ (COMMA id_)*)?
13671369
| COMMA? KW_LATERAL (
1368-
KW_VIEW function_ tableAlias (KW_AS id_ (COMMA id_)*)?
1369-
| KW_TABLE LPAREN valuesClause RPAREN KW_AS? tableAlias (LPAREN id_ (COMMA id_)* RPAREN)?
1370+
KW_VIEW function_ alias=tableAlias (KW_AS id_ (COMMA id_)*)?
1371+
| KW_TABLE LPAREN valuesClause RPAREN KW_AS? alias=tableAlias (
1372+
LPAREN id_ (COMMA id_)* RPAREN
1373+
)?
13701374
)
13711375
;
13721376

src/grammar/impala/ImpalaSqlParser.g4

+22-18
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,20 @@ createKuduTableAsSelect
9696
: KW_CREATE KW_EXTERNAL? KW_TABLE ifNotExists? tableNameCreate (
9797
LPAREN kuduTableElement (COMMA kuduTableElement)* (COMMA KW_PRIMARY KW_KEY columnAliases)? RPAREN
9898
)? (KW_PRIMARY KW_KEY columnAliases?)? (KW_PARTITION KW_BY kuduPartitionClause)? (
99-
KW_COMMENT stringLiteral
99+
commentClause
100100
)? KW_STORED KW_AS KW_KUDU (KW_TBLPROPERTIES tblProp=properties)? (KW_AS queryStatement)?
101101
;
102102

103103
createView
104-
: KW_CREATE KW_VIEW ifNotExists? viewNameCreate viewColumns? (KW_COMMENT stringLiteral)? (
104+
: KW_CREATE KW_VIEW ifNotExists? viewNameCreate viewColumns? commentClause? (
105105
KW_TBLPROPERTIES tblProp=properties
106106
)? KW_AS queryStatement
107107
;
108108

109109
createSchema
110-
: KW_CREATE (KW_SCHEMA | KW_DATABASE) ifNotExists? databaseNameCreate (
111-
KW_COMMENT comment=stringLiteral
112-
)? (KW_LOCATION location=stringLiteral)?
110+
: KW_CREATE (KW_SCHEMA | KW_DATABASE) ifNotExists? databaseNameCreate (commentClause)? (
111+
KW_LOCATION location=stringLiteral
112+
)?
113113
;
114114

115115
createRole
@@ -119,14 +119,14 @@ createRole
119119
createAggregateFunction
120120
: KW_CREATE KW_AGGREGATE? KW_FUNCTION ifNotExists? functionNameCreate (
121121
LPAREN (type (COMMA type)*)? RPAREN
122-
)? KW_RETURNS type (KW_INTERMEDIATE type)? KW_LOCATION STRING (KW_INIT_FN EQ STRING)? KW_UPDATE_FN EQ STRING KW_MERGE_FN EQ STRING (
122+
)? KW_RETURNS returnType=type (KW_INTERMEDIATE type)? KW_LOCATION STRING (KW_INIT_FN EQ STRING)? KW_UPDATE_FN EQ STRING KW_MERGE_FN EQ STRING (
123123
KW_PREPARE_FN EQ STRING
124124
)? (KW_CLOSEFN EQ STRING)? (KW_SERIALIZE_FN EQ STRING)? (KW_FINALIZE_FN EQ STRING)?
125125
;
126126

127127
createFunction
128128
: KW_CREATE KW_FUNCTION ifNotExists? functionNameCreate (LPAREN (type (COMMA type)*)? RPAREN)? (
129-
KW_RETURNS type
129+
KW_RETURNS returnType=type
130130
)? KW_LOCATION STRING KW_SYMBOL EQ symbol=stringLiteral
131131
;
132132

@@ -569,11 +569,9 @@ tableOrViewPath
569569
;
570570

571571
createCommonItem
572-
: (KW_SORT KW_BY columnAliases)? (KW_COMMENT comment=stringLiteral)? (
573-
KW_ROW KW_FORMAT rowFormat
574-
)? (KW_WITH KW_SERDEPROPERTIES serdProp=properties)? (KW_STORED KW_AS fileFormat)? (
575-
KW_LOCATION location=stringLiteral
576-
)? (
572+
: (KW_SORT KW_BY columnAliases)? commentClause? (KW_ROW KW_FORMAT rowFormat)? (
573+
KW_WITH KW_SERDEPROPERTIES serdProp=properties
574+
)? (KW_STORED KW_AS fileFormat)? (KW_LOCATION location=stringLiteral)? (
577575
KW_CACHED KW_IN cacheName=qualifiedName (KW_WITH KW_REPLICATION EQ INTEGER_VALUE)?
578576
| KW_UNCACHED
579577
)? (KW_TBLPROPERTIES tblProp=properties)?
@@ -588,9 +586,11 @@ assignmentItem
588586
;
589587

590588
viewColumns
591-
: LPAREN columnNamePathCreate (KW_COMMENT stringLiteral)? (
592-
COMMA columnNamePathCreate (KW_COMMENT stringLiteral)?
593-
)* RPAREN
589+
: LPAREN viewColumnItem? (COMMA viewColumnItem?)* RPAREN
590+
;
591+
592+
viewColumnItem
593+
: columnNamePathCreate commentClause?
594594
;
595595

596596
queryStatement
@@ -621,19 +621,23 @@ columnSpec
621621
;
622622

623623
columnDefinition
624-
: columnNamePathCreate type (KW_COMMENT stringLiteral)?
624+
: columnNamePathCreate colType=type commentClause?
625625
;
626626

627627
kuduTableElement
628628
: kuduColumnDefinition
629629
;
630630

631631
kuduColumnDefinition
632-
: columnNamePathCreate type (kuduAttributes kuduAttributes*?)? (KW_COMMENT stringLiteral)? (
632+
: columnNamePathCreate colType=type (kuduAttributes kuduAttributes*?)? commentClause? (
633633
KW_PRIMARY KW_KEY
634634
)?
635635
;
636636

637+
commentClause
638+
: KW_COMMENT comment=stringLiteral
639+
;
640+
637641
columnSpecWithKudu
638642
: columnSpec (kuduAttributes kuduAttributes*?)?
639643
;
@@ -838,7 +842,7 @@ sampleType
838842
;
839843

840844
aliasedRelation
841-
: relationPrimary (KW_AS? identifier columnAliases?)?
845+
: relationPrimary (KW_AS? alias=identifier columnAliases?)?
842846
;
843847

844848
columnAliases

src/grammar/mysql/MySqlParser.g4

+4-4
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ functionParameter
442442
;
443443

444444
routineOption
445-
: KW_COMMENT STRING_LITERAL # routineComment
445+
: KW_COMMENT comment=STRING_LITERAL # routineComment
446446
| KW_LANGUAGE KW_SQL # routineLanguage
447447
| KW_NOT? KW_DETERMINISTIC # routineBehavior
448448
| (KW_CONTAINS KW_SQL | KW_NO KW_SQL | KW_READS KW_SQL KW_DATA | KW_MODIFIES KW_SQL KW_DATA) # routineData
@@ -483,7 +483,7 @@ constraintSymbol
483483
;
484484

485485
columnDefinition
486-
: dataType columnConstraint*
486+
: colType=dataType columnConstraint*
487487
;
488488

489489
columnConstraint
@@ -494,7 +494,7 @@ columnConstraint
494494
| (KW_AUTO_INCREMENT | KW_ON KW_UPDATE currentTimestamp) # autoIncrementColumnConstraint
495495
| KW_PRIMARY? KW_KEY # primaryKeyColumnConstraint
496496
| KW_UNIQUE KW_KEY? # uniqueKeyColumnConstraint
497-
| KW_COMMENT STRING_LITERAL # commentColumnConstraint
497+
| KW_COMMENT comment=STRING_LITERAL # commentColumnConstraint
498498
| KW_COLUMN_FORMAT colformat=(KW_FIXED | KW_DYNAMIC | KW_DEFAULT) # formatColumnConstraint
499499
| KW_STORAGE storageval=(KW_DISK | KW_MEMORY | KW_DEFAULT) # storageColumnConstraint
500500
| referenceDefinition # referenceColumnConstraint
@@ -536,7 +536,7 @@ tableOption
536536
| KW_DEFAULT? charSet '='? (charsetName | KW_DEFAULT) # tableOptionCharset
537537
| (KW_CHECKSUM | KW_PAGE_CHECKSUM) '='? boolValue=('0' | '1') # tableOptionChecksum
538538
| KW_DEFAULT? KW_COLLATE '='? collationName # tableOptionCollate
539-
| KW_COMMENT '='? STRING_LITERAL # tableOptionComment
539+
| KW_COMMENT '='? comment=STRING_LITERAL # tableOptionComment
540540
| KW_COMPRESSION '='? (STRING_LITERAL | ID) # tableOptionCompression
541541
| KW_CONNECTION '='? STRING_LITERAL # tableOptionConnection
542542
| (KW_DATA | KW_INDEX) KW_DIRECTORY '='? STRING_LITERAL # tableOptionDataDirectory

src/grammar/postgresql/PostgreSqlParser.g4

+4-4
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ typedtableelement
620620
;
621621

622622
column_def
623-
: column_name_create typename create_generic_options? (
623+
: column_name_create colType=typename create_generic_options? (
624624
KW_STORAGE (KW_PLAIN | KW_EXTERNAL | KW_EXTENDED | KW_MAIN | KW_DEFAULT | colid)
625625
)? (KW_COMPRESSION colid)? (opt_collate_clause)? (KW_WITH KW_OPTIONS)? colconstraint*
626626
;
@@ -2163,12 +2163,12 @@ table_ref
21632163
;
21642164

21652165
alias_clause
2166-
: KW_AS? colid (OPEN_PAREN name_list CLOSE_PAREN)?
2166+
: KW_AS? alias=colid (OPEN_PAREN name_list CLOSE_PAREN)?
21672167
;
21682168

21692169
func_alias_clause
21702170
: alias_clause
2171-
| (KW_AS colid? | colid) OPEN_PAREN tablefuncelementlist CLOSE_PAREN
2171+
| (KW_AS alias=colid? | alias=colid) OPEN_PAREN tablefuncelementlist CLOSE_PAREN
21722172
;
21732173

21742174
join_type
@@ -2600,7 +2600,7 @@ func_arg_list
26002600

26012601
func_arg_expr
26022602
: expression
2603-
| type_function_name (COLON_EQUALS | EQUALS_GREATER) expression
2603+
| type_function_name ((COLON_EQUALS | EQUALS_GREATER) expression)?
26042604
;
26052605

26062606
array_expr

src/grammar/spark/SparkSqlParser.g4

+10-10
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ statement
8585
| KW_ALTER (KW_TABLE tableName | KW_VIEW viewName) KW_SET KW_TBLPROPERTIES propertyList # setTableProperties
8686
| KW_ALTER (KW_TABLE tableName | KW_VIEW viewName) KW_UNSET KW_TBLPROPERTIES (ifExists)? propertyList # unsetTableProperties
8787
| KW_ALTER KW_TABLE table=tableName (KW_ALTER | KW_CHANGE) KW_COLUMN? column=columnName alterColumnAction? # alterTableAlterColumn
88-
| KW_ALTER KW_TABLE table=tableName partitionSpec? KW_CHANGE KW_COLUMN? colName=columnName colType colPosition? # hiveChangeColumn
88+
| KW_ALTER KW_TABLE table=tableName partitionSpec? KW_CHANGE KW_COLUMN? colName=columnName columnType colPosition? # hiveChangeColumn
8989
| KW_ALTER KW_TABLE table=tableName partitionSpec? KW_REPLACE KW_COLUMNS LEFT_PAREN qualifiedColTypeWithPositionSeqForReplace RIGHT_PAREN #
9090
hiveReplaceColumns
9191
| KW_ALTER KW_TABLE tableName (partitionSpec)? KW_SET KW_SERDE stringLit (
@@ -153,8 +153,8 @@ statement
153153
| (KW_DESC | KW_DESCRIBE) KW_DATABASE KW_EXTENDED? namespaceName # describeNamespace
154154
| (KW_DESC | KW_DESCRIBE) KW_TABLE? option=(KW_EXTENDED | KW_FORMATTED)? tableName partitionSpec? describeColName? # describeRelation
155155
| (KW_DESC | KW_DESCRIBE) KW_QUERY? query # describeQuery
156-
| KW_COMMENT KW_ON namespace namespaceName KW_IS comment # commentNamespace
157-
| KW_COMMENT KW_ON KW_TABLE tableName KW_IS comment # commentTable
156+
| KW_COMMENT KW_ON namespace namespaceName KW_IS commentStr # commentNamespace
157+
| KW_COMMENT KW_ON KW_TABLE tableName KW_IS commentStr # commentTable
158158
| KW_REFRESH KW_TABLE tableName # refreshTable
159159
| KW_REFRESH KW_FUNCTION functionName # refreshFunction
160160
| KW_REFRESH (stringLit | .*?) # refreshResource
@@ -269,7 +269,7 @@ locationSpec
269269
;
270270

271271
commentSpec
272-
: KW_COMMENT stringLit
272+
: KW_COMMENT comment=stringLit
273273
;
274274

275275
query
@@ -851,7 +851,7 @@ functionTable
851851
;
852852

853853
tableAlias
854-
: (KW_AS? strictIdentifier identifierList?)?
854+
: (KW_AS? alias=strictIdentifier identifierList?)?
855855
;
856856

857857
rowFormat
@@ -905,7 +905,7 @@ partitionFieldList
905905

906906
partitionField
907907
: transform
908-
| colType
908+
| columnType
909909
;
910910

911911
transform
@@ -1227,10 +1227,10 @@ variableDefaultExpression
12271227
;
12281228

12291229
colTypeList
1230-
: colType (COMMA colType)*
1230+
: columnType (COMMA columnType)*
12311231
;
12321232

1233-
colType
1233+
columnType
12341234
: colName=errorCapturingIdentifier dataType (KW_NOT KW_NULL)? commentSpec?
12351235
;
12361236

@@ -1239,7 +1239,7 @@ createOrReplaceTableColTypeList
12391239
;
12401240

12411241
createOrReplaceTableColType
1242-
: colName=columnNameCreate dataType colDefinitionOption*
1242+
: colName=columnNameCreate colType=dataType colDefinitionOption*
12431243
;
12441244

12451245
colDefinitionOption
@@ -1388,7 +1388,7 @@ stringLit
13881388
| DOUBLEQUOTED_STRING
13891389
;
13901390

1391-
comment
1391+
commentStr
13921392
: stringLit
13931393
| KW_NULL
13941394
;

src/grammar/trino/TrinoSql.g4

+7-7
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ statement
8484
| KW_ALTER KW_SCHEMA schemaRef KW_RENAME KW_TO schemaNameCreate # renameSchema
8585
| KW_ALTER KW_SCHEMA schemaRef KW_SET KW_AUTHORIZATION principal # setSchemaAuthorization
8686
| KW_CREATE (KW_OR KW_REPLACE)? KW_TABLE (KW_IF KW_NOT KW_EXISTS)? tableNameCreate columnListCreate? (
87-
KW_COMMENT string
87+
KW_COMMENT comment=string
8888
)? (KW_WITH properties)? KW_AS (rootQuery | '(' rootQuery ')') (KW_WITH (KW_NO)? KW_DATA)? # createTableAsSelect
8989
| KW_CREATE (KW_OR KW_REPLACE)? KW_TABLE (KW_IF KW_NOT KW_EXISTS)? tableNameCreate '(' tableElement (
9090
',' tableElement
91-
)* ')' (KW_COMMENT string)? (KW_WITH properties)? # createTable
91+
)* ')' (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
9494
| KW_DELETE KW_FROM tableRef (KW_WHERE booleanExpression)? # delete
@@ -110,8 +110,8 @@ statement
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
113-
)? (KW_COMMENT string)? (KW_WITH properties)? KW_AS rootQuery # createMaterializedView
114-
| KW_CREATE (KW_OR KW_REPLACE)? KW_VIEW viewNameCreate (KW_COMMENT string)? (
113+
)? (KW_COMMENT comment=string)? (KW_WITH properties)? KW_AS rootQuery # createMaterializedView
114+
| KW_CREATE (KW_OR KW_REPLACE)? KW_VIEW viewNameCreate (KW_COMMENT comment=string)? (
115115
KW_SECURITY (KW_DEFINER | KW_INVOKER)
116116
)? (KW_WITH properties)? KW_AS rootQuery # createView
117117
| KW_REFRESH KW_MATERIALIZED KW_VIEW viewRef # refreshMaterializedView
@@ -214,7 +214,7 @@ tableElement
214214
;
215215

216216
columnDefinition
217-
: columnNameCreate type (KW_NOT KW_NULL)? (KW_COMMENT string)? (KW_WITH properties)?
217+
: columnNameCreate colType=type (KW_NOT KW_NULL)? (KW_COMMENT comment=string)? (KW_WITH properties)?
218218
;
219219

220220
likeClause
@@ -330,7 +330,7 @@ setQuantifier
330330
;
331331

332332
selectItem
333-
: (columnRef | expression) (KW_AS? identifier)? # selectSingle
333+
: (columnRef | expression) (KW_AS? alias=identifier)? # selectSingle
334334
| primaryExpression '.' ASTERISK (KW_AS columnAliases)? # selectAll
335335
| ASTERISK # selectAll
336336
;
@@ -425,7 +425,7 @@ variableDefinition
425425
;
426426

427427
aliasedRelation
428-
: relationPrimary (KW_AS? identifier columnAliases?)?
428+
: relationPrimary (KW_AS? alias=identifier columnAliases?)?
429429
;
430430

431431
columnListCreate

src/lib/hive/HiveSqlParser.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -10836,7 +10836,7 @@ export class HiveSqlParser extends SQLParserBase {
1083610836
this.state = 2290;
1083710837
localContext._colName = this.columnNameCreate();
1083810838
this.state = 2291;
10839-
this.columnType();
10839+
localContext._colType = this.columnType();
1084010840
this.state = 2293;
1084110841
this.errorHandler.sync(this);
1084210842
_la = this.tokenStream.LA(1);
@@ -16816,7 +16816,7 @@ export class HiveSqlParser extends SQLParserBase {
1681616816
this.state = 3480;
1681716817
this.function_();
1681816818
this.state = 3481;
16819-
this.tableAlias();
16819+
localContext._alias = this.tableAlias();
1682016820
this.state = 3491;
1682116821
this.errorHandler.sync(this);
1682216822
_la = this.tokenStream.LA(1);
@@ -16874,7 +16874,7 @@ export class HiveSqlParser extends SQLParserBase {
1687416874
this.state = 3498;
1687516875
this.function_();
1687616876
this.state = 3499;
16877-
this.tableAlias();
16877+
localContext._alias = this.tableAlias();
1687816878
this.state = 3509;
1687916879
this.errorHandler.sync(this);
1688016880
_la = this.tokenStream.LA(1);
@@ -16928,7 +16928,7 @@ export class HiveSqlParser extends SQLParserBase {
1692816928
}
1692916929

1693016930
this.state = 3518;
16931-
this.tableAlias();
16931+
localContext._alias = this.tableAlias();
1693216932
this.state = 3530;
1693316933
this.errorHandler.sync(this);
1693416934
switch (this.interpreter.adaptivePredict(this.tokenStream, 458, this.context) ) {
@@ -34747,16 +34747,17 @@ export class TableConstraintContext extends antlr.ParserRuleContext {
3474734747

3474834748
export class ColumnNameTypeConstraintContext extends antlr.ParserRuleContext {
3474934749
public _colName?: ColumnNameCreateContext;
34750+
public _colType?: ColumnTypeContext;
3475034751
public _comment?: Token | null;
3475134752
public constructor(parent: antlr.ParserRuleContext | null, invokingState: number) {
3475234753
super(parent, invokingState);
3475334754
}
34754-
public columnType(): ColumnTypeContext {
34755-
return this.getRuleContext(0, ColumnTypeContext)!;
34756-
}
3475734755
public columnNameCreate(): ColumnNameCreateContext {
3475834756
return this.getRuleContext(0, ColumnNameCreateContext)!;
3475934757
}
34758+
public columnType(): ColumnTypeContext {
34759+
return this.getRuleContext(0, ColumnTypeContext)!;
34760+
}
3476034761
public columnConstraint(): ColumnConstraintContext | null {
3476134762
return this.getRuleContext(0, ColumnConstraintContext);
3476234763
}
@@ -38744,6 +38745,7 @@ export class JoinTokenContext extends antlr.ParserRuleContext {
3874438745

3874538746

3874638747
export class LateralViewContext extends antlr.ParserRuleContext {
38748+
public _alias?: TableAliasContext;
3874738749
public constructor(parent: antlr.ParserRuleContext | null, invokingState: number) {
3874838750
super(parent, invokingState);
3874938751
}

0 commit comments

Comments
 (0)