Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: support ScalarSubQuery to display them in EXPLAIN #45252

Merged
merged 8 commits into from
Jul 24, 2023

Conversation

winoros
Copy link
Member

@winoros winoros commented Jul 9, 2023

What problem does this PR solve?

Issue Number: close #22076

Problem Summary:

What is changed and how it works?

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

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

@winoros winoros requested a review from a team as a code owner July 9, 2023 18:09
@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jul 9, 2023
@ti-chi-bot ti-chi-bot bot deleted a comment from ti-chi-bot Jul 9, 2023
@winoros winoros added the sig/planner SIG: Planner label Jul 9, 2023
@hawkingrei
Copy link
Member

/test all

@hawkingrei
Copy link
Member

/retest

@hawkingrei
Copy link
Member

/test all

@hawkingrei
Copy link
Member

/retest

1 similar comment
@chrysan
Copy link
Contributor

chrysan commented Jul 18, 2023

/retest


// EvalInt returns the int64 representation of expression.
func (*ScalarSubQueryExpr) EvalInt(_ sessionctx.Context, _ chunk.Row) (val int64, isNull bool, err error) {
return 0, false, errors.Errorf("Evaluation methods is not implemented for ScalarSubQueryExpr")
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to implement these funcs in the future?

Copy link
Member Author

Choose a reason for hiding this comment

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

Don't need it for now. Since it will not be executed during optimization.

Copy link
Contributor

Choose a reason for hiding this comment

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

Need to revisit and implement those EvalXXX later, when we need to support execution under ScalarSubqueryExpr. Please add some comments here.

)

// ScalarSubQueryExpr is a expression placeholder for the non-correlated scalar subqueries which can be evaluated during optimizing phase.
type ScalarSubQueryExpr struct {
Copy link
Member

Choose a reason for hiding this comment

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

core has too many codes in it. so can you put this code into the sub package of the core.

Copy link
Member Author

Choose a reason for hiding this comment

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

Decoupling the codes in core is not easy work...
And you'll see that the struct embeds basePlan in it, which means we cannot move it out from the package...

I have tried to decouple some codes in core before. But it's really a time costing work.

@chrysan chrysan requested review from fixdb and elsa0520 July 19, 2023 03:35
"SQL": "explain analyze format = 'brief' select * from t1 where (a, b) = (select a, b from t2 limit 1)",
"IsExplainAnalyze": true,
"HasErr": false
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add some tests about constant folding

@codecov
Copy link

codecov bot commented Jul 19, 2023

Codecov Report

Merging #45252 (f35bd77) into master (c6d83d7) will increase coverage by 0.0757%.
The diff coverage is 45.7875%.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #45252        +/-   ##
================================================
+ Coverage   73.2767%   73.3524%   +0.0757%     
================================================
  Files          1264       1268         +4     
  Lines        389025     391061      +2036     
================================================
+ Hits         285065     286853      +1788     
- Misses        85717      85873       +156     
- Partials      18243      18335        +92     
Flag Coverage Δ
integration 78.1388% <ø> (?)
unit 73.3441% <45.7875%> (+0.0674%) ⬆️

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

Components Coverage Δ
dumpling 54.0444% <ø> (ø)
parser 85.0554% <ø> (-0.0346%) ⬇️
br 52.1557% <ø> (+0.0424%) ⬆️

@hawkingrei
Copy link
Member

/retest

Copy link
Contributor

@elsa0520 elsa0520 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 the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Jul 20, 2023
Copy link
Contributor

@AilinKid AilinKid left a comment

Choose a reason for hiding this comment

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

Rest LGTM

for _, scalarSubQ := range p.SCtx().GetSessionVars().MapScalarSubQ {
castedScalarSubQ, ok := scalarSubQ.(*ScalarSubQueryExpr)
if !ok {
continue
Copy link
Contributor

Choose a reason for hiding this comment

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

how can we assert fail here? the log is better to notify at debug level

"Selection 8000.00 root ScalarQueryCol#15",
"└─TableReader 10000.00 root data:TableFullScan",
" └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
"ScalarSubQuery N/A root Output: ScalarQueryCol#13",
Copy link
Contributor

Choose a reason for hiding this comment

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

may we follow the dependency order here?

main (use col2 inside)
dependency2 (use col1 inside) output col2
dependency1 output col1

Copy link
Member Author

Choose a reason for hiding this comment

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

We can left for future optimization

{
"SQL": "explain format = 'brief' select * from t1 where (a, b) = (select a, b from t2 limit 1)",
"Plan": [
"Selection 8000.00 root eq(test.t1.a, ScalarQueryCol#9), eq(test.t1.b, ScalarQueryCol#10)",
Copy link
Contributor

Choose a reason for hiding this comment

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

may we add one more case like:
select sub-q as yy, xx, xx from t where yy > 0 order by yy;
this kind subq's output col will be built and used through projection then selection, then sort

return s.Value, s.evalErr
}
err := s.selfEvaluate()
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

may merge the below two line together?

@ti-chi-bot ti-chi-bot bot added lgtm approved and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jul 22, 2023
@ti-chi-bot
Copy link

ti-chi-bot bot commented Jul 22, 2023

[LGTM Timeline notifier]

Timeline:

  • 2023-07-20 08:02:59.11207413 +0000 UTC m=+1183070.882412843: ☑️ agreed by elsa0520.
  • 2023-07-22 03:25:06.188455686 +0000 UTC m=+1339197.958794393: ☑️ agreed by chrysan.

@hawkingrei
Copy link
Member

/hold

@ti-chi-bot ti-chi-bot bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jul 24, 2023
@hawkingrei
Copy link
Member

/hold

Waiting for the configure review's approval.

@hawkingrei
Copy link
Member

/unhold

@ti-chi-bot ti-chi-bot bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jul 24, 2023
@ti-chi-bot
Copy link

ti-chi-bot bot commented Jul 24, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: AilinKid, chrysan, elsa0520, hawkingrei, qw4990

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:
  • OWNERS [AilinKid,chrysan,hawkingrei,qw4990]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@hawkingrei
Copy link
Member

/retest

@ti-chi-bot ti-chi-bot bot merged commit 2eb698c into pingcap:master Jul 24, 2023
@chrysan chrysan added type/5.4-cherry-pick needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. and removed type/5.4-cherry-pick labels Jul 24, 2023
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-5.4: #45521.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Jul 24, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@winoros winoros deleted the add-scalar-subq-expr branch July 24, 2023 12:13
@seiya-annie
Copy link

/found community

@ti-chi-bot ti-chi-bot bot added the report/community The community has encountered this bug. label Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. release-note-none Denotes a PR that doesn't merit a release note. report/community The community has encountered this bug. 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.

subquery execution should be displayed in explain and explain analyze
8 participants