Skip to content

[jdbc-v2] Antlr4 parser fails to parse mixed CTEs #2431

@Gerrit-K

Description

@Gerrit-K

Description

The parser fails to parse queries with CTEs that use both forms, the WITH <expression> AS <identifier> form and the WITH <identifier> AS <subquery expression> form (see syntax). See this example:

SqlParser parser = new SqlParser();
ParsedStatement stmt = parser.parsedStatement("with 1 as a, b as (select 1) select a from b");
assertFalse(stmt.isHasErrors());

The following queries can be parsed without issues:

with 1 as a, 2 as b select a, b; -- two CTEs of the first form
with a as (select 1), b as (select 2) select * from a, b; -- two CTEs of the second form

Steps to reproduce

  1. Run the java code above in a test
  2. Observe a test failure

Error Log or Exception StackTrace

When debugging this in the test, you can see that inside parsedStatement() the parseTree.children.get(0).exception is org.antlr.v4.runtime.NoViableAltException.

Expected Behaviour

Both forms of CTEs should be parseable if used in a single query.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions