Skip to content

Commit 8f033e4

Browse files
compheadrluvaton
andauthored
feat: minor lambda perf improvements (#21896)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #. Followup on #21679 (comment) ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> --------- Co-authored-by: Raz Luvaton <16746759+rluvaton@users.noreply.github.com>
1 parent 720aaff commit 8f033e4

2 files changed

Lines changed: 144 additions & 117 deletions

File tree

datafusion/expr/src/higher_order_function.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::expr::schema_name_from_exprs_comma_separated_without_space;
2121
use crate::{ColumnarValue, Documentation, Expr};
2222
use arrow::array::{ArrayRef, RecordBatch};
2323
use arrow::datatypes::{DataType, FieldRef, Schema};
24+
use arrow_schema::SchemaRef;
2425
use datafusion_common::config::ConfigOptions;
2526
use datafusion_common::{Result, ScalarValue, not_impl_err};
2627
use datafusion_expr_common::dyn_eq::{DynEq, DynHash};
@@ -218,11 +219,21 @@ pub struct LambdaArgument {
218219
/// For example, for `array_transform([2], v -> -v)`,
219220
/// this will be the physical expression of `-v`
220221
body: Arc<dyn PhysicalExpr>,
222+
/// Cached schema built from `params`. Reused across every `evaluate` call
223+
/// (and across every nested-list iteration when the lambda is called once
224+
/// per outer sublist), avoiding the per-call `Schema::new` build that
225+
/// includes constructing the internal name -> index map.
226+
schema: SchemaRef,
221227
}
222228

223229
impl LambdaArgument {
224230
pub fn new(params: Vec<FieldRef>, body: Arc<dyn PhysicalExpr>) -> Self {
225-
Self { params, body }
231+
let schema = Arc::new(Schema::new(params.clone()));
232+
Self {
233+
params,
234+
body,
235+
schema,
236+
}
226237
}
227238

228239
/// Evaluate this lambda
@@ -238,9 +249,7 @@ impl LambdaArgument {
238249
.map(|arg| arg())
239250
.collect::<Result<_>>()?;
240251

241-
let schema = Arc::new(Schema::new(self.params.clone()));
242-
243-
let batch = RecordBatch::try_new(schema, columns)?;
252+
let batch = RecordBatch::try_new(Arc::clone(&self.schema), columns)?;
244253

245254
self.body.evaluate(&batch)
246255
}

0 commit comments

Comments
 (0)