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 @@ -16,12 +16,14 @@ public static ClickHouseEnum of(Class<? extends Enum> clazz) {
return EMPTY;
}

Enum<?>[] constants = clazz.getEnumConstants();
// A constant with a class body is an instance of an anonymous subclass of the enum class
Class<?> enumClass = clazz.isEnum() ? clazz : clazz.getSuperclass();
Enum<?>[] constants = (Enum<?>[]) enumClass.getEnumConstants();
int size = constants.length;
String[] names = new String[size];
int[] values = new int[size];
int i = 0;
for (Enum<?> e : clazz.getEnumConstants()) {
for (Enum<?> e : constants) {
names[i] = e.name();
values[i] = e.ordinal();
i++;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
package com.clickhouse.data.value;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.clickhouse.data.BaseClickHouseValueTest;
import com.clickhouse.data.ClickHouseDataType;
import com.clickhouse.data.ClickHouseEnum;

public class ClickHouseEnumValueTest extends BaseClickHouseValueTest {
enum EnumWithConstantBody {
PLAIN,
WITH_BODY {
@Override
public String toString() {
return "with body";
}
},
LAST
}

@DataProvider(name = "enumClassProvider")
private Object[][] getEnumClasses() {
String[] names = { "PLAIN", "WITH_BODY", "LAST" };
int[] values = { 0, 1, 2 };
return new Object[][] {
{ EnumWithConstantBody.class, names, values },
{ EnumWithConstantBody.WITH_BODY.getClass(), names, values },
{ null, new String[0], new int[0] },
{ String.class, new String[0], new int[0] } };
}

@Test(dataProvider = "enumClassProvider", groups = { "unit" })
public void testEnumTypeOfClass(Class<? extends Enum> clazz, String[] names, int[] values) {
ClickHouseEnum type = ClickHouseEnum.of(clazz);
Assert.assertEquals(type.getNames(), names);
Assert.assertEquals(type.getValues(), values);
}

@Test(groups = { "unit" })
public void testOfEnumWithConstantBody() {
ClickHouseEnumValue v = ClickHouseEnumValue.of(EnumWithConstantBody.WITH_BODY);
Assert.assertEquals(v.asInteger(), 1);
Assert.assertEquals(v.asString(), "WITH_BODY");
Assert.assertEquals(v.asEnum(EnumWithConstantBody.class), EnumWithConstantBody.WITH_BODY);
Assert.assertEquals(v.update(EnumWithConstantBody.LAST).asString(), "LAST");
}

@Test(groups = { "unit" })
public void testCopy() {
sameValue(ClickHouseEnumValue.ofNull(ClickHouseDataType.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,35 @@ public void testDynamicWithEnum8WithMoreThan127Constants() throws Exception {
Assert.assertEquals(records.get(0).getInteger("num"), 42);
}

@Test(groups = {"integration"})
public void testDynamicWithEnumConstantBody() throws Exception {
if (isVersionMatch("(,24.8]")) {
return;
}
final String table = "test_dynamic_enum_constant_body";
final int tail = 987654321;
client.execute("DROP TABLE IF EXISTS " + table).get();
client.execute(tableDefinition(table, "rowId Int32", "field Dynamic", "tail Int32"),
(CommandSettings) new CommandSettings().serverSetting("allow_experimental_dynamic_type", "1")).get();
client.register(DTOForDynamicDecimalTests.class, client.getTableSchema(table));

client.insert(table, Arrays.asList(
new DTOForDynamicDecimalTests(1, DataTypesTestingPOJO.EnumWithConstantBody.WITH_BODY, tail),
new DTOForDynamicDecimalTests(2, DataTypesTestingPOJO.EnumWithConstantBody.PLAIN, tail))).get().close();

List<GenericRecord> rows = client.queryAll(
"SELECT rowId, field, tail, dynamicType(field) AS type FROM " + table + " ORDER BY rowId");
Assert.assertEquals(rows.size(), 2);
String[] expectedNames = {"WITH_BODY", "PLAIN"};
for (int i = 0; i < rows.size(); i++) {
GenericRecord row = rows.get(i);
Assert.assertEquals(row.getInteger("rowId"), i + 1);
Assert.assertEquals(row.getString("field"), expectedNames[i]);
Assert.assertEquals(row.getInteger("tail"), tail);
Assert.assertEquals(row.getString("type"), "Enum8('PLAIN' = 0, 'WITH_BODY' = 1, 'LAST' = 2)");
}
}

@Test(groups = {"integration"})
public void testDynamicWithParametrizedTypesInsideMapAndTuple() throws Exception {
if (isVersionMatch("(,24.8]")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,17 @@ public enum SmallEnum {
CONSTANT_1, CONSTANT_2, CONSTANT_3, CONSTANT_4, CONSTANT_5, CONSTANT_6, CONSTANT_7, CONSTANT_8, CONSTANT_9, CONSTANT_10;
}

public enum EnumWithConstantBody {
PLAIN,
WITH_BODY {
@Override
public String toString() {
return "with body";
}
},
LAST
}

public enum LargeEnum {
CONSTANT_1, CONSTANT_2, CONSTANT_3, CONSTANT_4, CONSTANT_5, CONSTANT_6, CONSTANT_7, CONSTANT_8, CONSTANT_9, CONSTANT_10,
CONSTANT_11, CONSTANT_12, CONSTANT_13, CONSTANT_14, CONSTANT_15, CONSTANT_16, CONSTANT_17, CONSTANT_18, CONSTANT_19, CONSTANT_20,
Expand Down
7 changes: 7 additions & 0 deletions history/latest/3374.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- **[client-v2]** Fixed an insert of a Java `enum` constant with a class body into a `Dynamic` column. The insert
failed on the client with `NullPointerException: Cannot read the array length because "constants" is null`. The
class of such a constant is an anonymous subclass of the enum class and has no enum constants of its own, but
`ClickHouseEnum.of(Class)` read the constants from that class. Now `ClickHouseEnum.of(Class)` reads them from the
enum class, so the value gets the same type in the `Dynamic` column as the other constants of the enum.
`ClickHouseEnumValue.of(Enum)` failed the same way for such a constant and works now too.
(https://github.com/ClickHouse/clickhouse-java/issues/3374)
Loading