rich7420 commented on code in PR #7155:
URL: https://github.com/apache/datafusion-comet/pull/7155#discussion_r4345801986
##########
spark/src/main/scala/org/apache/comet/serde/predicates.scala:
##########
@@ -491,3 +495,51 @@ object ComparisonUtils {
}
}
}
+
+object CometAssertNotNull extends CometExpressionSerde[AssertNotNull] {
+ // Enabling this previously unsupported expression must not make an
enclosing eager kernel
+ // check rows that Spark skips (for example, false AND assertnotnull(NULL)).
Admit the
+ // structural and lazy conditional contexts used by V2 writes; keep other
compositions in
+ // Spark until their evaluation order has been verified. Check every parent
during serde so
+ // a safe wrapper cannot hide an unsafe descendant.
+ private[serde] def hasUnsupportedParent(expr: Expression, proto: Expr):
Boolean = expr match {
+ case _: Alias | _: AssertNotNull | _: CreateNamedStruct | _:
GetStructField | _: If |
+ _: CaseWhen =>
+ false
+ case _ =>
+ expr.containsPattern(NULL_CHECK) && containsNativeAssertion(proto)
Review Comment:
`IsNull` and `IsNotNull` also carry `NULL_CHECK`, so inferred `isnotnull`
filters would also trigger a proto walk. Could we check for an actual
`AssertNotNull` first?
```suggestion
expr.containsPattern(NULL_CHECK) &&
expr.exists(_.isInstanceOf[AssertNotNull]) &&
containsNativeAssertion(proto)
```
##########
spark/src/main/scala/org/apache/comet/rules/CometExecRule.scala:
##########
@@ -1356,6 +1389,14 @@ case class CometExecRule(session: SparkSession,
queryStagePrep: Boolean = false)
None
case _ => partialReader
Review Comment:
Should `SampleExec` be treated as a partial reader too? With codegen, Spark
evaluates the projection only for sampled rows, so a native assertion below
`sample(false, 0.1)` could throw where Spark wouldn't.
```suggestion
case _: SampleExec => Some("a sample")
case _ => partialReader
```
##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -1097,6 +1098,16 @@ object QueryPlanSerde extends Logging with CometExprShim
with CometTypeShim {
None
}
})
+ .filter { protoExpr =>
+ val supported = !CometAssertNotNull.hasUnsupportedParent(expr,
protoExpr)
Review Comment:
Does this also cover aggregate arguments? `aggExprToProto` converts them as
roots, so expressions like `FIRST(AssertNotNull(v))` or a grouped `MIN(...)
FILTER (WHERE ...)` could throw on rows Spark never evaluates.
##########
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)?;
+ let has_null = match &value {
+ // A dictionary key can be non-null but refer to a null value.
Conversely, an
+ // unused null dictionary entry does not make any row null.
+ ColumnarValue::Array(array) => array.logical_null_count() > 0,
+ ColumnarValue::Scalar(scalar) => scalar.is_null(),
+ };
+ if has_null {
Review Comment:
nit: if two required fields contain NULLs on different rows in the same
batch, this reports the first failing column rather than Spark's first failing
row. Could we add a note or test?
--
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]