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.
Files changed (93) hide show
  1. package/README.md +124 -43
  2. package/dist/index.d.ts +5 -5
  3. package/dist/index.js +4 -4
  4. package/dist/index.js.map +1 -1
  5. package/dist/lib/flinksql/FlinkSqlLexer.d.ts +202 -201
  6. package/dist/lib/flinksql/FlinkSqlLexer.js +2003 -1996
  7. package/dist/lib/flinksql/FlinkSqlLexer.js.map +1 -1
  8. package/dist/lib/flinksql/FlinkSqlParser.d.ts +328 -265
  9. package/dist/lib/flinksql/FlinkSqlParser.js +3712 -3076
  10. package/dist/lib/flinksql/FlinkSqlParser.js.map +1 -1
  11. package/dist/lib/flinksql/FlinkSqlParserListener.d.ts +44 -0
  12. package/dist/lib/flinksql/FlinkSqlParserVisitor.d.ts +28 -0
  13. package/dist/lib/hive/HiveSqlLexer.d.ts +365 -351
  14. package/dist/lib/hive/HiveSqlLexer.js +2494 -2457
  15. package/dist/lib/hive/HiveSqlLexer.js.map +1 -1
  16. package/dist/lib/hive/{HiveSql.d.ts → HiveSqlParser.d.ts} +2791 -2463
  17. package/dist/lib/hive/{HiveSql.js → HiveSqlParser.js} +18554 -16800
  18. package/dist/lib/hive/HiveSqlParser.js.map +1 -0
  19. package/dist/lib/hive/{HiveSqlListener.d.ts → HiveSqlParserListener.d.ts} +1759 -1517
  20. package/dist/lib/hive/HiveSqlParserListener.js +3 -0
  21. package/dist/lib/hive/HiveSqlParserListener.js.map +1 -0
  22. package/dist/lib/hive/{HiveSqlVisitor.d.ts → HiveSqlParserVisitor.d.ts} +1165 -1011
  23. package/dist/lib/hive/HiveSqlParserVisitor.js +3 -0
  24. package/dist/lib/hive/HiveSqlParserVisitor.js.map +1 -0
  25. package/dist/lib/pgsql/PostgreSQLLexerBase.js +0 -4
  26. package/dist/lib/pgsql/PostgreSQLLexerBase.js.map +1 -1
  27. package/dist/lib/pgsql/PostgreSQLParserBase.js +0 -6
  28. package/dist/lib/pgsql/PostgreSQLParserBase.js.map +1 -1
  29. package/dist/lib/spark/SparkSqlLexer.d.ts +398 -322
  30. package/dist/lib/spark/SparkSqlLexer.js +2302 -1738
  31. package/dist/lib/spark/SparkSqlLexer.js.map +1 -1
  32. package/dist/lib/spark/SparkSqlParser.d.ts +3319 -3314
  33. package/dist/lib/spark/SparkSqlParser.js +18718 -16132
  34. package/dist/lib/spark/SparkSqlParser.js.map +1 -1
  35. package/dist/lib/spark/SparkSqlParserListener.d.ts +2130 -0
  36. package/dist/lib/spark/SparkSqlParserListener.js +3 -0
  37. package/dist/lib/spark/SparkSqlParserListener.js.map +1 -0
  38. package/dist/lib/spark/SparkSqlParserVisitor.d.ts +1361 -0
  39. package/dist/lib/spark/SparkSqlParserVisitor.js +3 -0
  40. package/dist/lib/spark/SparkSqlParserVisitor.js.map +1 -0
  41. package/dist/parser/common/basic-parser-types.d.ts +16 -3
  42. package/dist/parser/common/basic-parser-types.js +12 -2
  43. package/dist/parser/common/basic-parser-types.js.map +1 -1
  44. package/dist/parser/common/basicParser.d.ts +27 -28
  45. package/dist/parser/common/basicParser.js +76 -60
  46. package/dist/parser/common/basicParser.js.map +1 -1
  47. package/dist/parser/common/parseErrorListener.d.ts +33 -0
  48. package/dist/parser/common/parseErrorListener.js +28 -0
  49. package/dist/parser/common/parseErrorListener.js.map +1 -0
  50. package/dist/parser/flinksql.d.ts +1 -1
  51. package/dist/parser/flinksql.js +32 -13
  52. package/dist/parser/flinksql.js.map +1 -1
  53. package/dist/parser/generic.js +1 -1
  54. package/dist/parser/generic.js.map +1 -1
  55. package/dist/parser/hive.d.ts +12 -5
  56. package/dist/parser/hive.js +91 -7
  57. package/dist/parser/hive.js.map +1 -1
  58. package/dist/parser/index.d.ts +1 -0
  59. package/dist/parser/index.js +1 -0
  60. package/dist/parser/index.js.map +1 -1
  61. package/dist/parser/pgsql.js +1 -1
  62. package/dist/parser/pgsql.js.map +1 -1
  63. package/dist/parser/plsql.js +1 -1
  64. package/dist/parser/plsql.js.map +1 -1
  65. package/dist/parser/spark.d.ts +10 -3
  66. package/dist/parser/spark.js +90 -7
  67. package/dist/parser/spark.js.map +1 -1
  68. package/dist/parser/trinosql.js +1 -1
  69. package/dist/parser/trinosql.js.map +1 -1
  70. package/dist/utils/findCaretTokenIndex.d.ts +2 -2
  71. package/dist/utils/findCaretTokenIndex.js +5 -4
  72. package/dist/utils/findCaretTokenIndex.js.map +1 -1
  73. package/dist/utils/index.d.ts +1 -1
  74. package/dist/utils/index.js +1 -5
  75. package/dist/utils/index.js.map +1 -1
  76. package/dist/utils/token.d.ts +2 -2
  77. package/dist/utils/token.js +2 -2
  78. package/dist/utils/token.js.map +1 -1
  79. package/package.json +16 -9
  80. package/dist/lib/hive/HiveSql.js.map +0 -1
  81. package/dist/lib/hive/HiveSqlListener.js +0 -3
  82. package/dist/lib/hive/HiveSqlListener.js.map +0 -1
  83. package/dist/lib/hive/HiveSqlVisitor.js +0 -3
  84. package/dist/lib/hive/HiveSqlVisitor.js.map +0 -1
  85. package/dist/lib/spark/SparkSqlListener.d.ts +0 -3516
  86. package/dist/lib/spark/SparkSqlListener.js +0 -3
  87. package/dist/lib/spark/SparkSqlListener.js.map +0 -1
  88. package/dist/lib/spark/SparkSqlVisitor.d.ts +0 -2201
  89. package/dist/lib/spark/SparkSqlVisitor.js +0 -3
  90. package/dist/lib/spark/SparkSqlVisitor.js.map +0 -1
  91. package/dist/parser/common/parserErrorListener.d.ts +0 -30
  92. package/dist/parser/common/parserErrorListener.js +0 -62
  93. 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
- Besides, it provides some helper methods, like **split** SQL, and filter the `--` and `/**/` types of comments in SQL.
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
- // use npm
56
+ # use npm
41
57
  npm i dt-sql-parser --save
42
58
 
43
- // use yarn
59
+ # use yarn
44
60
  yarn add dt-sql-parser
45
61
  ```
46
62
 
47
- ## Usage
63
+ <br/>
48
64
 
49
- ### Syntax Validation
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
- First, we need to import the **Parser** object from `dt-sql-parser`, the different language needs
52
- different Parser, so if you need to handle the **Flink SQL**, you can import the **FlinkSQL Parser**.
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 below is a **GenericSQL Parser** example:
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
- ### Clean
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 { cleanSql } from 'dt-sql-parser';
206
-
207
- const sql = `-- comment comment
208
- select id,name from user1; `
209
- const cleanedSql = cleanSql(sql)
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
- select id,name from user1;
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
- ### Other API
235
-
236
- - parserTreeToString(input: string)
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
- Parse the input and convert the AST to a `List-like` tree string.
318
+ ### Other API
239
319
 
240
- ## Roadmap
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
- - Auto-complete
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/HiveSqlListener';
8
- export * from './lib/hive/HiveSqlVisitor';
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/SparkSqlVisitor';
12
- export * from './lib/spark/SparkSqlListener';
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, ParserError } from './parser/common/parserErrorListener';
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/HiveSqlListener';
8
- export * from './lib/hive/HiveSqlVisitor';
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/SparkSqlVisitor';
12
- export * from './lib/spark/SparkSqlListener';
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,4BAA4B,CAAC;AAC3C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iCAAiC,CAAC;AAChD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sCAAsC,CAAC;AACrD,cAAc,qCAAqC,CAAC;AACpD,cAAc,iCAAiC,CAAC;AAChD,cAAc,gCAAgC,CAAC;AAC/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAA"}
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"}