Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions pkg/executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1615,3 +1615,16 @@ func TestInsertBigScientificNotation(t *testing.T) {
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1264 Out of range value for column 'a' at row 1"))
tk.MustQuery("select id, a from t1 order by id asc").Check(testkit.Rows("1 2147483647", "2 -2147483648"))
}

// see issue: https://github.com/pingcap/tidb/issues/47945
func TestUnsignedDecimalFloatInsertNegative(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table tf(a float(1, 0) unsigned)")
err := tk.ExecToErr("insert into tf values('-100')")
require.EqualError(t, err, "[types:1264]Out of range value for column 'a' at row 1")
tk.MustExec("set @@sql_mode=''")
tk.MustExec("insert into tf values('-100')")
tk.MustQuery("select * from tf").Check(testkit.Rows("0"))
}
2 changes: 1 addition & 1 deletion pkg/expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ func (b *builtinCastStringAsRealSig) evalReal(row chunk.Row) (res float64, isNul
if b.inUnion && mysql.HasUnsignedFlag(b.tp.GetFlag()) && res < 0 {
res = 0
}
res, err = types.ProduceFloatWithSpecifiedTp(res, b.tp, sc)
res, err = types.ProduceFloatWithSpecifiedTp(res, b.tp)
return res, false, err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/expression/builtin_cast_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,7 @@ func (b *builtinCastStringAsRealSig) vecEvalReal(input *chunk.Chunk, result *chu
if b.inUnion && mysql.HasUnsignedFlag(b.tp.GetFlag()) && res < 0 {
res = 0
}
res, err = types.ProduceFloatWithSpecifiedTp(res, b.tp, sc)
res, err = types.ProduceFloatWithSpecifiedTp(res, b.tp)
if err != nil {
return err
}
Expand Down
9 changes: 3 additions & 6 deletions pkg/types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ func (d *Datum) convertToFloat(sc *stmtctx.StatementContext, target *FieldType)
default:
return invalidConv(d, target.GetType())
}
f, err1 := ProduceFloatWithSpecifiedTp(f, target, sc)
f, err1 := ProduceFloatWithSpecifiedTp(f, target)
if err == nil && err1 != nil {
err = err1
}
Expand All @@ -1001,7 +1001,7 @@ func (d *Datum) convertToFloat(sc *stmtctx.StatementContext, target *FieldType)
}

// ProduceFloatWithSpecifiedTp produces a new float64 according to `flen` and `decimal`.
func ProduceFloatWithSpecifiedTp(f float64, target *FieldType, sc *stmtctx.StatementContext) (_ float64, err error) {
func ProduceFloatWithSpecifiedTp(f float64, target *FieldType) (_ float64, err error) {
if math.IsNaN(f) {
return 0, overflow(f, target.GetType())
}
Expand All @@ -1012,9 +1012,6 @@ func ProduceFloatWithSpecifiedTp(f float64, target *FieldType, sc *stmtctx.State
// If no D is set, we will handle it like origin float whether M is set or not.
if target.GetFlen() != UnspecifiedLength && target.GetDecimal() != UnspecifiedLength {
f, err = TruncateFloat(f, target.GetFlen(), target.GetDecimal())
if err = sc.HandleOverflow(err, err); err != nil {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

HandleOverflow seems useless now, because in cast expr like cast("1" as float(2, 1)) , target.GetDecimal() always return UnspecifiedLength ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In mysql cast to float with decimal will fail:

mysql> select cast("1" as float(1, 0));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ', 0))' at line 1

return f, errors.Trace(err)
}
}
if mysql.HasUnsignedFlag(target.GetFlag()) && f < 0 {
return 0, overflow(f, target.GetType())
Expand All @@ -1025,7 +1022,7 @@ func ProduceFloatWithSpecifiedTp(f float64, target *FieldType, sc *stmtctx.State
}
return -math.MaxFloat32, overflow(f, target.GetType())
}
return f, nil
return f, errors.Trace(err)
}

func (d *Datum) convertToString(sc *stmtctx.StatementContext, target *FieldType) (Datum, error) {
Expand Down