Skip to content

Reduce double-copy of data on Map functions - #111749

Merged
diegomestre2 merged 13 commits into
ClickHouse:masterfrom
diegomestre2:map_high_order
Aug 11, 2026
Merged

diegomestre2 merged 13 commits into
ClickHouse:masterfrom
diegomestre2:map_high_order

Conversation

@diegomestre2

@diegomestre2 diegomestre2 commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Part 2 of avoiding multiple copies of data in higher-order functions (Depends on Part 1: #111581, arrays).

mapContainsKeyLike, mapContainsValueLike, mapExtractKeyLike and mapExtractValueLike
have no SQL lambda, but are implemented through the same machinery: MapLikeAdapter
synthesizes a lambda-like ColumnFunction that captures the pattern column, and the array
implementation replicates it by the map offsets before appending the keys/values.

That synthetic ColumnFunction was created without allow_lazy_replicated_captures, so a non-constant pattern was physically replicated once per map entry at capture time and the expanded column stayed alive through the whole arrayExists/arrayFilter pipeline, even with enable_lazy_columns_replication enabled.

With this change the capture stays lazy (ColumnReplicated) until the synthetic lambda is executed. The pattern is still materialized in full at that point: IExecutableFunction::execute falls back to full materialization because the lambda receives a mix of replicated (pattern) and full (keys/values) arguments.

Related: #111581
Related: #54967

Performance Test:

Query Metric eager (enable_lazy_columns_replication = 0) lazy (enable_lazy_columns_replication = 1) Gain
mapContainsKeyLike, 100 KB pattern median 1.68 s 1.18 s 1.42× faster
mapContainsKeyLike, 100 KB pattern p90 2.22 s 1.35 s 1.64×
mapContainsKeyLike, 100 KB pattern peak memory 2.05 GB 1.47 GB −580 MB (−28%)
mapExtractValueLike, 100 KB pattern median 1.71 s 1.25 s 1.37× faster
mapContainsKeyLike, tiny pattern, 200k rows median 0.261 s 0.261 s no regression

Changes

  • FunctionMapToArrayAdapter takes ContextPtr and reads enable_lazy_columns_replication
    at function creation time.
  • The flag is forwarded to Adapter::extractNestedTypesAndColumns via a requires-clause
    dispatch.
  • MapLikeAdapter passes the flag to ColumnFunction::create for the captured pattern, so ColumnFunction::replicate wraps it in ColumnReplicated instead of copying at capture time. The full copy still happens when the lambda executes, but it is deferred and shorter-lived.

Changelog category (leave one):

  • Performance Improvement

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

Functions mapContainsKeyLike, mapContainsValueLike, mapExtractKeyLike and mapExtractValueLike with a non-constant pattern use less memory and time when enable_lazy_columns_replication is enabled. The pattern is no longer eagerly copied per map entry at capture time; the copy is deferred to the execution of the internal lambda and is freed sooner, so one fewer expanded copy of the pattern exists at peak.

Version info

  • Merged into: 26.8.1.1164 (included in 26.8 and later)

@clickhouse-gh

clickhouse-gh Bot commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5880712]

Summary: ✅


AI Review

Summary

This PR threads enable_lazy_columns_replication into the synthetic lambda path used by mapContains*Like / mapExtract*Like, so the captured non-constant pattern stays as ColumnReplicated until the internal lambda executes instead of being eagerly expanded at capture time. I did not find a remaining correctness, contract, or coverage issue in the current diff: the implementation matches the updated PR description and changelog, and the new tests cover both result stability and the intended peak-memory improvement path.

Final Verdict

✅ No review findings in the current diff.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.60% 86.60% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.90% 78.90% +0.00%

Changed lines: Changed C/C++ lines covered: 36/37 (97.30%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jul 24, 2026
@diegomestre2
diegomestre2 marked this pull request as ready for review August 6, 2026 13:29
@diegomestre2 diegomestre2 added can be tested Allows running workflows for external contributors performance labels Aug 6, 2026
@diegomestre2
diegomestre2 requested a review from Manerone August 6, 2026 13:41
@clickhouse-gh

clickhouse-gh Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

No arm_release build profile data for commit 5880712 - the build was skipped, reused from cache, or predates profile upload.

@clickhouse-gh

clickhouse-gh Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This change makes mapLike functions (mapContainsKeyLike, mapExtractKeyLike, etc.) capture their pattern column lazily to cut peak memory, and adds optional ContextPtr construction to the LowCardinality FastPath mixin. ClickBench Q17 is a GROUP BY aggregation over hits and does not exercise any mapLike function or the changed capture path, so the flagged -10.1% improvement cannot plausibly be attributed to this PR and is downgraded to not_sure as run-to-run variance. The remaining diff (comment edits and the constructor plumbing) is behavior-neutral for existing functions. No cross-query pattern emerged from a single flagged query.

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 17 not_sure 556 500 -10.1% <0.0001 aggregation: This PR only changes the lazy-replication path of map*Like functions plus FastPath constructor plumbing; ClickBench Q17 is a plain GROUP BY aggregation that touches none of it, so the -10.1% is run-to-run variance.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: fc8fb5d5-5096-4dfc-9a8b-e3744cc62858
  • MIRAI run: 4f6036bb-dd43-4d24-9c7d-c47696e4f1a5
  • PR check IDs:
    • clickbench_586709_1786364387
    • clickbench_586715_1786364387
    • clickbench_586732_1786364387
    • tpch_adapted_1_official_586742_1786364387
    • tpch_adapted_1_official_586765_1786364388
    • tpch_adapted_1_official_586779_1786364388

@diegomestre2
diegomestre2 added this pull request to the merge queue Aug 11, 2026
Merged via the queue into ClickHouse:master with commit 451e003 Aug 11, 2026
701 of 705 checks passed
@diegomestre2
diegomestre2 deleted the map_high_order branch August 11, 2026 12:37
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 11, 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 performance pr-performance Pull request with some performance improvements 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