Skip to content

Fix flaky 01780_column_sparse: order the arrayFilter result before limiting it - #119686

Merged
alexey-milovidov merged 2 commits into
masterfrom
fix-01780-column-sparse-nondeterministic-limit
Sep 19, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
fix-01780-column-sparse-nondeterministic-limit

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

01780_column_sparse ends with

SELECT arrayFilter(x -> x % 2 = 1, arr2) FROM t_sparse WHERE arr2 != [] LIMIT 5;

t_sparse is ORDER BY tuple(), so there is no implicit row order to fall back on, and once the table is read by more than one stream the five rows that survive LIMIT arrive in an arbitrary order.

Every "result differs with reference" failure of this test over the last 400 days — six of them — is the same permutation of that one statement's output, across Stateless tests (amd_debug, distributed plan, s3 storage, parallel), (amd_debug, AsyncInsert, s3 storage, parallel), (amd_tsan, s3 storage, parallel), (amd_binary, ParallelReplicas, s3 storage, parallel) and Fast test. Two of them were on master pushes rather than pull requests; the 2026-04-08 one ran with a randomized index_granularity of 3, which maximises the number of marks and hence of reading streams:

 SELECT arrayFilter(x -> x % 2 = 1, arr2) FROM t_sparse WHERE arr2 != [] LIMIT 5;
-[1]
+[1,3,5,7]
 [1,3]
 [1,3,5]
-[1,3,5,7]
+[1]

ORDER BY id makes the choice of rows deterministic. It is the order the reference already records, so only the echoed query text changes. The statement was added without an ordering in 2022 and the file has not been touched since; the test is untracked by any flakiness issue.

Found while triaging the CI of an unrelated pull request, where the most recent instance appeared.

Changelog category (leave one):

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

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

Version info

  • Backported to: 26.9.2.35, 26.8.11.47, 26.7.14.46, 26.3.33.150

`01780_column_sparse` ends with

    SELECT arrayFilter(x -> x % 2 = 1, arr2) FROM t_sparse WHERE arr2 != [] LIMIT 5;

`t_sparse` is `ORDER BY tuple()`, so there is no implicit row order to fall
back on, and once the table is read by more than one stream the five rows that
survive `LIMIT` arrive in an arbitrary order. Every "result differs with
reference" failure of this test over the last 400 days - six of them, the most
recent in `Stateless tests (amd_release, distributed cache, meta in keeper, s3
storage, parallel)` - is the same permutation of that one statement's output,
and the 2026-04-08 instance on a master push ran with a randomized
`index_granularity` of 3, which maximises the number of marks and hence of
reading streams.

`ORDER BY id` makes the choice of rows deterministic. It is the order the
reference already records, so only the echoed query text changes.

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

clickhouse-gh Bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [53220ab]

Summary: ✅


AI Review

Summary

This PR makes the last query in 01780_column_sparse deterministic by adding ORDER BY id before LIMIT 5, which matches the flaky failure mode described in the PR for a table created with ORDER BY tuple(). The change is narrow, preserves the expected result set, and I did not find a remaining correctness or coverage gap in the current patch.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-ci comp-testing Test content only (no production code). labels Sep 12, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119686&sha=09ae8c074aeaaf2588f26ec4f0d5e4a00f856e8e&name_0=PR&name_1=Stateless%20tests%20%28amd_binary%2C%20flaky%20check%29 and provide a fix in a separate PR. If the fix is already in progress, link it here.

The only red on this PR was 01780_column_sparse_alter with Reason: Test runs too long (> 180s). That test is not touched by this PR — it is only pulled into the flaky check because clickhouse-test matches test names by prefix, so 01780_column_sparse also selects 01780_column_sparse_alter. The run that timed out used --index_granularity 1, under which that test's repeated ALTERs become very slow. It would be good to make 01780_column_sparse_alter faster (or pin its granularity) so it stops timing out under randomized MergeTree settings.

@groeneai

Copy link
Copy Markdown
Collaborator

Acknowledged, and no fix is in progress: no open or merged PR touches tests/queries/0_stateless/01780_column_sparse_alter.sql. I am investigating and will post the PR link here.

For context, CIDB says the slowness is a single draw rather than a chronically slow test: one FAIL in 30 days (the one in this flaky check), and inside that same 50-run batch p50 was 6.3s, p95 7.7s, max 208.4s. On the ordinary lanes it stays cheap, e.g. Stateless tests (arm_asan_ubsan, azure, parallel): 4378 runs, p50 1.5s, max 22.8s. So I will first pin down which randomized setting combination produces the 200s draw, then choose between making the test faster and clamping the granularity.

@groeneai

Copy link
Copy Markdown
Collaborator

Fix: #120968

From that job's own logs the draw was index_granularity 1, so the 2000-row Compact part carried 2001 marks and the two mutations_sync = 2 ALTERs paid a per-granule cost 2000 times, 82.99 s and 103.33 s of the 208.35 s. In the same batch's other 49 draws no statement exceeded 1.31 s, so there is no slow step to make faster and I pinned the granularity instead: -- Random settings limits: index_granularity=(100, None); index_granularity_bytes=(100000, None). Those are the two bounds clickhouse-test already applies for exactly this reason, but only on sanitizer, debug and coverage builds (tests/clickhouse-test:2101), and amd_binary is none of those, so the full range applied here. Over 6000 randomized runs per arm, 7 runs drew granules below 100 rows without the line and none did with it.

@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.

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Sep 19, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 19, 2026
Merged via the queue into master with commit 1e07e94 Sep 19, 2026
177 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-01780-column-sparse-nondeterministic-limit branch September 19, 2026 10:05
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 19, 2026
pull Bot pushed a commit to TCeason/ClickHouse that referenced this pull request Sep 19, 2026
Stateless tests (amd_binary, flaky check) on ClickHouse#119686 failed this test at
208.35 s against the 180 s cap (TEST_MAX_RUN_TIME_IN_SECONDS,
tests/clickhouse-test:214, enforced at :3770-3775). The test is untouched
by that PR; the flaky check matches test names by prefix, so a change to
01780_column_sparse.sql selects 01780_column_sparse_alter too and runs it
50 times, each with a fresh randomized settings draw.

That draw was index_granularity 1. With index_granularity_bytes 17728586
(never binding) and min_bytes_for_wide_part 1073741824 (so the part is
Compact), the 2000-row INSERT produced one part with 2001 marks. SET
mutations_sync = 2 then makes each ALTER wait for a mutation that rewrites
the whole part one granule at a time, so every per-chunk fixed cost is paid
2000 times. From the failing job's own part_log: DROP COLUMN + RENAME took
82 990 ms (Seek 2002, CompressedReadBufferBlocks 4000) and MODIFY COLUMN
103 184 ms (Seek 2003, FunctionExecute 2000, RWLockAcquiredReadLocks 2000),
with the three uniqExact reads adding 16 770 ms. CPU is a small fraction of
that wall time and MutatePartStart is logged in the same second as the
ALTER, so this is neither data volume nor scheduling delay.

Speeding the test up is not available: in the other 49 draws of the same
batch no single statement exceeded 1.31 s, so there is no slow step. The
granule count is the variable, and unlike the row count (an oracle input,
via the 1-in-11 and 1-in-13 densities behind the reference counts 182 and
155) it is purely a randomizer artefact. So bound the two settings that
set granule size instead.

clickhouse-test already applies exactly these two bounds, for exactly this
reason, at tests/clickhouse-test:2101-2103, but only on sanitizer, debug
and coverage builds; amd_binary is none of those, so randint(1, 65536)
applied in full. This test opts into the same bound rather than widening
the global one.

index_granularity_bytes is clamped as well because adaptive granularity
takes the minimum of the two, so a low byte draw shrinks granules even at
index_granularity 100: measured, 1024 gives 49 marks against 21 at 100000.

Coverage is kept. index_granularity still ranges over [100, 65536], i.e.
1 to 20 granules over 2000 rows, so multi-granule sparse serialization is
still exercised, and the assertions are untouched: the Sparse kind is
chosen once per part at INSERT in MergeTreeDataWriter::writeTempPartImpl
(choose_kind = true) and the mutations inherit it (MutateTask.cpp,
choose_kind = false), while index_granularity affects only mark layout.
The test's output is byte-identical to the reference at index_granularity
1, 100 and 8192, and with index_granularity_bytes 1024 and 100000.

Validated through the runner with 6000 randomized runs per arm: without
this line 7 runs produced granules below 100 rows (worst 5 rows, 400
marks), with it none did and the worst run sits on the intended floor of
20 marks. The directive itself fails silently when unparsed, so it was
also checked against the real parser and randomizer over 20000 draws, with
a negative control that corrupts one character of the prefix and brings
sub-100 draws back (25 of 20000, minimum 1).

The seven sibling 01780_column_sparse* tests are subject to the same
randomizer but show no too-long failure in 30 days of CIDB, so they are
left alone.

Asked for in ClickHouse#119686 (comment)

Co-Authored-By: Claude Opus 5 <[email protected]>
@PedroTadim

Copy link
Copy Markdown
Member

Backporting to fix CI

@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 25, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 25, 2026
PedroTadim added a commit that referenced this pull request Sep 25, 2026
Backport #119686 to 26.7: Fix flaky `01780_column_sparse`: order the `arrayFilter` result before limiting it
clickhouse-gh Bot added a commit that referenced this pull request Sep 25, 2026
Backport #119686 to 26.9: Fix flaky `01780_column_sparse`: order the `arrayFilter` result before limiting it
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 25, 2026
…`arrayFilter` result before limiting it
clickhouse-gh Bot added a commit that referenced this pull request Sep 25, 2026
Backport #119686 to 26.8: Fix flaky `01780_column_sparse`: order the `arrayFilter` result before limiting it
PedroTadim added a commit that referenced this pull request Sep 25, 2026
Backport #119686 to 26.3: Fix flaky `01780_column_sparse`: order the `arrayFilter` result before limiting it
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
…order the `arrayFilter` result before limiting it
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
…order the `arrayFilter` result before limiting it
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
…order the `arrayFilter` result before limiting it
@clickhouse-gh clickhouse-gh Bot added comp-datatype-wrapper Type modifiers/wrappers (Nullable, LowCardinality, etc.). and removed comp-testing Test content only (no production code). labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-datatype-wrapper Type modifiers/wrappers (Nullable, LowCardinality, etc.). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-ci 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.3-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants