Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[debezium] Fix bug in EncodeDecimal #758

Merged
merged 1 commit into from
Jun 25, 2024
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
8 changes: 5 additions & 3 deletions lib/debezium/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ func EncodeDecimal(value string, scale uint16) ([]byte, error) {
return nil, fmt.Errorf("unable to use %q as a floating-point number", value)
}

scaledValue := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(scale)), nil)
bigFloatValue.Mul(bigFloatValue, new(big.Float).SetInt(scaledValue))
if scale > 0 {
scaledValue := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(scale)), nil)
bigFloatValue.Mul(bigFloatValue, new(big.Float).SetInt(scaledValue))
}

// Extract the scaled integer value.
bigIntValue := new(big.Int)
if _, success := bigIntValue.SetString(bigFloatValue.String(), 10); !success {
if _, success := bigIntValue.SetString(bigFloatValue.Text('f', 0), 10); !success {
return nil, fmt.Errorf("unable to use %q as a floating-point number", value)
}

Expand Down
10 changes: 10 additions & 0 deletions lib/debezium/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ func TestEncodeDecimal(t *testing.T) {
value: "-65535",
scale: 0,
},
{
name: "number with a scale of 15",
value: "0.000022998904125",
scale: 15,
},
{
name: "number with a scale of 15",
value: "145.183000000000000",
scale: 15,
},
{
name: "malformed - empty string",
value: "",
Expand Down