Skip to content

Commit

Permalink
fix decimal panic(1.2.3-shulian) (#19716)
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyan1 authored Oct 31, 2024
1 parent 6d6d7b8 commit ef81710
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
18 changes: 18 additions & 0 deletions pkg/container/types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,9 @@ func Decimal256ToFloat64(x Decimal256, scale int32) float64 {
}

func Parse64(x string) (y Decimal64, scale int32, err error) {
if x == "" {
return 0, 0, moerr.NewInvalidInputNoCtx("can't cast empty string to Decimal64")
}
y = Decimal64(0)
z := Decimal64(0)
scale = -1
Expand Down Expand Up @@ -1617,6 +1620,9 @@ func Parse64(x string) (y Decimal64, scale int32, err error) {
}

func ParseDecimal64(x string, width, scale int32) (y Decimal64, err error) {
if x == "" {
return 0, moerr.NewInvalidInputNoCtx("can't cast empty string to Decimal64")
}
if width > 18 {
width = 18
}
Expand Down Expand Up @@ -1650,6 +1656,9 @@ func ParseDecimal64(x string, width, scale int32) (y Decimal64, err error) {
}

func ParseDecimal64FromByte(x string, width, scale int32) (y Decimal64, err error) {
if x == "" {
return 0, moerr.NewInvalidInputNoCtx("can't cast empty string to Decimal64")
}
y = 0
err = nil
n := len(x)
Expand All @@ -1667,6 +1676,9 @@ func ParseDecimal64FromByte(x string, width, scale int32) (y Decimal64, err erro
}

func Parse128(x string) (y Decimal128, scale int32, err error) {
if x == "" {
return Decimal128{0, 0}, 0, moerr.NewInvalidInputNoCtx("can't cast empty string to Decimal128")
}
var z Decimal128
width := 0
t := false
Expand Down Expand Up @@ -1797,6 +1809,9 @@ func Parse128(x string) (y Decimal128, scale int32, err error) {
}

func ParseDecimal128(x string, width, scale int32) (y Decimal128, err error) {
if x == "" {
return Decimal128{0, 0}, moerr.NewInvalidInputNoCtx("can't cast empty string to Decimal128")
}
if width > 38 {
width = 38
}
Expand Down Expand Up @@ -1836,6 +1851,9 @@ func ParseDecimal128(x string, width, scale int32) (y Decimal128, err error) {
}

func ParseDecimal128FromByte(x string, width, scale int32) (y Decimal128, err error) {
if x == "" {
return Decimal128{0, 0}, moerr.NewInvalidInputNoCtx("can't cast empty string to Decimal128")
}
y = Decimal128{0, 0}
err = nil
n := len(x)
Expand Down
12 changes: 9 additions & 3 deletions pkg/container/types/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,16 @@ func TestParseFormat(t *testing.T) {
panic("wrong")
}
}
func BenchmarkFor(b *testing.B) {
for i := 0; i < b.N; i++ {
}

func TestParseEmpty(t *testing.T) {
Parse64("")
ParseDecimal64("", 0, 0)
ParseDecimal64FromByte("", 0, 0)
Parse128("")
ParseDecimal128("", 0, 0)
ParseDecimal128FromByte("", 0, 0)
}

func BenchmarkFloatAdd(b *testing.B) {
x := float64(rand.Int())
y := float64(rand.Int())
Expand Down

0 comments on commit ef81710

Please sign in to comment.