Skip to content

executor: skip probe for empty hash joins - #11001

Open
ChangRui-Ryan wants to merge 1 commit into
pingcap:masterfrom
ChangRui-Ryan:changrui_build_empty
Open

executor: skip probe for empty hash joins#11001
ChangRui-Ryan wants to merge 1 commit into
pingcap:masterfrom
ChangRui-Ryan:changrui_build_empty

Conversation

@ChangRui-Ryan

@ChangRui-Ryan ChangRui-Ryan commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: close #11000

Problem Summary

When the build side of a hash join is empty after filtering, the join result may already be known without reading the probe side. However, the current execution pipeline can still scan the entire probe side and perform unnecessary hash lookups against an empty hash table.

For example:

SELECT *
FROM orders
INNER JOIN lineitem
    ON l_comment = o_comment
WHERE o_clerk = 'Clerk#000000000';

The filter produces an empty build side, but the profile shows:

Build actRows:              0
Probe actRows:              59,986,052
lineitem data_scanned_rows: 59,987,779
Query time:                 about 715 ms

This causes unnecessary probe-side I/O, block processing, pipeline scheduling, and hash-table lookup overhead. The same problem also applies to eligible Semi Join cases.

What is changed and how it works

This change allows supported hash joins to terminate the probe side early when the finalized build hash table is empty.

The implementation has two parts:

  1. Track whether the finalized build side is empty.

    • V1 publishes the build-empty state after build finalization.
    • V1 only enables this state when no actual spill has happened.
    • V2 publishes the state after all build workers finish.
    • The check is based on effective hash-table entries, so a build input containing only NULL join keys is also treated as empty.
  2. Skip probe processing at both the join and pipeline levels.

    • At the join level, incoming probe blocks are marked as processed without probing the hash table.
    • At the pipeline level, the probe transform can indicate that its source is no longer needed.
    • PipelineExec then avoids reading the probe source and finishes the transform normally, without relying on source EOF or repeatedly producing empty blocks.
    • This prevents unnecessary local probe scans and avoids repeated lookups against an empty hash table.

The optimization is enabled for:

V1: Inner Join, Semi Join, RightSemi Join
V2: Inner Join, Semi Join

RightSemi Join is not currently supported by Hash Join V2 and therefore falls back to the V1 implementation.

Join types whose output still depends on every probe row, such as AntiSemi Join and LeftOuterSemi Join, are not included in this optimization. V1 also keeps the existing behavior when an actual external join spill has occurred.

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

  • Performance Improvements

    • Improved hash join execution when the build side is empty, allowing unnecessary probe and source processing to be skipped.
    • Reduced pipeline work for joins that can complete without reading source data.
    • Preserved correct handling for join types that still require probe processing.
  • Bug Fixes

    • Improved join completion behavior and output handling when probe processing is bypassed.
  • Tests

    • Added coverage for empty and null-only build inputs across multiple join types.
    • Added lifecycle tests verifying skipped sources are not executed.

@ti-chi-bot ti-chi-bot Bot added the release-note-none Denotes a PR that doesn't merit a release note. label Jul 27, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign jinhelin 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 the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Jul 27, 2026
@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Empty-build hash joins now publish probe-skip state, bypass probe processing for eligible join kinds, and prevent pipeline sources from running when transforms can finish without input. Tests cover empty and null-key builds plus anti-semi behavior.

Changes

Empty-build probe skipping

Layer / File(s) Summary
Build emptiness and skip predicates
dbms/src/Interpreters/Join.*, dbms/src/Interpreters/JoinV2/HashJoin.*
Join implementations record build-side emptiness and expose skip decisions for eligible join kinds.
Probe execution fast paths
dbms/src/DataStreams/HashJoinProbeExec.cpp, dbms/src/Interpreters/Join.cpp, dbms/src/Interpreters/JoinV2/HashJoin.cpp, dbms/src/Operators/*ProbeTransform*
Probe executors finalize or bypass probe processing when the join indicates that probing can be skipped.
Pipeline source lifecycle skipping
dbms/src/Flash/Pipeline/Exec/*, dbms/src/Operators/Operator.h, dbms/src/Operators/*ProbeTransformOp.h, dbms/src/Flash/Pipeline/Exec/tests/gtest_simple_operator.cpp
Transforms report source-skipping capability, and PipelineExec conditionally omits source lifecycle execution.
Join and pipeline validation
dbms/src/Flash/tests/gtest_join_executor.cpp
Tests validate empty-build and null-key join cases, including anti-semi joins that still read probe input.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant BuildSide
  participant Join
  participant ProbeTransform
  participant PipelineExec
  participant ProbeSource
  BuildSide->>Join: publish empty build state
  ProbeTransform->>Join: request shouldSkipProbe()
  Join-->>ProbeTransform: return skip decision
  PipelineExec->>ProbeTransform: request shouldSkipSource()
  ProbeTransform-->>PipelineExec: return true
  PipelineExec-->>ProbeSource: skip source lifecycle
  ProbeTransform->>Join: finalize skipped probe
Loading

Suggested reviewers: yongman

Poem

A rabbit hopped past an empty build,
“No probe-side rows need be spilled!”
The source slept tight,
The join finished right,
And tests watched every path fulfilled.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 8.33% 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
Linked Issues check ✅ Passed The code implements empty-build probe skipping for hash joins and eligible semi-join cases as requested in #11000.
Out of Scope Changes check ✅ Passed The patch stays focused on the join-probe optimization and its necessary pipeline and test updates.
Title check ✅ Passed The title is concise and accurately summarizes the main change: skipping probe work for empty hash joins.
Description check ✅ Passed The description covers the required problem, change, checklist, side effects, and release-note sections, with only minor formatting differences.
✨ 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.

🧹 Nitpick comments (2)
dbms/src/Interpreters/Join.h (1)

250-258: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use camelCase for newly introduced member names.

The new state variables use snake_case, contrary to the C++ naming guideline. Rename them consistently and update all references:

  • dbms/src/Interpreters/Join.h#L250-L258,449-449: rename build_side_empty to buildSideEmpty.
  • dbms/src/Interpreters/JoinV2/HashJoin.h#L77-L83,156-156: rename build_side_empty to buildSideEmpty.
  • dbms/src/Interpreters/JoinV2/HashJoin.cpp#L468-L468: update the state publication reference.
  • dbms/src/Flash/Pipeline/Exec/PipelineExec.h#L83-L83: rename source_prefix_executed to sourcePrefixExecuted.
🤖 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/Interpreters/Join.h` around lines 250 - 258, Rename the newly
introduced state members to camelCase and update every reference: in
dbms/src/Interpreters/Join.h lines 250-258 and 449-449, rename build_side_empty
to buildSideEmpty; in dbms/src/Interpreters/JoinV2/HashJoin.h lines 77-83 and
156-156, rename build_side_empty to buildSideEmpty; in
dbms/src/Interpreters/JoinV2/HashJoin.cpp line 468, update the state publication
reference; and in dbms/src/Flash/Pipeline/Exec/PipelineExec.h line 83, rename
source_prefix_executed to sourcePrefixExecuted.

Source: Coding guidelines

dbms/src/Flash/tests/gtest_join_executor.cpp (1)

443-469: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Consider adding empty-build coverage for Cross-join and NullAware semi/anti kinds.

Current new tests cover Inner/Semi/RightSemi (skip) and Anti (no-skip) for the standard equi hash join, but there's no test for Cross_LeftOuterAnti/Cross_LeftOuter or NullAware_* kinds with an empty build side. Given the skip logic in Join::joinBlock (Join.cpp Lines 1990-1994) runs before the kind-specific dispatch, a test confirming these families are not incorrectly skipped would close the verification gap raised on the Join.cpp change.

🤖 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_join_executor.cpp` around lines 443 - 469, Add
empty-build coverage alongside EmptyBuildAntiSemiJoinStillReadsProbe for
Cross_LeftOuterAnti, Cross_LeftOuter, and NullAware semi/anti join kinds. Build
requests with an empty build side and non-empty probe side, then verify
execution is not skipped and produces the expected probe-side results, covering
the pre-dispatch skip logic in Join::joinBlock.
🤖 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.

Nitpick comments:
In `@dbms/src/Flash/tests/gtest_join_executor.cpp`:
- Around line 443-469: Add empty-build coverage alongside
EmptyBuildAntiSemiJoinStillReadsProbe for Cross_LeftOuterAnti, Cross_LeftOuter,
and NullAware semi/anti join kinds. Build requests with an empty build side and
non-empty probe side, then verify execution is not skipped and produces the
expected probe-side results, covering the pre-dispatch skip logic in
Join::joinBlock.

In `@dbms/src/Interpreters/Join.h`:
- Around line 250-258: Rename the newly introduced state members to camelCase
and update every reference: in dbms/src/Interpreters/Join.h lines 250-258 and
449-449, rename build_side_empty to buildSideEmpty; in
dbms/src/Interpreters/JoinV2/HashJoin.h lines 77-83 and 156-156, rename
build_side_empty to buildSideEmpty; in dbms/src/Interpreters/JoinV2/HashJoin.cpp
line 468, update the state publication reference; and in
dbms/src/Flash/Pipeline/Exec/PipelineExec.h line 83, rename
source_prefix_executed to sourcePrefixExecuted.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 33aad696-443c-48c3-b6c6-f48d79afe38e

📥 Commits

Reviewing files that changed from the base of the PR and between 555bb7c and eb60661.

📒 Files selected for processing (15)
  • dbms/src/DataStreams/HashJoinProbeExec.cpp
  • dbms/src/Flash/Pipeline/Exec/PipelineExec.cpp
  • dbms/src/Flash/Pipeline/Exec/PipelineExec.h
  • dbms/src/Flash/Pipeline/Exec/tests/gtest_simple_operator.cpp
  • dbms/src/Flash/tests/gtest_join_executor.cpp
  • dbms/src/Interpreters/Join.cpp
  • dbms/src/Interpreters/Join.h
  • dbms/src/Interpreters/JoinV2/HashJoin.cpp
  • dbms/src/Interpreters/JoinV2/HashJoin.h
  • dbms/src/Operators/HashJoinProbeTransformOp.cpp
  • dbms/src/Operators/HashJoinProbeTransformOp.h
  • dbms/src/Operators/HashJoinV2ProbeTransformOp.cpp
  • dbms/src/Operators/HashJoinV2ProbeTransformOp.h
  • dbms/src/Operators/HashProbeTransformExec.h
  • dbms/src/Operators/Operator.h

@ChangRui-Ryan

Copy link
Copy Markdown
Contributor Author

/retest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid unnecessary probe-side MPP work for empty-build hash joins

1 participant