From 2887bb5128d2911e676a13041716e9b1bd528c23 Mon Sep 17 00:00:00 2001 From: Polyglot AI <293096396+polyglotAI-bot@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:15:27 +0000 Subject: [PATCH 1/2] [jdbc-v2] Cut FixedString text after the last valid UTF-8 character ResultSet#getString and ResultSet#getObject (when the result is a String) cut a FixedString(N) value after its last valid UTF-8 character: the zero bytes that the server adds to a value shorter than N bytes, and an incomplete multi-byte character at the end, are removed. Only top-level FixedString columns change. The binary accessors still return all N bytes. Implements: https://github.com/ClickHouse/clickhouse-java/issues/3288 Co-Authored-By: Claude Opus 5.5 --- docs/features.md | 2 + history/latest/3288.md | 10 +++ .../com/clickhouse/jdbc/ResultSetImpl.java | 26 ++++++- .../clickhouse/jdbc/JdbcDataTypeTests.java | 67 +++++++++++++++++++ 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 history/latest/3288.md diff --git a/docs/features.md b/docs/features.md index ef27f4018..c3d1238cf 100644 --- a/docs/features.md +++ b/docs/features.md @@ -91,6 +91,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. @@ -117,6 +118,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 000000000..a781c550c --- /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 e91dd2e44..1cbbfafa0 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java @@ -317,7 +317,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; @@ -327,6 +328,26 @@ 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, so these characters are removed + * from the end. 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(); @@ -1556,7 +1577,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 61931bf96b1131022c6b0853c3d3b657f2bac3e8 Mon Sep 17 00:00:00 2001 From: Polyglot AI <293096396+polyglotAI-bot@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:42:22 +0000 Subject: [PATCH 2/2] [jdbc-v2] Test FixedString text cut in 2-, 3- and 4-byte characters Add test rows for a FixedString value that ends in the first 1, 2 or 3 bytes of a 2-, 3- or 4-byte character, with and without zero padding. Each cut character decodes to one U+FFFD, which getString and getObject remove. Explain this in the trimFixedString Javadoc. Update docs/clickhouse-docs/jdbc.mdx: jdbc-v2 no longer reads FixedString text with the zero padding, and V1 and V2 are different here. Co-Authored-By: Claude Opus 5.5 --- docs/clickhouse-docs/jdbc.mdx | 4 ++-- .../src/main/java/com/clickhouse/jdbc/ResultSetImpl.java | 7 ++++--- .../test/java/com/clickhouse/jdbc/JdbcDataTypeTests.java | 6 ++++++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/docs/clickhouse-docs/jdbc.mdx b/docs/clickhouse-docs/jdbc.mdx index f8e044bf4..3514d058e 100644 --- a/docs/clickhouse-docs/jdbc.mdx +++ b/docs/clickhouse-docs/jdbc.mdx @@ -358,7 +358,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** @@ -1002,7 +1002,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/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java index 1cbbfafa0..72f306082 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java @@ -332,9 +332,10 @@ 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, so these characters are removed - * from the end. The same characters inside the value are kept. The text of other column types is returned - * unchanged. + * 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(); diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/JdbcDataTypeTests.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/JdbcDataTypeTests.java index 4966116bf..ae93f2d49 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/JdbcDataTypeTests.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/JdbcDataTypeTests.java @@ -2100,7 +2100,13 @@ public Object[][] testFixedStringTextReadDP() { { "toFixedString('', 4)", "" }, { "toFixedString('a\u00e9\u20ac\ud83d\ude00', 14)", "a\u00e9\u20ac\ud83d\ude00" }, { "toFixedString(left('Jos\u00e9', 4), 4)", "Jos" }, + { "toFixedString(left('a\u00e9', 2), 4)", "a" }, + { "toFixedString(left('a\u20ac', 2), 2)", "a" }, { "toFixedString(left('a\u20ac', 3), 6)", "a" }, + { "toFixedString(left('a\ud83d\ude00', 2), 2)", "a" }, + { "toFixedString(left('a\ud83d\ude00', 3), 5)", "a" }, + { "toFixedString(left('a\ud83d\ude00', 4), 4)", "a" }, + { "toFixedString(left('\u00e9\u20ac\ud83d\ude00', 8), 10)", "\u00e9\u20ac" }, { "CAST(unhex('6100620000') AS FixedString(5))", "a\0b" }, { "CAST(unhex('61FF62') AS FixedString(4))", "a\uFFFDb" }, { "CAST('ab' AS Nullable(FixedString(4)))", "ab" },