From 8c2a163df5f34587a23674544e8976082ff3441c Mon Sep 17 00:00:00 2001 From: Sergey Chernov Date: Mon, 30 Nov 2026 14:47:41 -0800 Subject: [PATCH 1/4] Merge pull request #3427 from ClickHouse/polyglot/jdbc-v2-fixedstring-trim-trailing-nuls [jdbc-v2] Cut FixedString text after the last valid UTF-8 character in getString and getObject (cherry picked from commit 15bb71093fcdc934841e9e2140abe96e19dc8af0) --- docs/clickhouse-docs/jdbc.mdx | 4 +- docs/features.md | 2 + history/latest/3288.md | 10 +++ .../com/clickhouse/jdbc/ResultSetImpl.java | 27 ++++++- .../clickhouse/jdbc/JdbcDataTypeTests.java | 73 +++++++++++++++++++ 5 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 history/latest/3288.md diff --git a/docs/clickhouse-docs/jdbc.mdx b/docs/clickhouse-docs/jdbc.mdx index d24a75e973..8f92d25053 100644 --- a/docs/clickhouse-docs/jdbc.mdx +++ b/docs/clickhouse-docs/jdbc.mdx @@ -313,7 +313,7 @@ There are few ways to change the mapping: | FixedString | VARCHAR | java.lang.String | - `String` can be read only as `java.lang.String` or `byte[]`. -- `FixedString` is read as is and will be padded with zeros to the length of the column. (For example `FixedString(10)` for `'John'` will be read as `'John\0\0\0\0\0\0\0\0\0'`.) +- `FixedString(N)` values are padded with zero bytes (`\0`) to `N` bytes. `getString(...)`, and `getObject(...)` when it returns a `String`, cut the value after its last valid UTF-8 character: they remove the trailing `\0` characters and an incomplete multi-byte character at the end (decoded as `U+FFFD`). For example `FixedString(10)` for `'John'` is read as `'John'`. `getBytes(...)`, `getBinaryStream(...)` and `getObject(..., byte[].class)` return all `N` bytes. **Enum Types** @@ -955,7 +955,7 @@ try (Connection conn = DriverManager.getConnection(url, properties)) { | String | ✅ | VARCHAR | java.lang.String | VARCHAR | java.lang.String | | FixedString | ✅ | VARCHAR | java.lang.String | VARCHAR | java.lang.String | -- `FixedString` is read as is in both versions. For example `FixedString(10)` for `'John'` will be read as `'John\0\0\0\0\0\0\0\0\0'`. +- `FixedString` text is different: V1 reads it as is, and V2 `getString(...)` and `getObject(...)` (when it returns a `String`) remove the trailing `\0` padding and an incomplete multi-byte character at the end. For example `FixedString(10)` for `'John'` is read as `'John\0\0\0\0\0\0'` in V1 and as `'John'` in V2. In V2, `getBytes(...)` returns all `N` bytes. - When `PreparedStatement#setBytes` is used it will be converted to `unhex('')` and then read as `String`. - Strings are stored in UTF-8 encoding. diff --git a/docs/features.md b/docs/features.md index 8fe573de6c..73de30fefc 100644 --- a/docs/features.md +++ b/docs/features.md @@ -87,6 +87,7 @@ Compatibility-sensitive traits: - JDBC escape processing: Translates supported JDBC escape syntax for dates, timestamps, and functions before execution. Escape sequences are only recognized outside of quoted text, so string literals and quoted identifiers — including inlined parameter values that contain `{fn `, `{d '...'}`, or `{ts '...'}` — are passed through unchanged. - Result set streaming: Streams result sets from ClickHouse binary formats and `FORMAT JSONEachRow`, enforces max-row limits, and manages result-set lifecycle correctly. - Binary string reads: `ResultSet#getBytes(int|String)` and `ResultSet#getBinaryStream(int|String)` return the raw bytes of a `String`/`FixedString` column. Combined with the `binary_string_support` connection property, non-UTF-8/binary content stored in `String` columns round-trips byte-for-byte; `NULL` values report `null` with `wasNull()` set. `ResultSet#getObject(...)` never exposes the internal `StringValue` holder for these columns: `getObject(column, byte[].class)` returns the raw bytes, while `getObject(column, Object.class)` and the no-type `getObject(column)` overloads return a decoded `String`. +- `FixedString` text reads: `ResultSet#getString(int|String)`, and `ResultSet#getObject(...)` when it returns a `String`, cut a `FixedString(N)` value after its last valid UTF-8 character. The server pads a value that is shorter than `N` bytes with zero bytes (`\0`), and a value that is cut to `N` bytes (for example with `left`) can end in an incomplete multi-byte character. `toFixedString('Dario', 10)` is read as `"Dario"`, not as `"Dario\0\0\0\0\0"`, and `toFixedString(left('José', 4), 4)` as `"Jos"`. This applies to `FixedString`, `Nullable(FixedString)`, `LowCardinality(FixedString)` and `SimpleAggregateFunction(f, FixedString)` columns of the binary formats, also with `binary_string_support`, and has no setting. The binary accessors (`getBytes(...)`, `getBinaryStream(...)`, `getObject(..., byte[].class)`) are not changed. A `FORMAT JSONEachRow` result set has no `FixedString` column type, so its values are not changed; the server setting `output_format_trim_fixed_string` removes the zero bytes there. - Result-set metadata: Exposes JDBC `ResultSetMetaData` backed by ClickHouse column schema. - Database metadata: Implements JDBC `DatabaseMetaData` for ClickHouse catalogs, schemas, tables, columns, and related capability reporting. - Metadata with `SHOW` statements: By default `DatabaseMetaData#getSchemas`, `#getTables` and `#getColumns` use `SHOW DATABASES`, `SHOW FULL TABLES` and `DESCRIBE TABLE`, so they also return tables that the server does not show in the system tables by default (for example, tables of `DataLakeCatalog` databases). The driver property `jdbc_metadata_use_show_statements=false` selects the `system.databases`, `system.tables` and `system.columns` queries. @@ -111,6 +112,7 @@ Compatibility-sensitive traits: - `?` placeholder detection is SQL-aware and should not treat question marks inside quoted strings, quoted identifiers, comments, casts, or similar syntax as bind parameters. - String-like ClickHouse values have stable JDBC expectations: `String`, `FixedString`, and `Enum` values are returned as strings, while `UUID` is available both as `getString()` and `getObject(..., UUID.class)`. - Binary access to `String`/`FixedString` columns is compatibility-sensitive: `getBytes(...)` and `getBinaryStream(...)` expose the raw column bytes (not a re-encoded text literal), and a `NULL` column returns `null` with `wasNull()` reporting `true`. The `binary_string_support` connection property is passed through to the underlying `client-v2` transport. +- Trimming of `FixedString` text is compatibility-sensitive and limited to `getString(...)` and `getObject(...)` results of type `String` for a `FixedString` column. jdbc-v2 sees the text after the UTF-8 decoding, so it removes the trailing `\0` characters (zero bytes) and the trailing replacement characters `U+FFFD` (bytes that are not valid UTF-8). A real `U+FFFD` at the end of the value is also removed. These characters inside the value, and trailing spaces, are kept. This trimming does not apply to `String` columns, to `FixedString` values inside `Array`, `Tuple`, `Map`, `Variant` or `Dynamic`, or to `getNString`, `getNCharacterStream` and `getUnicodeStream`. - `Geometry` has a stable JDBC mapping: metadata reports SQL type `ARRAY` with type name `Geometry`, read paths return nested Java arrays rather than custom wrappers, and write paths depend on the caller preserving the intended point/array nesting shape. - JDBC `Geometry` writes share the same ambiguity as the client serializer: variant selection is inferred from nesting depth, so `Ring` versus `LineString` versus `MultiPoint`, and `Polygon` versus `MultiLineString`, are not currently distinguishable when writing through the generic `Geometry` path. - JDBC `FORMAT JSONEachRow` support is opt-in through the `jdbc_json_parser_factory` driver property, whose value must be a fully-qualified `JsonParserFactory` class name with a public no-argument constructor; JSONEachRow numeric and structured value behavior follows the selected parser and configured server output settings. Inferred JSON arrays are returned from `ResultSet.getObject(...)` as parser-native `List` values rather than JDBC `Array` values because JSONEachRow does not include element metadata. JDBC temporal typed accessors such as `getTimestamp(...)` are not guaranteed for JSONEachRow result sets; callers that need stable JDBC temporal conversions should use the binary default format or perform application-level conversion from string/object values. The recommended way to read JSON in JDBC is configuring `format=JSONEachRow` in connection properties (along with `jdbc_json_parser_factory`). Setting `format=` (empty or `null`) is an option: while it permits SQL query `FORMAT` clauses to take effect, any statement without an explicit `FORMAT` clause will fail because ClickHouse server falls back to `default_format` (`TabSeparated` or what set in `clickhouse_setting_default_format` on the connection). `DatabaseMetaData` methods are exempt — they pin `RowBinaryWithNamesAndTypes` in the settings of the statements they run internally, so metadata works for any value of `format`. diff --git a/history/latest/3288.md b/history/latest/3288.md new file mode 100644 index 0000000000..a781c550c6 --- /dev/null +++ b/history/latest/3288.md @@ -0,0 +1,10 @@ +## Breaking changes + +- **[jdbc-v2]** `ResultSet#getString(...)`, and `ResultSet#getObject(...)` when it returns a `String`, now cut a + `FixedString(N)` value after its last valid UTF-8 character: the trailing zero bytes (`\0`) that the server adds to a + value shorter than `N` bytes, and an incomplete multi-byte character at the end, are removed. + `toFixedString('Dario', 10)` is read as `"Dario"`, not as `"Dario\0\0\0\0\0"`. Applications such as Tableau 2026.3 + show these zero bytes. Only top-level `FixedString` columns change. An application that removes the zero bytes itself + (for example with `indexOf('\0')`), or that expects a text of `N` characters, must change. To read all `N` bytes, use + `getBytes(...)`, `getBinaryStream(...)` or `getObject(..., byte[].class)`, which are not changed. + (https://github.com/ClickHouse/clickhouse-java/issues/3288) diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java index cd4406cb67..f59c3408f6 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java @@ -313,7 +313,8 @@ public String getString(String columnLabel) throws SQLException { try { if (reader.hasValue(columnLabel)) { wasNull = false; - return reader.getString(columnLabel); + String value = reader.getString(columnLabel); + return value == null ? null : trimFixedString(columnLabel, value); } else { wasNull = true; return null; @@ -323,6 +324,27 @@ public String getString(String columnLabel) throws SQLException { } } + /** + * Cuts the text of a {@code FixedString} column after its last valid UTF-8 character. The server pads a + * {@code FixedString(N)} value that is shorter than {@code N} bytes with zero bytes, and a value that is cut to + * {@code N} bytes can end in an incomplete multi-byte character. The UTF-8 decoding gives {@code '\0'} for a zero + * byte and the replacement character U+FFFD for bytes that are not valid UTF-8: the first 1, 2 or 3 bytes of a + * 2-, 3- or 4-byte character decode to one U+FFFD. These characters are removed from the end, so the text ends + * with the last complete character. The same characters inside the value are kept. The text of other column + * types is returned unchanged. + */ + private String trimFixedString(String columnLabel, String value) { + int end = value.length(); + while (end > 0 && (value.charAt(end - 1) == '\0' || value.charAt(end - 1) == '\uFFFD')) { + end--; + } + if (end == value.length() + || getSchema().getColumnByName(columnLabel).getValueDataType() != ClickHouseDataType.FixedString) { + return value; + } + return value.substring(0, end); + } + @Override public boolean getBoolean(String columnLabel) throws SQLException { checkClosed(); @@ -1552,7 +1574,8 @@ public T getObjectImpl(String columnLabel, Class type, Map rows = new ArrayList<>(); + for (Object[] value : values) { + rows.add(new Object[] { value[0], value[1], false }); + rows.add(new Object[] { value[0], value[1], true }); + } + return rows.toArray(new Object[0][]); + } + + @Test(groups = { "integration" }) + public void testFixedStringTextReadKeepsOtherValues() throws Exception { + try (Connection conn = getJdbcConnection(); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT toFixedString('ab', 4) AS fixed, 'ab\\0\\0' AS str, " + + "[toFixedString('ab', 4)] AS arr, CAST(NULL AS Nullable(FixedString(4))) AS nul")) { + assertTrue(rs.next()); + byte[] padded = { 'a', 'b', 0, 0 }; + assertEquals(rs.getBytes("fixed"), padded); + assertEquals(rs.getBinaryStream("fixed").readAllBytes(), padded); + assertEquals(rs.getObject("fixed", byte[].class), padded); + assertEquals(rs.getString("str"), "ab\0\0"); + assertEquals(rs.getObject("str"), "ab\0\0"); + assertEquals(java.lang.reflect.Array.get(rs.getArray("arr").getArray(), 0), "ab\0\0"); + assertNull(rs.getString("nul")); + assertTrue(rs.wasNull()); + assertNull(rs.getObject("nul")); + assertTrue(rs.wasNull()); + assertFalse(rs.next()); + } + } + private static byte[] readClickHouseLogo() { try (InputStream is = JdbcDataTypeTests.class.getResourceAsStream("/ch_logo.png")) { Assert.assertNotNull(is, "ch_logo.png not found in test resources"); From aa83bfd1fa2ed998f351e69287140c29aad38f61 Mon Sep 17 00:00:00 2001 From: Sergey Chernov Date: Mon, 30 Nov 2026 21:36:01 -0800 Subject: [PATCH 2/4] backport of fixedString fix issue 3288 --- VERSION | 2 +- examples/client-v2-apache-arrow/pom.xml | 2 +- examples/client-v2-json-processors/pom.xml | 2 +- examples/client-v2/pom.xml | 2 +- examples/client/pom.xml | 4 ++-- examples/demo-kotlin-service/pom.xml | 2 +- examples/demo-service/pom.xml | 2 +- examples/demo-spring-service/pom.xml | 2 +- examples/jdbc-v2-json-processors/pom.xml | 2 +- examples/jdbc/pom.xml | 2 +- examples/r2dbc/clickhouse-r2dbc-spring-webflux-sample/pom.xml | 2 +- performance/pom.xml | 2 +- pom.xml | 2 +- 13 files changed, 14 insertions(+), 14 deletions(-) diff --git a/VERSION b/VERSION index bc859cbd6d..1a96df19c0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.11.2 +0.11.3 diff --git a/examples/client-v2-apache-arrow/pom.xml b/examples/client-v2-apache-arrow/pom.xml index 2e7c741c39..3563745223 100644 --- a/examples/client-v2-apache-arrow/pom.xml +++ b/examples/client-v2-apache-arrow/pom.xml @@ -54,7 +54,7 @@ UTF-8 UTF-8 - 0.11.2-SNAPSHOT + 0.11.3-SNAPSHOT 19.0.0 2.0.17 7.5.1 diff --git a/examples/client-v2-json-processors/pom.xml b/examples/client-v2-json-processors/pom.xml index 65196903b2..87dca7c7c7 100644 --- a/examples/client-v2-json-processors/pom.xml +++ b/examples/client-v2-json-processors/pom.xml @@ -17,7 +17,7 @@ UTF-8 17 - 0.11.2-SNAPSHOT + 0.11.3-SNAPSHOT 2.22.3 2.14.0 2.0.17 diff --git a/examples/client-v2/pom.xml b/examples/client-v2/pom.xml index be8ef13992..7934acee92 100644 --- a/examples/client-v2/pom.xml +++ b/examples/client-v2/pom.xml @@ -54,7 +54,7 @@ UTF-8 UTF-8 - 0.11.2-SNAPSHOT + 0.11.3-SNAPSHOT 3.8.1 diff --git a/examples/client/pom.xml b/examples/client/pom.xml index 4af37766f2..9393b18d31 100644 --- a/examples/client/pom.xml +++ b/examples/client/pom.xml @@ -40,9 +40,9 @@ UTF-8 UTF-8 - 0.11.2-SNAPSHOT + 0.11.3-SNAPSHOT - + 5.6.3 diff --git a/examples/demo-kotlin-service/pom.xml b/examples/demo-kotlin-service/pom.xml index a408f40099..b70f15d9c1 100644 --- a/examples/demo-kotlin-service/pom.xml +++ b/examples/demo-kotlin-service/pom.xml @@ -55,7 +55,7 @@ UTF-8 17 - 0.11.2-SNAPSHOT + 0.11.3-SNAPSHOT all 2.4.10 diff --git a/examples/demo-service/pom.xml b/examples/demo-service/pom.xml index 950c621e28..6573165fce 100644 --- a/examples/demo-service/pom.xml +++ b/examples/demo-service/pom.xml @@ -60,7 +60,7 @@ 2026 17 - 0.11.2-SNAPSHOT + 0.11.3-SNAPSHOT all 1.18.42 1.14.3 diff --git a/examples/demo-spring-service/pom.xml b/examples/demo-spring-service/pom.xml index 2a85177e82..ffb0679670 100755 --- a/examples/demo-spring-service/pom.xml +++ b/examples/demo-spring-service/pom.xml @@ -22,7 +22,7 @@ 2.30.0 otlp http://localhost:4318/v1/traces - 0.11.2-SNAPSHOT + 0.11.3-SNAPSHOT diff --git a/examples/jdbc-v2-json-processors/pom.xml b/examples/jdbc-v2-json-processors/pom.xml index 9bc0f62871..1a4bec93d5 100644 --- a/examples/jdbc-v2-json-processors/pom.xml +++ b/examples/jdbc-v2-json-processors/pom.xml @@ -17,7 +17,7 @@ UTF-8 17 - 0.11.2-SNAPSHOT + 0.11.3-SNAPSHOT 2.22.3 2.14.0 2.0.17 diff --git a/examples/jdbc/pom.xml b/examples/jdbc/pom.xml index ab88f2e53b..8ff92b755b 100644 --- a/examples/jdbc/pom.xml +++ b/examples/jdbc/pom.xml @@ -54,7 +54,7 @@ UTF-8 UTF-8 - 0.11.2-SNAPSHOT + 0.11.3-SNAPSHOT 4.0.3 5.2.1 diff --git a/examples/r2dbc/clickhouse-r2dbc-spring-webflux-sample/pom.xml b/examples/r2dbc/clickhouse-r2dbc-spring-webflux-sample/pom.xml index 672f2238b0..20052459ee 100644 --- a/examples/r2dbc/clickhouse-r2dbc-spring-webflux-sample/pom.xml +++ b/examples/r2dbc/clickhouse-r2dbc-spring-webflux-sample/pom.xml @@ -14,7 +14,7 @@ 1.8 1.8 - 0.11.2-SNAPSHOT + 0.11.3-SNAPSHOT 2.7.18 diff --git a/performance/pom.xml b/performance/pom.xml index 36d50607c8..f160be5759 100644 --- a/performance/pom.xml +++ b/performance/pom.xml @@ -15,7 +15,7 @@ 5.3.1 2.0.17 - 0.11.2-SNAPSHOT + 0.11.3-SNAPSHOT 1.37 2.0.2 1.5.7-23 diff --git a/pom.xml b/pom.xml index 0cd21327a8..c2096345c9 100644 --- a/pom.xml +++ b/pom.xml @@ -73,7 +73,7 @@ - 0.11.2-SNAPSHOT + 0.11.3-SNAPSHOT 2026 UTF-8 UTF-8 From a676659e8f438f95b2a146a0676ab9b767c879d4 Mon Sep 17 00:00:00 2001 From: Sergey Chernov Date: Tue, 1 Dec 2026 09:26:43 -0800 Subject: [PATCH 3/4] optimized code a bit --- .../main/java/com/clickhouse/jdbc/ResultSetImpl.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java index f59c3408f6..2ab5b042e3 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java @@ -334,15 +334,17 @@ public String getString(String columnLabel) throws SQLException { * types is returned unchanged. */ private String trimFixedString(String columnLabel, String value) { - int end = value.length(); - while (end > 0 && (value.charAt(end - 1) == '\0' || value.charAt(end - 1) == '\uFFFD')) { - end--; + final int originalLength = value.length(); + int lastCharPos = originalLength - 1; + while (lastCharPos >= 0 && (value.charAt(lastCharPos) == '\0' || value.charAt(lastCharPos) == '\uFFFD')) { + lastCharPos--; } - if (end == value.length() + final int trimmedLength = lastCharPos + 1; + if (trimmedLength == originalLength || getSchema().getColumnByName(columnLabel).getValueDataType() != ClickHouseDataType.FixedString) { return value; } - return value.substring(0, end); + return value.substring(0, trimmedLength); } @Override From 61172377492de154bc3b3bd625543e9987f34200 Mon Sep 17 00:00:00 2001 From: Sergey Chernov Date: Tue, 1 Dec 2026 09:45:28 -0800 Subject: [PATCH 4/4] Fixed trimFixedString to return proper value --- .../src/main/java/com/clickhouse/jdbc/ResultSetImpl.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java index 2ab5b042e3..c099026e9b 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java @@ -334,14 +334,16 @@ public String getString(String columnLabel) throws SQLException { * types is returned unchanged. */ private String trimFixedString(String columnLabel, String value) { + if (getSchema().getColumnByName(columnLabel).getValueDataType() != ClickHouseDataType.FixedString) { + return value; + } final int originalLength = value.length(); int lastCharPos = originalLength - 1; while (lastCharPos >= 0 && (value.charAt(lastCharPos) == '\0' || value.charAt(lastCharPos) == '\uFFFD')) { lastCharPos--; } final int trimmedLength = lastCharPos + 1; - if (trimmedLength == originalLength - || getSchema().getColumnByName(columnLabel).getValueDataType() != ClickHouseDataType.FixedString) { + if (trimmedLength == originalLength) { return value; } return value.substring(0, trimmedLength);