dt-sql-parser 4.1.0-beta.8 → 4.1.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.
Files changed (47) hide show
  1. package/CHANGELOG.md +369 -0
  2. package/README.md +0 -51
  3. package/dist/lib/flink/FlinkSqlParser.d.ts +6 -2
  4. package/dist/lib/flink/FlinkSqlParser.js +2207 -2118
  5. package/dist/lib/postgresql/PostgreSqlParser.d.ts +3 -4
  6. package/dist/lib/postgresql/PostgreSqlParser.js +9175 -9197
  7. package/dist/lib/trino/TrinoSqlListener.d.ts +13 -0
  8. package/dist/lib/trino/TrinoSqlParser.d.ts +17 -4
  9. package/dist/lib/trino/TrinoSqlParser.js +1958 -1970
  10. package/dist/lib/trino/TrinoSqlVisitor.d.ts +8 -0
  11. package/dist/parser/common/basicSQL.d.ts +2 -15
  12. package/dist/parser/common/basicSQL.js +1 -15
  13. package/dist/parser/common/entityCollector.d.ts +7 -1
  14. package/dist/parser/common/entityCollector.js +20 -3
  15. package/dist/parser/common/types.d.ts +0 -25
  16. package/dist/parser/common/types.js +0 -7
  17. package/dist/parser/flink/index.d.ts +6 -8
  18. package/dist/parser/flink/index.js +5 -9
  19. package/dist/parser/hive/index.d.ts +5 -7
  20. package/dist/parser/hive/index.js +4 -8
  21. package/dist/parser/impala/index.d.ts +5 -7
  22. package/dist/parser/impala/index.js +4 -8
  23. package/dist/parser/mysql/index.d.ts +5 -7
  24. package/dist/parser/mysql/index.js +4 -8
  25. package/dist/parser/postgresql/index.d.ts +4 -6
  26. package/dist/parser/postgresql/index.js +4 -8
  27. package/dist/parser/spark/index.d.ts +7 -9
  28. package/dist/parser/spark/index.js +5 -9
  29. package/dist/parser/trino/index.d.ts +7 -9
  30. package/dist/parser/trino/index.js +5 -9
  31. package/package.json +2 -1
  32. package/dist/parser/common/semanticContextCollector.d.ts +0 -51
  33. package/dist/parser/common/semanticContextCollector.js +0 -202
  34. package/dist/parser/flink/flinkSemanticContextCollector.d.ts +0 -9
  35. package/dist/parser/flink/flinkSemanticContextCollector.js +0 -14
  36. package/dist/parser/hive/hiveSemanticContextCollector.d.ts +0 -9
  37. package/dist/parser/hive/hiveSemanticContextCollector.js +0 -14
  38. package/dist/parser/impala/impalaSemanticContextCollector.d.ts +0 -9
  39. package/dist/parser/impala/impalaSemanticContextCollector.js +0 -14
  40. package/dist/parser/mysql/mysqlSemanticContextCollector.d.ts +0 -9
  41. package/dist/parser/mysql/mysqlSemanticContextCollector.js +0 -14
  42. package/dist/parser/postgresql/postgreSemanticContextCollector.d.ts +0 -9
  43. package/dist/parser/postgresql/postgreSemanticContextCollector.js +0 -14
  44. package/dist/parser/spark/sparkSemanticContextCollector.d.ts +0 -9
  45. package/dist/parser/spark/sparkSemanticContextCollector.js +0 -14
  46. package/dist/parser/trino/trinoSemanticContextCollector.d.ts +0 -9
  47. package/dist/parser/trino/trinoSemanticContextCollector.js +0 -14
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dt-sql-parser",
3
- "version": "4.1.0-beta.8",
3
+ "version": "4.1.0",
4
4
  "authors": "DTStack Corporation",
5
5
  "description": "SQL Parsers for BigData, built with antlr4",
6
6
  "keywords": [
@@ -55,6 +55,7 @@
55
55
  "chalk": "4.1.2",
56
56
  "commitizen": "^4.3.0",
57
57
  "console-table-printer": "^2.12.0",
58
+ "crypto": "^1.0.1",
58
59
  "envinfo": "^7.11.1",
59
60
  "glob": "^10.3.10",
60
61
  "husky": "^8.0.3",
@@ -1,51 +0,0 @@
1
- import { ErrorNode, ParserRuleContext, TerminalNode, Token } from 'antlr4ng';
2
- import { CaretPosition, SemanticCollectOptions, SemanticContext } from '../common/types';
3
- export declare const SQL_SPLIT_SYMBOL_TEXT = ";";
4
- declare abstract class SemanticContextCollector {
5
- constructor(_input: string, caretPosition: CaretPosition, allTokens: Token[], options?: SemanticCollectOptions);
6
- readonly options: SemanticCollectOptions;
7
- private _tokenIndex;
8
- private _allTokens;
9
- /**
10
- * If current caret position is in a beginning of statement semantics, it needs to follow some cases:
11
- * @case1 there is no statement node with an error before the current statement in the parse tree;
12
- *
13
- * @case2 if it is an uncomplete keyword, it will be parsed as an `ErrorNode`
14
- * and need be a direct child node of `program`;
15
- *
16
- * @case3 if it is a complete keyword, the parsed TerminalNode or ErrorNode should be
17
- * the first leaf node of current statement rule;
18
- *
19
- * @case4 if it is whiteSpace in caret position, we can't visit it in antlr4 listener,
20
- * so we find the first unhidden token before the whiteSpace token, and the unhidden token
21
- * should be the last leaf node of statement its belongs to;
22
- *
23
- * @case5 if the previous token is split symbol like `;`, ignore case1 and forcefully judged as beginning of statement.
24
- */
25
- private _isStatementBeginning;
26
- /**
27
- * Prev tokenIndex that not white space before current tokenIndex or caret position
28
- */
29
- private _prevTokenIndex;
30
- get semanticContext(): SemanticContext;
31
- abstract getWhiteSpaceRuleType(): number;
32
- abstract getStatementRuleType(): number;
33
- private prevStatementHasError;
34
- /**
35
- * Most root rule is `program`.
36
- */
37
- private isRootRule;
38
- /**
39
- * link to @case4
40
- * It should be called in each language's own `enterStatement`.
41
- */
42
- protected visitStatement(ctx: ParserRuleContext): void;
43
- /**
44
- * Uncomplete keyword will be error node
45
- */
46
- visitErrorNode(node: ErrorNode): void;
47
- visitTerminal(node: TerminalNode): void;
48
- enterEveryRule(_node: ParserRuleContext): void;
49
- exitEveryRule(_node: ParserRuleContext): void;
50
- }
51
- export default SemanticContextCollector;
@@ -1,202 +0,0 @@
1
- import { ErrorNode, ParserRuleContext, Token } from 'antlr4ng';
2
- import { findCaretTokenIndex } from '../common/findCaretTokenIndex';
3
- import { SqlSplitStrategy, } from '../common/types';
4
- export const SQL_SPLIT_SYMBOL_TEXT = ';';
5
- class SemanticContextCollector {
6
- constructor(_input, caretPosition, allTokens, options) {
7
- this.options = {
8
- sqlSplitStrategy: SqlSplitStrategy.STRICT,
9
- };
10
- this._allTokens = [];
11
- /**
12
- * If current caret position is in a beginning of statement semantics, it needs to follow some cases:
13
- * @case1 there is no statement node with an error before the current statement in the parse tree;
14
- *
15
- * @case2 if it is an uncomplete keyword, it will be parsed as an `ErrorNode`
16
- * and need be a direct child node of `program`;
17
- *
18
- * @case3 if it is a complete keyword, the parsed TerminalNode or ErrorNode should be
19
- * the first leaf node of current statement rule;
20
- *
21
- * @case4 if it is whiteSpace in caret position, we can't visit it in antlr4 listener,
22
- * so we find the first unhidden token before the whiteSpace token, and the unhidden token
23
- * should be the last leaf node of statement its belongs to;
24
- *
25
- * @case5 if the previous token is split symbol like `;`, ignore case1 and forcefully judged as beginning of statement.
26
- */
27
- this._isStatementBeginning = false;
28
- // If caretPosition token is whiteSpace, tokenIndex may be undefined.
29
- const tokenIndex = findCaretTokenIndex(caretPosition, allTokens);
30
- if (tokenIndex !== undefined) {
31
- this._tokenIndex = tokenIndex;
32
- }
33
- this._allTokens = allTokens;
34
- this.options = Object.assign(Object.assign({}, this.options), options);
35
- if (allTokens === null || allTokens === void 0 ? void 0 : allTokens.length) {
36
- let i = tokenIndex ? tokenIndex - 1 : allTokens.length - 1;
37
- /**
38
- * Link to @case4 and @case5
39
- * Find the previous unhidden token.
40
- * If can't find tokenIndex or current token is whiteSpace at caretPosition,
41
- * prevTokenIndex is useful to help us determine if it is beginning of statement.
42
- */
43
- while (i >= 0) {
44
- if (allTokens[i].channel !== Token.HIDDEN_CHANNEL &&
45
- (allTokens[i].line < caretPosition.lineNumber ||
46
- (allTokens[i].line === caretPosition.lineNumber &&
47
- allTokens[i].column < caretPosition.column))) {
48
- this._prevTokenIndex = allTokens[i].tokenIndex;
49
- break;
50
- }
51
- i--;
52
- }
53
- /**
54
- * We can directly conclude beginning of statement semantics when current token is
55
- * the first token of tokenStream or the previous token is semicolon
56
- */
57
- if (tokenIndex === 0 ||
58
- i === -1 ||
59
- (this._prevTokenIndex &&
60
- this._allTokens[this._prevTokenIndex].text === SQL_SPLIT_SYMBOL_TEXT)) {
61
- this._isStatementBeginning = true;
62
- }
63
- }
64
- }
65
- get semanticContext() {
66
- return {
67
- isStatementBeginning: this._isStatementBeginning,
68
- };
69
- }
70
- prevStatementHasError(node) {
71
- let parent = node.parent;
72
- if (!parent)
73
- return false;
74
- const currentNodeIndex = parent.children.findIndex((child) => child === node);
75
- if (currentNodeIndex <= 0)
76
- return false;
77
- for (let i = currentNodeIndex - 1; i >= 0; i--) {
78
- const prevNode = parent.children[i];
79
- if (prevNode instanceof ErrorNode ||
80
- (prevNode instanceof ParserRuleContext && prevNode.exception !== null))
81
- return true;
82
- }
83
- return false;
84
- }
85
- /**
86
- * Most root rule is `program`.
87
- */
88
- isRootRule(node) {
89
- return node instanceof ParserRuleContext && (node === null || node === void 0 ? void 0 : node.parent) === null;
90
- }
91
- /**
92
- * link to @case4
93
- * It should be called in each language's own `enterStatement`.
94
- */
95
- visitStatement(ctx) {
96
- var _a, _b, _c;
97
- if (this.options.sqlSplitStrategy === SqlSplitStrategy.STRICT)
98
- return;
99
- const isWhiteSpaceToken = this._tokenIndex === undefined ||
100
- ((_a = this._allTokens[this._tokenIndex]) === null || _a === void 0 ? void 0 : _a.type) === this.getWhiteSpaceRuleType() ||
101
- // PostgreSQL whiteSpace not inlcudes '\n' symbol
102
- ((_b = this._allTokens[this._tokenIndex]) === null || _b === void 0 ? void 0 : _b.text) === '\n';
103
- const isPrevTokenEndOfStatement = this._prevTokenIndex && ((_c = ctx.stop) === null || _c === void 0 ? void 0 : _c.tokenIndex) === this._prevTokenIndex;
104
- if (isWhiteSpaceToken && isPrevTokenEndOfStatement && ctx.exception === null) {
105
- this._isStatementBeginning = !this.prevStatementHasError(ctx)
106
- ? true
107
- : this._isStatementBeginning;
108
- }
109
- }
110
- /**
111
- * Uncomplete keyword will be error node
112
- */
113
- visitErrorNode(node) {
114
- var _a, _b, _c, _d;
115
- if (node.symbol.tokenIndex !== this._tokenIndex ||
116
- this._isStatementBeginning ||
117
- this.options.sqlSplitStrategy === SqlSplitStrategy.STRICT)
118
- return;
119
- let parent = node.parent;
120
- let currentNode = node;
121
- /**
122
- * Link to @case2
123
- * The error node is a direct child node of the program node
124
- */
125
- if (this.isRootRule(parent)) {
126
- this._isStatementBeginning = !this.prevStatementHasError(currentNode);
127
- return;
128
- }
129
- /**
130
- * Link to @case3
131
- * Error node must be the first leaf node of the statement parse tree.
132
- **/
133
- while (parent !== null && parent.ruleIndex !== this.getStatementRuleType()) {
134
- if (((_a = parent.children) === null || _a === void 0 ? void 0 : _a[0]) !== currentNode) {
135
- this._isStatementBeginning = false;
136
- return;
137
- }
138
- currentNode = parent;
139
- parent = currentNode.parent;
140
- }
141
- let isStatementBeginning = true;
142
- /**
143
- * Link to @case1
144
- * Previous statement must have no exception
145
- */
146
- if ((parent === null || parent === void 0 ? void 0 : parent.ruleIndex) === this.getStatementRuleType()) {
147
- const programRule = parent.parent;
148
- const currentStatementRuleIndex = ((_b = programRule === null || programRule === void 0 ? void 0 : programRule.children) === null || _b === void 0 ? void 0 : _b.findIndex((node) => node === parent)) || -1;
149
- if (currentStatementRuleIndex > 0) {
150
- /**
151
- * When you typed a keyword and doesn't match any rule, you will get a EOF error,
152
- * For example, just typed 'CREATE', 'INSERT'.
153
- */
154
- const isStatementEOF = ((_d = (_c = parent.exception) === null || _c === void 0 ? void 0 : _c.offendingToken) === null || _d === void 0 ? void 0 : _d.text) === '<EOF>';
155
- isStatementBeginning =
156
- this.prevStatementHasError(parent) && !isStatementEOF
157
- ? false
158
- : isStatementBeginning;
159
- }
160
- }
161
- this._isStatementBeginning = isStatementBeginning;
162
- }
163
- visitTerminal(node) {
164
- var _a, _b;
165
- if (node.symbol.tokenIndex !== this._tokenIndex ||
166
- this._isStatementBeginning ||
167
- this.options.sqlSplitStrategy === SqlSplitStrategy.STRICT)
168
- return;
169
- let currentNode = node;
170
- let parent = node.parent;
171
- /**
172
- * Link to @case3
173
- * Current terminal node must be the first leaf node of the statement parse tree.
174
- **/
175
- while (parent !== null && parent.ruleIndex !== this.getStatementRuleType()) {
176
- if (((_a = parent.children) === null || _a === void 0 ? void 0 : _a[0]) !== currentNode) {
177
- this._isStatementBeginning = false;
178
- return;
179
- }
180
- currentNode = parent;
181
- parent = currentNode.parent;
182
- }
183
- let isStatementBeginning = true;
184
- /**
185
- * Link to @case1
186
- * Previous statement must have no exception
187
- */
188
- if ((parent === null || parent === void 0 ? void 0 : parent.ruleIndex) === this.getStatementRuleType()) {
189
- const programRule = parent.parent;
190
- const currentStatementRuleIndex = ((_b = programRule === null || programRule === void 0 ? void 0 : programRule.children) === null || _b === void 0 ? void 0 : _b.findIndex((node) => node === parent)) || -1;
191
- if (currentStatementRuleIndex > 0) {
192
- isStatementBeginning = this.prevStatementHasError(parent)
193
- ? false
194
- : isStatementBeginning;
195
- }
196
- }
197
- this._isStatementBeginning = isStatementBeginning;
198
- }
199
- enterEveryRule(_node) { }
200
- exitEveryRule(_node) { }
201
- }
202
- export default SemanticContextCollector;
@@ -1,9 +0,0 @@
1
- import { FlinkSqlParserListener } from '../../lib';
2
- import { SingleStatementContext } from '../../lib/flink/FlinkSqlParser';
3
- import SemanticContextCollector from '../common/semanticContextCollector';
4
- declare class FlinkSemanticContextCollector extends SemanticContextCollector implements FlinkSqlParserListener {
5
- getWhiteSpaceRuleType(): number;
6
- getStatementRuleType(): number;
7
- enterSingleStatement(ctx: SingleStatementContext): void;
8
- }
9
- export { FlinkSemanticContextCollector };
@@ -1,14 +0,0 @@
1
- import { FlinkSqlParser } from '../../lib/flink/FlinkSqlParser';
2
- import SemanticContextCollector from '../common/semanticContextCollector';
3
- class FlinkSemanticContextCollector extends SemanticContextCollector {
4
- getWhiteSpaceRuleType() {
5
- return FlinkSqlParser.SPACE;
6
- }
7
- getStatementRuleType() {
8
- return FlinkSqlParser.RULE_singleStatement;
9
- }
10
- enterSingleStatement(ctx) {
11
- this.visitStatement(ctx);
12
- }
13
- }
14
- export { FlinkSemanticContextCollector };
@@ -1,9 +0,0 @@
1
- import { HiveSqlParserListener } from '../../lib';
2
- import { StatementContext } from '../../lib/hive/HiveSqlParser';
3
- import SemanticContextCollector from '../common/semanticContextCollector';
4
- declare class HiveSemanticContextCollector extends SemanticContextCollector implements HiveSqlParserListener {
5
- getWhiteSpaceRuleType(): number;
6
- getStatementRuleType(): number;
7
- enterStatement(ctx: StatementContext): void;
8
- }
9
- export { HiveSemanticContextCollector };
@@ -1,14 +0,0 @@
1
- import { HiveSqlParser } from '../../lib/hive/HiveSqlParser';
2
- import SemanticContextCollector from '../common/semanticContextCollector';
3
- class HiveSemanticContextCollector extends SemanticContextCollector {
4
- getWhiteSpaceRuleType() {
5
- return HiveSqlParser.WHITE_SPACE;
6
- }
7
- getStatementRuleType() {
8
- return HiveSqlParser.RULE_statement;
9
- }
10
- enterStatement(ctx) {
11
- this.visitStatement(ctx);
12
- }
13
- }
14
- export { HiveSemanticContextCollector };
@@ -1,9 +0,0 @@
1
- import { ImpalaSqlParserListener } from '../../lib';
2
- import { SingleStatementContext } from '../../lib/impala/ImpalaSqlParser';
3
- import SemanticContextCollector from '../common/semanticContextCollector';
4
- declare class ImpalaSemanticContextCollector extends SemanticContextCollector implements ImpalaSqlParserListener {
5
- getWhiteSpaceRuleType(): number;
6
- getStatementRuleType(): number;
7
- enterSingleStatement(ctx: SingleStatementContext): void;
8
- }
9
- export { ImpalaSemanticContextCollector };
@@ -1,14 +0,0 @@
1
- import { ImpalaSqlParser } from '../../lib/impala/ImpalaSqlParser';
2
- import SemanticContextCollector from '../common/semanticContextCollector';
3
- class ImpalaSemanticContextCollector extends SemanticContextCollector {
4
- getWhiteSpaceRuleType() {
5
- return ImpalaSqlParser.WS;
6
- }
7
- getStatementRuleType() {
8
- return ImpalaSqlParser.RULE_singleStatement;
9
- }
10
- enterSingleStatement(ctx) {
11
- this.visitStatement(ctx);
12
- }
13
- }
14
- export { ImpalaSemanticContextCollector };
@@ -1,9 +0,0 @@
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 };
@@ -1,14 +0,0 @@
1
- import { MySqlParser } from '../../lib/mysql/MySqlParser';
2
- import SemanticContextCollector from '../common/semanticContextCollector';
3
- class MySqlSemanticContextCollector extends SemanticContextCollector {
4
- getWhiteSpaceRuleType() {
5
- return MySqlParser.SPACE;
6
- }
7
- getStatementRuleType() {
8
- return MySqlParser.RULE_singleStatement;
9
- }
10
- enterSingleStatement(ctx) {
11
- this.visitStatement(ctx);
12
- }
13
- }
14
- export { MySqlSemanticContextCollector };
@@ -1,9 +0,0 @@
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 };
@@ -1,14 +0,0 @@
1
- import { PostgreSqlParser } from '../../lib/postgresql/PostgreSqlParser';
2
- import SemanticContextCollector from '../common/semanticContextCollector';
3
- class PostgreSemanticContextCollector extends SemanticContextCollector {
4
- getWhiteSpaceRuleType() {
5
- return PostgreSqlParser.Whitespace;
6
- }
7
- getStatementRuleType() {
8
- return PostgreSqlParser.RULE_singleStmt;
9
- }
10
- enterSingleStmt(ctx) {
11
- this.visitStatement(ctx);
12
- }
13
- }
14
- export { PostgreSemanticContextCollector };
@@ -1,9 +0,0 @@
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 };
@@ -1,14 +0,0 @@
1
- import { SparkSqlParser } from '../../lib/spark/SparkSqlParser';
2
- import SemanticContextCollector from '../common/semanticContextCollector';
3
- class SparkSemanticContextCollector extends SemanticContextCollector {
4
- getWhiteSpaceRuleType() {
5
- return SparkSqlParser.WS;
6
- }
7
- getStatementRuleType() {
8
- return SparkSqlParser.RULE_singleStatement;
9
- }
10
- enterSingleStatement(ctx) {
11
- this.visitStatement(ctx);
12
- }
13
- }
14
- export { SparkSemanticContextCollector };
@@ -1,9 +0,0 @@
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 };
@@ -1,14 +0,0 @@
1
- import { TrinoSqlParser } from '../../lib/trino/TrinoSqlParser';
2
- import SemanticContextCollector from '../common/semanticContextCollector';
3
- class TrinoSemanticContextCollector extends SemanticContextCollector {
4
- getWhiteSpaceRuleType() {
5
- return TrinoSqlParser.WS;
6
- }
7
- getStatementRuleType() {
8
- return TrinoSqlParser.RULE_statements;
9
- }
10
- enterStatements(ctx) {
11
- this.visitStatement(ctx);
12
- }
13
- }
14
- export { TrinoSemanticContextCollector };