Skip to content

jdbc-v2: ANTLR4 parser backends report a truncated VALUES list (and miscount value groups) for a value expression the grammar cannot parse #3019

Description

@polyglotAI-bot

Describe the bug

With jdbc_sql_parser=ANTLR4 (or ANTLR4_PARAMS_PARSER), an INSERT ... VALUES (...) whose values list contains a value expression the bundled ANTLR grammar cannot parse gets a truncated values-list template, and PreparedStatement#executeBatch() then sends a syntactically broken statement to the server.

Two properties are read from the parse tree in SqlParserFacade and both come out wrong for such a statement:

  • assignValuesListStartPosition / assignValuesListStopPosition (enterAssignmentValuesList, SqlParserFacade.java:322-325): the stop is reported at the closing parenthesis of a nested function call instead of the end of the value group;
  • assignValuesGroups (enterDataClauseValues): a single value group is counted twice, and — worse — a two-group values list is counted as one.

PreparedStatementImpl slices the original SQL with those positions when assignValuesGroups == 1, so the batch template loses its closing parenthesis.

Steps to reproduce

hex(x'AB') is valid ClickHouse SQL (SELECT hex(x'AB')AB) but the driver's ANTLR grammar has no hex-string literal, so the parse falls into error recovery:

Properties props = new Properties();
props.setProperty("jdbc_sql_parser", "ANTLR4");
try (Connection conn = DriverManager.getConnection(url, props)) {
    try (Statement s = conn.createStatement()) {
        s.execute("CREATE TABLE t (v1 Int32, v2 String) Engine MergeTree ORDER BY ()");
    }
    try (PreparedStatement ps = conn.prepareStatement("INSERT INTO t (v1, v2) VALUES (?, hex(x'AB'))")) {
        ps.setInt(1, 1);
        ps.addBatch();
        ps.setInt(1, 2);
        ps.addBatch();
        ps.executeBatch();
    }
}

Expected behaviour

Both rows are inserted (this is what the default JAVACC parser does).

Actual behaviour

Code: 62. DB::Exception: Cannot parse expression of type String here: hex(x'AB'),(2, hex(x'AB'):
While executing ValuesBlockInputFormat. (SYNTAX_ERROR)

The values-list template was cut one character short ((?, hex(x'AB') instead of (?, hex(x'AB'))).

Same shape with a JDBC escape sequence, i.e. the ANTLR-side counterpart of #3017:

INSERT INTO t (a, b) VALUES (?, toDate({d '2024-01-01'}))   -- stop reported at the inner ')'
INSERT INTO t (a, b) VALUES (?, hex(x'AB')), (?, hex(x'CD'))  -- reported as a single value group

Configuration

  • Driver: jdbc-v2, main
  • Property: jdbc_sql_parser=ANTLR4 / ANTLR4_PARAMS_PARSER (the default JAVACC is not affected by this one)
  • Server: 26.5.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions