From 7c2e8334a676d54a3167bc440b6ccfb3b8cf67e9 Mon Sep 17 00:00:00 2001 From: Polyglot AI <293096396+polyglotAI-bot@users.noreply.github.com> Date: Wed, 2 Dec 2026 05:19:20 +0000 Subject: [PATCH 1/3] [tests] Core SQL test harness: write the zero bytes of a jdbc FixedString column Since #3427, jdbc-v2 getObject gives the text of a FixedString(N) column without its trailing zero bytes (docs/features.md, "FixedString text reads"). The shim wrote the bytes of that String, so the jdbc backend lost the zero bytes that clickhouse-client writes: LiveTest failed for toFixedString('ab', 4), and 4 allowlisted upstream tests failed on jdbc. The renderer now adds zero bytes up to N to a String value of a top-level FixedString(N) column (also in Nullable, LowCardinality and SimpleAggregateFunction), in the escaped and the Raw formats. A FixedString value in a container or a Variant keeps its N bytes in jdbc-v2, so those values do not change, and a String member of a Variant gets no zero bytes. Fixes: https://github.com/ClickHouse/clickhouse-java/issues/3488 Co-Authored-By: Claude Opus 5.5 --- tests/clickhouse-client/README.md | 7 +++- .../clickhouse/client/cli/TsvRenderer.java | 37 +++++++++++++++++++ .../com/clickhouse/client/cli/LiveTest.java | 5 +++ .../client/cli/TsvRendererTest.java | 16 ++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/tests/clickhouse-client/README.md b/tests/clickhouse-client/README.md index 65871137e..88ccb4d13 100644 --- a/tests/clickhouse-client/README.md +++ b/tests/clickhouse-client/README.md @@ -379,8 +379,11 @@ tests read their own queries from `system.query_log`. `TabSeparated`: escaped strings and `\N` at the top level, quoted strings and `NULL` in `Array`, `Tuple` and `Map`, the shortest float text, decimals without trailing zeros, `DateTime64(P)` with P digits, `Time` over 24 hours, - the server's IPv6 forms, and the map order of the server. `WITH TOTALS` and - `extremes` rows are not in the binary output over HTTP. + the server's IPv6 forms, and the map order of the server. jdbc-v2 gives the + text of a `FixedString(N)` column without its trailing zero bytes + ([`docs/features.md`](../../docs/features.md)), so the renderer adds zero + bytes up to N bytes. `WITH TOTALS` and `extremes` rows are not in the + binary output over HTTP. - **Settings.** Of the settings on the command line, only `log_comment`, `max_insert_threads`, `max_threads`, `send_logs_level`, `async_insert` and `enable_parallel_replicas` go to the server, with each query, as in diff --git a/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java b/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java index 4d7db386f..3fcbf6a29 100644 --- a/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java +++ b/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java @@ -78,6 +78,15 @@ private static String describe(Object value) { * escapes. */ void render(TsvOutput out, Object value, boolean raw) { + byte[] fixed = paddedFixedString(type, value); + if (fixed != null) { + if (raw) { + out.write(fixed); + } else { + writeEscaped(out, fixed, false); + } + return; + } if (raw && value != null && isStringType(type)) { byte[] bytes = stringBytes(type, value); if (bytes != null) { @@ -304,6 +313,34 @@ static boolean isStringType(ChType t) { } } + /** + * Returns the bytes of a {@code String} value of a {@code FixedString(N)} column, with zero bytes added up to N + * bytes, or null for any other column or value. jdbc-v2 gives the text of such a column without its trailing + * zero bytes ({@code docs/features.md}, "{@code FixedString} text reads"), and the server writes all N bytes. A + * {@code FixedString} value in a container or a {@code Variant} keeps its N bytes in jdbc-v2, so only the column + * value gets the zero bytes back. + */ + private static byte[] paddedFixedString(ChType t, Object v) { + if (!(v instanceof String)) { + return null; + } + while (t != null && (t.base.equals("Nullable") || t.base.equals("LowCardinality") + || t.base.equals("SimpleAggregateFunction"))) { + t = t.typeArg(t.base.equals("SimpleAggregateFunction") ? t.args.size() - 1 : 0); + } + if (t == null || !t.base.equals("FixedString")) { + return null; + } + int n; + try { + n = Integer.parseInt(t.literalArg(0)); + } catch (NumberFormatException e) { + throw new UnsupportedTypeException(t.toString()); + } + byte[] bytes = ((String) v).getBytes(StandardCharsets.UTF_8); + return bytes.length < n ? Arrays.copyOf(bytes, n) : bytes; + } + /** Returns the bytes of a string-like value, or null when v is not one. */ private static byte[] stringBytes(ChType t, Object v) { if (v instanceof String) { diff --git a/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/LiveTest.java b/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/LiveTest.java index 39e50b720..4427157f0 100644 --- a/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/LiveTest.java +++ b/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/LiveTest.java @@ -92,6 +92,11 @@ private ShimResult runShim(String backend, Map env, String query "toFloat32(0.1), toFloat32(3.4028235e38), toFloat32(-1e-7), toBFloat16(1.5)", "toDecimal32(1.50, 2), toDecimal64(-0.001, 5), toDecimal128(100, 3), toDecimal256('1.5', 10), toDecimal32(0, 3)", "'a\\tb\\\\c\\nd''e\\0\\b\\f\\v\\x01\\r', toFixedString('ab', 4), 'ünïcödé'", + "toNullable(toFixedString('y', 2)), toLowCardinality(toFixedString('z', 2)), toLowCardinality(toNullable(toFixedString('v', 2))), toFixedString('', 3), toFixedString('é', 3), toFixedString('abcd', 4)", + "CAST(toFixedString('w', 3) AS SimpleAggregateFunction(any, FixedString(3))), CAST(NULL AS Nullable(FixedString(2))), [toFixedString('x', 3)], (toFixedString('t', 2), 1)", + // The String member of a Variant with a FixedString member: no zero bytes. + "CAST(CAST('ab', 'Variant(String, UInt8)') AS Variant(String, FixedString(4), UInt8)) SETTINGS allow_suspicious_variant_types = 1", + "toFixedString('a\\tb', 4), toNullable(toFixedString('c', 2)), 'd\\te' FORMAT TSVRaw", "true, false, NULL, CAST(NULL AS Nullable(String)), toNullable(1)", "toDate('2020-01-02'), toDate32('1900-01-01'), toDate32('2299-12-31'), toDateTime('2020-01-01 10:00:00', 'UTC')", "toDateTime('2020-01-01 10:00:00', 'Asia/Tokyo'), toDateTime64('2020-01-01 00:00:00.120', 3), toDateTime64('2020-01-01 00:00:00', 9, 'Asia/Tokyo')", diff --git a/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/TsvRendererTest.java b/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/TsvRendererTest.java index 3f38465b4..9b4c8207c 100644 --- a/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/TsvRendererTest.java +++ b/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/TsvRendererTest.java @@ -214,6 +214,19 @@ public Object[][] values() throws Exception { {"IntervalWeek", Period.ofWeeks(2), "2"}, {"IntervalSecond", Duration.ofSeconds(-5), "-5"}, {"LowCardinality(String)", "a", "a"}, + // jdbc-v2 gives the text of a FixedString column without the trailing zero bytes. The server writes + // all N bytes. + {"FixedString(4)", "ab", "ab\\0\\0"}, + {"Nullable(FixedString(3))", "é", "é\\0"}, + {"LowCardinality(Nullable(FixedString(2)))", "", "\\0\\0"}, + {"SimpleAggregateFunction(any, FixedString(3))", "w", "w\\0\\0"}, + {"Nullable(FixedString(2))", null, "\\N"}, + {"FixedString(4)", "\0a", "\\0a\\0\\0"}, + // A value of N or more bytes is not cut: bytes that are not valid UTF-8 became U+FFFD (3 bytes). + {"FixedString(2)", "\uFFFDa", "\uFFFDa"}, + // A FixedString value in a container is not trimmed. A String member of a Variant is not padded. + {"Array(FixedString(3))", Arrays.asList("x\0\0", null), "['x\\0\\0',NULL]"}, + {"Variant(FixedString(4), String, UInt8)", "ab", "ab"}, {"SimpleAggregateFunction(sum, UInt64)", BigInteger.valueOf(7), "7"}, {"Enum8('a' = 1, 'b' = 2)", new BinaryStreamReader.EnumValue("b", 2), "b"}, {"Array(Enum8('a' = 1))", Collections.singletonList("a"), "['a']"}, @@ -243,6 +256,9 @@ public void testRenderRaw() { TsvOutput out = new TsvOutput(); new TsvRenderer("Nullable(String)").render(out, "a\tb", true); assertEquals(out.toString(), "a\tb"); + out = new TsvOutput(); + new TsvRenderer("Nullable(FixedString(4))").render(out, "a\tb", true); + assertEquals(out.toString(), "a\tb\0"); } @Test From 1504146c03921c03db283a9251af3a740f01a235 Mon Sep 17 00:00:00 2001 From: Polyglot AI <293096396+polyglotAI-bot@users.noreply.github.com> Date: Wed, 2 Dec 2026 05:39:54 +0000 Subject: [PATCH 2/3] [tests] Core SQL test harness: add the FixedString zero bytes for the jdbc backend only client-v2 gives all N bytes of a FixedString value, so the padding does nothing there today. With it, a client-v2 value without its zero bytes would show no diff. The jdbc Result now tells the renderer that the client trims FixedString text (Backend.Result.trimsFixedString), and the renderer gets N from the column type once. More cases: a value of exactly N bytes, LowCardinality(FixedString), SimpleAggregateFunction(any, Nullable(FixedString)), a Map of FixedString, the FixedString member of a Variant, and the client mode. README: also the trailing U+FFFD of jdbc-v2, and the values that keep their N bytes. Co-Authored-By: Claude Opus 5.5 --- tests/clickhouse-client/README.md | 12 +++-- .../com/clickhouse/client/cli/Backend.java | 8 +++ .../clickhouse/client/cli/JdbcBackend.java | 5 ++ .../com/clickhouse/client/cli/Runner.java | 2 +- .../clickhouse/client/cli/TsvRenderer.java | 49 ++++++++++------- .../com/clickhouse/client/cli/LiveTest.java | 6 +-- .../client/cli/TsvRendererTest.java | 54 ++++++++++++++----- 7 files changed, 94 insertions(+), 42 deletions(-) diff --git a/tests/clickhouse-client/README.md b/tests/clickhouse-client/README.md index 88ccb4d13..f8d350c4e 100644 --- a/tests/clickhouse-client/README.md +++ b/tests/clickhouse-client/README.md @@ -380,10 +380,14 @@ tests read their own queries from `system.query_log`. and `NULL` in `Array`, `Tuple` and `Map`, the shortest float text, decimals without trailing zeros, `DateTime64(P)` with P digits, `Time` over 24 hours, the server's IPv6 forms, and the map order of the server. jdbc-v2 gives the - text of a `FixedString(N)` column without its trailing zero bytes - ([`docs/features.md`](../../docs/features.md)), so the renderer adds zero - bytes up to N bytes. `WITH TOTALS` and `extremes` rows are not in the - binary output over HTTP. + text of a `FixedString(N)` column (also in `Nullable`, `LowCardinality` and + `SimpleAggregateFunction`) without its trailing zero bytes and trailing + U+FFFD characters ([`docs/features.md`](../../docs/features.md)), so for + the `jdbc` backend the renderer adds zero bytes up to N bytes. A value that + ends in bytes that are not valid UTF-8 cannot be restored + (`[string-invalid-utf8]`). A `FixedString` value in `Array`, `Tuple`, + `Map` or `Variant` keeps its N bytes in jdbc-v2. `WITH TOTALS` and + `extremes` rows are not in the binary output over HTTP. - **Settings.** Of the settings on the command line, only `log_comment`, `max_insert_threads`, `max_threads`, `send_logs_level`, `async_insert` and `enable_parallel_replicas` go to the server, with each query, as in diff --git a/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/Backend.java b/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/Backend.java index aa9e44034..98a880f9d 100644 --- a/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/Backend.java +++ b/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/Backend.java @@ -43,6 +43,14 @@ default byte[] rawText() throws Exception { return null; } + /** + * True when the client gives the text of a {@code FixedString(N)} column without its trailing zero bytes + * (jdbc-v2, {@code docs/features.md}). Then the renderer writes the zero bytes from the column type. + */ + default boolean trimsFixedString() { + return false; + } + @Override void close() throws Exception; } diff --git a/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/JdbcBackend.java b/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/JdbcBackend.java index 3204482ae..339f727e0 100644 --- a/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/JdbcBackend.java +++ b/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/JdbcBackend.java @@ -108,6 +108,11 @@ public Object get(int i) throws SQLException { return rs.getObject(i + 1); } + @Override + public boolean trimsFixedString() { + return true; + } + @Override public void close() throws SQLException { rs.close(); diff --git a/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/Runner.java b/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/Runner.java index 05e7cd535..791adf572 100644 --- a/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/Runner.java +++ b/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/Runner.java @@ -141,7 +141,7 @@ private void writeRows(Backend.Result rs, String sql, boolean other) throws Exce List types = rs.types(); List renderers = new ArrayList<>(types.size()); for (String t : types) { - renderers.add(new TsvRenderer(t)); + renderers.add(new TsvRenderer(t, rs.trimsFixedString())); } if (!discard && format.names) { writeHeader(names, format.raw); diff --git a/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java b/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java index 3fcbf6a29..9254651ce 100644 --- a/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java +++ b/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java @@ -68,22 +68,37 @@ private static String describe(Object value) { } private final ChType type; + /** N when the client gives the text of this {@code FixedString(N)} column without its zero bytes, else -1. */ + private final int fixedStringLength; TsvRenderer(String typeName) { + this(typeName, false); + } + + /** + * @param trimsFixedString true when the client gives the text of a {@code FixedString(N)} column without its + * trailing zero bytes ({@link Backend.Result#trimsFixedString()}) + */ + TsvRenderer(String typeName, boolean trimsFixedString) { this.type = ChType.parse(typeName); + this.fixedStringLength = trimsFixedString ? fixedStringLength(type) : -1; } /** * Writes one top-level value. With raw (the {@code TSVRaw} formats), a top-level string is written without - * escapes. + * escapes. When the client gives the text of a {@code FixedString(N)} column without its trailing zero bytes, + * a value of fewer than N bytes gets zero bytes up to N bytes, as the server writes it. */ void render(TsvOutput out, Object value, boolean raw) { - byte[] fixed = paddedFixedString(type, value); - if (fixed != null) { + if (fixedStringLength >= 0 && value instanceof String) { + byte[] bytes = ((String) value).getBytes(StandardCharsets.UTF_8); + if (bytes.length < fixedStringLength) { + bytes = Arrays.copyOf(bytes, fixedStringLength); + } if (raw) { - out.write(fixed); + out.write(bytes); } else { - writeEscaped(out, fixed, false); + writeEscaped(out, bytes, false); } return; } @@ -314,31 +329,25 @@ static boolean isStringType(ChType t) { } /** - * Returns the bytes of a {@code String} value of a {@code FixedString(N)} column, with zero bytes added up to N - * bytes, or null for any other column or value. jdbc-v2 gives the text of such a column without its trailing - * zero bytes ({@code docs/features.md}, "{@code FixedString} text reads"), and the server writes all N bytes. A - * {@code FixedString} value in a container or a {@code Variant} keeps its N bytes in jdbc-v2, so only the column - * value gets the zero bytes back. + * Returns N for a {@code FixedString(N)} column, also in {@code Nullable}, {@code LowCardinality} and + * {@code SimpleAggregateFunction}, or -1 for any other column. These are the columns whose text jdbc-v2 gives + * without the trailing zero bytes ({@code docs/features.md}, "{@code FixedString} text reads"). A + * {@code FixedString} value in a container or a {@code Variant} keeps its N bytes in jdbc-v2. A type without a + * valid N also gives -1, and its values are written as the client gives them. */ - private static byte[] paddedFixedString(ChType t, Object v) { - if (!(v instanceof String)) { - return null; - } + private static int fixedStringLength(ChType t) { while (t != null && (t.base.equals("Nullable") || t.base.equals("LowCardinality") || t.base.equals("SimpleAggregateFunction"))) { t = t.typeArg(t.base.equals("SimpleAggregateFunction") ? t.args.size() - 1 : 0); } if (t == null || !t.base.equals("FixedString")) { - return null; + return -1; } - int n; try { - n = Integer.parseInt(t.literalArg(0)); + return Integer.parseInt(t.literalArg(0)); } catch (NumberFormatException e) { - throw new UnsupportedTypeException(t.toString()); + return -1; } - byte[] bytes = ((String) v).getBytes(StandardCharsets.UTF_8); - return bytes.length < n ? Arrays.copyOf(bytes, n) : bytes; } /** Returns the bytes of a string-like value, or null when v is not one. */ diff --git a/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/LiveTest.java b/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/LiveTest.java index 4427157f0..b74df4fc4 100644 --- a/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/LiveTest.java +++ b/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/LiveTest.java @@ -93,9 +93,9 @@ private ShimResult runShim(String backend, Map env, String query "toDecimal32(1.50, 2), toDecimal64(-0.001, 5), toDecimal128(100, 3), toDecimal256('1.5', 10), toDecimal32(0, 3)", "'a\\tb\\\\c\\nd''e\\0\\b\\f\\v\\x01\\r', toFixedString('ab', 4), 'ünïcödé'", "toNullable(toFixedString('y', 2)), toLowCardinality(toFixedString('z', 2)), toLowCardinality(toNullable(toFixedString('v', 2))), toFixedString('', 3), toFixedString('é', 3), toFixedString('abcd', 4)", - "CAST(toFixedString('w', 3) AS SimpleAggregateFunction(any, FixedString(3))), CAST(NULL AS Nullable(FixedString(2))), [toFixedString('x', 3)], (toFixedString('t', 2), 1)", - // The String member of a Variant with a FixedString member: no zero bytes. - "CAST(CAST('ab', 'Variant(String, UInt8)') AS Variant(String, FixedString(4), UInt8)) SETTINGS allow_suspicious_variant_types = 1", + "CAST(toFixedString('w', 3) AS SimpleAggregateFunction(any, FixedString(3))), CAST(NULL AS Nullable(FixedString(2))), [toFixedString('x', 3)], (toFixedString('t', 2), 1), map(toFixedString('k', 2), toFixedString('v', 3))", + // A Variant with a FixedString and a String member: the String member gets no zero bytes. + "CAST(CAST('ab', 'Variant(String, UInt8)') AS Variant(String, FixedString(4), UInt8)), CAST(toFixedString('cd', 4) AS Variant(String, FixedString(4))) SETTINGS allow_suspicious_variant_types = 1", "toFixedString('a\\tb', 4), toNullable(toFixedString('c', 2)), 'd\\te' FORMAT TSVRaw", "true, false, NULL, CAST(NULL AS Nullable(String)), toNullable(1)", "toDate('2020-01-02'), toDate32('1900-01-01'), toDate32('2299-12-31'), toDateTime('2020-01-01 10:00:00', 'UTC')", diff --git a/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/TsvRendererTest.java b/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/TsvRendererTest.java index 9b4c8207c..c502723a5 100644 --- a/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/TsvRendererTest.java +++ b/tests/clickhouse-client/src/test/java/com/clickhouse/client/cli/TsvRendererTest.java @@ -214,19 +214,6 @@ public Object[][] values() throws Exception { {"IntervalWeek", Period.ofWeeks(2), "2"}, {"IntervalSecond", Duration.ofSeconds(-5), "-5"}, {"LowCardinality(String)", "a", "a"}, - // jdbc-v2 gives the text of a FixedString column without the trailing zero bytes. The server writes - // all N bytes. - {"FixedString(4)", "ab", "ab\\0\\0"}, - {"Nullable(FixedString(3))", "é", "é\\0"}, - {"LowCardinality(Nullable(FixedString(2)))", "", "\\0\\0"}, - {"SimpleAggregateFunction(any, FixedString(3))", "w", "w\\0\\0"}, - {"Nullable(FixedString(2))", null, "\\N"}, - {"FixedString(4)", "\0a", "\\0a\\0\\0"}, - // A value of N or more bytes is not cut: bytes that are not valid UTF-8 became U+FFFD (3 bytes). - {"FixedString(2)", "\uFFFDa", "\uFFFDa"}, - // A FixedString value in a container is not trimmed. A String member of a Variant is not padded. - {"Array(FixedString(3))", Arrays.asList("x\0\0", null), "['x\\0\\0',NULL]"}, - {"Variant(FixedString(4), String, UInt8)", "ab", "ab"}, {"SimpleAggregateFunction(sum, UInt64)", BigInteger.valueOf(7), "7"}, {"Enum8('a' = 1, 'b' = 2)", new BinaryStreamReader.EnumValue("b", 2), "b"}, {"Array(Enum8('a' = 1))", Collections.singletonList("a"), "['a']"}, @@ -251,13 +238,52 @@ public void testRender(String type, Object value, String expected) { assertEquals(render(type, value), expected); } + @DataProvider + public Object[][] trimmedFixedStrings() { + // jdbc-v2 gives the text of a FixedString column without the trailing zero bytes. The server writes all N + // bytes. + return new Object[][] { + {"FixedString(4)", "ab", "ab\\0\\0"}, + {"Nullable(FixedString(3))", "é", "é\\0"}, + {"LowCardinality(FixedString(2))", "", "\\0\\0"}, + {"LowCardinality(Nullable(FixedString(2)))", "z", "z\\0"}, + {"SimpleAggregateFunction(any, Nullable(FixedString(3)))", "w", "w\\0\\0"}, + {"Nullable(FixedString(2))", null, "\\N"}, + {"FixedString(4)", "\0a", "\\0a\\0\\0"}, + // A value of N bytes gets no more zero bytes. + {"FixedString(4)", "ab\0\0", "ab\\0\\0"}, + // A value of more than N bytes is not cut. (Bytes that are not valid UTF-8 read as U+FFFD, 3 bytes; + // the server writes the original bytes: [string-invalid-utf8] in allowlist.txt.) + {"FixedString(2)", "\uFFFDa", "\uFFFDa"}, + // A FixedString value in a container keeps its N bytes in jdbc-v2. A String member of a Variant and + // a String column get no zero bytes. + {"Array(FixedString(3))", Arrays.asList("x\0\0", null), "['x\\0\\0',NULL]"}, + {"Variant(FixedString(4), String, UInt8)", "ab", "ab"}, + {"String", "ab", "ab"}, + }; + } + + @Test(dataProvider = "trimmedFixedStrings") + public void testRenderTrimmedFixedString(String type, Object value, String expected) { + TsvOutput out = new TsvOutput(); + new TsvRenderer(type, true).render(out, value, false); + assertEquals(out.toString(), expected); + } + + @Test + public void testRenderFixedStringOfClient() { + // client-v2 gives all N bytes. A shorter value is written as the client gives it, so that the diff shows it. + assertEquals(render("FixedString(4)", "ab\0\0"), "ab\\0\\0"); + assertEquals(render("FixedString(4)", "ab"), "ab"); + } + @Test public void testRenderRaw() { TsvOutput out = new TsvOutput(); new TsvRenderer("Nullable(String)").render(out, "a\tb", true); assertEquals(out.toString(), "a\tb"); out = new TsvOutput(); - new TsvRenderer("Nullable(FixedString(4))").render(out, "a\tb", true); + new TsvRenderer("Nullable(FixedString(4))", true).render(out, "a\tb", true); assertEquals(out.toString(), "a\tb\0"); } From 04ab00c4e179095ae135677cb9fe972eff84b3a7 Mon Sep 17 00:00:00 2001 From: Polyglot AI <293096396+polyglotAI-bot@users.noreply.github.com> Date: Thu, 3 Dec 2026 12:59:18 +0000 Subject: [PATCH 3/3] [tests] Core SQL test harness: write the padded FixedString through renderValue render() wrote a padded FixedString value with its own raw and escaped branches. Since #3494, renderValue writes a top-level string of a Nullable, LowCardinality or SimpleAggregateFunction column raw or escaped. render() now only adds the zero characters to the String and gives it to renderValue, so one code path writes all string values. The output does not change. README: a value that ends in a real U+FFFD character cannot be restored either. jdbc-v2 removes every trailing U+FFFD (#3427). Co-Authored-By: Claude Opus 5.5 --- tests/clickhouse-client/README.md | 8 ++++---- .../com/clickhouse/client/cli/TsvRenderer.java | 17 +++++++---------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/tests/clickhouse-client/README.md b/tests/clickhouse-client/README.md index f8d350c4e..bf7a3bef7 100644 --- a/tests/clickhouse-client/README.md +++ b/tests/clickhouse-client/README.md @@ -384,10 +384,10 @@ tests read their own queries from `system.query_log`. `SimpleAggregateFunction`) without its trailing zero bytes and trailing U+FFFD characters ([`docs/features.md`](../../docs/features.md)), so for the `jdbc` backend the renderer adds zero bytes up to N bytes. A value that - ends in bytes that are not valid UTF-8 cannot be restored - (`[string-invalid-utf8]`). A `FixedString` value in `Array`, `Tuple`, - `Map` or `Variant` keeps its N bytes in jdbc-v2. `WITH TOTALS` and - `extremes` rows are not in the binary output over HTTP. + ends in bytes that are not valid UTF-8 (`[string-invalid-utf8]`), or in a + real U+FFFD character, cannot be restored. A `FixedString` value in + `Array`, `Tuple`, `Map` or `Variant` keeps its N bytes in jdbc-v2. + `WITH TOTALS` and `extremes` rows are not in the binary output over HTTP. - **Settings.** Of the settings on the command line, only `log_comment`, `max_insert_threads`, `max_threads`, `send_logs_level`, `async_insert` and `enable_parallel_replicas` go to the server, with each query, as in diff --git a/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java b/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java index aad11d24b..ec0a8dbb9 100644 --- a/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java +++ b/tests/clickhouse-client/src/main/java/com/clickhouse/client/cli/TsvRenderer.java @@ -91,20 +91,17 @@ private static String describe(Object value) { */ void render(TsvOutput out, Object value, boolean raw) { if (fixedStringLength >= 0 && value instanceof String) { - byte[] bytes = ((String) value).getBytes(StandardCharsets.UTF_8); - if (bytes.length < fixedStringLength) { - bytes = Arrays.copyOf(bytes, fixedStringLength); - } - if (raw) { - out.write(bytes); - } else { - writeEscaped(out, bytes, false); - } - return; + value = padFixedString((String) value, fixedStringLength); } renderValue(out, type, value, false, raw); } + /** Adds zero characters to s up to n UTF-8 bytes. A value of n or more bytes is not changed. */ + private static String padFixedString(String s, int n) { + int missing = n - s.getBytes(StandardCharsets.UTF_8).length; + return missing > 0 ? s + "\0".repeat(missing) : s; + } + static void renderValue(TsvOutput out, ChType t, Object v, boolean nested) { renderValue(out, t, v, nested, false); }