Skip to content

Conversation

@terry1purcell
Copy link
Contributor

@terry1purcell terry1purcell commented Sep 8, 2025

What problem does this PR solve?

Issue Number: ref #44850

Problem Summary:

What changed and how does it work?

Create global/session variable tidb_opt_enable_semi_join_rewrite to allow users to control semi join rewrite of EXISTS correlated subqueries without needing the hint.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.
CREATE TABLE `t1` (
  `a` int,
  `b` int,
  `c` int,
  Primary KEY `ia` (`a`),
  KEY `ib` (`b`)
);

CREATE TABLE `t2` (
  `a` int,
  `b` int,
  `c` int,
  Unique KEY `ia` (`a`),
  KEY `ib` (`b`)
);
set @@cte_max_recursion_depth=10000000;
INSERT INTO t1 (a, b, c)
SELECT a, mod(a, 1000) AS b, mod(a, 10) AS c
FROM (
    WITH RECURSIVE x AS (
        SELECT 1 AS a
        UNION ALL
        SELECT a + 1 AS a
        FROM x
        WHERE a < 1000000
    )
    SELECT a
    FROM x
) AS subquery;
Insert into t2 select * from t1;
Analyze table t1, t2;

To test before change, and with hint:

explain analyze select count(*) from t1 where exists (select * from t2 where t1.b = t2.b and t2.a < 100);
explain analyze select t1.* from t1 where exists (select /*+ SEMI_JOIN_REWRITE() */ * from t2 where t1.b = t2.b and t2.a < 100);

Test with new variable:

set tidb_opt_enable_semi_join_rewrite=ON;
explain analyze select count(*) from t1 where exists (select * from t2 where t1.b = t2.b and t2.a < 100);

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

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-linked-issue release-note-none Denotes a PR that doesn't merit a release note. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. sig/planner SIG: Planner labels Sep 8, 2025
@tiprow
Copy link

tiprow bot commented Sep 8, 2025

Hi @terry1purcell. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot ti-chi-bot bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Sep 8, 2025
@codecov
Copy link

codecov bot commented Sep 8, 2025

Codecov Report

❌ Patch coverage is 89.47368% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.2548%. Comparing base (d022959) to head (45b41a8).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #63416        +/-   ##
================================================
+ Coverage   72.7831%   73.2548%   +0.4717%     
================================================
  Files          1835       1835                
  Lines        496868     497013       +145     
================================================
+ Hits         361636     364086      +2450     
+ Misses       113266     111055      -2211     
+ Partials      21966      21872        -94     
Flag Coverage Δ
integration 42.0445% <68.4210%> (?)
unit 72.3744% <89.4736%> (+0.0540%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 52.8700% <ø> (ø)
parser ∅ <ø> (∅)
br 46.5294% <ø> (+0.0355%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ti-chi-bot ti-chi-bot bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 9, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a new global/session variable tidb_opt_enable_semi_join_rewrite to control the automatic rewriting of EXISTS correlated subqueries to inner joins with aggregation, similar to what the SEMI_JOIN_REWRITE() hint already provides.

  • Adds session variable tidb_opt_enable_semi_join_rewrite to enable semi-join rewrite optimization without requiring the hint
  • Updates semi-join cardinality estimation logic to improve accuracy for semi-join operations
  • Modifies plan generation and binding logic to support the new variable

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pkg/sessionctx/vardef/tidb_vars.go Defines the new variable constant and default value
pkg/sessionctx/variable/sysvar.go Registers the new system variable with session setter
pkg/sessionctx/variable/session.go Adds EnableSemiJoinRewrite field to SessionVars struct
pkg/planner/core/rule_semi_join_rewrite.go Updates rewrite logic to check both hint and session variable
pkg/planner/core/logical_plan_builder.go Enables semi-join rewrite when session variable is set
pkg/planner/core/operator/logicalop/logical_join.go Improves semi-join cardinality estimation algorithm
pkg/planner/core/expression_rewriter.go Adds comment about variable usage in semi-join context
pkg/bindinfo/ Updates binding plan generation to handle new variable
tests/integrationtest/r/planner/core/casetest/physicalplantest/physical_plan.result Updated test expectations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@ti-chi-bot ti-chi-bot bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 9, 2025
@terry1purcell terry1purcell changed the title planner: Exists subquery to index join planner: Exists subquery to join variable Sep 9, 2025
@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Sep 10, 2025
@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Sep 10, 2025
@ti-chi-bot
Copy link

ti-chi-bot bot commented Sep 10, 2025

[LGTM Timeline notifier]

Timeline:

  • 2025-09-10 01:11:57.684714119 +0000 UTC m=+410184.244595606: ☑️ agreed by qw4990.
  • 2025-09-10 02:48:26.034447438 +0000 UTC m=+415972.594328935: ☑️ agreed by hawkingrei.

@hawkingrei
Copy link
Member

/ok-to-test

@ti-chi-bot ti-chi-bot bot added the ok-to-test Indicates a PR is ready to be tested. label Sep 10, 2025
@terry1purcell
Copy link
Contributor Author

/retest-required

@hawkingrei
Copy link
Member

/retest

1 similar comment
@hawkingrei
Copy link
Member

/retest

@ti-chi-bot
Copy link

ti-chi-bot bot commented Sep 10, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: hawkingrei, qw4990, yudongusa

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

The pull request process is described here

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 approved label Sep 10, 2025
@hawkingrei
Copy link
Member

/retest

1 similar comment
@hawkingrei
Copy link
Member

/retest

@ti-chi-bot ti-chi-bot bot merged commit 6ba9835 into pingcap:master Sep 10, 2025
28 checks passed
@terry1purcell terry1purcell deleted the exists branch September 10, 2025 13:26
@terry1purcell
Copy link
Contributor Author

/cherry-pick release-8.5

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Sep 16, 2025
@ti-chi-bot
Copy link
Member

@terry1purcell: new pull request created to branch release-8.5: #63540.
But this PR has conflicts, please resolve them!

In response to this:

/cherry-pick release-8.5

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

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

Labels

approved lgtm ok-to-test Indicates a PR is ready to be tested. release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants