Coder Social home page Coder Social logo

vizsql's Introduction

VizSQL

License Build Status Maven Central

VizSQL is a SQL parser & typer for scala

VizSQL provides support for parsing SQL statements into a scala AST. This AST can then be used to support many interesting transformations. We provide for example a static optimizer and an OLAP query rewriter. It also support typing the query to retrieve the resultset columns returned by a SQL statement.

Installation

SBT

libraryDependencies += "com.criteo" % "vizsql_2.11" % "{latestVersion}"

Maven

<dependency>
  <groupId>com.criteo</groupId>
  <artifactId>vizsql_2.11</artifactId>
  <version>{latestVersion}</version>
</dependency>

tl;dr

Here we use VizSQL to parse a SQL SELECT statement based on the SAKILA database, and to retrieve the column names and types of the returned resultset.

import com.criteo.vizatra.vizsql

val resultColumns =
  VizSQL.parseQuery(
    """SELECT country_id, max(last_update) as updated from City as x""",
    SAKILA
  )
  .fold(e => sys.error(s"SQL syntax error: $e"), identity)
  .columns
  .fold(e => sys.error(s"SQL error: $e"), identity)

assert(
  resultColumns == List(
    Column("country_id", INTEGER(nullable = false)),
    Column("updated", TIMESTAMP(nullable = true))
  )
)

Note that the query is not executed. It is just parsed, validated, analyzed and typed using the SAKILA database schema. The schema can be provided directly or extracted at runtime from a JDBC connection if needed. Here is for example the minimum SAKILA schema we need to provide so VizSQL is able to type the previous query:

val SAKILA = DB(schemas = List(
  Schema(
    "sakila",
    tables = List(
      Table(
        "City",
        columns = List(
          Column("city_id", INTEGER(nullable = false)),
          Column("city", STRING(nullable = true)),
          Column("country_id", INTEGER(nullable = false)),
          Column("last_update", TIMESTAMP(nullable = true))
        )
      )
    )
  )
))

Dialects

VizSQL support several dialects allowing to understand specific SQL syntax or functions for different databases. We started the core one based on the SQL99 standard. Then we enriched it to create specific dialects for Vertica and Hive. This is of course a work in progress (even for the SQL99 one).

In-browser VizSQL

Yes, VizSQL can run in browsers, thanks to Scala.js, the project can be compiled to JavaScript, so that your front-end code can also be equipped with VizSQL.

To compile to JavaScript:

sbt fullOptJS

then vizsql-opt.js is generated in the target folder, it's packed as a CommonJS (node.js) module

To use VizSQL:

const Database = require('vizsql').Database;
const db = Database().from({
    /* db definitions */
});

const parseResult = db.parse("SELECT * FROM table");

the parse result contains { error, select }:

  • error (object), null if no error is present
  {
    msg: 'err' // message of the error
    pos: 1 // position of the error
  }
  • select (object), null if there's an error
  {
    columns: [...], // columns in the query
    tables: [...]   // tables in the query
  }

License

This project is licensed under the Apache 2.0 license.

Copyright

Copyright © Criteo, 2016.

vizsql's People

Contributors

brugidou avatar guillaumebort avatar jedirandy avatar lordshinjo avatar

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.