Skip to content

Commit

Permalink
[typing] Clean up NewDecimalWithPrecision
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie committed Jun 27, 2024
1 parent 0b260fe commit 65ed630
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
21 changes: 3 additions & 18 deletions lib/typing/decimal/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,15 @@ import (
"github.com/cockroachdb/apd/v3"
)

const PrecisionNotSpecified int32 = -1

// Decimal is Artie's wrapper around [*apd.Decimal] which can store large numbers w/ no precision loss.
type Decimal struct {
precision int32
value *apd.Decimal
}

const (
DefaultScale int32 = 5
PrecisionNotSpecified int32 = -1
// MaxPrecisionBeforeString - if the precision is greater than 38, we'll cast it as a string.
// This is because Snowflake and BigQuery both do not have NUMERIC data types that go beyond 38.
MaxPrecisionBeforeString int32 = 38
)

func NewDecimalWithPrecision(value *apd.Decimal, precision int32) *Decimal {
scale := -value.Exponent
if scale > precision && precision != PrecisionNotSpecified {
// Note: -1 precision means it's not specified.

// This is typically not possible, but Postgres has a design flaw that allows you to do things like: NUMERIC(5, 6) which actually equates to NUMERIC(7, 6)
// We are setting precision to be scale + 1 to account for the leading zero for decimal numbers.
precision = scale + 1
}

return &Decimal{
precision: precision,
value: value,
Expand Down Expand Up @@ -58,5 +43,5 @@ func (d *Decimal) String() string {
}

func (d *Decimal) Details() Details {
return Details{scale: d.Scale(), precision: d.precision}
return NewDetails(d.precision, d.Scale())
}
7 changes: 7 additions & 0 deletions lib/typing/decimal/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import (
"fmt"
)

const (
DefaultScale int32 = 5
// MaxPrecisionBeforeString - if the precision is greater than 38, we'll cast it as a string.
// This is because Snowflake and BigQuery both do not have NUMERIC data types that go beyond 38.
MaxPrecisionBeforeString int32 = 38
)

type Details struct {
scale int32
precision int32
Expand Down

0 comments on commit 65ed630

Please sign in to comment.