kosiew commented on code in PR #22948:
URL: https://github.com/apache/datafusion/pull/22948#discussion_r3432818087
##########
datafusion/sql/src/expr/mod.rs:
##########
@@ -55,6 +57,55 @@ mod unary_op;
mod value;
impl<S: ContextProvider> SqlToRel<'_, S> {
+ pub(crate) fn warn_on_null_equality_predicate(&self, predicate: &SQLExpr) {
+ fn null_value_span(expr: &SQLExpr) -> Option<Option<Span>> {
+ if let SQLExpr::Value(ValueWithSpan {
+ value: Value::Null,
+ span,
+ }) = expr
+ {
+ Some(Span::try_from_sqlparser_span(*span))
+ } else {
+ None
+ }
+ }
+
+ fn null_equality_warning(expr: &SQLExpr) -> Option<Diagnostic> {
+ let SQLExpr::BinaryOp { left, op, right } = expr else {
+ return None;
+ };
+
+ let null_span = null_value_span(left).or_else(||
null_value_span(right))?;
+
+ let (message, help) = match op {
+ BinaryOperator::Eq => (
+ "comparison with NULL using `=` always evaluates to NULL",
+ "use `IS NULL` to check for NULL values",
+ ),
+ BinaryOperator::NotEq => (
+ "comparison with NULL using `<>` always evaluates to NULL",
+ "use `IS NOT NULL` to check for non-NULL values",
+ ),
+ _ => return None,
+ };
+
+ Some(
+ Diagnostic::new_warning(
+ message,
+ Span::try_from_sqlparser_span(expr.span()),
+ )
+ .with_help(help, null_span),
+ )
+ }
+
+ let _ = visit_expressions(predicate, |expr| {
Review Comment:
Nice cleanup here. One small edge case I noticed:
`warn_on_null_equality_predicate` is only called from predicate contexts, but
`visit_expressions(predicate, ...)` will also recurse into subquery bodies.
That means we can now warn on non-predicate expressions inside a predicate
subquery, for example `WHERE EXISTS (SELECT first_name = NULL FROM person)`.
That projection would not warn at top level, which seems a bit broader than the
existing `test_eq_null_projection_has_no_warning` intent.
Could we avoid descending into subquery query bodies for this pass, or rely
on the normal planning flow to run the check only on the subquery's own WHERE,
JOIN, and HAVING predicate contexts? A regression test for `EXISTS (SELECT col
= NULL ...)` would also help keep projection expressions warning-free.
--
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]