Skip to content

planner: deep copy AST-owned FieldTypes when building expressions#66652

Merged
ti-chi-bot[bot] merged 2 commits intopingcap:masterfrom
AilinKid:ast-multi-logical-plan
Mar 3, 2026
Merged

planner: deep copy AST-owned FieldTypes when building expressions#66652
ti-chi-bot[bot] merged 2 commits intopingcap:masterfrom
AilinKid:ast-multi-logical-plan

Conversation

@AilinKid
Copy link
Copy Markdown
Contributor

@AilinKid AilinKid commented Mar 3, 2026

What problem does this PR solve?

Issue Number: close #66651

Problem Summary:

This PR implements step 1 of issue #66651. It also records the step 2 examination result for the remaining planner-held AST references.

When the planner rebuilds logical plans directly from a shared AST, some planner paths still reuse AST-owned mutable FieldType objects. That can make different builds share return-type state through expressions or planner-created constants.

What changed and how does it work?

  • Replace direct use of AST-owned FieldType pointers in expression_rewriter.go with DeepCopy() before building scalar functions or cast/json helper expressions.
  • Replace direct use of driver.ValueExpr.Type in planner-created constants with DeepCopy() in planbuilder.go.
  • Add regression tests to verify rebuilding from the same AST does not share RetType state, and update BUILD.bazel for the new test dependency.
  • Audit the remaining AST-backed planner fields and document the ones currently treated as read-only metadata after plan build, including AstIndexHints, TableAsName, PartitionNames, SelectLockInfo, and SHOW-specific column/limit metadata.

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

Tests

  • GOCACHE=/tmp/tidb-go-build-cache go test -run 'TestCastRetTypeDoesNotShareASTFieldType|TestGetInsertColExprDeepCopiesValueExprFieldType' -tags=intest,deadlock ./pkg/planner/core
  • make bazel_lint_changed

Summary by CodeRabbit

  • Bug Fixes

    • Prevented unintended type mutations during expression rewriting and plan building by ensuring types are copied per-use.
  • Tests

    • Added unit tests validating type isolation between AST field types and expression/constant return types.
  • Documentation

    • Clarified planner struct fields and immutability expectations with inline comments.
  • Enhancements

    • Planner now preserves additional read-only AST metadata (index hints, table alias, lock info) through planning stages.

@ti-chi-bot ti-chi-bot bot added the release-note-none Denotes a PR that doesn't merit a release note. label Mar 3, 2026
@pantheon-ai
Copy link
Copy Markdown

pantheon-ai bot commented Mar 3, 2026

Review Complete

Findings: 0 issues
Posted: 0
Duplicates/Skipped: 0

ℹ️ Learn more details on Pantheon AI.

@ti-chi-bot ti-chi-bot bot added sig/planner SIG: Planner size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Mar 3, 2026
@tiprow
Copy link
Copy Markdown

tiprow bot commented Mar 3, 2026

Hi @AilinKid. 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.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 3, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bd298fb and c184509.

📒 Files selected for processing (4)
  • pkg/planner/core/operator/logicalop/logical_datasource.go
  • pkg/planner/core/operator/logicalop/logical_lock.go
  • pkg/planner/core/operator/logicalop/logical_show.go
  • pkg/planner/core/operator/physicalop/physical_lock.go
✅ Files skipped from review due to trivial changes (1)
  • pkg/planner/core/operator/logicalop/logical_show.go

📝 Walkthrough

Walkthrough

Deep-copies AST-owned FieldType objects when building expression return types and planner-created constants; adds tests validating type isolation; documents and exposes some AST-backed metadata fields on logical and physical operator structs; adds a test dependency in BUILD.bazel.

Changes

Cohort / File(s) Summary
Build files
pkg/planner/core/BUILD.bazel
Added //pkg/types/parser_driver to go_test dependencies for planner core tests.
Expression rewriting
pkg/planner/core/expression_rewriter.go
Replaced usages of AST-owned FieldType pointers with FieldType.DeepCopy() when constructing rewritten expressions and function nodes to avoid shared/mutable RetType references.
Plan building (constants)
pkg/planner/core/planbuilder.go
Changed constant RetType construction to use x.Type.DeepCopy() instead of &x.Type, ensuring planner-created constants own their type objects.
Tests — expression and planbuilder
pkg/planner/core/expression_test.go, pkg/planner/core/planbuilder_test.go
Added TestCastRetTypeDoesNotShareASTFieldType and TestGetInsertColExprDeepCopiesValueExprFieldType to assert deep-copy semantics for expression return types and insert-related constant types. Added imports (including parser_driver) as needed.
Logical operator metadata
pkg/planner/core/operator/logicalop/logical_datasource.go
Added exported fields AstIndexHints []*ast.IndexHint and TableAsName *ast.CIStr to DataSource with comments documenting read-only semantics after plan build.
Logical operator docs
pkg/planner/core/operator/logicalop/logical_lock.go, .../logical_show.go
Added or improved inline documentation/comments for existing public fields (Lock, Column, Limit) without changing behavior.
Physical operator metadata
pkg/planner/core/operator/physicalop/physical_lock.go
Added exported field Lock *ast.SelectLockInfo (with plan-cache-clone:"shallow" tag) to PhysicalLock to carry AST lock info into the physical plan.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 I copy each whisker, I copy each type,
Not one more pointer to cause a gripe.
From AST gardens we build anew,
Separate roots for each plan to chew.
Hooray—no shared hops, only safe bytes! 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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 accurately reflects the main change: deep copying AST-owned FieldTypes when building expressions, which is the core objective of this PR.
Description check ✅ Passed The description follows the template structure with issue number, problem summary, changes explained, tests confirmed, and release note. All required sections are present and adequately filled out.
Linked Issues check ✅ Passed The PR successfully implements step 1 of issue #66651: deep-copying AST-owned FieldType objects in expression_rewriter.go and planbuilder.go, adding regression tests, and documenting read-only AST references.
Out of Scope Changes check ✅ Passed All changes align with the stated PR objective: deep-copying FieldTypes, adding test coverage, updating BUILD.bazel, and documenting read-only AST references. No out-of-scope modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.5.0)

Command failed


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 and usage tips.

@AilinKid
Copy link
Copy Markdown
Contributor Author

AilinKid commented Mar 3, 2026

/retest-required

@tiprow
Copy link
Copy Markdown

tiprow bot commented Mar 3, 2026

@AilinKid: 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.

Details

In response to this:

/retest-required

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
Copy Markdown

codecov bot commented Mar 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.6271%. Comparing base (21b5bb6) to head (c184509).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #66652        +/-   ##
================================================
- Coverage   77.6714%   77.6271%   -0.0444%     
================================================
  Files          2008       1929        -79     
  Lines        549230     537835     -11395     
================================================
- Hits         426595     417506      -9089     
+ Misses       120964     120316       -648     
+ Partials       1671         13      -1658     
Flag Coverage Δ
integration 41.5238% <95.8333%> (-6.6692%) ⬇️
unit 76.7697% <91.6666%> (+0.4517%) ⬆️

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

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

Copy link
Copy Markdown

@pantheon-ai pantheon-ai bot left a comment

Choose a reason for hiding this comment

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

✅ Code looks good. No issues found.

@ti-chi-bot ti-chi-bot bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Mar 3, 2026
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot bot commented Mar 3, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: guo-shaoge, qw4990

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

The pull request process is described 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 Mar 3, 2026
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot bot commented Mar 3, 2026

[LGTM Timeline notifier]

Timeline:

  • 2026-03-03 09:50:14.093235534 +0000 UTC m=+265258.671314718: ☑️ agreed by qw4990.
  • 2026-03-03 10:35:29.121048221 +0000 UTC m=+267973.699127405: ☑️ agreed by guo-shaoge.

@ti-chi-bot ti-chi-bot bot merged commit 37b583d into pingcap:master Mar 3, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved lgtm release-note-none Denotes a PR that doesn't merit a release note. 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: track AST decoupling for rebuilding logical plans from a shared AST

3 participants