Improve documentation on how to build ScalarValue::Struct and add ScalarStructBuilder#9229
Conversation
| /// # } | ||
| /// ``` | ||
| /// | ||
| /// # Nested Types |
There was a problem hiding this comment.
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.
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.
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.
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.
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.
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.
Scalar value is a single value of different datatypes: bool, ints, floats, arrays, structs, etc
There was a problem hiding this comment.
Good point -- reworded
…tafusion into alamb/better_struct_array
alamb
left a comment
There was a problem hiding this comment.
FYI @jayzhan211 for your comments
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?