Skip to content

Repository files navigation

Turn any SQL statement into a traversable tree of Java objects -- and back again.

An RDBMS-agnostic SQL parser for the JVM:
one grammar, twelve dialects, no native extensions.

JSqlParser

CICoverage StatusCodacy BadgeManticore BuildMaven CentralJavadocsGitHub StarsGitter

Website · Samples · Syntax · Change Log · Contributing


What it does

Give it SQL. Get an AST you can walk, rewrite, and print back out.

SELECT1FROM dual WHERE a = b
SQL Text
└─Statements: statement.select.PlainSelect
├─selectItems: statement.select.SelectItem
│ └─LongValue: 1
├─Table: dual
└─where: expression.operators.relational.EqualsTo
├─Column: a
└─Column: b
StringsqlStr = "select 1 from dual where a=b";
PlainSelectselect = (PlainSelect) CCJSqlParserUtil.parse(sqlStr);
SelectItemselectItem = select.getSelectItems().get(0);
Assertions.assertEquals(newLongValue(1), selectItem.getExpression());
Tabletable = (Table) select.getFromItem();
Assertions.assertEquals("dual", table.getName());
EqualsToequalsTo = (EqualsTo) select.getWhere();
Columna = (Column) equalsTo.getLeftExpression();
Columnb = (Column) equalsTo.getRightExpression();
Assertions.assertEquals("a", a.getColumnName());
Assertions.assertEquals("b", b.getColumnName());

The tree is traversable with the Visitor pattern, and the same object model works in reverse: build statements from Java with a fluent API and render them as SQL text.

Install

Use the stable Manticore builds. They are released continuously from the current development line and carry all of the performance and grammar work described below. The upstream com.github.jsqlparser release on Maven Central is considerably older.

<dependency>
<groupId>com.manticore-projects.jsqlformatter</groupId>
<artifactId>jsqlparser</artifactId>
<version>[5.3.218,)</version>
</dependency>
implementation("com.manticore-projects.jsqlformatter:jsqlparser:+")
Upstream release and snapshots
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>5.3</version>
</dependency>

Snapshot coordinates and repository setup are on the build dependencies page.

Performance

11× faster than 5.3, and the fastest parser on real-world SQL of any of the parsers tested, in any language — 19× ahead of sqlglot[c] on JSqlParser's own SELECT test suite.

SQL parser benchmark score
Benchmark (version) Mode Cnt Score Error Units
JSQLParserBenchmark.parseSQLStatements latest avgt 15 7.602 ± 0.135 ms/op
JSQLParserBenchmark.parseSQLStatements 5.3 avgt 15 84.687 ± 3.321 ms/op

Methodology and the full cross-parser comparison against SQLGlot, sqlglot[c] and polyglot-sql: jsqlparser-bench.

What it parses

JSqlParser targets the SQL standard plus all major RDBMS. One grammar covers all of them, and missing syntax gets added on demand — open an issue.

BigQuery · Snowflake · DuckDB · Redshift · Oracle · MS SQL Server · SybasePostgreSQL · MySQL · MariaDB · DB2 · H2 · HSQLDB · Derby · SQLite

Statements
QueriesSELECT · WITH … · Piped SQL
DMLINSERT · UPDATE · UPSERT · MERGE · DELETE · TRUNCATE TABLE
DDLCREATE … · ALTER … · DROP …
PostgreSQL RLSCREATE POLICY · ALTER TABLE … ENABLE/DISABLE/FORCE/NO FORCE ROW LEVEL SECURITY
Salesforce SOQLINCLUDES · EXCLUDES

Beyond statement shapes, the grammar handles nested sub-selects, bind parameters (?, :name), window and analytic functions, Oracle hints, and the T-SQL square-bracket versus array-literal ambiguity. The complete reference is on the syntax page.

Piped SQL

Support is progressing for Piped SQL, which writes queries in the order they actually execute rather than the order SQL historically demanded.

FROM Produce
|>WHERE
item !='bananas'AND category IN ('fruit', 'nut')
|> AGGREGATE COUNT(*) AS num_items, SUM(sales) AS total_sales
GROUP BY item
|>ORDER BY item DESC;

Background reading: the Google research paper, BigQuery pipe syntax and DuckDB FROM-first syntax.

Java version

JSqlParserRuntimeNotes
4.9JDK 8last JDK 8 compatible release
5.0 and laterJDK 11breaking changes to the AST Visitors, see the Migration Guide
5.1 and laterJDK 11building requires a JDK 17 toolchain (plugin requirement)
5.4 and laterJDK 11parser generated with JavaCC 8

Sister projects

Alternatives

The dual-licensed JOOQ ships a hand-written parser with broad RDBMS support, cross-dialect translation, SQL transformation, and a JDBC proxy mode. Worth a look if translation between dialects is your primary need rather than AST access.

Sponsor

A huge thank you to Starlake.ai, who simplify data ingestion, transformation and orchestration for faster delivery of high-quality data. Starlake has been instrumental in providing Piped SQL support and a large number of test cases for BigQuery, Redshift, Databricks and DuckDB. If JSqlParser is useful to you, visit Starlake.ai and give them a star.

Documentation

  1. Samples
  2. Build instructions and Maven artifact
  3. Contribution guide
  4. Change log
  5. Issues

License

Dual licensed under LGPL 2.1 or the Apache License, Version 2.0. Take your pick.

About

JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes. The generated hierarchy can be navigated using the Visitor Pattern

Topics

Resources

Stars

6.0k stars

Watchers

211 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages