Skip to content

Supporting lazily replicated arrays on arrayElement and arraySlice (reduces memory usage and improves performance) - #112304

Merged
diegomestre2 merged 22 commits into
masterfrom
lazy_replication_stage2
Aug 27, 2026
Merged

diegomestre2 merged 22 commits into
masterfrom
lazy_replication_stage2

Conversation

@diegomestre2

@diegomestre2 diegomestre2 commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

arrayElement and arraySlice now consume lazily replicated arrays (ColumnReplicated, produced by lazy ARRAY JOIN and JOIN under enable_lazy_columns_replication) without materializing them.

Follow up of : #111581 and #111749
Closes: #54967

Implementation:

  • A new ReplicatedSource adapter in GatherUtils wraps any array source (numeric, generic, or nullable) over the compact nested column and remaps each logical row to its nested row through the replication indexes.
  • arrayElement gets a dedicated path: a per-row gather from the nested data for non-constant indexes (supporting negative indexes, out-of-range defaults, Nullable elements, and arrayElementOrNull), and for constant indexes it executes on the nested rows only and re-wraps the result lazily with the same indexes.
  • Per-row index arguments (arraySlice offset/length, arrayElement index) are materialized if they arrive replicated
  • Unsupported shapes (Map, LowCardinality elements)

Measured on the test workload (1000-element String arrays, 50× replication by ARRAY JOIN, 100 rows): peak memory drops from 1.12 GB to 29 MB (38×).

First Query

WITH materialize(range(1000)) AS large_array
SELECT count() FROM system.numbers WHERE NOT ignore(
    arrayMap(idx -> arraySlice(large_array, idx, 5),
        arrayEnumerate(large_array)))
SETTINGS max_rows_to_read = 262144, read_overflow_mode = 'break',
         max_memory_usage = 20000000000, enable_lazy_columns_replication = 1

Second Query

WITH materialize(range(1000)) AS large_array
SELECT count() FROM system.numbers WHERE NOT ignore(
    arrayMap(idx -> arraySlice(large_array, idx, 5),
        arrayEnumerate(large_array)))
SETTINGS max_rows_to_read = 16384, read_overflow_mode = 'break',
         max_block_size = 512, max_threads = 1,
         max_memory_usage = 20000000000, enable_lazy_columns_replication = {1|0}
Configuration Rows Time Peak RSS Result
Lazy ON, default block size, 262k rows 327,045 ~4.5 s (~72M lambda calls/s) 3.15 GB ✅ completes
Lazy OFF, same settings — fails in 0.5 s — ❌ MEMORY_LIMIT_EXCEEDED: tries to allocate 121.83 GiB for one 65536-row block
Lazy ON, max_block_size=512, 1 thread, 16384 rows 16,384 0.28 s 272 MB ✅
Lazy OFF, same 16,384 7.26 s 2.28 GB ✅

Tests: a stateless test covering numeric/String/Nullable/Tuple elements, dynamic and constant offsets, negative and Nullable indexes, empty arrays, ARRAY JOIN and JOIN producers, UInt16 replication indexes, plus a performance test comparing both settings.

Changelog category (leave one):

  • Performance Improvement

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

arrayElement and arraySlice now read lazily replicated arrays (produced by ARRAY JOIN and JOIN when enable_lazy_columns_replication is enabled) directly, without materializing them. This greatly reduces memory usage
and improves performance of queries that index or slice a large array.


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

Version info

  • Merged into: 26.9.1.195-master (included in 26.9 and later)
  • Backported to: 26.8.22.8

@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8acb190]

Summary: ✅


AI Review

Summary

This PR teaches arrayElement and arraySlice to consume lazily replicated arrays produced by ARRAY JOIN and JOIN without materializing them first, and it adds focused stateless/performance coverage for the main replicated, nullable, low-cardinality, and compaction paths. I did not find a remaining correctness or rollout blocker in the current 8acb1900d91b0496de6d331e3a41660e9dd06e68 head.

Final Verdict

✅ No blocking issues found in the current PR head.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.20% 87.20% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 180/235 (76.60%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jul 28, 2026
Comment thread src/Functions/array/arrayElement.cpp Resolved
@diegomestre2 diegomestre2 changed the title Lazy replication stage2 Supporting lazily replicated arrays on arrayElement and arraySlice (reduces memory usage and improves performance) Jul 29, 2026
@diegomestre2
diegomestre2 marked this pull request as ready for review July 30, 2026 13:51
@antaljanosbenjamin antaljanosbenjamin self-assigned this Jul 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

Release-transition baseline: compared against the last 26.8 master because no 26.9 master baseline exists at or before the trusted base-commit anchor.

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

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: a9398e61-57eb-4804-bc3d-69fb1b405432
  • MIRAI run: e704c8c2-34e6-45b1-96b1-ee3b83656c94
  • PR check IDs:
    • clickbench_465980_1787756629
    • clickbench_465986_1787756629
    • clickbench_465992_1787756628
    • tpch_adapted_1_official_466009_1787756630
    • tpch_adapted_1_official_466021_1787756629
    • tpch_adapted_1_official_466503_1787756680

@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.60% 78.60% +0.00%

Changed lines: Changed C/C++ lines covered: 171/216 (79.17%) · Uncovered code

Full report · Diff report

Comment thread src/Functions/GatherUtils/Sources.h Resolved
Comment thread src/Functions/array/arrayElement.cpp Resolved
Comment thread src/Functions/GatherUtils/Sources.h Resolved
Comment thread src/Functions/array/arrayElement.cpp Resolved
Comment thread src/Functions/array/arrayElement.cpp Resolved
Comment thread src/Functions/array/arrayElement.cpp Resolved
Comment thread src/Functions/array/arrayElement.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8acb1900d with master 573230bfe (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

⚠️ Significant changes: object file sizes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 711.33 MiB 708.60 MiB -2.72 MiB (-0.38%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Object file sizes ⚠️

57 object files changed (+1.53 MiB total), 0 added.

Object file Master PR Δ
src/Functions/GatherUtils/CMakeFiles/clickhouse_functions_gatherutils.dir/sliceDynamicOffsetBounded… 1.19 MiB 1.47 MiB +290.58 KiB (+23.86%)
src/CMakeFiles/dbms.dir/Functions/GatherUtils/createArraySource.cpp.o 556.77 KiB 822.96 KiB +266.19 KiB (+47.81%)
src/Functions/GatherUtils/CMakeFiles/clickhouse_functions_gatherutils.dir/sliceDynamicOffsetUnbound… 968.27 KiB 1.12 MiB +178.88 KiB (+18.47%)
src/Functions/GatherUtils/CMakeFiles/clickhouse_functions_gatherutils.dir/sliceFromRightConstantOff… 906.73 KiB 1.04 MiB +158.40 KiB (+17.47%)
src/Functions/GatherUtils/CMakeFiles/clickhouse_functions_gatherutils.dir/sliceFromLeftConstantOffs… 883.97 KiB 1021.84 KiB +137.88 KiB (+15.60%)
src/Functions/GatherUtils/CMakeFiles/clickhouse_functions_gatherutils.dir/sliceFromLeftConstantOffs… 855.22 KiB 982.12 KiB +126.89 KiB (+14.84%)
src/Functions/GatherUtils/CMakeFiles/clickhouse_functions_gatherutils.dir/sliceFromRightConstantOff… 855.60 KiB 982.47 KiB +126.87 KiB (+14.83%)
src/Functions/array/CMakeFiles/clickhouse_functions_array.dir/arrayElement.cpp.o 9.36 MiB 9.45 MiB +89.09 KiB (+0.93%)
src/Functions/GatherUtils/CMakeFiles/clickhouse_functions_gatherutils.dir/has_all.cpp.o 3.02 MiB 3.05 MiB +32.54 KiB (+1.05%)
src/Functions/GatherUtils/CMakeFiles/clickhouse_functions_gatherutils.dir/has_any.cpp.o 3.02 MiB 3.06 MiB +32.54 KiB (+1.05%)
src/Functions/GatherUtils/CMakeFiles/clickhouse_functions_gatherutils.dir/starts_with.cpp.o 3.01 MiB 3.04 MiB +32.52 KiB (+1.06%)
src/Functions/GatherUtils/CMakeFiles/clickhouse_functions_gatherutils.dir/ends_with.cpp.o 3.01 MiB 3.04 MiB +32.51 KiB (+1.05%)
src/Functions/GatherUtils/CMakeFiles/clickhouse_functions_gatherutils.dir/has_substr.cpp.o 3.25 MiB 3.28 MiB +31.95 KiB (+0.96%)

737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

279 translation units recompiled, 2601 s compile time in total, 279 of them have a recent master baseline.

Job report

Comment thread src/Functions/array/arrayElement.cpp Resolved
@diegomestre2
diegomestre2 added this pull request to the merge queue Aug 27, 2026
Merged via the queue into master with commit 6e6a2a5 Aug 27, 2026
179 checks passed
@diegomestre2
diegomestre2 deleted the lazy_replication_stage2 branch August 27, 2026 08:25
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 27, 2026
pull Bot pushed a commit to erickirt/ClickHouse that referenced this pull request Sep 26, 2026
…ness's own queries

After a clean fuzz run (`Fuzzer exit code is 143`), the post-fuzz liveness
probe `clickhouse-client --handshake_timeout_ms=30000 --receive_timeout=5
--query 'SELECT 1'` sometimes never returns. status.tsv is then never
written, praktika kills the job at its 18000 s timeout before any artifact
is uploaded, and the job reports a bare ERROR with no server log. Five such
runs in 45 days: PR ClickHouse#112304 (2026-08-18), PR ClickHouse#98670 (2026-08-19), PR ClickHouse#115530
(2026-08-20), true master 9816d0a `AST fuzzer (amd_release, oracle)`
(2026-09-24) and PR ClickHouse#119237 `AST fuzzer (amd_debug, targeted,
old_compatibility)` (2026-09-25). Only the last one printed the `1`.

Two harness defects make the probe unbounded.

(a) The probe is itself fuzzed on the server. query-fuzzer-tweaks-users.xml
sets ast_fuzzer_runs=5 and ast_fuzzer_any_query=true on the `default`
profile, and no harness-owned client call opted out. The server runs the
mutated copies in a query-finish callback, and TCPHandler calls
io.onFinish() after sending the data and before EndOfStream, so a fuzzed
follow-up that does not finish holds the reply open after the `1` was
printed. The same pin was part of ClickHouse#113248, which was closed as already
integrated, but its run-fuzzer.sh half never reached master. The stress
harness (ClickHouse#114127), the log export (ClickHouse#117996) and tests/clickhouse-test
already pin it.

(b) --receive_timeout does not bound the probe either. On a receive
timeout ClientBase::receiveResult only records TIMEOUT_EXCEEDED and cancels
the query; from then on it polls the connection until the server sends a
packet, with no bound. A server whose connection thread never gets back to
the socket holds the probe forever, which is what the four runs that
printed nothing show.

Pin --ast_fuzzer_runs=0 on every harness-owned client call through one
variable; the fuzzer's own client stays unpinned. Run the two post-fuzz
probes (the restart liveness check and the probe loop) under
`timeout --signal TERM --kill-after=10 60`. A probe killed by timeout
(rc 124, or 137 after the kill) is counted like the client's own
"Timeout exceeded while", so a server that stops answering reaches the
existing "treating server as hung" verdict after 12 retries (about
14 minutes) and the job keeps its logs. 60 s covers the probe's own worst
case (30 s handshake, 5 s receive timeout, the cancel round trip) with room
for a slow sanitizer server. The fallback branch now prints the probe's
exit code instead of the last grep's.

The pre-fuzz probes are not wrapped: the check after attaching gdb waits
for gdb to load debug info on purpose, and the server is fresh there.

One server-side hang the fuzzer produces, a self-deadlock when one query
builds two system.stack_trace sources, is fixed separately in ClickHouse#120216.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fm4v added a commit to m-selmi/ClickHouse that referenced this pull request Oct 8, 2026
… to 26.9

The memory regression (ClickHouse#123756) is fixed in 26.9 by ClickHouse#112304, so `compatibility` below 26.9 restores the previous measurement.
@robot-clickhouse robot-clickhouse added pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Oct 8, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 9, 2026
Backport #112304 to 26.8: Supporting lazily replicated arrays on arrayElement and arraySlice (reduces memory usage and improves performance)
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 9, 2026
…ys on arrayElement and arraySlice (reduces memory usage and improves performance)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport

Projects

None yet

5 participants