From 65ed63045c41d24295b885e858c03fd8eee8abe1 Mon Sep 17 00:00:00 2001 From: Nathan <148575555+nathan-artie@users.noreply.github.com> Date: Thu, 27 Jun 2024 14:01:59 -0700 Subject: [PATCH] [typing] Clean up `NewDecimalWithPrecision` --- lib/typing/decimal/decimal.go | 21 +++------------------ lib/typing/decimal/details.go | 7 +++++++ 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/lib/typing/decimal/decimal.go b/lib/typing/decimal/decimal.go index c220c353b..8fdc24f95 100644 --- a/lib/typing/decimal/decimal.go +++ b/lib/typing/decimal/decimal.go @@ -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, @@ -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()) } diff --git a/lib/typing/decimal/details.go b/lib/typing/decimal/details.go index 688046866..dd348c9b6 100644 --- a/lib/typing/decimal/details.go +++ b/lib/typing/decimal/details.go @@ -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