From 8ba876bd5c4455a18c15020927cea55382d6b156 Mon Sep 17 00:00:00 2001 From: Indian-boult Date: Wed, 18 Nov 2026 09:25:41 +0530 Subject: [PATCH] Parse Enum labels that end with a backslash readParameters treated a quote as escaped when the character before it was a backslash. For a label that ends with an escaped backslash, such as 'a\\', the closing quote was skipped, the rest of the type name was read as part of the label and the parser failed with NumberFormatException. A query that returned such a column failed before the first row. Skip the character after each backslash in a quoted string, the same way as readNameOrQuotedString does. Escaped quotes are read as before. Closes #3371 --- .../com/clickhouse/data/ClickHouseUtils.java | 7 +++- .../clickhouse/data/ClickHouseColumnTest.java | 32 +++++++++++++++++++ .../clickhouse/data/ClickHouseUtilsTest.java | 6 ++++ .../datatypes/RowBinaryFormatWriterTest.java | 1 + history/latest/3371.md | 6 ++++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 history/latest/3371.md diff --git a/clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseUtils.java b/clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseUtils.java index 0ce876e47..75fee6236 100644 --- a/clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseUtils.java +++ b/clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseUtils.java @@ -1544,7 +1544,12 @@ public static int readParameters(String args, int startIndex, int len, List self = c -> c; + Function first = c -> c.getNestedColumns().get(0); + return new Object[][] { + { "Enum8('a\\\\' = 1, 'z' = 2)", self, new String[] { "a\\", "z" }, new int[] { 1, 2 } }, + { "Enum8('z' = 2, 'a\\\\' = 1)", self, new String[] { "z", "a\\" }, new int[] { 2, 1 } }, + { "Enum16('a\\\\\\\\' = 1, 'z' = 2)", self, new String[] { "a\\\\", "z" }, new int[] { 1, 2 } }, + { "Enum8('\\\\\\'' = 1, 'z' = 2)", self, new String[] { "\\'", "z" }, new int[] { 1, 2 } }, + { "Nullable(Enum8('a\\\\' = 1, 'z' = 2))", self, new String[] { "a\\", "z" }, new int[] { 1, 2 } }, + { "Array(Enum8('a\\\\' = 1, 'z' = 2))", + (Function) ClickHouseColumn::getArrayBaseColumn, + new String[] { "a\\", "z" }, new int[] { 1, 2 } }, + { "Map(String, Enum8('a\\\\' = 1, 'z' = 2))", + (Function) ClickHouseColumn::getValueInfo, + new String[] { "a\\", "z" }, new int[] { 1, 2 } }, + { "Tuple(Enum8('a\\\\' = 1, 'z' = 2), String)", first, new String[] { "a\\", "z" }, + new int[] { 1, 2 } }, + { "Variant(Enum8('a\\\\' = 1, 'z' = 2), String)", first, new String[] { "a\\", "z" }, + new int[] { 1, 2 } }, + }; + } + + @Test(groups = { "unit" }, dataProvider = "enumLabelBackslashProvider") + public void testEnumLabelEndsWithBackslash(String typeName, + Function enumColumn, String[] names, int[] values) { + ClickHouseColumn column = enumColumn.apply(ClickHouseColumn.of("e", typeName)); + Assert.assertEquals(column.getEnumConstants().getNames(), names); + Assert.assertEquals(column.getEnumConstants().getValues(), values); + } + @DataProvider(name = "enumWidthProvider") private Object[][] getEnumWidths() { return new Object[][] { diff --git a/clickhouse-data/src/test/java/com/clickhouse/data/ClickHouseUtilsTest.java b/clickhouse-data/src/test/java/com/clickhouse/data/ClickHouseUtilsTest.java index bd80c0686..7473dc339 100644 --- a/clickhouse-data/src/test/java/com/clickhouse/data/ClickHouseUtilsTest.java +++ b/clickhouse-data/src/test/java/com/clickhouse/data/ClickHouseUtilsTest.java @@ -523,6 +523,12 @@ public void testReadParameters() { Assert.assertEquals(ClickHouseUtils.readParameters(args, args.indexOf('('), args.length(), params), args.lastIndexOf(')') + 1); Assert.assertEquals(params, Arrays.asList("anyLast", "Nested(a string,b string)")); + + params.clear(); + args = "Enum8('a\\\\' = 1, 'b\\\\\\'' = 2, 'c' = 3)"; + Assert.assertEquals(ClickHouseUtils.readParameters(args, args.indexOf('('), args.length(), params), + args.length()); + Assert.assertEquals(params, Arrays.asList("'a\\\\'=1", "'b\\\\\\''=2", "'c'=3")); } @Test(groups = { "unit" }) diff --git a/client-v2/src/test/java/com/clickhouse/client/datatypes/RowBinaryFormatWriterTest.java b/client-v2/src/test/java/com/clickhouse/client/datatypes/RowBinaryFormatWriterTest.java index 9f46b0f0c..101eff630 100644 --- a/client-v2/src/test/java/com/clickhouse/client/datatypes/RowBinaryFormatWriterTest.java +++ b/client-v2/src/test/java/com/clickhouse/client/datatypes/RowBinaryFormatWriterTest.java @@ -302,6 +302,7 @@ private Object[][] enumTypesInSchema() { {"Enum('a' = 1, 'b' = 1000)", "a"}, {"enum('a' = 1, 'b' = 1000)", "b"}, {"ENUM('a' = -128, 'b' = 127)", "b"}, + {"Enum8('a\\\\' = 1, 'z' = 2)", "a\\"}, {"Variant(Bool, Enum('a' = 1, 'b' = 1000))", "b"}, {"Nullable(Enum('a' = 1, 'b' = 1000))", "a"}, }; diff --git a/history/latest/3371.md b/history/latest/3371.md new file mode 100644 index 000000000..7f4adabd7 --- /dev/null +++ b/history/latest/3371.md @@ -0,0 +1,6 @@ +- **[client-v2, jdbc-v2]** Fixed type names with an Enum label that ends with a backslash, such as + `Enum8('a\\' = 1, 'z' = 2)`. The type parser treated the closing quote as escaped because the character before it + was a backslash, so it failed with `NumberFormatException`. A query that returned such a column failed before the + first row, and so did a `TableSchema` built from the type name. The parser now skips the character after each + backslash in a quoted string, so `\\` is one escaped backslash and the next quote closes the string. Escaped quotes + such as `\'` are read as before. (https://github.com/ClickHouse/clickhouse-java/issues/3371)