Skip to content

planner: pull up IN-list predicates in constant propagation | tidb-test=pr/2829 - #73349

Open
terry1purcell wants to merge 3 commits into
pingcap:masterfrom
terry1purcell:inlist-constprop
Open

terry1purcell wants to merge 3 commits into
pingcap:masterfrom
terry1purcell:inlist-constprop

Conversation

@terry1purcell

@terry1purcell terry1purcell commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: close #73348

Problem Summary:

Constant propagation pulls col <op> const predicates out of a join child so that a join equality can carry them to the other child, but ValidCompareConstantPredicate only accepts =, >, >=, <, <=. An IN list of constants is left behind, so the other child loses a predicate that the equality version gets.

A left join that hydrates the child rows of a page of parent ids hits this. With id = ? the child side is a point read; with id in (?, ?) it scans every child row under the shared key prefix and filters the rest of the key row by row.

What changed and how does it work?

  • ValidCompareConstantPredicate also accepts col IN (const, ...) when every constant has the column's collation, the same requirement it applies to a single comparison.
  • Nothing after the pull-up changes: the outer-join solver (propagateColumnEQ / deriveConds) and the inner-join solver already derive the other side's IN once they see the predicate.

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.
  • TestInListConstantPropagation in tests/integrationtest/t/planner/core/rule_constant_propagation.test: inner join, left join from the outer child, no propagation from the inner child of a left or right join, through a projection, matching and mismatched collation, a non-constant item, NOT IN, and the results of three of the queries.

  • go test -tags=intest ./pkg/planner/core/casetest/... ./pkg/expression/ with failpoints enabled: passes, no recorded plan changes.

  • tests/integrationtest/run-tests.sh: every case passes; no other recorded plan changes.

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.

The optimizer propagates an `IN` list of constants on a join key from one side of a join to the other, so a join filtered by `IN` on its key can read only the matching rows of the other table.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Performance
    • Queries using IN filters may benefit from constant propagation across joins, including supported inner- and outer-join cases and queries with projections.
    • Propagation follows join semantics: it does not flow from nullable sides of outer joins, and does not apply to NOT IN.
    • Propagation is limited when an IN list contains non-constant values or string collations do not match.
  • Bug Fixes
    • Predicates containing expressions with mutable effects are excluded from constant propagation.

Constant propagation pulls `col <op> const` predicates out of a join child
so that a join equality can carry them to the other child. `col IN (const,
...)` was not pulled up, so a derived table filtered by an IN list on a join
key left the other side without the derived IN, and a left join that
hydrates child rows for a page of ids scanned the whole id prefix.

Accept `col IN (const, ...)` in ValidCompareConstantPredicate when every
constant has the column's collation, the same requirement as a single
comparison. The outer-join solver already derives the inner-side IN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ti-chi-bot ti-chi-bot Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 17, 2026
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Eligible column IN predicates with constant list values can pass the constant-predicate check when their collations match. Predicate pull-up also excludes expressions with mutable effects. Integration cases cover propagation across joins and related conditions.

Changes: IN-list Constant Propagation

Layer / File(s) Summary
Validate and pull up constant predicates
pkg/expression/constant_propagation.go, pkg/planner/core/operator/logicalop/logical_selection.go
ValidCompareConstantPredicate now accepts IN predicates when the first argument is a column and every remaining argument is a constant with matching collation. PullUpConstantPredicates excludes predicates that contain mutable-effect expressions.
Exercise propagation across joins
tests/integrationtest/t/planner/core/rule_constant_propagation.test, tests/integrationtest/r/planner/core/rule_constant_propagation.result, pkg/planner/core/casetest/rule/BUILD.bazel
Integration cases cover inner and outer joins, projections, collation matches and mismatches, non-constant list items, NOT IN, query results, and prepared-plan-cache predicates. The rule-test target’s shard count increases from 27 to 28.

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

Suggested reviewers: qw4990, winoros, hawkingrei

Merge Risk: Minimal · up to 65bf4

The change lets constant IN lists propagate across joins and skips mutable predicates during pull-up. No concrete merge-blocking defect was found. A known Index Join estimate inflation is tracked in separate follow-up issues.

Pre-merge checks: Passed 4

Check name Status Explanation
Description check Passed The description follows the repository template. It includes the required Issue Number line (close #73348), a problem summary, a description of what changed and how it works, a checked integration-tes…
Linked Issues check Passed [#73348] Requirement: constant propagation must pull up constant IN-list predicates out of a join child so a join equality can carry them to the other child. The whole-PR diff (merge base 8c05d01 to …
Out of Scope Changes check Passed Changes since the previous review: pkg/planner/core/operator/logicalop/logical_selection.go now skips predicates for which expression.IsMutableEffectsExpr is true during PullUpConstantPredicates. The …
Title check Passed The title 'planner: pull up IN-list predicates in constant propagation' matches the main change. The changeset extends ValidCompareConstantPredicate to accept IN predicates with constant list items th…
Full details: Description check

Explanation

The description follows the repository template. It includes the required Issue Number line (close #73348), a problem summary, a description of what changed and how it works, a checked integration-test box with a list of the tests added, and a release note. The Side effects and Documentation boxes are unchecked, and the description does not explain why the mutable-predicate exclusion in logical_selection.go was added. The commit message mentions that change, but the description does not.

Full details: Linked Issues check

Explanation

[#73348] Requirement: constant propagation must pull up constant IN-list predicates out of a join child so a join equality can carry them to the other child. The whole-PR diff (merge base 8c05d01 to head 65bf4d4) adds an ast.In branch to ValidCompareConstantPredicate in pkg/expression/constant_propagation.go. This dispatches to validInConstantPredicate, which accepts only [Column IN (Constant, ...)] where every constant has the column's collation. Non-constant list items and NOT IN are rejected. The change addresses the gap the issue names. The issue states that the outer-join and inner-join solvers already derive the other side once they see the predicate, and this PR does not change them. The integration test TestInListConstantPropagation (tests/integrationtest/t/planner/core/rule_constant_propagation.test and its result file) covers inner joins, left joins from the outer child, projections, and collation match and mismatch. No unit test was added. I did not re-run the plans; plan evidence comes from the result file. Remaining uncertainty: the new mutable-effect guard in LogicalSelection.PullUpConstantPredicates (see out-of-scope assessment) can exclude some predicates. The issue example uses plain literal constants, which the guard does not exclude.

Full details: Out of Scope Changes check

Explanation

Changes since the previous review: pkg/planner/core/operator/logicalop/logical_selection.go now skips predicates for which expression.IsMutableEffectsExpr is true during PullUpConstantPredicates. The commit message and code comment state that pull-up copies the predicate and a Constant may wrap a mutable deferred expression. This guard protects the same pull-up path that the IN support widens. The integration test file adds prepared-plan-cache cases that use RANDOM_BYTES in IN, equality, and range predicates, which exercise this guard. The guard is therefore connected to the objective of #73348 and is not unrelated. It does change behavior for existing comparison predicates as well, not only IN predicates. Other changes: the constant_propagation.go change limited to ValidCompareConstantPredicate and validInConstantPredicate; the integration test and result file; and the rule_test Bazel shard count from 27 to 28 for the added test cases. All of these support the objective. Limit: test and result file contents were checked through their summaries and the source diffs above, not by a full read of the test files.

Full details: Title check

Explanation

The title 'planner: pull up IN-list predicates in constant propagation' matches the main change. The changeset extends ValidCompareConstantPredicate to accept IN predicates with constant list items that share the column's collation, and LogicalSelection.PullUpConstantPredicates now skips mutable predicates. The title names the primary change. It includes the tracking tag 'tidb-test=pr/2829', which is noise but does not make the title misleading.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

golangci-lint (2.15.0)

Command failed
golangci-lint (2.15.0) exceeded its 180-second time budget. Findings from completed work are retained; remaining checks are incomplete.

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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 8fefa14 and 65bf4d4.

Files selected for processing: 3

  • pkg/planner/core/operator/logicalop/logical_selection.go
  • tests/integrationtest/r/planner/core/rule_constant_propagation.result
  • tests/integrationtest/t/planner/core/rule_constant_propagation.test

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.23529% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.0620%. Comparing base (8c05d01) to head (65bf4d4).
⚠️ Report is 109 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #73349        +/-   ##
================================================
- Coverage   72.1766%   72.0620%   -0.1146%     
================================================
  Files          2173       2184        +11     
  Lines        603609     605822      +2213     
================================================
+ Hits         435665     436568       +903     
- Misses       167942     169250      +1308     
- Partials          2          4         +2     
Flag Coverage Δ
integration 46.0617% <88.2352%> (+0.0623%) ⬆️

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

Components Coverage Δ
dumpling 59.9320% <ø> (ø)
parser ∅ <ø> (∅)
br 43.4660% <ø> (+0.0288%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@terry1purcell
terry1purcell requested a balanced review from Copilot November 18, 2026 03:05
@terry1purcell

Copy link
Copy Markdown
Contributor Author

/retest-required

Copilot AI 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.

🟢 Approval recommended

No active review issues were identified in the supplied diff and review context, though tests were not run locally.

No open issues 🎉

What changed in this PR

Extends constant propagation to pull constant IN predicates out of join children, allowing existing solvers to propagate them across join equalities and enable more selective reads. This addresses #73348.

Changes:

  • Accepts column IN (constant, ...) when every list item is a constant with the column’s collation.
  • Adds integration coverage for join direction, projection, collation compatibility, unsupported predicates, and query results.

Validation was limited to the supplied diff and review context; no tests were run locally.

File Description
tests/​integrationtest/​t/​planner/​core/​rule_constant_propagation.test Adds propagation and SQL-result regression cases.
tests/​integrationtest/​r/​planner/​core/​rule_constant_propagation.result Records expected plans and query results.
pkg/​expression/​constant_propagation.go Recognizes eligible constant IN predicates for pull-up.

Review effort: Balanced

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ti-chi-bot ti-chi-bot Bot added needs-cherry-pick-release-nextgen-202609 Should cherry pick this PR to release-nextgen-202609 branch. sig/planner SIG: Planner labels Nov 18, 2026
@winoros

winoros commented Nov 18, 2026

Copy link
Copy Markdown
Member

The derived IN helps the original composite-primary-key case, but it also exposes #73372 in Index Join. When the probe already binds both (a, b) from an outer row that passed b IN (10, 20), the copied t2.b IN (10, 20) is redundant. In my local comparison, it increased the probe scan estimate from 25 to 12,500 while the scan still returned 3 rows.

Please follow up with #73373 to complete the Index Join side of this change. I ran the stacked version locally and confirmed that the redundant filter disappeared, the probe scan estimate returned to 25, and the original #73348 query still used a six-key Batch_Point_Get on the right.

@ti-chi-bot

ti-chi-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: winoros
Once this PR has been reviewed and has the lgtm label, please assign windtalker 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 needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Nov 18, 2026
@ti-chi-bot

ti-chi-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

[LGTM Timeline notifier]

Timeline:

  • 2026-11-18 08:54:25.61707105 +0000 UTC m=+1866390.242221347: ☑️ agreed by winoros.

@terry1purcell

Copy link
Copy Markdown
Contributor Author

@winoros Thanks for running the stacked version. #73373 is the Index Join follow-up for #73372; I'll rebase it onto master and keep it moving alongside this PR.

@terry1purcell

Copy link
Copy Markdown
Contributor Author

@windtalker Could you review this one? It needs an approver for pkg/expression. It extends constant propagation to pull up IN-list predicates across equality joins (#73348); winoros has approved.

@terry1purcell terry1purcell changed the title planner: pull up IN-list predicates in constant propagation planner: pull up IN-list predicates in constant propagation | tidb-test=pr/2829 Nov 29, 2026
@terry1purcell

Copy link
Copy Markdown
Contributor Author

/test mysql-test

@ti-chi-bot

ti-chi-bot Bot commented Nov 29, 2026

Copy link
Copy Markdown

@terry1purcell: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
idc-jenkins-ci-tidb/mysql-test 65bf4d4 link true /test mysql-test

Full PR test history. Your PR dashboard.

Details

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. I understand the commands that are listed here.

This branch has not been deployed

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

Labels

needs-1-more-lgtm Indicates a PR needs 1 more LGTM. needs-cherry-pick-release-nextgen-202609 Should cherry pick this PR to release-nextgen-202609 branch. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/planner SIG: Planner size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

planner: constant propagation does not pull up IN-list predicates across a join

3 participants