-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Variant] impl [Try]From for VariantDecimalXX types #7809
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
Conversation
|
Attn @alamb and @friendlymatthew |
parquet-variant/src/variant.rs
Outdated
| Variant::Int64(i) => Some(i), | ||
| Variant::Decimal4(d) if d.scale == 0 => Some(d.integer.into()), | ||
| Variant::Decimal8(d) if d.scale == 0 => Some(d.integer), | ||
| Variant::Decimal16(d) if d.scale == 0 => d.integer.try_into().ok(), |
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.
One minor comment is that the ok() here discards an ArowError which has an owned string -- so in othr words, this call is pretty expensive. It might be worth a method that returns a Option rather than Err for testing conversion
The same general rule applies below
One potential solution woudl be to add methods like
struct VariantDecimal8 {
// returns Some if this variant can be represented as Decimal4, None otherwise
fn as_decimal_4(&self) -> Option<VariantDecimal4> { ... }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.
This is always tricky -- if we go for Option instead, and convert None to Err, then the resulting error message contains little or no information about why the conversion failed. And I don't think we want to duplicate code, to do both?
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.
Yeah, let's go with this for now and we can optimize if it shows up in traces
parquet-variant/src/variant.rs
Outdated
| Variant::Int64(i) => Some(i), | ||
| Variant::Decimal4(d) if d.scale == 0 => Some(d.integer.into()), | ||
| Variant::Decimal8(d) if d.scale == 0 => Some(d.integer), | ||
| Variant::Decimal16(d) if d.scale == 0 => d.integer.try_into().ok(), |
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.
Yeah, let's go with this for now and we can optimize if it shows up in traces
|
I merged this PR up to resolve a conflict and hope to merge it shortly |
|
@scovich I noticed this PR is marked as Draft. Is it ok to mark ready for review and merge? |
Go for it! |
|
Thanks again @scovich |
Which issue does this PR close?
Rationale for this change
The existing
Variant::as_decimal_XXmethods were actually incorrect, failing to validate scale when converting from a wider decimal to a narrower one. Fix it, while also improving ergonomics of the decimal code to reduce the chances of future issues of this type.What changes are included in this PR?
Add proper [Try]From for converting to decimal types from other decimals or their underlying integer type.
Add missing conversions in the
Variant::as_int_xxandVariant::as_decimal_xxhelpers.Are these changes tested?
TODO - need more tests for the new conversions
Are there any user-facing changes?
The
Variant:as_decimal_xxmethods have been renamed and now return actual decimal types.New conversions available.