alamb commented on code in PR #17813:
URL: https://github.com/apache/datafusion/pull/17813#discussion_r2504683223


##########
datafusion/expr/src/predicate_eval.rs:
##########


Review Comment:
   Perhaps if we called this module `partial_eval.rs` it would better reflect 
its contents



##########
datafusion/expr/src/predicate_eval.rs:
##########
@@ -0,0 +1,727 @@
+// 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 crate::predicate_eval::TriStateBool::{False, True, Uncertain};
+use crate::{BinaryExpr, Expr, ExprSchemable};
+use arrow::datatypes::DataType;
+use datafusion_common::tree_node::{TreeNode, TreeNodeRecursion};
+use datafusion_common::{DataFusionError, ExprSchema, ScalarValue};
+use datafusion_expr_common::operator::Operator;
+use std::ops::{BitAnd, BitOr, Not};
+
+/// Represents the possible values for SQL's three valued logic.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+enum TriStateBool {
+    True,
+    False,
+    Uncertain,
+}
+
+impl From<&Option<bool>> for TriStateBool {
+    fn from(value: &Option<bool>) -> Self {
+        match value {
+            None => Uncertain,
+            Some(true) => True,
+            Some(false) => False,
+        }
+    }
+}
+
+impl TryFrom<&ScalarValue> for TriStateBool {
+    type Error = DataFusionError;
+
+    fn try_from(value: &ScalarValue) -> Result<Self, Self::Error> {
+        match value {
+            ScalarValue::Null => {
+                // Literal null is equivalent to boolean uncertain
+                Ok(Uncertain)
+            }
+            ScalarValue::Boolean(b) => Ok(match b {
+                Some(true) => True,
+                Some(false) => False,
+                None => Uncertain,
+            }),
+            _ => Self::try_from(&value.cast_to(&DataType::Boolean)?),
+        }
+    }
+}
+
+impl TriStateBool {
+    fn try_from_no_cooerce(value: &ScalarValue) -> Option<Self> {
+        match value {
+            ScalarValue::Null => Some(Uncertain),
+            ScalarValue::Boolean(b) => Some(TriStateBool::from(b)),
+            _ => None,
+        }
+    }
+
+    fn is_null(&self) -> TriStateBool {
+        match self {
+            True | False => False,
+            Uncertain => True,
+        }
+    }
+
+    fn is_true(&self) -> TriStateBool {
+        match self {
+            True => True,
+            Uncertain | False => False,
+        }
+    }
+
+    fn is_false(&self) -> TriStateBool {
+        match self {
+            False => True,
+            Uncertain | True => False,
+        }
+    }
+
+    fn is_unknown(&self) -> TriStateBool {
+        match self {
+            Uncertain => True,
+            True | False => False,
+        }
+    }
+}
+
+impl Not for TriStateBool {
+    type Output = TriStateBool;
+
+    fn not(self) -> Self::Output {
+        match self {
+            True => False,
+            False => True,
+            Uncertain => Uncertain,
+        }
+    }
+}
+
+impl BitAnd for TriStateBool {
+    type Output = TriStateBool;
+
+    fn bitand(self, rhs: Self) -> Self::Output {
+        match (self, rhs) {
+            (False, _) | (_, False) => False,
+            (Uncertain, _) | (_, Uncertain) => Uncertain,
+            (True, True) => True,
+        }
+    }
+}
+
+impl BitOr for TriStateBool {
+    type Output = TriStateBool;
+
+    fn bitor(self, rhs: Self) -> Self::Output {
+        match (self, rhs) {
+            (True, _) | (_, True) => True,
+            (Uncertain, _) | (_, Uncertain) => Uncertain,
+            (False, False) => False,
+        }
+    }
+}
+
+/// Attempts to partially constant-evaluate a predicate under SQL three-valued 
logic.
+///
+/// Semantics of the return value:
+/// - `Some(true)`      => predicate is provably true
+/// - `Some(false)`     => predicate is provably false
+/// - `None`            => inconclusive with available static information
+///
+/// The evaluation is conservative and only uses:
+/// - Expression nullability from `input_schema`
+/// - Simple type checks (e.g. whether an expression is Boolean)
+/// - Syntactic patterns (IS NULL/IS NOT NULL/IS TRUE/IS FALSE/etc.)
+/// - Three-valued boolean algebra for AND/OR/NOT
+///
+/// It does not evaluate user-defined functions.

Review Comment:
   This is a very interesting approach. 
   
   The partial evaluation reminds me of the range / interval analysis in
   - 
https://docs.rs/datafusion/latest/datafusion/physical_expr/intervals/cp_solver/index.html
   
   I think in theory that framework is more general and could cover this type 
of analysis (you provide knowledge of the input values and if the output comes 
out as CERTAINLY_TRUE or CERTAINLY_FALSE we can make some assesment for sure



-- 
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]

Reply via email to