Skip to content

client-v2: LocalDateTime/LocalTime query parameter with zero seconds fails (DateTime) or binds a wrong value (Time) #3387

Open

Description

@polyglotAI-bot

Description

Client.query(sql, Map<String, Object> params, ...) sends a scalar java.time.LocalDateTime or java.time.LocalTime parameter without the seconds field when the seconds and nanoseconds are zero. For example, LocalDate.of(2026, 11, 19).atStartOfDay() is sent as 2026-11-19T00:00, and LocalTime.of(9, 0) as 09:00. The server needs the seconds field:

  • {p:DateTime}, {p:DateTime64(n)}, {p:Nullable(DateTime64(n))}: the query fails with CANNOT_PARSE_DATETIME (code 41). Every LocalDate.atStartOfDay() value has zero seconds, so a typical day-range filter fails.
  • {p:Time}, {p:Time64(n)}: there is no error. The server reads 09:00 as minutes:seconds, so the bound value is 00:09:00. An INSERT ... SELECT {t:Time} stores the wrong value.

Values with non-zero seconds work, and so do the same values inside a List or Map parameter.

Steps to reproduce

  1. Run the code example below against a ClickHouse server.
  2. The LocalTime.of(9, 0) value is stored as 00:09:00.
  3. The LocalDateTime query with a zero-seconds value fails with CANNOT_PARSE_DATETIME.

Error Log or Exception StackTrace

com.clickhouse.client.api.ClientException: Failed to get query response
Caused by: com.clickhouse.client.api.ServerException: Code: 41. DB::Exception: Cannot parse time component of DateTime 00:00: value 2026-11-19T00:00 cannot be parsed as DateTime64(3, 'UTC') for query parameter 'from'. (CANNOT_PARSE_DATETIME) (version 26.10.4.207 (official build))

Results of SELECT toString({p:<type>}):

Java value text sent DateTime DateTime64(3), DateTime64(9) Time Time64(3)
LocalDateTime 2000-01-01T00:00 2000-01-01T00:00 code 41 code 41
LocalDateTime 2000-01-01T12:34 2000-01-01T12:34 code 41 code 41
LocalDateTime 2000-01-01T12:34:56 2000-01-01T12:34:56 OK OK
List.of(LocalDateTime 2000-01-01T00:00) as Array(...) ['2000-01-01 00:00:00'] OK OK
LocalTime 12:34 12:34 00:12:34 (wrong) 00:12:34.000 (wrong)
LocalTime 09:00 09:00 00:09:00 (wrong)
LocalTime 12:34:56 12:34:56 OK OK

Expected Behaviour

A LocalDateTime or LocalTime parameter binds the same value for all values of the seconds field. docs/features.md lists "SQL parameter formatting, including date/time handling" and says that the supplied value must match the ClickHouse text form for the type. The server accepts the form with seconds and rejects (or misreads) the form without them:

$ curl -G "$CH" --data-urlencode 'param_p=2000-01-01T00:00'    --data-urlencode "query=SELECT toString({p:DateTime('UTC')})"
Code: 41. DB::Exception: Cannot parse time component of DateTime 00:00: value 2000-01-01T00:00 cannot be parsed as DateTime('UTC') for query parameter ...
$ curl -G "$CH" --data-urlencode 'param_p=2000-01-01 00:00:00' --data-urlencode "query=SELECT toString({p:DateTime('UTC')})"
2000-01-01 00:00:00
$ curl -G "$CH" --data-urlencode 'param_p=12:34'    --data-urlencode 'query=SELECT toString({p:Time})'
00:12:34
$ curl -G "$CH" --data-urlencode 'param_p=12:34:00' --data-urlencode 'query=SELECT toString({p:Time})'
12:34:00

Code Example

Reproduction program (Java, 20 lines)
try (Client client = new Client.Builder().addEndpoint("http://localhost:8123")
        .setUsername("default").setPassword("").build()) {

    // Contrast: non-zero seconds -> OK, prints 2026-11-19 00:00:01.000
    Map<String, Object> p1 = new HashMap<>();
    p1.put("p", LocalDateTime.of(2026, 11, 19, 0, 0, 1));
    System.out.println(client.queryAll("SELECT toString({p:DateTime64(3, 'UTC')}) AS s", p1).get(0).getString("s"));

    // 1. LocalTime with zero seconds -> wrong value, no error
    client.queryAll("CREATE OR REPLACE TABLE t_time (t Time) ENGINE = Memory");
    Map<String, Object> p2 = new HashMap<>();
    p2.put("t", LocalTime.of(9, 0));                               // toString(): 09:00
    client.query("INSERT INTO t_time SELECT {t:Time}", p2).get().close();
    System.out.println(client.queryAll("SELECT toString(t) AS s FROM t_time").get(0).getString("s")); // prints 00:09:00, expected 09:00:00

    // 2. LocalDateTime with zero seconds -> CANNOT_PARSE_DATETIME
    Map<String, Object> p3 = new HashMap<>();
    p3.put("from", LocalDate.of(2026, 11, 19).atStartOfDay());     // toString(): 2026-11-19T00:00
    client.queryAll("SELECT toString({from:DateTime64(3, 'UTC')}) AS s", p3);  // throws ServerException
}

Root cause

convertParameterToString (client-v2/src/main/java/com/clickhouse/client/api/internal/DataTypeConverter.java:288, called from Client.java:2009) returns String.valueOf(value) for every scalar that is not null, a CharSequence or a container. LocalDateTime.toString() and LocalTime.toString() use the shortest ISO-8601 form, which leaves out :ss when the seconds and nanoseconds are zero (LocalTime#toString javadoc: "The output will be one of the ISO-8601 formats: HH:mm, HH:mm:ss, ...").

The fix is local to convertParameterToString. A maintainer decision: should it also cover scalar Instant, ZonedDateTime, OffsetDateTime and java.sql.Timestamp (bound to DateTime)? The same line sends them, and they fail for every value.

Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Configuration

Client Configuration

new Client.Builder().addEndpoint("http://localhost:8123").setUsername("default").setPassword("").build();

Environment

  • Cloud
  • Client version: client-v2 0.12.0-rc1-SNAPSHOT, built from main at 7506301
  • Language version: OpenJDK 17.0.21
  • OS: Linux (Docker)

ClickHouse Server

  • ClickHouse Server version: 26.10.4.207
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: in the code example (t_time, only for the Time example)
  • Sample data for all these tables: not needed

Found by automated analysis (Polyglot AI) while working on a DateTime64 write test of client-v2. Verified against a live server. Shortened on 2026-11-20: the edit history has the first version.

Activity

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

    area:data-typebugclient-api-v2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions