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: fix unexpected error msg for cast string to decimal #47267

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,11 @@ func (b *builtinCastStringAsDecimalSig) evalDecimal(row chunk.Row) (res *types.M
res = new(types.MyDecimal)
sc := b.ctx.GetSessionVars().StmtCtx
if !(b.inUnion && mysql.HasUnsignedFlag(b.tp.GetFlag()) && isNegative) {
err = sc.HandleTruncate(res.FromString([]byte(val)))
err = res.FromString([]byte(val))
if err == types.ErrTruncated {
err = types.ErrTruncatedWrongVal.GenWithStackByArgs("DECIMAL", []byte(val))
}
err = sc.HandleTruncate(err)
if err != nil {
return res, false, err
}
Expand Down
12 changes: 12 additions & 0 deletions expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2059,3 +2059,15 @@ func TestIssue41986(t *testing.T) {
// shouldn't report they can't find column error and return the right result.
tk.MustQuery("SELECT GROUP_CONCAT(effective_date order by stlmnt_hour DESC) FROM ( SELECT (COALESCE(pct.clearing_time, 0)/3600000) AS stlmnt_hour ,COALESCE(pct.effective_date, '1970-01-01 08:00:00') AS effective_date FROM poi_clearing_time_topic pct ORDER BY pct.effective_date DESC ) a;").Check(testkit.Rows("2023-08-25 00:00:00"))
}

func TestCastErrMsg(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Better move it to tests/integrationtest. It will have better performance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("drop table if exists t1;")
tk.MustExec("CREATE TABLE t1 (c1 TEXT);")
tk.MustExec("INSERT INTO t1 VALUES ('a');")
err := tk.ExecToErr("UPDATE t1 SET c1 = CAST('61QW' AS DECIMAL);")
require.Contains(t, err.Error(), "Truncated incorrect DECIMAL value: '61QW'")
}