Skip to content

jdbc-v2: Issue with parameter data type #2674

Open

Description

@jnd77

Description

When running a prepared statement with COALESCE and a date, the execution fails due to type.

Am not entirely sure whether it's an issue with the JDBC driver (which serializes the date as a String in PreparedStatementImpl#encodeObject - nothing wrong with that) or the server (which should deserialize it with the proper type, or COALESCE which should handle types better - see ClickHouse/ClickHouse#57292 , although most other databases handle this use case).

Steps to reproduce

See the code below.

Error Log or Exception StackTrace

Caused by: com.clickhouse.client.api.ServerException: Code: 386. DB::Exception: There is no supertype for types Date32, String because some of them are String/FixedString/Enum and some of them are not: In scope SELECT COALESCE(date, '2025-12-31') FROM test_local_date. (NO_COMMON_TYPE) (version 25.8.7.3 (official build)) 
	at com.clickhouse.client.api.internal.HttpAPIClientHelper.readError(HttpAPIClientHelper.java:403)

Expected Behaviour

The query should return without error.

Code Example

  @Test
  void required_testJdbcWithLocalDate() throws Exception {
    final String url = "jdbc:ch://localhost:" + SERVER.getFirstMappedPort();

    final Properties info = new Properties();
    info.put("user", SERVER.getClickhouseUser());
    info.put("password", SERVER.getClickhousePassword());

    try (final Connection conn = DriverManager.getConnection(url, info)) {
      conn.prepareStatement("CREATE TABLE test_local_date"
          + " (id Int64, date Nullable(Date32)) ENGINE = MergeTree() ORDER BY id").execute();
      conn.prepareStatement("INSERT INTO test_local_date VALUES (1, null)").execute();
      try (final PreparedStatement preparedStatement = conn.prepareStatement("SELECT COALESCE(date, ?) FROM test_local_date")) {
        preparedStatement.setObject(1, LocalDate.of(2025, 12, 31));
        preparedStatement.executeQuery();
      }
    }
  }

Configuration

Client Configuration

Environment

  • Cloud
  • Client version: clickhouse-jdbc 0.9.4
  • Language version:
  • OS:

ClickHouse Server

  • ClickHouse Server version: 25.8.7.3

Activity

  1. added
    jdbc
    on Dec 3, 2025
  2. chernser commented on Dec 3, 2025

    @chernser
    Contributor

    Good day, @jnd77 !

    Thank you for reporting!

    This is the problem in how to represent values like DateTime in text format so it is suitable for functions expecting string and for functions that expect certain type. You may use com.clickhouse.jdbc.PreparedStatementImpl#setObject(int, java.lang.Object, java.sql.SQLType) so driver will add CAST.
    I agree it is counter-intuitive to pass Date object and get string on server side. There is more simple example SELECT ? where it will become string and we might fail to return Date .
    We are working on this .

  3. added
    data-types
    on Dec 3, 2025
  4. polyglotAI-bot commented on Nov 28, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — maintainer decision
    A maintainer decides how jdbc-v2 sends the type of a ? value: the CAST of 0.9.1–0.9.6 broke other queries (#2701). The CAST workaround in the thread does not work since v0.9.7 (#2710).

    • Category: Bug
    • Classification: impact: none · area: Data types & formats (Date32, LocalDate parameters)
    • Status on main: Reproduced on main — the issue's error on ClickHouse 25.8; from 26.1 no error, but the result is Variant(Date32, String) with a String value.
    • Breaking change: Yes — a typed value changes the SQL and the result types of current queries (docs/features.md:128 documents the quoted text form); behind a new option it does not break.
    • Duplicates / related: Related: Invalid query is generated by setObject due to bad type cast #2701 — the old CAST broke parseDateTimeBestEffort(?).
    • Workaround: COALESCE(date, toDate32(?)) returns a Date32 on ClickHouse 25.8 and 26.11.

    Analysis: PreparedStatementImpl.java:774-775 writes a LocalDate as the text literal '2025-12-31', so the server gets COALESCE(Nullable(Date32), String). ClickHouse 25.8 has no common type for these; from 26.1, use_variant_as_common_type is on by default and gives a Variant. Since v0.9.7, setObject(int, Object, SQLType) (lines 285-289) only checks the type and adds no CAST.

    Reproduction: complete program and output

    Changed from the issue's steps: the test body in a main, with its own DROP TABLE; added a setObject(…, JDBCType.DATE) case, a toDate32(?) case, and the result type of each case.

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.JDBCType;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.time.LocalDate;
    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", "");
    
        interface Bind { void apply(PreparedStatement ps) throws Exception; }
    
        static void runCase(Connection conn, String name, String sql, Bind bind) {
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                bind.apply(ps);
                try (ResultSet rs = ps.executeQuery()) {
                    rs.next();
                    Object v = rs.getObject(1);
                    System.out.println(name + ": " + v + " (" + rs.getMetaData().getColumnTypeName(1) + ", "
                            + (v == null ? "null" : v.getClass().getSimpleName()) + ")");
                }
            } catch (Exception e) {
                String msg = String.valueOf(e.getMessage());
                System.out.println(name + ": " + e.getClass().getName() + ": " + msg.split("\n")[0]);
            }
        }
    
        public static void main(String[] args) throws Exception {
            Properties props = new Properties();
            props.setProperty("user", "default");
            props.setProperty("password", PASSWORD);
            String jdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default";
            try (Connection conn = DriverManager.getConnection(jdbcUrl, props)) {
                conn.prepareStatement("DROP TABLE IF EXISTS test_local_date").execute();
                conn.prepareStatement("CREATE TABLE test_local_date"
                        + " (id Int64, date Nullable(Date32)) ENGINE = MergeTree() ORDER BY id").execute();
                conn.prepareStatement("INSERT INTO test_local_date VALUES (1, null)").execute();
    
                LocalDate d = LocalDate.of(2025, 12, 31);
                runCase(conn, "setObject(LocalDate)", "SELECT COALESCE(date, ?) FROM test_local_date",
                        ps -> ps.setObject(1, d));
                runCase(conn, "setObject(LocalDate, JDBCType.DATE)", "SELECT COALESCE(date, ?) FROM test_local_date",
                        ps -> ps.setObject(1, d, JDBCType.DATE));
                runCase(conn, "toDate32(?) with setObject(LocalDate)", "SELECT COALESCE(date, toDate32(?)) FROM test_local_date",
                        ps -> ps.setObject(1, d));
    
                conn.prepareStatement("DROP TABLE test_local_date").execute();
            }
        }
    }

    Output on main (8480ca001), ClickHouse 25.8.33.6:

    setObject(LocalDate): java.sql.SQLException: Code: 386. DB::Exception: There is no supertype for types Date32, String because some of them are String/FixedString/Enum and some of them are not: In scope SELECT COALESCE(date, '2025-12-31') FROM test_local_date. (NO_COMMON_TYPE) …
    setObject(LocalDate, JDBCType.DATE): java.sql.SQLException: Code: 386. … In scope SELECT COALESCE(date, '2025-12-31') FROM test_local_date. (NO_COMMON_TYPE) …
    toDate32(?) with setObject(LocalDate): 2025-12-31 (Date32, Date)
    

    Output on main (8480ca001), ClickHouse 26.11.4.11:

    setObject(LocalDate): 2025-12-31 (Variant(Date32, String), String)
    setObject(LocalDate, JDBCType.DATE): 2025-12-31 (Variant(Date32, String), String)
    toDate32(?) with setObject(LocalDate): 2025-12-31 (Date32, Date)
    

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

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

    bugdata-typesjdbc

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions