All Projects → DTStack → dt-sql-parser

DTStack / dt-sql-parser

Licence: other
SQL Parsers for BigData, built with antlr4.

Programming Languages

javascript
184084 projects - #8 most used programming language
ANTLR
299 projects
typescript
32286 projects

Projects that are alternatives of or similar to dt-sql-parser

Real-time-Data-Warehouse
Real-time Data Warehouse with Apache Flink & Apache Kafka & Apache Hudi
Stars: ✭ 52 (-61.48%)
Mutual labels:  spark-sql, flink-sql
Spark
.NET for Apache® Spark™ makes Apache Spark™ easily accessible to .NET developers.
Stars: ✭ 1,721 (+1174.81%)
Mutual labels:  bigdata, spark-sql
SparkProgrammingInScala
Apache Spark Course Material
Stars: ✭ 57 (-57.78%)
Mutual labels:  bigdata, spark-sql
Ale
Check syntax in Vim asynchronously and fix files, with Language Server Protocol (LSP) support
Stars: ✭ 11,380 (+8329.63%)
Mutual labels:  autocomplete, syntax-checker
big data
A collection of tutorials on Hadoop, MapReduce, Spark, Docker
Stars: ✭ 34 (-74.81%)
Mutual labels:  bigdata, spark-sql
bigdatatutorial
bigdatatutorial
Stars: ✭ 34 (-74.81%)
Mutual labels:  bigdata, spark-sql
awesome-coder-resources
编程路上加油站!------【持续更新中...欢迎star,欢迎常回来看看......】【内容:编程/学习/阅读资源,开源项目,面试题,网站,书,博客,教程等等】
Stars: ✭ 54 (-60%)
Mutual labels:  bigdata
flycheck-joker
Clojure syntax checker (via Joker) for flycheck
Stars: ✭ 55 (-59.26%)
Mutual labels:  syntax-checker
spark-twitter-sentiment-analysis
Sentiment Analysis of a Twitter Topic with Spark Structured Streaming
Stars: ✭ 55 (-59.26%)
Mutual labels:  spark-sql
indicium
🔎 A simple in-memory search for collections and key-value stores.
Stars: ✭ 41 (-69.63%)
Mutual labels:  autocomplete
greycat
GreyCat - Data Analytics, Temporal data, What-if, Live machine learning
Stars: ✭ 104 (-22.96%)
Mutual labels:  bigdata
TheVimIDE
Modern Vim IDE with support for C/C++, Java, Python, Lua, PHP, JavaScript, Ruby and much more ...
Stars: ✭ 33 (-75.56%)
Mutual labels:  syntax-checker
revodropdown
Minimalistic dropdown and auto-complete component with filtering and keyboard support
Stars: ✭ 20 (-85.19%)
Mutual labels:  autocomplete
TiBigData
TiDB connectors for Flink/Hive/Presto
Stars: ✭ 192 (+42.22%)
Mutual labels:  bigdata
flink-connector-kudu
基于Apache-bahir-kudu-connector的flink-connector-kudu,支持Flink1.11.x DynamicTableSource/Sink,支持Range分区等
Stars: ✭ 40 (-70.37%)
Mutual labels:  flink-sql
HiveRunner
An Open Source unit test framework for Hive queries based on JUnit 4 and 5
Stars: ✭ 244 (+80.74%)
Mutual labels:  hive-sql
2019 egu workshop jupyter notebooks
Short course on interactive analysis of Big Earth Data with Jupyter Notebooks
Stars: ✭ 29 (-78.52%)
Mutual labels:  bigdata
Autocomplete-System
Restful Autocomplete service with Neo4j graph backend. Returns top suggestions.
Stars: ✭ 36 (-73.33%)
Mutual labels:  autocomplete
platform
A collection of minimalistic, easy-to-use and fully customizable Angular components, directives and services
Stars: ✭ 17 (-87.41%)
Mutual labels:  autocomplete
Tweet-Analysis-With-Kafka-and-Spark
A real time analytics dashboard to analyze the trending hashtags and @ mentions at any location using kafka and spark streaming.
Stars: ✭ 18 (-86.67%)
Mutual labels:  spark-sql

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

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
        text: "SELECT"
    },
    ...
]
*/

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()
        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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].