Repository navigation
[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
Open
[client-v2] Fix NullPointerException for an enum constant with a class body in a Dynamic column#3375Indian-boult wants to merge 1 commit into
Indian-boult wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A Java enum constant with a class body failed on insert into a
Dynamiccolumn withNullPointerException: Cannot read the array length because "constants" is null. The class of such a constant is ananonymous subclass of the enum class.
SerializerUtils.enumValue2Columnpasses that class toClickHouseEnum.of(Class),and
getEnumConstants()returnsnullfor it.ClickHouseEnumValue.of(Enum)passes the same class and failed thesame way.
ClickHouseEnum.of(Class)now reads the constants from the superclass when the given class is not an enum classitself, the same idea as
Enum.getDeclaringClass(). The change is insideof()only, so both callers are fixed andSerializerUtilsdoes not change.There is no change to the public API. A null class or a class that does not extend
Enumstill givesClickHouseEnum.EMPTY. An enum class or a constant without a class body gives the same result as before. A constantwith a class body now gets the same type as the other constants of its enum and no longer throws.
ClickHouseEnumValueTesthas a new data provider test forClickHouseEnum.of(Class)with the enum class, the class ofa constant with a class body,
nulland a class that is not an enum, and a new test forClickHouseEnumValue.of(Enum)with such a constant.
DataTypeTests#testDynamicWithEnumConstantBodyinserts POJOs whoseDynamicfield holds aconstant with a class body and a constant without one, with an
Int32column after the field. It reads the whole rowsback and checks
dynamicTypeon the server. The new tests fail without the change inClickHouseEnumand 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
NullPointerExceptionwhen inserting or wrapping a Java enum constant that declares a class body (e.g. into aDynamiccolumn). Those values use an anonymous subclassClass, wheregetEnumConstants()isnull;ClickHouseEnum.of(Class)now resolves constants from the declaring enum type (clazz.isEnum() ? clazz : clazz.getSuperclass()), matching normal enum constants.Adds unit coverage in
ClickHouseEnumValueTestand an integration testtestDynamicWithEnumConstantBody, plus changeloghistory/latest/3374.md.Reviewed by Cursor Bugbot for commit 98473b3. Bugbot is set up for automated code reviews on this repo. Configure here.