Skip to content

Reject Decimal precision outside 1..38 - #655

Open
HawkForge wants to merge 1 commit into
ClickHouse:masterfrom
HawkForge:fix/decimal-precision-over-38
Open

HawkForge wants to merge 1 commit into
ClickHouse:masterfrom
HawkForge:fix/decimal-precision-over-38

Conversation

@HawkForge

@HawkForge HawkForge commented Dec 6, 2026 •

Copy link
Copy Markdown

The server sends Decimal256(S) as Decimal(76, S), and a Decimal(P, S) column with P from 39 to 76 as is, with 32 bytes per value. ColumnDecimal has no 32-byte storage: it used ColumnInt128 for every precision above 18, so Select() read these columns as 16-byte values (wrong values with no error, or an unrelated protocol error), and Insert() of a ColumnDecimal(76, 2) blocked until the server receive timeout.

DecimalType now throws ValidationError for a precision outside 1..38 (this replaces the TODO: assert(precision <= 38 && precision > 0)). CreateColumnByType(), Select() and ColumnDecimal reject such types before they read or send data, also inside Nullable, Array and so on. Real Decimal256 support stays in #377.

Tests:

  • TypesCase.DecimalPrecisionOutOfRange in ut/types_ut.cpp: 0, 39 and 76 throw; 1 and 38 do not.
  • CreateColumnByType.DecimalPrecisionOverLimit in ut/CreateColumnByType_ut.cpp: Decimal(39, 2), Decimal(76, 2), also in Nullable and Array.
  • ClientCase.SelectDecimal256Throws in ut/client_ut.cpp: SELECT toDecimal256(number + 1, 2) throws ValidationError. Before the change it threw UnimplementedError: unimplemented 72 (no compression) or CompressionError (LZ4).

The new tests fail without the change. The whole clickhouse-cpp-ut suite passes against a local server (1078 tests).

Fixes #620


Note

Medium Risk
Changes decimal type handling on the read/write path, but only rejects previously broken precisions 39–76; supported decimals 1–38 are unchanged.

Overview
Fails fast on unsupported high-precision decimals instead of mis-reading 32-byte Decimal256 / Decimal(76, …) wire data as 16-byte values or hanging on insert.

DecimalType now throws ValidationError when precision is not in 1..38 (replacing a TODO assert), which blocks unsupported decimals at type/column creation—including nested Nullable / Array—before Select() or CreateColumnByType() touch protocol data.

Tests cover Type::CreateDecimal, column factory parsing, and a live SELECT toDecimal256(...) expecting ValidationError.

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

The server sends Decimal256(S) as Decimal(76, S), and Decimal(P, S) with
P > 38 as is. ColumnDecimal has no 32-byte storage and used 16 bytes per
value, so Select() could return wrong values with no error and Insert()
hung until the server receive timeout. DecimalType now throws
ValidationError for a precision outside 1..38, so CreateColumnByType()
and ColumnDecimal reject such types before any data is read or sent.

Closes ClickHouse#620
@HawkForge
HawkForge requested review from a team and slabko as code owners December 6, 2026 10:22
@github-actions

github-actions Bot commented Dec 6, 2026

Copy link
Copy Markdown

AI review gate — AI review gate could not decide

  • ai-gate: could not read the AI review — gh api repos/ClickHouse/clickhouse-cpp/commits/53dd55e2e25b7caa9d8cd86f832ca6d49d58f60a/check-runs --paginate --slurp failed: unexpected end of JSON input

What to do:

  • This is a GitHub API or permissions problem, not your change. Re-run the gate; if it keeps failing, ask a repository administrator.

Re-run the gate with “Re-run jobs” after an approval or a finished review. A push re-runs it automatically.

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.

Decimal(P, S) with P > 38 (Decimal256) is accepted and read as 16-byte values: wrong data with no error, Insert blocks

1 participant