From e4f48490dcdbd5a67524c1688a3b23a9d1008ee2 Mon Sep 17 00:00:00 2001 From: 7487 <1042653432@qq.com> Date: Thu, 19 Nov 2026 11:20:15 +0800 Subject: [PATCH] Fix scalar local temporal query parameters Fixes #3387. Signed-off-by: 7487 <1042653432@qq.com> --- .../api/internal/DataTypeConverter.java | 6 ++++++ .../api/internal/DataTypeConverterTest.java | 8 ++++++- .../clickhouse/client/query/QueryTests.java | 21 +++++++++++++++++++ docs/features.md | 1 + history/latest/3387.md | 4 ++++ 5 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 history/latest/3387.md diff --git a/client-v2/src/main/java/com/clickhouse/client/api/internal/DataTypeConverter.java b/client-v2/src/main/java/com/clickhouse/client/api/internal/DataTypeConverter.java index 713e9cc430..8ac41258b2 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/internal/DataTypeConverter.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/internal/DataTypeConverter.java @@ -283,6 +283,12 @@ public String convertParameterToString(Object value) { // the value). Escape those three characters so any String value round-trips. return escapeStringParameter((CharSequence) value); } + if (value instanceof LocalDateTime) { + return DataTypeUtils.DATETIME_WITH_NANOS_FORMATTER.format((LocalDateTime) value); + } + if (value instanceof LocalTime) { + return DataTypeUtils.TIME_WITH_NANOS_FORMATTER.format((LocalTime) value); + } // Other scalars have no escapable characters and are read verbatim by the server, so they // are passed through unquoted (e.g. Date, numbers, Identifier). return String.valueOf(value); diff --git a/client-v2/src/test/java/com/clickhouse/client/api/internal/DataTypeConverterTest.java b/client-v2/src/test/java/com/clickhouse/client/api/internal/DataTypeConverterTest.java index 40416db867..99c2a734ad 100644 --- a/client-v2/src/test/java/com/clickhouse/client/api/internal/DataTypeConverterTest.java +++ b/client-v2/src/test/java/com/clickhouse/client/api/internal/DataTypeConverterTest.java @@ -201,6 +201,12 @@ public static Object[][] queryParameters() { // --- Scalars: bare, UNQUOTED text form. The server reads a scalar {name:Type} value // verbatim, so quoting it (e.g. '2026-05-13' for a Date) is rejected. --- {LocalDate.of(2026, 5, 13), "2026-05-13"}, + {LocalDateTime.of(2026, 11, 19, 0, 0), "2026-11-19 00:00:00"}, + {LocalDateTime.of(2026, 11, 19, 12, 34, 56), "2026-11-19 12:34:56"}, + {LocalDateTime.of(2026, 11, 19, 12, 34, 0, 500_000_000), "2026-11-19 12:34:00.5"}, + {LocalTime.of(9, 0), "09:00:00"}, + {LocalTime.of(12, 34, 56), "12:34:56"}, + {LocalTime.of(12, 34, 0, 500_000_000), "12:34:00.5"}, {"hello", "hello"}, {42, "42"}, {new BigDecimal("1.50"), "1.50"}, @@ -288,4 +294,4 @@ public void testConvertDeeplyNestedParameterDoesNotOverflow() { assertEquals(new DataTypeConverter().convertParameterToString(nested), expected.toString()); } -} \ No newline at end of file +} diff --git a/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java b/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java index a1e5da5343..4f45f1cad6 100644 --- a/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java +++ b/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java @@ -63,6 +63,7 @@ import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; @@ -1864,6 +1865,26 @@ public void testContainerQueryParamsQuoteInnerValues(String clickHouseType, Obje Assert.assertEquals(records.get(0).getString("v"), expected); } + @DataProvider(name = "scalarTemporalQueryParameters") + Object[][] scalarTemporalQueryParameters() { + return new Object[][]{ + {"DateTime('UTC')", LocalDateTime.of(2026, 11, 19, 0, 0), "2026-11-19 00:00:00"}, + {"DateTime64(9, 'UTC')", LocalDateTime.of(2026, 11, 19, 12, 34, 0, 123_456_789), "2026-11-19 12:34:00.123456789"}, + {"Time", LocalTime.of(9, 0), "09:00:00"}, + {"Time64(3)", LocalTime.of(12, 34, 0, 500_000_000), "12:34:00.500"}, + }; + } + + @Test(groups = {"integration"}, dataProvider = "scalarTemporalQueryParameters") + public void testScalarTemporalQueryParamsRoundTrip(String clickHouseType, Object value, String expected) { + QuerySettings settings = new QuerySettings().serverSetting("allow_experimental_time_time64_type", "1"); + Map params = Collections.singletonMap("p", value); + List records = client.queryAll("SELECT toString({p:" + clickHouseType + "}) AS v", params, settings); + + Assert.assertEquals(records.size(), 1); + Assert.assertEquals(records.get(0).getString("v"), expected); + } + @DataProvider(name = "scalarStringQueryParameters") Object[][] scalarStringQueryParameters() { // Scalar {p:String} values that must round-trip byte-for-byte through the server's diff --git a/docs/features.md b/docs/features.md index a8f7e12b30..7bd725a139 100644 --- a/docs/features.md +++ b/docs/features.md @@ -46,6 +46,7 @@ This document lists stable, user-visible behavior in `client-v2` and `jdbc-v2` t Compatibility-sensitive traits: - Named parameter typing is part of the contract: placeholders are written as `{name:Type}` and the supplied value must match the expected ClickHouse textual representation for that type. +- Scalar `LocalDateTime` and `LocalTime` query parameters always include the seconds field and preserve non-zero fractional seconds, matching the ClickHouse `DateTime`/`DateTime64` and `Time`/`Time64` textual forms. - String query parameters are expected to round-trip correctly for ordinary text, Unicode, slashes, dashes, leading or trailing spaces, and characters the server's escaped-text parameter parser treats as structural — a backslash, tab, or newline — which the client escapes before sending. - A `null` query-parameter value is sent as the ClickHouse `\N` NULL sentinel, so a top-level `{name:Nullable(T)}` placeholder binds SQL `NULL`; a `null` nested inside an `Array`/`Map` parameter value renders as the SQL `NULL` keyword. - Runtime authentication changes are compatibility-sensitive: after `updateUserAndPassword()`, `updateAccessToken()`, or `updateBearerToken()`, subsequent requests from the same `Client` are expected to use the updated credentials. The authentication method itself is fixed at construction time; calling a runtime updater that does not match the configured method throws `ClientMisconfigurationException`. diff --git a/history/latest/3387.md b/history/latest/3387.md new file mode 100644 index 0000000000..ae8db47bed --- /dev/null +++ b/history/latest/3387.md @@ -0,0 +1,4 @@ +- **[client-v2]** Scalar `LocalDateTime` and `LocalTime` query parameters now always include seconds + and preserve non-zero fractional seconds. Zero-second `DateTime` values no longer fail to parse, + and zero-second `Time` values no longer bind as minutes and seconds. + (https://github.com/ClickHouse/clickhouse-java/issues/3387)