Go to file
Hayden 1b02ff5d75
feat: FlinkSQL supports auto complete (#115)
* feat: add antlr4-c3 dependencies

* feat: distinguish table, catalog and database from uid

* feat: move semicolon from sqlStatements to sqlStatement

* chore: move antlr4ts-cli to devDependencies

* feat: improve basic parser and support suggestions of token and syntax

* feat: implement suggest method in sql parsers

* test: flink sql suggestion test cases

* feat: optimize ts defination of suggestion

* feat: add split listener and optimize performance of auto-completion

* test: supplementary flink suggestion unit tests
2023-06-09 11:22:53 +08:00
.github ci: branch that triggers ci is changed from master to main 2023-06-08 17:05:16 +08:00
build refactor: migrate antlr4 v4.12.0 to antlr4ts(4.9.0) (#106) 2023-05-30 14:44:03 +08:00
docs docs: add release example 2021-01-05 16:09:07 +08:00
src feat: FlinkSQL supports auto complete (#115) 2023-06-09 11:22:53 +08:00
test feat: FlinkSQL supports auto complete (#115) 2023-06-09 11:22:53 +08:00
.eslintrc.js build: disable the max-len rule 2021-09-10 17:50:58 +08:00
.gitignore fix: correct the grammar usage, especially in the parts targeting javascript (#109) 2023-05-24 15:07:53 +08:00
.gitlab-ci.yml ci: integrate with gitlab-ci 2020-12-17 19:03:25 +08:00
.npmignore build: ignore useless file 2023-01-06 10:19:26 +08:00
CHANGELOG.md chore(release): 4.0.0-beta.3.2 2023-01-09 11:16:17 +08:00
CONTRIBUTING.md feat: upgrade antlr4 to 4.12.0 (#88) 2023-05-04 10:13:05 +08:00
jest.config.js refactor: migrate antlr4 v4.12.0 to antlr4ts(4.9.0) (#106) 2023-05-30 14:44:03 +08:00
package.json feat: FlinkSQL supports auto complete (#115) 2023-06-09 11:22:53 +08:00
pnpm-lock.yaml feat: FlinkSQL supports auto complete (#115) 2023-06-09 11:22:53 +08:00
README-zh_CN.md docs(readme): fix documentation error (#110) 2023-05-24 17:01:17 +08:00
README.md docs(readme): fix documentation error (#110) 2023-05-24 17:01:17 +08:00
tsconfig.json refactor: migrate antlr4 v4.12.0 to antlr4ts(4.9.0) (#106) 2023-05-30 14:44:03 +08:00
yarn.lock feat: FlinkSQL supports auto complete (#115) 2023-06-09 11:22:53 +08:00

dt-sql-parser

NPM version NPM downloads Chat

English | 简体中文

dt-sql-parser is a SQL Parser project built with ANTLR4, and it's mainly for the BigData domain. The 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.

Besides, it provides some helper methods, like split SQL, and filter the -- and /**/ types of comments in SQL.

Supported SQL:

  • Generic SQL (MySQL)
  • Flink SQL
  • Spark SQL
  • Hive SQL
  • PL/SQL
  • PostgreSQL
  • Trino SQL

Tips: This project is the default for Javascript language, also you can try to compile it to other languages if you need.

Integrating SQL Parser with Monaco Editor

We have provided a monaco-sql-languages package, you can integrate with monaco-editor easily.

Installation

// use npm
npm i dt-sql-parser --save

// use yarn
yarn add dt-sql-parser

Usage

Syntax Validation

First, we need to import the Parser object from dt-sql-parser, the different language needs different Parser, so if you need to handle the Flink SQL, you can import the FlinkSQL Parser.

The below is a GenericSQL Parser example:

import { GenericSQL } from 'dt-sql-parser';

const parser = new GenericSQL();

const correctSql = 'select id,name from user1;';
const errors = parser.validate(correctSql);
console.log(errors); 

Output:

/*
[]
*/

Validate failed:

const incorrectSql = 'selec id,name from user1;'
const errors = parser.validate(incorrectSql);
console.log(errors); 

Output:

/*
[
    {
        endCol: 5,
        endLine: 1,
        startCol: 0,
        startLine: 1,
        message: "mismatched input 'SELEC' expecting {<EOF>, 'ALTER', 'ANALYZE', 'CALL', 'CHANGE', 'CHECK', 'CREATE', 'DELETE', 'DESC', 'DESCRIBE', 'DROP', 'EXPLAIN', 'GET', 'GRANT', 'INSERT', 'KILL', 'LOAD', 'LOCK', 'OPTIMIZE', 'PURGE', 'RELEASE', 'RENAME', 'REPLACE', 'RESIGNAL', 'REVOKE', 'SELECT', 'SET', 'SHOW', 'SIGNAL', 'UNLOCK', 'UPDATE', 'USE', 'BEGIN', 'BINLOG', 'CACHE', 'CHECKSUM', 'COMMIT', 'DEALLOCATE', 'DO', 'FLUSH', 'HANDLER', 'HELP', 'INSTALL', 'PREPARE', 'REPAIR', 'RESET', 'ROLLBACK', 'SAVEPOINT', 'START', 'STOP', 'TRUNCATE', 'UNINSTALL', 'XA', 'EXECUTE', 'SHUTDOWN', '--', '(', ';'}"
    }
]
*/

We instanced a Parser object, and use the validate method to check the SQL syntax, if failed returns an array object includes error message.

Tokenizer

Get all tokens by the Parser:

import { GenericSQL } from 'dt-sql-parser';

const parser = new GenericSQL()
const sql = 'select id,name,sex from user1;'
const tokens = parser.getAllTokens(sql)
console.log(tokens)
/*
[
    {
        channel: 0
        column: 0
        line: 1
        source: [SqlLexer, InputStream]
        start: 0
        stop: 5
        tokenIndex: -1
        type: 137
        _text: null
    },
    ...
]
*/

Visitor

Traverse the tree node by the Visitor:

import { GenericSQL, SqlParserVisitor } from 'dt-sql-parser';

const parser = new GenericSQL()
const sql = `select id,name from user1;`
// parseTree
const tree = parser.parse(sql)
class MyVisitor extends SqlParserVisitor {
    // overwrite visitTableName
    visitTableName(ctx) {
        let tableName = ctx.getText().toLowerCase()
        console.log('TableName', tableName)
    }
    // overwrite visitSelectElements
    visitSelectElements(ctx) {
        let selectElements = ctx.getText().toLowerCase()
        console.log('SelectElements', selectElements)
    }
}
const visitor = new MyVisitor()
visitor.visit(tree)

/*
SelectElements id,name
TableName user1
*/

Tips: The node's method name can be found in the Visitor file under the corresponding SQL directory

Listener

Access the specified node in the AST by the Listener

import { GenericSQL, SqlParserListener } from 'dt-sql-parser';

const parser = new GenericSQL();
const sql = 'select id,name from user1;'
// parseTree
const tree = parser.parse(sql)
class MyListener extends SqlParserListener {
    enterTableName(ctx) {
        let tableName = ctx.getText().toLowerCase()
        console.log('TableName', tableName)
    }
    enterSelectElements(ctx) {
        let selectElements = ctx.getText().toLowerCase()
        console.log('SelectElements', selectElements)
    }
}
const listenTableName = new MyListener();
parser.listen(listenTableName, tree);

/*
SelectElements id,name
TableName user1
*/

Tips: The node's method name can be found in the Listener file under the corresponding SQL directory

Clean

Clear the comments and spaces before and after

import { cleanSql } from 'dt-sql-parser';

const sql = `-- comment comment
select id,name from user1; `
const cleanedSql = cleanSql(sql)
console.log(cleanedSql)

/*
select id,name from user1;
*/

Split SQL

When the SQL text is very big, you can think about to split it by ; , and handle it by each line.

import { splitSql } from 'dt-sql-parser';

const sql = `select id,name from user1;
select id,name from user2;`
const sqlList = splitSql(sql)
console.log(sqlList)

/*
["select id,name from user1;", "\nselect id,name from user2;"]
*/

Other API

  • parserTreeToString(input: string)

Parse the input and convert the AST to a List-like tree string.

Roadmap

  • Auto-complete
  • Code formatting

License

MIT