LiaCastaneda commented on code in PR #18921: URL: https://github.com/apache/datafusion/pull/18921#discussion_r2966475263
########## datafusion/physical-expr/src/expressions/lambda.rs: ########## @@ -0,0 +1,136 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Physical lambda expression: [`LambdaExpr`] + +use std::hash::Hash; +use std::sync::Arc; +use std::{any::Any, sync::OnceLock}; + +use crate::expressions::Column; +use crate::physical_expr::PhysicalExpr; +use arrow::{ + datatypes::{DataType, Schema}, + record_batch::RecordBatch, +}; +use datafusion_common::tree_node::{TreeNode, TreeNodeRecursion}; +use datafusion_common::{internal_err, HashSet, Result}; +use datafusion_expr::ColumnarValue; + +/// Represents a lambda with the given parameters names and body +#[derive(Debug, Eq, Clone)] +pub struct LambdaExpr { + params: Vec<String>, + body: Arc<dyn PhysicalExpr>, + captures: OnceLock<HashSet<usize>>, +} + +// Manually derive PartialEq and Hash to work around https://github.com/rust-lang/rust/issues/78808 [https://github.com/apache/datafusion/issues/13196] +impl PartialEq for LambdaExpr { + fn eq(&self, other: &Self) -> bool { + self.params.eq(&other.params) && self.body.eq(&other.body) + } +} + +impl Hash for LambdaExpr { + fn hash<H: std::hash::Hasher>(&self, state: &mut H) { + self.params.hash(state); + self.body.hash(state); + } +} + +impl LambdaExpr { + /// Create a new lambda expression with the given parameters and body + pub fn new(params: Vec<String>, body: Arc<dyn PhysicalExpr>) -> Self { + Self { + params, + body, + captures: OnceLock::new(), + } + } + + /// Get the lambda's params names + pub fn params(&self) -> &[String] { + &self.params + } + + /// Get the lambda's body + pub fn body(&self) -> &Arc<dyn PhysicalExpr> { + &self.body + } + + pub fn captures(&self) -> &HashSet<usize> { + self.captures.get_or_init(|| { Review Comment: > But we can also remove it from creation and rerun for every batch, just let me know! This code is not in this PR anymore I don't remember how exactly it ended up looking :D but my main point was that `OnceLock` was a bit overkill. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
