-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Improve documentation on how to build ScalarValue::Struct
and add ScalarStructBuilder
#9229
Conversation
@@ -99,6 +102,66 @@ use arrow_buffer::NullBuffer; | |||
/// # } | |||
/// ``` | |||
/// | |||
/// # Nested Types |
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 would be nice to have some similar examples for creating Struct::List
/// # use arrow::datatypes::{DataType, Field}; | ||
/// # use datafusion_common::{ScalarValue, scalar::ScalarStructBuilder}; | ||
/// // Build a struct like: {a: 1, b: "foo"} | ||
/// let field_a = Field::new("a", DataType::Int32, false); |
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.
The use case for the ScalarStructBuilder
is pretty nicely illustrated by this example compared to the one in
## Example: Creating [`ScalarValue::Struct`] directly
@@ -2710,27 +2774,6 @@ impl From<String> for ScalarValue { | |||
} | |||
} | |||
|
|||
// TODO: Remove this after changing to Scalar<T> |
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.
These were added in #7893 and not yet released so this is not a breaking API change
datafusion/common/src/scalar/mod.rs
Outdated
let struct_arr = sv.to_array().unwrap(); | ||
let actual = as_struct_array(&struct_arr).unwrap(); | ||
assert_eq!(actual, &expected); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "assertion `left == right` failed")] | ||
#[should_panic( | ||
expected = "Error building SclarValue::Struct. Expected array with exactly one element, found array with 4 elements" |
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.
the builder makes better messages I think
@@ -932,17 +932,17 @@ fn round_trip_scalar_values() { | |||
ScalarValue::Binary(None), | |||
ScalarValue::LargeBinary(Some(b"bar".to_vec())), | |||
ScalarValue::LargeBinary(None), | |||
ScalarValue::from(( | |||
vec![ |
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.
FWIW this would have panic'd previously if the input was invalid. Using the ScalarStructBuilder
makes it clear that this can happen
…ScalarStructBuilder`
b18b7ca
to
2ff57e4
Compare
datafusion/common/src/scalar/mod.rs
Outdated
@@ -15,7 +15,9 @@ | |||
// specific language governing permissions and limitations | |||
// under the License. | |||
|
|||
//! This module provides ScalarValue, an enum that can be used for storage of single elements | |||
//! [`ScalarValue`]: stores single constant values |
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.
constant may be misleading.
Maybe like
In databases, a scalar value is simply a single value, as opposed to a set of values or a combination of values. It's like having just one piece of information
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.
Scalar value is a single value of different datatypes: bool, ints, floats, arrays, structs, etc
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.
Good point -- reworded
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.
lgtm, thanks @alamb
couple of minors you may want to fix
…tafusion into alamb/better_struct_array
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.
FYI @jayzhan211 for your comments
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.
LGTM!
Which issue does this PR close?
Closes #9227
Rationale for this change
Described on #9227
Basically I found it hard to build
ScalarValue::Struct
after #7893What changes are included in this PR?
ScalarStructBuilder
to help ease the conversion when trying to add ScalarValues or arrays as fieldsFrom..
impls forScalarValue
that were added in Change ScalarValue::Struct to ArrayRef #7893 but are of limited utilityAre these changes tested?
Yes, by doc tests
Are there any user-facing changes?