Skip to content

Fix arrayResize with a Decimal size - #107389

Merged
alexey-milovidov merged 2 commits into
masterfrom
fix-arrayresize-decimal-size
Jun 14, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
fix-arrayresize-decimal-size

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

arrayResize read its size argument via IColumn::getInt, which for a Decimal column returns the raw unscaled value rather than the logical one. As a result arrayResize([1, 2, 3], 1.5::Decimal(2, 1)) resized to 15 elements instead of 1, inconsistently with Float and integer sizes (which truncate towards zero). A tiny fractional value such as -0.000000000000000000000006116852::Decimal(38, 30) has a raw value of -6116852, so the array was padded with millions of elements.

This is now fixed by converting a Decimal size to a plain integer before resizing, so its real (rounded-towards-zero) value is used.

The bug was found by FunctionsStress.stress (function_prop_fuzzer), where the above query made a worker pad ~6 million elements one by one and get reported as stuck. The existing arrayResize argument cap in that test did not prevent it: it only bounds the value from above (value > limit), so a large-magnitude negative size slips through, and for a Decimal it compares the logical value while the function used the raw one. The cap is now changed to bound the magnitude (abs(value) > limit) so large negative sizes are clamped too.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107319&sha=2603a4dd53466ae560f080b1a55ed3009d28fa9e&name_0=PR&name_1=Unit%20tests%20%28asan_ubsan%2C%20function_prop_fuzzer%29

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix arrayResize with a Decimal size argument: the size is now interpreted by its real value (e.g. arrayResize([1, 2, 3], 1.5::Decimal(2, 1)) returns one element) instead of the raw unscaled representation.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

🤖 Generated with Claude Code

Version info

  • Merged into: 26.6.1.780

…r cap

`arrayResize` read its size argument via `IColumn::getInt`, which for a
`Decimal` column returns the raw unscaled value rather than the logical one.
As a result `arrayResize([1, 2, 3], 1.5::Decimal(2, 1))` resized to 15
elements instead of 1, inconsistently with `Float` and integer sizes (which
truncate towards zero). A tiny fractional value such as
`-0.000000000000000000000006116852::Decimal(38, 30)` has a raw value of
`-6116852`, so the array was padded with millions of elements.

Convert a `Decimal` size to a plain integer before resizing, so its real
(rounded towards zero) value is used.

This was found by `FunctionsStress.stress` (`function_prop_fuzzer`), where the
above query made a worker pad ~6 million elements one by one and get reported
as stuck. The existing `arrayResize` argument cap did not prevent it: it only
bounds the value from above (`value > limit`), so a large-magnitude negative
size slips through, and for a `Decimal` it compares the logical value while the
function used the raw one. Make the cap bound the magnitude instead
(`abs(value) > limit`) so large negative sizes are clamped too.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107319&sha=2603a4dd53466ae560f080b1a55ed3009d28fa9e&name_0=PR&name_1=Unit%20tests%20%28asan_ubsan%2C%20function_prop_fuzzer%29
Related: #107319

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alexey-milovidov
alexey-milovidov force-pushed the fix-arrayresize-decimal-size branch from 753d2d0 to ec12cb3 Compare June 13, 2026 13:01
@clickhouse-gh

clickhouse-gh Bot commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b0af15f]

Summary: ✅


AI Review

Summary

This PR fixes arrayResize so Decimal size arguments are interpreted by their logical value before resize, and tightens the function stress-test argument cap to clamp large negative values as well as positive values. I found no correctness, safety, or metadata issues that warrant inline review comments.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 13, 2026
@azat azat self-assigned this Jun 13, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, link the fix of Mutation of Memory table produced incomplete output here.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

Found it: #105286

@groeneai

Copy link
Copy Markdown
Collaborator

The failure on this PR (BuzzHouse (arm_asan_ubsan), ALTER TABLE d6.t152 (APPLY PATCHES), 2026-06-13 15:36) is STID 2380-3043, the StorageMemory::mutate no-op-command family.

#105286 is the narrow fix: it filters only APPLY PATCHES / APPLY DELETED MASK. The current authoritative fix is #106621, which Closes: #105286 and supersedes it. #106621 additionally filters MATERIALIZE STATISTICS / MATERIALIZE INDEX / MATERIALIZE PROJECTION / REWRITE PARTS, which reach the same per-block row-count invariant in StorageMemory.cpp and which CIDB shows hitting master (e.g. MATERIALIZE STATISTICS ALL on t238, 2026-06-05) but the narrow fix does not cover.

Both are still open and unmerged, so the bug is live on master (most recent master hits 2026-06-11). #106621 is the one to merge.

alexey-milovidov added a commit that referenced this pull request Jun 13, 2026
…pus)

Extracted from the AST-fuzzer logs (fuzzer.log of the failing run): the targeted/
old_compatibility fuzzer seeded from 01552_impl_aggfunc_cloneresize.sql (test_bm /
test_bm_join, groupBitmapState + bitmapOrCardinality) and the PR's arrayResize test, then
mutated them into bitmap self-joins, arrayResize(..., N) with huge N over num_10k, and ~97
fuzzed MergeTree table variants being written and merged. The test recreates the exact seed
tables and replays those exact query families concurrently with the fuzzer's per-query
max_memory_usage = 10 GiB, in a large container with ClickHouse limits at their defaults and
swap disabled.

Measured: this drives the node's real memory.current to ~7.3 GiB against the 8 GiB cgroup -
faithfully reproducing the fuzzer's load. It does not fire the kernel OOM in the docker-in-
docker integration runner, because there real memory tracks the tracker limit (~7.2 GiB) and
stays just under the cgroup: the MemoryWorker's dirty-page purge - which lost the race on the
fuzzer's real VM under sustained chaotic load, letting RSS drift above tracked - wins it in the
cleaner/faster container, and --cgroupns=host makes the server misread its cgroup (RSS read
~10 GiB) and over-throttle. The kill reproduces where per-container memory is accounted as on
the fuzzer's VM.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107389&sha=ec12cb3ce0a49a403226cb0668b092f02a2fa3f6&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29
Related: #107389

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
alexey-milovidov added a commit that referenced this pull request Jun 13, 2026
The docker mem_limit used by the integration runner is not enforced as a hard memory.max in
the docker-in-docker setup (--cgroupns=host), so a real kernel OOM never fires there - which is
why the kill could not be reproduced via the test harness. This script instead creates a cgroup
v2 itself (which the kernel does enforce), starts clickhouse-server inside it (so the server
reads the cgroup and keeps its limits intact), and runs sustained concurrent groupArrayState
merge/insert churn. The allocator retention gap (resident memory above the tracked limit) pushes
resident memory past the cgroup and the kernel OOM killer fires - the same mechanism that killed
the AST-fuzzer's server (tracked 48.9 GiB, resident 52-57 GiB).

Verified: cgroup memory.events oom_kill increments and dmesg shows
"Memory cgroup out of memory: Killed process ... (clickhouse), oom_memcg=/ch_merge_oom"
(constraint=CONSTRAINT_MEMCG). Requires root + cgroup v2 + a built clickhouse binary.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107389&sha=ec12cb3ce0a49a403226cb0668b092f02a2fa3f6&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29
Related: #107389

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

clickhouse-gh Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.70% 84.70% +0.00%
Functions 92.40% 92.40% +0.00%
Branches 77.30% 77.30% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 6/24 (25.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

CI triage (/continue-pr): the only red is AST fuzzer (arm_asan_ubsan), which aborted on

SELECT plus(1048577, xor(...)), toUInt16OrNull(toFixedString(toString(number), NULL))
FROM system.numbers
GLOBAL ALL FULL OUTER JOIN system.numbers AS alias0 ON equals(number, alias0.number)
GLOBAL FULL OUTER JOIN system.numbers AS alias1 ON equals(number, alias1.number)
LEFT JOIN system.numbers AS alias2 ON ...
WHERE 257 LIMIT 65537

with Logical error: 'Invalid number of rows in Chunk ... at position 3: expected 65409, got 65537' inside the JOIN pipeline. This is unrelated to arrayResize (the change only touches arrayResize.cpp and gtest_functions_stress.cpp): it is a pre-existing, master-wide fuzzer failure that reproduces on master (pull_request_number = 0) on 06-13/06-12/06-11 and on other unrelated PRs (#107264, #106928), and is already tracked by #107172.

Re-ran the failed job; no re-merge (merge-base is from today).

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 14, 2026
Merged via the queue into master with commit 14b89ce Jun 14, 2026
327 of 328 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-arrayresize-decimal-size branch June 14, 2026 15:19
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 14, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
A background merge runs under its own memory tracker (a child of the
"Background process (mutate/merge)" tracker), not under the tracker of the
query that triggered it. Consequently the per-query `max_memory_usage` limit
does not bound a merge: merging parts that hold large values consumes memory
regardless of how small `max_memory_usage` is.

This is the mechanism behind the AST-fuzzer OOM where sequential,
individually-small fuzzer queries left behind parts that the background merge
subsystem then combined, growing server memory across query boundaries until
the kernel OOM-killer reaped the server.

The test inserts two parts of large arrays, sets a tiny `max_memory_usage`,
and shows via `system.part_log` that the merge completes and its
`peak_memory_usage` is far above the per-query limit.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107389&sha=ec12cb3ce0a49a403226cb0668b092f02a2fa3f6&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29
Related: ClickHouse#107389

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…pus)

Extracted from the AST-fuzzer logs (fuzzer.log of the failing run): the targeted/
old_compatibility fuzzer seeded from 01552_impl_aggfunc_cloneresize.sql (test_bm /
test_bm_join, groupBitmapState + bitmapOrCardinality) and the PR's arrayResize test, then
mutated them into bitmap self-joins, arrayResize(..., N) with huge N over num_10k, and ~97
fuzzed MergeTree table variants being written and merged. The test recreates the exact seed
tables and replays those exact query families concurrently with the fuzzer's per-query
max_memory_usage = 10 GiB, in a large container with ClickHouse limits at their defaults and
swap disabled.

Measured: this drives the node's real memory.current to ~7.3 GiB against the 8 GiB cgroup -
faithfully reproducing the fuzzer's load. It does not fire the kernel OOM in the docker-in-
docker integration runner, because there real memory tracks the tracker limit (~7.2 GiB) and
stays just under the cgroup: the MemoryWorker's dirty-page purge - which lost the race on the
fuzzer's real VM under sustained chaotic load, letting RSS drift above tracked - wins it in the
cleaner/faster container, and --cgroupns=host makes the server misread its cgroup (RSS read
~10 GiB) and over-throttle. The kill reproduces where per-container memory is accounted as on
the fuzzer's VM.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107389&sha=ec12cb3ce0a49a403226cb0668b092f02a2fa3f6&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29
Related: ClickHouse#107389

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
The docker mem_limit used by the integration runner is not enforced as a hard memory.max in
the docker-in-docker setup (--cgroupns=host), so a real kernel OOM never fires there - which is
why the kill could not be reproduced via the test harness. This script instead creates a cgroup
v2 itself (which the kernel does enforce), starts clickhouse-server inside it (so the server
reads the cgroup and keeps its limits intact), and runs sustained concurrent groupArrayState
merge/insert churn. The allocator retention gap (resident memory above the tracked limit) pushes
resident memory past the cgroup and the kernel OOM killer fires - the same mechanism that killed
the AST-fuzzer's server (tracked 48.9 GiB, resident 52-57 GiB).

Verified: cgroup memory.events oom_kill increments and dmesg shows
"Memory cgroup out of memory: Killed process ... (clickhouse), oom_memcg=/ch_merge_oom"
(constraint=CONSTRAINT_MEMCG). Requires root + cgroup v2 + a built clickhouse binary.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107389&sha=ec12cb3ce0a49a403226cb0668b092f02a2fa3f6&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29
Related: ClickHouse#107389

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants