-
Notifications
You must be signed in to change notification settings - Fork 28
Add from_json func #103
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
Add from_json func #103
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #103 +/- ##
==========================================
+ Coverage 84.12% 85.28% +1.16%
==========================================
Files 16 17 +1
Lines 1216 1346 +130
Branches 1216 1346 +130
==========================================
+ Hits 1023 1148 +125
- Misses 131 134 +3
- Partials 62 64 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
friendlymatthew
left a comment
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.
Makes sense to me. I think we should address 2 comments below before merge
src/json_from_scalar.rs
Outdated
| impl Default for JsonFromScalar { | ||
| fn default() -> Self { | ||
| Self { | ||
| signature: Signature::variadic_any(Volatility::Immutable), |
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.
It's a bit odd that we model this as Signature::variadic_any when the function requires exactly 1 argument
Signature::any(1, Volatility::Immutable) seems like a better fit
src/json_from_scalar.rs
Outdated
| } | ||
| } | ||
|
|
||
| fn scalar_to_json_union_field(scalar: ScalarValue) -> DataFusionResult<Option<JsonUnionField>> { |
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.
It seems the return type's Option wrap is superfluous. I don't see any of the arms below return an Ok(None)
src/json_from_scalar.rs
Outdated
| match array.data_type() { | ||
| DataType::Null => { | ||
| for _ in 0..array.len() { | ||
| union.push(JsonUnionField::JsonNull); | ||
| } | ||
| } | ||
|
|
||
| DataType::Boolean => { | ||
| let arr = array.as_boolean(); | ||
| for i in 0..arr.len() { | ||
| if arr.is_null(i) { | ||
| union.push_none(); | ||
| } else { | ||
| union.push(JsonUnionField::Bool(arr.value(i))); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Integer types - coerce to i64 | ||
| DataType::Int8 => { | ||
| let arr = array.as_primitive::<Int8Type>(); | ||
| for i in 0..arr.len() { | ||
| if arr.is_null(i) { | ||
| union.push_none(); | ||
| } else { | ||
| union.push(JsonUnionField::Int(i64::from(arr.value(i)))); | ||
| } | ||
| } | ||
| } | ||
| DataType::Int16 => { | ||
| let arr = array.as_primitive::<Int16Type>(); | ||
| for i in 0..arr.len() { | ||
| if arr.is_null(i) { | ||
| union.push_none(); | ||
| } else { | ||
| union.push(JsonUnionField::Int(i64::from(arr.value(i)))); | ||
| } | ||
| } | ||
| } | ||
| DataType::Int32 => { | ||
| let arr = array.as_primitive::<Int32Type>(); | ||
| for i in 0..arr.len() { | ||
| if arr.is_null(i) { | ||
| union.push_none(); | ||
| } else { | ||
| union.push(JsonUnionField::Int(i64::from(arr.value(i)))); | ||
| } | ||
| } | ||
| } | ||
| DataType::Int64 => { | ||
| let arr = array.as_primitive::<Int64Type>(); | ||
| for i in 0..arr.len() { | ||
| if arr.is_null(i) { | ||
| union.push_none(); | ||
| } else { | ||
| union.push(JsonUnionField::Int(arr.value(i))); | ||
| } | ||
| } | ||
| } | ||
| DataType::UInt8 => { | ||
| let arr = array.as_primitive::<UInt8Type>(); | ||
| for i in 0..arr.len() { | ||
| if arr.is_null(i) { | ||
| union.push_none(); | ||
| } else { | ||
| union.push(JsonUnionField::Int(i64::from(arr.value(i)))); | ||
| } | ||
| } | ||
| } | ||
| DataType::UInt16 => { | ||
| let arr = array.as_primitive::<UInt16Type>(); | ||
| for i in 0..arr.len() { | ||
| if arr.is_null(i) { | ||
| union.push_none(); | ||
| } else { | ||
| union.push(JsonUnionField::Int(i64::from(arr.value(i)))); | ||
| } | ||
| } | ||
| } | ||
| DataType::UInt32 => { | ||
| let arr = array.as_primitive::<UInt32Type>(); | ||
| for i in 0..arr.len() { | ||
| if arr.is_null(i) { | ||
| union.push_none(); | ||
| } else { | ||
| union.push(JsonUnionField::Int(i64::from(arr.value(i)))); | ||
| } | ||
| } | ||
| } | ||
| DataType::UInt64 => { | ||
| let arr = array.as_primitive::<UInt64Type>(); | ||
| for i in 0..arr.len() { | ||
| if arr.is_null(i) { | ||
| union.push_none(); | ||
| } else { | ||
| union.push(JsonUnionField::Int(i64::try_from(arr.value(i)).map_err(|_| { | ||
| exec_datafusion_err!("UInt64 value {} is out of range for i64", arr.value(i)) | ||
| })?)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Float types - coerce to f64 | ||
| DataType::Float32 => { | ||
| let arr = array.as_primitive::<Float32Type>(); | ||
| for i in 0..arr.len() { | ||
| if arr.is_null(i) { | ||
| union.push_none(); | ||
| } else { | ||
| union.push(JsonUnionField::Float(f64::from(arr.value(i)))); | ||
| } | ||
| } | ||
| } | ||
| DataType::Float64 => { | ||
| let arr = array.as_primitive::<Float64Type>(); | ||
| for i in 0..arr.len() { | ||
| if arr.is_null(i) { | ||
| union.push_none(); | ||
| } else { | ||
| union.push(JsonUnionField::Float(arr.value(i))); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // String types | ||
| DataType::Utf8 => { | ||
| let arr = array.as_string::<i32>(); | ||
| for i in 0..arr.len() { | ||
| if arr.is_null(i) { | ||
| union.push_none(); | ||
| } else { | ||
| union.push(JsonUnionField::Str(arr.value(i).to_string())); | ||
| } | ||
| } | ||
| } | ||
| DataType::LargeUtf8 => { | ||
| let arr = array.as_string::<i64>(); | ||
| for i in 0..arr.len() { | ||
| if arr.is_null(i) { | ||
| union.push_none(); | ||
| } else { | ||
| union.push(JsonUnionField::Str(arr.value(i).to_string())); | ||
| } | ||
| } | ||
| } | ||
| DataType::Utf8View => { | ||
| let arr = array.as_string_view(); | ||
| for i in 0..arr.len() { | ||
| if arr.is_null(i) { | ||
| union.push_none(); | ||
| } else { | ||
| union.push(JsonUnionField::Str(arr.value(i).to_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.
More of a nit and note about future work, we could probably remove a lot of this code if we impl JsonUnion::from_iter where the Iterator::Item is an Option<T> where None represents null 🤔
No description provided.