Skip to content

Commit

Permalink
*: update to apd v2.0.0
Browse files Browse the repository at this point in the history
There was a breaking API change but it's now much safer to use.

Release note: None
  • Loading branch information
maddyblue committed Oct 17, 2018
1 parent f57009b commit b25f50c
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 42 deletions.
7 changes: 4 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions pkg/ccl/changefeedccl/avro.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,5 @@ func ratToDecimal(rat big.Rat, scale int32) apd.Decimal {
sf := denom.Div(exp, denom)
coeff := num.Mul(num, sf)
dec := apd.NewWithBigInt(coeff, -scale)
if dec.Coeff.Sign() < 0 {
dec.Coeff.Neg(&dec.Coeff)
dec.Negative = true
}
return *dec
}
2 changes: 1 addition & 1 deletion pkg/sql/pgwire/pgwirebase/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func DecodeOidDatum(id oid.Oid, code FormatCode, b []byte) (tree.Datum, error) {
if _, ok := alloc.dd.Coeff.SetString(decString, 10); !ok {
return nil, errors.Errorf("could not parse string %q as decimal", decString)
}
alloc.dd.SetExponent(-int32(Dscale))
alloc.dd.Exponent = -int32(Dscale)
}

switch alloc.pgNum.Sign {
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/sem/builtins/aggregate_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,12 +1015,12 @@ func (a *intSumAggregate) Add(ctx context.Context, datum tree.Datum, _ ...tree.D
// And overflow was detected; go to large integers, but keep the
// sum computed so far.
a.large = true
a.decSum.SetCoefficient(a.intSum)
a.decSum.SetFinite(a.intSum, 0)
}
}

if a.large {
a.tmpDec.SetCoefficient(t)
a.tmpDec.SetFinite(t, 0)
_, err := tree.ExactCtx.Add(&a.decSum, &a.decSum, &a.tmpDec)
if err != nil {
return err
Expand All @@ -1043,7 +1043,7 @@ func (a *intSumAggregate) Result() (tree.Datum, error) {
if a.large {
dd.Set(&a.decSum)
} else {
dd.SetCoefficient(a.intSum)
dd.SetFinite(a.intSum, 0)
}
return dd, nil
}
Expand Down Expand Up @@ -1218,7 +1218,7 @@ func (a *intSqrDiffAggregate) Add(ctx context.Context, datum tree.Datum, _ ...tr
return nil
}

a.tmpDec.SetCoefficient(int64(tree.MustBeDInt(datum)))
a.tmpDec.SetFinite(int64(tree.MustBeDInt(datum)), 0)
return a.agg.Add(ctx, &a.tmpDec)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/sem/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2254,7 +2254,7 @@ may increase either contention or retry errors, or both.`,
"negative."),
decimalOverload1(func(x *apd.Decimal) (tree.Datum, error) {
d := &tree.DDecimal{}
d.Decimal.SetCoefficient(int64(x.Sign()))
d.Decimal.SetFinite(int64(x.Sign()), 0)
return d, nil
}, "Determines the sign of `val`: **1** for positive; **0** for 0 values; **-1** for "+
"negative."),
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/sem/builtins/window_frame_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (w *avgWindowFunc) Compute(
return &avg, err
case *tree.DInt:
dd := tree.DDecimal{}
dd.SetCoefficient(int64(*t))
dd.SetFinite(int64(*t), 0)
var avg tree.DDecimal
count := apd.New(int64(frameSize), 0)
_, err := tree.DecimalCtx.Quo(&avg.Decimal, &dd.Decimal, count)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/sem/tree/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ func (d *DDecimal) Compare(ctx *EvalContext, other Datum) int {
case *DDecimal:
v = &t.Decimal
case *DInt:
v.SetCoefficient(int64(*t)).SetExponent(0)
v.SetFinite(int64(*t), 0)
case *DFloat:
if _, err := v.SetFloat64(float64(*t)); err != nil {
panic(err)
Expand Down
38 changes: 19 additions & 19 deletions pkg/sql/sem/tree/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
l := &left.(*DDecimal).Decimal
r := MustBeDInt(right)
dd := &DDecimal{}
dd.SetCoefficient(int64(r))
dd.SetFinite(int64(r), 0)
_, err := ExactCtx.Add(&dd.Decimal, l, &dd.Decimal)
return dd, err
},
Expand All @@ -514,7 +514,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
l := MustBeDInt(left)
r := &right.(*DDecimal).Decimal
dd := &DDecimal{}
dd.SetCoefficient(int64(l))
dd.SetFinite(int64(l), 0)
_, err := ExactCtx.Add(&dd.Decimal, &dd.Decimal, r)
return dd, err
},
Expand Down Expand Up @@ -705,7 +705,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
l := &left.(*DDecimal).Decimal
r := MustBeDInt(right)
dd := &DDecimal{}
dd.SetCoefficient(int64(r))
dd.SetFinite(int64(r), 0)
_, err := ExactCtx.Sub(&dd.Decimal, l, &dd.Decimal)
return dd, err
},
Expand All @@ -718,7 +718,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
l := MustBeDInt(left)
r := &right.(*DDecimal).Decimal
dd := &DDecimal{}
dd.SetCoefficient(int64(l))
dd.SetFinite(int64(l), 0)
_, err := ExactCtx.Sub(&dd.Decimal, &dd.Decimal, r)
return dd, err
},
Expand Down Expand Up @@ -967,7 +967,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
l := &left.(*DDecimal).Decimal
r := MustBeDInt(right)
dd := &DDecimal{}
dd.SetCoefficient(int64(r))
dd.SetFinite(int64(r), 0)
_, err := ExactCtx.Mul(&dd.Decimal, l, &dd.Decimal)
return dd, err
},
Expand All @@ -980,7 +980,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
l := MustBeDInt(left)
r := &right.(*DDecimal).Decimal
dd := &DDecimal{}
dd.SetCoefficient(int64(l))
dd.SetFinite(int64(l), 0)
_, err := ExactCtx.Mul(&dd.Decimal, &dd.Decimal, r)
return dd, err
},
Expand Down Expand Up @@ -1054,9 +1054,9 @@ var BinOps = map[BinaryOperator]binOpOverload{
ReturnType: types.Decimal,
Fn: func(ctx *EvalContext, left Datum, right Datum) (Datum, error) {
rInt := MustBeDInt(right)
div := ctx.getTmpDec().SetCoefficient(int64(rInt)).SetExponent(0)
div := ctx.getTmpDec().SetFinite(int64(rInt), 0)
dd := &DDecimal{}
dd.SetCoefficient(int64(MustBeDInt(left)))
dd.SetFinite(int64(MustBeDInt(left)), 0)
cond, err := DecimalCtx.Quo(&dd.Decimal, &dd.Decimal, div)
if cond.DivisionByZero() {
return dd, ErrDivByZero
Expand Down Expand Up @@ -1095,7 +1095,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
l := &left.(*DDecimal).Decimal
r := MustBeDInt(right)
dd := &DDecimal{}
dd.SetCoefficient(int64(r))
dd.SetFinite(int64(r), 0)
cond, err := DecimalCtx.Quo(&dd.Decimal, l, &dd.Decimal)
if cond.DivisionByZero() {
return dd, ErrDivByZero
Expand All @@ -1111,7 +1111,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
l := MustBeDInt(left)
r := &right.(*DDecimal).Decimal
dd := &DDecimal{}
dd.SetCoefficient(int64(l))
dd.SetFinite(int64(l), 0)
cond, err := DecimalCtx.Quo(&dd.Decimal, &dd.Decimal, r)
if cond.DivisionByZero() {
return dd, ErrDivByZero
Expand Down Expand Up @@ -1191,7 +1191,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
return nil, ErrDivByZero
}
dd := &DDecimal{}
dd.SetCoefficient(int64(r))
dd.SetFinite(int64(r), 0)
_, err := HighPrecisionCtx.QuoInteger(&dd.Decimal, l, &dd.Decimal)
return dd, err
},
Expand All @@ -1207,7 +1207,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
return nil, ErrDivByZero
}
dd := &DDecimal{}
dd.SetCoefficient(int64(l))
dd.SetFinite(int64(l), 0)
_, err := HighPrecisionCtx.QuoInteger(&dd.Decimal, &dd.Decimal, r)
return dd, err
},
Expand Down Expand Up @@ -1255,7 +1255,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
l := &left.(*DDecimal).Decimal
r := MustBeDInt(right)
dd := &DDecimal{}
dd.SetCoefficient(int64(r))
dd.SetFinite(int64(r), 0)
_, err := HighPrecisionCtx.Rem(&dd.Decimal, l, &dd.Decimal)
return dd, err
},
Expand All @@ -1268,7 +1268,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
l := MustBeDInt(left)
r := &right.(*DDecimal).Decimal
dd := &DDecimal{}
dd.SetCoefficient(int64(l))
dd.SetFinite(int64(l), 0)
_, err := HighPrecisionCtx.Rem(&dd.Decimal, &dd.Decimal, r)
return dd, err
},
Expand Down Expand Up @@ -1423,7 +1423,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
l := &left.(*DDecimal).Decimal
r := MustBeDInt(right)
dd := &DDecimal{}
dd.SetCoefficient(int64(r))
dd.SetFinite(int64(r), 0)
_, err := DecimalCtx.Pow(&dd.Decimal, l, &dd.Decimal)
return dd, err
},
Expand All @@ -1436,7 +1436,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
l := MustBeDInt(left)
r := &right.(*DDecimal).Decimal
dd := &DDecimal{}
dd.SetCoefficient(int64(l))
dd.SetFinite(int64(l), 0)
_, err := DecimalCtx.Pow(&dd.Decimal, &dd.Decimal, r)
return dd, err
},
Expand Down Expand Up @@ -3014,12 +3014,12 @@ func PerformCast(ctx *EvalContext, d Datum, t coltypes.CastTargetType) (Datum, e
switch v := d.(type) {
case *DBool:
if *v {
dd.SetCoefficient(1)
dd.SetFinite(1, 0)
}
case *DInt:
dd.SetCoefficient(int64(*v))
dd.SetFinite(int64(*v), 0)
case *DDate:
dd.SetCoefficient(int64(*v))
dd.SetFinite(int64(*v), 0)
case *DFloat:
_, err = dd.SetFloat64(float64(*v))
case *DDecimal:
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/sem/tree/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ func ContainsVars(evalCtx *EvalContext, expr Expr) bool {
var DecimalOne DDecimal

func init() {
DecimalOne.SetCoefficient(1)
DecimalOne.SetFinite(1, 0)
}

// ReType ensures that the given numeric expression evaluates
Expand Down
3 changes: 1 addition & 2 deletions pkg/sql/sem/tree/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ func SampleDatum(t types.T) Datum {
return &f
case types.Decimal:
d := &DDecimal{}
d.Decimal.SetExponent(6)
// int64(rng.Uint64()) to get negative numbers, too
d.Decimal.SetCoefficient(3)
d.Decimal.SetFinite(3, 6)
return d
case types.String:
return NewDString("Carl")
Expand Down
3 changes: 1 addition & 2 deletions pkg/sql/sqlbase/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ func RandDatum(rng *rand.Rand, typ ColumnType, nullOk bool) tree.Datum {
return tree.NewDFloat(tree.DFloat(rng.NormFloat64()))
case ColumnType_DECIMAL:
d := &tree.DDecimal{}
d.Decimal.SetExponent(int32(rng.Intn(40) - 20))
// int64(rng.Uint64()) to get negative numbers, too
d.Decimal.SetCoefficient(int64(rng.Uint64()))
d.Decimal.SetFinite(int64(rng.Uint64()), int32(rng.Intn(40)-20))
return d
case ColumnType_DATE:
return tree.NewDDate(tree.DDate(rng.Intn(10000)))
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,14 +875,14 @@ func fromMap(v map[string]interface{}) (JSON, error) {
// FromInt returns a JSON value given a int.
func FromInt(v int) JSON {
dec := apd.Decimal{}
dec.SetCoefficient(int64(v))
dec.SetFinite(int64(v), 0)
return jsonNumber(dec)
}

// FromInt64 returns a JSON value given a int64.
func FromInt64(v int64) JSON {
dec := apd.Decimal{}
dec.SetCoefficient(v)
dec.SetFinite(v, 0)
return jsonNumber(dec)
}

Expand Down

0 comments on commit b25f50c

Please sign in to comment.