Skip to content

Commit

Permalink
*: use new MyDecimal implementation. (pingcap#1611)
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood authored Aug 22, 2016
1 parent e487035 commit e31b281
Show file tree
Hide file tree
Showing 32 changed files with 521 additions and 2,143 deletions.
8 changes: 4 additions & 4 deletions ast/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ func (ts *testFunctionsSuite) TestAggFuncSum(c *C) {
agg.Update()
}
ctx := agg.GetContext()
expect, _ := mysql.ConvertToDecimal(1)
expect := mysql.NewDecFromInt(1)
c.Assert(ctx.Value.Kind(), Equals, types.KindMysqlDecimal)
c.Assert(ctx.Value.GetMysqlDecimal().Equals(expect), IsTrue)
c.Assert(ctx.Value.GetMysqlDecimal().Compare(expect), Equals, 0)
// sum without distinct
agg = &AggregateFuncExpr{
Args: args,
Expand All @@ -145,7 +145,7 @@ func (ts *testFunctionsSuite) TestAggFuncSum(c *C) {
agg.Update()
}
ctx = agg.GetContext()
expect, _ = mysql.ConvertToDecimal(4)
expect = mysql.NewDecFromInt(4)
c.Assert(ctx.Value.Kind(), Equals, types.KindMysqlDecimal)
c.Assert(ctx.Value.GetMysqlDecimal().Equals(expect), IsTrue)
c.Assert(ctx.Value.GetMysqlDecimal().Compare(expect), Equals, 0)
}
13 changes: 9 additions & 4 deletions evaluator/builtin_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,16 +454,21 @@ func unaryOpFactory(op opcode.Op) BuiltinFunc {
case types.KindFloat32:
d.SetFloat32(-aDatum.GetFloat32())
case types.KindMysqlDuration:
d.SetMysqlDecimal(mysql.ZeroDecimal.Sub(aDatum.GetMysqlDuration().ToNumber()))
dec := new(mysql.MyDecimal)
err = mysql.DecimalSub(new(mysql.MyDecimal), aDatum.GetMysqlDuration().ToNumber(), dec)
d.SetMysqlDecimal(dec)
case types.KindMysqlTime:
d.SetMysqlDecimal(mysql.ZeroDecimal.Sub(aDatum.GetMysqlTime().ToNumber()))
dec := new(mysql.MyDecimal)
err = mysql.DecimalSub(new(mysql.MyDecimal), aDatum.GetMysqlTime().ToNumber(), dec)
d.SetMysqlDecimal(dec)
case types.KindString, types.KindBytes:
f, err1 := types.StrToFloat(aDatum.GetString())
err = errors.Trace(err1)
d.SetFloat64(-f)
case types.KindMysqlDecimal:
f, _ := aDatum.GetMysqlDecimal().Float64()
d.SetMysqlDecimal(mysql.NewDecimalFromFloat(-f))
dec := new(mysql.MyDecimal)
err = mysql.DecimalSub(new(mysql.MyDecimal), aDatum.GetMysqlDecimal(), dec)
d.SetMysqlDecimal(dec)
case types.KindMysqlHex:
d.SetFloat64(-aDatum.GetMysqlHex().ToNumber())
case types.KindMysqlBit:
Expand Down
23 changes: 17 additions & 6 deletions evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,16 +537,24 @@ func (e *Evaluator) unaryOperation(u *ast.UnaryOperationExpr) bool {
case types.KindFloat32:
u.SetFloat32(-aDatum.GetFloat32())
case types.KindMysqlDuration:
u.SetMysqlDecimal(mysql.ZeroDecimal.Sub(aDatum.GetMysqlDuration().ToNumber()))
var to = new(mysql.MyDecimal)
var zero mysql.MyDecimal
mysql.DecimalSub(&zero, aDatum.GetMysqlDuration().ToNumber(), to)
u.SetMysqlDecimal(to)
case types.KindMysqlTime:
u.SetMysqlDecimal(mysql.ZeroDecimal.Sub(aDatum.GetMysqlTime().ToNumber()))
dec := aDatum.GetMysqlTime().ToNumber()
var zero, to mysql.MyDecimal
mysql.DecimalSub(&zero, dec, &to)
u.SetMysqlDecimal(&to)
case types.KindString, types.KindBytes:
f, err := types.StrToFloat(aDatum.GetString())
e.err = errors.Trace(err)
u.SetFloat64(-f)
case types.KindMysqlDecimal:
f, _ := aDatum.GetMysqlDecimal().Float64()
u.SetMysqlDecimal(mysql.NewDecimalFromFloat(-f))
dec := aDatum.GetMysqlDecimal()
var zero, to mysql.MyDecimal
mysql.DecimalSub(&zero, dec, &to)
u.SetMysqlDecimal(&to)
case types.KindMysqlHex:
u.SetFloat64(-aDatum.GetMysqlHex().ToNumber())
case types.KindMysqlBit:
Expand Down Expand Up @@ -714,8 +722,11 @@ func (e *Evaluator) evalAggAvg(v *ast.AggregateFuncExpr) {
v.SetValue(t)
case types.KindMysqlDecimal:
x := ctx.Value.GetMysqlDecimal()
t := x.Div(mysql.NewDecimalFromUint(uint64(ctx.Count), 0))
v.SetMysqlDecimal(t)
var y, to mysql.MyDecimal
y.FromUint(uint64(ctx.Count))
mysql.DecimalDiv(x, &y, &to, mysql.DivFracIncr)
to.Round(&to, ctx.Value.Frac()+mysql.DivFracIncr)
v.SetMysqlDecimal(&to)
}
ctx.Value = *v.GetDatum()
}
Expand Down
24 changes: 12 additions & 12 deletions evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (s *testEvaluatorSuite) TestBinopNumeric(c *C) {
{1, opcode.Plus, 1, 2},
{1, opcode.Plus, uint64(1), 2},
{1, opcode.Plus, "1", 2},
{1, opcode.Plus, mysql.NewDecimalFromInt(1, 0), 2},
{1, opcode.Plus, mysql.NewDecFromInt(1), 2},
{uint64(1), opcode.Plus, 1, 2},
{uint64(1), opcode.Plus, uint64(1), 2},
{1, opcode.Plus, []byte("1"), 2},
Expand All @@ -291,17 +291,17 @@ func (s *testEvaluatorSuite) TestBinopNumeric(c *C) {
{1, opcode.Minus, 1, 0},
{1, opcode.Minus, uint64(1), 0},
{1, opcode.Minus, float64(1), 0},
{1, opcode.Minus, mysql.NewDecimalFromInt(1, 0), 0},
{1, opcode.Minus, mysql.NewDecFromInt(1), 0},
{uint64(1), opcode.Minus, 1, 0},
{uint64(1), opcode.Minus, uint64(1), 0},
{mysql.NewDecimalFromInt(1, 0), opcode.Minus, 1, 0},
{mysql.NewDecFromInt(1), opcode.Minus, 1, 0},
{"1", opcode.Minus, []byte("1"), 0},

// mul
{1, opcode.Mul, 1, 1},
{1, opcode.Mul, uint64(1), 1},
{1, opcode.Mul, float64(1), 1},
{1, opcode.Mul, mysql.NewDecimalFromInt(1, 0), 1},
{1, opcode.Mul, mysql.NewDecFromInt(1), 1},
{uint64(1), opcode.Mul, 1, 1},
{uint64(1), opcode.Mul, uint64(1), 1},
{mysql.Time{}, opcode.Mul, 0, 0},
Expand Down Expand Up @@ -341,8 +341,8 @@ func (s *testEvaluatorSuite) TestBinopNumeric(c *C) {
{uint64(10), opcode.Mod, -2, 0},
{float64(10), opcode.Mod, 2, 0},
{float64(10), opcode.Mod, 0, nil},
{mysql.NewDecimalFromInt(10, 0), opcode.Mod, 2, 0},
{mysql.NewDecimalFromInt(10, 0), opcode.Mod, 0, nil},
{mysql.NewDecFromInt(10), opcode.Mod, 2, 0},
{mysql.NewDecFromInt(10), opcode.Mod, 0, nil},
}

for _, t := range tbl {
Expand Down Expand Up @@ -873,14 +873,14 @@ func (s *testEvaluatorSuite) TestUnaryOp(c *C) {
op opcode.Op
result interface{}
}{
{mysql.NewDecimalFromInt(1, 0), opcode.Plus, mysql.NewDecimalFromInt(1, 0)},
{mysql.NewDecFromInt(1), opcode.Plus, mysql.NewDecFromInt(1)},
{mysql.Duration{Duration: time.Duration(838*3600 + 59*60 + 59), Fsp: mysql.DefaultFsp}, opcode.Plus,
mysql.Duration{Duration: time.Duration(838*3600 + 59*60 + 59), Fsp: mysql.DefaultFsp}},
{mysql.Time{Time: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), Type: mysql.TypeDatetime, Fsp: 0}, opcode.Plus, mysql.Time{Time: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), Type: mysql.TypeDatetime, Fsp: 0}},

{mysql.NewDecimalFromInt(1, 0), opcode.Minus, mysql.NewDecimalFromInt(-1, 0)},
{mysql.ZeroDuration, opcode.Minus, mysql.NewDecimalFromInt(0, 0)},
{mysql.Time{Time: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), Type: mysql.TypeDatetime, Fsp: 0}, opcode.Minus, mysql.NewDecimalFromInt(-20091110230000, 0)},
{mysql.NewDecFromInt(1), opcode.Minus, mysql.NewDecFromInt(-1)},
{mysql.ZeroDuration, opcode.Minus, new(mysql.MyDecimal)},
{mysql.Time{Time: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), Type: mysql.TypeDatetime, Fsp: 0}, opcode.Minus, mysql.NewDecFromInt(-20091110230000)},
}

for _, t := range tbl {
Expand Down Expand Up @@ -934,9 +934,9 @@ func (s *testEvaluatorSuite) TestAggFuncAvg(c *C) {

result, err = Eval(ctx, avg)
c.Assert(err, IsNil)
expect, _ := mysql.ConvertToDecimal(3)
expect := mysql.NewDecFromInt(3)
c.Assert(result.Kind(), Equals, types.KindMysqlDecimal)
c.Assert(result.GetMysqlDecimal().Equals(expect), IsTrue)
c.Assert(result.GetMysqlDecimal().Compare(expect), Equals, 0)
}

func (s *testEvaluatorSuite) TestGetTimeValue(c *C) {
Expand Down
14 changes: 7 additions & 7 deletions executor/aggregate_xapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,21 +241,21 @@ func (s *testAggFuncSuite) TestXAPISum(c *C) {
ctx := mock.NewContext()
val, err := evaluator.Eval(ctx, fc)
c.Assert(err, IsNil)
c.Assert(val, testutil.DatumEquals, types.NewDecimalDatum(mysql.NewDecimalFromInt(int64(15), 0)))
c.Assert(val, testutil.DatumEquals, types.NewDecimalDatum(mysql.NewDecFromInt(15)))
// Second row: 21
row, err = agg.Next()
c.Assert(err, IsNil)
c.Assert(row, NotNil)
val, err = evaluator.Eval(ctx, fc)
c.Assert(err, IsNil)
c.Assert(val, testutil.DatumEquals, types.NewDecimalDatum(mysql.NewDecimalFromInt(int64(21), 0)))
c.Assert(val, testutil.DatumEquals, types.NewDecimalDatum(mysql.NewDecFromInt(21)))
// Third row: 31
row, err = agg.Next()
c.Assert(err, IsNil)
c.Assert(row, NotNil)
val, err = evaluator.Eval(ctx, fc)
c.Assert(err, IsNil)
c.Assert(val, testutil.DatumEquals, types.NewDecimalDatum(mysql.NewDecimalFromInt(int64(31), 0)))
c.Assert(val, testutil.DatumEquals, types.NewDecimalDatum(mysql.NewDecFromInt(31)))
agg.Close()
// After clear up, fc's value should be default.
val, err = evaluator.Eval(ctx, fc)
Expand Down Expand Up @@ -337,22 +337,22 @@ func (s *testAggFuncSuite) TestXAPIAvg(c *C) {
ctx := mock.NewContext()
val, err := evaluator.Eval(ctx, fc)
c.Assert(err, IsNil)
c.Assert(val, testutil.DatumEquals, types.NewDecimalDatum(mysql.NewDecimalFromInt(int64(5), 0)))
c.Assert(val, testutil.DatumEquals, types.NewDecimalDatum(mysql.NewDecFromInt(5)))
// Second row: 21
row, err = agg.Next()
c.Assert(err, IsNil)
c.Assert(row, NotNil)
val, err = evaluator.Eval(ctx, fc)
c.Assert(err, IsNil)
c.Assert(val, testutil.DatumEquals, types.NewDecimalDatum(mysql.NewDecimalFromInt(int64(21), 0)))
c.Assert(val, testutil.DatumEquals, types.NewDecimalDatum(mysql.NewDecFromInt(21)))
// Third row: 16.5000
row, err = agg.Next()
c.Assert(err, IsNil)
c.Assert(row, NotNil)
val, err = evaluator.Eval(ctx, fc)
c.Assert(err, IsNil)
d := mysql.NewDecimalFromFloat(float64(16.5))
d.SetFracDigits(4) // For div operator, default frac is 4.
d := mysql.NewDecFromFloatForTest(16.5)
c.Logf("d1 %s, d2 %s, cmp %d", d.String(), val.GetMysqlDecimal().String(), val.GetMysqlDecimal().Compare(d))
c.Assert(val, testutil.DatumEquals, types.NewDecimalDatum(d))
// Forth row: nil
row, err = agg.Next()
Expand Down
2 changes: 1 addition & 1 deletion executor/executor_xapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func (b *executorBuilder) datumToPBExpr(client kv.Client, d types.Datum) *tipb.E
val = codec.EncodeInt(nil, int64(d.GetMysqlDuration().Duration))
case types.KindMysqlDecimal:
tp = tipb.ExprType_MysqlDecimal
val = codec.EncodeDecimal(nil, d.GetMysqlDecimal())
val = codec.EncodeDecimal(nil, d)
default:
return nil
}
Expand Down
7 changes: 5 additions & 2 deletions expression/aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,11 @@ func (af *avgFunction) GetGroupResult(groupKey []byte) (d types.Datum) {
d.SetValue(t)
case types.KindMysqlDecimal:
x := ctx.Value.GetMysqlDecimal()
t := x.Div(mysql.NewDecimalFromUint(uint64(ctx.Count), 0))
d.SetMysqlDecimal(t)
y := mysql.NewDecFromInt(ctx.Count)
to := new(mysql.MyDecimal)
mysql.DecimalDiv(x, y, to, mysql.DivFracIncr)
to.Round(to, ctx.Value.Frac()+mysql.DivFracIncr)
d.SetMysqlDecimal(to)
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion meta/autoid/autoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func NewMemoryAllocator(dbID int64) Allocator {
//autoid error codes.
const codeInvalidTableID terror.ErrCode = 1

var localSchemaID int64 = math.MaxInt64
var localSchemaID = int64(math.MaxInt64)

// GenLocalSchemaID generates a local schema ID.
func GenLocalSchemaID() int64 {
Expand Down
Loading

0 comments on commit e31b281

Please sign in to comment.