Coder Social home page Coder Social logo

crate / cratedb-sqlparse Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 1.0 334 KB

Parsing utilities to validate and split SQL statements for CrateDB.

License: Other

Python 13.72% JavaScript 86.28%
antlr4 antlr4-grammar cratedb grammar-conversion grammar-converter sql sql-parser sql-parsing sqlparser cratedb-sql

cratedb-sqlparse's Introduction

cratedb-sqlparse

Antlr4 is a parser generator for reading, processing and executing text, there are several target languages (Java, Python, JavaScript, Dart...) available. CrateDB uses the Java target.

The repository holds libraries/packages created from some of those available languages, so far: Python and JavaScript. More might be added if needed in the future.

These libraries allow you to parse Crate's SQL dialect without sending it to a CrateDB instance.

Example:

from cratedb_sqlparse import sqlparse

query = """
    SELECT * FROM SYS.SHARDS;
    INSERT INTO doc.tbl VALUES (1);
"""
statements = sqlparse(query)

select_query = statements[0]

print(select_query.query)
# 'SELECT * FROM SYS.SHARDS'

Limitations

Listeners are not implemented, which means that you can only: Validate SQL syntax, split queries and get some Tokens metadata from the query, if you need some more information like what https://github.com/macbre/sql-metadata does ( e.g. get the columns of this query) open a new issue.

New features should preferably be implemented in all available targets.

Adding a new target

The target language has to be available in antlr4, see https://github.com/antlr/antlr4/blob/master/doc/targets.md.

Add the new target and paths to the build script, see setup_grammar.py.

There are several features that would need to be implemented, like case-insensitive input stream, native exceptions as error listener, dollar strings and any new one. See past commits to see how they were implemented in Python and Javascript, remember that CrateDB's SQLParser written in Java is the most complete and the default reference.

Building locally & using a different CrateDB version.

The generated parser is not uploaded to the repository since it's huge, to use the package locally or to build a different version use the build script.

Clone the project

git clone [email protected]:crate/cratedb-sqlparse.git

Install the dependencies

pip install antlr4-python3-runtime requests

Run the build script

python3 setup_grammar.py

At the end of the build script setup_grammar.py the target and the CrateDB version can be modified.

if __name__ == '__main__':
   version = '5.6.4'
   target = Antlr4Target.python
   download_cratedb_grammar(version)
   compile_grammar(target)
   patch_lexer(target)
   set_version(target, version)

cratedb-sqlparse's People

Contributors

surister avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

surister

cratedb-sqlparse's Issues

Add verbose options to both python and javascript target to include query and approximate position of the error.

From crate/crate#15826

Steps:

  • Implement a new ErrorListener that displays the query with the offending token marked both in javascript and python targets.
  • Modify the ErrorListener so it does not throw an exception but a dictionary, e.g.
{
   "error": "ParseError",
   "original_query": '...",
   "original_query_with_offending_token": "...",
   "offending_token": "...",
   "error_message": "...",
   "col": 1,
   "line": 1
}

We should be able to enable this by:

sqlparse('SELECT 1;', verbose=True)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.