Skip to content

Stabilize the two arrayLast filter queries of the array_first_last performance test - #122483

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:stabilize-array-first-last-perf
Sep 27, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:stabilize-array-first-last-perf

Conversation

@groeneai

@groeneai groeneai commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #117153

Changelog category (leave one):

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

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

Not required: CI-only change.

Description

array_first_last #9 and #11 (arrayLast(x -> x > 3, x) on the 32- and 128-element tables) take one of two times on the same code. On #117153 the dashboard scored #9 +19.2% as a confirmed regression (arm 1/6, report): 7 of 7 runs took 53.1-53.6 ms on the old server and 63.1-64.4 ms on the new one, where #11 stayed fast. Master too: arm 1/6 @ 350963d1, #9 54.8 -> 65.0 ms, slower. In-tree, #9 has already learned a 27% threshold.

Every row is [number, 0, ..., 0], so x > 3 matches only element 1: arrayFirst (#8, #10) stops at its first check, arrayLast walks back over 31 or 127 rejected elements per row. Only the scanning queries are bimodal (runs moving more than 15%, 14 days):

#8 #9 #10 #11
arm 4/3868 336/3630 4/3843 178/3708
arm, master 1/418 37/384 1/414 17/398
amd 0/1107 17/1053 1/1086 40/1051

The moves go both ways about equally often; on arm the slow level is extra user CPU time with the same page faults, system time and memory.

I changed the predicate of #9 and #11 to x -> x = 0, which matches the trailing zero that arrayLast checks first, so all four filter queries do the per-row work of the stable arrayFirst twins. Query order is unchanged. Cost: no query times a long scan over rejected elements any more; the output reservation this test was added for (#118188) is still measured.

Locally (x86, official master build, perf.py, 7 runs): old #9 108.0 vs #8 63.7 ms; new #9 74.3 vs #8 75.1 ms, new #11 56.9 vs #10 59.8 ms.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.789-master (included in 26.10 and later)

The fixture rows are arrayResize([toUInt64(number)], N) = [number, 0, ..., 0],
and `x -> x > 3` is true only for element 1. So arrayFirst (queries ClickHouse#8 and ClickHouse#10)
stops at the first element it checks, while arrayLast (ClickHouse#9 on the 32-element
table, ClickHouse#11 on the 128-element one) walks backward over 31 or 127 rejected
elements per row before it reaches element 1. The lambda, the tables and the
output reservation are the same for the twins; only the backward scan differs.

Only the scanning queries are bimodal. Performance comparison runs between
2026-09-12 and 2026-09-26, client_time, runs moving more than 15% either way:

  arm: ClickHouse#8 4 of 3868, ClickHouse#9 336 of 3630 (37 of 384 on master),
       ClickHouse#10 4 of 3843, ClickHouse#11 178 of 3708 (17 of 398 on master)
  amd: ClickHouse#8 0 of 1107, ClickHouse#9 17 of 1053, ClickHouse#10 1 of 1086, ClickHouse#11 40 of 1051

The moves go both ways in about equal numbers (arm ClickHouse#9: 156 slower, 180
faster), so they do not come from the compared builds. On arm the slow level
is extra user CPU time on identical work: over the 156 runs where the new
server was slow, ClickHouse#9 user time was 54.1 -> 63.3 ms, with the same soft page
faults (816 vs 815), system time (1.45 vs 1.49 ms) and memory usage. On PR
ClickHouse#117153 (arm 1/6, job 107092288374) all 7 runs of ClickHouse#9 took 53.1-53.6 ms on
one server and 63.1-64.4 ms on the other, where ClickHouse#11 stayed fast. The learned
in-tree thresholds of ClickHouse#9 and ClickHouse#11 are 27% and 23%, and the performance
dashboard scored that +19.2% as a confirmed regression.

Change the predicate of ClickHouse#9 and ClickHouse#11 to `x -> x = 0`. It is true for the
trailing zero, which is the element arrayLast checks first, so all four
filter-path queries evaluate the lambda over every element and then stop at
their first check, like the stable arrayFirst twins. The query order and
count are unchanged, so the query indices keep their identity. The new query
texts start without learned thresholds.

Cost: no query times a backward scan over many rejected elements any more
(the arrayFirst queries never timed a long forward one). The output
reservation the test was added for is still measured.

Local check on x86 with the official 26.10.1.761 master build, perf.py, 7
runs: old ClickHouse#9 108.0 ms vs ClickHouse#8 63.7 ms; new ClickHouse#9 74.3 ms vs ClickHouse#8 75.1 ms, new ClickHouse#11
56.9 ms vs ClickHouse#10 59.8 ms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Sep 26, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex): 0 findings on this head. My own cold review of the diff, done before reading the implementation's evidence, found no blocker or major; its one nit (a wording overstatement in this description) was fixed before publication.

Session id: cron:clickhouse-review-slot-48:20260926-222500

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Per-server timing mode on Graviton4 runners, so the CI record is the repro: job 107092288374 (7/7 runs 53.1-53.6 vs 63.1-64.4 ms), master job 107030861425, 336/3630 arm runs. The cause is on demand: old #9 scans back to element 1 on 1999996 of 2000000 rows.
b Root cause explained? x > 3 matches only element 1 of [number, 0, ..., 0], so only arrayLast walks back over 31/127 rejected elements per row, and that scan runs at one of two speeds per server (extra user CPU only).
c Fix matches root cause? Yes: the predicate now matches the element arrayLast checks first, removing the only work that separates #9/#11 from their stable arrayFirst twins. No threshold, tag or data size changed.
d Test intent preserved / new tests added? Both functions, both tables and the output reservation (#118188) are still timed; lost: a long scan over rejected elements (stated as the cost). Test-only change.
e Both directions demonstrated? Locally (x86): old #9 108.0 ms vs #8 63.7 ms, new #9 74.3 vs 75.1 ms, new #11 56.9 vs #10 59.8 ms. The arm side is measured by this PR's own perf jobs and the post-merge census.
f Fix is general across code paths? All four filter-path queries now stop at their first check; the constant-predicate queries do no per-element check; no other perf test calls arrayFirst/arrayLast.
g Fix generalizes across inputs (params/datatypes/wrappers)? Both tables; x = 0 matches the last element of every row, number = 0 included (0 exceptions checked on both tables).
h Backward compatible? (maintainer-approved exception only) Test-only. The two new query texts restart their learned in-tree thresholds at the 0.15 floor.
i Invariants and contracts preserved? Query count and order unchanged, so indices #0-#11 keep their identity in CIDB and on the dashboard; fixture and settings untouched.

Session id: cron:clickhouse-impl-slot-41:20260926-215800

@clickhouse-gh

clickhouse-gh Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [14385fe]

Summary: ✅


AI Review

Summary

This PR rewrites the two arrayLast filter queries in tests/performance/array_first_last.xml so they stop on the first element scanned from the back instead of exercising the old late-match shape that was bimodal on arm. The rewrite itself is plausible, but I found one review concern: the PR still does not show that the new queries are actually stable on the arm CI runners that motivated the change.

Findings

⚠️ Majors

  • [tests/performance/array_first_last.xml:67-69] The change is being justified as a stability fix for array_first_last #9 and #11, but this head has no Performance Comparison evidence from the affected runners. The PR is not labeled pr-performance, so all perf shards are skipped, and the only after-change numbers in the description are local x86 runs. That leaves the arm-side stability claim unproven on the exact workload that would gate this change.
    Suggested fix: run a perf comparison on this exact head on arm, make sure the modified #9 and #11 queries actually execute, and link that report in the PR.
Missing context / blind spots
  • ⚠️ I could not validate the claimed stability improvement on CI because the perf jobs are skipped on this head.
Final Verdict

Changes requested: the benchmark rewrite needs arm perf evidence on this exact head before the stability claim is established.

Comment on lines +67 to +69
<query>SELECT arrayLast(x -&gt; x = 0, x) FROM array_first_last_32 FORMAT Null</query>
<query>SELECT arrayFirst(x -&gt; x &gt; 3, x) FROM array_first_last_128 FORMAT Null</query>
<query>SELECT arrayLast(x -&gt; x &gt; 3, x) FROM array_first_last_128 FORMAT Null</query>
<query>SELECT arrayLast(x -&gt; x = 0, x) FROM array_first_last_128 FORMAT Null</query>

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.

The contract of this change is "the replacement arrayLast filter queries are stable on the CI runners that were flagging array_first_last #9 and #11". We still do not have that proof on this head: all Performance Comparison shards are skipped because the PR is not labeled pr-performance, and the only after-change numbers in the description are local x86 runs. Since array_first_last.xml has 12 queries and perf CI normally samples 10, I would want an exact-head arm perf run that actually executes these two slots before we rewrite the benchmark around them.

@clickhouse-gh clickhouse-gh Bot added the comp-testing Test content only (no production code). label Sep 26, 2026
@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 ClickHouse:master with commit 0c64d50 Sep 27, 2026
172 of 173 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 27, 2026
@clickhouse-gh clickhouse-gh Bot added comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... 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

can be tested Allows running workflows for external contributors comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-ci 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.

3 participants