ametel01 commented on code in PR #22948:
URL: https://github.com/apache/datafusion/pull/22948#discussion_r3427199343


##########
datafusion/sql/src/expr/mod.rs:
##########
@@ -55,6 +55,99 @@ 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<Span> {
+            match expr {
+                SQLExpr::Value(ValueWithSpan {
+                    value: Value::Null,
+                    span,
+                }) => Span::try_from_sqlparser_span(*span),
+                _ => None,
+            }
+        }
+
+        fn null_equality_warning(expr: &SQLExpr) -> Option<Diagnostic> {
+            let SQLExpr::BinaryOp { left, op, right } = expr else {
+                return None;
+            };
+
+            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,
+            };
+
+            let null_span = null_value_span(left).or_else(|| 
null_value_span(right));
+            null_span.map(|null_span| {
+                Diagnostic::new_warning(
+                    message,
+                    Span::try_from_sqlparser_span(expr.span()),
+                )
+                .with_help(help, Some(null_span))
+            })
+        }
+

Review Comment:
   Fixed. NULL detection is now independent of span conversion; the warning is 
emitted even when the NULL literal has no sqlparser span, with the help span 
omitted in that case.
    
   Added test_eq_null_warning_without_null_span.



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