pepijnve commented on code in PR #17813:
URL: https://github.com/apache/datafusion/pull/17813#discussion_r2415726130
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -647,6 +679,246 @@ impl ExprSchemable for Expr {
}
}
+/// Represents the possible values for SQL's three valued logic.
+/// `Option<bool>` is not used for this since `None` is used to represent
+/// inconclusive answers already.
+enum TriStateBool {
+ True,
+ False,
+ Uncertain,
+}
+
+impl TryFrom<&ScalarValue> for TriStateBool {
+ type Error = DataFusionError;
+
+ fn try_from(value: &ScalarValue) -> std::result::Result<Self, Self::Error>
{
+ match value {
+ ScalarValue::Null => Ok(TriStateBool::Uncertain),
+ ScalarValue::Boolean(b) => Ok(match b {
+ None => TriStateBool::Uncertain,
+ Some(true) => TriStateBool::True,
+ Some(false) => TriStateBool::False,
+ }),
+ _ => Self::try_from(&value.cast_to(&DataType::Boolean)?),
+ }
+ }
+}
+
+struct WhenThenConstEvaluator<'a> {
+ then_expr: &'a Expr,
+ input_schema: &'a dyn ExprSchema,
+}
+
+impl WhenThenConstEvaluator<'_> {
+ /// Attempts to const evaluate the given predicate.
+ /// Returns a `Some` value containing the const result if so; otherwise
returns `None`.
+ fn const_eval_predicate(&self, predicate: &Expr) -> Option<TriStateBool> {
+ match predicate {
+ // Literal null is equivalent to boolean uncertain
+ Expr::Literal(scalar, _) => TriStateBool::try_from(scalar).ok(),
Review Comment:
My rationale here was that, since nullability analysis must to be
infallible, that it would be ok to return `None` here which means "answer is
undefined". If you were to actually try to evaluate the expression it will most
likely cause a runtime error at that point so inaccurate nullability is the
least of your concerns.
--
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]