minor: reuse ColumnarValue::into_array in map's expand_if_scalar and avoid uncessary clones#22984
Open
nathanb9 wants to merge 2 commits into
Open
minor: reuse ColumnarValue::into_array in map's expand_if_scalar and avoid uncessary clones#22984nathanb9 wants to merge 2 commits into
nathanb9 wants to merge 2 commits into
Conversation
alamb
reviewed
Jun 16, 2026
alamb
left a comment
Contributor
There was a problem hiding this comment.
Thanks @nathanb9
I think we could avoid some clones too like this:
diff --git a/datafusion/functions-nested/src/map.rs b/datafusion/functions-nested/src/map.rs
index 868f6dd290..147f051163 100644
--- a/datafusion/functions-nested/src/map.rs
+++ b/datafusion/functions-nested/src/map.rs
@@ -63,27 +63,19 @@ fn can_evaluate_to_const(args: &[ColumnarValue]) -> bool {
.all(|arg| matches!(arg, ColumnarValue::Scalar(_)))
}
-fn expand_if_scalar(arg: &ColumnarValue, rows: usize) -> Result<ColumnarValue> {
- match arg {
- ColumnarValue::Scalar(s) => Ok(ColumnarValue::Array(s.to_array_of_size(rows)?)),
- ColumnarValue::Array(a) => Ok(ColumnarValue::Array(Arc::clone(a))),
- }
+fn expand_if_scalar(arg: ColumnarValue, rows: usize) -> Result<ColumnarValue> {
+ Ok(ColumnarValue::Array(arg.into_array(rows)?))
}
-fn make_map_batch(args: &[ColumnarValue], number_rows: usize) -> Result<ColumnarValue> {
- let [keys_arg, values_arg] = take_function_args("make_map", args)?;
-
- let can_evaluate_to_const = can_evaluate_to_const(args);
+fn make_map_batch(args: Vec<ColumnarValue>, number_rows: usize) -> Result<ColumnarValue> {
+ let can_evaluate_to_const = can_evaluate_to_const(&args);
+ let [mut keys_arg, mut values_arg] = take_function_args("make_map", args)?;
// if we can't evaluate to const (inputs are not both scalar) then ensure they
// are expanded to arrays which following logic expects
- let (keys_arg, values_arg) = if !can_evaluate_to_const {
- (
- expand_if_scalar(keys_arg, number_rows)?,
- expand_if_scalar(values_arg, number_rows)?,
- )
- } else {
- (keys_arg.clone(), values_arg.clone())
+ if !can_evaluate_to_const {
+ keys_arg = expand_if_scalar(keys_arg, number_rows)?;
+ values_arg = expand_if_scalar(values_arg, number_rows)?;
};
let keys = get_first_array_ref(&keys_arg)?;
@@ -417,7 +409,7 @@ impl ScalarUDFImpl for MapFunc {
}
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
- make_map_batch(&args.args, args.number_rows)
+ make_map_batch(args.args, args.number_rows)
}
Contributor
Author
|
Thanks. I added suggested changes I'd swapped the one |
alamb
approved these changes
Jun 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #22934: per @alamb's suggestion, reuse the existing
ColumnarValue::to_arrayinmap.rs'sexpand_if_scalarinstead of hand-rolling the same logic.to_array(borrowing) is used overinto_arrayto avoid an extraScalarValueclone; no behavior change.