Skip to content

planner: generate PK filters prototype (wip)#66292

Open
terry1purcell wants to merge 4 commits intopingcap:masterfrom
terry1purcell:gensubq
Open

planner: generate PK filters prototype (wip)#66292
terry1purcell wants to merge 4 commits intopingcap:masterfrom
terry1purcell:gensubq

Conversation

@terry1purcell
Copy link
Contributor

@terry1purcell terry1purcell commented Feb 16, 2026

What problem does this PR solve?

Issue Number: ref #66297

This PR has the effect of taking the SQL from the issue - such as the following.
NOTE: There is an index on c, and another index on b - but no index on both columns (this is by design).

Explain select *
From t1
Where c = 5
Order by b
Limit 10;

And generating "lookup" subqueries to produce the min/max PK values related to another indexes filters. An example that demonstrates how these are internally rewritten appears here. This allows the ranges to apply as either a TableRangeScan (against the PK), or against the ORDER BY index as filters against the index (since a clustered PK is part of a secondary index):

Explain analyze select *
From t1
Where c = 5
And a >= (select min(a) from t1 where c = 5)
And a <= (select max(a) from t1 where c = 5)
Order by b
Limit 10;

In this PR - this is triggered by using a hint to specify the generation of the PK ranges, and which index to generate the subqueries on:

Explain select /*+ pk_filter(t1, ic) */ *
From t1
Where c = 5
Order by b
Limit 10;

Problem Summary:

What changed and how does it work?

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.

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. labels Feb 16, 2026
@pantheon-ai
Copy link

pantheon-ai bot commented Feb 16, 2026

Posted PR review with 3 inline P2 comments: #66292 (review)

@ti-chi-bot
Copy link

ti-chi-bot bot commented Feb 16, 2026

[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 bb7133, qw4990, 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 size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. sig/planner SIG: Planner labels Feb 16, 2026
@tiprow
Copy link

tiprow bot commented Feb 16, 2026

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.

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.

@codecov
Copy link

codecov bot commented Feb 16, 2026

Codecov Report

❌ Patch coverage is 69.08023% with 158 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.2551%. Comparing base (2f9776e) to head (ebcb52f).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #66292        +/-   ##
================================================
- Coverage   77.7002%   77.2551%   -0.4451%     
================================================
  Files          2006       1928        -78     
  Lines        548386     536430     -11956     
================================================
- Hits         426097     414420     -11677     
- Misses       120629     122002      +1373     
+ Partials       1660          8      -1652     
Flag Coverage Δ
integration 41.4342% <63.2411%> (-6.7524%) ⬇️

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

Components Coverage Δ
dumpling 56.7974% <ø> (ø)
parser ∅ <ø> (∅)
br 48.7604% <ø> (-12.1132%) ⬇️
🚀 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.

// Optimize the sub-plan to get a physical plan
nthPlanBackup := sctx.GetSessionVars().StmtCtx.StmtHints.ForceNthPlan
sctx.GetSessionVars().StmtCtx.StmtHints.ForceNthPlan = -1
optFlag := rule.FlagPredicatePushDown | rule.FlagBuildKeyInfo | rule.FlagPruneColumns
Copy link

Choose a reason for hiding this comment

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

P2: MIN/MAX subqueries lose equality predicates

The cloned DataSource for MIN/MAX subqueries loses its equality predicates during sub-optimization. The code re-runs optimization with predicate pushdown enabled, but DataSource.PredicatePushDown receives an empty predicates slice and overwrites ds.PushedDownConds, resulting in IndexFullScan instead of efficient range scans.

This causes 2 full secondary index scans per qualifying index (MIN + MAX), which can cause severe performance regressions on large tables.

Evidence: Integration test shows IndexFullScan without range predicates at tests/integrationtest/r/planner/core/casetest/rule/rule_generate_pk_filter.result:15. Root cause: empty predicates passed to DataSource.PredicatePushDown at pkg/planner/core/operator/logicalop/logical_datasource.go:175.

}

// Inject conditions into DataSource's PushedDownConds
ds.PushedDownConds = append(ds.PushedDownConds, newConds...)
Copy link

Choose a reason for hiding this comment

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

P2: DataSource filter state inconsistency

New PK filter predicates are appended only to PushedDownConds without updating AllConds. The ColumnPruner pass (which runs after this rule) only inspects AllConds to determine column usage, so it may incorrectly prune PK columns that are only referenced by the new predicates.

This can cause planner/executor errors when the predicates (containing ScalarSubQueryExpr) are evaluated and reference pruned columns.

Evidence: ds.PushedDownConds = append(ds.PushedDownConds, newConds...) here, but ColumnPruner only checks ds.AllConds at pkg/planner/core/operator/logicalop/logical_datasource.go:188.

@ti-chi-bot
Copy link

ti-chi-bot bot commented Feb 18, 2026

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

Test name Commit Details Required Rerun command
pull-unit-test-next-gen ebcb52f link true /test pull-unit-test-next-gen
idc-jenkins-ci-tidb/unit-test ebcb52f link true /test unit-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.

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. sig/planner SIG: Planner 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.

1 participant