Skip to content

Support running local TopN for multi-stage late materialization - #11005

Merged
ti-chi-bot[bot] merged 18 commits into
pingcap:feature/release-8.5-materialized-viewfrom
windtalker:feature/mslm-running-local-topn-design
Aug 1, 2026
Merged

Support running local TopN for multi-stage late materialization#11005
ti-chi-bot[bot] merged 18 commits into
pingcap:feature/release-8.5-materialized-viewfrom
windtalker:feature/mslm-running-local-topn-design

Conversation

@windtalker

@windtalker windtalker commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: ref #11003

Problem Summary:

This PR improves multi-stage late materialization for queries with TopN by avoiding materializing all rest columns before local TopN pruning.

What is changed and how it works?

Support running local TopN for multi-stage late materialization.

When a supported TopN is above a table scan using multi-stage late materialization, TiFlash can run a local TopN on the stage-1 candidate rows first. Then it reads the final rest columns only for the TopN candidates instead of all rows passing the stage-1 filters.

This PR also adds runtime statistics and logging refinements for the multi-stage late materialization path.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

None

Summary by CodeRabbit

  • New Features
    • Added multi-stage late materialization TopN with adaptive “running local TopN” to cut final materialization work for eligible TopN queries.
    • Added integer modes for dt_enable_multi_stage_late_materialization: disabled (0), selection-only (1), TopN-enhanced (2).
  • Bug Fixes
    • Corrected enablement gating for mode 0 and improved propagation of TopN configuration through delta-merge execution.
    • Updated runtime stats and executor row-override behavior to match TopN vs non-TopN modes.
  • Documentation
    • Updated design docs for multi-stage late materialization and running-local TopN.
  • Tests
    • Expanded tests for mode handling and adaptive TopN warmup/disable behavior.

@ti-chi-bot ti-chi-bot Bot added release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jul 28, 2026
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The PR changes multi-stage late materialization to use numeric modes and adds a Running Local TopN path. TopN metadata flows from physical plans through DeltaMerge execution, where typed sort keys, adaptive candidate filtering, runtime statistics, documentation, and tests are added.

Changes

Multi-stage late materialization TopN

Layer / File(s) Summary
TopN contracts and design
dbms/src/Interpreters/Settings.h, dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationTopN.*, dbms/src/Storages/SelectQueryInfo.*, dbms/src/Storages/DeltaMerge/Filter/PushDownFilter.h, docs/design/*
The setting becomes a three-mode integer selector, with TopN descriptions, typed sort keys, query metadata, pushdown metadata, and design documentation.
Planner eligibility and metadata wiring
dbms/src/Storages/StorageDeltaMerge.cpp, dbms/src/Flash/Planner/Plans/*, dbms/src/Debug/MockStorage.*
TopN plans are validated and attached to supported scans, Stage 1 columns are merged, and configuration is passed through DeltaMerge APIs.
Running local TopN algorithm
dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationTopN.*
A bounded heap processes typed nullable sort keys and produces candidate filters for Stage 1 rows.
Stage 1 execution and runtime statistics
dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.*, dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationRuntimeStats.h, dbms/src/Flash/Coprocessor/*
DeltaMerge streams apply RunningLocalTopN with adaptive disabling, report candidate and stream counters, and propagate TopN metadata through query construction.
Mode coverage and adaptive TopN tests
dbms/src/Flash/tests/gtest_executors_with_dm.cpp, dbms/src/Storages/DeltaMerge/tests/gtest_skippable_block_input_stream.cpp
Tests cover disabled, selection-only, and TopN-enhanced modes, executor traversal, pushed-down-filter requests, and adaptive behavior.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant PhysicalTopN
  participant PhysicalTableScan
  participant StorageDeltaMerge
  participant DeltaMergeStream
  participant RunningLocalTopN
  participant RuntimeStats
  PhysicalTopN->>PhysicalTableScan: attach TopN description
  PhysicalTableScan->>StorageDeltaMerge: build TopN-aware filter
  StorageDeltaMerge->>DeltaMergeStream: pass TopN metadata
  DeltaMergeStream->>RunningLocalTopN: update Stage 1 block
  RunningLocalTopN-->>DeltaMergeStream: return candidate filter
  DeltaMergeStream->>RuntimeStats: record candidates and stream state
Loading

Possibly related issues

  • pingcap/tiflash#11003 — Extends the same multi-stage late materialization implementation with TopN-aware propagation and execution.

Possibly related PRs

  • pingcap/tiflash#10999 — Adds the preceding multi-stage late materialization row-override and runtime-statistics plumbing extended here for TopN.

Poem

A rabbit bounds through rows so bright,
With heaps of keys arranged just right.
Filters trim the reading trail,
While adaptive counters tell the tale.
TopN hops through every stream—
MSLM now can chase the dream.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 5.97% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: enabling local TopN for multi-stage late materialization.
Description check ✅ Passed The description follows the template, includes issue/problem summary, implementation details, and a unit test checklist item.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
dbms/src/Flash/Planner/Plans/PhysicalMockTableScan.cpp (1)

220-254: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

mock_streams cached before TopN attachment; setMultiStageLateMaterializationTopN never refreshes them.

setFilterConditions (lines 220-254) eagerly rebuilds mock_streams via getStreamFromDeltaMerge(..., multi_stage_late_materialization_topn), reading the member's value at that call time. But setMultiStageLateMaterializationTopN (267-272) — invoked later from PhysicalTopN::build/tryAttachMultiStageLateMaterializationTopN once the child (table scan + pushed-down selection) already exists — only assigns the member and never rebuilds mock_streams. Since buildBlockInputStreamImpl just replays the already-cached mock_streams, the stream-execution-model path silently loses the TopN optimization for any DAG shaped TableScan -> Selection -> TopN, even though buildPipelineExecGroupImpl (pipeline model) reads the member live and works correctly. This asymmetry could make new unit tests validating the TopN feature pass/fail inconsistently depending on execution model.

Consider rebuilding mock_streams inside setMultiStageLateMaterializationTopN when hasFilterConditions() is true (mirroring the rebuild already done in setFilterConditions), or deferring the initial getStreamFromDeltaMerge call until all wiring (filter + topn) is finalized.

Also applies to: 267-272

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@dbms/src/Flash/Planner/Plans/PhysicalMockTableScan.cpp` around lines 220 -
254, Update setMultiStageLateMaterializationTopN so that, after assigning the
TopN configuration, it rebuilds mock_streams when hasFilterConditions() is true,
using the same DeltaMerge stream construction and current scan/filter state as
setFilterConditions. Preserve the existing behavior when filters are not
attached, and ensure the rebuilt stream receives the updated
multi_stage_late_materialization_topn value.
🧹 Nitpick comments (8)
dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationRuntimeStats.h (1)

35-44: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Prefer tryLogCurrentException over a silent catch (...).

Swallowing the exception silently hides real failures (e.g. bad format args) in the destructor path.

As per coding guidelines: "Use tryLogCurrentException(log, "context") in broad catch (...) paths to avoid duplicated exception-formatting code".

♻️ Proposed fix
         catch (...)
         {
+            tryLogCurrentException(log, "MultiStageLateMaterializationRuntimeStats::logSummary");
         }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationRuntimeStats.h`
around lines 35 - 44, Update the destructor of
MultiStageLateMaterializationRuntimeStats to call tryLogCurrentException in the
broad catch (...) block, passing the appropriate logger and context for the
logSummary failure; do not silently swallow exceptions.

Source: Coding guidelines

dbms/src/Flash/tests/gtest_executors_with_dm.cpp (2)

375-382: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use camelCase for the new helper parameters.

Rename pushed_down_filter, residual_filter, order_by_column, and is_desc to pushedDownFilter, residualFilter, orderByColumn, and isDesc, including the named arguments at Lines 623-630, 710-717, and 786-793.

As per coding guidelines, method and variable names should use camelCase.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@dbms/src/Flash/tests/gtest_executors_with_dm.cpp` around lines 375 - 382,
Rename the parameters of buildDAGRequestWithPushedDownFilterAndTopN from
pushed_down_filter, residual_filter, order_by_column, and is_desc to
pushedDownFilter, residualFilter, orderByColumn, and isDesc, and update all
corresponding references and named arguments at the indicated call sites.

Source: Coding guidelines


623-630: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Make TopN request construction independent of the default setting.

PhysicalTopN::build reads the MSLM setting while building the request, but all three tests select MSLMModeTopN afterward. Set the mode before each request is built.

  • dbms/src/Flash/tests/gtest_executors_with_dm.cpp#L623-L630: set MSLMModeTopN before buildDAGRequestWithPushedDownFilterAndTopN.
  • dbms/src/Flash/tests/gtest_executors_with_dm.cpp#L710-L717: set MSLMModeTopN before building the DateTime request.
  • dbms/src/Flash/tests/gtest_executors_with_dm.cpp#L786-L793: set MSLMModeTopN before building the string-order request.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@dbms/src/Flash/tests/gtest_executors_with_dm.cpp` around lines 623 - 630, Set
the MSLM mode to MSLMModeTopN before constructing each request with
buildDAGRequestWithPushedDownFilterAndTopN, because PhysicalTopN::build reads
the setting during construction. Apply this ordering change at
dbms/src/Flash/tests/gtest_executors_with_dm.cpp lines 623-630, 710-717, and
786-793; preserve the existing request parameters and subsequent test logic.
dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationTopN.h (1)

33-149: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use the project’s C++ naming and width-type conventions for the new TopN contract.

  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationTopN.h#L33-L149: rename new variables/methods to camelCase (for example, orderByColumns, columnId, currentBlockSequence) and use Int32 for direction.
  • dbms/src/Storages/SelectQueryInfo.h#L67-L67: rename the new metadata member to camelCase and update its consumers.
  • dbms/src/Debug/MockStorage.h#L112-L112: rename the new parameter to camelCase.
  • dbms/src/Debug/MockStorage.h#L127-L127: rename the new parameter to camelCase.

As per coding guidelines, “Method and variable names should use camelCase” and explicit-width types should be used from dbms/src/Core/Types.h.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationTopN.h` around
lines 33 - 149, Apply the TopN naming and type conventions across all affected
sites: in
dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationTopN.h:33-149, rename
new variables and methods to camelCase and change direction to Int32; in
dbms/src/Storages/SelectQueryInfo.h:67-67, rename the new metadata member to
camelCase and update all consumers; in dbms/src/Debug/MockStorage.h:112-112 and
:127-127, rename each new parameter to camelCase.

Source: Coding guidelines

docs/design/2026-07-26-running-local-topn-for-mslm.md (1)

198-203: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Align the design pseudocode with the implemented API.

  • docs/design/2026-07-26-running-local-topn-for-mslm.md#L198-L203: use the actual MultiStageLateMaterializationTopNDescription structure or explicitly label this as conceptual.
  • docs/design/2026-07-26-running-local-topn-for-mslm.md#L342-L349: remove stream_sequence, which is not present in HeapEntry.
  • docs/design/2026-07-26-running-local-topn-for-mslm.md#L393-L405: update RunningLocalTopN::update to its actual result and parameters.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/design/2026-07-26-running-local-topn-for-mslm.md` around lines 198 -
203, Align the design pseudocode with the implemented API: at
docs/design/2026-07-26-running-local-topn-for-mslm.md:198-203, use
MultiStageLateMaterializationTopNDescription or explicitly mark
MSLMTopNDescription as conceptual; at :342-349, remove stream_sequence from
HeapEntry; at :393-405, update RunningLocalTopN::update to match its actual
parameters and return result.
dbms/src/Storages/StorageDeltaMerge.cpp (2)

846-859: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Duplicated topk/order-by bound validation.

The topk == 0, topk > multi_stage_late_materialization_topn_max_topk, and order_by_columns.size() > multi_stage_late_materialization_topn_max_order_by_columns checks here duplicate the same checks in tryBuildMultiStageLateMaterializationTopN (dbms/src/Flash/Planner/Plans/PhysicalTopN.cpp, lines 52-57). If the bounds ever change, both call sites must be updated in lockstep. Consider extracting a tiny shared helper (e.g., in MultiStageLateMaterializationTopN.h/.cpp) that validates (topk, order_by_size) against the two limits.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@dbms/src/Storages/StorageDeltaMerge.cpp` around lines 846 - 859, Extract the
duplicated topk and order_by_columns bound validation from the StorageDeltaMerge
path and tryBuildMultiStageLateMaterializationTopN into a shared helper in
MultiStageLateMaterializationTopN.h/.cpp. Have both call sites use the helper
while preserving their existing disable/error behavior and the separate empty
order-by validation.

908-920: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Magic numbers for the "worth-it" heuristic.

10 and the 2 * ratio decide whether TopN-enhanced multi-stage late materialization is enabled, but they're unexplained literals. Extracting them into named constexpr (e.g., multi_stage_late_materialization_topn_min_final_rest_columns, ..._min_rest_to_stage1_ratio) with a short rationale comment would make the heuristic easier to tune/audit later.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@dbms/src/Storages/StorageDeltaMerge.cpp` around lines 908 - 920, Replace the
unexplained literals in the worth-it heuristic within the final rest-column
checks with named constexpr constants for the minimum final-rest-column count
and minimum rest-to-stage1 ratio. Add a brief rationale comment explaining these
thresholds, then use the constants in the checks while preserving the existing
behavior.
dbms/src/Flash/Planner/Plans/PhysicalTopN.cpp (1)

141-144: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Replace the magic 2 with a named late-materialization mode.

The setting is raw unsigned-integer typed and documented as 0: disabled, 1: selection only, 2: TopN enhanced, but PhysicalTopN.cpp uses the literal 2 directly. Add/use a named mode/const value such as MultiStageLateMaterializationMode::TopN so the top-N-enhanced path is self-documenting and less brittle.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@dbms/src/Flash/Planner/Plans/PhysicalTopN.cpp` around lines 141 - 144,
Replace the literal comparison to 2 in the TopN planning branch with the named
late-materialization mode representing TopN enhancement, such as
MultiStageLateMaterializationMode::TopN. Reuse the existing mode definition if
available, adding one only where the setting’s documented values are defined,
while preserving the current enabled and disabled control flow.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@dbms/src/Flash/tests/gtest_executors_with_dm.cpp`:
- Line 688: Update the DateTime fixture construction in the test so rows 24–31
derive a valid day and hour from row: use the day offset 1 + row / 24 and hour
row % 24, while preserving the existing date and other components.

In `@dbms/src/Interpreters/Settings.h`:
- Line 229: Update the dt_enable_multi_stage_late_materialization setting
definition to preserve compatibility with existing true/false configuration
values while retaining its UInt64 modes 0, 1, and 2. Use the setting’s parsing
or alias mechanism rather than requiring numeric-only input, and ensure both
boolean aliases and numeric values deserialize correctly.

In `@dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationTopN.cpp`:
- Line 277: Update the unsupported-order-type throw in the TopN-enhanced
late-materialization logic to use the fmt-style DB::Exception constructor with a
suitable ErrorCodes value, preferably NOT_IMPLEMENTED. Add the corresponding
ErrorCodes declaration near the file’s existing declarations and pass the type
name as a format argument instead of concatenating strings.

---

Outside diff comments:
In `@dbms/src/Flash/Planner/Plans/PhysicalMockTableScan.cpp`:
- Around line 220-254: Update setMultiStageLateMaterializationTopN so that,
after assigning the TopN configuration, it rebuilds mock_streams when
hasFilterConditions() is true, using the same DeltaMerge stream construction and
current scan/filter state as setFilterConditions. Preserve the existing behavior
when filters are not attached, and ensure the rebuilt stream receives the
updated multi_stage_late_materialization_topn value.

---

Nitpick comments:
In `@dbms/src/Flash/Planner/Plans/PhysicalTopN.cpp`:
- Around line 141-144: Replace the literal comparison to 2 in the TopN planning
branch with the named late-materialization mode representing TopN enhancement,
such as MultiStageLateMaterializationMode::TopN. Reuse the existing mode
definition if available, adding one only where the setting’s documented values
are defined, while preserving the current enabled and disabled control flow.

In `@dbms/src/Flash/tests/gtest_executors_with_dm.cpp`:
- Around line 375-382: Rename the parameters of
buildDAGRequestWithPushedDownFilterAndTopN from pushed_down_filter,
residual_filter, order_by_column, and is_desc to pushedDownFilter,
residualFilter, orderByColumn, and isDesc, and update all corresponding
references and named arguments at the indicated call sites.
- Around line 623-630: Set the MSLM mode to MSLMModeTopN before constructing
each request with buildDAGRequestWithPushedDownFilterAndTopN, because
PhysicalTopN::build reads the setting during construction. Apply this ordering
change at dbms/src/Flash/tests/gtest_executors_with_dm.cpp lines 623-630,
710-717, and 786-793; preserve the existing request parameters and subsequent
test logic.

In `@dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationRuntimeStats.h`:
- Around line 35-44: Update the destructor of
MultiStageLateMaterializationRuntimeStats to call tryLogCurrentException in the
broad catch (...) block, passing the appropriate logger and context for the
logSummary failure; do not silently swallow exceptions.

In `@dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationTopN.h`:
- Around line 33-149: Apply the TopN naming and type conventions across all
affected sites: in
dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationTopN.h:33-149, rename
new variables and methods to camelCase and change direction to Int32; in
dbms/src/Storages/SelectQueryInfo.h:67-67, rename the new metadata member to
camelCase and update all consumers; in dbms/src/Debug/MockStorage.h:112-112 and
:127-127, rename each new parameter to camelCase.

In `@dbms/src/Storages/StorageDeltaMerge.cpp`:
- Around line 846-859: Extract the duplicated topk and order_by_columns bound
validation from the StorageDeltaMerge path and
tryBuildMultiStageLateMaterializationTopN into a shared helper in
MultiStageLateMaterializationTopN.h/.cpp. Have both call sites use the helper
while preserving their existing disable/error behavior and the separate empty
order-by validation.
- Around line 908-920: Replace the unexplained literals in the worth-it
heuristic within the final rest-column checks with named constexpr constants for
the minimum final-rest-column count and minimum rest-to-stage1 ratio. Add a
brief rationale comment explaining these thresholds, then use the constants in
the checks while preserving the existing behavior.

In `@docs/design/2026-07-26-running-local-topn-for-mslm.md`:
- Around line 198-203: Align the design pseudocode with the implemented API: at
docs/design/2026-07-26-running-local-topn-for-mslm.md:198-203, use
MultiStageLateMaterializationTopNDescription or explicitly mark
MSLMTopNDescription as conceptual; at :342-349, remove stream_sequence from
HeapEntry; at :393-405, update RunningLocalTopN::update to match its actual
parameters and return result.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5b19c44f-11d3-494c-858d-02fcb6c17ab9

📥 Commits

Reviewing files that changed from the base of the PR and between 8a01c28 and 4bf257d.

📒 Files selected for processing (23)
  • dbms/src/Debug/MockStorage.cpp
  • dbms/src/Debug/MockStorage.h
  • dbms/src/Flash/Coprocessor/DAGStorageInterpreter.cpp
  • dbms/src/Flash/Coprocessor/DAGStorageInterpreter.h
  • dbms/src/Flash/Planner/Plans/PhysicalMockTableScan.cpp
  • dbms/src/Flash/Planner/Plans/PhysicalMockTableScan.h
  • dbms/src/Flash/Planner/Plans/PhysicalTableScan.cpp
  • dbms/src/Flash/Planner/Plans/PhysicalTableScan.h
  • dbms/src/Flash/Planner/Plans/PhysicalTopN.cpp
  • dbms/src/Flash/tests/gtest_executors_with_dm.cpp
  • dbms/src/Interpreters/Settings.h
  • dbms/src/Storages/DeltaMerge/Filter/PushDownFilter.h
  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.cpp
  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.h
  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationRuntimeStats.h
  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationTopN.cpp
  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationTopN.h
  • dbms/src/Storages/DeltaMerge/Segment.cpp
  • dbms/src/Storages/SelectQueryInfo.cpp
  • dbms/src/Storages/SelectQueryInfo.h
  • dbms/src/Storages/StorageDeltaMerge.cpp
  • docs/design/2026-07-25-multi-stage-late-materialization.md
  • docs/design/2026-07-26-running-local-topn-for-mslm.md

for (size_t row = 0; row < rows; ++row)
{
c0_values.push_back(row);
ts_values.push_back(MyDateTime(2020, 1, 1, static_cast<UInt32>(row), 0, 0, 0).toPackedUInt());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Keep the DateTime fixture values valid.

Rows 24-31 pass hours 24-31 to MyDateTime. Build the day and hour from the row index, for example day = 1 + row / 24 and hour = row % 24, so this test exercises valid DateTime values.

Suggested fix
-        ts_values.push_back(MyDateTime(2020, 1, 1, static_cast<UInt32>(row), 0, 0, 0).toPackedUInt());
+        ts_values.push_back(
+            MyDateTime(
+                2020,
+                1,
+                static_cast<UInt32>(1 + row / 24),
+                static_cast<UInt32>(row % 24),
+                0,
+                0,
+                0)
+                .toPackedUInt());
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
ts_values.push_back(MyDateTime(2020, 1, 1, static_cast<UInt32>(row), 0, 0, 0).toPackedUInt());
ts_values.push_back(
MyDateTime(
2020,
1,
static_cast<UInt32>(1 + row / 24),
static_cast<UInt32>(row % 24),
0,
0,
0)
.toPackedUInt());
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@dbms/src/Flash/tests/gtest_executors_with_dm.cpp` at line 688, Update the
DateTime fixture construction in the test so rows 24–31 derive a valid day and
hour from row: use the day offset 1 + row / 24 and hour row % 24, while
preserving the existing date and other components.

Comment thread dbms/src/Interpreters/Settings.h Outdated
if (checkDataType<DataTypeMyDateTime>(type_not_null.get()))
return makeSortKeyTypeInfo<UInt64, UInt64, SortKeyKind::DateTime, ColumnVector<UInt64>>();

throw Exception("Unsupported order by type for TopN-enhanced multi-stage late materialization: " + type->getName());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Use the fmt-style DB::Exception constructor with an error code.

String concatenation without an error code deviates from the repo convention for storage-engine error handling.

As per coding guidelines: "Use DB::Exception for error handling with the fmt-style constructor: throw Exception(ErrorCodes::SOME_CODE, "Message with {}", arg);" and "Use DB::Exception with appropriate error codes from ErrorCodes.cpp for error handling in storage engine code".

♻️ Proposed fix
-    throw Exception("Unsupported order by type for TopN-enhanced multi-stage late materialization: " + type->getName());
+    throw Exception(
+        ErrorCodes::NOT_IMPLEMENTED,
+        "Unsupported order by type for TopN-enhanced multi-stage late materialization, type={}",
+        type->getName());

Add the corresponding namespace ErrorCodes { extern const int NOT_IMPLEMENTED; } declaration (or another suitable existing code) near the top of the file.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
throw Exception("Unsupported order by type for TopN-enhanced multi-stage late materialization: " + type->getName());
throw Exception(
ErrorCodes::NOT_IMPLEMENTED,
"Unsupported order by type for TopN-enhanced multi-stage late materialization, type={}",
type->getName());
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationTopN.cpp` at line
277, Update the unsupported-order-type throw in the TopN-enhanced
late-materialization logic to use the fmt-style DB::Exception constructor with a
suitable ErrorCodes value, preferably NOT_IMPLEMENTED. Add the corresponding
ErrorCodes declaration near the file’s existing declarations and pass the type
name as a format argument instead of concatenating strings.

Source: Coding guidelines

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
docs/design/2026-07-26-running-local-topn-for-mslm.md (1)

658-660: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Keep Selection::actRows semantically accurate.

Overwriting it with topn_candidate_rows makes EXPLAIN ANALYZE report materialized candidate rows instead of rows passing the residual Selection, misleading diagnostics and tooling that interprets operator cardinalities. Preserve actRows and expose candidate rows through a separate, clearly labeled metric.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/design/2026-07-26-running-local-topn-for-mslm.md` around lines 658 -
660, Remove the overwrite of the residual Selection executor’s actRows with
topn_candidate_rows in the TopN-enhanced MSLM path. Preserve Selection::actRows
as the number of rows passing residual Selection, and expose storage candidate
rows through a separate clearly labeled runtime metric for EXPLAIN ANALYZE.
dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.h (1)

93-116: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚖️ Poor tradeoff

Use camelCase for newly introduced C++ identifiers.

  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.h#L93-L116: rename adaptive state fields to names such as runningTopN and topnAdaptiveDisabled.
  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.cpp#L62-L65: update the renamed member initialization.
  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.cpp#L287-L331: rename parameters and locals such as residualPassedRows and warmupRows.
  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.cpp#L382-L459: update references to the renamed state.
  • dbms/src/Storages/DeltaMerge/tests/gtest_skippable_block_input_stream.cpp#L99-L104: rename candidate_rows.
  • dbms/src/Storages/DeltaMerge/tests/gtest_skippable_block_input_stream.cpp#L287-L308: rename topk and related locals.
  • dbms/src/Storages/DeltaMerge/tests/gtest_skippable_block_input_stream.cpp#L1042-L1178: rename new test locals such as warmupRows, stage0Filters, and filterValues.

As per coding guidelines, “Method and variable names should use camelCase.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.h`
around lines 93 - 116, Rename all newly introduced C++ methods, members,
parameters, and locals to camelCase, preserving behavior: update adaptive state
members such as running_topn and topn_adaptive_disabled in
MultiStageLateMaterializationBlockInputStream, their initialization and
references, and parameters/locals in updateTopNAdaptiveState and related logic.
Also rename candidate_rows, topk-related locals, and test locals such as
warmupRows, stage0Filters, and filterValues in
dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.h:93-116,
dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.cpp:62-65,
287-331, 382-459, and
dbms/src/Storages/DeltaMerge/tests/gtest_skippable_block_input_stream.cpp:99-104,
287-308, 1042-1178.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/design/2026-07-26-running-local-topn-for-mslm.md`:
- Around line 575-577: Update the adaptive disable-ratio calculation to use
residual-passed rows, such as stage1_output_rows, rather than
residual_input_rows, so selective residual predicates do not dilute the ratio.
Define and document the zero-denominator behavior, and apply the same
denominator consistently to the warmup eligibility and post-warmup ratio checks
where relevant.

---

Nitpick comments:
In
`@dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.h`:
- Around line 93-116: Rename all newly introduced C++ methods, members,
parameters, and locals to camelCase, preserving behavior: update adaptive state
members such as running_topn and topn_adaptive_disabled in
MultiStageLateMaterializationBlockInputStream, their initialization and
references, and parameters/locals in updateTopNAdaptiveState and related logic.
Also rename candidate_rows, topk-related locals, and test locals such as
warmupRows, stage0Filters, and filterValues in
dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.h:93-116,
dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.cpp:62-65,
287-331, 382-459, and
dbms/src/Storages/DeltaMerge/tests/gtest_skippable_block_input_stream.cpp:99-104,
287-308, 1042-1178.

In `@docs/design/2026-07-26-running-local-topn-for-mslm.md`:
- Around line 658-660: Remove the overwrite of the residual Selection executor’s
actRows with topn_candidate_rows in the TopN-enhanced MSLM path. Preserve
Selection::actRows as the number of rows passing residual Selection, and expose
storage candidate rows through a separate clearly labeled runtime metric for
EXPLAIN ANALYZE.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 41c5293a-e439-43fc-a10d-803e59880da1

📥 Commits

Reviewing files that changed from the base of the PR and between 4bf257d and e39d208.

📒 Files selected for processing (6)
  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.cpp
  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationBlockInputStream.h
  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationRuntimeStats.h
  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationTopN.h
  • dbms/src/Storages/DeltaMerge/tests/gtest_skippable_block_input_stream.cpp
  • docs/design/2026-07-26-running-local-topn-for-mslm.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationRuntimeStats.h
  • dbms/src/Storages/DeltaMerge/MultiStageLateMaterializationTopN.h

Comment thread docs/design/2026-07-26-running-local-topn-for-mslm.md Outdated
Comment thread dbms/src/Storages/StorageDeltaMerge.cpp Outdated
Comment thread dbms/src/Flash/Coprocessor/DAGStorageInterpreter.cpp Outdated
Comment thread dbms/src/Interpreters/SettingsCommon.h Outdated
Comment thread dbms/src/Storages/DeltaMerge/File/DMFilePackFilter.cpp Outdated
Comment thread docs/design/2026-07-26-running-local-topn-for-mslm.md Outdated
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>

@JaySon-Huang JaySon-Huang left a comment

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.

LGTM

@ti-chi-bot ti-chi-bot Bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Jul 30, 2026
@windtalker

Copy link
Copy Markdown
Contributor Author

/hold

@ti-chi-bot ti-chi-bot Bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jul 30, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: gengliqi, JaySon-Huang
Once this PR has been reviewed and has the lgtm label, please assign yudongusa for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot Bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jul 30, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

[LGTM Timeline notifier]

Timeline:

  • 2026-07-30 08:11:54.909955411 +0000 UTC m=+2083700.946050477: ☑️ agreed by JaySon-Huang.
  • 2026-07-30 08:16:14.489150088 +0000 UTC m=+2083960.525245164: ☑️ agreed by gengliqi.

Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
@ti-chi-bot ti-chi-bot Bot removed the approved label Jul 30, 2026
@windtalker

Copy link
Copy Markdown
Contributor Author

/hold cancel

@ti-chi-bot ti-chi-bot Bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 1, 2026
@ti-chi-bot
ti-chi-bot Bot merged commit d813287 into pingcap:feature/release-8.5-materialized-view Aug 1, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants