Skip to content

Commit

Permalink
Merge branch 'master' into fix-32672
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 authored Apr 2, 2022
2 parents 92e9356 + 9b358bc commit 21fe2fa
Show file tree
Hide file tree
Showing 25 changed files with 169 additions and 79 deletions.
4 changes: 2 additions & 2 deletions executor/aggfuncs/func_avg.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (e *baseAvgDecimal) AppendFinalResult2Chunk(sctx sessionctx.Context, pr Par
if frac == -1 {
frac = mysql.MaxDecimalScale
}
err = finalResult.Round(finalResult, frac, types.ModeHalfEven)
err = finalResult.Round(finalResult, frac, types.ModeHalfUp)
if err != nil {
return err
}
Expand Down Expand Up @@ -276,7 +276,7 @@ func (e *avgOriginal4DistinctDecimal) AppendFinalResult2Chunk(sctx sessionctx.Co
if frac == -1 {
frac = mysql.MaxDecimalScale
}
err = finalResult.Round(finalResult, frac, types.ModeHalfEven)
err = finalResult.Round(finalResult, frac, types.ModeHalfUp)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion executor/aggfuncs/func_first_row.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func (e *firstRow4Decimal) AppendFinalResult2Chunk(sctx sessionctx.Context, pr P
if frac == -1 {
frac = mysql.MaxDecimalScale
}
err := p.val.Round(&p.val, frac, types.ModeHalfEven)
err := p.val.Round(&p.val, frac, types.ModeHalfUp)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion executor/aggfuncs/func_max_min.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ func (e *maxMin4Decimal) AppendFinalResult2Chunk(sctx sessionctx.Context, pr Par
if frac == -1 {
frac = mysql.MaxDecimalScale
}
err := p.val.Round(&p.val, frac, types.ModeHalfEven)
err := p.val.Round(&p.val, frac, types.ModeHalfUp)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion executor/aggfuncs/func_sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (e *sum4Decimal) AppendFinalResult2Chunk(sctx sessionctx.Context, pr Partia
if frac == -1 {
frac = mysql.MaxDecimalScale
}
err := p.val.Round(&p.val, frac, types.ModeHalfEven)
err := p.val.Round(&p.val, frac, types.ModeHalfUp)
if err != nil {
return err
}
Expand Down
19 changes: 19 additions & 0 deletions executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1984,3 +1984,22 @@ func TestInsertIntoSelectError(t *testing.T) {
tk.MustQuery("SELECT * FROM t1;").Check(testkit.Rows("0", "0", "0"))
tk.MustExec("DROP TABLE t1;")
}

// https://github.com/pingcap/tidb/issues/32213.
func TestIssue32213(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)

tk.MustExec("create table test.t1(c1 float)")
tk.MustExec("insert into test.t1 values(999.99)")
tk.MustQuery("select cast(test.t1.c1 as decimal(4, 1)) from test.t1").Check(testkit.Rows("999.9"))
tk.MustQuery("select cast(test.t1.c1 as decimal(5, 1)) from test.t1").Check(testkit.Rows("1000.0"))

tk.MustExec("drop table if exists test.t1")
tk.MustExec("create table test.t1(c1 decimal(6, 4))")
tk.MustExec("insert into test.t1 values(99.9999)")
tk.MustQuery("select cast(test.t1.c1 as decimal(5, 3)) from test.t1").Check(testkit.Rows("99.999"))
tk.MustQuery("select cast(test.t1.c1 as decimal(6, 3)) from test.t1").Check(testkit.Rows("100.000"))
}
2 changes: 1 addition & 1 deletion expression/aggregation/avg.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (af *avgFunction) GetResult(evalCtx *AggEvaluateContext) (d types.Datum) {
if frac == -1 {
frac = mysql.MaxDecimalScale
}
err = to.Round(to, mathutil.Min(frac, mysql.MaxDecimalScale), types.ModeHalfEven)
err = to.Round(to, mathutil.Min(frac, mysql.MaxDecimalScale), types.ModeHalfUp)
terror.Log(err)
d.SetMysqlDecimal(to)
}
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ func (s *builtinArithmeticDivideDecimalSig) evalDecimal(row chunk.Row) (*types.M
} else if err == nil {
_, frac := c.PrecisionAndFrac()
if frac < s.baseBuiltinFunc.tp.Decimal {
err = c.Round(c, s.baseBuiltinFunc.tp.Decimal, types.ModeHalfEven)
err = c.Round(c, s.baseBuiltinFunc.tp.Decimal, types.ModeHalfUp)
}
} else if err == types.ErrOverflow {
err = types.ErrOverflow.GenWithStackByArgs("DECIMAL", fmt.Sprintf("(%s / %s)", s.args[0].String(), s.args[1].String()))
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_arithmetic_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (b *builtinArithmeticDivideDecimalSig) vecEvalDecimal(input *chunk.Chunk, r
} else if err == nil {
_, frac = to.PrecisionAndFrac()
if frac < b.baseBuiltinFunc.tp.Decimal {
if err = to.Round(&to, b.baseBuiltinFunc.tp.Decimal, types.ModeHalfEven); err != nil {
if err = to.Round(&to, b.baseBuiltinFunc.tp.Decimal, types.ModeHalfUp); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ func (b *builtinCastDecimalAsIntSig) evalInt(row chunk.Row) (res int64, isNull b

// Round is needed for both unsigned and signed.
var to types.MyDecimal
err = val.Round(&to, 0, types.ModeHalfEven)
err = val.Round(&to, 0, types.ModeHalfUp)
if err != nil {
return 0, true, err
}
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_cast_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,7 @@ func (b *builtinCastDecimalAsIntSig) vecEvalInt(input *chunk.Chunk, result *chun

// Round is needed for both unsigned and signed.
to := d64s[i]
err = d64s[i].Round(&to, 0, types.ModeHalfEven)
err = d64s[i].Round(&to, 0, types.ModeHalfUp)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions expression/builtin_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func (b *builtinRoundDecSig) evalDecimal(row chunk.Row) (*types.MyDecimal, bool,
return nil, isNull, err
}
to := new(types.MyDecimal)
if err = val.Round(to, 0, types.ModeHalfEven); err != nil {
if err = val.Round(to, 0, types.ModeHalfUp); err != nil {
return nil, true, err
}
return to, false, nil
Expand Down Expand Up @@ -469,7 +469,7 @@ func (b *builtinRoundWithFracDecSig) evalDecimal(row chunk.Row) (*types.MyDecima
return nil, isNull, err
}
to := new(types.MyDecimal)
if err = val.Round(to, mathutil.Min(int(frac), b.tp.Decimal), types.ModeHalfEven); err != nil {
if err = val.Round(to, mathutil.Min(int(frac), b.tp.Decimal), types.ModeHalfUp); err != nil {
return nil, true, err
}
return to, false, nil
Expand Down
4 changes: 2 additions & 2 deletions expression/builtin_math_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func (b *builtinRoundDecSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.Co
if result.IsNull(i) {
continue
}
if err := d64s[i].Round(buf, 0, types.ModeHalfEven); err != nil {
if err := d64s[i].Round(buf, 0, types.ModeHalfUp); err != nil {
return err
}
d64s[i] = *buf
Expand Down Expand Up @@ -994,7 +994,7 @@ func (b *builtinRoundWithFracDecSig) vecEvalDecimal(input *chunk.Chunk, result *
continue
}
// TODO: reuse d64[i] and remove the temporary variable tmp.
if err := d64s[i].Round(tmp, mathutil.Min(int(i64s[i]), b.tp.Decimal), types.ModeHalfEven); err != nil {
if err := d64s[i].Round(tmp, mathutil.Min(int(i64s[i]), b.tp.Decimal), types.ModeHalfUp); err != nil {
return err
}
d64s[i] = *tmp
Expand Down
8 changes: 4 additions & 4 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1734,7 +1734,7 @@ func evalFromUnixTime(ctx sessionctx.Context, fsp int, unixTimeStamp *types.MyDe

sc := ctx.GetSessionVars().StmtCtx
tmp := time.Unix(integralPart, fractionalPart).In(sc.TimeZone)
t, err := convertTimeToMysqlTime(tmp, fsp, types.ModeHalfEven)
t, err := convertTimeToMysqlTime(tmp, fsp, types.ModeHalfUp)
if err != nil {
return res, true, err
}
Expand Down Expand Up @@ -2050,7 +2050,7 @@ func (b *builtinSysDateWithFspSig) evalTime(row chunk.Row) (d types.Time, isNull

loc := b.ctx.GetSessionVars().Location()
now := time.Now().In(loc)
result, err := convertTimeToMysqlTime(now, int(fsp), types.ModeHalfEven)
result, err := convertTimeToMysqlTime(now, int(fsp), types.ModeHalfUp)
if err != nil {
return types.ZeroTime, true, err
}
Expand All @@ -2072,7 +2072,7 @@ func (b *builtinSysDateWithoutFspSig) Clone() builtinFunc {
func (b *builtinSysDateWithoutFspSig) evalTime(row chunk.Row) (d types.Time, isNull bool, err error) {
tz := b.ctx.GetSessionVars().Location()
now := time.Now().In(tz)
result, err := convertTimeToMysqlTime(now, 0, types.ModeHalfEven)
result, err := convertTimeToMysqlTime(now, 0, types.ModeHalfUp)
if err != nil {
return types.ZeroTime, true, err
}
Expand Down Expand Up @@ -2393,7 +2393,7 @@ func evalUTCTimestampWithFsp(ctx sessionctx.Context, fsp int) (types.Time, bool,
if err != nil {
return types.ZeroTime, true, err
}
result, err := convertTimeToMysqlTime(nowTs.UTC(), fsp, types.ModeHalfEven)
result, err := convertTimeToMysqlTime(nowTs.UTC(), fsp, types.ModeHalfUp)
if err != nil {
return types.ZeroTime, true, err
}
Expand Down
4 changes: 2 additions & 2 deletions expression/builtin_time_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (b *builtinSysDateWithoutFspSig) vecEvalTime(input *chunk.Chunk, result *ch

result.ResizeTime(n, false)
times := result.Times()
t, err := convertTimeToMysqlTime(now, 0, types.ModeHalfEven)
t, err := convertTimeToMysqlTime(now, 0, types.ModeHalfUp)
if err != nil {
return err
}
Expand Down Expand Up @@ -775,7 +775,7 @@ func (b *builtinSysDateWithFspSig) vecEvalTime(input *chunk.Chunk, result *chunk
if result.IsNull(i) {
continue
}
t, err := convertTimeToMysqlTime(now, int(ds[i]), types.ModeHalfEven)
t, err := convertTimeToMysqlTime(now, int(ds[i]), types.ModeHalfUp)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion expression/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (c *Constant) EvalDecimal(ctx sessionctx.Context, row chunk.Row) (*types.My
// The decimal may be modified during plan building.
_, frac := res.PrecisionAndFrac()
if frac < c.GetType().Decimal {
err = res.Round(res, c.GetType().Decimal, types.ModeHalfEven)
err = res.Round(res, c.GetType().Decimal, types.ModeHalfUp)
}
return res, false, err
}
Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/BurntSushi/toml v0.3.1
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/Jeffail/gabs/v2 v2.5.1
github.com/Shopify/sarama v1.32.0
github.com/Shopify/sarama v1.29.0
github.com/aws/aws-sdk-go v1.35.3
github.com/blacktear23/go-proxyprotocol v0.0.0-20180807104634-af7a81e8dd0d
github.com/carlmjohnson/flagext v0.21.0
Expand All @@ -21,7 +21,7 @@ require (
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548
github.com/cznic/sortutil v0.0.0-20181122101858-f5f958428db8
github.com/danjacques/gofslock v0.0.0-20191023191349-0a45f885bc37
github.com/dgraph-io/ristretto v0.1.0
github.com/dgraph-io/ristretto v0.1.1-0.20211108053508-297c39e6640f
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13
github.com/docker/go-units v0.4.0
github.com/fsouza/fake-gcs-server v1.19.0
Expand Down Expand Up @@ -83,7 +83,7 @@ require (
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5
golang.org/x/sys v0.0.0-20220330033206-e17cdc41300f
golang.org/x/text v0.3.7
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65
golang.org/x/tools v0.1.8
Expand Down Expand Up @@ -205,3 +205,6 @@ replace github.com/pingcap/tidb/parser => ./parser

// fix potential security issue(CVE-2020-26160) introduced by indirect dependency.
replace github.com/dgrijalva/jwt-go => github.com/form3tech-oss/jwt-go v3.2.6-0.20210809144907-32ab6a8243d7+incompatible

// it can be removed after merging https://github.com/dgraph-io/ristretto/pull/294
replace github.com/dgraph-io/ristretto => github.com/hawkingrei/ristretto v0.1.1-0.20220402052934-7556ec01f9db
Loading

0 comments on commit 21fe2fa

Please sign in to comment.