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, expr: eval readonly user var during plan phase | tidb-test=pr/2419 #54462

Merged
merged 14 commits into from
Dec 25, 2024

Conversation

winoros
Copy link
Member

@winoros winoros commented Jul 4, 2024

What problem does this PR solve?

Issue Number: close #52742, #8733, #14231, #24276

Problem Summary:

What changed and how does it work?

If a user var is not affected by setvar at SQL level, we can directly use its value. No need to wrap it with getvar.

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.

Support fold read-only user variables to constants
支持将只读的用户变量折叠为常量

@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-triage-completed 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. sig/planner SIG: Planner labels Jul 4, 2024
@winoros winoros force-pushed the eval-readonly-var branch from 1dfbb1c to 3830760 Compare July 4, 2024 21:49
@ti-chi-bot ti-chi-bot bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed do-not-merge/needs-triage-completed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 4, 2024
Copy link

codecov bot commented Jul 4, 2024

Codecov Report

Attention: Patch coverage is 89.06250% with 7 lines in your changes missing coverage. Please review.

Project coverage is 73.6128%. Comparing base (6806af4) to head (8aeca9d).
Report is 13 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #54462        +/-   ##
================================================
+ Coverage   73.5201%   73.6128%   +0.0926%     
================================================
  Files          1681       1681                
  Lines        464346     466462      +2116     
================================================
+ Hits         341388     343376      +1988     
- Misses       102131     102280       +149     
+ Partials      20827      20806        -21     
Flag Coverage Δ
integration 42.8800% <76.5625%> (?)
unit 72.3415% <89.0625%> (+0.0660%) ⬆️

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

Components Coverage Δ
dumpling 52.6910% <ø> (ø)
parser ∅ <ø> (∅)
br 45.6167% <ø> (-0.1548%) ⬇️

@winoros winoros force-pushed the eval-readonly-var branch from 3830760 to cddf002 Compare July 8, 2024 16:46
@ti-chi-bot ti-chi-bot bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 8, 2024
@winoros winoros force-pushed the eval-readonly-var branch from cddf002 to 0561371 Compare July 8, 2024 17:02
@ti-chi-bot ti-chi-bot bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 8, 2024
@winoros winoros force-pushed the eval-readonly-var branch from 0561371 to c8dc5cb Compare July 8, 2024 17:09
@ti-chi-bot ti-chi-bot bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 8, 2024
@ti-chi-bot ti-chi-bot bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 30, 2024
Copy link

ti-chi-bot bot commented Aug 30, 2024

PR needs rebase.

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.

@winoros winoros changed the title planner, expr: eval readonly user var during plan phase planner, expr: eval readonly user var during plan phase | tidb-test=pr/2419 Oct 22, 2024
@winoros
Copy link
Member Author

winoros commented Nov 4, 2024

/retest

Comment on lines +63 to +67
var (
val types.Datum
err error
)
val, err = usingParam.Eval(sctx.GetExprCtx().GetEvalCtx(), chunk.Row{})
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
var (
val types.Datum
err error
)
val, err = usingParam.Eval(sctx.GetExprCtx().GetEvalCtx(), chunk.Row{})
val, err := usingParam.Eval(sctx.GetExprCtx().GetEvalCtx(), chunk.Row{})

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Nov 5, 2024
Comment on lines 1016 to 1019
d, ok := ctx.GetEvalCtx().GetUserVarsReader().GetUserVarVal(arg0.Value.GetString())
if ok && d.Kind() == types.KindBinaryLiteral {
v.SetBinaryLiteral(v.GetBytes())
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we need a reuse here to avoid a read-only var evaluated multiple times

@@ -236,6 +242,9 @@ type preprocessor struct {

staleReadProcessor staleread.Processor

varsChanged map[string]struct{}
Copy link
Contributor

Choose a reason for hiding this comment

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

varsWritable or varsMutable ?

if !isConst || arg0.DeferredExpr != nil {
return getVar
}
isReadonly := ctx.IsReadonlyUserVar(arg0.Value.GetString())
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
isReadonly := ctx.IsReadonlyUserVar(arg0.Value.GetString())
varName := arg.Value.GetString()
isReadonly := ctx.IsReadonlyUserVar(varName)

intest.Assert(false, "readonly user variable should not meet error when executing.")
return getVar
}
d, ok := ctx.GetEvalCtx().GetUserVarsReader().GetUserVarVal(arg0.Value.GetString())
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
d, ok := ctx.GetEvalCtx().GetUserVarsReader().GetUserVarVal(arg0.Value.GetString())
d, ok := ctx.GetEvalCtx().GetUserVarsReader().GetUserVarVal(varName)

}

// optimizeReadonlyVar tries to convert the readonly user variables to constants.
func optimizeReadonlyVar(ctx BuildContext, getVar *ScalarFunction) Expression {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func optimizeReadonlyVar(ctx BuildContext, getVar *ScalarFunction) Expression {
func convertReadonlyVarToConst(ctx BuildContext, getVar *ScalarFunction) Expression {

Copy link

ti-chi-bot bot commented Dec 25, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: qw4990, XuHuaiyu

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:

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 approved lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Dec 25, 2024
Copy link

ti-chi-bot bot commented Dec 25, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-11-05 03:44:15.166733789 +0000 UTC m=+926168.005889336: ☑️ agreed by qw4990.
  • 2024-12-25 15:39:05.000520812 +0000 UTC m=+1662535.089323372: ☑️ agreed by XuHuaiyu.

@winoros
Copy link
Member Author

winoros commented Dec 25, 2024

/retest

@ti-chi-bot ti-chi-bot bot merged commit 9958228 into pingcap:master Dec 25, 2024
24 checks passed
@ti-chi-bot ti-chi-bot bot added the needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. label Dec 25, 2024
@winoros
Copy link
Member Author

winoros commented Dec 25, 2024

/cherrypick release-8.5

@winoros winoros deleted the eval-readonly-var branch December 25, 2024 18:05
ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Dec 25, 2024
@ti-chi-bot
Copy link
Member

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

@ti-chi-bot
Copy link
Member

@winoros: new pull request could not be created: failed to create pull request against pingcap/tidb#release-8.5 from head ti-chi-bot:cherry-pick-54462-to-release-8.5: status code 422 not one of [201], body: {"message":"Validation Failed","errors":[{"resource":"PullRequest","code":"custom","message":"A pull request already exists for ti-chi-bot:cherry-pick-54462-to-release-8.5."}],"documentation_url":"https://docs.github.com/rest/pulls/pulls#create-a-pull-request","status":"422"}

In response to this:

/cherrypick release-8.5

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 ti-community-infra/tichi repository.

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-8.5 Should cherry pick this PR to release-8.5 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.

Optimizer should use index when comparing with user-defined variables
5 participants