151

Is there an open-source Java library for parsing SQL statements?

If possible, it should be customizable or flexible enough to also be able to parse (or at least ignore) vendor-specific syntax (such as Oracle tablespace definitions or MySQL's LIMIT clause).

If not, strict adherence to the SQL standard is also fine.

Update: I need this for two things:

  • providing an SQL interface to a non-SQL database (mapping to internal API calls)
  • rewriting SQL before it goes to the actual database (e.g. Oracle)
0

7 Answers 7

54

ANTLR3 has an ANSI SQL grammar available. You can use that to create your own parser.

ANTLR4 has a SQL grammar.

Sign up to request clarification or add additional context in comments.

7 Comments

Why use ANTLR when you can implement your own parser generator?
Why generate your own parser generator when you can use ANTLR?
Any links to how one can use Antlr SQL Grammar to parse these queries? I looked at the grammar of some PL/SQL Parsers as well as Lexers and Parsers but was unable to fathom how to use one. Would appreciate any links.
You feed the grammar to ANTLR, which spits out the lexer/parser classes that you'll then compile and run. The best source I can think of is the ANTLR reference: amazon.com/…
We're up to ANTLR 4 now. Perhaps the old grammars don't run on the new version.
|
41
  • JSqlParser
  • Trino's parser is written using ANTLR4 and has its own immutable AST classes that are built from the parser. The AST has a visitor and pretty printer.

2 Comments

I have a question for Presto, If I have Statement statement = SQL_PARSER.createStatement(query); How can I get the Query body, i.e. the Select, From, Where, etc values?
Statement is a base class. A SELECT statement will be of type Query. It contains a QueryBody that has the subclass QuerySpecification. The structure is more complex than you might expect in order to support UNION, TABLE, VALUES, set operations, etc. You can create a visitor by extending AstVisitor or DefaultTraversalVisitor. Look at SqlFormatter for an example of how to walk the tree.
9

Parser

If you need a parser there should be a parser in the code base of Apache Derby.

Dealing with vendor-specific SQL

You may want to look at the .native() method on the jdbc Connection object which you can pass it vendor neutral queries that will get postprocessed into vendor specific queries.

3 Comments

That native() method looks interesting. Are there any examples as to how it can be used? What kind of conversions are possible there?
@Thilo e.g. SQL Server 2014, nativeSQL Method (SQLServerConnection): "This method is not currently supported by the Microsoft JDBC Driver for SQL Server."
@Thilo OJDBC's OracleConnectionWrapper seems like supporting it.
7

General SQL Parser for Java is not open source, but is exactly what you are looking after.

2 Comments

The best sql parser for Java +1
surprised no one mention github.com/porcelli/plsql-parser.git , this is the most comprehensive sql parser I've seen
6

Try

  • Zql (Original Sourceforce)
  • Zql (Github)
  • Zql (Maven Central)

1 Comment

zql is good for basic queries but when you try to parse a query that contains join statement, it blows up. so i don't suggest it
3

Hibernate uses ANTLR for sql and hql parsing.

JSqlParser is also a good option.although it has some bugs(or some features not implemented) while parsing oracle pl/sql. see its forum for detail.

so if you're parsing oracle pl/sql, ANTLR is recommended.

1 Comment

btw, anltr is under BSD.
2

What you want to do with the parsed SQL? I can recommend a few Java implementation of Lex/Yacc (BYACC/J, Java Cup) that you can use an existing SQL grammar with.

If you want to actually do something with the resulting parsed grammar, I might suggest looking at Derby, an open source SQL database written in Java.

4 Comments

Is the SQL parser used in Derby available as an independent JAR ?
I don't know. I've never looked at the source for Derby.
I now know the answer to this question: No, the SQL parser for Derby isn't a separate project. You will need pull it apart to use it for something other than Derby.
I came back again after 10 years :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.