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 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
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
10 changes: 10 additions & 0 deletions tests/integrationtest/r/expression/cast.result
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,13 @@ select cast(col4 as time(31)) from t where col1 is null;
Error 1426 (42000): Too big precision 31 specified for column 'CAST'. Maximum is 6.
select cast(col5 as time(31)) from t where col1 is null;
Error 1426 (42000): Too big precision 31 specified for column 'CAST'. Maximum is 6.
drop table if exists t1;
create table t1 (c1 text);
insert into t1 values ('a');
update t1 set c1 = cast('61qw' as decimal);
Error 1292 (22007): Truncated incorrect DECIMAL value: '61qw'
select cast('61qw' as decimal);
cast('61qw' as decimal)
61
Level Code Message
Warning 1292 Truncated incorrect DECIMAL value: '61qw'
10 changes: 10 additions & 0 deletions tests/integrationtest/t/expression/cast.test
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ select cast(col3 as time(31)) from t where col1 is null;
select cast(col4 as time(31)) from t where col1 is null;
-- error 1426
select cast(col5 as time(31)) from t where col1 is null;

# TestCastErrMsg
drop table if exists t1;
create table t1 (c1 text);
insert into t1 values ('a');
--error 1292
update t1 set c1 = cast('61qw' as decimal);
--enable_warnings
select cast('61qw' as decimal);
--disable_warnings