4.1.0 • Published 11 days ago

bq-dt-sql-parser v4.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 days ago

dt-sql-parser

English | 简体中文

dt-sql-parser is a SQL Parser project built with ANTLR4, and it's mainly for the BigData field. The ANTLR4 generated the basic Parser, Visitor, and Listener, so it's easy to complete the Lexer, Parser, traverse the AST, and so on features.

Additionally, it provides advanced features such as SQL Validation, Code Completion, and Collecting Table and Columns in SQL.

Supported SQL Dialects

  • MySQL
  • Flink
  • Spark (including Recursive Queries)
  • Hive
  • PostgreSQL
  • Trino
  • Impala

!TIP This project is the default for the TypeScript target, but you can also try to compile it to other languages if needed.

New in this Version

  • Recursive Queries Support for SparkSQL: We have updated SparkSqlParser.ts to include support for recursive queries, allowing better parsing and analysis of complex queries in Spark.

Installation

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

# use yarn
yarn add dt-sql-parser

Usage

We recommend learning the fundamentals before continuing. The dt-sql-parser library provides SQL classes for different types of SQL.

import { MySQL, FlinkSQL, SparkSQL, HiveSQL, PostgreSQL, TrinoSQL, ImpalaSQL } from 'dt-sql-parser';

Before using syntax validation, code completion, and other features, it is necessary to instantiate the parser for the relevant SQL type. For instance, one can consider using MySQL as an example:

const mysql = new MySQL();

The following usage examples will utilize MySQL, but the parser for other SQL types works similarly.

Syntax Validation

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

const mysql = new MySQL();
const incorrectSql = 'selec id,name from user1;';
const errors = mysql.validate(incorrectSql);

console.log(errors);

Recursive Queries in SparkSQL

Now, dt-sql-parser supports Recursive Queries in SparkSQL, allowing better handling of queries using WITH RECURSIVE.

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

const spark = new SparkSQL();
const recursiveQuery = `
    WITH RECURSIVE employee_hierarchy AS (
        SELECT id, manager_id, name FROM employees WHERE manager_id IS NULL
        UNION ALL
        SELECT e.id, e.manager_id, e.name FROM employees e
        INNER JOIN employee_hierarchy eh ON e.manager_id = eh.id
    )
    SELECT * FROM employee_hierarchy;
`;

const errors = spark.validate(recursiveQuery);
console.log(errors.length === 0 ? "Valid SQL" : errors);

For more examples and features, check the documentation.