Skip to content

[client-v2] Fix NullPointerException for an enum constant with a class body in a Dynamic column - #3375

Open
Indian-boult wants to merge 1 commit into
ClickHouse:mainfrom
Indian-boult:fix/enum-constant-body
Open

Indian-boult wants to merge 1 commit into
ClickHouse:mainfrom
Indian-boult:fix/enum-constant-body

Conversation

@Indian-boult

@Indian-boult Indian-boult commented Nov 18, 2026 •

Copy link
Copy Markdown

Summary

A Java enum constant with a class body failed on insert into a Dynamic column 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. SerializerUtils.enumValue2Column passes that class to ClickHouseEnum.of(Class),
and getEnumConstants() returns null for it. ClickHouseEnumValue.of(Enum) passes the same class and failed the
same way.

ClickHouseEnum.of(Class) now reads the constants from the superclass when the given class is not an enum class
itself, the same idea as Enum.getDeclaringClass(). The change is inside of() only, so both callers are fixed and
SerializerUtils does not change.

There is no change to the public API. A null class or a class that does not extend Enum still gives
ClickHouseEnum.EMPTY. An enum class or a constant without a class body gives the same result as before. A constant
with a class body now gets the same type as the other constants of its enum and no longer throws.

ClickHouseEnumValueTest has a new data provider test for ClickHouseEnum.of(Class) with the enum class, the class of
a constant with a class body, null and a class that is not an enum, and a new test for ClickHouseEnumValue.of(Enum)
with such a constant. DataTypeTests#testDynamicWithEnumConstantBody inserts POJOs whose Dynamic field holds a
constant with a class body and a constant without one, with an Int32 column after the field. It reads the whole rows
back and checks dynamicType on the server. The new tests fail without the change in ClickHouseEnum and pass with it.

Closes #3374

Checklist

Delete items not relevant to your PR:


Note

Low Risk
Small, localized change in enum type resolution with new tests; no public API change and ordinary enums behave the same.

Overview
Fixes NullPointerException when inserting or wrapping a Java enum constant that declares a class body (e.g. into a Dynamic column). Those values use an anonymous subclass Class, where getEnumConstants() is null; ClickHouseEnum.of(Class) now resolves constants from the declaring enum type (clazz.isEnum() ? clazz : clazz.getSuperclass()), matching normal enum constants.

Adds unit coverage in ClickHouseEnumValueTest and an integration test testDynamicWithEnumConstantBody, plus changelog history/latest/3374.md.

Reviewed by Cursor Bugbot for commit 98473b3. Bugbot is set up for automated code reviews on this repo. Configure here.

A Java enum constant with a class body failed on insert into a Dynamic
column 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. SerializerUtils.enumValue2Column passes
that class to ClickHouseEnum.of, and getEnumConstants() returns null
for it. ClickHouseEnumValue.of(Enum) passes the same class and failed
the same way.

ClickHouseEnum.of now reads the constants from the superclass when the
given class is not an enum class itself, the same idea as
Enum.getDeclaringClass(). A null class or a class that does not extend
Enum still gives ClickHouseEnum.EMPTY.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant