Skip to content

[jdbc-v2] ANTLR4 parser: keyword column names (CLIENT, SCHEMA, MODEL, ...) break INSERT parsing, executeBatch falls back to one INSERT per row #3457

Backlog

Description

@ricardo-costa-effy

Description

With the ANTLR4 and ANTLR4_PARAMS_PARSER SQL parser backends, an INSERT whose column list contains an unquoted column named after certain keywords (for example CLIENT, SCHEMA, MODEL, CONDITION, PAGE, CONFIG) fails to parse. The parse error is swallowed, the VALUES list is discarded (getAssignValuesListStartPosition() == -1), and PreparedStatement#executeBatch() silently falls back to one INSERT per row (PreparedStatementImpl#executeBatchImpl → executeUpdateImpl in a loop) instead of a single multi-row INSERT.

The rows are inserted correctly, so nothing fails visibly. The batch is just orders of magnitude slower, and on the server each row becomes its own part. We hit this in a streaming pipeline that flushes JDBC batches during checkpoints. Flushes of ~1,000 rows took minutes, which led to checkpoint timeouts.

The default JAVACC backend parses these statements correctly.

Cause

These tokens are defined in ClickHouseLexer.g4 but are missing from the keyword rule in ClickHouseParser.g4, so they cannot be used as unquoted identifiers. 64 tokens are affected (most were added for SYSTEM ... statements):

ALLOW, ARBITRARY, ASYNCHRONOUS, BLOCKING, CANCEL, CLIENT, COLLECTIONS, COMPILED, CONDITION, CONFIG, CUSTOM, DNS, EMBEDDED, FAILPOINT, FUZZER, GRPC, HIVE, HTTPS, INTROSPECTION, JDBC, JEMALLOC, KAFKA, LISTEN, LOAD, LOADING, MANAGEMENT, MARK, MMAP, MODEL, MONGO, MOVES, MYSQL, NATS, ODBC, PAGE, POLICIES, POSTGRES, POSTGRESQL, PROMETHEUS, PROXY, PULLING, QUEUE, QUEUES, QUOTAS, RABBITMQ, READINESS, REDIS, REDUCE, REPLICAS, REPLICATION, RESTART, SCHEMA, SECRETS, SECURE, SHARDS, SHUTDOWN, SOURCES, SQLITE, TCP, UNCOMPRESSED, UNLOAD, VIEWS, VIRTUAL, WAIT

None of them are in jdbc-v2/src/test/resources/reserved_keywords.txt. (NAN_SQL is also missing, but it is the nan literal, so it is left out.)

Steps to reproduce

// jdbc-v2 0.11.2, url ...?jdbc_sql_parser=ANTLR4
PreparedStatement ps = conn.prepareStatement("INSERT INTO t (id, CLIENT) VALUES (?, ?)");
for (int i = 0; i < 1000; i++) { ps.setInt(1, i); ps.setString(2, "c"); ps.addBatch(); }
ps.executeBatch(); // 1000 INSERT queries in system.query_log, not 1

At the parser level:

ParsedPreparedStatement p = SqlParserFacade.getParser("ANTLR4", config)
        .parsePreparedStatement("INSERT INTO t (id, CLIENT) VALUES (?, ?)");
p.isHasErrors();                       // true
p.getAssignValuesGroups();             // 0
p.getAssignValuesListStartPosition();  // -1

The same statement with id, b or with `CLIENT` parses with no errors and one values group.

Expected behaviour

Non-reserved keywords can be used as unquoted column names, and the batch is sent as one INSERT, as with the JAVACC backend.

Workaround

Quote the column (`CLIENT` or "CLIENT").

Environment

  • clickhouse-java / jdbc-v2: 0.11.2 (also reproduced on main)
  • Parser: ANTLR4 (also ANTLR4_PARAMS_PARSER)
  • Java 17

I have a fix (add the missing tokens to the keyword rule, plus a data-driven test across the three parser backends) and will open a PR referencing this issue.

Activity

  1. polyglotAI-bot commented on Nov 27, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — fix needed
    Review PR #3458: with its diff, every case below sends one INSERT, and the 2058 parser unit tests pass. It also fixes the names of #3451, but not its implicit aliases (FROM t schema, FROM t timeout): the PR does not change keywordForAlias.

    Analysis: Confirmed on main: the keyword rule (ClickHouseParser.g4:1225) does not list 64 lexer tokens (for example CLIENT, ClickHouseLexer.g4:54), so identifier (ClickHouseParser.g4:1800) rejects them. After the parse error, SqlParserFacade.java:264-266 drops the values list, so PreparedStatementImpl.java:99 does not select the multi-row path, and executeBatchImpl (:350-356) runs each row.

    Reproduction: complete program and output

    Changed from the issue's steps: added the table, the system.query_log count, and the cases for ANTLR4_PARAMS_PARSER, a quoted column, and JAVACC.

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class Repro {
        static final String URL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
        static final String PASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
    
        static Connection connect(String parser) throws Exception {
            Properties props = new Properties();
            props.setProperty("user", "default");
            props.setProperty("password", PASSWORD);
            props.setProperty("jdbc_sql_parser", parser);
            return DriverManager.getConnection("jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default", props);
        }
    
        static void run(String label, String parser, String column, String table) throws Exception {
            try (Connection conn = connect(parser); Statement st = conn.createStatement()) {
                st.execute("DROP TABLE IF EXISTS " + table);
                st.execute("CREATE TABLE " + table + " (id Int32, CLIENT String) ENGINE = MergeTree ORDER BY id");
                String start;
                try (ResultSet rs = st.executeQuery("SELECT toString(now64(6))")) { rs.next(); start = rs.getString(1); }
                try (PreparedStatement ps = conn.prepareStatement("INSERT INTO " + table + " (id, " + column + ") VALUES (?, ?)")) {
                    for (int i = 0; i < 1000; i++) { ps.setInt(1, i); ps.setString(2, "c"); ps.addBatch(); }
                    ps.executeBatch();
                }
                st.execute("SYSTEM FLUSH LOGS");
                long rows, inserts;
                try (ResultSet rs = st.executeQuery("SELECT count() FROM " + table)) { rs.next(); rows = rs.getLong(1); }
                try (ResultSet rs = st.executeQuery("SELECT count() FROM system.query_log WHERE type = 'QueryFinish'"
                        + " AND query_kind = 'Insert' AND has(tables, 'default." + table + "')"
                        + " AND event_time_microseconds >= toDateTime64('" + start + "', 6)")) { rs.next(); inserts = rs.getLong(1); }
                System.out.println(label + ": rows=" + rows + ", INSERT queries=" + inserts);
                st.execute("DROP TABLE " + table);
            } catch (Exception e) {
                System.out.println(label + ": " + e.getClass().getName() + ": " + e.getMessage());
            }
        }
    
        public static void main(String[] args) throws Exception {
            run("ANTLR4, CLIENT", "ANTLR4", "CLIENT", "triage_3457_a");
            run("ANTLR4_PARAMS_PARSER, CLIENT", "ANTLR4_PARAMS_PARSER", "CLIENT", "triage_3457_b");
            run("ANTLR4, `CLIENT`", "ANTLR4", "`CLIENT`", "triage_3457_c");
            run("JAVACC, CLIENT", "JAVACC", "CLIENT", "triage_3457_d");
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.2.4:

    ANTLR4, CLIENT: rows=1000, INSERT queries=1000
    ANTLR4_PARAMS_PARSER, CLIENT: rows=1000, INSERT queries=1000
    ANTLR4, `CLIENT`: rows=1000, INSERT queries=1
    JAVACC, CLIENT: rows=1000, INSERT queries=1
    

    Output with the diff of PR #3458:

    ANTLR4, CLIENT: rows=1000, INSERT queries=1
    ANTLR4_PARAMS_PARSER, CLIENT: rows=1000, INSERT queries=1
    ANTLR4, `CLIENT`: rows=1000, INSERT queries=1
    JAVACC, CLIENT: rows=1000, INSERT queries=1
    

    Automated triage by Polyglot AI. A maintainer makes the final decision.

  2. added
    jdbc
    queries
    sql-parser
    on Nov 27, 2026
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

    jdbcperformancequeriessql-parser

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions