client-v2: a scalar date/time query parameter with sub-second precision is rejected by a DateTime placeholder
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
Start with client-v2/src/main/java/com/clickhouse/client/api/internal/DataTypeConverter.java and its convertParameterToString and dateTimeToString methods; compare scalar formatting with the container path that uses ClickHouseValues.convertToSqlExpression. Reproduce with the provided DateTime and DateTime64 examples, then check Nullable(DateTime) as described. Done when scalar date/time parameters use a server accepted form for the declared type, with coverage for the listed Java time classes.
由索引模型根据 Issue 内容生成。
描述
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.Dateandjava.sql.Timestampa ClickHouse text form instead ofString.valueOf, and document that a value with sub-second precision needs aDateTime64(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
- 主要语言
- Java
- 星标
- 1.6k
- 派生
- 657
- 平均合并
- 2 天 6 小时
- 30 天内合并 PR
- 36
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
ClickHouse/clickhouse-java 的其他 Issue
-
api-diagnostics bug jdbc
难度 2/5 1-3 小时 新手友好度 75/100
ClickHouse/clickhouse-java#3464 ·
维护者通常 1 天内回复
-
tests
难度 2/5 1-3 小时 新手友好度 76/100
ClickHouse/clickhouse-java#3452 ·
维护者通常 1 天内回复
-
bug jdbc performance queries sql-parser
难度 2/5 1-3 小时 新手友好度 77/100
ClickHouse/clickhouse-java#3451 ·
维护者通常 1 天内回复
-
bug jdbc legacy queries
难度 2/5 1-3 小时 新手友好度 70/100
ClickHouse/clickhouse-java#3444 ·
维护者通常 1 天内回复
-
api-diagnostics bug client jdbc
难度 2/5 1-3 小时 新手友好度 76/100
ClickHouse/clickhouse-java#3439 · 1 条评论 ·
维护者通常 1 天内回复
查看 ClickHouse/clickhouse-java 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 60/100
quelea-projection/Quelea#749 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 92/100
erkamyaman/rust-anything#963 ·
维护者通常 1 天内回复
-
难度 2/5 半天 新手友好度 76/100
-
Deferred
难度 2/5 1-3 小时 新手友好度 78/100
Sanne/incus-spawn#2768 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复