Skip to content

Commit

Permalink
Kill scale
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie committed Jun 26, 2024
1 parent e02f40f commit aee754d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 18 deletions.
4 changes: 2 additions & 2 deletions clients/bigquery/storagewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ func TestRowToMessage(t *testing.T) {
"c_float_int32": int32(1234),
"c_float_int64": int64(1234),
"c_float_string": "4444.55555",
"c_numeric": decimal.NewDecimal(nil, 5, mustParseDecimal("3.1415926")),
"c_numeric": decimal.NewDecimal(nil, mustParseDecimal("3.14159")),
"c_string": "foo bar",
"c_string_decimal": decimal.NewDecimal(nil, 5, mustParseDecimal("1.618033")),
"c_string_decimal": decimal.NewDecimal(nil, mustParseDecimal("1.61803")),
"c_time": ext.NewExtendedTime(time.Date(0, 0, 0, 4, 5, 6, 7, time.UTC), ext.TimeKindType, ""),
"c_date": ext.NewExtendedTime(time.Date(2001, 2, 3, 0, 0, 0, 0, time.UTC), ext.DateKindType, ""),
"c_datetime": ext.NewExtendedTime(time.Date(2001, 2, 3, 4, 5, 6, 7, time.UTC), ext.DateTimeKindType, ""),
Expand Down
4 changes: 2 additions & 2 deletions lib/debezium/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (f Field) DecodeDecimal(encoded []byte) (*decimal.Decimal, error) {
return nil, fmt.Errorf("failed to get scale and/or precision: %w", err)
}
_decimal := DecodeDecimal(encoded, int32(scale))
return decimal.NewDecimal(precision, scale, _decimal), nil
return decimal.NewDecimal(precision, _decimal), nil
}

func (f Field) DecodeDebeziumVariableDecimal(value any) (*decimal.Decimal, error) {
Expand All @@ -261,5 +261,5 @@ func (f Field) DecodeDebeziumVariableDecimal(value any) (*decimal.Decimal, error
return nil, err
}
_decimal := DecodeDecimal(bytes, int32(scale))
return decimal.NewDecimal(ptr.ToInt(decimal.PrecisionNotSpecified), scale, _decimal), nil
return decimal.NewDecimal(ptr.ToInt(decimal.PrecisionNotSpecified), _decimal), nil
}
2 changes: 1 addition & 1 deletion lib/parquetutil/parse_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestParseValue(t *testing.T) {
},
{
name: "decimal",
colVal: decimal.NewDecimal(ptr.ToInt(30), 5, numbers.MustParseDecimal("5000.2232")),
colVal: decimal.NewDecimal(ptr.ToInt(30), numbers.MustParseDecimal("5000.22320")),
colKind: columns.NewColumn("", eDecimal),
expectedValue: "5000.22320",
},
Expand Down
18 changes: 6 additions & 12 deletions lib/typing/decimal/decimal.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package decimal

import (
"github.com/artie-labs/transfer/lib/numbers"
"github.com/artie-labs/transfer/lib/ptr"
"github.com/cockroachdb/apd/v3"
)

// Decimal is Artie's wrapper around [apd.Decimal] which can store large numbers w/ no precision loss.
type Decimal struct {
scale int
precision *int
value *apd.Decimal
}
Expand All @@ -21,7 +19,9 @@ const (
MaxPrecisionBeforeString = 38
)

func NewDecimal(precision *int, scale int, value *apd.Decimal) *Decimal {
func NewDecimal(precision *int, value *apd.Decimal) *Decimal {
scale := int(-value.Exponent)

if precision != nil {
if scale > *precision && *precision != -1 {
// Note: -1 precision means it's not specified.
Expand All @@ -33,14 +33,13 @@ func NewDecimal(precision *int, scale int, value *apd.Decimal) *Decimal {
}

return &Decimal{
scale: scale,
precision: precision,
value: value,
}
}

func (d *Decimal) Scale() int {
return d.scale
return int(-d.value.Exponent)
}

func (d *Decimal) Precision() *int {
Expand All @@ -51,14 +50,9 @@ func (d *Decimal) Precision() *int {
// This is particularly useful for Snowflake because we're writing all the values as STRINGS into TSV format.
// This function guarantees backwards compatibility.
func (d *Decimal) String() string {
targetExponent := -int32(d.scale)
value := d.value
if value.Exponent != targetExponent {
value = numbers.DecimalWithNewExponent(value, targetExponent)
}
return value.Text('f')
return d.value.Text('f')
}

func (d *Decimal) Details() DecimalDetails {
return DecimalDetails{scale: d.scale, precision: d.precision}
return DecimalDetails{scale: d.Scale(), precision: d.precision}
}
2 changes: 1 addition & 1 deletion lib/typing/values/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestToString(t *testing.T) {
// Decimals
_decimal, _, err := apd.NewFromString("585692791691858.25")
assert.NoError(t, err)
value := decimal.NewDecimal(ptr.ToInt(38), 2, _decimal)
value := decimal.NewDecimal(ptr.ToInt(38), _decimal)
val, err = ToString(value, columns.Column{KindDetails: typing.EDecimal}, nil)
assert.NoError(t, err)
assert.Equal(t, "585692791691858.25", val)
Expand Down

0 comments on commit aee754d

Please sign in to comment.