-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 ScalarValue::try_as_str
to get str value from logical strings
#14167
Conversation
@@ -2849,6 +2849,50 @@ impl ScalarValue { | |||
ScalarValue::from(value).cast_to(target_type) | |||
} | |||
|
|||
/// Returns the Some(`&str`) representation of `ScalarValue` of logical string type |
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.
this is the new function
ScalarValue::Utf8(Some(delimiter)) | ||
| ScalarValue::LargeUtf8(Some(delimiter)) => { | ||
Ok(Box::new(StringAggAccumulator::new(delimiter.as_str()))) | ||
return match lit.value().try_as_str() { |
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.
This is a pretty good example of can reducing the repetition in the code to check for string literal values. This also now implicitly will work for Dictionary values where it would not have before
| ScalarValue::Utf8(Some(method)) | ||
| ScalarValue::LargeUtf8(Some(method)) => method.parse::<DigestAlgorithm>(), | ||
other => exec_err!("Unsupported data type {other:?} for function digest"), | ||
ColumnarValue::Scalar(scalar) => match scalar.try_as_str() { |
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.
We could also avoid a bunch more duplication of stuff like this:
let part = if let ColumnarValue::Scalar(ScalarValue::Utf8(Some(v))) = part {
If we added a similar convenience method ColumnarValue::try_as_scalar_str()
that returned a Option<Option<&str>>
Similarly we could do the same with Expr::try_as_scalar_str()
| ScalarValue::LargeUtf8(a) | ||
| ScalarValue::Utf8(a) => { | ||
ColumnarValue::Scalar(scalar) => match scalar.try_as_str() { | ||
Some(a) => { |
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.
I think this is clearer now
@@ -2849,6 +2849,50 @@ impl ScalarValue { | |||
ScalarValue::from(value).cast_to(target_type) | |||
} | |||
|
|||
/// Returns the Some(`&str`) representation of `ScalarValue` of logical string type |
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.
💯 doc
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.
Nice. ❤️
/// let scalar = ScalarValue::from("hello"); | ||
/// assert_eq!(scalar.try_as_str().flatten(), Some("hello")); | ||
/// ``` | ||
pub fn try_as_str(&self) -> Option<Option<&str>> { |
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.
Why not -> Result<Option<&str>>
for this try method?
Caller can always convert to an option.
(Also, most of the use cases in this PR are converting a returned None to an error).
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.
Thank you for the review @wiedld
DataFusionError
always has an owned String in it, so returning an Result
is actually quite slow as it needs to allocate some memory and copy stuff around. Thus I think this API should return an Option
Thanks @wiedld and @xudong963 ! |
Which issue does this PR close?
ScalarValue
s as&str
#14166Rationale for this change
See #14166
TLDR is I don't want to have to remember to check all the variants of ScalarValue that can contain a string
What changes are included in this PR?
ScalarValue::try_as_str
to get str value from logical stringsAre these changes tested?
yes, by doc tests and examples
Are there any user-facing changes?
there is a new API but all existing APIs still work