-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1bd606
commit 55c4ace
Showing
8 changed files
with
155 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package numbers | ||
|
||
import "github.com/cockroachdb/apd/v3" | ||
|
||
// MustParseDecimal parses a string to an [apd.Decimal] or panics -- used for tests. | ||
func MustParseDecimal(value string) *apd.Decimal { | ||
decimal, _, err := apd.NewFromString(value) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return decimal | ||
} | ||
|
||
// DecimalWithNewExponent takes a [apd.Decimal] and returns a new [apd.Decimal] with a the given exponent. | ||
// If the new exponent is less precise then the extra digits will be truncated. | ||
func DecimalWithNewExponent(decimal *apd.Decimal, newExponent int32) *apd.Decimal { | ||
exponentDelta := newExponent - decimal.Exponent // Exponent is negative. | ||
|
||
if exponentDelta == 0 { | ||
return new(apd.Decimal).Set(decimal) | ||
} | ||
|
||
coefficient := new(apd.BigInt).Set(&decimal.Coeff) | ||
|
||
if exponentDelta < 0 { | ||
multiplier := new(apd.BigInt).Exp(apd.NewBigInt(10), apd.NewBigInt(int64(-exponentDelta)), nil) | ||
coefficient.Mul(coefficient, multiplier) | ||
} else if exponentDelta > 0 { | ||
divisor := new(apd.BigInt).Exp(apd.NewBigInt(10), apd.NewBigInt(int64(exponentDelta)), nil) | ||
coefficient.Div(coefficient, divisor) | ||
} | ||
|
||
return &apd.Decimal{ | ||
Form: decimal.Form, | ||
Negative: decimal.Negative, | ||
Exponent: newExponent, | ||
Coeff: *coefficient, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package numbers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cockroachdb/apd/v3" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDecimalWithNewExponent(t *testing.T) { | ||
assert.Equal(t, "0", DecimalWithNewExponent(apd.New(0, 0), 0).Text('f')) | ||
assert.Equal(t, "00", DecimalWithNewExponent(apd.New(0, 1), 1).Text('f')) | ||
assert.Equal(t, "0", DecimalWithNewExponent(apd.New(0, 100), 0).Text('f')) | ||
assert.Equal(t, "00", DecimalWithNewExponent(apd.New(0, 0), 1).Text('f')) | ||
assert.Equal(t, "0.0", DecimalWithNewExponent(apd.New(0, 0), -1).Text('f')) | ||
|
||
// Same exponent: | ||
assert.Equal(t, "12.349", DecimalWithNewExponent(MustParseDecimal("12.349"), -3).Text('f')) | ||
// More precise exponent: | ||
assert.Equal(t, "12.3490", DecimalWithNewExponent(MustParseDecimal("12.349"), -4).Text('f')) | ||
assert.Equal(t, "12.34900", DecimalWithNewExponent(MustParseDecimal("12.349"), -5).Text('f')) | ||
// Lest precise exponent: | ||
// Extra digits should be truncated rather than rounded. | ||
assert.Equal(t, "12.34", DecimalWithNewExponent(MustParseDecimal("12.349"), -2).Text('f')) | ||
assert.Equal(t, "12.3", DecimalWithNewExponent(MustParseDecimal("12.349"), -1).Text('f')) | ||
assert.Equal(t, "12", DecimalWithNewExponent(MustParseDecimal("12.349"), 0).Text('f')) | ||
assert.Equal(t, "10", DecimalWithNewExponent(MustParseDecimal("12.349"), 1).Text('f')) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"strings" | ||
|
||
"github.com/artie-labs/transfer/lib/debezium" | ||
"github.com/cockroachdb/apd/v3" | ||
) | ||
|
||
func mustEncodeAndDecodeDecimal(decimal *apd.Decimal, scale int32) string { | ||
bytes := debezium.EncodeDecimalWithScale(decimal, scale) | ||
return debezium.DecodeDecimal(bytes, scale).Text('f') | ||
} | ||
|
||
func randDigit() (byte, bool) { | ||
offset := rand.Intn(10) | ||
return byte(48 + offset), offset == 0 | ||
} | ||
|
||
func generateNumberWithScale(maxDigitsBefore int, maxDigitsAfter int) (*apd.Decimal, int32) { | ||
out := strings.Builder{} | ||
|
||
var wroteNonZero bool | ||
for range rand.Intn(maxDigitsBefore + 1) { | ||
digit, isZero := randDigit() | ||
if isZero && !wroteNonZero { | ||
continue | ||
} | ||
wroteNonZero = true | ||
out.WriteByte(digit) | ||
} | ||
|
||
if !wroteNonZero { | ||
out.WriteRune('0') | ||
} | ||
|
||
scale := rand.Intn(maxDigitsAfter + 1) | ||
if scale > 0 { | ||
out.WriteRune('.') | ||
|
||
for range scale { | ||
digit, isZero := randDigit() | ||
if !isZero { | ||
wroteNonZero = true | ||
} | ||
out.WriteByte(digit) | ||
} | ||
} | ||
|
||
stringValue := out.String() | ||
|
||
if wroteNonZero && rand.Intn(2) == 1 { | ||
stringValue = "-" + stringValue | ||
} | ||
|
||
decimal, _, err := apd.NewFromString(stringValue) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return decimal, -decimal.Exponent | ||
} | ||
|
||
func main() { | ||
for i := range 1000 { | ||
fmt.Printf("Checking batch %d...\n", i) | ||
for range 1_000_000 { | ||
in, scale := generateNumberWithScale(30, 30) | ||
out := mustEncodeAndDecodeDecimal(in, scale) | ||
if in.Text('f') != out { | ||
panic(fmt.Sprintf("Failed for %s -> %s", in.Text('f'), out)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters