dt-sql-parser 4.1.2-beta.1 → 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -0
- package/dist/lib/flink/FlinkSqlLexer.d.ts +2 -2
- package/dist/lib/flink/FlinkSqlLexer.js +21 -20
- package/dist/lib/flink/FlinkSqlParser.d.ts +178 -168
- package/dist/lib/flink/FlinkSqlParser.js +2799 -2741
- package/dist/lib/flink/FlinkSqlParserListener.d.ts +11 -0
- package/dist/lib/flink/FlinkSqlParserVisitor.d.ts +7 -0
- package/dist/lib/hive/HiveSqlParser.d.ts +241 -221
- package/dist/lib/hive/HiveSqlParser.js +6229 -5639
- package/dist/lib/hive/HiveSqlParserListener.d.ts +22 -0
- package/dist/lib/hive/HiveSqlParserVisitor.d.ts +14 -0
- package/dist/lib/impala/ImpalaSqlLexer.d.ts +2 -2
- package/dist/lib/impala/ImpalaSqlLexer.js +5 -5
- package/dist/lib/impala/ImpalaSqlParser.d.ts +147 -109
- package/dist/lib/impala/ImpalaSqlParser.js +3448 -3234
- package/dist/lib/impala/ImpalaSqlParserListener.d.ts +44 -0
- package/dist/lib/impala/ImpalaSqlParserVisitor.d.ts +28 -0
- package/dist/lib/mysql/MySqlLexer.d.ts +2 -2
- package/dist/lib/mysql/MySqlLexer.js +43 -43
- package/dist/lib/mysql/MySqlParser.d.ts +2 -2
- package/dist/lib/mysql/MySqlParser.js +42 -42
- package/dist/lib/postgresql/PostgreSqlLexer.d.ts +14 -15
- package/dist/lib/postgresql/PostgreSqlLexer.js +2077 -2092
- package/dist/lib/postgresql/PostgreSqlParser.d.ts +152 -131
- package/dist/lib/postgresql/PostgreSqlParser.js +10052 -9925
- package/dist/lib/postgresql/PostgreSqlParserListener.d.ts +22 -0
- package/dist/lib/postgresql/PostgreSqlParserVisitor.d.ts +14 -0
- package/dist/lib/spark/SparkSqlLexer.d.ts +2 -2
- package/dist/lib/spark/SparkSqlLexer.js +6 -6
- package/dist/lib/spark/SparkSqlParser.d.ts +391 -623
- package/dist/lib/spark/SparkSqlParser.js +6749 -8109
- package/dist/lib/spark/SparkSqlParserListener.d.ts +38 -326
- package/dist/lib/spark/SparkSqlParserVisitor.d.ts +24 -207
- package/dist/lib/trino/TrinoSqlLexer.d.ts +2 -2
- package/dist/lib/trino/TrinoSqlLexer.js +5 -5
- package/dist/lib/trino/TrinoSqlListener.d.ts +44 -0
- package/dist/lib/trino/TrinoSqlParser.d.ts +182 -153
- package/dist/lib/trino/TrinoSqlParser.js +4758 -4599
- package/dist/lib/trino/TrinoSqlVisitor.d.ts +28 -0
- package/dist/parser/common/basicSQL.d.ts +43 -9
- package/dist/parser/common/basicSQL.js +147 -40
- package/dist/parser/common/semanticContextCollector.d.ts +50 -0
- package/dist/parser/common/semanticContextCollector.js +202 -0
- package/dist/parser/common/tokenUtils.d.ts +12 -0
- package/dist/parser/common/tokenUtils.js +38 -0
- package/dist/parser/common/types.d.ts +30 -1
- package/dist/parser/common/types.js +11 -0
- package/dist/parser/flink/flinkErrorListener.js +14 -2
- package/dist/parser/flink/flinkSemanticContextCollector.d.ts +9 -0
- package/dist/parser/flink/flinkSemanticContextCollector.js +14 -0
- package/dist/parser/flink/index.d.ts +4 -2
- package/dist/parser/flink/index.js +32 -14
- package/dist/parser/hive/hiveErrorListener.js +9 -3
- package/dist/parser/hive/hiveSemanticContextCollector.d.ts +9 -0
- package/dist/parser/hive/hiveSemanticContextCollector.js +14 -0
- package/dist/parser/hive/index.d.ts +4 -2
- package/dist/parser/hive/index.js +29 -14
- package/dist/parser/impala/ImpalaErrorListener.js +8 -2
- package/dist/parser/impala/impalaSemanticContextCollector.d.ts +9 -0
- package/dist/parser/impala/impalaSemanticContextCollector.js +14 -0
- package/dist/parser/impala/index.d.ts +4 -2
- package/dist/parser/impala/index.js +21 -14
- package/dist/parser/mysql/index.d.ts +4 -2
- package/dist/parser/mysql/index.js +10 -14
- package/dist/parser/mysql/mysqlErrorListener.js +6 -2
- package/dist/parser/mysql/mysqlSemanticContextCollector.d.ts +9 -0
- package/dist/parser/mysql/mysqlSemanticContextCollector.js +14 -0
- package/dist/parser/postgresql/index.d.ts +4 -2
- package/dist/parser/postgresql/index.js +23 -14
- package/dist/parser/postgresql/postgreErrorListener.js +9 -3
- package/dist/parser/postgresql/postgreSemanticContextCollector.d.ts +9 -0
- package/dist/parser/postgresql/postgreSemanticContextCollector.js +14 -0
- package/dist/parser/spark/index.d.ts +4 -2
- package/dist/parser/spark/index.js +26 -14
- package/dist/parser/spark/sparkErrorListener.js +9 -3
- package/dist/parser/spark/sparkSemanticContextCollector.d.ts +9 -0
- package/dist/parser/spark/sparkSemanticContextCollector.js +14 -0
- package/dist/parser/trino/index.d.ts +4 -2
- package/dist/parser/trino/index.js +22 -14
- package/dist/parser/trino/trinoErrorListener.js +9 -3
- package/dist/parser/trino/trinoSemanticContextCollector.d.ts +9 -0
- package/dist/parser/trino/trinoSemanticContextCollector.js +14 -0
- package/package.json +2 -1
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { processTokenCandidates } from '../common/tokenUtils';
|
|
1
2
|
import { MySqlLexer } from '../../lib/mysql/MySqlLexer';
|
|
2
3
|
import { MySqlParser } from '../../lib/mysql/MySqlParser';
|
|
3
4
|
import { BasicSQL } from '../common/basicSQL';
|
|
4
|
-
import { EntityContextType } from '../common/types';
|
|
5
|
+
import { EntityContextType, } from '../common/types';
|
|
5
6
|
import { MySqlEntityCollector } from './mysqlEntityCollector';
|
|
6
7
|
import { MysqlErrorListener } from './mysqlErrorListener';
|
|
7
8
|
import { MysqlSplitListener } from './mysqlSplitListener';
|
|
9
|
+
import { MySqlSemanticContextCollector } from '../mysql/mysqlSemanticContextCollector';
|
|
8
10
|
export { MySqlEntityCollector, MysqlSplitListener };
|
|
9
11
|
export class MySQL extends BasicSQL {
|
|
10
12
|
constructor() {
|
|
@@ -38,13 +40,15 @@ export class MySQL extends BasicSQL {
|
|
|
38
40
|
createEntityCollector(input, allTokens, caretTokenIndex) {
|
|
39
41
|
return new MySqlEntityCollector(input, allTokens, caretTokenIndex);
|
|
40
42
|
}
|
|
41
|
-
|
|
43
|
+
createSemanticContextCollector(input, caretPosition, allTokens, options) {
|
|
44
|
+
return new MySqlSemanticContextCollector(input, caretPosition, allTokens, options);
|
|
45
|
+
}
|
|
46
|
+
processCandidates(candidates, allTokens, caretTokenIndex) {
|
|
42
47
|
const originalSyntaxSuggestions = [];
|
|
43
48
|
const keywords = [];
|
|
44
49
|
for (const candidate of candidates.rules) {
|
|
45
50
|
const [ruleType, candidateRule] = candidate;
|
|
46
|
-
const
|
|
47
|
-
const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1);
|
|
51
|
+
const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);
|
|
48
52
|
let syntaxContextType = void 0;
|
|
49
53
|
switch (ruleType) {
|
|
50
54
|
case MySqlParser.RULE_databaseName: {
|
|
@@ -97,16 +101,8 @@ export class MySQL extends BasicSQL {
|
|
|
97
101
|
});
|
|
98
102
|
}
|
|
99
103
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const displayName = this._parser.vocabulary.getDisplayName(candidate[0]);
|
|
103
|
-
if (displayName && symbolicName && symbolicName.startsWith('KW_')) {
|
|
104
|
-
const keyword = displayName.startsWith("'") && displayName.endsWith("'")
|
|
105
|
-
? displayName.slice(1, -1)
|
|
106
|
-
: displayName;
|
|
107
|
-
keywords.push(keyword);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
104
|
+
const processedKeywords = processTokenCandidates(this._parser, candidates.tokens);
|
|
105
|
+
keywords.push(...processedKeywords);
|
|
110
106
|
return {
|
|
111
107
|
syntax: originalSyntaxSuggestions,
|
|
112
108
|
keywords,
|
|
@@ -49,7 +49,9 @@ export class MysqlErrorListener extends ParseErrorListener {
|
|
|
49
49
|
case MySqlParser.RULE_functionName:
|
|
50
50
|
case MySqlParser.RULE_viewName:
|
|
51
51
|
case MySqlParser.RULE_columnName: {
|
|
52
|
-
result.
|
|
52
|
+
if (!result.includes(`{existing}${name}`)) {
|
|
53
|
+
result.push(`{existing}${name}`);
|
|
54
|
+
}
|
|
53
55
|
break;
|
|
54
56
|
}
|
|
55
57
|
case MySqlParser.RULE_databaseNameCreate:
|
|
@@ -57,7 +59,9 @@ export class MysqlErrorListener extends ParseErrorListener {
|
|
|
57
59
|
case MySqlParser.RULE_functionNameCreate:
|
|
58
60
|
case MySqlParser.RULE_viewNameCreate:
|
|
59
61
|
case MySqlParser.RULE_columnNameCreate: {
|
|
60
|
-
result.
|
|
62
|
+
if (!result.includes(`{new}${name}`)) {
|
|
63
|
+
result.push(`{new}${name}`);
|
|
64
|
+
}
|
|
61
65
|
break;
|
|
62
66
|
}
|
|
63
67
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { MySqlParserListener } from '../../lib';
|
|
2
|
+
import { SingleStatementContext } from '../../lib/mysql/MySqlParser';
|
|
3
|
+
import SemanticContextCollector from '../common/semanticContextCollector';
|
|
4
|
+
declare class MySqlSemanticContextCollector extends SemanticContextCollector implements MySqlParserListener {
|
|
5
|
+
getWhiteSpaceRuleType(): number;
|
|
6
|
+
getStatementRuleType(): number;
|
|
7
|
+
enterSingleStatement(ctx: SingleStatementContext): void;
|
|
8
|
+
}
|
|
9
|
+
export { MySqlSemanticContextCollector };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { MySqlParser } from '../../lib/mysql/MySqlParser';
|
|
2
|
+
import SemanticContextCollector from '../common/semanticContextCollector';
|
|
3
|
+
class MySqlSemanticContextCollector extends SemanticContextCollector {
|
|
4
|
+
getWhiteSpaceRuleType() {
|
|
5
|
+
return MySqlParser.WHITE_SPACE;
|
|
6
|
+
}
|
|
7
|
+
getStatementRuleType() {
|
|
8
|
+
return MySqlParser.RULE_singleStatement;
|
|
9
|
+
}
|
|
10
|
+
enterSingleStatement(ctx) {
|
|
11
|
+
this.visitStatement(ctx);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export { MySqlSemanticContextCollector };
|
|
@@ -2,12 +2,13 @@ import { CandidatesCollection } from 'antlr4-c3';
|
|
|
2
2
|
import { CharStream, CommonTokenStream, Token } from 'antlr4ng';
|
|
3
3
|
import { PostgreSqlLexer } from '../../lib/postgresql/PostgreSqlLexer';
|
|
4
4
|
import { PostgreSqlParser, ProgramContext } from '../../lib/postgresql/PostgreSqlParser';
|
|
5
|
+
import { CaretPosition, SemanticCollectOptions, Suggestions } from '../common/types';
|
|
5
6
|
import { BasicSQL } from '../common/basicSQL';
|
|
6
7
|
import { ErrorListener } from '../common/parseErrorListener';
|
|
7
|
-
import { Suggestions } from '../common/types';
|
|
8
8
|
import { PostgreSqlEntityCollector } from './postgreEntityCollector';
|
|
9
9
|
import { PostgreSqlErrorListener } from './postgreErrorListener';
|
|
10
10
|
import { PostgreSqlSplitListener } from './postgreSplitListener';
|
|
11
|
+
import { PostgreSemanticContextCollector } from './postgreSemanticContextCollector';
|
|
11
12
|
export { PostgreSqlEntityCollector, PostgreSqlSplitListener };
|
|
12
13
|
export declare class PostgreSQL extends BasicSQL<PostgreSqlLexer, ProgramContext, PostgreSqlParser> {
|
|
13
14
|
protected createLexerFromCharStream(charStreams: CharStream): PostgreSqlLexer;
|
|
@@ -16,5 +17,6 @@ export declare class PostgreSQL extends BasicSQL<PostgreSqlLexer, ProgramContext
|
|
|
16
17
|
protected get splitListener(): PostgreSqlSplitListener;
|
|
17
18
|
protected createErrorListener(_errorListener: ErrorListener): PostgreSqlErrorListener;
|
|
18
19
|
protected createEntityCollector(input: string, allTokens?: Token[], caretTokenIndex?: number): PostgreSqlEntityCollector;
|
|
19
|
-
protected
|
|
20
|
+
protected createSemanticContextCollector(input: string, caretPosition: CaretPosition, allTokens: Token[], options?: SemanticCollectOptions): PostgreSemanticContextCollector;
|
|
21
|
+
protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
|
|
20
22
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { processTokenCandidates } from '../common/tokenUtils';
|
|
1
2
|
import { PostgreSqlLexer } from '../../lib/postgresql/PostgreSqlLexer';
|
|
2
3
|
import { PostgreSqlParser } from '../../lib/postgresql/PostgreSqlParser';
|
|
4
|
+
import { EntityContextType, } from '../common/types';
|
|
3
5
|
import { BasicSQL } from '../common/basicSQL';
|
|
4
|
-
import { EntityContextType } from '../common/types';
|
|
5
6
|
import { PostgreSqlEntityCollector } from './postgreEntityCollector';
|
|
6
7
|
import { PostgreSqlErrorListener } from './postgreErrorListener';
|
|
7
8
|
import { PostgreSqlSplitListener } from './postgreSplitListener';
|
|
9
|
+
import { PostgreSemanticContextCollector } from './postgreSemanticContextCollector';
|
|
8
10
|
export { PostgreSqlEntityCollector, PostgreSqlSplitListener };
|
|
9
11
|
export class PostgreSQL extends BasicSQL {
|
|
10
12
|
constructor() {
|
|
@@ -24,6 +26,7 @@ export class PostgreSQL extends BasicSQL {
|
|
|
24
26
|
PostgreSqlParser.RULE_procedureName, // procedure name
|
|
25
27
|
PostgreSqlParser.RULE_columnNameCreate, // column name that will be created
|
|
26
28
|
PostgreSqlParser.RULE_columnName, // column name
|
|
29
|
+
PostgreSqlParser.RULE_columnNamePath, // column name
|
|
27
30
|
]);
|
|
28
31
|
}
|
|
29
32
|
createLexerFromCharStream(charStreams) {
|
|
@@ -42,13 +45,15 @@ export class PostgreSQL extends BasicSQL {
|
|
|
42
45
|
createEntityCollector(input, allTokens, caretTokenIndex) {
|
|
43
46
|
return new PostgreSqlEntityCollector(input, allTokens, caretTokenIndex);
|
|
44
47
|
}
|
|
45
|
-
|
|
48
|
+
createSemanticContextCollector(input, caretPosition, allTokens, options) {
|
|
49
|
+
return new PostgreSemanticContextCollector(input, caretPosition, allTokens, options);
|
|
50
|
+
}
|
|
51
|
+
processCandidates(candidates, allTokens, caretTokenIndex) {
|
|
46
52
|
const originalSyntaxSuggestions = [];
|
|
47
53
|
const keywords = [];
|
|
48
54
|
for (let candidate of candidates.rules) {
|
|
49
55
|
const [ruleType, candidateRule] = candidate;
|
|
50
|
-
const
|
|
51
|
-
const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1);
|
|
56
|
+
const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);
|
|
52
57
|
let syntaxContextType = void 0;
|
|
53
58
|
switch (ruleType) {
|
|
54
59
|
case PostgreSqlParser.RULE_tableNameCreate: {
|
|
@@ -107,6 +112,18 @@ export class PostgreSQL extends BasicSQL {
|
|
|
107
112
|
syntaxContextType = EntityContextType.COLUMN;
|
|
108
113
|
break;
|
|
109
114
|
}
|
|
115
|
+
case PostgreSqlParser.RULE_columnNamePath: {
|
|
116
|
+
if (candidateRule.ruleList.includes(PostgreSqlParser.RULE_groupClause) ||
|
|
117
|
+
candidateRule.ruleList.includes(PostgreSqlParser.RULE_sortClause) ||
|
|
118
|
+
candidateRule.ruleList.includes(PostgreSqlParser.RULE_limitClause) ||
|
|
119
|
+
candidateRule.ruleList.includes(PostgreSqlParser.RULE_whereClause) ||
|
|
120
|
+
candidateRule.ruleList.includes(PostgreSqlParser.RULE_havingClause) ||
|
|
121
|
+
candidateRule.ruleList.includes(PostgreSqlParser.RULE_windowClause) ||
|
|
122
|
+
candidateRule.ruleList.includes(PostgreSqlParser.RULE_triggerWhen)) {
|
|
123
|
+
syntaxContextType = EntityContextType.COLUMN;
|
|
124
|
+
}
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
110
127
|
default:
|
|
111
128
|
break;
|
|
112
129
|
}
|
|
@@ -117,16 +134,8 @@ export class PostgreSQL extends BasicSQL {
|
|
|
117
134
|
});
|
|
118
135
|
}
|
|
119
136
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const displayName = this._parser.vocabulary.getDisplayName(candidate[0]);
|
|
123
|
-
if (displayName && symbolicName && symbolicName.startsWith('KW_')) {
|
|
124
|
-
const keyword = displayName.startsWith("'") && displayName.endsWith("'")
|
|
125
|
-
? displayName.slice(1, -1)
|
|
126
|
-
: displayName;
|
|
127
|
-
keywords.push(keyword);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
137
|
+
const processedKeywords = processTokenCandidates(this._parser, candidates.tokens);
|
|
138
|
+
keywords.push(...processedKeywords);
|
|
130
139
|
return {
|
|
131
140
|
syntax: originalSyntaxSuggestions,
|
|
132
141
|
keywords,
|
|
@@ -15,6 +15,7 @@ export class PostgreSqlErrorListener extends ParseErrorListener {
|
|
|
15
15
|
[PostgreSqlParser.RULE_functionNameCreate, 'function'],
|
|
16
16
|
[PostgreSqlParser.RULE_columnName, 'column'],
|
|
17
17
|
[PostgreSqlParser.RULE_columnNameCreate, 'column'],
|
|
18
|
+
[PostgreSqlParser.RULE_columnNamePath, 'column'],
|
|
18
19
|
[PostgreSqlParser.RULE_schemaNameCreate, 'schema'],
|
|
19
20
|
[PostgreSqlParser.RULE_schemaName, 'schema'],
|
|
20
21
|
[PostgreSqlParser.RULE_procedureNameCreate, 'procedure'],
|
|
@@ -54,8 +55,11 @@ export class PostgreSqlErrorListener extends ParseErrorListener {
|
|
|
54
55
|
case PostgreSqlParser.RULE_viewName:
|
|
55
56
|
case PostgreSqlParser.RULE_databaseName:
|
|
56
57
|
case PostgreSqlParser.RULE_procedureName:
|
|
57
|
-
case PostgreSqlParser.RULE_columnName:
|
|
58
|
-
|
|
58
|
+
case PostgreSqlParser.RULE_columnName:
|
|
59
|
+
case PostgreSqlParser.RULE_columnNamePath: {
|
|
60
|
+
if (!result.includes(`{existing}${name}`)) {
|
|
61
|
+
result.push(`{existing}${name}`);
|
|
62
|
+
}
|
|
59
63
|
break;
|
|
60
64
|
}
|
|
61
65
|
case PostgreSqlParser.RULE_tableNameCreate:
|
|
@@ -65,7 +69,9 @@ export class PostgreSqlErrorListener extends ParseErrorListener {
|
|
|
65
69
|
case PostgreSqlParser.RULE_databaseNameCreate:
|
|
66
70
|
case PostgreSqlParser.RULE_procedureNameCreate:
|
|
67
71
|
case PostgreSqlParser.RULE_columnNameCreate: {
|
|
68
|
-
result.
|
|
72
|
+
if (!result.includes(`{new}${name}`)) {
|
|
73
|
+
result.push(`{new}${name}`);
|
|
74
|
+
}
|
|
69
75
|
break;
|
|
70
76
|
}
|
|
71
77
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { PostgreSqlParserListener } from '../../lib';
|
|
2
|
+
import { SingleStmtContext } from '../../lib/postgresql/PostgreSqlParser';
|
|
3
|
+
import SemanticContextCollector from '../common/semanticContextCollector';
|
|
4
|
+
declare class PostgreSemanticContextCollector extends SemanticContextCollector implements PostgreSqlParserListener {
|
|
5
|
+
getWhiteSpaceRuleType(): number;
|
|
6
|
+
getStatementRuleType(): number;
|
|
7
|
+
enterSingleStmt(ctx: SingleStmtContext): void;
|
|
8
|
+
}
|
|
9
|
+
export { PostgreSemanticContextCollector };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PostgreSqlParser } from '../../lib/postgresql/PostgreSqlParser';
|
|
2
|
+
import SemanticContextCollector from '../common/semanticContextCollector';
|
|
3
|
+
class PostgreSemanticContextCollector extends SemanticContextCollector {
|
|
4
|
+
getWhiteSpaceRuleType() {
|
|
5
|
+
return PostgreSqlParser.WHITE_SPACE;
|
|
6
|
+
}
|
|
7
|
+
getStatementRuleType() {
|
|
8
|
+
return PostgreSqlParser.RULE_singleStmt;
|
|
9
|
+
}
|
|
10
|
+
enterSingleStmt(ctx) {
|
|
11
|
+
this.visitStatement(ctx);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export { PostgreSemanticContextCollector };
|
|
@@ -3,11 +3,12 @@ import { CharStream, CommonTokenStream, Token } from 'antlr4ng';
|
|
|
3
3
|
import { SparkSqlLexer } from '../../lib/spark/SparkSqlLexer';
|
|
4
4
|
import { ProgramContext, SparkSqlParser } from '../../lib/spark/SparkSqlParser';
|
|
5
5
|
import { BasicSQL } from '../common/basicSQL';
|
|
6
|
+
import { Suggestions, CaretPosition, SemanticCollectOptions } from '../common/types';
|
|
6
7
|
import { ErrorListener } from '../common/parseErrorListener';
|
|
7
|
-
import { Suggestions } from '../common/types';
|
|
8
8
|
import { SparkEntityCollector } from './sparkEntityCollector';
|
|
9
9
|
import { SparkErrorListener } from './sparkErrorListener';
|
|
10
10
|
import { SparkSqlSplitListener } from './sparkSplitListener';
|
|
11
|
+
import { SparkSemanticContextCollector } from './sparkSemanticContextCollector';
|
|
11
12
|
export { SparkEntityCollector, SparkSqlSplitListener };
|
|
12
13
|
export declare class SparkSQL extends BasicSQL<SparkSqlLexer, ProgramContext, SparkSqlParser> {
|
|
13
14
|
protected createLexerFromCharStream(charStreams: CharStream): SparkSqlLexer;
|
|
@@ -16,5 +17,6 @@ export declare class SparkSQL extends BasicSQL<SparkSqlLexer, ProgramContext, Sp
|
|
|
16
17
|
protected get splitListener(): SparkSqlSplitListener;
|
|
17
18
|
protected createErrorListener(_errorListener: ErrorListener): SparkErrorListener;
|
|
18
19
|
protected createEntityCollector(input: string, allTokens?: Token[], caretTokenIndex?: number): SparkEntityCollector;
|
|
19
|
-
protected
|
|
20
|
+
protected createSemanticContextCollector(input: string, caretPosition: CaretPosition, allTokens: Token[], options?: SemanticCollectOptions): SparkSemanticContextCollector;
|
|
21
|
+
protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
|
|
20
22
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { processTokenCandidates } from '../common/tokenUtils';
|
|
1
2
|
import { SparkSqlLexer } from '../../lib/spark/SparkSqlLexer';
|
|
2
3
|
import { SparkSqlParser } from '../../lib/spark/SparkSqlParser';
|
|
3
4
|
import { BasicSQL } from '../common/basicSQL';
|
|
4
|
-
import { EntityContextType } from '../common/types';
|
|
5
|
+
import { EntityContextType, } from '../common/types';
|
|
5
6
|
import { SparkEntityCollector } from './sparkEntityCollector';
|
|
6
7
|
import { SparkErrorListener } from './sparkErrorListener';
|
|
7
8
|
import { SparkSqlSplitListener } from './sparkSplitListener';
|
|
9
|
+
import { SparkSemanticContextCollector } from './sparkSemanticContextCollector';
|
|
8
10
|
export { SparkEntityCollector, SparkSqlSplitListener };
|
|
9
11
|
export class SparkSQL extends BasicSQL {
|
|
10
12
|
constructor() {
|
|
@@ -19,6 +21,7 @@ export class SparkSQL extends BasicSQL {
|
|
|
19
21
|
SparkSqlParser.RULE_functionName,
|
|
20
22
|
SparkSqlParser.RULE_functionNameCreate,
|
|
21
23
|
SparkSqlParser.RULE_columnName,
|
|
24
|
+
SparkSqlParser.RULE_columnNamePath,
|
|
22
25
|
SparkSqlParser.RULE_columnNameCreate,
|
|
23
26
|
]);
|
|
24
27
|
}
|
|
@@ -38,13 +41,15 @@ export class SparkSQL extends BasicSQL {
|
|
|
38
41
|
createEntityCollector(input, allTokens, caretTokenIndex) {
|
|
39
42
|
return new SparkEntityCollector(input, allTokens, caretTokenIndex);
|
|
40
43
|
}
|
|
41
|
-
|
|
44
|
+
createSemanticContextCollector(input, caretPosition, allTokens, options) {
|
|
45
|
+
return new SparkSemanticContextCollector(input, caretPosition, allTokens, options);
|
|
46
|
+
}
|
|
47
|
+
processCandidates(candidates, allTokens, caretTokenIndex) {
|
|
42
48
|
const originalSyntaxSuggestions = [];
|
|
43
49
|
const keywords = [];
|
|
44
50
|
for (const candidate of candidates.rules) {
|
|
45
51
|
const [ruleType, candidateRule] = candidate;
|
|
46
|
-
const
|
|
47
|
-
const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1);
|
|
52
|
+
const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);
|
|
48
53
|
let syntaxContextType = void 0;
|
|
49
54
|
switch (ruleType) {
|
|
50
55
|
case SparkSqlParser.RULE_namespaceName: {
|
|
@@ -87,6 +92,21 @@ export class SparkSQL extends BasicSQL {
|
|
|
87
92
|
syntaxContextType = EntityContextType.COLUMN_CREATE;
|
|
88
93
|
break;
|
|
89
94
|
}
|
|
95
|
+
case SparkSqlParser.RULE_columnNamePath: {
|
|
96
|
+
if (candidateRule.ruleList.includes(SparkSqlParser.RULE_whenClause) ||
|
|
97
|
+
candidateRule.ruleList.includes(SparkSqlParser.RULE_whereClause) ||
|
|
98
|
+
candidateRule.ruleList.includes(SparkSqlParser.RULE_joinRelation) ||
|
|
99
|
+
candidateRule.ruleList.includes(SparkSqlParser.RULE_orderOrSortByClause) ||
|
|
100
|
+
candidateRule.ruleList.includes(SparkSqlParser.RULE_groupByClause) ||
|
|
101
|
+
candidateRule.ruleList.includes(SparkSqlParser.RULE_aggregationClause) ||
|
|
102
|
+
candidateRule.ruleList.includes(SparkSqlParser.RULE_havingClause) ||
|
|
103
|
+
candidateRule.ruleList.includes(SparkSqlParser.RULE_windowClause) ||
|
|
104
|
+
candidateRule.ruleList.includes(SparkSqlParser.RULE_selectClause) ||
|
|
105
|
+
candidateRule.ruleList.includes(SparkSqlParser.RULE_limitClause) ||
|
|
106
|
+
candidateRule.ruleList.includes(SparkSqlParser.RULE_clusterOrDistributeBy)) {
|
|
107
|
+
syntaxContextType = EntityContextType.COLUMN;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
90
110
|
default:
|
|
91
111
|
break;
|
|
92
112
|
}
|
|
@@ -97,16 +117,8 @@ export class SparkSQL extends BasicSQL {
|
|
|
97
117
|
});
|
|
98
118
|
}
|
|
99
119
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const displayName = this._parser.vocabulary.getDisplayName(candidate[0]);
|
|
103
|
-
if (displayName && symbolicName && symbolicName.startsWith('KW_')) {
|
|
104
|
-
const keyword = displayName.startsWith("'") && displayName.endsWith("'")
|
|
105
|
-
? displayName.slice(1, -1)
|
|
106
|
-
: displayName;
|
|
107
|
-
keywords.push(keyword);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
120
|
+
const processedKeywords = processTokenCandidates(this._parser, candidates.tokens);
|
|
121
|
+
keywords.push(...processedKeywords);
|
|
110
122
|
return {
|
|
111
123
|
syntax: originalSyntaxSuggestions,
|
|
112
124
|
keywords,
|
|
@@ -14,6 +14,7 @@ export class SparkErrorListener extends ParseErrorListener {
|
|
|
14
14
|
[SparkSqlParser.RULE_functionName, 'function'],
|
|
15
15
|
[SparkSqlParser.RULE_functionNameCreate, 'function'],
|
|
16
16
|
[SparkSqlParser.RULE_columnName, 'column'],
|
|
17
|
+
[SparkSqlParser.RULE_columnNamePath, 'column'],
|
|
17
18
|
[SparkSqlParser.RULE_columnNameCreate, 'column'],
|
|
18
19
|
]);
|
|
19
20
|
}
|
|
@@ -48,8 +49,11 @@ export class SparkErrorListener extends ParseErrorListener {
|
|
|
48
49
|
case SparkSqlParser.RULE_tableName:
|
|
49
50
|
case SparkSqlParser.RULE_viewName:
|
|
50
51
|
case SparkSqlParser.RULE_functionName:
|
|
51
|
-
case SparkSqlParser.RULE_columnName:
|
|
52
|
-
|
|
52
|
+
case SparkSqlParser.RULE_columnName:
|
|
53
|
+
case SparkSqlParser.RULE_columnNamePath: {
|
|
54
|
+
if (!result.includes(`{existing}${name}`)) {
|
|
55
|
+
result.push(`{existing}${name}`);
|
|
56
|
+
}
|
|
53
57
|
break;
|
|
54
58
|
}
|
|
55
59
|
case SparkSqlParser.RULE_namespaceNameCreate:
|
|
@@ -57,7 +61,9 @@ export class SparkErrorListener extends ParseErrorListener {
|
|
|
57
61
|
case SparkSqlParser.RULE_functionNameCreate:
|
|
58
62
|
case SparkSqlParser.RULE_viewNameCreate:
|
|
59
63
|
case SparkSqlParser.RULE_columnNameCreate: {
|
|
60
|
-
result.
|
|
64
|
+
if (!result.includes(`{new}${name}`)) {
|
|
65
|
+
result.push(`{new}${name}`);
|
|
66
|
+
}
|
|
61
67
|
break;
|
|
62
68
|
}
|
|
63
69
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SparkSqlParserListener } from '../../lib';
|
|
2
|
+
import { SingleStatementContext } from '../../lib/spark/SparkSqlParser';
|
|
3
|
+
import SemanticContextCollector from '../common/semanticContextCollector';
|
|
4
|
+
declare class SparkSemanticContextCollector extends SemanticContextCollector implements SparkSqlParserListener {
|
|
5
|
+
getWhiteSpaceRuleType(): number;
|
|
6
|
+
getStatementRuleType(): number;
|
|
7
|
+
enterSingleStatement(ctx: SingleStatementContext): void;
|
|
8
|
+
}
|
|
9
|
+
export { SparkSemanticContextCollector };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SparkSqlParser } from '../../lib/spark/SparkSqlParser';
|
|
2
|
+
import SemanticContextCollector from '../common/semanticContextCollector';
|
|
3
|
+
class SparkSemanticContextCollector extends SemanticContextCollector {
|
|
4
|
+
getWhiteSpaceRuleType() {
|
|
5
|
+
return SparkSqlParser.WHITE_SPACE;
|
|
6
|
+
}
|
|
7
|
+
getStatementRuleType() {
|
|
8
|
+
return SparkSqlParser.RULE_singleStatement;
|
|
9
|
+
}
|
|
10
|
+
enterSingleStatement(ctx) {
|
|
11
|
+
this.visitStatement(ctx);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export { SparkSemanticContextCollector };
|
|
@@ -3,11 +3,12 @@ import { CharStream, CommonTokenStream, Token } from 'antlr4ng';
|
|
|
3
3
|
import { TrinoSqlLexer } from '../../lib/trino/TrinoSqlLexer';
|
|
4
4
|
import { ProgramContext, TrinoSqlParser } from '../../lib/trino/TrinoSqlParser';
|
|
5
5
|
import { BasicSQL } from '../common/basicSQL';
|
|
6
|
+
import { Suggestions, CaretPosition, SemanticCollectOptions } from '../common/types';
|
|
6
7
|
import { ErrorListener } from '../common/parseErrorListener';
|
|
7
|
-
import { Suggestions } from '../common/types';
|
|
8
8
|
import { TrinoEntityCollector } from './trinoEntityCollector';
|
|
9
9
|
import { TrinoErrorListener } from './trinoErrorListener';
|
|
10
10
|
import { TrinoSqlSplitListener } from './trinoSplitListener';
|
|
11
|
+
import { TrinoSemanticContextCollector } from './trinoSemanticContextCollector';
|
|
11
12
|
export { TrinoEntityCollector, TrinoSqlSplitListener };
|
|
12
13
|
export declare class TrinoSQL extends BasicSQL<TrinoSqlLexer, ProgramContext, TrinoSqlParser> {
|
|
13
14
|
protected createLexerFromCharStream(charStreams: CharStream): TrinoSqlLexer;
|
|
@@ -15,6 +16,7 @@ export declare class TrinoSQL extends BasicSQL<TrinoSqlLexer, ProgramContext, Tr
|
|
|
15
16
|
protected get splitListener(): TrinoSqlSplitListener;
|
|
16
17
|
protected createErrorListener(_errorListener: ErrorListener): TrinoErrorListener;
|
|
17
18
|
protected createEntityCollector(input: string, allTokens?: Token[], caretTokenIndex?: number): TrinoEntityCollector;
|
|
19
|
+
protected createSemanticContextCollector(input: string, caretPosition: CaretPosition, allTokens: Token[], options?: SemanticCollectOptions): TrinoSemanticContextCollector;
|
|
18
20
|
protected preferredRules: Set<number>;
|
|
19
|
-
protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number
|
|
21
|
+
protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
|
|
20
22
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { processTokenCandidates } from '../common/tokenUtils';
|
|
1
2
|
import { TrinoSqlLexer } from '../../lib/trino/TrinoSqlLexer';
|
|
2
3
|
import { TrinoSqlParser } from '../../lib/trino/TrinoSqlParser';
|
|
3
4
|
import { BasicSQL } from '../common/basicSQL';
|
|
4
|
-
import { EntityContextType } from '../common/types';
|
|
5
|
+
import { EntityContextType, } from '../common/types';
|
|
5
6
|
import { TrinoEntityCollector } from './trinoEntityCollector';
|
|
6
7
|
import { TrinoErrorListener } from './trinoErrorListener';
|
|
7
8
|
import { TrinoSqlSplitListener } from './trinoSplitListener';
|
|
9
|
+
import { TrinoSemanticContextCollector } from './trinoSemanticContextCollector';
|
|
8
10
|
export { TrinoEntityCollector, TrinoSqlSplitListener };
|
|
9
11
|
export class TrinoSQL extends BasicSQL {
|
|
10
12
|
constructor() {
|
|
@@ -21,6 +23,7 @@ export class TrinoSQL extends BasicSQL {
|
|
|
21
23
|
TrinoSqlParser.RULE_functionName,
|
|
22
24
|
TrinoSqlParser.RULE_functionNameCreate,
|
|
23
25
|
TrinoSqlParser.RULE_columnRef,
|
|
26
|
+
TrinoSqlParser.RULE_columnName,
|
|
24
27
|
TrinoSqlParser.RULE_columnNameCreate,
|
|
25
28
|
]);
|
|
26
29
|
}
|
|
@@ -40,13 +43,15 @@ export class TrinoSQL extends BasicSQL {
|
|
|
40
43
|
createEntityCollector(input, allTokens, caretTokenIndex) {
|
|
41
44
|
return new TrinoEntityCollector(input, allTokens, caretTokenIndex);
|
|
42
45
|
}
|
|
43
|
-
|
|
46
|
+
createSemanticContextCollector(input, caretPosition, allTokens, options) {
|
|
47
|
+
return new TrinoSemanticContextCollector(input, caretPosition, allTokens, options);
|
|
48
|
+
}
|
|
49
|
+
processCandidates(candidates, allTokens, caretTokenIndex) {
|
|
44
50
|
const originalSyntaxSuggestions = [];
|
|
45
51
|
const keywords = [];
|
|
46
52
|
for (let candidate of candidates.rules) {
|
|
47
53
|
const [ruleType, candidateRule] = candidate;
|
|
48
|
-
const
|
|
49
|
-
const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1);
|
|
54
|
+
const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);
|
|
50
55
|
let syntaxContextType = void 0;
|
|
51
56
|
switch (ruleType) {
|
|
52
57
|
case TrinoSqlParser.RULE_catalogRef: {
|
|
@@ -97,6 +102,17 @@ export class TrinoSQL extends BasicSQL {
|
|
|
97
102
|
syntaxContextType = EntityContextType.COLUMN;
|
|
98
103
|
break;
|
|
99
104
|
}
|
|
105
|
+
case TrinoSqlParser.RULE_columnName: {
|
|
106
|
+
if (candidateRule.ruleList.includes(TrinoSqlParser.RULE_groupBy) ||
|
|
107
|
+
candidateRule.ruleList.includes(TrinoSqlParser.RULE_sortItem) ||
|
|
108
|
+
candidateRule.ruleList.includes(TrinoSqlParser.RULE_whereClause) ||
|
|
109
|
+
candidateRule.ruleList.includes(TrinoSqlParser.RULE_havingClause) ||
|
|
110
|
+
candidateRule.ruleList.includes(TrinoSqlParser.RULE_partitionBy) ||
|
|
111
|
+
candidateRule.ruleList.includes(TrinoSqlParser.RULE_whenClause) ||
|
|
112
|
+
candidateRule.ruleList.includes(TrinoSqlParser.RULE_relation)) {
|
|
113
|
+
syntaxContextType = EntityContextType.COLUMN;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
100
116
|
default:
|
|
101
117
|
break;
|
|
102
118
|
}
|
|
@@ -107,16 +123,8 @@ export class TrinoSQL extends BasicSQL {
|
|
|
107
123
|
});
|
|
108
124
|
}
|
|
109
125
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const displayName = this._parser.vocabulary.getDisplayName(candidate[0]);
|
|
113
|
-
if (displayName && symbolicName && symbolicName.startsWith('KW_')) {
|
|
114
|
-
const keyword = displayName.startsWith("'") && displayName.endsWith("'")
|
|
115
|
-
? displayName.slice(1, -1)
|
|
116
|
-
: displayName;
|
|
117
|
-
keywords.push(keyword);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
126
|
+
const processedKeywords = processTokenCandidates(this._parser, candidates.tokens);
|
|
127
|
+
keywords.push(...processedKeywords);
|
|
120
128
|
return {
|
|
121
129
|
syntax: originalSyntaxSuggestions,
|
|
122
130
|
keywords,
|
|
@@ -15,6 +15,7 @@ export class TrinoErrorListener extends ParseErrorListener {
|
|
|
15
15
|
[TrinoSqlParser.RULE_schemaNameCreate, 'schema'],
|
|
16
16
|
[TrinoSqlParser.RULE_functionName, 'function'],
|
|
17
17
|
[TrinoSqlParser.RULE_columnRef, 'column'],
|
|
18
|
+
[TrinoSqlParser.RULE_columnName, 'column'],
|
|
18
19
|
[TrinoSqlParser.RULE_columnNameCreate, 'column'],
|
|
19
20
|
]);
|
|
20
21
|
}
|
|
@@ -50,8 +51,11 @@ export class TrinoErrorListener extends ParseErrorListener {
|
|
|
50
51
|
case TrinoSqlParser.RULE_tableRef:
|
|
51
52
|
case TrinoSqlParser.RULE_viewRef:
|
|
52
53
|
case TrinoSqlParser.RULE_functionName:
|
|
53
|
-
case TrinoSqlParser.RULE_columnRef:
|
|
54
|
-
|
|
54
|
+
case TrinoSqlParser.RULE_columnRef:
|
|
55
|
+
case TrinoSqlParser.RULE_columnName: {
|
|
56
|
+
if (!result.includes(`{existing}${name}`)) {
|
|
57
|
+
result.push(`{existing}${name}`);
|
|
58
|
+
}
|
|
55
59
|
break;
|
|
56
60
|
}
|
|
57
61
|
case TrinoSqlParser.RULE_catalogNameCreate:
|
|
@@ -59,7 +63,9 @@ export class TrinoErrorListener extends ParseErrorListener {
|
|
|
59
63
|
case TrinoSqlParser.RULE_schemaNameCreate:
|
|
60
64
|
case TrinoSqlParser.RULE_viewNameCreate:
|
|
61
65
|
case TrinoSqlParser.RULE_tableNameCreate: {
|
|
62
|
-
result.
|
|
66
|
+
if (!result.includes(`{new}${name}`)) {
|
|
67
|
+
result.push(`{new}${name}`);
|
|
68
|
+
}
|
|
63
69
|
break;
|
|
64
70
|
}
|
|
65
71
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { TrinoSqlListener } from '../../lib';
|
|
2
|
+
import { StatementsContext } from '../../lib/trino/TrinoSqlParser';
|
|
3
|
+
import SemanticContextCollector from '../common/semanticContextCollector';
|
|
4
|
+
declare class TrinoSemanticContextCollector extends SemanticContextCollector implements TrinoSqlListener {
|
|
5
|
+
getWhiteSpaceRuleType(): number;
|
|
6
|
+
getStatementRuleType(): number;
|
|
7
|
+
enterStatements(ctx: StatementsContext): void;
|
|
8
|
+
}
|
|
9
|
+
export { TrinoSemanticContextCollector };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { TrinoSqlParser } from '../../lib/trino/TrinoSqlParser';
|
|
2
|
+
import SemanticContextCollector from '../common/semanticContextCollector';
|
|
3
|
+
class TrinoSemanticContextCollector extends SemanticContextCollector {
|
|
4
|
+
getWhiteSpaceRuleType() {
|
|
5
|
+
return TrinoSqlParser.WHITE_SPACE;
|
|
6
|
+
}
|
|
7
|
+
getStatementRuleType() {
|
|
8
|
+
return TrinoSqlParser.RULE_statements;
|
|
9
|
+
}
|
|
10
|
+
enterStatements(ctx) {
|
|
11
|
+
this.visitStatement(ctx);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export { TrinoSemanticContextCollector };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dt-sql-parser",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"authors": "DTStack Corporation",
|
|
5
5
|
"description": "SQL Parsers for BigData, built with antlr4",
|
|
6
6
|
"keywords": [
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"prepublishOnly": "npm run build",
|
|
28
28
|
"antlr4": "node ./scripts/antlr4.js",
|
|
29
29
|
"build": "rm -rf dist && tsc",
|
|
30
|
+
"watch": "tsc -w",
|
|
30
31
|
"check-types": "tsc -p ./tsconfig.json && tsc -p ./test/tsconfig.json",
|
|
31
32
|
"test": "NODE_OPTIONS=--max_old_space_size=4096 && jest",
|
|
32
33
|
"release": "node ./scripts/release.js",
|