viirya commented on code in PR #1122: URL: https://github.com/apache/datafusion-comet/pull/1122#discussion_r1859195781
########## native/spark-expr/src/list.rs: ########## @@ -708,6 +708,91 @@ impl PartialEq<dyn Any> for ArrayInsert { } } +#[derive(Debug, Hash)] +pub struct ArraySize { + src_array_expr: Arc<dyn PhysicalExpr>, +} + +impl ArraySize { + pub fn new(src_array_expr: Arc<dyn PhysicalExpr>) -> Self { + Self { src_array_expr } + } +} + +impl Display for ArraySize { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "ArraySize [array: {:?}]", self.src_array_expr) + } +} + +impl PartialEq<dyn Any> for ArraySize { + fn eq(&self, other: &dyn Any) -> bool { + down_cast_any_ref(other) + .downcast_ref::<Self>() + .map(|x| self.src_array_expr.eq(&x.src_array_expr)) + .unwrap_or(false) + } +} + +impl PhysicalExpr for ArraySize { + fn as_any(&self) -> &dyn Any { + self + } + + fn data_type(&self, _input_schema: &Schema) -> DataFusionResult<DataType> { + Ok(DataType::Int32) + } + + fn nullable(&self, input_schema: &Schema) -> DataFusionResult<bool> { + self.src_array_expr.nullable(input_schema) + } + + fn evaluate(&self, batch: &RecordBatch) -> DataFusionResult<ColumnarValue> { + let array_value = self + .src_array_expr + .evaluate(batch)? + .into_array(batch.num_rows())?; + match array_value.data_type() { + DataType::List(_) => { + let list_array = as_list_array(&array_value)?; + let mut builder = Int32Array::builder(list_array.len()); + for i in 0..list_array.len() { + if list_array.is_null(i) { + builder.append_null(); + } else { + builder.append_value(list_array.value_length(i)); + } + } + let sizes_array = Int32Array::from(builder.finish()); + Ok(ColumnarValue::Array(Arc::new(sizes_array))) + } + _ => Err(DataFusionError::Internal(format!( + "Unexpected data type in ArraySize: {:?}", + array_value.data_type() + ))), + } + } + + fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> { + vec![&self.src_array_expr] + } + fn with_new_children( + self: Arc<Self>, + children: Vec<Arc<dyn PhysicalExpr>>, + ) -> datafusion_common::Result<Arc<dyn PhysicalExpr>> { + match children.len() { + 1 => Ok(Arc::new(ArraySize::new(Arc::clone(&children[0])))), + _ => internal_err!("ListExtract should have exactly two children"), Review Comment: ```suggestion _ => internal_err!("ArraySize should have exactly one children"), ``` -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org