diff options
-rw-r--r-- | core/vm/sqlvm/ast/types.go | 10 | ||||
-rw-r--r-- | core/vm/sqlvm/ast/types_test.go | 4 |
2 files changed, 10 insertions, 4 deletions
diff --git a/core/vm/sqlvm/ast/types.go b/core/vm/sqlvm/ast/types.go index 1928d2338..c006086e9 100644 --- a/core/vm/sqlvm/ast/types.go +++ b/core/vm/sqlvm/ast/types.go @@ -163,8 +163,14 @@ func decimalEncode(size int, d decimal.Decimal) []byte { return ret } - exp := new(big.Int).Exp(bigIntTen, big.NewInt(int64(d.Exponent())), nil) - b := new(big.Int).Mul(d.Coefficient(), exp) + var b *big.Int + if exponent := int64(d.Exponent()); exponent >= 0 { + exp := new(big.Int).Exp(bigIntTen, big.NewInt(exponent), nil) + b = new(big.Int).Mul(d.Coefficient(), exp) + } else { + exp := new(big.Int).Exp(bigIntTen, big.NewInt(-exponent), nil) + b = new(big.Int).Div(d.Coefficient(), exp) + } if s > 0 { bs := b.Bytes() diff --git a/core/vm/sqlvm/ast/types_test.go b/core/vm/sqlvm/ast/types_test.go index 17378d2bd..fe125ba2a 100644 --- a/core/vm/sqlvm/ast/types_test.go +++ b/core/vm/sqlvm/ast/types_test.go @@ -93,9 +93,9 @@ func (s *TypesTestSuite) TestDecodeError() { } func (s *TypesTestSuite) TestEncodeAndDecodeDecimal() { - pos := decimal.New(15, 0) + pos := decimal.New(15, 1) zero := decimal.Zero - neg := decimal.New(-15, 0) + neg := decimal.New(-150, -1) s.requireEncodeAndDecodeDecimalNoError( ComposeDataType(DataTypeMajorInt, 2), |