Skip to content

clickhouse-jdbc (v1): the JavaCC SQL parser has no heredoc token, so a ; inside $$...$$ truncates the statement #3039

Closed

Description

@polyglotAI-bot

Describe the bug

The legacy clickhouse-jdbc (v1) JavaCC grammar
(clickhouse-jdbc/src/main/javacc/ClickHouseSqlParser.jj) has no heredoc token, so the body of a
ClickHouse heredoc literal ($$body$$ / $tag$body$tag$) is lexed as ordinary SQL. Consequences:

  • a ; inside a heredoc body ends the statement: select $$a;b$$ parses to a single statement whose
    SQL is only select $$a, i.e. the rest of the query is silently dropped. Because
    ClickHouseConnectionImpl.prepareStatement builds the prepared query from parsedStmt.getSQL(),
    the driver then sends the truncated SQL to the server;
  • a body character that is not a valid token in that position (?, an operator, ...) aborts the
    parse, so the statement is classified as UNKNOWN with no table name — e.g.
    select $$a?b$$, 1 from tbl and insert into tbl values ($$a;b$$, 1).

This is the v1 counterpart of #3029 / #3031 (the jdbc-v2 JavaCC and ANTLR4 grammars) and is a
separate mechanism from #3035 (the v1 JdbcParameterizedQuery placeholder scan) — both layers have
to know about heredocs for the v1 driver to accept such a statement.

Steps to reproduce

Parser level (main @ 1a11756):

ClickHouseSqlStatement[] stmts = ClickHouseSqlParser.parse("select $$a;b$$", new ClickHouseConfig());
stmts.length;            // 1
stmts[0].getSQL();       // "select $$a" -- expected "select $$a;b$$"

ClickHouseSqlParser.parse("select $$a?b$$, 1 from tbl", new ClickHouseConfig())[0]
    .getStatementType(); // UNKNOWN -- expected SELECT

Driver level, with the v1 driver (clickhouse.jdbc.v1=true):

conn.prepareStatement("select $$a;b$$ as s").executeQuery();
// Code: 62. DB::Exception: Syntax error: failed at position 8 ($): $$a.

The server accepts all of these statements — a heredoc is one string literal to it:

:) SELECT $$a;b$$, $tag$ x $tag$, $$a(b,c)$$
a;b      x      a(b,c)

Expected behaviour

A heredoc is lexed as a single string literal, so its body cannot split a statement or change its
classification.

Environment

  • Client version: main @ 1a11756
  • Language version: Java 8+
  • ClickHouse server: 26.5.1.882 (server behaviour is not version-specific here)

Notes

clickhouse-jdbc (v1) is the legacy stack; close this if v1 grammar changes are out of scope.

Activity

  1. chernser commented on Aug 18, 2026

    @chernser
    Contributor

    wontfix in V1 - fix is risky and need more effort. will be reviewed in V2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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