Skip to content

feat: Emit warning with Diagnostic when doing = Null #15696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 132 additions & 4 deletions datafusion/sql/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ use datafusion_expr::planner::{
};
use sqlparser::ast::{
AccessExpr, BinaryOperator, CastFormat, CastKind, DataType as SQLDataType,
DictionaryField, Expr as SQLExpr, ExprWithAlias as SQLExprWithAlias, MapEntry,
DictionaryField, Expr as SQLExpr, ExprWithAlias as SQLExprWithAlias, Ident, MapEntry,
StructField, Subscript, TrimWhereField, Value, ValueWithSpan,
};

use datafusion_common::{
internal_datafusion_err, internal_err, not_impl_err, plan_err, DFSchema, Result,
ScalarValue,
internal_datafusion_err, internal_err, not_impl_err, plan_err, DFSchema, Diagnostic,
Result, ScalarValue, Span,
};

use datafusion_expr::expr::ScalarFunction;
Expand Down Expand Up @@ -86,6 +86,42 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
StackEntry::SQLExpr(sql_expr) => {
match *sql_expr {
SQLExpr::BinaryOp { left, op, right } => {
// Detect if there is "= Null" in SQL
if op == BinaryOperator::Eq {
if let SQLExpr::Value(ValueWithSpan {
value: Value::Null,
span: null_span,
}) = *right
{
let left_span = match &*left {
SQLExpr::Identifier(Ident { span, .. }) => *span,
// In this case, we expect left to be
// Indentifier. Just to make the code
// more robust, we'll make left_span
// equals to null_span otherwise.
_ => null_span,
};
let combined_span = Span {
start: Into::into(left_span.start),
end: Into::into(null_span.end),
};

let diagnostic = Diagnostic::new_warning(
"Ambiguous NULL comparison".to_string(),
Some(combined_span),
)
.with_help(
"Use IS NULL instead of = NULL",
Some(Span {
start: Into::into(null_span.start),
end: Into::into(null_span.end),
}),
);

self.warnings.borrow_mut().push(diagnostic);
}
}

// Note the order that we push the entries to the stack
// is important. We want to visit the left node first.
stack.push(StackEntry::Operator(op));
Expand Down Expand Up @@ -1174,7 +1210,7 @@ mod tests {
use sqlparser::parser::Parser;

use datafusion_common::config::ConfigOptions;
use datafusion_common::TableReference;
use datafusion_common::{Location, TableReference};
use datafusion_expr::logical_plan::builder::LogicalTableSource;
use datafusion_expr::{AggregateUDF, ScalarUDF, TableSource, WindowUDF};

Expand Down Expand Up @@ -1316,4 +1352,96 @@ mod tests {

assert!(matches!(expr, Expr::Alias(_)));
}

// Helper to parse SQL expressions
fn parse_expr(sql: &str) -> SQLExpr {
let dialect = GenericDialect {};
Parser::new(&dialect)
.try_with_sql(sql)
.unwrap()
.parse_expr()
.unwrap()
}

#[test]
fn test_single_null_comparison() {
let context = TestContextProvider::new();
let planner = SqlToRel::new(&context);

// Test single = NULL case
let expr = parse_expr("password = NULL");
let _ = planner
.sql_expr_to_logical_expr(
expr,
&DFSchema::empty(),
&mut PlannerContext::new(),
)
.unwrap();

let warnings = planner.warnings.take();
assert_eq!(warnings.len(), 1, "Should detect 1 warning");
let warning = &warnings[0];
assert_eq!(warning.message, "Ambiguous NULL comparison");

assert_eq!(
warning.span,
Some(Span {
start: Location { line: 1, column: 1 },
end: Location {
line: 1,
column: 16
}
})
);

assert_eq!(warning.helps.len(), 1);
let help = &warning.helps[0];
assert_eq!(help.message, "Use IS NULL instead of = NULL");
}

#[test]
fn test_multiple_null_comparisons() {
let context = TestContextProvider::new();
let planner = SqlToRel::new(&context);

// Test multiple = NULL cases
let expr = parse_expr("(name = NULL) OR (age = NULL)");
let _ = planner
.sql_expr_to_logical_expr(
expr,
&DFSchema::empty(),
&mut PlannerContext::new(),
)
.unwrap();

let warnings = planner.warnings.take();
assert_eq!(warnings.len(), 2, "Should detect 2 warnings");

let first = &warnings[0];
assert_eq!(
first.span,
Some(Span {
start: Location { line: 1, column: 2 },
end: Location {
line: 1,
column: 13
}
})
);

let second = &warnings[1];
assert_eq!(
second.span,
Some(Span {
start: Location {
line: 1,
column: 19
},
end: Location {
line: 1,
column: 29
}
})
);
}
}
3 changes: 3 additions & 0 deletions datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

//! [`SqlToRel`]: SQL Query Planner (produces [`LogicalPlan`] from SQL AST)
use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::Arc;
use std::vec;
Expand Down Expand Up @@ -337,6 +338,7 @@ pub struct SqlToRel<'a, S: ContextProvider> {
pub(crate) context_provider: &'a S,
pub(crate) options: ParserOptions,
pub(crate) ident_normalizer: IdentNormalizer,
pub(crate) warnings: RefCell<Vec<Diagnostic>>,
}

impl<'a, S: ContextProvider> SqlToRel<'a, S> {
Expand All @@ -359,6 +361,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
context_provider,
options,
ident_normalizer: IdentNormalizer::new(ident_normalize),
warnings: RefCell::new(Vec::new()),
}
}

Expand Down