-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[VARIANT] Add support for the json_to_variant API #7783
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
87b438d
add json_to_variant
harshmotw-db a946ac6
comment fix
harshmotw-db bca3b81
Added another sample buffer managger
harshmotw-db 339e880
minor refactoring
harshmotw-db fe798c3
Merge branch 'main' of https://github.com/harshmotw-db/arrow-rs into …
harshmotw-db 882d3a7
Merge branch 'main' of https://github.com/harshmotw-db/arrow-rs into …
harshmotw-db 3c18fdf
incorporated new changes
harshmotw-db 67a83fe
minor changes
harshmotw-db c9aa519
Merge branch 'main' of https://github.com/harshmotw-db/arrow-rs into …
harshmotw-db dede88d
test fix based on recent commit
harshmotw-db fa3befc
constant fix
harshmotw-db 38bac59
fix
harshmotw-db cd530ee
addressed comments
harshmotw-db 57b3eb0
fix
harshmotw-db 71b7d6f
fixed VariantBufferManager
harshmotw-db 031c916
deduped a bit of code
harshmotw-db d4fc876
deduplicated code
harshmotw-db c41af4e
moved serde code out of variant.rs
harshmotw-db ecaf557
incorporated Ryan's latest comments
harshmotw-db 4abc598
add more object tests
harshmotw-db 0842ef8
fix
harshmotw-db 94531af
doc fix
harshmotw-db 28d0012
Merge branch 'main' into harsh-motwani_data/from_json
harshmotw-db e2788f5
Removed dependency on VariantBufferManager and leave that to later
harshmotw-db 3178449
Merge branch 'harsh-motwani_data/from_json' of https://github.com/har…
harshmotw-db 0455685
fix
harshmotw-db cc0b66e
fix
harshmotw-db d2a7516
resolved test comment
harshmotw-db a29b5c3
fixed more comments
harshmotw-db 7f23cf5
fix decimal to string
harshmotw-db 50f4b25
fmt and clippy
harshmotw-db 560e430
fix
harshmotw-db 3249d93
Merge remote-tracking branch 'apache/main' into harsh-motwani_data/fr…
alamb 07d5688
clippy
alamb af937ac
Split tests into separate functions
alamb 388f188
partially removed decimal dependency
harshmotw-db 7407776
merge
harshmotw-db 3b42d91
removed decimal type from variant json parsing
harshmotw-db 43d6ea5
comment fix
harshmotw-db e9deda9
refined lifetimes
harshmotw-db eb11890
Fix clippy, remove unused dependency
alamb 3531540
Merge remote-tracking branch 'apache/main' into harsh-motwani_data/fr…
alamb ea5b573
Update parquet-variant/src/from_json.rs
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,14 +18,10 @@ | |
| //! Module for converting Variant data to JSON format | ||
| use arrow_schema::ArrowError; | ||
| use base64::{engine::general_purpose, Engine as _}; | ||
| use rust_decimal::prelude::*; | ||
| use serde_json::{Number, Value}; | ||
| use serde_json::Value; | ||
| use std::io::Write; | ||
|
|
||
| use crate::{ | ||
| variant::{Variant, VariantList, VariantObject}, | ||
| VariantDecimal, | ||
| }; | ||
| use crate::variant::{Variant, VariantList, VariantObject}; | ||
|
|
||
| // Format string constants to avoid duplication and reduce errors | ||
| const DATE_FORMAT: &str = "%Y-%m-%d"; | ||
|
|
@@ -249,27 +245,6 @@ pub fn variant_to_json_string(variant: &Variant) -> Result<String, ArrowError> { | |
| .map_err(|e| ArrowError::InvalidArgumentError(format!("UTF-8 conversion error: {e}"))) | ||
| } | ||
|
|
||
| fn variant_decimal_to_json_value(decimal: &impl VariantDecimal) -> Result<Value, ArrowError> { | ||
|
Contributor
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. Seems like a macro could avoid both overhead and duplication? The only part that needs to change (besides the data types) is Decimal16 has a more complicated way of handling the integer value produced at the end. |
||
| let integer = decimal.integer(); | ||
| let scale = decimal.scale(); | ||
| if scale == 0 { | ||
| return Ok(Value::from(integer)); | ||
| } | ||
| let divisor = 10_i128.pow(scale); | ||
| if integer % divisor != 0 { | ||
| // try decimal | ||
| if let Ok(dec) = Decimal::try_from_i128_with_scale(integer, scale) { | ||
| if let Ok(value) = serde_json::from_str::<Number>(&dec.to_string()) { | ||
| return Ok(serde_json::Value::Number(value)); | ||
| } | ||
| } | ||
| // fall back to floating point | ||
| return Ok(Value::from(integer as f64 / divisor as f64)); | ||
| } | ||
| // integer perfect division | ||
| Ok(Value::from(integer / divisor)) | ||
| } | ||
|
|
||
| /// Convert [`Variant`] to [`serde_json::Value`] | ||
| /// | ||
| /// This function converts a Variant to a [`serde_json::Value`], which is useful | ||
|
|
@@ -311,9 +286,60 @@ pub fn variant_to_json_value(variant: &Variant) -> Result<Value, ArrowError> { | |
| Variant::Double(f) => serde_json::Number::from_f64(*f) | ||
| .map(Value::Number) | ||
| .ok_or_else(|| ArrowError::InvalidArgumentError("Invalid double value".to_string())), | ||
| Variant::Decimal4(decimal4) => variant_decimal_to_json_value(decimal4), | ||
| Variant::Decimal8(decimal8) => variant_decimal_to_json_value(decimal8), | ||
| Variant::Decimal16(decimal16) => variant_decimal_to_json_value(decimal16), | ||
| Variant::Decimal4(decimal4) => { | ||
| let scale = decimal4.scale(); | ||
| let integer = decimal4.integer(); | ||
|
|
||
| let integer = if scale == 0 { | ||
| integer | ||
| } else { | ||
| let divisor = 10_i32.pow(scale as u32); | ||
| if integer % divisor != 0 { | ||
| // fall back to floating point | ||
| return Ok(Value::from(integer as f64 / divisor as f64)); | ||
| } | ||
| integer / divisor | ||
| }; | ||
| Ok(Value::from(integer)) | ||
| } | ||
| Variant::Decimal8(decimal8) => { | ||
| let scale = decimal8.scale(); | ||
| let integer = decimal8.integer(); | ||
|
|
||
| let integer = if scale == 0 { | ||
| integer | ||
| } else { | ||
| let divisor = 10_i64.pow(scale as u32); | ||
| if integer % divisor != 0 { | ||
| // fall back to floating point | ||
| return Ok(Value::from(integer as f64 / divisor as f64)); | ||
| } | ||
| integer / divisor | ||
| }; | ||
| Ok(Value::from(integer)) | ||
| } | ||
| Variant::Decimal16(decimal16) => { | ||
| let scale = decimal16.scale(); | ||
| let integer = decimal16.integer(); | ||
|
|
||
| let integer = if scale == 0 { | ||
| integer | ||
| } else { | ||
| let divisor = 10_i128.pow(scale as u32); | ||
| if integer % divisor != 0 { | ||
| // fall back to floating point | ||
| return Ok(Value::from(integer as f64 / divisor as f64)); | ||
| } | ||
| integer / divisor | ||
| }; | ||
| // i128 has higher precision than any 64-bit type. Try a lossless narrowing cast to | ||
| // i64 or u64 first, falling back to a lossy narrowing cast to f64 if necessary. | ||
| let value = i64::try_from(integer) | ||
| .map(Value::from) | ||
| .or_else(|_| u64::try_from(integer).map(Value::from)) | ||
| .unwrap_or_else(|_| Value::from(integer as f64)); | ||
| Ok(value) | ||
| } | ||
| Variant::Date(date) => Ok(Value::String(format_date_string(date))), | ||
| Variant::TimestampMicros(ts) => Ok(Value::String(ts.to_rfc3339())), | ||
| Variant::TimestampNtzMicros(ts) => Ok(Value::String(format_timestamp_ntz_string(ts))), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.