ClickHouseIO: Add Decimal(P, S) support for fixed-point columns - #39846
Open
tomnewton wants to merge 2 commits into
Open
ClickHouseIO: Add Decimal(P, S) support for fixed-point columns#39846tomnewton wants to merge 2 commits into
tomnewton wants to merge 2 commits into
Conversation
tomnewton
force-pushed
the
clickhouse-decimal-support
branch
from
August 21, 2026 20:40
17c461f to
8a9bece
Compare
Contributor
|
Assigning reviewers: R: @ahmedabu98 for label java. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
BinaryStreamUtils.writeDecimal only range-checks against the backing storage width (32/64/128/256 bits, selected from the precision), which is wider than the declared type, and ClickHouse's RowBinary reader does not re-check. A value like 100000 was therefore written and stored in a Decimal(5, 0) column whose declared maximum is 99999. Truncate to the column scale toward zero, as before, then bound the resulting unscaled integer by the declared precision and throw IllegalArgumentException when it does not fit. The bound applies to the unscaled integer at the column scale, so Decimal(5, 2) admits values up to 999.99, and the check runs after truncation so 999.999 remains valid. Removes the integration test that pinned the previous lenient behavior; the rejection is now covered by unit tests.
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.
ClickHouseIO's
TableSchemaand column-type parser had no notion of ClickHouse's fixed-pointDecimalfamily, soClickHouseIO.getTableSchema()failed on theDESCRIBE TABLEoutput of any table containing a Decimal column and pipelines carrying monetary/fixed-point values could not write to ClickHouse at all.This change adds first-class
Decimal(P, S)support toClickHouseIO:TypeName.DECIMAL;ColumnTypecarriesscalealongside the existingprecision(validated: P in [1, 76], S in [0, P]), with aColumnType.decimal(precision, scale)factory. BareDecimalisDecimal(10, 0)andDecimal(P)isDecimal(P, 0), matching ClickHouse's defaults.Decimal[(P[, S])]and the width aliasesDecimal32(S)/Decimal64(S)/Decimal128(S)/Decimal256(S)(pinned to precisions 9/18/38/76 per ClickHouse's synonym rule;DESCRIBE TABLEcanonicalizes the aliases toDecimal(P, S)anyway). Reachable throughNullable(...)andArray(...)via the existingprimitive()rule. Lexical, syntactic and range-validation failures all surface as the parser's uniformfailed to parseerror.Decimal(P, S)maps toFixedPrecisionNumeric.of(P, S)(base typeDECIMAL, values areBigDecimal), preserving the declared precision/scale throughgetEquivalentSchemaand giving an early, loud failure at Row construction for values whose digits genuinely exceed the declared precision. This is the same logical type JdbcIO uses for NUMERIC.BinaryStreamUtils.writeDecimal. Fractional digits beyond the column scale are truncated toward zero, consistent with ClickHouse's own "excessive digits in a fraction are discarded (not rounded)" semantics. The truncated value is then bounded by the declared precision and rejected with anIllegalArgumentExceptionif out of range:writeDecimalalone only range-checks against the backing storage width, which is wider than the declared type, and ClickHouse's RowBinary reader does not re-check — so without this a value like100000would be stored in aDecimal(5, 0)column whose declared maximum is99999.ColumnType.parseDefaultExpressionhandles Decimal literals (DEFAULT 1.23), sogetTableSchemano longer throws for tables with defaulted Decimal columns, andwriteRow's existing null-substitution works for them.Tests:
TableSchemaTest— parser cases forDecimal(10, 2),Decimal(5), bareDecimal, all four width aliases,Nullable(Decimal(10, 2)),Array(Decimal(38, 10)); uniformfailed to parseerrors forDecimal(77, 2),Decimal(0),Decimal(9, 10),Decimal(9, -1)andDecimal(abc); factory range validation; schema-mapping tests including nullable; a test pinning that Row construction under the mappedFixedPrecisionNumerictype rejects values exceeding the declared precision (the layer that guards the declared range, since the writer checks only the storage width);parseDefaultExpressionfor positive and negative literals.ClickHouseWriterTest— byte-level tests locking the wire format for every width bucket: little-endian unscaled Int32, scaling below the column scale, 16 × 0xFF two's-complement sign extension for a negative Decimal128, a 32-byte Decimal256 encoding, truncation-toward-zero of excess fractional digits, storage-width overflow rejection, and null/non-null throughwriteNullableValue. Declared-precision enforcement is covered by rejecting100000inDecimal(5, 0)(which fits the 32-bit storage width) while accepting99999, rejecting1000.00inDecimal(5, 2)while accepting999.99(the bound applies to the unscaled integer at the column scale), and confirming the check runs on the truncated value so999.999inDecimal(5, 2)still succeeds.ClickHouseIOIT— round-trip integration tests against the ClickHouse test container forDecimal(9, 2),Decimal(18, 4),Decimal(38, 10)andDecimal(76, 20)(including negative values),Nullable(Decimal(10, 2))with a null row,Array(Decimal(9, 2)), aDEFAULT 2.25column exercised end-to-end by writing a null row, agetTableSchemaround-trip over a table declared with the width aliases (verifying we parse the server's canonicalizedDESCRIBEoutput), and the user-visible truncation of over-scaled values (-1.239→-1.23, confirming ClickHouse truncates toward zero rather than rounding, in agreement with the client-side encoding).fixes #39840
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, commentfixes #<ISSUE NUMBER>instead.CHANGES.mdwith noteworthy changes.See the Contributor Guide for more tips on how to make review process smoother.
To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md