Skip to content

Commit

Permalink
expression: fix unexpected error msg for cast string to decimal (#47267)
Browse files Browse the repository at this point in the history
close #44274
  • Loading branch information
guo-shaoge authored Sep 27, 2023
1 parent 3802398 commit 99d9d41
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
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

0 comments on commit 99d9d41

Please sign in to comment.