Skip to content

[client-v2] Validate Enum8/Enum16 values on insert - #3312

Open
jaya232 wants to merge 2 commits into
ClickHouse:mainfrom
jaya232:fix/3304-enum-serialization-validation
Open

jaya232 wants to merge 2 commits into
ClickHouse:mainfrom
jaya232:fix/3304-enum-serialization-validation

Conversation

@jaya232

@jaya232 jaya232 commented Nov 7, 2026 •

Copy link
Copy Markdown

Closes #3304

Problem

SerializerUtils.serializeEnumData() only validated the String form of an Enum8/Enum16 value. Two other paths wrote data without checking:

  • a Number was written as-is, even if it is not a member of the column's enum (e.g. 7 into Enum8('active' = 1, 'inactive' = 2))
  • a Java Enum was written using ordinal(), which is the declaration index in Java and not the value declared in the column (Status.active ended up as 0 instead of 1)

The server accepts such rows, but reading them back as text then fails with UNKNOWN_ELEMENT_OF_ENUM.

Fix

  • Number: validated with ClickHouseEnum.validate(int), so an unknown value fails on the client with Unknown enum value: 7
  • Java Enum: mapped by name with ClickHouseEnum.value(name()), so it is written as the declared value, and a constant that is not declared in the column fails

Tests

Tests
Integration tests (they need Docker, so they run with the integration group):

  • client-v2 DataTypeTests: a String, a valid member number and a Java enum are inserted into Enum8 and Enum16 columns with non-sequential declared values (5/10 and 500/1000), and both the name and the stored integer are checked. A non-member number and a Java enum constant not declared in the column fail with Unknown enum value: 7 / Unknown enum name: active.
  • jdbc-v2 JdbcDataTypeTests: with beta.row_binary_for_simple_insert enabled, a valid member number is inserted into Enum8/Enum16 and read back, and a non-member number fails with Unknown enum value: 7.

I ran them locally with Docker. Without the fix, the non-member number test in jdbc-v2 fails (nothing is thrown); with the fix, all of them pass.

Compatibility

Behavior change: values that used to be written silently (non-member numbers, Java enums via ordinal()) now throw IllegalArgumentException. Valid strings, valid member numbers and valid Java enum names are unchanged.

Added history/latest/3304.md.


Note

Medium Risk
This is a deliberate compatibility change: inserts that previously wrote non-member numbers or Java enums via ordinal() now throw on the client, which may break callers but prevents silent bad enum data.

Overview
Enum8/Enum16 client writes are now validated and mapped consistently in SerializerUtils.serializeEnumData(). Numeric inputs must be a declared enum member (via ClickHouseEnum.validate); otherwise the client fails with messages like Unknown enum value: 7 instead of writing invalid bytes the server would accept. Java enum constants are resolved by name to the column's declared value, not by ordinal(), so inserts match ClickHouse semantics and avoid UNKNOWN_ELEMENT_OF_ENUM on read.

Integration coverage was added for client-v2 (DataTypeTests) and jdbc-v2 row-binary inserts (JdbcDataTypeTests), plus a history note for issue #3304.

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

@jaya232
jaya232 force-pushed the fix/3304-enum-serialization-validation branch from eabfd41 to daa3d48 Compare November 7, 2026 06:36
enumValue = column.getEnumConstants().validate(((Number) value).intValue());
} else if (value instanceof Enum<?>) {
enumValue = ((Enum<?>)value).ordinal();
enumValue = column.getEnumConstants().value(((Enum<?>) value).name());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is integer changed to string. Would it work?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does work. ClickHouseEnum.value(String) looks up the declared integer for that name (for example ‘inactive’ → 10), so the Java enum is converted by name and the integer is what gets written. Before this change it wrote ordinal(), which is wrong whenever the declared values differ from the position. The new integration tests use values like 5/10 and 500/1000 to check this.

private enum Status { active, inactive }

@Test(dataProvider = "nonNullableEnumTypes")
public void testNonMemberNumberIntoEnumThrows(String typeName) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please convert to integration tests and place in DataTypesTests (client and JDBC)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to DataTypeTests. They cover String, number and Java enum inputs for Enum8 and Enum16 (checking both the name and the stored integer), and the invalid number / undeclared name cases.Added to JdbcDataTypeTests with the row-binary writer enabled, so the call actually reaches serializeEnumData. One test inserts a valid number, one checks that a non-member number fails and Removed the unit tests I had added in SerializerUtilsTest. please check updated changes

@chernser chernser left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution!

please see comments.
integration tests should be added in both client-v2 and jdbc-v2

Thanks!

@security-ai-gate-reviewer

security-ai-gate-reviewer Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

AI review gate — the gate never ran on this head

The org-required gate workflow has no run on daa3d4812c1d: this pull request was open before the repository was enrolled, and GitHub starts that workflow only on opened, synchronize and reopened. Nothing here can merge until it runs.

What to do: a maintainer runs rollout kick-gate --repo ClickHouse/clickhouse-java --pr 3312 --apply from infra/github/conformance in security-integrations (dry run without --apply), or closes and reopens this pull request (auto-merge is turned off by the close; re-enable it after).

History (latest 5 heads, newest first)

  • daa3d4812c1d gate never ran — needs a kick

@chernser

Copy link
Copy Markdown
Contributor

@jaya232

Thank you for the update!
I've commented in the original issue - WDYT?

So current change is good and looks correct. However we need to handle it properly.

  • we should not probably validate number. In some cases it can be useful when newer software version need to write to old definition.
  • we should write a migration guide.
  • we need to add temp. feature flag to let people migrate to the new logic.

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] Enum8/Enum16 insert accepts a non-member integer and uses Enum.ordinal(), silently storing values that are not enum members

2 participants