Skip to content

client-v2: a scalar date/time query parameter with sub-second precision is rejected by a DateTime placeholder #3435

Backlog

Description

@claude

Description

Client.query(sql, Map<String, Object> params, settings) formats a scalar query parameter with DataTypeConverter.convertParameterToString(Object), which has no knowledge of the declared {name:Type} in the SQL text. Every scalar that is not a CharSequence and not a container falls through to String.valueOf(value) (client-v2/src/main/java/com/clickhouse/client/api/internal/DataTypeConverter.java:269-288).

For a date/time value that carries sub-second precision, toString() emits a fractional part. The server parses a {p:DateTime} parameter as whole seconds, so it rejects the value with BAD_QUERY_PARAMETER (code 457, "isn't parsed completely"). The same value works with {p:DateTime64(3)}.

The same type-unawareness also means that Instant, ZonedDateTime, java.util.Date and java.sql.Timestamp are sent in their Java toString() form (trailing Z, [UTC] zone suffix, or the legacy EEE MMM dd ... form), which the server cannot parse as a DateTime at all. The same values inside a List/Map parameter are formatted correctly, because the container path uses ClickHouseValues.convertToSqlExpression.

This is the Java counterpart of ClickHouse/clickhouse-js#1204, where a JS Date with non-zero milliseconds produces a fractional timestamp that a DateTime parameter rejects.

Related but not the same: #3387 / PR #3388 cover a LocalDateTime/LocalTime with zero seconds. PR #3388 switches LocalDateTime to DATETIME_WITH_NANOS_FORMATTER, which still emits 2026-11-19 12:34:00.5 for a value with milliseconds, so a DateTime placeholder keeps failing after that fix. PR #3388 also leaves Instant, ZonedDateTime, java.util.Date and java.sql.Timestamp on the String.valueOf path.

ClickHouse server version

26.11.2.4 (official build), reached over HTTP at localhost:8123.

Reproduction

Test class placed in client-v2/src/test/java/com/clickhouse/client/, run with mvn -pl client-v2 test -Dtest=ScratchDateTimeParamTest -Dgroups=scratch:

package com.clickhouse.client;

import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
import com.clickhouse.client.api.internal.DataTypeConverter;
import com.clickhouse.client.api.query.QueryResponse;
import org.testng.annotations.Test;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

public class ScratchDateTimeParamTest {

    private static void show(String label, Object value) {
        System.out.println("FORMATTED " + label + " -> ["
                + DataTypeConverter.INSTANCE.convertParameterToString(value) + "]");
    }

    private static void run(Client client, String label, String type, Object value) {
        String sql = "SELECT {d:" + type + "} AS dt";
        Map<String, Object> params = Collections.singletonMap("d", value);
        try (QueryResponse resp = client.query(sql, params, null).get()) {
            ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(resp);
            reader.next();
            System.out.println("RESULT " + label + " type=" + type + " -> OK " + reader.getString("dt"));
        } catch (Exception e) {
            System.out.println("RESULT " + label + " type=" + type + " -> ERROR " + e.getMessage());
        }
    }

    @Test(groups = {"scratch"})
    public void testDateTimeParamWithMillis() {
        LocalDateTime ldtMs = LocalDateTime.of(2025, 3, 19, 10, 30, 0, 500_000_000);
        LocalDateTime ldtNoMs = LocalDateTime.of(2025, 3, 19, 10, 30, 0);
        Instant instMs = ldtMs.toInstant(ZoneOffset.UTC);
        ZonedDateTime zdtMs = ldtMs.atZone(ZoneId.of("UTC"));
        Date utilDate = Date.from(instMs);
        java.sql.Timestamp ts = java.sql.Timestamp.valueOf(ldtMs);

        show("LocalDateTime(ms=500)", ldtMs);
        show("Instant(ms=500)", instMs);
        show("ZonedDateTime(ms=500)", zdtMs);
        show("java.util.Date(ms=500)", utilDate);
        show("java.sql.Timestamp(ms=500)", ts);

        try (Client client = new Client.Builder()
                .addEndpoint("http://localhost:8123")
                .setUsername("default")
                .setPassword("")
                .compressServerResponse(false)
                .build()) {
            run(client, "LocalDateTime(ms=500)", "DateTime('UTC')", ldtMs);
            run(client, "LocalDateTime(ms=0)", "DateTime('UTC')", ldtNoMs);
            run(client, "LocalDateTime(ms=500)", "DateTime64(3,'UTC')", ldtMs);
            run(client, "Instant(ms=500)", "DateTime('UTC')", instMs);
            run(client, "ZonedDateTime(ms=500)", "DateTime('UTC')", zdtMs);
            run(client, "java.util.Date(ms=500)", "DateTime('UTC')", utilDate);
            run(client, "java.sql.Timestamp(ms=500)", "DateTime('UTC')", ts);
        }
    }
}

Actual output (current main, server 26.11.2.4)

FORMATTED LocalDateTime(ms=500)      -> [2025-03-19T10:30:00.500]
FORMATTED Instant(ms=500)            -> [2025-03-19T10:30:00.500Z]
FORMATTED ZonedDateTime(ms=500)      -> [2025-03-19T10:30:00.500Z[UTC]]
FORMATTED java.util.Date(ms=500)     -> [Wed Mar 19 10:30:00 UTC 2025]
FORMATTED java.sql.Timestamp(ms=500) -> [2025-03-19 10:30:00.5]

RESULT LocalDateTime(ms=500)  type=DateTime('UTC')     -> ERROR Code: 457. DB::Exception: Value 2025-03-19T10:30:00.500 cannot be parsed as DateTime('UTC') for query parameter 'd' because it isn't parsed completely: only 19 of 23 bytes was parsed: 2025-03-19T10:30:00. (BAD_QUERY_PARAMETER)
RESULT LocalDateTime(ms=0)    type=DateTime('UTC')     -> ERROR Code: 41. DB::Exception: Cannot parse time component of DateTime 10:30: value 2025-03-19T10:30 cannot be parsed as DateTime('UTC') for query parameter 'd'. (CANNOT_PARSE_DATETIME)
RESULT LocalDateTime(ms=500)  type=DateTime64(3,'UTC') -> OK 2025-03-19 10:30:00.5
RESULT Instant(ms=500)        type=DateTime('UTC')     -> ERROR Code: 457. DB::Exception: Value 2025-03-19T10:30:00.500Z cannot be parsed as DateTime('UTC') ... only 19 of 24 bytes was parsed
RESULT ZonedDateTime(ms=500)  type=DateTime('UTC')     -> ERROR Code: 457. DB::Exception: Value 2025-03-19T10:30:00.500Z[UTC] cannot be parsed as DateTime('UTC') ... only 19 of 29 bytes was parsed
RESULT java.util.Date(ms=500) type=DateTime('UTC')     -> ERROR Code: 41. DB::Exception: Cannot parse datetime: value Wed Mar 19 10:30:00 UTC 2025 cannot be parsed as DateTime('UTC') for query parameter 'd'. (CANNOT_PARSE_DATETIME)
RESULT java.sql.Timestamp(ms=500) type=DateTime('UTC') -> ERROR Code: 457. DB::Exception: Value 2025-03-19 10:30:00.5 cannot be parsed as DateTime('UTC') ... only 19 of 21 bytes was parsed

The LocalDateTime(ms=0) line is the already-reported #3387; it is included only to separate the two failures.

Expected

A date/time value binds to a DateTime placeholder, the way the same value inside Array(DateTime) already does. The server accepts 2025-03-19 10:30:00 for a DateTime parameter and 2025-03-19 10:30:00.500 for a DateTime64(3) parameter.

Suggested fix

DataTypeConverter.convertParameterToString (client-v2/src/main/java/com/clickhouse/client/api/internal/DataTypeConverter.java:269) needs the declared parameter type to pick between the whole-second and the fractional text form; the converter already has type-aware dateTimeToString(Object, ClickHouseColumn) helpers for the write path. Options a maintainer may want to weigh:

  • read the {name:Type} placeholder out of the SQL text and route the value through the existing type-aware formatters (this is the direction discussed in ClickHouse/clickhouse-js#1204), or
  • expose a typed parameter API so the caller declares the target type, or
  • at minimum, give Instant, ZonedDateTime, OffsetDateTime, java.util.Date and java.sql.Timestamp a ClickHouse text form instead of String.valueOf, and document that a value with sub-second precision needs a DateTime64(n) placeholder.

Whatever is chosen should also cover Nullable(DateTime), which fails the same way.

Link

Relayed from ClickHouse/clickhouse-js#1204 · tracking issue ClickHouse/integrations-ai-playground#794

Activity

  1. polyglotAI-bot commented on Nov 26, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — maintainer decision
    Decide how a scalar parameter value gets the text form of its {name:Type}: the converter does not get the type. Open PR #3388 does not change this.

    Analysis: DataTypeConverter.convertParameterToString (DataTypeConverter.java:269, called from Client.java:2009) gets the value without its {name:Type} and formats each scalar that is not a String with String.valueOf (:288), so it cannot choose whole seconds for DateTime. Container leaves use ClickHouseValues.DATETIME_FORMATTER (clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseValues.java:70-71), which also writes the fraction, so Array(DateTime) fails too. docs/features.md:50 says that the value must match the server's text form for the type.

    Reproduction: complete program and output

    Changed from the issue's steps: the TestNG test as a main, with a password; added the last case (the same value in a List).

    import com.clickhouse.client.api.Client;
    import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
    import com.clickhouse.client.api.internal.DataTypeConverter;
    import com.clickhouse.client.api.query.QueryResponse;
    
    import java.time.Instant;
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.time.ZoneOffset;
    import java.time.ZonedDateTime;
    import java.util.Collections;
    import java.util.Date;
    import java.util.Map;
    
    public class Repro {
        static final String URL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
        static final String PASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
    
        private static void show(String label, Object value) {
            System.out.println("FORMATTED " + label + " -> ["
                    + DataTypeConverter.INSTANCE.convertParameterToString(value) + "]");
        }
    
        private static void run(Client client, String label, String type, Object value) {
            String sql = "SELECT {d:" + type + "} AS dt";
            Map<String, Object> params = Collections.singletonMap("d", value);
            try (QueryResponse resp = client.query(sql, params, null).get()) {
                ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(resp);
                reader.next();
                System.out.println("RESULT " + label + " type=" + type + " -> OK " + reader.getString("dt"));
            } catch (Exception e) {
                System.out.println("RESULT " + label + " type=" + type + " -> ERROR " + e.getMessage());
            }
        }
    
        public static void main(String[] args) {
            LocalDateTime ldtMs = LocalDateTime.of(2025, 3, 19, 10, 30, 0, 500_000_000);
            LocalDateTime ldtNoMs = LocalDateTime.of(2025, 3, 19, 10, 30, 0);
            Instant instMs = ldtMs.toInstant(ZoneOffset.UTC);
            ZonedDateTime zdtMs = ldtMs.atZone(ZoneId.of("UTC"));
            Date utilDate = Date.from(instMs);
            java.sql.Timestamp ts = java.sql.Timestamp.valueOf(ldtMs);
    
            show("LocalDateTime(ms=500)", ldtMs);
            show("Instant(ms=500)", instMs);
            show("ZonedDateTime(ms=500)", zdtMs);
            show("java.util.Date(ms=500)", utilDate);
            show("java.sql.Timestamp(ms=500)", ts);
    
            try (Client client = new Client.Builder()
                    .addEndpoint(URL)
                    .setUsername("default")
                    .setPassword(PASSWORD)
                    .compressServerResponse(false)
                    .build()) {
                run(client, "LocalDateTime(ms=500)", "DateTime('UTC')", ldtMs);
                run(client, "LocalDateTime(ms=0)", "DateTime('UTC')", ldtNoMs);
                run(client, "LocalDateTime(ms=500)", "DateTime64(3,'UTC')", ldtMs);
                run(client, "Instant(ms=500)", "DateTime('UTC')", instMs);
                run(client, "ZonedDateTime(ms=500)", "DateTime('UTC')", zdtMs);
                run(client, "java.util.Date(ms=500)", "DateTime('UTC')", utilDate);
                run(client, "java.sql.Timestamp(ms=500)", "DateTime('UTC')", ts);
                // added: the same LocalDateTime inside a List
                run(client, "List<LocalDateTime(ms=500)>", "Array(DateTime('UTC'))", Collections.singletonList(ldtMs));
            }
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.2.4 (RESULT lines only):

    RESULT LocalDateTime(ms=500) type=DateTime('UTC') -> ERROR Code: 457. DB::Exception: Value 2025-03-19T10:30:00.500 cannot be parsed as DateTime('UTC') … (BAD_QUERY_PARAMETER) …
    RESULT LocalDateTime(ms=0) type=DateTime('UTC') -> ERROR Code: 41. DB::Exception: Cannot parse time component of DateTime 10:30 … (CANNOT_PARSE_DATETIME) …
    RESULT LocalDateTime(ms=500) type=DateTime64(3,'UTC') -> OK 2025-03-19 10:30:00.5
    RESULT Instant(ms=500) type=DateTime('UTC') -> ERROR Code: 457. DB::Exception: Value 2025-03-19T10:30:00.500Z cannot be parsed as DateTime('UTC') … (BAD_QUERY_PARAMETER) …
    RESULT ZonedDateTime(ms=500) type=DateTime('UTC') -> ERROR Code: 457. DB::Exception: Value 2025-03-19T10:30:00.500Z[UTC] cannot be parsed as DateTime('UTC') … (BAD_QUERY_PARAMETER) …
    RESULT java.util.Date(ms=500) type=DateTime('UTC') -> ERROR Code: 41. DB::Exception: Cannot parse datetime: value Wed Mar 19 10:30:00 UTC 2025 … (CANNOT_PARSE_DATETIME) …
    RESULT java.sql.Timestamp(ms=500) type=DateTime('UTC') -> ERROR Code: 457. DB::Exception: Value 2025-03-19 10:30:00.5 cannot be parsed as DateTime('UTC') … (BAD_QUERY_PARAMETER) …
    RESULT List<LocalDateTime(ms=500)> type=Array(DateTime('UTC')) -> ERROR Code: 27. DB::Exception: Cannot parse input: expected '\'' before: '.5\']': value ['2025-03-19 10:30:00.5'] cannot be parsed as Array(DateTime('UTC')) … (CANNOT_PARSE_INPUT_ASSERTION_FAILED) …
    

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

  2. added
    client
    data-types
    on Nov 26, 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

    bugclientdata-types

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions