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

expression: use the correct type when eval decimal and float session var #51395

Merged
merged 4 commits into from
Feb 29, 2024

Conversation

Rustin170506
Copy link
Member

@Rustin170506 Rustin170506 commented Feb 28, 2024

What problem does this PR solve?

Issue Number: close #43527

Problem Summary:

What changed and how does it work?

create table test (a datetime, b bigint, c decimal(10, 2), d float);
insert into test values('2010-10-10 10:10:10', 100, 100.01, 100);
SELECT @total := @total + c FROM (SELECT c FROM test) AS temp, (SELECT @total := 200) AS T1;

In the SQL, @total type is int64, but when we do eval decimal we will directly convert it as decimal then we will get a panic because there is no x for int value.
GetMysqlDecimal:

func (d *Datum) GetMysqlDecimal() *MyDecimal {
	return d.x.(*MyDecimal)
}

ToDecimal:

func (d *Datum) ToDecimal(ctx Context) (*MyDecimal, error) {
	switch d.Kind() {
	case KindMysqlTime:
		return d.GetMysqlTime().ToNumber(), nil
	case KindMysqlDuration:
		return d.GetMysqlDuration().ToNumber(), nil
	default:
		return ConvertDatumToDecimal(ctx, *d)
	}
}

So we have to try to convert it explicitly.

Same for the float value eval process. We also need to convert it.

Because #8412 disabled the vectorized execution of get or set variable, so we don't really execute that code right now. But I also updated it.

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.

Fixed an issue that TiDB could not correctly convert the type of a session variable
修复 TiDB 无法正确转换表达式中系统变量类型的问题

Signed-off-by: hi-rustin <rustin.liu@gmail.com>
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
@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/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 28, 2024
@Rustin170506 Rustin170506 changed the title expression: use the correct type when eval decimal system var expression: use the correct type when eval decimal and float session var Feb 28, 2024
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
@ti-chi-bot ti-chi-bot bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 28, 2024
Copy link
Member Author

@Rustin170506 Rustin170506 left a comment

Choose a reason for hiding this comment

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

🔢 Self-check

cc: @time-and-fate

Thanks for your help!

@Rustin170506
Copy link
Member Author

/retest

Copy link

codecov bot commented Feb 28, 2024

Codecov Report

Merging #51395 (a3484a5) into master (c23f0c9) will increase coverage by 2.4235%.
Report is 23 commits behind head on master.
The diff coverage is 20.0000%.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #51395        +/-   ##
================================================
+ Coverage   70.6663%   73.0898%   +2.4235%     
================================================
  Files          1461       1462         +1     
  Lines        434876     442198      +7322     
================================================
+ Hits         307311     323202     +15891     
+ Misses       108307      99059      -9248     
- Partials      19258      19937       +679     
Flag Coverage Δ
integration 49.3792% <20.0000%> (?)
unit 70.6050% <20.0000%> (+0.1424%) ⬆️

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

Components Coverage Δ
dumpling 53.9957% <ø> (ø)
parser ∅ <ø> (∅)
br 51.2558% <ø> (+5.3537%) ⬆️

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Feb 28, 2024
@Rustin170506
Copy link
Member Author

/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 lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Feb 28, 2024
@@ -1052,7 +1052,11 @@ func (b *builtinGetRealVarSig) evalReal(ctx EvalContext, row chunk.Row) (float64
}
varName = strings.ToLower(varName)
if v, ok := sessionVars.GetUserVarVal(varName); ok {
return v.GetFloat64(), false, nil
Copy link
Contributor

@XuHuaiyu XuHuaiyu Feb 29, 2024

Choose a reason for hiding this comment

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

What about the implementation of (b *builtinGetRealVarSig) vecEvalReal

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Copy link
Member Author

Choose a reason for hiding this comment

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

Because this PR: #8412

There is no way to vectorized eval real. But I guess it makes sense to update it as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, please. Much appreciated.

Let's talk about enabling vectorization for get/set var function in the future.

Copy link
Contributor

@zanmato1984 zanmato1984 left a comment

Choose a reason for hiding this comment

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

Is there any case we can add into this UT:

func TestGetVar(t *testing.T) {
.
As well as https://github.com/pingcap/tidb/blob/27ce02afd2e3d312b2bb92e20489cefce000e320/pkg/expression/builtin_other_vec_test.go , assuming you are going to add vectorized version of builtinGetRealVarSig.

@@ -1052,7 +1052,11 @@ func (b *builtinGetRealVarSig) evalReal(ctx EvalContext, row chunk.Row) (float64
}
varName = strings.ToLower(varName)
if v, ok := sessionVars.GetUserVarVal(varName); ok {
return v.GetFloat64(), false, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

@ti-chi-bot ti-chi-bot bot removed the lgtm label Feb 29, 2024
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
Copy link
Member Author

@Rustin170506 Rustin170506 left a comment

Choose a reason for hiding this comment

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

🔢 Self-check

Copy link
Contributor

@zanmato1984 zanmato1984 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 approved label Feb 29, 2024
@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Feb 29, 2024
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

Copy link

ti-chi-bot bot commented Feb 29, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: AilinKid, elsa0520, zanmato1984

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

ti-chi-bot bot commented Feb 29, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-02-28 09:20:18.283019794 +0000 UTC m=+1040107.030642913: ☑️ agreed by AilinKid.
  • 2024-02-28 10:00:50.671372065 +0000 UTC m=+1042539.418995171: ☑️ agreed by elsa0520.
  • 2024-02-29 03:41:45.72004397 +0000 UTC m=+1106194.467667080: ✖️🔁 reset by zanmato1984.
  • 2024-02-29 09:18:32.645908407 +0000 UTC m=+1126401.393531518: ☑️ agreed by zanmato1984.
  • 2024-02-29 09:34:25.804268372 +0000 UTC m=+1127354.551891483: ☑️ agreed by elsa0520.

@ti-chi-bot ti-chi-bot bot merged commit 38ab23b into pingcap:master Feb 29, 2024
23 checks passed
@ti-chi-bot ti-chi-bot added the needs-cherry-pick-release-7.5 Should cherry pick this PR to release-7.5 branch. label May 21, 2024
@ti-chi-bot
Copy link
Member

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

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request May 21, 2024
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved component/expression lgtm needs-cherry-pick-release-7.5 Should cherry pick this PR to release-7.5 branch. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/planner SIG: Planner size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Got interface conversion error when there are get_sys_var expr
6 participants