From 6517ca7d10ec87c9aa79e5f13f864da85aa5305f Mon Sep 17 00:00:00 2001 From: Sergey Chernov Date: Tue, 1 Dec 2026 10:36:26 -0800 Subject: [PATCH] optimized --- .../com/clickhouse/jdbc/ResultSetImpl.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 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 72f306082..b414c302e 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java @@ -328,7 +328,7 @@ 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 @@ -337,16 +337,20 @@ public String getString(String columnLabel) throws SQLException { * 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--; + 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--; } - if (end == value.length() - || getSchema().getColumnByName(columnLabel).getValueDataType() != ClickHouseDataType.FixedString) { + final int trimmedLength = lastCharPos + 1; + if (trimmedLength == originalLength) { return value; } - return value.substring(0, end); + return value.substring(0, trimmedLength); } @Override