Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -50,6 +52,24 @@ protected ClickHouseEnum(Collection<String> params) {
}
i++;
}
checkUnique(names, values);
}

private static void checkUnique(String[] names, int[] values) {
Map<String, Integer> valueByName = new HashMap<>();
Map<Integer, String> 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) {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
6 changes: 6 additions & 0 deletions history/latest/3349.md
Original file line number Diff line number Diff line change
@@ -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)