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) #53415

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pkg/expression/builtin_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,11 @@ func (b *builtinGetRealVarSig) evalReal(row chunk.Row) (float64, bool, error) {
}
varName = strings.ToLower(varName)
if v, ok := sessionVars.GetUserVarVal(varName); ok {
return v.GetFloat64(), false, nil
d, err := v.ToFloat64(sessionVars.StmtCtx)
if err != nil {
return 0, false, err
}
return d, false, nil
}
return 0, true, nil
}
Expand Down Expand Up @@ -1093,7 +1097,11 @@ func (b *builtinGetDecimalVarSig) evalDecimal(row chunk.Row) (*types.MyDecimal,
}
varName = strings.ToLower(varName)
if v, ok := sessionVars.GetUserVarVal(varName); ok {
return v.GetMysqlDecimal(), false, nil
d, err := v.ToDecimal(sessionVars.StmtCtx)
if err != nil {
return nil, false, err
}
return d, false, nil
}
return nil, true, nil
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/expression/builtin_other_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,33 @@ func TestGetVar(t *testing.T) {
}
}

func TestTypeConversion(t *testing.T) {
ctx := createContext(t)
// Set value as int64
key := "a"
val := int64(3)
ctx.GetSessionVars().SetUserVarVal(key, types.NewDatum(val))
tp := types.NewFieldType(mysql.TypeLonglong)
ctx.GetSessionVars().SetUserVarType(key, tp)

args := []any{"a"}
// To Decimal.
tp = types.NewFieldType(mysql.TypeNewDecimal)
fn, err := BuildGetVarFunction(ctx, datumsToConstants(types.MakeDatums(args...))[0], tp)
require.NoError(t, err)
d, err := fn.Eval(chunk.Row{})
require.NoError(t, err)
des := types.NewDecFromInt(3)
require.Equal(t, des, d.GetValue())
// To Float.
tp = types.NewFieldType(mysql.TypeDouble)
fn, err = BuildGetVarFunction(ctx, datumsToConstants(types.MakeDatums(args...))[0], tp)
require.NoError(t, err)
d, err = fn.Eval(chunk.Row{})
require.NoError(t, err)
require.Equal(t, float64(3), d.GetValue())
}

func TestValues(t *testing.T) {
ctx := createContext(t)
fc := &valuesFunctionClass{baseFunctionClass{ast.Values, 0, 0}, 1, types.NewFieldType(mysql.TypeVarchar)}
Expand Down
16 changes: 14 additions & 2 deletions pkg/expression/builtin_other_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ func (b *builtinGetRealVarSig) vectorized() bool {
return true
}

// NOTE: get/set variable vectorized eval was disabled. See more in
// https://github.com/pingcap/tidb/pull/8412
func (b *builtinGetRealVarSig) vecEvalReal(input *chunk.Chunk, result *chunk.Column) error {
n := input.NumRows()
buf0, err := b.bufAllocator.get()
Expand All @@ -406,7 +408,11 @@ func (b *builtinGetRealVarSig) vecEvalReal(input *chunk.Chunk, result *chunk.Col
}
varName := strings.ToLower(buf0.GetString(i))
if v, ok := sessionVars.GetUserVarVal(varName); ok {
f64s[i] = v.GetFloat64()
d, err := v.ToFloat64(sessionVars.StmtCtx)
if err != nil {
return err
}
f64s[i] = d
continue
}
result.SetNull(i, true)
Expand All @@ -418,6 +424,8 @@ func (b *builtinGetDecimalVarSig) vectorized() bool {
return true
}

// NOTE: get/set variable vectorized eval was disabled. See more in
// https://github.com/pingcap/tidb/pull/8412
func (b *builtinGetDecimalVarSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.Column) error {
n := input.NumRows()
buf0, err := b.bufAllocator.get()
Expand All @@ -438,7 +446,11 @@ func (b *builtinGetDecimalVarSig) vecEvalDecimal(input *chunk.Chunk, result *chu
}
varName := strings.ToLower(buf0.GetString(i))
if v, ok := sessionVars.GetUserVarVal(varName); ok {
decs[i] = *v.GetMysqlDecimal()
d, err := v.ToDecimal(sessionVars.StmtCtx)
if err != nil {
return err
}
decs[i] = *d
continue
}
result.SetNull(i, true)
Expand Down
2 changes: 1 addition & 1 deletion pkg/expression/integration_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_test(
"main_test.go",
],
flaky = True,
shard_count = 28,
shard_count = 29,
deps = [
"//pkg/config",
"//pkg/domain",
Expand Down
24 changes: 24 additions & 0 deletions pkg/expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3271,3 +3271,27 @@ func TestTiDBRowChecksumBuiltin(t *testing.T) {
tk.MustGetDBError("select tidb_row_checksum() from t", expression.ErrNotSupportedYet)
tk.MustGetDBError("select tidb_row_checksum() from t where id > 0", expression.ErrNotSupportedYet)
}

func TestIssue43527(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table test (a datetime, b bigint, c decimal(10, 2), d float)")
tk.MustExec("insert into test values('2010-10-10 10:10:10', 100, 100.01, 100)")
// Decimal.
tk.MustQuery(
"SELECT @total := @total + c FROM (SELECT c FROM test) AS temp, (SELECT @total := 200) AS T1",
).Check(testkit.Rows("300.01"))
// Float.
tk.MustQuery(
"SELECT @total := @total + d FROM (SELECT d FROM test) AS temp, (SELECT @total := 200) AS T1",
).Check(testkit.Rows("300"))
tk.MustExec("insert into test values('2010-10-10 10:10:10', 100, 100.01, 100)")
// Vectorized.
// NOTE: Because https://github.com/pingcap/tidb/pull/8412 disabled the vectorized execution of get or set variable,
// the following test case will not be executed in vectorized mode.
// It will be executed in the normal mode.
tk.MustQuery(
"SELECT @total := @total + d FROM (SELECT d FROM test) AS temp, (SELECT @total := b FROM test) AS T1 where @total >= 100",
).Check(testkit.Rows("200", "300", "400", "500"))
}