Skip to content

Saturate an Iceberg decimal bound that cannot be widened inside its type - #119712

Merged
alexey-milovidov merged 7 commits into
masterfrom
iceberg-decimal-bound-saturate
Sep 28, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
iceberg-decimal-bound-saturate

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 13, 2026 •

Copy link
Copy Markdown
Member

Closes: #116999
Related: #116604
Related: #115832

Iceberg stores a decimal min/max bound rounded to the integral part, so the reader moves each bound one integral unit outwards before pruning on it. At scale 38 that unit is 10^38 and the bound lives in an Int128, which holds 1.7 * 10^38, so the shift leaves the type for every bound of magnitude above roughly 0.70141. Decimal(38, 38) is the only width where that happens to a bound the column can hold: at the other three the largest value the precision allows plus 10^scale still fits.

Until #116604 the shift wrapped, which inverted the pair reaching KeyCondition::mayBeTrueInRange, pruned the data file holding the matching rows and lost them silently (#116999). That pull request rejects such a bound instead, which is correct but gives the column no min/max condition at all - and for a Decimal(38, 38) column that is every bound of magnitude above 0.7, so those tables lost min/max pruning altogether.

A bound only has to stay on the outer side of every value in the file, and the largest magnitude the precision of the type allows is outside all of them, so it stands in for a widened bound that no longer fits. A bound that is itself outside that magnitude is one the column cannot hold: nothing vouches for the values in the file, so there is no stand-in for it and it stays unusable, which is the case test_iceberg_inverted_decimal_manifest_bounds pins for a Decimal32 bound.

The new stateless test reads a Decimal(38, 38) column whose two data files hold 0.99 and -0.99: every filter on the column returns its rows, and a probe outside one of the two shifted bounds prunes that file and only it. A second table holds 0.1 and -0.1, where the shift stays inside Int128 whatever the reader does with an overflow, so the two arms together say the pruning is lost to the overflow rather than to the shape of the test. On master the test fails on the two Decimal(38, 38) pruning arms only (0 pruned files instead of 1), and it passes with this change.

Note on released versions: the silent row loss that #116999 reports is fixed on master by #116604, but that fix is not in any release branch, while the change that introduced the overflow (#115832) was backported to 26.8 by #116860 and shipped in v26.8.1.2041-lts and v26.8.2.7-lts. Those releases still lose rows on a filter over a Decimal(38, 38) Iceberg column; that needs a backport of #116604 and is not addressed here.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Restore min/max pruning of Iceberg data files for a Decimal(38, 38) column. A manifest bound of magnitude above roughly 0.7 cannot be widened inside Int128 and was dropped, which left the column without a min/max condition; it is now saturated at the largest magnitude the precision allows, which is still outside every value the file can hold.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Workflow [PR]
Sync PR [sync-upstream/pr/119712]

Version info

  • Merged into: 26.10.1.860-master (included in 26.10 and later)
  • Backported to: 26.9.17.3, 26.8.25.5

Iceberg stores a decimal min/max bound rounded to the integral part, so the reader moves each
bound one integral unit outwards before pruning on it. At scale 38 that unit is `10^38` and the
bound lives in an `Int128`, which holds `1.7 * 10^38`, so the shift leaves the type for every
bound of magnitude above roughly 0.70141. `Decimal(38, 38)` is the only width where that happens
to a bound the column can hold: at the other three the largest value the precision allows plus
`10^scale` still fits.

Until #116604 the shift wrapped, which inverted the pair reaching
`KeyCondition::mayBeTrueInRange`, pruned the data file holding the matching rows and lost them
silently. That pull request rejects such a bound instead, which is correct but gives the column
no min/max condition at all - and for a `Decimal(38, 38)` column that is every bound of
magnitude above 0.7, so those tables lost min/max pruning altogether.

A bound only has to stay on the outer side of every value in the file, and the largest magnitude
the precision of the type allows is outside all of them, so it stands in for a widened bound that
no longer fits. A bound that is itself outside that magnitude is one the column cannot hold:
nothing vouches for the values in the file, so there is no stand-in for it and it stays unusable,
which is the case `test_iceberg_inverted_decimal_manifest_bounds` pins for a `Decimal32` bound.

The new test reads a `Decimal(38, 38)` column whose two data files hold `0.99` and `-0.99`: every
filter on the column returns its rows, and a probe outside one of the two shifted bounds prunes
that file and only it. A second table holds `0.1` and `-0.1`, where the shift stays inside
`Int128` whatever the reader does with an overflow, so the two arms together say the pruning is
lost to the overflow rather than to the shape of the test.

Closes: #116999
Related: #116604
Related: #115832

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6d6adc7]

Summary: ✅


AI Review

Summary

This PR restores Iceberg min/max pruning for Decimal(38, 38) bounds that overflow when widened and adds a focused stateless regression. The side-aware saturation fixes the previously reviewed overflow cases, but the current helper still accepts a narrower class of malformed out-of-precision decimal bounds and can turn them back into trusted ranges, which reopens the silent-row-loss class on bad manifests.

Findings

❌ Blockers

  • [src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergFieldParseHelpers.cpp:174-186] The new outer-side saturation is still too permissive: it accepts any decimal bound that is outside the declared precision as long as widening pulls it back under limit, not just the single writer-rounded limit + 1 / -limit - 1 shape this PR is trying to recover. A concrete Decimal32(9, 1) pair such as 1000000004 / 1000000005 becomes [99999999.4, 99999999.9], so ManifestFileIterator keeps min/max pruning instead of dropping it. If the file actually contains smaller values, KeyCondition can prune matching rows silently again.
    Suggested fix: if the declared unscaled decimal is outside precision by anything other than exactly one step (limit + 1 or -limit - 1), return std::nullopt and extend the malformed-manifest regression coverage to pin that case.
Final Verdict

Changes requested.

LLVM Coverage Report

Measured on commit 6d6adc7.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.50% 79.40% -0.10%

Changed lines: Changed C/C++ lines covered: 24/25 (96.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Sep 13, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. label Sep 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 6d6adc7b3292e797e33a13006b40b54b1bfe9d03 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

@clickhouse-gh

clickhouse-gh Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 3bfe6319-4923-47eb-9640-1d3321b4a067
  • MIRAI run: 31ffc05d-f015-490c-a7bb-9b4de6098193
  • PR check IDs:
    • clickbench_1816224_1790354531
    • clickbench_1816235_1790354531
    • clickbench_1816242_1790354532
    • tpch_adapted_1_official_1816248_1790354532
    • tpch_adapted_1_official_1816280_1790354532
    • tpch_adapted_1_official_1816294_1790354532

alexey-milovidov and others added 2 commits September 19, 2026 05:36
…ejecting it

The overflow branch of `deserializeDecimalBound` still dropped the bound
shape the compensation exists to recover. Spark and Amazon Athena round a
decimal bound to the integral part, so a file whose extreme value is `0.99`
carries `1.0` - `10^38` unscaled at scale 38, one unit outside what
`Decimal(38, 38)` holds. `common::addOverflow` fired for it, the precision
check then returned `std::nullopt`, and `ManifestFileIterator::processRow`
skipped min/max pruning for the whole column again.

Every value in the file fits the precision of the type it is read as, so the
largest magnitude that precision allows is on the outer side of all of them:
saturating to it is a valid bound and is the tightest one that does not leave
the type. The widened bound is now saturated rather than rejected, which also
covers a bound that is out of range before widening. Saturation is monotonic,
so it cannot invert a pair that was not inverted as declared.

The stateless test could not catch this, because it writes the table with
ClickHouse, whose `dumpDecimalValue` serializes the exact unscaled value. It
now patches the rounded form into the manifests - an uncompressed 16-byte
big-endian `fixed` that both forms fill completely - and checks that the two
data files still prune on the probes that sit outside them.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
alexey-milovidov and others added 3 commits September 19, 2026 20:42
A lower bound above the largest value of its type (or an upper bound below
the smallest) lies on the inner side of every value in the data file, so
clamping it collapsed the range to a point that excludes the data and let
`KeyCondition` prune a file that holds matching rows. Such a bound is not a
usable range border, so `deserializeDecimalBound` rejects it again. This is
the `Decimal32(9, 1)` case of `test_iceberg_inverted_decimal_manifest_bounds`.

Also add the three `count` lines of section `B1` that were missing from the
reference of `05210_iceberg_decimal38_bound_widening_116999`.

Addresses #119712 (comment) (review thread by clickhouse-gh)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
/// bound is not a usable range border.
if (lower_bound)
{
if (widened_value > limit)

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.

This outer-side guard is still too broad. It accepts any out-of-precision decimal bound whose widened value lands back inside [-limit, limit], not just the single writer-rounded shape this comment describes.

A concrete case is Decimal32(9, 1) with manifest bytes 1000000004 / 1000000005: deserializeDecimalBound turns them into [99999999.4, 99999999.9], ManifestFileIterator::processRow keeps that range because the declared pair is ordered, and KeyCondition can then prune a file that actually contains smaller values (for example e < toDecimal32(0.5, 1)), which brings back the silent-row-loss class on malformed manifests. The existing integration test around test_iceberg_inverted_manifest_bounds.py:441-449 is the same contract, just with a farther-out value that still takes the std::nullopt path.

I think the recoverable case has to be limited to the exact one-step-outside precision values (limit + 1 / -limit - 1), with everything else outside precision still returning std::nullopt.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Sep 27, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 27, 2026
Merged via the queue into master with commit 3a52fa3 Sep 28, 2026
177 checks passed
@alexey-milovidov
alexey-milovidov deleted the iceberg-decimal-bound-saturate branch September 28, 2026 00:00
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 28, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@alexey-milovidov — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 11, 2026
robot-ch-test-poll added a commit that referenced this pull request Oct 11, 2026
Cherry pick #119712 to 26.8: Saturate an Iceberg decimal bound that cannot be widened inside its type
robot-clickhouse added a commit that referenced this pull request Oct 11, 2026
robot-ch-test-poll added a commit that referenced this pull request Oct 11, 2026
Cherry pick #119712 to 26.9: Saturate an Iceberg decimal bound that cannot be widened inside its type
robot-clickhouse added a commit that referenced this pull request Oct 11, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 11, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 11, 2026
Backport #119712 to 26.8: Saturate an Iceberg decimal bound that cannot be widened inside its type
clickhouse-gh Bot added a commit that referenced this pull request Oct 11, 2026
Backport #119712 to 26.9: Saturate an Iceberg decimal bound that cannot be widened inside its type
@clickhouse-gh clickhouse-gh Bot added comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... and removed comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport

Projects

None yet

5 participants