ClickHouse / clickhouse-java

ClickHouse Java Clients & JDBC Driver
https://clickhouse.com
Apache License 2.0
1.45k stars 538 forks source link

Unexpected `BadSqlGrammarException` Instead of `TimeoutException` on Query Timeout #1972

Open koyr-exem opened 4 days ago

koyr-exem commented 4 days ago

Describe the bug

When using the ClickHouse JDBC driver with JdbcTemplate in a Spring application, a query timeout is set using JdbcTemplate.setQueryTimeout(10). However, when the query exceeds the configured timeout, a BadSqlGrammarException is thrown instead of the expected TimeoutException. This behavior is misleading as it suggests an issue with SQL syntax rather than a timeout.

Steps to reproduce

  1. Configure JdbcTemplate with a query timeout: jdbcTemplate.setQueryTimeout(10) (10 seconds).
  2. Execute a query that takes longer than the configured timeout.
  3. Observe the exception thrown when the query times out.

Expected behaviour

A TimeoutException or another appropriate exception should be thrown to clearly indicate that the query execution exceeded the configured timeout.

Code example

public executeQuery(String query) { 
        jdbcTemplate.query(query)
}

Error log

org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [SELECT * FROM large_table]; 
nested exception is java.sql.SQLException: Code: 159. DB::Exception: Timeout exceeded: elapsed 10.114165251 seconds, maximum: 10: While executing MergeTreeSelect(pool: ReadPoolInOrder, algorithm: InOrder). (TIMEOUT_EXCEEDED) (version 24.1.5.6 (official build))

    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:79)
    at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1541)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:393)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:465)

Configuration

Environment

ClickHouse server

chernser commented 3 days ago

Good day, @koyr-exem ! Thank you for bringing it up. We will try to look into it soon.