Skip to content

Potential performance issue with SQL parsing in jdbc-v2 #2585

Open

Description

@jploski

Reporting against v.0.9.1. We are using ClickHouse JDBC via Hibernate - our code is calling createNativeQuery a lot - which means creating (and throwing away) a great number of PreparedStatements. As a result we're seeing in close to 6% of CPU time being spent in the driver's statement parsing with production loads (as evaluated with asprof), more specifically in the method org.antlr.v4.runtime.atn.ParserATNSimulator.closureCheckingStopState.

While I'm aware that this is partly a problem of our usage pattern (PreparedStatements should be cached, which is what we're going to address next at our end), we did not have this issue with the old version (0.2.6) of the driver. In short, the SQL parsing has become slow(er).

I'm writing this to alert you in case no performance benchmarks were performed for that area of code.

I'm also wondering why ANTLR is at all necessary in the driver - I'd assume the actual 'heavyweight' parsing to detect syntax errors and the like is done on the server anyhow, so why have such overheads in the client? But this is likely a deeper design issue not fixable by profiling...

Activity

  1. added
    jdbc
    on Sep 16, 2025
  2. chernser commented on Sep 16, 2025

    @chernser
    Contributor

    Good day, @jploski

    Thank you for your feedback! We will look into the issue.
    There are cases when we have to parse SQL as if it is done by server. This is how JDBC is designed.

    • We need to know that parameter is in place of a value not somewhere else in SQL
    • We need to know if parameter is within function arguments and we have to use string representation instead of binary
    • We need to know if result set expected from query (it is not as simple as checking if query has SELECT)

    In other words we have to parse SQL to get some information. Doing it with custom code became quickly unmaintainable and buggy. Parser like JavaCC, Antlr4 helps to keep parsing logic maintainable and understandable by community, it doesn't fall apart once a new condition should be added. Fixing bugs in parser much easier than finding bugs in tangle-hairy code.

    As alternative we may implement a lightweight SQL parser in the future.

  3. added this to the 0.12.0 milestone on Jul 22, 2026
  4. removed this from the 0.12.0 milestone on Nov 10, 2026
  5. polyglotAI-bot commented on Nov 28, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Close — already fixed
    Fixed by #2579, first released in v0.9.3: jdbc-v2 now parses with the JavaCC parser by default, not with ANTLR4. On main, one prepareStatement costs about 1/20 of the time of 0.9.1.

    • Category: Performance
    • Classification: impact: Performance · area: Queries (jdbc-v2 SQL parser)
    • Status on main: Fixed on main — one prepareStatement takes about 80 µs and 61 KB on main, and 1.6 ms and 3.7 MB on 0.9.1. With jdbc_sql_parser=ANTLR4, main still takes 2.1 ms and 4.7 MB.
    • Breaking change: No.
    • Duplicates / related: Related: [jdbc-v2] LightParser Implementation #2797 — the lightweight parser that the maintainer comment names. Related: [performance][jdbc] Reduce memory allocation in PreparedStatement #3333 — allocations of prepareStatement and the setters on main.

    Analysis: prepareStatement parses the SQL on each call, and nothing caches the result (ConnectionImpl.java:458). 0.9.1 parses with ANTLR4 only; since #2579 the default of jdbc_sql_parser is JAVACC (DriverProperties.java:84), and ANTLR4 is an option.

    Reproduction: complete program and output
    import java.lang.management.ManagementFactory;
    import java.sql.Connection;
    import java.sql.DriverManager;
    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 final com.sun.management.ThreadMXBean MX =
                (com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean();
        // a native query as Hibernate generates it
        static final String SQL = "select o.id as id1_0_, o.customer_id as customer2_0_, o.status as status3_0_, "
                + "o.total as total4_0_, o.created_at as created5_0_ from orders o "
                + "where o.customer_id = ? and o.status in (?, ?, ?) and o.created_at >= ? "
                + "order by o.created_at desc limit ?";
        static final int N = 20_000;
    
        public static void main(String[] args) throws Exception {
            // no arguments: the default parser, then each parser by name (jdbc_sql_parser)
            String[] parsers = args.length > 0 ? args : new String[] {"default", "ANTLR4", "JAVACC"};
            for (String parser : parsers) {
                Properties props = new Properties();
                props.setProperty("user", "default");
                props.setProperty("password", PASSWORD);
                if (!parser.equals("default")) props.setProperty("jdbc_sql_parser", parser);
                String jdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default";
                try (Connection conn = DriverManager.getConnection(jdbcUrl, props)) {
                    for (int round = 0; round < 2; round++) { // round 0 is the warm-up (JIT)
                        long bytes = MX.getThreadAllocatedBytes(Thread.currentThread().getId());
                        long start = System.nanoTime();
                        for (int i = 0; i < N; i++) conn.prepareStatement(SQL).close();
                        long ns = System.nanoTime() - start;
                        bytes = MX.getThreadAllocatedBytes(Thread.currentThread().getId()) - bytes;
                        if (round == 1) {
                            System.out.printf("prepareStatement, parser %-8s %8.1f us/call %8d B/call%n",
                                    parser, ns / 1000.0 / N, bytes / N);
                        }
                    }
                }
            }
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.4.11:

    prepareStatement, parser default      79.8 us/call    61408 B/call
    prepareStatement, parser ANTLR4     2102.4 us/call  4676488 B/call
    prepareStatement, parser JAVACC       74.2 us/call    61408 B/call
    

    Output on release 0.9.1 (com.clickhouse:clickhouse-jdbc:0.9.1, argument default), ClickHouse 26.11.4.11:

    prepareStatement, parser default    1646.9 us/call  3672552 B/call
    

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

  6. added
    queries
    sql-parser
    on Nov 28, 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-parserv2-feedback

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions