-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[typing] Remove pointer for DecimalDetails
precision
#771
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import ( | |
) | ||
|
||
func (d *DecimalDetails) isNumeric() bool { | ||
if d.precision == nil || *d.precision == PrecisionNotSpecified { | ||
if d.precision == PrecisionNotSpecified { | ||
return false | ||
} | ||
|
||
|
@@ -17,11 +17,11 @@ func (d *DecimalDetails) isNumeric() bool { | |
} | ||
|
||
// max(1,s) <= p <= s + 29 | ||
return numbers.BetweenEq(max(1, d.scale), d.scale+29, *d.precision) | ||
return numbers.BetweenEq(max(1, d.scale), d.scale+29, d.precision) | ||
} | ||
|
||
func (d *DecimalDetails) isBigNumeric() bool { | ||
if d.precision == nil || *d.precision == -1 { | ||
if d.precision == PrecisionNotSpecified { | ||
return false | ||
} | ||
|
||
|
@@ -31,18 +31,13 @@ func (d *DecimalDetails) isBigNumeric() bool { | |
} | ||
|
||
// max(1,s) <= p <= s + 38 | ||
return numbers.BetweenEq(max(1, d.scale), d.scale+38, *d.precision) | ||
return numbers.BetweenEq(max(1, d.scale), d.scale+38, d.precision) | ||
} | ||
|
||
func (d *DecimalDetails) toKind(maxPrecision int32, exceededKind string) string { | ||
precision := maxPrecision | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a subtle change here in that before if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's fine |
||
if d.precision != nil { | ||
precision = *d.precision | ||
} | ||
|
||
if precision > maxPrecision || precision == -1 { | ||
if d.precision > maxPrecision || d.precision == PrecisionNotSpecified { | ||
return exceededKind | ||
} | ||
|
||
return fmt.Sprintf("NUMERIC(%v, %v)", precision, d.scale) | ||
return fmt.Sprintf("NUMERIC(%v, %v)", d.precision, d.scale) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,5 +72,9 @@ func (d *Decimal) Value() any { | |
} | ||
|
||
func (d *Decimal) Details() DecimalDetails { | ||
return DecimalDetails{scale: d.Scale(), precision: d.precision} | ||
var precision int32 = PrecisionNotSpecified | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. precision := PrecisionNotSpecified |
||
if d.precision != nil { | ||
precision = *d.precision | ||
} | ||
return DecimalDetails{scale: d.Scale(), precision: precision} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,24 +2,20 @@ package decimal | |
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/artie-labs/transfer/lib/ptr" | ||
) | ||
|
||
type DecimalDetails struct { | ||
scale int32 | ||
precision *int32 | ||
precision int32 | ||
} | ||
|
||
func NewDecimalDetails(precision *int32, scale int32) *DecimalDetails { | ||
if precision != nil { | ||
if scale > *precision && *precision != -1 { | ||
// Note: -1 precision means it's not specified. | ||
func NewDecimalDetails(precision int32, scale int32) *DecimalDetails { | ||
if scale > precision && precision != -1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use constant |
||
// 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 = ptr.ToInt32(scale + 1) | ||
} | ||
// 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 &DecimalDetails{ | ||
|
@@ -32,7 +28,7 @@ func (d DecimalDetails) Scale() int32 { | |
return d.scale | ||
} | ||
|
||
func (d DecimalDetails) Precision() *int32 { | ||
func (d DecimalDetails) Precision() int32 { | ||
return d.precision | ||
} | ||
|
||
|
@@ -55,9 +51,9 @@ func (d *DecimalDetails) RedshiftKind() string { | |
// BigQueryKind - is inferring logic from: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#decimal_types | ||
func (d *DecimalDetails) BigQueryKind() string { | ||
if d.isNumeric() { | ||
return fmt.Sprintf("NUMERIC(%v, %v)", *d.precision, d.scale) | ||
return fmt.Sprintf("NUMERIC(%v, %v)", d.precision, d.scale) | ||
} else if d.isBigNumeric() { | ||
return fmt.Sprintf("BIGNUMERIC(%v, %v)", *d.precision, d.scale) | ||
return fmt.Sprintf("BIGNUMERIC(%v, %v)", d.precision, d.scale) | ||
} | ||
|
||
return "STRING" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
precision := decimal.PrecisionNotSpecified