gstvg commented on code in PR #21679: URL: https://github.com/apache/datafusion/pull/21679#discussion_r3104922151
########## datafusion/physical-expr/src/expressions/lambda.rs: ########## @@ -0,0 +1,156 @@ +// 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 crate::physical_expr::PhysicalExpr; +use arrow::{ + datatypes::{DataType, Schema}, + record_batch::RecordBatch, +}; +use datafusion_common::plan_err; +use datafusion_common::{HashSet, Result, internal_err}; +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>, +} + +// 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 try_new(params: Vec<String>, body: Arc<dyn PhysicalExpr>) -> Result<Self> { + if all_unique(¶ms) { + Ok(Self::new(params, body)) + } else { + plan_err!("lambda params must be unique, got ({})", params.join(", ")) + } + } + + fn new(params: Vec<String>, body: Arc<dyn PhysicalExpr>) -> Self { + Self { params, body } + } + + /// 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 + } +} + +impl std::fmt::Display for LambdaExpr { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "({}) -> {}", self.params.join(", "), self.body) + } +} + +impl PhysicalExpr for LambdaExpr { + fn data_type(&self, _input_schema: &Schema) -> Result<DataType> { + Ok(DataType::Null) + } + + fn nullable(&self, _input_schema: &Schema) -> Result<bool> { + Ok(true) + } + + fn evaluate(&self, _batch: &RecordBatch) -> Result<ColumnarValue> { + internal_err!("LambdaExpr::evaluate() should not be called") + } + + fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> { + vec![&self.body] + } + + fn with_new_children( + self: Arc<Self>, + children: Vec<Arc<dyn PhysicalExpr>>, + ) -> Result<Arc<dyn PhysicalExpr>> { + Ok(Arc::new(Self::new( + self.params.clone(), + Arc::clone(&children[0]), + ))) Review Comment: https://github.com/apache/datafusion/pull/21679/changes/f9507fad2bf2017f45bd87080d247ef8b10665af Thanks -- 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]
