Skip to content

Fix flaky 04625_hash_table_sizes_stats_table_expression_modifiers - #111984

Closed
alexey-milovidov wants to merge 2 commits into
masterfrom
fix-04625-external-group-by-flake
Closed

alexey-milovidov wants to merge 2 commits into
masterfrom
fix-04625-external-group-by-flake

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Documentation entry for user-facing changes

Description

Fixes the flaky 04625_hash_table_sizes_stats_table_expression_modifiers (added in #109847), e.g. this MasterCI run:

 final-prealloc	520000
-no-final-prealloc	650000
+no-final-prealloc	0

The test asserts the exact number of elements preallocated in the shard-side aggregation hash tables, and the settings randomizer enables external aggregation in a fraction of the runs. All 15 failures in the CI database over the last five days (master and PRs, across amd_tsan, amd_asan_ubsan, arm_asan_ubsan, arm_binary, amd_msan) name the same culprit — max_bytes_before_external_group_by.

When the first run of a pair spills, Aggregator::writeToTemporaryFile re-inits the hash table after each flush, so the sizes updateStatistics reads at the end of the query are the post-spill remainder instead of the real group count. The stats cache entry then sits below the 500e3 lower bound in getSizeHint, so the second (-prealloc) run preallocates nothing and AggregationPreallocatedElementsInHashTables is 0.

Pinning both thresholds to 0 is enough — the test is about the stats cache key including table expression modifiers, not about spilling. Everything else stays randomized.

Verified locally against a server with process_query_plan_packet, forcing the culprit through --client-option:

test version max_bytes_before_external_group_by=67836231 max_bytes_ratio_before_external_group_by=0.0001 randomized, 25 runs
before FAIL (no-final-prealloc 0, same diff as CI) FAIL (both pairs 0) OK
after OK OK 25/25 OK

The ratio-based variant of the randomizer breaks the test the same way, hence pinning both.

🤖 Generated with Claude Code

The test asserts the exact number of elements preallocated in the shard-side
aggregation hash tables, and the settings randomizer turns external aggregation
on (`max_bytes_before_external_group_by`, or its ratio-based variant) for ~half
of the runs.

When the first run of a pair spills, `Aggregator::writeToTemporaryFile` re-inits
the hash table after each flush, so the sizes `updateStatistics` reads at the
end of the query are the post-spill remainder instead of the real group count.
The stats cache entry then sits below the 500e3 lower bound in `getSizeHint`,
the second (`-prealloc`) run preallocates nothing, and
`AggregationPreallocatedElementsInHashTables` is 0 rather than 650000.

Pin both thresholds to 0 in the test's settings; the test is about the stats
cache key including table expression modifiers, not about spilling. Everything
else stays randomized.

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

clickhouse-gh Bot commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0f06415]

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 26, 2026
--max_size_to_preallocate_for_aggregation=1000000000000
--serialize_query_plan=1
--prefer_localhost_replica=0
--max_bytes_before_external_group_by=0

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 disables randomized external aggregation for 04625, but the sibling tests/queries/0_stateless/04509_hash_table_sizes_stats_table_functions.sh is still exposed to the same failure mode. That test has no no-random-settings tag and both of its settings arrays still leave max_bytes_before_external_group_by and max_bytes_ratio_before_external_group_by randomized while asserting the same cold/hot AggregationPreallocatedElementsInHashTables invariant on 650e3 / 520e3 groups. A low positive spill threshold can therefore still make the first run spill, record only the post-spill remainder, and turn the -prealloc run into 0, so this PR only partially fixes the flake family. Please pin the same two settings to 0 there as well, or otherwise opt that test out of randomized settings.

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.

Already addressed on master: #111502 pinned max_bytes_before_external_group_by and max_bytes_ratio_before_external_group_by to 0 in both settings arrays of 04509_hash_table_sizes_stats_table_functions.sh (commit c781f06), in addition to 04625 (commit 9650bb8). So the sibling test is no longer exposed to the randomized-spill failure mode, and this PR is a duplicate of that one — nothing left to implement here.

@alexey-milovidov

alexey-milovidov commented Jul 27, 2026 •

Copy link
Copy Markdown
Member Author

This PR is obsolete: the same fix already landed on master in #111502 (commit 9650bb8, merged 2026-07-23) — the branch here was cut from a master that predated it, so the change is a duplicate. On current master, 04625_hash_table_sizes_stats_table_expression_modifiers.sh already pins both max_bytes_before_external_group_by and max_bytes_ratio_before_external_group_by to 0; the only difference left in this branch is a longer explanatory comment above the settings array.

The AI review finding about the sibling test is also already addressed on master: #111502 additionally pinned both settings in both settings arrays of 04509_hash_table_sizes_stats_table_functions.sh (commit c781f06), so the flaky family is fully covered there.

The sole CI red here (Stress test (arm_debug), Logical error: Inconsistent KeyCondition behavior) is a known unrelated flake, not caused by a test-settings change.

Nothing to rework or merge — recommending closing this as a duplicate of #111502.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant