You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Potential performance issue with SQL parsing in jdbc-v2 #2585
jdbcModule: the JDBC driver (jdbc-v2; with legacy: the v1 driver)performancequeriesArea: how the client builds and runs statements (parameters, quoting, batches)sql-parserDetail: the SQL parser of the JDBC driver (v1 and v2)v2-feedback
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...
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.
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.
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.
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
importjava.lang.management.ManagementFactory;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.util.Properties;
publicclassRepro {
staticfinalStringURL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
staticfinalStringPASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
staticfinalcom.sun.management.ThreadMXBeanMX =
(com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean();
// a native query as Hibernate generates itstaticfinalStringSQL = "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 ?";
staticfinalintN = 20_000;
publicstaticvoidmain(String[] args) throwsException {
// no arguments: the default parser, then each parser by name (jdbc_sql_parser)String[] parsers = args.length > 0 ? args : newString[] {"default", "ANTLR4", "JAVACC"};
for (Stringparser : parsers) {
Propertiesprops = newProperties();
props.setProperty("user", "default");
props.setProperty("password", PASSWORD);
if (!parser.equals("default")) props.setProperty("jdbc_sql_parser", parser);
StringjdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default";
try (Connectionconn = DriverManager.getConnection(jdbcUrl, props)) {
for (intround = 0; round < 2; round++) { // round 0 is the warm-up (JIT)longbytes = MX.getThreadAllocatedBytes(Thread.currentThread().getId());
longstart = System.nanoTime();
for (inti = 0; i < N; i++) conn.prepareStatement(SQL).close();
longns = 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:
jdbcModule: the JDBC driver (jdbc-v2; with legacy: the v1 driver)performancequeriesArea: how the client builds and runs statements (parameters, quoting, batches)sql-parserDetail: the SQL parser of the JDBC driver (v1 and v2)v2-feedback
Activity
chernser commentedon Sep 16, 2025
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.
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.
polyglotAI-bot commentedon Nov 28, 2026
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, oneprepareStatementcosts about 1/20 of the time of 0.9.1.main: Fixed on main — oneprepareStatementtakes about 80 µs and 61 KB onmain, and 1.6 ms and 3.7 MB on 0.9.1. Withjdbc_sql_parser=ANTLR4,mainstill takes 2.1 ms and 4.7 MB.prepareStatementand the setters onmain.Analysis:
prepareStatementparses the SQL on each call, and nothing caches the result (ConnectionImpl.java:458). 0.9.1 parses with ANTLR4 only; since #2579 the default ofjdbc_sql_parserisJAVACC(DriverProperties.java:84), and ANTLR4 is an option.Reproduction: complete program and output
Output on
main(8480ca001), ClickHouse 26.11.4.11:Output on release 0.9.1 (
com.clickhouse:clickhouse-jdbc:0.9.1, argumentdefault), ClickHouse 26.11.4.11:Automated triage by Polyglot AI. A maintainer makes the final decision.