From 183a434b836ccb485cd05f2e10cee220536f3ec6 Mon Sep 17 00:00:00 2001 From: utvaxling Date: Fri, 27 Nov 2026 12:16:58 -0800 Subject: [PATCH 1/2] [client-v2] Return a String value as it is from DataTypeConverter.stringToString The typed getter getString of the binary format readers sends the value of a String column through DataTypeConverter.stringToString. For a value that already is a String, the method made a StringBuilder, copied the text into it and made a new String from it: three allocations and two copies for each call, for a result that is equal to the input. The method now returns the value when it is a String and the column is not an array. The quotes for an array column and all other value types (StringValue, CharSequence, byte[]) go through the same code as before. Measured with the JMH benchmark QueryClient.queryV2WithTypes of the performance module on JDK 17: one query of 100,000 rows of 30 columns, each value read with readValue and with its typed getter, 2 forks before and 2 forks after. Allocation for each operation goes from 237.3 MB to 153.4 MB (-35.4%). The mean time goes from 117.7 ms to 109.4 ms (-7.0%); this is from one pair of runs, and the noise of this benchmark is 5%. QueryClient.queryV2, which does not call getString, does not change. Co-Authored-By: Claude Fable 5.2 --- .../client/api/internal/DataTypeConverter.java | 3 +++ .../api/internal/DataTypeConverterTest.java | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) 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..cb8f527067 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 @@ -106,6 +106,9 @@ public String qbitToString(Object value, ClickHouseColumn column) { } public String stringToString(Object bytesOrString, ClickHouseColumn column) { + if (bytesOrString instanceof String && !column.isArray()) { + return (String) bytesOrString; + } StringBuilder sb = new StringBuilder(); if (column.isArray()) { sb.append(QUOTE); 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..10eab0b542 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 @@ -5,6 +5,7 @@ import org.testng.annotations.Test; import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; @@ -63,6 +64,23 @@ public void testListToString() { assertEquals(converter.convertToString(Arrays.asList(null, null, null), column), "[NULL, NULL, NULL]"); } + @DataProvider(name = "stringValues") + public static Object[][] stringValues() { + return new Object[][] { + {"abc", "String", "abc"}, + {"", "String", ""}, + {new StringBuilder("abc"), "String", "abc"}, + {"abc".getBytes(StandardCharsets.UTF_8), "String", "abc"}, + {"abc", "Array(String)", "'abc'"}, + {new StringBuilder("abc"), "Array(String)", "'abc'"}, + }; + } + + @Test(dataProvider = "stringValues") + public void testStringToString(Object value, String columnType, String expected) { + assertEquals(new DataTypeConverter().stringToString(value, ClickHouseColumn.of("v", columnType)), expected); + } + @Test public void testDateToString() { DataTypeConverter converter = new DataTypeConverter(); From 1af8dfa8d06e6a990a4be66fb8223ff87f0cc1b5 Mon Sep 17 00:00:00 2001 From: utvaxling Date: Mon, 30 Nov 2026 12:48:56 -0800 Subject: [PATCH 2/2] [client-v2] DataTypeConverter.stringToString makes a StringBuilder only for the quoted form The first commit of this change returned a String value as it is. All other values (StringValue, CharSequence, byte[], other objects) still went through a StringBuilder, also when the column is not an array and no quotes are added. The method now finds the text first and makes a StringBuilder, with the correct size, only when it adds the quotes for an array column. For a StringValue it returns the String that the StringValue keeps. The results do not change: a byte[] value is decoded with the default charset as before, and a null column gives a NullPointerException as before. A probe of the method on JDK 17 gave, for each call (the value included where the call makes it): StringValue 248 bytes and 32 ns before, 120 bytes and 17 ns after; StringBuilder 232 bytes before, 96 bytes after; byte[] 216 bytes before, 64 bytes after; String on an array column 160 bytes before, 48 bytes after; String on a String column 0 bytes and 3 ns before and after. The JMH benchmark QueryClient.queryV2WithTypes of the performance module reads String values only, so it does not change against the first commit: 153.4 MB for each operation, against 237.3 MB before the change (-35.4%). The mean time is 108.7 ms against 119.1 ms before the change (-8.8%); the run of the first commit gave -7.0%. Co-Authored-By: Claude Fable 5.2 --- .../api/internal/DataTypeConverter.java | 27 +++++++++---------- .../api/internal/DataTypeConverterTest.java | 3 +++ 2 files changed, 15 insertions(+), 15 deletions(-) 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 cb8f527067..fb219c2234 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 @@ -106,26 +106,23 @@ public String qbitToString(Object value, ClickHouseColumn column) { } public String stringToString(Object bytesOrString, ClickHouseColumn column) { - if (bytesOrString instanceof String && !column.isArray()) { - return (String) bytesOrString; - } - StringBuilder sb = new StringBuilder(); - if (column.isArray()) { - sb.append(QUOTE); - } - if (bytesOrString instanceof StringValue) { - sb.append(((StringValue) bytesOrString).asString()); + boolean quoted = column.isArray(); + String str; + if (bytesOrString instanceof String) { + str = (String) bytesOrString; + } else if (bytesOrString instanceof StringValue) { + str = ((StringValue) bytesOrString).asString(); } else if (bytesOrString instanceof CharSequence) { - sb.append(((CharSequence) bytesOrString)); + str = bytesOrString.toString(); } else if (bytesOrString instanceof byte[]) { - sb.append(new String((byte[]) bytesOrString)); + str = new String((byte[]) bytesOrString); } else { - sb.append(bytesOrString); + str = String.valueOf(bytesOrString); } - if (column.isArray()) { - sb.append(QUOTE); + if (quoted) { + return new StringBuilder(str.length() + 2).append(QUOTE).append(str).append(QUOTE).toString(); } - return sb.toString(); + return str; } public static ZoneId UTC_ZONE_ID = ZoneId.of("UTC"); 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 10eab0b542..00437a0113 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 @@ -71,8 +71,11 @@ public static Object[][] stringValues() { {"", "String", ""}, {new StringBuilder("abc"), "String", "abc"}, {"abc".getBytes(StandardCharsets.UTF_8), "String", "abc"}, + {42, "String", "42"}, {"abc", "Array(String)", "'abc'"}, {new StringBuilder("abc"), "Array(String)", "'abc'"}, + {"abc".getBytes(StandardCharsets.UTF_8), "Array(String)", "'abc'"}, + {42, "Array(String)", "'42'"}, }; }