rluvaton commented on code in PR #18921: URL: https://github.com/apache/datafusion/pull/18921#discussion_r3073442031
########## datafusion/physical-expr/src/expressions/lambda_variable.rs: ########## @@ -0,0 +1,155 @@ +// 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 variable reference: [`LambdaVariable`] + +use std::any::Any; +use std::hash::Hash; +use std::sync::Arc; + +use crate::physical_expr::PhysicalExpr; +use arrow::datatypes::FieldRef; +use arrow::{ + datatypes::{DataType, Schema}, + record_batch::RecordBatch, +}; + +use datafusion_common::{Result, internal_err, plan_err}; +use datafusion_expr::ColumnarValue; + +/// Represents the lambda variable with a given name and field +#[derive(Debug, Clone)] +pub struct LambdaVariable { + name: String, + index: usize, + field: FieldRef, +} + +impl Eq for LambdaVariable {} + +impl PartialEq for LambdaVariable { + fn eq(&self, other: &Self) -> bool { + self.name == other.name && self.field == other.field + } +} + +impl Hash for LambdaVariable { + fn hash<H: std::hash::Hasher>(&self, state: &mut H) { + self.name.hash(state); + self.field.hash(state); + } +} + +impl LambdaVariable { + /// Create a new lambda variable expression + pub fn new(name: String, index: usize, field: FieldRef) -> Self { + Self { name, index, field } + } + + /// Get the variable's name + pub fn name(&self) -> &str { + &self.name + } + + /// Get the variable's index + pub fn index(&self) -> usize { + self.index + } + + /// Get the variable's field + pub fn field(&self) -> &FieldRef { + &self.field + } +} + +impl std::fmt::Display for LambdaVariable { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{}@", self.name) + } +} + +impl PhysicalExpr for LambdaVariable { + fn as_any(&self) -> &dyn Any { + self + } + + fn data_type(&self, _input_schema: &Schema) -> Result<DataType> { + Ok(self.field.data_type().clone()) + } + + fn nullable(&self, _input_schema: &Schema) -> Result<bool> { + Ok(self.field.is_nullable()) + } + + fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> { + if self.index >= batch.num_columns() { + return internal_err!( + "PhysicalExpr LambdaVariable references column '{}' at index {} (zero-based) but batch only has {} columns: {:?}", + self.name, + self.index, + batch.num_columns(), + batch + .schema_ref() + .fields() + .iter() + .map(|f| f.name()) + .collect::<Vec<_>>() + ); + } + + Ok(ColumnarValue::Array(Arc::clone(batch.column(self.index)))) Review Comment: can you verify that the type here actually match the field?, since I can pass whatever field I want and it will pass -- 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]
