Skip to content

Commit b25f50c

Browse files
committed
*: update to apd v2.0.0
There was a breaking API change but it's now much safer to use. Release note: None
1 parent f57009b commit b25f50c

File tree

13 files changed

+37
-42
lines changed

13 files changed

+37
-42
lines changed

Gopkg.lock

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ccl/changefeedccl/avro.go

-4
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,5 @@ func ratToDecimal(rat big.Rat, scale int32) apd.Decimal {
400400
sf := denom.Div(exp, denom)
401401
coeff := num.Mul(num, sf)
402402
dec := apd.NewWithBigInt(coeff, -scale)
403-
if dec.Coeff.Sign() < 0 {
404-
dec.Coeff.Neg(&dec.Coeff)
405-
dec.Negative = true
406-
}
407403
return *dec
408404
}

pkg/sql/pgwire/pgwirebase/encoding.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func DecodeOidDatum(id oid.Oid, code FormatCode, b []byte) (tree.Datum, error) {
440440
if _, ok := alloc.dd.Coeff.SetString(decString, 10); !ok {
441441
return nil, errors.Errorf("could not parse string %q as decimal", decString)
442442
}
443-
alloc.dd.SetExponent(-int32(Dscale))
443+
alloc.dd.Exponent = -int32(Dscale)
444444
}
445445

446446
switch alloc.pgNum.Sign {

pkg/sql/sem/builtins/aggregate_builtins.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1015,12 +1015,12 @@ func (a *intSumAggregate) Add(ctx context.Context, datum tree.Datum, _ ...tree.D
10151015
// And overflow was detected; go to large integers, but keep the
10161016
// sum computed so far.
10171017
a.large = true
1018-
a.decSum.SetCoefficient(a.intSum)
1018+
a.decSum.SetFinite(a.intSum, 0)
10191019
}
10201020
}
10211021

10221022
if a.large {
1023-
a.tmpDec.SetCoefficient(t)
1023+
a.tmpDec.SetFinite(t, 0)
10241024
_, err := tree.ExactCtx.Add(&a.decSum, &a.decSum, &a.tmpDec)
10251025
if err != nil {
10261026
return err
@@ -1043,7 +1043,7 @@ func (a *intSumAggregate) Result() (tree.Datum, error) {
10431043
if a.large {
10441044
dd.Set(&a.decSum)
10451045
} else {
1046-
dd.SetCoefficient(a.intSum)
1046+
dd.SetFinite(a.intSum, 0)
10471047
}
10481048
return dd, nil
10491049
}
@@ -1218,7 +1218,7 @@ func (a *intSqrDiffAggregate) Add(ctx context.Context, datum tree.Datum, _ ...tr
12181218
return nil
12191219
}
12201220

1221-
a.tmpDec.SetCoefficient(int64(tree.MustBeDInt(datum)))
1221+
a.tmpDec.SetFinite(int64(tree.MustBeDInt(datum)), 0)
12221222
return a.agg.Add(ctx, &a.tmpDec)
12231223
}
12241224

pkg/sql/sem/builtins/builtins.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2254,7 +2254,7 @@ may increase either contention or retry errors, or both.`,
22542254
"negative."),
22552255
decimalOverload1(func(x *apd.Decimal) (tree.Datum, error) {
22562256
d := &tree.DDecimal{}
2257-
d.Decimal.SetCoefficient(int64(x.Sign()))
2257+
d.Decimal.SetFinite(int64(x.Sign()), 0)
22582258
return d, nil
22592259
}, "Determines the sign of `val`: **1** for positive; **0** for 0 values; **-1** for "+
22602260
"negative."),

pkg/sql/sem/builtins/window_frame_builtins.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func (w *avgWindowFunc) Compute(
255255
return &avg, err
256256
case *tree.DInt:
257257
dd := tree.DDecimal{}
258-
dd.SetCoefficient(int64(*t))
258+
dd.SetFinite(int64(*t), 0)
259259
var avg tree.DDecimal
260260
count := apd.New(int64(frameSize), 0)
261261
_, err := tree.DecimalCtx.Quo(&avg.Decimal, &dd.Decimal, count)

pkg/sql/sem/tree/datum.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ func (d *DDecimal) Compare(ctx *EvalContext, other Datum) int {
911911
case *DDecimal:
912912
v = &t.Decimal
913913
case *DInt:
914-
v.SetCoefficient(int64(*t)).SetExponent(0)
914+
v.SetFinite(int64(*t), 0)
915915
case *DFloat:
916916
if _, err := v.SetFloat64(float64(*t)); err != nil {
917917
panic(err)

pkg/sql/sem/tree/eval.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
501501
l := &left.(*DDecimal).Decimal
502502
r := MustBeDInt(right)
503503
dd := &DDecimal{}
504-
dd.SetCoefficient(int64(r))
504+
dd.SetFinite(int64(r), 0)
505505
_, err := ExactCtx.Add(&dd.Decimal, l, &dd.Decimal)
506506
return dd, err
507507
},
@@ -514,7 +514,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
514514
l := MustBeDInt(left)
515515
r := &right.(*DDecimal).Decimal
516516
dd := &DDecimal{}
517-
dd.SetCoefficient(int64(l))
517+
dd.SetFinite(int64(l), 0)
518518
_, err := ExactCtx.Add(&dd.Decimal, &dd.Decimal, r)
519519
return dd, err
520520
},
@@ -705,7 +705,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
705705
l := &left.(*DDecimal).Decimal
706706
r := MustBeDInt(right)
707707
dd := &DDecimal{}
708-
dd.SetCoefficient(int64(r))
708+
dd.SetFinite(int64(r), 0)
709709
_, err := ExactCtx.Sub(&dd.Decimal, l, &dd.Decimal)
710710
return dd, err
711711
},
@@ -718,7 +718,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
718718
l := MustBeDInt(left)
719719
r := &right.(*DDecimal).Decimal
720720
dd := &DDecimal{}
721-
dd.SetCoefficient(int64(l))
721+
dd.SetFinite(int64(l), 0)
722722
_, err := ExactCtx.Sub(&dd.Decimal, &dd.Decimal, r)
723723
return dd, err
724724
},
@@ -967,7 +967,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
967967
l := &left.(*DDecimal).Decimal
968968
r := MustBeDInt(right)
969969
dd := &DDecimal{}
970-
dd.SetCoefficient(int64(r))
970+
dd.SetFinite(int64(r), 0)
971971
_, err := ExactCtx.Mul(&dd.Decimal, l, &dd.Decimal)
972972
return dd, err
973973
},
@@ -980,7 +980,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
980980
l := MustBeDInt(left)
981981
r := &right.(*DDecimal).Decimal
982982
dd := &DDecimal{}
983-
dd.SetCoefficient(int64(l))
983+
dd.SetFinite(int64(l), 0)
984984
_, err := ExactCtx.Mul(&dd.Decimal, &dd.Decimal, r)
985985
return dd, err
986986
},
@@ -1054,9 +1054,9 @@ var BinOps = map[BinaryOperator]binOpOverload{
10541054
ReturnType: types.Decimal,
10551055
Fn: func(ctx *EvalContext, left Datum, right Datum) (Datum, error) {
10561056
rInt := MustBeDInt(right)
1057-
div := ctx.getTmpDec().SetCoefficient(int64(rInt)).SetExponent(0)
1057+
div := ctx.getTmpDec().SetFinite(int64(rInt), 0)
10581058
dd := &DDecimal{}
1059-
dd.SetCoefficient(int64(MustBeDInt(left)))
1059+
dd.SetFinite(int64(MustBeDInt(left)), 0)
10601060
cond, err := DecimalCtx.Quo(&dd.Decimal, &dd.Decimal, div)
10611061
if cond.DivisionByZero() {
10621062
return dd, ErrDivByZero
@@ -1095,7 +1095,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
10951095
l := &left.(*DDecimal).Decimal
10961096
r := MustBeDInt(right)
10971097
dd := &DDecimal{}
1098-
dd.SetCoefficient(int64(r))
1098+
dd.SetFinite(int64(r), 0)
10991099
cond, err := DecimalCtx.Quo(&dd.Decimal, l, &dd.Decimal)
11001100
if cond.DivisionByZero() {
11011101
return dd, ErrDivByZero
@@ -1111,7 +1111,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
11111111
l := MustBeDInt(left)
11121112
r := &right.(*DDecimal).Decimal
11131113
dd := &DDecimal{}
1114-
dd.SetCoefficient(int64(l))
1114+
dd.SetFinite(int64(l), 0)
11151115
cond, err := DecimalCtx.Quo(&dd.Decimal, &dd.Decimal, r)
11161116
if cond.DivisionByZero() {
11171117
return dd, ErrDivByZero
@@ -1191,7 +1191,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
11911191
return nil, ErrDivByZero
11921192
}
11931193
dd := &DDecimal{}
1194-
dd.SetCoefficient(int64(r))
1194+
dd.SetFinite(int64(r), 0)
11951195
_, err := HighPrecisionCtx.QuoInteger(&dd.Decimal, l, &dd.Decimal)
11961196
return dd, err
11971197
},
@@ -1207,7 +1207,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
12071207
return nil, ErrDivByZero
12081208
}
12091209
dd := &DDecimal{}
1210-
dd.SetCoefficient(int64(l))
1210+
dd.SetFinite(int64(l), 0)
12111211
_, err := HighPrecisionCtx.QuoInteger(&dd.Decimal, &dd.Decimal, r)
12121212
return dd, err
12131213
},
@@ -1255,7 +1255,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
12551255
l := &left.(*DDecimal).Decimal
12561256
r := MustBeDInt(right)
12571257
dd := &DDecimal{}
1258-
dd.SetCoefficient(int64(r))
1258+
dd.SetFinite(int64(r), 0)
12591259
_, err := HighPrecisionCtx.Rem(&dd.Decimal, l, &dd.Decimal)
12601260
return dd, err
12611261
},
@@ -1268,7 +1268,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
12681268
l := MustBeDInt(left)
12691269
r := &right.(*DDecimal).Decimal
12701270
dd := &DDecimal{}
1271-
dd.SetCoefficient(int64(l))
1271+
dd.SetFinite(int64(l), 0)
12721272
_, err := HighPrecisionCtx.Rem(&dd.Decimal, &dd.Decimal, r)
12731273
return dd, err
12741274
},
@@ -1423,7 +1423,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
14231423
l := &left.(*DDecimal).Decimal
14241424
r := MustBeDInt(right)
14251425
dd := &DDecimal{}
1426-
dd.SetCoefficient(int64(r))
1426+
dd.SetFinite(int64(r), 0)
14271427
_, err := DecimalCtx.Pow(&dd.Decimal, l, &dd.Decimal)
14281428
return dd, err
14291429
},
@@ -1436,7 +1436,7 @@ var BinOps = map[BinaryOperator]binOpOverload{
14361436
l := MustBeDInt(left)
14371437
r := &right.(*DDecimal).Decimal
14381438
dd := &DDecimal{}
1439-
dd.SetCoefficient(int64(l))
1439+
dd.SetFinite(int64(l), 0)
14401440
_, err := DecimalCtx.Pow(&dd.Decimal, &dd.Decimal, r)
14411441
return dd, err
14421442
},
@@ -3014,12 +3014,12 @@ func PerformCast(ctx *EvalContext, d Datum, t coltypes.CastTargetType) (Datum, e
30143014
switch v := d.(type) {
30153015
case *DBool:
30163016
if *v {
3017-
dd.SetCoefficient(1)
3017+
dd.SetFinite(1, 0)
30183018
}
30193019
case *DInt:
3020-
dd.SetCoefficient(int64(*v))
3020+
dd.SetFinite(int64(*v), 0)
30213021
case *DDate:
3022-
dd.SetCoefficient(int64(*v))
3022+
dd.SetFinite(int64(*v), 0)
30233023
case *DFloat:
30243024
_, err = dd.SetFloat64(float64(*v))
30253025
case *DDecimal:

pkg/sql/sem/tree/normalize.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ func ContainsVars(evalCtx *EvalContext, expr Expr) bool {
949949
var DecimalOne DDecimal
950950

951951
func init() {
952-
DecimalOne.SetCoefficient(1)
952+
DecimalOne.SetFinite(1, 0)
953953
}
954954

955955
// ReType ensures that the given numeric expression evaluates

pkg/sql/sem/tree/testutils.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ func SampleDatum(t types.T) Datum {
5151
return &f
5252
case types.Decimal:
5353
d := &DDecimal{}
54-
d.Decimal.SetExponent(6)
5554
// int64(rng.Uint64()) to get negative numbers, too
56-
d.Decimal.SetCoefficient(3)
55+
d.Decimal.SetFinite(3, 6)
5756
return d
5857
case types.String:
5958
return NewDString("Carl")

pkg/sql/sqlbase/testutils.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ func RandDatum(rng *rand.Rand, typ ColumnType, nullOk bool) tree.Datum {
9292
return tree.NewDFloat(tree.DFloat(rng.NormFloat64()))
9393
case ColumnType_DECIMAL:
9494
d := &tree.DDecimal{}
95-
d.Decimal.SetExponent(int32(rng.Intn(40) - 20))
9695
// int64(rng.Uint64()) to get negative numbers, too
97-
d.Decimal.SetCoefficient(int64(rng.Uint64()))
96+
d.Decimal.SetFinite(int64(rng.Uint64()), int32(rng.Intn(40)-20))
9897
return d
9998
case ColumnType_DATE:
10099
return tree.NewDDate(tree.DDate(rng.Intn(10000)))

pkg/util/json/json.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -875,14 +875,14 @@ func fromMap(v map[string]interface{}) (JSON, error) {
875875
// FromInt returns a JSON value given a int.
876876
func FromInt(v int) JSON {
877877
dec := apd.Decimal{}
878-
dec.SetCoefficient(int64(v))
878+
dec.SetFinite(int64(v), 0)
879879
return jsonNumber(dec)
880880
}
881881

882882
// FromInt64 returns a JSON value given a int64.
883883
func FromInt64(v int64) JSON {
884884
dec := apd.Decimal{}
885-
dec.SetCoefficient(v)
885+
dec.SetFinite(v, 0)
886886
return jsonNumber(dec)
887887
}
888888

0 commit comments

Comments
 (0)