LinSimon-901101 commented on code in PR #7155: URL: https://github.com/apache/datafusion-comet/pull/7155#discussion_r4373542922
########## native/spark-expr/src/predicate_funcs/assert_not_null.rs: ########## @@ -0,0 +1,213 @@ +// 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. + +use std::fmt::{Display, Formatter}; +use std::hash::Hash; +use std::sync::Arc; + +use arrow::array::new_empty_array; +use arrow::datatypes::{DataType, Schema}; +use arrow::record_batch::RecordBatch; +use datafusion::common::Result; +use datafusion::logical_expr::ColumnarValue; +use datafusion::physical_expr::PhysicalExpr; +use datafusion_comet_common::SparkError; + +/// Spark's runtime nullability check. Only the value itself is checked, not its nested fields. +#[derive(Debug, Eq)] +pub struct AssertNotNull { + child: Arc<dyn PhysicalExpr>, + walked_type_path: String, +} + +impl AssertNotNull { + pub fn new(child: Arc<dyn PhysicalExpr>, walked_type_path: String) -> Self { + Self { + child, + walked_type_path, + } + } +} + +impl PartialEq for AssertNotNull { + fn eq(&self, other: &Self) -> bool { + self.child.eq(&other.child) && self.walked_type_path == other.walked_type_path + } +} + +impl Hash for AssertNotNull { + fn hash<H: std::hash::Hasher>(&self, state: &mut H) { + self.child.hash(state); + self.walked_type_path.hash(state); + } +} + +impl Display for AssertNotNull { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!( + f, + "AssertNotNull({}, {:?})", + self.child, self.walked_type_path + ) + } +} + +impl PhysicalExpr for AssertNotNull { + fn fmt_sql(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + Display::fmt(self, f) + } + + fn data_type(&self, input_schema: &Schema) -> Result<DataType> { + self.child.data_type(input_schema) + } + + fn nullable(&self, _input_schema: &Schema) -> Result<bool> { + Ok(false) + } + + fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> { + // There is no row to check, including when a conditional filters out every row. + if batch.num_rows() == 0 { + return Ok(ColumnarValue::Array(new_empty_array( + &self.data_type(batch.schema().as_ref())?, + ))); + } + let value = self.child.evaluate(batch)?; Review Comment: Fixed in `4678792`, included in the current head `6e850d7`. In that head, `predicates.scala:577–582` uses `isNonThrowing` to keep assertions over potentially throwing children on Spark, so a later divide-by-zero does not replace an earlier NULL assertion. The regression test is in `CometExpressionSuite.scala:139–160`. -- 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]
