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

Move encodeDecimalWithScale from transfer #476

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 40 additions & 1 deletion lib/debezium/converters/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,50 @@ package converters

import (
"fmt"

"github.com/artie-labs/transfer/lib/debezium"
"github.com/artie-labs/transfer/lib/typing"
"github.com/cockroachdb/apd/v3"
)

// decimalWithNewExponent takes a [*apd.Decimal] and returns a new [*apd.Decimal] with a the given exponent.
Copy link
Collaborator Author

@nathan-artie nathan-artie Aug 23, 2024

Choose a reason for hiding this comment

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

No changes to these two functions.

// 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,
}
}

// encodeDecimalWithScale is used to encode a [*apd.Decimal] to `org.apache.kafka.connect.data.Decimal`
// using a specific scale.
func encodeDecimalWithScale(decimal *apd.Decimal, scale int32) []byte {
targetExponent := -scale // Negate scale since [Decimal.Exponent] is negative.
if decimal.Exponent != targetExponent {
decimal = decimalWithNewExponent(decimal, targetExponent)
}
bytes, _ := debezium.EncodeDecimal(decimal)
return bytes
}

type decimalConverter struct {
scale uint16
precision *int
Expand Down Expand Up @@ -44,7 +83,7 @@ func (d decimalConverter) Convert(value any) (any, error) {
return nil, fmt.Errorf(`unable to use %q as a decimal: %w`, stringValue, err)
}

return debezium.EncodeDecimalWithScale(decimal, int32(d.scale)), nil
return encodeDecimalWithScale(decimal, int32(d.scale)), nil
}

type VariableNumericConverter struct{}
Expand Down
2 changes: 1 addition & 1 deletion lib/debezium/converters/money.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ func (m MoneyConverter) Convert(value any) (any, error) {
return nil, fmt.Errorf(`unable to use %q as a money value: %w`, valString, err)
}

return debezium.EncodeDecimalWithScale(decimal, int32(m.Scale())), nil
return encodeDecimalWithScale(decimal, int32(m.Scale())), nil
}
Loading