Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 138 additions & 58 deletions pkg/container/types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2692,44 +2692,76 @@ func (x Decimal256) Format(scale int32) string {
}

func (x Decimal64) Ceil(scale1, scale2 int32, isConst bool) Decimal64 {
if scale2 >= scale1 {
return x
}
if x.Sign() {
return x.Minus().Floor(scale1, scale2, isConst).Minus()
magnitude := x.Minus()
if magnitude.Sign() {
panic(moerr.NewOutOfRangeNoCtx("decimal64", "CEIL"))
}
return magnitude.Floor(scale1, scale2, isConst).Minus()
}
if scale1 > scale2 {
k := scale1 - scale2
if k > 18 {
k = 18
k := scale1 - scale2
if k > 18 {
k = 18
}
y, _, err := x.Mod(Decimal64(1), k, 0)
if err != nil {
panic(moerr.NewOutOfRangeNoCtx("decimal64", "CEIL"))
}
if y != 0 {
x, err = x.Sub64(y)
if err != nil {
panic(moerr.NewOutOfRangeNoCtx("decimal64", "CEIL"))
}
y, _, _ := x.Mod(Decimal64(1), k, 0)
if y != 0 {
x, _ = x.Sub64(y)
x, _, _ = x.Add(Decimal64(1), k, 0)
x, _, err = x.Add(Decimal64(1), k, 0)
if err != nil {
panic(moerr.NewOutOfRangeNoCtx("decimal64", "CEIL"))
}
if isConst {
if scale2 < 0 {
k = scale1
}
x, _ = x.Scale(-k)
}
if isConst {
if scale2 < 0 {
k = scale1
}
x, err = x.Scale(-k)
if err != nil {
panic(moerr.NewOutOfRangeNoCtx("decimal64", "CEIL"))
}
}
return x
}

func (x Decimal64) Floor(scale1, scale2 int32, isConst bool) Decimal64 {
if scale2 >= scale1 {
return x
}
if x.Sign() {
return x.Minus().Ceil(scale1, scale2, isConst).Minus()
magnitude := x.Minus()
if magnitude.Sign() {
panic(moerr.NewOutOfRangeNoCtx("decimal64", "FLOOR"))
}
return magnitude.Ceil(scale1, scale2, isConst).Minus()
}
if scale1 > scale2 {
k := scale1 - scale2
if k > 18 {
k = 18
k := scale1 - scale2
if k > 18 {
k = 18
}
y, _, err := x.Mod(Decimal64(1), k, 0)
if err != nil {
panic(moerr.NewOutOfRangeNoCtx("decimal64", "FLOOR"))
}
x, err = x.Sub64(y)
if err != nil {
panic(moerr.NewOutOfRangeNoCtx("decimal64", "FLOOR"))
}
if isConst {
if scale2 < 0 {
k = scale1
}
y, _, _ := x.Mod(Decimal64(1), k, 0)
x, _ = x.Sub64(y)
if isConst {
if scale2 < 0 {
k = scale1
}
x, _ = x.Scale(-k)
x, err = x.Scale(-k)
if err != nil {
panic(moerr.NewOutOfRangeNoCtx("decimal64", "FLOOR"))
}
}
return x
Expand All @@ -2739,58 +2771,98 @@ func (x Decimal64) Round(scale1, scale2 int32, isConst bool) Decimal64 {
if scale2 >= scale1 {
return x
}
negative := x.Sign()
k := scale1 - scale2
if k > 18 {
k = 18
}
x, _ = x.Scale(-k)
var err error
x, err = x.Scale(-k)
if err != nil || (x != 0 && x.Sign() != negative) {
panic(moerr.NewOutOfRangeNoCtx("decimal64", "ROUND"))
}
if !isConst {
x, _ = x.Scale(k)
x, err = x.Scale(k)
} else if scale2 < 0 {
x, _ = x.Scale(-scale2)
x, err = x.Scale(-scale2)
}
if err != nil || (x != 0 && x.Sign() != negative) {
panic(moerr.NewOutOfRangeNoCtx("decimal64", "ROUND"))
}
return x
}

func (x Decimal128) Ceil(scale1, scale2 int32, isConst bool) Decimal128 {
if scale2 >= scale1 {
return x
}
if x.Sign() {
return x.Minus().Floor(scale1, scale2, isConst).Minus()
magnitude := x.Minus()
if magnitude.Sign() {
panic(moerr.NewOutOfRangeNoCtx("decimal128", "CEIL"))
}
return magnitude.Floor(scale1, scale2, isConst).Minus()
}
if scale1 > scale2 {
k := scale1 - scale2
if k > 38 {
k = 38
k := scale1 - scale2
if k > 38 {
k = 38
}
y, _, err := x.Mod(Decimal128{1, 0}, k, 0)
if err != nil {
panic(moerr.NewOutOfRangeNoCtx("decimal128", "CEIL"))
}
if y.B0_63 != 0 || y.B64_127 != 0 {
x, err = x.Sub128(y)
if err != nil {
panic(moerr.NewOutOfRangeNoCtx("decimal128", "CEIL"))
}
y, _, _ := x.Mod(Decimal128{1, 0}, k, 0)
if y.B0_63 != 0 || y.B64_127 != 0 {
x, _ = x.Sub128(y)
x, _, _ = x.Add(Decimal128{1, 0}, k, 0)
x, _, err = x.Add(Decimal128{1, 0}, k, 0)
if err != nil {
panic(moerr.NewOutOfRangeNoCtx("decimal128", "CEIL"))
}
if isConst {
if scale2 < 0 {
k = scale1
}
x, _ = x.Scale(-k)
}
if isConst {
if scale2 < 0 {
k = scale1
}
x, err = x.Scale(-k)
if err != nil {
panic(moerr.NewOutOfRangeNoCtx("decimal128", "CEIL"))
}
}
return x
}

func (x Decimal128) Floor(scale1, scale2 int32, isConst bool) Decimal128 {
if scale2 >= scale1 {
return x
}
if x.Sign() {
return x.Minus().Ceil(scale1, scale2, isConst).Minus()
magnitude := x.Minus()
if magnitude.Sign() {
panic(moerr.NewOutOfRangeNoCtx("decimal128", "FLOOR"))
}
return magnitude.Ceil(scale1, scale2, isConst).Minus()
}
if scale1 > scale2 {
k := scale1 - scale2
if k > 38 {
k = 38
k := scale1 - scale2
if k > 38 {
k = 38
}
y, _, err := x.Mod(Decimal128{1, 0}, k, 0)
if err != nil {
panic(moerr.NewOutOfRangeNoCtx("decimal128", "FLOOR"))
}
x, err = x.Sub128(y)
if err != nil {
panic(moerr.NewOutOfRangeNoCtx("decimal128", "FLOOR"))
}
if isConst {
if scale2 < 0 {
k = scale1
}
y, _, _ := x.Mod(Decimal128{1, 0}, k, 0)
x, _ = x.Sub128(y)
if isConst {
if scale2 < 0 {
k = scale1
}
x, _ = x.Scale(-k)
x, err = x.Scale(-k)
if err != nil {
panic(moerr.NewOutOfRangeNoCtx("decimal128", "FLOOR"))
}
}
return x
Expand All @@ -2800,15 +2872,23 @@ func (x Decimal128) Round(scale1, scale2 int32, isConst bool) Decimal128 {
if scale2 >= scale1 {
return x
}
negative := x.Sign()
k := scale1 - scale2
if k > 38 {
k = 38
}
x, _ = x.Scale(-k)
var err error
x, err = x.Scale(-k)
if err != nil || ((x.B0_63 != 0 || x.B64_127 != 0) && x.Sign() != negative) {
panic(moerr.NewOutOfRangeNoCtx("decimal128", "ROUND"))
}
if !isConst {
x, _ = x.Scale(k)
x, err = x.Scale(k)
} else if scale2 < 0 {
x, _ = x.Scale(-scale2)
x, err = x.Scale(-scale2)
}
if err != nil || ((x.B0_63 != 0 || x.B64_127 != 0) && x.Sign() != negative) {
panic(moerr.NewOutOfRangeNoCtx("decimal128", "ROUND"))
}
return x
}
Expand Down
57 changes: 57 additions & 0 deletions pkg/container/types/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,66 @@ import (
"math/rand"
"testing"

"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/stretchr/testify/require"
)

func TestDecimalPrecisionCheckedOwner(t *testing.T) {
for _, tc := range []struct {
x, ceil, floor, round int64
}{
{105, 110, 100, 110}, {-105, -100, -110, -110},
{100, 100, 100, 100}, {-100, -100, -100, -100}, {0, 0, 0, 0},
} {
for _, constantScale := range []bool{false, true} {
divisor := int64(1)
if constantScale {
divisor = 10
}
x64 := Decimal64(uint64(tc.x))
x128 := Decimal128FromInt64(tc.x)
require.Equal(t, Decimal64(uint64(tc.ceil/divisor)), x64.Ceil(2, 1, constantScale))
require.Equal(t, Decimal64(uint64(tc.floor/divisor)), x64.Floor(2, 1, constantScale))
require.Equal(t, Decimal64(uint64(tc.round/divisor)), x64.Round(2, 1, constantScale))
require.Equal(t, Decimal128FromInt64(tc.ceil/divisor), x128.Ceil(2, 1, constantScale))
require.Equal(t, Decimal128FromInt64(tc.floor/divisor), x128.Floor(2, 1, constantScale))
require.Equal(t, Decimal128FromInt64(tc.round/divisor), x128.Round(2, 1, constantScale))
}
}
for _, constantScale := range []bool{false, true} {
for _, x := range []Decimal64{Decimal64Min, Decimal64Max} {
require.Equal(t, x, x.Ceil(0, 0, constantScale))
require.Equal(t, x, x.Floor(0, 0, constantScale))
require.Equal(t, x, x.Round(0, 0, constantScale))
}
for _, x := range []Decimal128{Decimal128Min, Decimal128Max} {
require.Equal(t, x, x.Ceil(0, 0, constantScale))
require.Equal(t, x, x.Floor(0, 0, constantScale))
require.Equal(t, x, x.Round(0, 0, constantScale))
}
for _, operation := range []func(){
func() { Decimal64Min.Ceil(0, -1, constantScale) },
func() { Decimal64Min.Floor(0, -1, constantScale) },
func() { Decimal128Min.Ceil(0, -1, constantScale) },
func() { Decimal128Min.Floor(0, -1, constantScale) },
func() { Decimal64Max.Round(0, -1, constantScale) },
func() { Decimal64Max.Minus().Round(0, -1, constantScale) },
func() { Decimal128Max.Round(0, -1, constantScale) },
func() { Decimal128Max.Minus().Round(0, -1, constantScale) },
func() { Decimal64Max.Ceil(0, -1, constantScale) },
func() { Decimal64Max.Minus().Floor(0, -1, constantScale) },
func() { Decimal128Max.Ceil(0, -1, constantScale) },
func() { Decimal128Max.Minus().Floor(0, -1, constantScale) },
} {
var failure any
func() { defer func() { failure = recover() }(); operation() }()
err, ok := failure.(error)
require.True(t, ok, "quantizer must reject a nonrepresentable result")
require.True(t, moerr.IsMoErrCode(err, moerr.ErrOutOfRange))
}
}
}

func TestParseDecimalRejectsBothPrecisionEndpoints(t *testing.T) {
for _, test := range []struct {
name string
Expand Down
Loading
Loading