Skip to content

[client-v2] NullPointerException when a Java enum constant with a class body is written to a Dynamic column #3374

Open

Description

@Indian-boult

Description

ClickHouseEnum.of(Class) throws NullPointerException for the class of an enum constant that has a class body.
The class of such a constant is an anonymous subclass of the enum class. Class.getEnumConstants() returns null
for that subclass, and ClickHouseEnum.of reads the length of the returned array.

Two callers pass the class of an enum value to ClickHouseEnum.of. SerializerUtils.enumValue2Column in client-v2
does it when a Java enum value is written to a Dynamic column. ClickHouseEnumValue.of(ClickHouseValue, Enum) in
clickhouse-data does it too, and ClickHouseEnumValue.of(Enum) calls that method. So a POJO insert of such a
constant into a Dynamic column fails on the client, and ClickHouseEnumValue.of(Enum) fails for such a constant.
A constant of the same enum without a class body works.

Steps to reproduce

  1. Create a table with a Dynamic column and an Int32 column after it.
  2. Register a POJO for the table with an Object field for the Dynamic column.
  3. Insert a POJO whose Dynamic field holds an enum constant with a class body.

Error Log or Exception StackTrace

The frames of the test runner and of the HTTP client are left out.

com.clickhouse.client.api.ClickHouseException: Insert request failed (attempt: 1, duration: 2ms, queryId: null)
	at com.clickhouse.client.api.internal.HttpAPIClientHelper.wrapException(HttpAPIClientHelper.java:1246)
	at com.clickhouse.client.api.Client.lambda$insert$1(Client.java:1665)
	at com.clickhouse.client.api.Client.runAsyncOperation(Client.java:2542)
	at com.clickhouse.client.api.Client.insert(Client.java:1697)
	at com.clickhouse.client.api.Client.insert(Client.java:1549)
Caused by: java.lang.NullPointerException: Cannot read the array length because "constants" is null
	at com.clickhouse.data.ClickHouseEnum.of(ClickHouseEnum.java:20)
	at com.clickhouse.client.api.data_formats.internal.SerializerUtils.enumValue2Column(SerializerUtils.java:370)
	at com.clickhouse.client.api.data_formats.internal.SerializerUtils.valueToColumnForDynamicType(SerializerUtils.java:284)
	at com.clickhouse.client.api.data_formats.internal.SerializerUtils.serializeData(SerializerUtils.java:121)
	at com.clickhouse.client.api.serde.POJOSerDe.lambda$registerClass$0(POJOSerDe.java:66)
	at com.clickhouse.client.api.Client.lambda$insert$0(Client.java:1644)

ClickHouseEnumValue.of(Enum) fails without a server.

java.lang.NullPointerException: Cannot read the array length because "constants" is null
	at com.clickhouse.data.ClickHouseEnum.of(ClickHouseEnum.java:20)
	at com.clickhouse.data.value.ClickHouseEnumValue.of(ClickHouseEnumValue.java:109)
	at com.clickhouse.data.value.ClickHouseEnumValue.of(ClickHouseEnumValue.java:65)

Expected Behaviour

The insert works. The value gets the same Enum8 type as a constant of the enum without a class body, here
Enum8('PLAIN' = 0, 'WITH_BODY' = 1, 'LAST' = 2), and the row reads back with the label WITH_BODY and the
Int32 value after it unchanged. ClickHouseEnum.of(Class) returns the constants of the enum class for the class of
any of its constants, and ClickHouseEnumValue.of(Enum) works for every constant.

Code Example

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

@Data
@AllArgsConstructor
public static class Row {
    private int rowId;
    private Object field;
    private int tail;
}

String table = "test_dynamic_enum_constant_body";
client.execute("CREATE TABLE " + table + " ( rowId Int32, field Dynamic, tail Int32) ORDER BY ()",
        (CommandSettings) new CommandSettings().serverSetting("allow_experimental_dynamic_type", "1")).get();
client.register(Row.class, client.getTableSchema(table));
client.insert(table, Collections.singletonList(new Row(1, EnumWithConstantBody.WITH_BODY, 987654321))).get().close();

// Both calls fail the same way without a server
ClickHouseEnum.of(EnumWithConstantBody.WITH_BODY.getClass());
ClickHouseEnumValue.of(EnumWithConstantBody.WITH_BODY);

Configuration

Client Configuration

Client client = new Client.Builder()
        .addEndpoint(Protocol.HTTP, "127.0.0.1", 8123, false)
        .setUsername("default")
        .setPassword(password)
        .setDefaultDatabase(database)
        .serverSetting(ServerSettings.WAIT_END_OF_QUERY, "1")
        .serverSetting(ServerSettings.WAIT_ASYNC_INSERT, "1")
        .build();

Environment

  • Cloud
  • Client version: main at commit 75063012, version 0.12.0-rc1-SNAPSHOT
  • Language version: Java 21, Temurin 21.0.7
  • OS: macOS, Darwin 27.2.0

ClickHouse Server

  • ClickHouse Server version: 26.10.4.207
  • ClickHouse Server non-default settings, if any: allow_experimental_dynamic_type=1 for the CREATE TABLE
  • CREATE TABLE statements for tables involved: CREATE TABLE test_dynamic_enum_constant_body ( rowId Int32, field Dynamic, tail Int32) ORDER BY ()
  • Sample data for all these tables, use clickhouse-obfuscator if necessary: no data is needed because the insert fails on the client

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions