diff options
author | yenlin.lai <yenlin.lai@cobinhood.com> | 2019-03-29 10:51:32 +0800 |
---|---|---|
committer | Jhih-Ming Huang <jm.huang@cobinhood.com> | 2019-05-06 10:44:04 +0800 |
commit | c6f123b6f7e343d6cf32521b6aa2a9543a6948dd (patch) | |
tree | c45caf2d8f0f5e9f5b69a7c827ab2f7d08883527 | |
parent | cdd653fd663f6550536e10f8b8b701327b2e88c7 (diff) | |
download | dexon-c6f123b6f7e343d6cf32521b6aa2a9543a6948dd.tar dexon-c6f123b6f7e343d6cf32521b6aa2a9543a6948dd.tar.gz dexon-c6f123b6f7e343d6cf32521b6aa2a9543a6948dd.tar.bz2 dexon-c6f123b6f7e343d6cf32521b6aa2a9543a6948dd.tar.lz dexon-c6f123b6f7e343d6cf32521b6aa2a9543a6948dd.tar.xz dexon-c6f123b6f7e343d6cf32521b6aa2a9543a6948dd.tar.zst dexon-c6f123b6f7e343d6cf32521b6aa2a9543a6948dd.zip |
vm: sqlvm: refine after updating dexon-foundation/decimal
Refine behaviors:
1. Check decimal.NewFromString by type instead of string compare.
2. Use Rescale in DecimalEncode.
-rw-r--r-- | core/vm/sqlvm/ast/types.go | 13 | ||||
-rw-r--r-- | core/vm/sqlvm/ast/types_test.go | 2 | ||||
-rw-r--r-- | core/vm/sqlvm/common/decimal/decimal.go | 2 | ||||
-rw-r--r-- | core/vm/sqlvm/parser/internal/helpers.go | 9 |
4 files changed, 7 insertions, 19 deletions
diff --git a/core/vm/sqlvm/ast/types.go b/core/vm/sqlvm/ast/types.go index 53f3217a0..975fd81ba 100644 --- a/core/vm/sqlvm/ast/types.go +++ b/core/vm/sqlvm/ast/types.go @@ -15,7 +15,6 @@ import ( var ( bigIntOne = big.NewInt(1) - bigIntTen = big.NewInt(10) ) // BoolValue represents a boolean value used by SQL three-valued logic. @@ -304,15 +303,9 @@ func (dt DataType) GetMinMax() (decimal.Decimal, decimal.Decimal, bool) { return pair.Min, pair.Max, ok } -func decimalToBig(d decimal.Decimal) (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) - } - return +func decimalToBig(d decimal.Decimal) *big.Int { + d = d.Rescale(0) + return d.Coefficient() } // Don't handle overflow here. diff --git a/core/vm/sqlvm/ast/types_test.go b/core/vm/sqlvm/ast/types_test.go index accbbe741..0973ba92a 100644 --- a/core/vm/sqlvm/ast/types_test.go +++ b/core/vm/sqlvm/ast/types_test.go @@ -156,7 +156,7 @@ func (s *TypesTestSuite) TestEncodeAndDecodeDecimal() { } func (s *TypesTestSuite) TestDataTypeGetMinMax() { - decAddressMax := decimal.New(2, 0).Pow(decimal.New(common.AddressLength*8, 0)).Sub(dec.One) + decAddressMax := decimal.Two.Pow(decimal.New(common.AddressLength*8, 0)).Sub(decimal.One) testcases := []struct { Name string In DataType diff --git a/core/vm/sqlvm/common/decimal/decimal.go b/core/vm/sqlvm/common/decimal/decimal.go index b02a8ed23..445256cf5 100644 --- a/core/vm/sqlvm/common/decimal/decimal.go +++ b/core/vm/sqlvm/common/decimal/decimal.go @@ -4,8 +4,6 @@ import "github.com/dexon-foundation/decimal" // Shared vars. var ( - One = decimal.New(1, 0) - False = decimal.New(0, 0) True = decimal.New(1, 0) ) diff --git a/core/vm/sqlvm/parser/internal/helpers.go b/core/vm/sqlvm/parser/internal/helpers.go index 386a4be7e..91b73b8ad 100644 --- a/core/vm/sqlvm/parser/internal/helpers.go +++ b/core/vm/sqlvm/parser/internal/helpers.go @@ -5,7 +5,6 @@ import ( "encoding/hex" "fmt" "strconv" - "strings" "unicode/utf8" "github.com/dexon-foundation/decimal" @@ -106,12 +105,10 @@ func convertDecimalError(err error) errors.ErrorCode { if err == nil { return errors.ErrorCodeNil } - errStr := err.Error() - if strings.HasSuffix(errStr, "decimal: fractional part too long") { + switch err.(type) { + case *decimal.ErrorExponentLimit: return errors.ErrorCodeFractionalPartTooLong - } else if strings.HasSuffix(errStr, "decimal: exponent is not numeric") { - return errors.ErrorCodeInvalidNumberSyntax - } else if strings.HasSuffix(errStr, "decimal: too many .s") { + case *decimal.ErrorInvalidFormat: return errors.ErrorCodeInvalidNumberSyntax } panic(fmt.Sprintf("unknown decimal error: %v", err)) |