From 49854d314fa1ee4acc6c3634682e64c94a584b98 Mon Sep 17 00:00:00 2001 From: Indian-boult Date: Sat, 21 Nov 2026 16:04:44 +0530 Subject: [PATCH] Reject Enum type names with a repeated value or name ClickHouseEnum did not check for repeated values or names, so a type like Enum8('a' = 1, 'b' = 1) was accepted. Both labels were written with the same value and 'b' read back as 'a'. The server rejects these types. Check both constructors and throw IllegalArgumentException that names the two entries. Closes #3349 --- .../com/clickhouse/data/ClickHouseEnum.java | 21 ++++++++++++ .../clickhouse/data/ClickHouseColumnTest.java | 34 +++++++++++++++++++ history/latest/3349.md | 6 ++++ 3 files changed, 61 insertions(+) create mode 100644 history/latest/3349.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..d506f41c2 100644 --- a/clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseEnum.java +++ b/clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseEnum.java @@ -4,6 +4,8 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; public class ClickHouseEnum implements Serializable { @@ -50,6 +52,24 @@ protected ClickHouseEnum(Collection params) { } i++; } + checkUnique(names, values); + } + + private static void checkUnique(String[] names, int[] values) { + Map valueByName = new HashMap<>(); + Map nameByValue = new HashMap<>(); + for (int i = 0; i < names.length; i++) { + Integer previousValue = valueByName.put(names[i], values[i]); + if (previousValue != null) { + throw new IllegalArgumentException("Duplicate enum name '" + names[i] + "' for values " + + previousValue + " and " + values[i]); + } + String previousName = nameByValue.put(values[i], names[i]); + if (previousName != null) { + throw new IllegalArgumentException("Duplicate enum value " + values[i] + " for names '" + + previousName + "' and '" + names[i] + "'"); + } + } } public ClickHouseEnum(String[] names, int[] values) { @@ -58,6 +78,7 @@ public ClickHouseEnum(String[] names, int[] values) { } else if (names.length != values.length) { throw new IllegalArgumentException("Names and values should have same length"); } + checkUnique(names, values); this.size = names.length; this.names = names; 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..812b27829 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,40 @@ public void testEnumWidth(String typeName, ClickHouseDataType expectedType) { Assert.assertEquals(variant.getNestedColumns().get(1).getDataType(), expectedType); } + @DataProvider(name = "enumDuplicatesProvider") + private Object[][] getEnumDuplicates() { + return new Object[][] { + { "Enum8('a' = 1, 'b' = 1)", "Duplicate enum value 1 for names 'a' and 'b'" }, + { "Enum16('a' = 1, 'a' = 2)", "Duplicate enum name 'a' for values 1 and 2" }, + { "Enum('a' = 1, 'b' = 2, 'a' = 3)", "Duplicate enum name 'a' for values 1 and 3" }, + { "Enum8('x=y' = 5, 'b' = 5)", "Duplicate enum value 5 for names 'x=y' and 'b'" }, + }; + } + + @Test(dataProvider = "enumDuplicatesProvider", groups = { "unit" }) + public void testEnumDuplicates(String typeName, String message) { + IllegalArgumentException e = Assert.expectThrows(IllegalArgumentException.class, + () -> ClickHouseColumn.of("e", typeName)); + Assert.assertEquals(e.getMessage(), message); + Assert.assertThrows(IllegalArgumentException.class, + () -> ClickHouseColumn.of("e", "Tuple(Int32, Nullable(" + typeName + "))")); + } + + @Test(groups = { "unit" }) + public void testEnumDuplicatesFromArrays() { + Assert.assertThrows(IllegalArgumentException.class, + () -> new ClickHouseEnum(new String[] { "a", "b" }, new int[] { 1, 1 })); + Assert.assertThrows(IllegalArgumentException.class, + () -> new ClickHouseEnum(new String[] { "a", "a" }, new int[] { 1, 2 })); + } + + @Test(groups = { "unit" }) + public void testEnumNamesThatDifferOnlyInCase() { + ClickHouseEnum constants = ClickHouseColumn.of("e", "Enum8('a' = 1, 'A' = 2)").getEnumConstants(); + Assert.assertEquals(constants.value("a"), 1); + Assert.assertEquals(constants.value("A"), 2); + } + @Test(groups = { "unit" }) public void testBareEnumWidthFromDataType() { Assert.assertEquals(ClickHouseColumn.of("e", ClickHouseDataType.Enum, false, false, "'a'=1").getDataType(), diff --git a/history/latest/3349.md b/history/latest/3349.md new file mode 100644 index 000000000..45826a19e --- /dev/null +++ b/history/latest/3349.md @@ -0,0 +1,6 @@ +- **[client-v2, jdbc-v2]** Fixed Enum type names with a repeated value or name, such as `Enum8('a' = 1, 'b' = 1)` or + `Enum8('a' = 1, 'a' = 2)`, in a type name that the application writes. The client accepted them, so both labels + were written with the same value and the second one read back as the first. The server rejects these types. Now + the client rejects them too with an `IllegalArgumentException` that names both entries. The same check applies to + `new ClickHouseEnum(String[], int[])`. Type names from the server never repeat a value or name and are read as + before. (https://github.com/ClickHouse/clickhouse-java/issues/3349)