Skip to content
Closed
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 @@ -39,17 +39,66 @@ protected ClickHouseEnum(Collection<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
};
}

Expand Down
9 changes: 9 additions & 0 deletions history/latest/3344.md
Original file line number Diff line number Diff line change
@@ -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)
Loading