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

[Decimal] Fix Encoding Problem #734

Merged
merged 5 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 8 additions & 3 deletions lib/debezium/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ import (

// EncodeDecimal is used to encode a string representation of a number to `org.apache.kafka.connect.data.Decimal`.
func EncodeDecimal(value string, scale int) ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably make scale an int64.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really? I think if we were to change it, it should be like uint16

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostgreSQL permits the scale in a numeric type declaration to be any value in the range -1000 to 1000. However, the SQL standard requires the scale to be in the range 0 to precision. Using scales outside that range may not be portable to other database systems.

Copy link
Contributor Author

@Tang8330 Tang8330 Jun 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think other databases would be similar. I can add a TODO here, if you think it's helpful

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, are you mentioning this because we then don't need to convert on L17?

big.NewInt(int64(scale))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just think that when it comes to mathematical operations we should avoid int and always try to use an integer with an explicit amount of bits (int16, int32, int64, etc). I would be fine with int16 here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sgtm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna add a TODO since it'll require refactoring our decimal type too.

scaledValue := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(scale)), nil)
bigFloatValue := new(big.Float)
if _, success := bigFloatValue.SetString(value); !success {
return nil, fmt.Errorf("unable to use '%s' as a floating-point number", value)
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))

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

data := bigIntValue.Bytes()
if bigIntValue.Sign() < 0 {
// Convert to two's complement if the number is negative
Expand Down
9 changes: 7 additions & 2 deletions lib/debezium/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,20 @@ func TestEncodeDecimal(t *testing.T) {
value: "6408.355",
scale: 3,
},
{
name: "total",
value: "1.05",
scale: 2,
},
{
name: "malformed - empty string",
value: "",
expectedErr: "unable to use '' as a floating-point number",
expectedErr: `unable to use "" as a floating-point number`,
},
{
name: "malformed - not a floating-point",
value: "abcdefg",
expectedErr: "unable to use 'abcdefg' as a floating-point number",
expectedErr: `unable to use "abcdefg" as a floating-point number`,
},
}

Expand Down