Repository navigation
Fix client-v2: size a bare Enum type name from its values - #3345
Conversation
A type name that the application writes (for example in a TableSchema for newBinaryFormatReader, RowBinaryFormatWriter or register) did not get the enum width that the server uses. enum(...)/ENUM(...) matched the ENUM alias of Enum8 and were always 1 byte, so a value outside -128..127 used 1 of its 2 bytes and shifted the rest of the row. Enum(...) parsed to the width-less Enum type, which the reader and the writer do not support. ClickHouseColumn.update() now resolves a bare Enum with values the same way as the server: Enum8 when all values fit into Int8, else Enum16. readColumn matches Enum, Enum8 and Enum16 without case sensitivity. EnumValue gets the type that the value was read with (getDataType()). Fixes: #3342 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Auto-approved by the AI review gate on 530c1a7. Details in the approver's comment on this pull request.
AI review gate — auto-approved
History (latest 5 heads, newest first)
|
Client V2 CoverageCoverage Report
Class Coverage
|
JDBC V2 CoverageCoverage Report
Class Coverage
|
…into polyglot/bare-enum-width # Conflicts: # client-v2/src/test/java/com/clickhouse/client/api/data_formats/RowBinaryTest.java
TriageCategory: Summary What this impacts
Concerns
Required reviewer action
|
There was a problem hiding this comment.
Auto-approved by the AI review gate on 1e77956. Details in the approver's comment on this pull request.
chernser
left a comment
There was a problem hiding this comment.
Please see the comments and update
…itive helper
Review feedback: remove the typeOf() helper in ClickHouseColumn and use
ClickHouseDataType.of() as before. Without the helper, enum(...) and
ENUM(...) resolved through the alias ENUM, which was on Enum8, so
enum('a' = 1000) stayed Enum8. Move the alias ENUM to Enum, as in the
server's system.data_type_families (ENUM alias_to Enum). Then
ClickHouseColumn.update() sizes it from its values.
enum8(...)/enum16(...) in lower case are not accepted any more (server
25.8 also rejects them).
Fixes: #3342
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AI review gate — Human approval in place of AI reviewApproved on the head commit by chernser (write access, not the author), standing in for the AI review:
What to do:
Evaluated at |
There was a problem hiding this comment.
Auto-approved by the AI review gate on db57370. Details in the approver's comment on this pull request.
Summary
Fixes #3342 (original report: ClickHouse/clickhouse-js#1130).
A type name that the application writes (for example
ClickHouseColumn.of("e", "enum('a' = 1000)")in aTableSchemaforClient#newBinaryFormatReader(response, schema),RowBinaryFormatWriterorClient#register(Class, schema)) did not get the enum width that the server uses:enum(...)/ENUM(...)matched the aliasENUMofClickHouseDataType.Enum8(the lookup inClickHouseDataType.of()falls back to the upper-case name). So the client always read and wrote 1 byte. The server typesenum('a' = 1000)asEnum16, so the reader used 1 of the 2 bytes and the next column read the high byte: 2 server rows gave 3 client rows, without an error. A write of a value outside -128..127 failed theInt8range check, and a small value into anEnum16column wrote 1 byte (CANNOT_READ_ALL_DATAon the server).Enum(...)parsed to the width-lessClickHouseDataType.Enum.BinaryStreamReader.readValueandSerializerUtils.serializeDatahave no case for it, so reads and writes failed withUnsupported data type: Enum, also forEnum('a' = 1).enum8(...)/enum16(...)failed withUnknown data type. The server accepts them.The server sizes a bare
Enumfrom its values:Enum8when all values are in -128..127, elseEnum16(toTypeName(CAST(128, 'Enum(''a'' = 128)'))=Enum16('a' = 128),toTypeName(CAST(127, 'Enum(''a'' = 127, ''b'' = -128)'))=Enum8(...)).Type names from the server (
RowBinaryWithNamesAndTypes/Nativeheader,getTableSchema, theDynamicbinary type encoding) always spellEnum8/Enum16. They are not affected and are read as before.Changes
ClickHouseColumn.update(): a bareEnumwith values becomesEnum8when all its values fit intoInt8, elseEnum16(the server rule).valueDataType,isFixedLength()andgetEstimatedLength()are set as for an explicitEnum8/Enum16. ExplicitEnum8/Enum16keep their width. A bareEnumwithout values (the type family, which v1ClickHouseDatabaseMetaData#getTypeInfo()builds) is not changed.ClickHouseColumn.readColumn(): the namesEnum,Enum8andEnum16are matched without case sensitivity. Soenum(...)/ENUM(...)are a bareEnum(and get the width from their values, as on all server versions), andenum16(...)isEnum16(the server acceptsenum8/enum16since 26.x: 26.3 does, 25.8 does not). Other type names are resolved as before.BinaryStreamReader.EnumValue: new fielddataTypeandgetDataType()(Enum8orEnum16), set byreadValuefrom the column type that the value was read with. The existing constructorEnumValue(String, int)is kept; it uses the smallest Enum type that holds the value.history/latest/3342.md: changelog entry.clickhouse-datais shared, so the parser change also applies to jdbc-v2 and v1. Each input that changes behavior failed or returned wrong data before.Tests
ClickHouseColumnTest#testEnumWidth(unit,@DataProvider): the width for each spelling (Enum,enum,ENUM,enum8,enum16) and the boundaries -128/127 (Enum8) and 128/-129 (Enum16), plusNullable,Array,Map,TupleandVariantaround the enum. Contrast rows: explicitEnum8(...)/Enum16('a' = 1)keep their width.testBareEnumWidthFromDataTypecoversClickHouseColumn.of(name, ClickHouseDataType.Enum, ...).RowBinaryTest#testReadEnumWithSchemaFromTypeName(integration):RowBinaryread with a hand-written schema[e <enum type>, u UInt8], 2 server rows. Asserts the name, the value andEnumValue#getDataType()of each row, thatuis 7 (no row shift), and that there is no third row.RowBinaryFormatWriterTest#writeEnumWithSchemaFromTypeNameTest(integration):RowBinaryFormatWriterwith a hand-written schema[id, e <enum type>, tail UInt8], then reads the stored row back. IncludesVariant(Bool, Enum('a' = 1, 'b' = 1000)): aStringvalue now goes to the enum variant, as for an explicitEnum16.DataTypeTests#testVariantWithEnum16(integration):Variant(Bool, Enum('a' = 1, 'b' = 1000))read with a hand-written schema (RowBinary, also spelledENUM(...)) and with the schema from the server; both enum rows reportEnum16with the correct name and value, theBoolrow readstrue, and the next column stays intact.DataTypeTests#testDynamicWithEnum(integration):Enum8andEnum16values in aDynamiccolumn (values in and out of theInt8range, a small value of a wide enum) report the correct name, value andEnumValue#getDataType().Expected values come from the server (
toTypeName(CAST(...)), ClickHouse 26.9.1.1629). The integration tests send only spellings that every CI server version accepts; the lowercaseenum8/enum16spellings are covered by the unit test.Without the fix, 12 of the 21 new integration rows fail (
Unsupported data type: Enum,expected [Enum16] but found [Enum8],expected [a] but found [<unknown>], theInt8range check on write, noVariantalternative for the value), and 8 unit rows fail. The rows that pass without the fix are the explicit-spelling contrast rows, the server-schemaVariantrow and theDynamicrows (that path was already correct; these rows pingetDataType()).One existing test changes:
ClickHouseColumnTest#testEnumasserted thatEnum(...)parses to the width-lessEnumtype, which is the bug. It now expectsEnum8forEnum('Query''Start' = 1, 'Query\'Finish' = 10)(both values fit intoInt8), and its fixed-length check now applies to all three spellings.Local runs (devbox, ClickHouse 26.9.1.1629):
clickhouse-dataunit 1701 (0 failures),client-v2unit 832/832,client-v2integrationRowBinaryTest,DataTypeTests,RowBinaryFormatWriterTest,MetadataTests,QueryTests,InsertTests374/374 (the new tests also pass on ClickHouse 25.8.33.6);jdbc-v2unit 2002/2002 and integrationJdbcDataTypeTests,DatabaseMetaDataTest,ResultSetImplTest150/150;clickhouse-jdbc(v1) unit 126/126.Compatibility
EnumValuegets one field, one getter and one constructor (additive).ClickHouseColumn#getDataType()changes only for application-written type names that failed or returned wrong data before: a bareEnum(...)with values (wasEnum),enum(...)/ENUM(...)with a value outside -128..127 (wasEnum8), andenum8/enum16in another letter case (was an exception).Checklist (docs/changes_checklist.md)
EnumValue#getDataType(),EnumValue(String, int, ClickHouseDataType)): name and style follow the existinggetName(); additive; covered by the integration tests above.testEnum.docs/features.md: no change, because no documented feature changes; this restores the server rule for a bareEnum.🤖 Generated with Claude Code
Note
Medium Risk
Changes type parsing and binary Enum width for hand-written schemas; wrong boundaries could still misalign RowBinary, but behavior now matches the server and is heavily tested.
Overview
Fixes Enum width when the app supplies type names (e.g. in
TableSchemafor RowBinary read/write): bareEnum(...)now becomesEnum8orEnum16from the declared numeric values (same rule as ClickHouse), instead of staying width-less or being forced toEnum8via theENUMalias.ClickHouseColumnpromotes bareEnumwith constants using a newisInt8check and updatesvalueDataType/ fixed byte length.ClickHouseDataTypemoves theENUMalias fromEnum8toEnum.BinaryStreamReader.EnumValuecarries the resolvedEnum8/Enum16type viadataTypeandgetDataType().Unit and integration tests cover spellings, boundaries, nested types, Variant/Dynamic, and round-trip RowBinary; changelog in
history/latest/3342.md.Reviewed by Cursor Bugbot for commit db57370. Bugbot is set up for automated code reviews on this repo. Configure here.