-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Improve documentation on how to build ScalarValue::Struct and add ScalarStructBuilder
#9229
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
| /// # } | ||
| /// ``` | ||
| /// | ||
| /// # 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
| } | ||
| } | ||
|
|
||
| // 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
| #[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
| 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
| // 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
comphead
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.
lgtm, thanks @alamb
couple of minors you may want to fix
…tafusion into alamb/better_struct_array
alamb
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.
FYI @jayzhan211 for your comments
jayzhan211
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.
LGTM!
Which issue does this PR close?
Closes #9227
Rationale for this change
Described on #9227
Basically I found it hard to build
ScalarValue::Structafter #7893What changes are included in this PR?
ScalarStructBuilderto help ease the conversion when trying to add ScalarValues or arrays as fieldsFrom..impls forScalarValuethat 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?