dt-sql-parser 4.0.0-beta.4.1 → 4.0.0-beta.4.3
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 +124 -43
- package/dist/index.d.ts +5 -5
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/lib/flinksql/FlinkSqlLexer.d.ts +202 -201
- package/dist/lib/flinksql/FlinkSqlLexer.js +2003 -1996
- package/dist/lib/flinksql/FlinkSqlLexer.js.map +1 -1
- package/dist/lib/flinksql/FlinkSqlParser.d.ts +328 -265
- package/dist/lib/flinksql/FlinkSqlParser.js +3712 -3076
- package/dist/lib/flinksql/FlinkSqlParser.js.map +1 -1
- package/dist/lib/flinksql/FlinkSqlParserListener.d.ts +44 -0
- package/dist/lib/flinksql/FlinkSqlParserVisitor.d.ts +28 -0
- package/dist/lib/hive/HiveSqlLexer.d.ts +365 -351
- package/dist/lib/hive/HiveSqlLexer.js +2494 -2457
- package/dist/lib/hive/HiveSqlLexer.js.map +1 -1
- package/dist/lib/hive/{HiveSql.d.ts → HiveSqlParser.d.ts} +2791 -2463
- package/dist/lib/hive/{HiveSql.js → HiveSqlParser.js} +18554 -16800
- package/dist/lib/hive/HiveSqlParser.js.map +1 -0
- package/dist/lib/hive/{HiveSqlListener.d.ts → HiveSqlParserListener.d.ts} +1759 -1517
- package/dist/lib/hive/HiveSqlParserListener.js +3 -0
- package/dist/lib/hive/HiveSqlParserListener.js.map +1 -0
- package/dist/lib/hive/{HiveSqlVisitor.d.ts → HiveSqlParserVisitor.d.ts} +1165 -1011
- package/dist/lib/hive/HiveSqlParserVisitor.js +3 -0
- package/dist/lib/hive/HiveSqlParserVisitor.js.map +1 -0
- package/dist/lib/pgsql/PostgreSQLLexerBase.js +0 -4
- package/dist/lib/pgsql/PostgreSQLLexerBase.js.map +1 -1
- package/dist/lib/pgsql/PostgreSQLParserBase.js +0 -6
- package/dist/lib/pgsql/PostgreSQLParserBase.js.map +1 -1
- package/dist/lib/spark/SparkSqlLexer.d.ts +398 -322
- package/dist/lib/spark/SparkSqlLexer.js +2302 -1738
- package/dist/lib/spark/SparkSqlLexer.js.map +1 -1
- package/dist/lib/spark/SparkSqlParser.d.ts +3319 -3314
- package/dist/lib/spark/SparkSqlParser.js +18718 -16132
- package/dist/lib/spark/SparkSqlParser.js.map +1 -1
- package/dist/lib/spark/SparkSqlParserListener.d.ts +2130 -0
- package/dist/lib/spark/SparkSqlParserListener.js +3 -0
- package/dist/lib/spark/SparkSqlParserListener.js.map +1 -0
- package/dist/lib/spark/SparkSqlParserVisitor.d.ts +1361 -0
- package/dist/lib/spark/SparkSqlParserVisitor.js +3 -0
- package/dist/lib/spark/SparkSqlParserVisitor.js.map +1 -0
- package/dist/parser/common/basic-parser-types.d.ts +16 -3
- package/dist/parser/common/basic-parser-types.js +12 -2
- package/dist/parser/common/basic-parser-types.js.map +1 -1
- package/dist/parser/common/basicParser.d.ts +27 -28
- package/dist/parser/common/basicParser.js +76 -60
- package/dist/parser/common/basicParser.js.map +1 -1
- package/dist/parser/common/parseErrorListener.d.ts +33 -0
- package/dist/parser/common/parseErrorListener.js +28 -0
- package/dist/parser/common/parseErrorListener.js.map +1 -0
- package/dist/parser/flinksql.d.ts +1 -1
- package/dist/parser/flinksql.js +32 -13
- package/dist/parser/flinksql.js.map +1 -1
- package/dist/parser/generic.js +1 -1
- package/dist/parser/generic.js.map +1 -1
- package/dist/parser/hive.d.ts +12 -5
- package/dist/parser/hive.js +91 -7
- package/dist/parser/hive.js.map +1 -1
- package/dist/parser/index.d.ts +1 -0
- package/dist/parser/index.js +1 -0
- package/dist/parser/index.js.map +1 -1
- package/dist/parser/pgsql.js +1 -1
- package/dist/parser/pgsql.js.map +1 -1
- package/dist/parser/plsql.js +1 -1
- package/dist/parser/plsql.js.map +1 -1
- package/dist/parser/spark.d.ts +10 -3
- package/dist/parser/spark.js +90 -7
- package/dist/parser/spark.js.map +1 -1
- package/dist/parser/trinosql.js +1 -1
- package/dist/parser/trinosql.js.map +1 -1
- package/dist/utils/findCaretTokenIndex.d.ts +2 -2
- package/dist/utils/findCaretTokenIndex.js +5 -4
- package/dist/utils/findCaretTokenIndex.js.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +1 -5
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/token.d.ts +2 -2
- package/dist/utils/token.js +2 -2
- package/dist/utils/token.js.map +1 -1
- package/package.json +16 -9
- package/dist/lib/hive/HiveSql.js.map +0 -1
- package/dist/lib/hive/HiveSqlListener.js +0 -3
- package/dist/lib/hive/HiveSqlListener.js.map +0 -1
- package/dist/lib/hive/HiveSqlVisitor.js +0 -3
- package/dist/lib/hive/HiveSqlVisitor.js.map +0 -1
- package/dist/lib/spark/SparkSqlListener.d.ts +0 -3516
- package/dist/lib/spark/SparkSqlListener.js +0 -3
- package/dist/lib/spark/SparkSqlListener.js.map +0 -1
- package/dist/lib/spark/SparkSqlVisitor.d.ts +0 -2201
- package/dist/lib/spark/SparkSqlVisitor.js +0 -3
- package/dist/lib/spark/SparkSqlVisitor.js.map +0 -1
- package/dist/parser/common/parserErrorListener.d.ts +0 -30
- package/dist/parser/common/parserErrorListener.js +0 -62
- package/dist/parser/common/parserErrorListener.js.map +0 -1
package/README.md
CHANGED
|
@@ -15,9 +15,9 @@ English | [简体中文](./README-zh_CN.md)
|
|
|
15
15
|
|
|
16
16
|
dt-sql-parser is a **SQL Parser** project built with [ANTLR4](https://github.com/antlr/antlr4), and it's mainly for the **BigData** domain. The [ANTLR4](https://github.com/antlr/antlr4) generated the basic Parser, Visitor, and Listener, so it's easy to complete the **syntax validation**, **tokenizer**, **traverse** the AST, and so on features.
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
Additionally, it provides auxiliary functions such as SQL splitting and Auto-Complete.
|
|
19
19
|
|
|
20
|
-
Supported SQL
|
|
20
|
+
**Supported SQL**:
|
|
21
21
|
|
|
22
22
|
- Generic SQL (MySQL)
|
|
23
23
|
- Flink SQL
|
|
@@ -27,32 +27,56 @@ Supported SQL:
|
|
|
27
27
|
- PostgreSQL
|
|
28
28
|
- Trino SQL
|
|
29
29
|
|
|
30
|
+
**Supported auxiliary methods**
|
|
31
|
+
|
|
32
|
+
| SQL Type | SQL Split | Auto-Complete |
|
|
33
|
+
| ----------- | -------- | -------- |
|
|
34
|
+
| Generic SQL | WIP | WIP |
|
|
35
|
+
| Flink SQL | ✅ | ✅ |
|
|
36
|
+
| Spark SQL | ✅ | ✅ |
|
|
37
|
+
| Hive SQL | ✅ | ✅ |
|
|
38
|
+
| PL/SQL | WIP | WIP |
|
|
39
|
+
| Postgre SQL | WIP | WIP |
|
|
40
|
+
| Trino SQL | WIP | WIP |
|
|
41
|
+
|
|
30
42
|
>Tips: This project is the default for Javascript language, also you can try to compile it to other languages if you need.
|
|
31
43
|
|
|
44
|
+
<br/>
|
|
45
|
+
|
|
32
46
|
## Integrating SQL Parser with Monaco Editor
|
|
33
47
|
|
|
34
48
|
We have provided a [monaco-sql-languages](https://github.com/DTStack/monaco-sql-languages) package, you can integrate with `monaco-editor`
|
|
35
49
|
easily.
|
|
36
50
|
|
|
51
|
+
<br/>
|
|
52
|
+
|
|
37
53
|
## Installation
|
|
38
54
|
|
|
39
55
|
```bash
|
|
40
|
-
|
|
56
|
+
# use npm
|
|
41
57
|
npm i dt-sql-parser --save
|
|
42
58
|
|
|
43
|
-
|
|
59
|
+
# use yarn
|
|
44
60
|
yarn add dt-sql-parser
|
|
45
61
|
```
|
|
46
62
|
|
|
47
|
-
|
|
63
|
+
<br/>
|
|
48
64
|
|
|
49
|
-
|
|
65
|
+
## Usage
|
|
66
|
+
We recommend learning the Fundamentals usage before continuing. The dt-sql-parser library provides SQL Parser classes for different types of SQL.
|
|
67
|
+
```javascript
|
|
68
|
+
import { GenericSQL, FlinkSQL, SparkSQL, HiveSQL, PLSQL, PostgresSQL, TrinoSQL } from 'dt-sql-parser';
|
|
69
|
+
```
|
|
50
70
|
|
|
51
|
-
|
|
52
|
-
|
|
71
|
+
Before employing syntax validation, code completion, and other features, it is necessary to instantiate the Parser of the relevant SQL type.
|
|
72
|
+
For instance, one can consider using `GenericSQL` as an example:
|
|
73
|
+
```javascript
|
|
74
|
+
const parser = new GenericSQL();
|
|
75
|
+
```
|
|
53
76
|
|
|
54
|
-
The
|
|
77
|
+
The following usage examples will utilize the `GenericSQL`, and the Parser for other SQL types will be employed in a similar manner as `GenericSQL`.
|
|
55
78
|
|
|
79
|
+
### Syntax Validation
|
|
56
80
|
```javascript
|
|
57
81
|
import GenericSQL from 'dt-sql-parser/dist/parser/generic';
|
|
58
82
|
|
|
@@ -197,50 +221,107 @@ TableName user1
|
|
|
197
221
|
|
|
198
222
|
> Tips: The node's method name can be found in the Listener file under the corresponding SQL directory
|
|
199
223
|
|
|
200
|
-
###
|
|
201
|
-
|
|
202
|
-
Clear the **comments** and **spaces** before and after
|
|
203
|
-
|
|
224
|
+
### Splitting SQL statements
|
|
225
|
+
Take `FlinkSQL` as an example:
|
|
204
226
|
```javascript
|
|
205
|
-
import {
|
|
206
|
-
|
|
207
|
-
const sql =
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
console.log(cleanedSql)
|
|
227
|
+
import { FlinkSQL } from 'dt-sql-parser';
|
|
228
|
+
const parser = new FlinkSQL();
|
|
229
|
+
const sql = 'SHOW TABLES;\nSELECT * FROM tb;';
|
|
230
|
+
const sqlSlices = parser.splitSQLByStatement(sql);
|
|
231
|
+
console.log(sqlSlices)
|
|
211
232
|
|
|
212
233
|
/*
|
|
213
|
-
|
|
234
|
+
[
|
|
235
|
+
{
|
|
236
|
+
startIndex: 0,
|
|
237
|
+
endIndex: 11,
|
|
238
|
+
startLine: 1,
|
|
239
|
+
endLine: 1,
|
|
240
|
+
startColumn: 1,
|
|
241
|
+
endColumn: 12,
|
|
242
|
+
text: 'SHOW TABLES;'
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
startIndex: 13,
|
|
246
|
+
endIndex: 29,
|
|
247
|
+
startLine: 2,
|
|
248
|
+
endLine: 2,
|
|
249
|
+
startColumn: 1,
|
|
250
|
+
endColumn: 17,
|
|
251
|
+
text: 'SELECT * FROM tb;'
|
|
252
|
+
}
|
|
253
|
+
]
|
|
214
254
|
*/
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
### Split SQL
|
|
218
|
-
|
|
219
|
-
When the SQL text is very big, you can think about to split it by `;` , and handle it by each line.
|
|
220
255
|
|
|
221
|
-
```javascript
|
|
222
|
-
import { splitSql } from 'dt-sql-parser';
|
|
223
|
-
|
|
224
|
-
const sql = `select id,name from user1;
|
|
225
|
-
select id,name from user2;`
|
|
226
|
-
const sqlList = splitSql(sql)
|
|
227
|
-
console.log(sqlList)
|
|
228
|
-
|
|
229
|
-
/*
|
|
230
|
-
["select id,name from user1;", "\nselect id,name from user2;"]
|
|
231
|
-
*/
|
|
232
256
|
```
|
|
233
257
|
|
|
234
|
-
###
|
|
235
|
-
|
|
236
|
-
|
|
258
|
+
### Code Completion
|
|
259
|
+
Obtaining autocomplete information at a specified position in SQL.
|
|
260
|
+
We can refer to the example of using `FlinkSQL`.
|
|
261
|
+
|
|
262
|
+
Invoke the `getSuggestionAtCaretPosition` method, pass the SQL content and the row and column numbers indicating the position where auto-completion is desired.
|
|
263
|
+
+ keyword candidates list
|
|
264
|
+
|
|
265
|
+
```javascript
|
|
266
|
+
import { FlinkSQL } from 'dt-sql-parser';
|
|
267
|
+
const parser = new FlinkSQL();
|
|
268
|
+
const sql = 'CREATE ';
|
|
269
|
+
const pos = { lineNumber: 1, column: 16 }; // the end position
|
|
270
|
+
const keywords = parser.getSuggestionAtCaretPosition(sql, pos)?.keywords;
|
|
271
|
+
console.log(keywords);
|
|
272
|
+
|
|
273
|
+
/*
|
|
274
|
+
[ 'CATALOG', 'FUNCTION', 'TEMPORARY', 'VIEW', 'DATABASE', 'TABLE' ]
|
|
275
|
+
*/
|
|
276
|
+
```
|
|
277
|
+
+ Obtaining information related to grammar completion
|
|
278
|
+
```javascript
|
|
279
|
+
const parser = new FlinkSQL();
|
|
280
|
+
const sql = 'SELECT * FROM tb';
|
|
281
|
+
const pos = { lineNumber: 1, column: 16 }; // after 'tb'
|
|
282
|
+
const syntaxSuggestions = parser.getSuggestionAtCaretPosition(sql, pos)?.syntax;
|
|
283
|
+
console.log(syntaxSuggestions);
|
|
284
|
+
|
|
285
|
+
/*
|
|
286
|
+
[
|
|
287
|
+
{
|
|
288
|
+
syntaxContextType: 'table',
|
|
289
|
+
wordRanges: [
|
|
290
|
+
{
|
|
291
|
+
text: 'tb',
|
|
292
|
+
startIndex: 14,
|
|
293
|
+
stopIndex: 15,
|
|
294
|
+
line: 1,
|
|
295
|
+
startColumn: 15,
|
|
296
|
+
stopColumn: 16
|
|
297
|
+
}
|
|
298
|
+
]
|
|
299
|
+
},
|
|
300
|
+
{
|
|
301
|
+
syntaxContextType: 'view',
|
|
302
|
+
wordRanges: [
|
|
303
|
+
{
|
|
304
|
+
text: 'tb',
|
|
305
|
+
startIndex: 14,
|
|
306
|
+
stopIndex: 15,
|
|
307
|
+
line: 1,
|
|
308
|
+
startColumn: 15,
|
|
309
|
+
stopColumn: 16
|
|
310
|
+
}
|
|
311
|
+
]
|
|
312
|
+
}
|
|
313
|
+
]
|
|
314
|
+
*/
|
|
315
|
+
```
|
|
316
|
+
The grammar-related autocomplete information returns an array, where each item represents what grammar can be filled in at that position. For example, the output in the above example represents that the position can be filled with either a **table name** or **a view name**. In this case, `syntaxContextType` represents the type of grammar that can be completed, and `wordRanges` represents the content that has already been filled.
|
|
237
317
|
|
|
238
|
-
|
|
318
|
+
### Other API
|
|
239
319
|
|
|
240
|
-
|
|
320
|
+
- `createLexer` Create an instance of Antlr4 Lexer and return it;
|
|
321
|
+
- `createParser` Create an instance of Antlr4 parser and return it;
|
|
322
|
+
- `parse` Parses the input SQL and returns the parse tree;
|
|
241
323
|
|
|
242
|
-
|
|
243
|
-
- Code formatting
|
|
324
|
+
<br/>
|
|
244
325
|
|
|
245
326
|
## License
|
|
246
327
|
|
package/dist/index.d.ts
CHANGED
|
@@ -4,16 +4,16 @@ export * from './lib/flinksql/FlinkSqlParserListener';
|
|
|
4
4
|
export * from './lib/flinksql/FlinkSqlParserVisitor';
|
|
5
5
|
export * from './lib/generic/SqlParserVisitor';
|
|
6
6
|
export * from './lib/generic/SqlParserListener';
|
|
7
|
-
export * from './lib/hive/
|
|
8
|
-
export * from './lib/hive/
|
|
7
|
+
export * from './lib/hive/HiveSqlParserListener';
|
|
8
|
+
export * from './lib/hive/HiveSqlParserVisitor';
|
|
9
9
|
export * from './lib/plsql/PlSqlParserListener';
|
|
10
10
|
export * from './lib/plsql/PlSqlParserVisitor';
|
|
11
|
-
export * from './lib/spark/
|
|
12
|
-
export * from './lib/spark/
|
|
11
|
+
export * from './lib/spark/SparkSqlParserVisitor';
|
|
12
|
+
export * from './lib/spark/SparkSqlParserListener';
|
|
13
13
|
export * from './lib/pgsql/PostgreSQLParserListener';
|
|
14
14
|
export * from './lib/pgsql/PostgreSQLParserVisitor';
|
|
15
15
|
export * from './lib/trinosql/TrinoSqlListener';
|
|
16
16
|
export * from './lib/trinosql/TrinoSqlVisitor';
|
|
17
17
|
export { SyntaxContextType } from './parser/common/basic-parser-types';
|
|
18
18
|
export type * from './parser/common/basic-parser-types';
|
|
19
|
-
export type { SyntaxError,
|
|
19
|
+
export type { SyntaxError, ParseError, ErrorHandler } from './parser/common/parseErrorListener';
|
package/dist/index.js
CHANGED
|
@@ -4,12 +4,12 @@ export * from './lib/flinksql/FlinkSqlParserListener';
|
|
|
4
4
|
export * from './lib/flinksql/FlinkSqlParserVisitor';
|
|
5
5
|
export * from './lib/generic/SqlParserVisitor';
|
|
6
6
|
export * from './lib/generic/SqlParserListener';
|
|
7
|
-
export * from './lib/hive/
|
|
8
|
-
export * from './lib/hive/
|
|
7
|
+
export * from './lib/hive/HiveSqlParserListener';
|
|
8
|
+
export * from './lib/hive/HiveSqlParserVisitor';
|
|
9
9
|
export * from './lib/plsql/PlSqlParserListener';
|
|
10
10
|
export * from './lib/plsql/PlSqlParserVisitor';
|
|
11
|
-
export * from './lib/spark/
|
|
12
|
-
export * from './lib/spark/
|
|
11
|
+
export * from './lib/spark/SparkSqlParserVisitor';
|
|
12
|
+
export * from './lib/spark/SparkSqlParserListener';
|
|
13
13
|
export * from './lib/pgsql/PostgreSQLParserListener';
|
|
14
14
|
export * from './lib/pgsql/PostgreSQLParserVisitor';
|
|
15
15
|
export * from './lib/trinosql/TrinoSqlListener';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,uCAAuC,CAAC;AACtD,cAAc,sCAAsC,CAAC;AACrD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,iCAAiC,CAAC;AAChD,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,uCAAuC,CAAC;AACtD,cAAc,sCAAsC,CAAC;AACrD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,iCAAiC,CAAC;AAChD,cAAc,kCAAkC,CAAC;AACjD,cAAc,iCAAiC,CAAC;AAChD,cAAc,iCAAiC,CAAC;AAChD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC;AAClD,cAAc,oCAAoC,CAAC;AACnD,cAAc,sCAAsC,CAAC;AACrD,cAAc,qCAAqC,CAAC;AACpD,cAAc,iCAAiC,CAAC;AAChD,cAAc,gCAAgC,CAAC;AAC/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAC"}
|