From b6b1c632264b91c31916de8597a80d8b126b216f Mon Sep 17 00:00:00 2001 From: Indian-boult Date: Wed, 18 Nov 2026 00:51:36 +0530 Subject: [PATCH] Number Enum entries without a value the same way as the server ClickHouseEnum split each entry on the last '=' and required a value, so type names like Enum8('a', 'b') or Enum8('a' = 5, 'b') failed with "Invalid enum entry", and a quoted name with '=' inside was split in the wrong place. Read the quoted name first and then an optional value. Give an entry without a value the first value plus its position, or 1 plus its position when the first entry has no value, as the server does. Keep rejecting lists where only some entries after the first one have a value. Closes #3344 --- .../com/clickhouse/data/ClickHouseEnum.java | 59 +++++++++++++++++-- .../clickhouse/data/ClickHouseColumnTest.java | 45 ++++++++++++++ .../datatypes/RowBinaryFormatWriterTest.java | 4 ++ history/latest/3344.md | 9 +++ 4 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 history/latest/3344.md diff --git a/clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseEnum.java b/clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseEnum.java index 8f6855920..f213cd74f 100644 --- a/clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseEnum.java +++ b/clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseEnum.java @@ -39,17 +39,66 @@ protected ClickHouseEnum(Collection params) { names = new String[size]; values = new int[size]; + boolean[] hasValue = new boolean[size]; int i = 0; for (String p : params) { - int index = p.lastIndexOf('='); - if (index > 0) { - names[i] = ClickHouseUtils.unescape(p.substring(0, index)); - values[i] = Integer.parseInt(p.substring(index + 1)); + int nameEnd = quotedNameEnd(p); + if (nameEnd == p.length()) { + names[i] = ClickHouseUtils.unescape(p); } else { - throw new IllegalArgumentException("Invalid enum entry: " + p); + int index = nameEnd > 0 ? nameEnd : p.lastIndexOf('='); + if (index > 0 && p.charAt(index) == '=') { + names[i] = ClickHouseUtils.unescape(p.substring(0, index)); + values[i] = Integer.parseInt(p.substring(index + 1)); + hasValue[i] = true; + } else { + throw new IllegalArgumentException("Invalid enum entry: " + p); + } } i++; } + + // The server gives an entry without a value the number base + position. The base is the value of the + // first entry, or 1 when the first entry has no value. All entries after the first one need a value, or + // none of them. + int valuesAfterFirst = 0; + for (i = 1; i < size; i++) { + if (hasValue[i]) { + valuesAfterFirst++; + } + } + if (valuesAfterFirst != 0 && valuesAfterFirst != size - 1) { + throw new IllegalArgumentException( + "Either all or none of the enum entries after the first one must have a value, got " + params); + } + int base = size > 0 && hasValue[0] ? values[0] : 1; + for (i = 0; i < size; i++) { + if (!hasValue[i]) { + values[i] = base + i; + } + } + } + + private static int quotedNameEnd(String entry) { + int len = entry.length(); + if (len == 0 || !ClickHouseUtils.isQuote(entry.charAt(0))) { + return -1; + } + + char quote = entry.charAt(0); + for (int i = 1; i < len; i++) { + char ch = entry.charAt(i); + if (ch == '\\') { + i++; + } else if (ch == quote) { + if (i + 1 < len && entry.charAt(i + 1) == quote) { + i++; + } else { + return i + 1; + } + } + } + return -1; } public ClickHouseEnum(String[] names, int[] values) { diff --git a/clickhouse-data/src/test/java/com/clickhouse/data/ClickHouseColumnTest.java b/clickhouse-data/src/test/java/com/clickhouse/data/ClickHouseColumnTest.java index 1f707e320..3fe9e0003 100644 --- a/clickhouse-data/src/test/java/com/clickhouse/data/ClickHouseColumnTest.java +++ b/clickhouse-data/src/test/java/com/clickhouse/data/ClickHouseColumnTest.java @@ -302,6 +302,51 @@ public void testEnumWidth(String typeName, ClickHouseDataType expectedType) { Assert.assertEquals(variant.getNestedColumns().get(1).getDataType(), expectedType); } + @DataProvider(name = "enumImplicitValuesProvider") + private Object[][] getEnumImplicitValues() { + return new Object[][] { + { "Enum8('a', 'b')", ClickHouseDataType.Enum8, new String[] { "a", "b" }, new int[] { 1, 2 } }, + { "Enum16('a', 'b')", ClickHouseDataType.Enum16, new String[] { "a", "b" }, new int[] { 1, 2 } }, + { "Enum('a', 'b')", ClickHouseDataType.Enum8, new String[] { "a", "b" }, new int[] { 1, 2 } }, + { "Enum8('a' = 5, 'b', 'c')", ClickHouseDataType.Enum8, new String[] { "a", "b", "c" }, + new int[] { 5, 6, 7 } }, + { "Enum8('a' = -3, 'b')", ClickHouseDataType.Enum8, new String[] { "a", "b" }, new int[] { -3, -2 } }, + { "Enum8('a', 'b' = 5, 'c' = 9)", ClickHouseDataType.Enum8, new String[] { "a", "b", "c" }, + new int[] { 1, 5, 9 } }, + { "Enum('a' = 127, 'b')", ClickHouseDataType.Enum16, new String[] { "a", "b" }, + new int[] { 127, 128 } }, + { "Enum8('x=y', 'b')", ClickHouseDataType.Enum8, new String[] { "x=y", "b" }, new int[] { 1, 2 } }, + { "Enum8('it''s', 'b')", ClickHouseDataType.Enum8, new String[] { "it's", "b" }, new int[] { 1, 2 } }, + }; + } + + @Test(dataProvider = "enumImplicitValuesProvider", groups = { "unit" }) + public void testEnumImplicitValues(String typeName, ClickHouseDataType expectedType, String[] names, + int[] values) { + ClickHouseColumn column = ClickHouseColumn.of("e", typeName); + Assert.assertEquals(column.getDataType(), expectedType); + Assert.assertEquals(column.getEnumConstants().getNames(), names); + Assert.assertEquals(column.getEnumConstants().getValues(), values); + + ClickHouseColumn tuple = ClickHouseColumn.of("e", "Tuple(Int32, Nullable(" + typeName + "), Float64)"); + Assert.assertEquals(tuple.getNestedColumns().get(1).getEnumConstants(), column.getEnumConstants()); + Assert.assertEquals(tuple.getNestedColumns().get(2).getDataType(), ClickHouseDataType.Float64); + } + + @DataProvider(name = "enumMixedValuesProvider") + private Object[][] getEnumMixedValues() { + return new Object[][] { + { "Enum8('a' = 5, 'b', 'c' = 10, 'd')" }, + { "Enum8('a' = 1, 'b' = 2, 'c')" }, + { "Enum8('a', 'b' = 5, 'c')" }, + }; + } + + @Test(dataProvider = "enumMixedValuesProvider", groups = { "unit" }) + public void testEnumMixedValues(String typeName) { + Assert.assertThrows(IllegalArgumentException.class, () -> ClickHouseColumn.of("e", typeName)); + } + @Test(groups = { "unit" }) public void testBareEnumWidthFromDataType() { Assert.assertEquals(ClickHouseColumn.of("e", ClickHouseDataType.Enum, false, false, "'a'=1").getDataType(), 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..186614eb7 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 @@ -304,6 +304,10 @@ private Object[][] enumTypesInSchema() { {"ENUM('a' = -128, 'b' = 127)", "b"}, {"Variant(Bool, Enum('a' = 1, 'b' = 1000))", "b"}, {"Nullable(Enum('a' = 1, 'b' = 1000))", "a"}, + {"Enum8('a', 'b')", "b"}, + {"Enum8('a' = 5, 'b', 'c')", "c"}, + {"Enum('a' = 127, 'b')", "b"}, + {"Nullable(Enum8('x=y', 'b'))", "x=y"}, }; } diff --git a/history/latest/3344.md b/history/latest/3344.md new file mode 100644 index 000000000..c6346d2d8 --- /dev/null +++ b/history/latest/3344.md @@ -0,0 +1,9 @@ +- **[client-v2, jdbc-v2]** Fixed Enum type names that leave out the values, such as `Enum8('a', 'b')` or + `Enum8('a' = 5, 'b')`, in a type name that the application writes. This covers a `TableSchema` for + `Client#newBinaryFormatReader(response, schema)`, `RowBinaryFormatWriter`, `Client#register(Class, schema)`, + `Connection#createArrayOf` and `Connection#createStruct`. They failed with `Invalid enum entry`, and a quoted name + that contains `=` failed with `NumberFormatException`. Now the client numbers these entries the same way as the + server. An entry without a value gets the value of the first entry plus its position, and counting starts at 1 when + the first entry has no value. A list where only some of the entries after the first one have a value is rejected, + as on the server. Type names from the server always have explicit values and are read as before. + (https://github.com/ClickHouse/clickhouse-java/issues/3344)