question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ParseException: Cannot parse statement: Encountered unexpected token: "exists" "EXISTS"

See original GitHub issue

My code:

	@Test
	public void testJSQParser()
	{
		String sql = "alter table if exists public.table2 rename to table3;";
		Validation validation = new Validation(List.of(DatabaseType.POSTGRESQL), sql);
		List<ValidationError> errors = validation.validate();
		Assert.assertTrue(errors.toString(), errors.isEmpty());
	}

Result:

java.lang.AssertionError: [ValidationError [
statement=alter table if exists public.table2 rename to table3;
capability=parsing
errors=[ParseException: Cannot parse statement: Encountered unexpected token: "exists" "EXISTS"
    at line 1, column 16.

Was expecting one of:

    "ADD"
    "ALGORITHM"
    "ALTER"
    "CHANGE"
    "COMMENT"
    "DROP"
    "MODIFY"
    "RENAME"
]
]]

This statement is a valid PostgreSQL syntax.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:24 (23 by maintainers)

github_iconTop GitHub Comments

1reaction
manticore-projectscommented, Sep 2, 2021

based on the unique grammar of each DB.

The idea of JSQLParser is to manage all possible SQL syntax of various RDBMS in one Grammar definition.

Unfortunately I see that this kind of use of this library is not common

It is not uncommon, I myself would also have taken a slightly different approach, e. g. defining a) a set of RDBMS which shall be supported and then b) find a way to flag each production for compatibility with a specific RDBMS. But that is only my gut feeling, the chosen approach is great for a lot of other objectives.

and that is why the code responsible for the validation functionality was not maintained.

Why not volunteer and just add the Validator Rules for Oracle and Postgresql? Or set a bounty for other developers? The frame work is there already, all it needs is some fine-tuning now.

Should I look for other solutions?

It is always good to know about options, but I myself came quite late for the party and stayed simply because I did not find anything even remotely as good as JSQLParser. Although I found a few short comings it was not too difficult to dive into the code and fill in what I was interested in myself.

Why not give it a try starting with the RENAME oldName TO newname statement and amend the Validator so it will accept Oracle and reject Postgresql.

0reactions
gitmottecommented, Sep 30, 2021

It’s about the definition of Feature’s and FeatureSetValidation (Version, DatabaseType). Only new features need an implementation within the Validator-classes. A new RDBMS to be validated only needs the definition of a new FeatureSet.

public enum DatabaseType implements FeatureSetValidation, Version {

    ANSI_SQL("ANSI SQL", SQLVersion.values()),
    // DBMS
    ORACLE(OracleVersion.values()),
    MYSQL(MySqlVersion.values()),
    SQLSERVER(SqlServerVersion.values()),
    MARIADB(MariaDbVersion.values()),
    POSTGRESQL(PostgresqlVersion.values()),
    H2(H2Version.values());

Personally I like the ANSI_SQL type the most, because it helps to write portable SQL, although this is not supported by all databases either.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ParseException: Cannot parse statement: Encountered unexpected ...
My code: @Test public void testJSQParser() { String sql = "alter table if exists public.table2 rename to table3;"; Validation validation = new ...
Read more >
net.sf.jsqlparser.parser.ParseException - java - Stack Overflow
This seemed to be a dependency issue for me. This is the query I ran with the older version of the jsqlparser library:...
Read more >
QMF V12.2 - Messages - IBM
Cannot parse data (base64 encoding rules violation); DAL01017 ... Parse exception occurred. ... Unexpected token: {0} is expected but {1} was found.
Read more >
Troubleshoot 'Uncaught SyntaxError: Unexpected token u in ...
The 'Uncaught SyntaxError: Unexpected token u in JSON at position 0' error is caused when the client has been asked to execute JSON.parse() ......
Read more >
How to fix Unexpected Token in JSON error (for ... - YouTube
Not a programmer? Read this! ** - If you get this error in an app you didn't make, and/or you're not a web...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found