diff --git a/lib/typing/decimal/base.go b/lib/typing/decimal/base.go index c2f7402fe..a6ac4f3eb 100644 --- a/lib/typing/decimal/base.go +++ b/lib/typing/decimal/base.go @@ -6,7 +6,7 @@ import ( "github.com/artie-labs/transfer/lib/numbers" ) -func (d *DecimalDetails) isNumeric() bool { +func (d Details) isNumeric() bool { if d.precision == PrecisionNotSpecified { return false } @@ -20,7 +20,7 @@ func (d *DecimalDetails) isNumeric() bool { return numbers.BetweenEq(max(1, d.scale), d.scale+29, d.precision) } -func (d *DecimalDetails) isBigNumeric() bool { +func (d Details) isBigNumeric() bool { if d.precision == PrecisionNotSpecified { return false } @@ -34,7 +34,7 @@ func (d *DecimalDetails) isBigNumeric() bool { return numbers.BetweenEq(max(1, d.scale), d.scale+38, d.precision) } -func (d *DecimalDetails) toKind(maxPrecision int32, exceededKind string) string { +func (d Details) toKind(maxPrecision int32, exceededKind string) string { if d.precision > maxPrecision || d.precision == PrecisionNotSpecified { return exceededKind } diff --git a/lib/typing/decimal/decimal.go b/lib/typing/decimal/decimal.go index 8e3af1b5b..c220c353b 100644 --- a/lib/typing/decimal/decimal.go +++ b/lib/typing/decimal/decimal.go @@ -57,6 +57,6 @@ func (d *Decimal) String() string { return d.value.Text('f') } -func (d *Decimal) Details() DecimalDetails { - return DecimalDetails{scale: d.Scale(), precision: d.precision} +func (d *Decimal) Details() Details { + return Details{scale: d.Scale(), precision: d.precision} } diff --git a/lib/typing/decimal/decimal_test.go b/lib/typing/decimal/decimal_test.go index 4aba71760..2fc86aa60 100644 --- a/lib/typing/decimal/decimal_test.go +++ b/lib/typing/decimal/decimal_test.go @@ -15,13 +15,13 @@ func TestNewDecimal(t *testing.T) { func TestNewDecimalWithPrecision(t *testing.T) { // Precision = -1 (PrecisionNotSpecified): - assert.Equal(t, DecimalDetails{scale: 2, precision: -1}, NewDecimalWithPrecision(numbers.MustParseDecimal("12.34"), PrecisionNotSpecified).Details()) + assert.Equal(t, Details{scale: 2, precision: -1}, NewDecimalWithPrecision(numbers.MustParseDecimal("12.34"), PrecisionNotSpecified).Details()) // Precision = scale: - assert.Equal(t, DecimalDetails{scale: 2, precision: 2}, NewDecimalWithPrecision(numbers.MustParseDecimal("12.34"), 2).Details()) + assert.Equal(t, Details{scale: 2, precision: 2}, NewDecimalWithPrecision(numbers.MustParseDecimal("12.34"), 2).Details()) // Precision < scale: - assert.Equal(t, DecimalDetails{scale: 2, precision: 3}, NewDecimalWithPrecision(numbers.MustParseDecimal("12.34"), 1).Details()) + assert.Equal(t, Details{scale: 2, precision: 3}, NewDecimalWithPrecision(numbers.MustParseDecimal("12.34"), 1).Details()) // Precision > scale: - assert.Equal(t, DecimalDetails{scale: 2, precision: 4}, NewDecimalWithPrecision(numbers.MustParseDecimal("12.34"), 4).Details()) + assert.Equal(t, Details{scale: 2, precision: 4}, NewDecimalWithPrecision(numbers.MustParseDecimal("12.34"), 4).Details()) } func TestDecimal_Scale(t *testing.T) { @@ -36,16 +36,16 @@ func TestDecimal_Scale(t *testing.T) { func TestDecimal_Details(t *testing.T) { // -1 precision (PrecisionNotSpecified): - assert.Equal(t, DecimalDetails{scale: 0, precision: -1}, NewDecimal(numbers.MustParseDecimal("0")).Details()) - assert.Equal(t, DecimalDetails{scale: 0, precision: -1}, NewDecimal(numbers.MustParseDecimal("12345")).Details()) - assert.Equal(t, DecimalDetails{scale: 0, precision: -1}, NewDecimal(numbers.MustParseDecimal("-12")).Details()) - assert.Equal(t, DecimalDetails{scale: 2, precision: -1}, NewDecimal(numbers.MustParseDecimal("12345.12")).Details()) - assert.Equal(t, DecimalDetails{scale: 3, precision: -1}, NewDecimal(numbers.MustParseDecimal("-12345.123")).Details()) + assert.Equal(t, Details{scale: 0, precision: -1}, NewDecimal(numbers.MustParseDecimal("0")).Details()) + assert.Equal(t, Details{scale: 0, precision: -1}, NewDecimal(numbers.MustParseDecimal("12345")).Details()) + assert.Equal(t, Details{scale: 0, precision: -1}, NewDecimal(numbers.MustParseDecimal("-12")).Details()) + assert.Equal(t, Details{scale: 2, precision: -1}, NewDecimal(numbers.MustParseDecimal("12345.12")).Details()) + assert.Equal(t, Details{scale: 3, precision: -1}, NewDecimal(numbers.MustParseDecimal("-12345.123")).Details()) // 10 precision: - assert.Equal(t, DecimalDetails{scale: 0, precision: 10}, NewDecimalWithPrecision(numbers.MustParseDecimal("0"), 10).Details()) - assert.Equal(t, DecimalDetails{scale: 0, precision: 10}, NewDecimalWithPrecision(numbers.MustParseDecimal("12345"), 10).Details()) - assert.Equal(t, DecimalDetails{scale: 0, precision: 10}, NewDecimalWithPrecision(numbers.MustParseDecimal("-12"), 10).Details()) - assert.Equal(t, DecimalDetails{scale: 2, precision: 10}, NewDecimalWithPrecision(numbers.MustParseDecimal("12345.12"), 10).Details()) - assert.Equal(t, DecimalDetails{scale: 3, precision: 10}, NewDecimalWithPrecision(numbers.MustParseDecimal("-12345.123"), 10).Details()) + assert.Equal(t, Details{scale: 0, precision: 10}, NewDecimalWithPrecision(numbers.MustParseDecimal("0"), 10).Details()) + assert.Equal(t, Details{scale: 0, precision: 10}, NewDecimalWithPrecision(numbers.MustParseDecimal("12345"), 10).Details()) + assert.Equal(t, Details{scale: 0, precision: 10}, NewDecimalWithPrecision(numbers.MustParseDecimal("-12"), 10).Details()) + assert.Equal(t, Details{scale: 2, precision: 10}, NewDecimalWithPrecision(numbers.MustParseDecimal("12345.12"), 10).Details()) + assert.Equal(t, Details{scale: 3, precision: 10}, NewDecimalWithPrecision(numbers.MustParseDecimal("-12345.123"), 10).Details()) } diff --git a/lib/typing/decimal/details.go b/lib/typing/decimal/details.go index 7c7a0fe2b..feb477ee8 100644 --- a/lib/typing/decimal/details.go +++ b/lib/typing/decimal/details.go @@ -4,12 +4,12 @@ import ( "fmt" ) -type DecimalDetails struct { +type Details struct { scale int32 precision int32 } -func NewDecimalDetails(precision int32, scale int32) *DecimalDetails { +func NewDecimalDetails(precision int32, scale int32) *Details { if scale > precision && precision != PrecisionNotSpecified { // Note: -1 precision means it's not specified. @@ -18,38 +18,38 @@ func NewDecimalDetails(precision int32, scale int32) *DecimalDetails { precision = scale + 1 } - return &DecimalDetails{ + return &Details{ scale: scale, precision: precision, } } -func (d DecimalDetails) Scale() int32 { +func (d Details) Scale() int32 { return d.scale } -func (d DecimalDetails) Precision() int32 { +func (d Details) Precision() int32 { return d.precision } // SnowflakeKind - is used to determine whether a NUMERIC data type should be a STRING or NUMERIC(p, s). -func (d *DecimalDetails) SnowflakeKind() string { +func (d Details) SnowflakeKind() string { return d.toKind(MaxPrecisionBeforeString, "STRING") } // MsSQLKind - Has the same limitation as Redshift // Spec: https://learn.microsoft.com/en-us/sql/t-sql/data-types/decimal-and-numeric-transact-sql?view=sql-server-ver16#arguments -func (d *DecimalDetails) MsSQLKind() string { +func (d Details) MsSQLKind() string { return d.toKind(MaxPrecisionBeforeString, "TEXT") } // RedshiftKind - is used to determine whether a NUMERIC data type should be a TEXT or NUMERIC(p, s). -func (d *DecimalDetails) RedshiftKind() string { +func (d Details) RedshiftKind() string { return d.toKind(MaxPrecisionBeforeString, "TEXT") } // BigQueryKind - is inferring logic from: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#decimal_types -func (d *DecimalDetails) BigQueryKind() string { +func (d Details) BigQueryKind() string { if d.isNumeric() { return fmt.Sprintf("NUMERIC(%v, %v)", d.precision, d.scale) } else if d.isBigNumeric() { diff --git a/lib/typing/typing.go b/lib/typing/typing.go index 1b9556188..ffbc9e398 100644 --- a/lib/typing/typing.go +++ b/lib/typing/typing.go @@ -20,7 +20,7 @@ type Settings struct { type KindDetails struct { Kind string ExtendedTimeDetails *ext.NestedKind - ExtendedDecimalDetails *decimal.DecimalDetails + ExtendedDecimalDetails *decimal.Details // Optional kind details metadata OptionalStringPrecision *int