Skip to content

Commit

Permalink
Decimal clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Jun 27, 2024
1 parent 39bb051 commit a4e583d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
6 changes: 3 additions & 3 deletions lib/typing/decimal/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions lib/typing/decimal/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
28 changes: 14 additions & 14 deletions lib/typing/decimal/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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())
}
18 changes: 9 additions & 9 deletions lib/typing/decimal/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion lib/typing/typing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a4e583d

Please sign in to comment.