Skip to content

Text index: pre-insert the postprocessor stop words into the token map - #111690

Merged
ahmadov merged 11 commits into
masterfrom
ahmadov/postprocess-fast-filter-2
Aug 25, 2026
Merged

ahmadov merged 11 commits into
masterfrom
ahmadov/postprocess-fast-filter-2

Conversation

@ahmadov

@ahmadov ahmadov commented Jul 23, 2026 •

Copy link
Copy Markdown
Member

A follow-up of 109049.

Before, every token was hashed twice during the index build: once to check it against the stop-word list and once to add it to the token map.

Now the stop words are put into the map up front and marked, so the map lookup alone tells whether to keep or drop a token.

Changelog category (leave one):

  • Performance Improvement

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

Reduce hashing of tokens defined in the postprocessor IN/NOT IN filter.


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

Before, every token was hashed twice during the index build: once to check
it against the stop-word list and once to add it to the token map.

Now the stop words are put into the map up front and marked, so the map
lookup alone tells whether to keep or drop a token.
@clickhouse-gh

clickhouse-gh Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bc68a9b]

Summary: ✅


AI Review

Summary

This PR rewires the text-index filter-only postprocessor fast path so the builder can reuse per-token state instead of consulting the filter set on every occurrence, and it broadens the performance benchmark coverage. The main logic looks consistent with the existing functional tests, but the new keep-set seeding path still bypasses text_index_max_memory_usage_before_flush, so I would not merge it until that accounting gap is fixed.

Findings

⚠️ Majors

  • [src/Storages/MergeTree/MergeTreeIndexText.cpp:1627, src/Storages/MergeTree/MergeTreeIndexText.cpp:1762, src/Storages/MergeTree/MergeTreeIndexText.cpp:1788, src/Storages/MergeTree/TextIndexUtils.cpp:171-179] The new NOT IN/keep-set fast path allocates the whole seeded token map in seedDropFilter from the aggregator constructor and again from reset, but BuildTextIndexTransform only charges bytes that appear inside aggregator_text.update. With a low text_index_max_memory_usage_before_flush and a keep-set that never matches any input token, estimated_allocated_bytes stays at 0 while the builder still retains the entire pre-seeded map, so the new path can exceed the configured flush threshold by the size of the keep-set. Suggested fix: either account the seeded bytes explicitly when enabling/resetting the fast path, or move the seeding into the memory-measured update path.
Tests

⚠️ Extend tests/queries/0_stateless/04627_text_index_flush_memory_tracking.sql with a NOT IN/keep-set case whose literals never occur in the input. Under a low text_index_max_memory_usage_before_flush, that path should still force multiple temporary segments; otherwise this regression stays uncovered.

Final Verdict

Changes requested: the seeded-memory accounting gap breaks the contract of text_index_max_memory_usage_before_flush for the new keep-set fast path.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.80% 86.90% +0.10%
Functions 92.00% 92.00% +0.00%
Branches 79.20% 79.30% +0.10%

Changed lines: Changed C/C++ lines covered: 77/81 (95.06%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jul 23, 2026
@ahmadov
ahmadov marked this pull request as ready for review July 28, 2026 08:20
@ahmadov
ahmadov requested review from Ergus and rschu1ze July 28, 2026 08:23

@Ergus Ergus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first comment is the only real issue IMO.

/// `NOT IN`: invert so `shouldDrop` fires for tokens outside the set.
filter.drop_on_match = is_not_in ? !drop_on_condition : drop_on_condition;

if (literals.size() > max_filter_set_size)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This disables the drop-set fast path right? Is it intended?

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.

yes, it's intended. Having more than 8192 tokens in the filter would follow the normal path. It would happen very rarely at all.

@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

🟢 AI verdict: improvement — 3 query(s) improved out of 35 analysed

This PR's only functional change is the text-index build fast path for IN/NOT IN filter postprocessors (MergeTreeIndexText); the rest is a perf-test XML update. ClickBench and TPC-H queries never build or query these text indexes, so none of the flagged improvements can be attributed to the diff. Six moderate ClickBench deltas were downgraded as off-path variance. Three very large deltas remain flagged only because they clear the hard trust threshold (clickbench Q23 ×2.2 faster, tpch_adapted_1_official Q7 ×8.2 faster, Q8 ×2.1 faster), but they exercise no changed code and most likely reflect a baseline/measurement shift — verify before treating them as real gains.

clickbench

🟢 1 improved · ⚠️ 6 inconclusive

Flagged queries (7 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
🟢 23 improvement 164 75 ×2.2 faster <0.0001 ×2.2 faster and consistent, but Q23 runs none of the text-index build code this PR changes; most likely a baseline/measurement shift rather than a PR effect.
⚠️ 13 not_sure 422 341 -19.2% <0.0001 This PR only changes the text-index build fast path for IN/NOT IN filter postprocessors; Q13 builds and uses no text index, so this delta is off-path variance.
⚠️ 15 not_sure 192 163 -15.1% <0.0001 The diff touches only text-index construction (MergeTreeIndexText). Q15 exercises none of that path, so the −15.1% is run-to-run variance, not a PR effect.
⚠️ 16 not_sure 734 628 -14.5% <0.0001 Q16 does not build or query a text index, and this PR changes only the text-index build path, so the −14.5% cannot be attributed to the diff.
⚠️ 18 not_sure 1244 1071 -13.9% <0.0001 The only functional change here is the text-index IN/NOT IN build fast path; Q18 never touches it, so the −13.9% is off-path noise.
⚠️ 31 not_sure 408 356 -12.9% <0.0001 Q31 runs no text-index build code, which is all this PR modifies, so the −12.9% is unrelated to the diff and reads as measurement variance.
⚠️ 33 not_sure 1250 1168 -6.5% <0.0001 The diff is confined to text-index construction; Q33 exercises none of it, so this −6.5% is off-path variance rather than a PR-driven gain.

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

🟢 2 improved

Flagged queries (2 of 22)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
🟢 7 improvement 560 68 ×8.2 faster <0.0001 ×8.2 faster is far too large for a text-index build-path change that TPC-H never exercises; almost certainly a baseline shift, not a real PR effect.
🟢 8 improvement 166 79 ×2.1 faster <0.0001 ×2.1 faster, but Q8 runs no text-index build code touched by this PR; read as baseline/measurement variance rather than a PR-driven gain.

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.

Debug info
  • StressHouse run: 9bee3694-1562-47c9-a656-d02be835e081
  • MIRAI run: 02d61ef6-a92e-4bbb-93a0-21f7a7511b7a
  • PR check IDs:
    • clickbench_1963627_1787073738
    • clickbench_1963640_1787073739
    • clickbench_1963646_1787073739
    • tpch_adapted_1_official_1963658_1787073738
    • tpch_adapted_1_official_1963683_1787073738
    • tpch_adapted_1_official_1963697_1787073738

@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 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.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 77/81 (95.06%) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

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

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 702.05 MiB 699.03 MiB -3.02 MiB (-0.43%)

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.

Compile time of recompiled translation units

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

Job report

@ahmadov
ahmadov added this pull request to the merge queue Aug 25, 2026
Merged via the queue into master with commit 3390d3a Aug 25, 2026
182 checks passed
@ahmadov
ahmadov deleted the ahmadov/postprocess-fast-filter-2 branch August 25, 2026 10:49
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 25, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@ahmadov @Ergus — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

nihalzp pushed a commit to nihalzp/ClickHouse that referenced this pull request Nov 16, 2026
All four entries of tests/performance/text_index_postprocessor_filter.xml
take 1.07-1.27 s on MasterCI (amd, arm 1.01-1.19 s). Each INSERT reads 3M
rows and writes three ~1M-row parts in parallel, so a run takes about one
part's text index build and write; half the rows would still write a
~1M-row part.

No existing query number or text changes: 2 queries (4 entries with the
substitution) are disabled and 2 copies (4 entries) appended as 4-7. The
copies read the first 500k of the same rows (the source fill is renamed
to tab_src_500k_{drop_pct}, read only by the copies) and write one part
of 500k rows into the same target tables.

Local A/B on official builds, two repetitions on different NUMA nodes,
L3 capped to one way (decide.py v2: every gate passes in both):
- per-row index and compression time of a copy 0.986-1.009 of its
  original, bytes per row 1.000, index share of the part write +-0.002;
- local time ratio copy/original 0.421-0.462 -> projected CI medians
  0.45-0.58 s amd, 0.42-0.54 s arm;
- on the MasterCI builds before and after ClickHouse#111690 (608eeb8 -> 04971e1,
  CI measured 1.28-1.40x on amd), the copies keep 0.77-1.05 of the
  original's speedup, both seat orders.
Saving: about 25 s of query time per master perf job on each
architecture; the setup fills 1.5M instead of 9M rows.

Related: ClickHouse#129258

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

5 participants