aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryenlinlai <38415072+yenlinlai@users.noreply.github.com>2019-03-18 16:45:03 +0800
committerJhih-Ming Huang <jm.huang@cobinhood.com>2019-04-11 10:39:59 +0800
commit806cb1c40c7596935c47d34eacc795d2a38e4b2f (patch)
tree8dff678d49d38d7844c5e73dc7636c389e8cf562
parent23a8d53d37e78cb69949963c957fddf6683ad3a7 (diff)
downloaddexon-806cb1c40c7596935c47d34eacc795d2a38e4b2f.tar
dexon-806cb1c40c7596935c47d34eacc795d2a38e4b2f.tar.gz
dexon-806cb1c40c7596935c47d34eacc795d2a38e4b2f.tar.bz2
dexon-806cb1c40c7596935c47d34eacc795d2a38e4b2f.tar.lz
dexon-806cb1c40c7596935c47d34eacc795d2a38e4b2f.tar.xz
dexon-806cb1c40c7596935c47d34eacc795d2a38e4b2f.tar.zst
dexon-806cb1c40c7596935c47d34eacc795d2a38e4b2f.zip
core: vm: sqlvm: types: fix encode bug when exponent is negative (#270)
It is possible that the number to encode is produced by arithmetic operations and has negative exponent even when it is an integer. Properly handle this case and modify test cases to check it.
-rw-r--r--core/vm/sqlvm/ast/types.go10
-rw-r--r--core/vm/sqlvm/ast/types_test.go4
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),