ametel01 commented on code in PR #22948:
URL: https://github.com/apache/datafusion/pull/22948#discussion_r3432972869
##########
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:
Fixed in 3b1e9cc5c. The NULL equality warning visitor now tracks subquery
query depth, so the outer predicate pass skips projection expressions inside
subquery bodies while subquery WHERE/JOIN/HAVING predicates are still checked
by their own planning flow. Added regression coverage for both cases.
--
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]