Skip to content

Conversation

@King-Dylan
Copy link
Member

@King-Dylan King-Dylan commented Aug 18, 2025

What problem does this PR solve?

Issue Number: close #63045

Problem Summary:
For actually executed SQL statements, the execution plans of subqueries that were pre-executed are not recorded or displayed. As a result, in slow query logs, statement summary, or EXPLAIN ANALYZE, these parts of the plan are missing.
This makes troubleshooting and performance tuning more difficult.

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-tests-checked release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed do-not-merge/needs-tests-checked labels Aug 18, 2025
@King-Dylan
Copy link
Member Author

pingcap/tipb#377

case *Constant:
v := expr.StringWithCtx(ctx, errors.RedactLogDisable)
redact.WriteRedact(builder, v, redactMode)
// For Projection operators, show only the subquery reference without the constant value
Copy link
Member

Choose a reason for hiding this comment

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

Why?

Copy link
Member Author

Choose a reason for hiding this comment

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

Since I think showing the column mapping is sufficient. For example: (ScalarQueryCol#14) -> Column#15 vs (ScalarQueryCol#14) -> 55 -> Column#15.

Copy link
Contributor

Choose a reason for hiding this comment

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

Before your pr, it will show 55?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, what I’m thinking is that, rather than the exact numbers, what we really need to know is which subquery influenced that operator, or in other words, their causal relationship.
Before:

55 -> Column#15

After this pr:

ScalarQueryCol#14 -> Column#15

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe append the constant value after ScalarQueryCol#14? like
ScalarQueryCol#14(55) -> Column#15

Copy link
Member

@time-and-fate time-and-fate left a comment

Choose a reason for hiding this comment

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

  1. I think we also need to update handleExistSubquery()
  2. Please fix the CI.

@ti-chi-bot ti-chi-bot bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Aug 22, 2025
@codecov
Copy link

codecov bot commented Aug 22, 2025

Codecov Report

❌ Patch coverage is 89.71963% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.7962%. Comparing base (68d18d6) to head (275fe34).
⚠️ Report is 20 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #63047        +/-   ##
================================================
+ Coverage   72.7588%   73.7962%   +1.0374%     
================================================
  Files          1835       1839         +4     
  Lines        496690     503619      +6929     
================================================
+ Hits         361386     371652     +10266     
+ Misses       113324     110176      -3148     
+ Partials      21980      21791       -189     
Flag Coverage Δ
integration 42.8622% <81.3084%> (?)
unit 72.9719% <89.7196%> (+0.6845%) ⬆️

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

Components Coverage Δ
dumpling 52.8700% <ø> (ø)
parser ∅ <ø> (∅)
br 46.5447% <ø> (+0.0711%) ⬆️
🚀 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.

@King-Dylan King-Dylan changed the title planner: Show Execution Plans for Scalar Subqueries planner: Show Execution Plans for Scalar Subqueries | tidb-test=pr/2585 Aug 22, 2025
@King-Dylan
Copy link
Member Author

/retest

@King-Dylan
Copy link
Member Author

/retest

1 similar comment
@King-Dylan
Copy link
Member Author

/retest

for _, arg := range x.GetArgs() {
if c, ok := arg.(*Constant); ok && c.SubqueryRefID > 0 {
subqRef = c.SubqueryRefID
break
Copy link
Contributor

Choose a reason for hiding this comment

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

what happens when there are more than 1 constant arguments with valid SubqueryRefID ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Will output like:

ScalarSubQuery	N/A	root		Output: ScalarQueryCol#14, ScalarQueryCol#15, ScalarQueryCol#16

Copy link
Contributor

Choose a reason for hiding this comment

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

What I mean is during constant folding, if a function has more than 1 arguments which are coming from scalar subquery, then that is that looks like? In your code logic, it seems only display the first argument's source?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, the in list case actually won’t reach this logic because the earlier call will exit in advance. For eq, le, and similar cases, only one SubqueryRefID > 0, so that’s fine. For + - * /, (select a from t where b = sub1+sub2-sub3) If we display all the subqueries that contribute to it, it would look really messy, so we just take the first one.

case *Constant:
v := expr.StringWithCtx(ctx, errors.RedactLogDisable)
redact.WriteRedact(builder, v, redactMode)
// For Projection operators, show only the subquery reference without the constant value
Copy link
Contributor

Choose a reason for hiding this comment

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

Before your pr, it will show 55?

@King-Dylan
Copy link
Member Author

/retest

1 similar comment
@King-Dylan
Copy link
Member Author

/retest

Comment on lines 158 to 160
if expr.SubqueryRefID > 0 {
return fmt.Sprintf("ScalarQueryCol#%d(%s)", expr.SubqueryRefID, valueStr)
}
Copy link
Member

Choose a reason for hiding this comment

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

This behavior here is different from (*Constant).StringWithCtx() when it's RedactLogMarker. ‹› is not added here.

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Sep 8, 2025
@King-Dylan
Copy link
Member Author

/retest

Copy link
Contributor

@windtalker windtalker left a comment

Choose a reason for hiding this comment

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

lgtm

@ti-chi-bot ti-chi-bot bot added lgtm approved and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Sep 9, 2025
@ti-chi-bot
Copy link

ti-chi-bot bot commented Sep 9, 2025

[LGTM Timeline notifier]

Timeline:

  • 2025-09-08 10:23:36.86936591 +0000 UTC m=+270483.429247397: ☑️ agreed by time-and-fate.
  • 2025-09-09 01:24:17.585733524 +0000 UTC m=+324524.145615021: ☑️ agreed by windtalker.

@King-Dylan
Copy link
Member Author

/retest

@ti-chi-bot ti-chi-bot bot removed the approved label Sep 9, 2025
@ti-chi-bot
Copy link

ti-chi-bot bot commented Sep 9, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: time-and-fate, windtalker

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

/retest

2 similar comments
@time-and-fate
Copy link
Member

/retest

@time-and-fate
Copy link
Member

/retest

@ti-chi-bot ti-chi-bot bot merged commit b704aa4 into pingcap:master Sep 11, 2025
28 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/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show Execution Plans for Scalar Subqueries

4 participants