-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Fix incorrect results for NOT IN subqueries with nulls #8271
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
abac256
Fix incorrect results for NOT IN subqueries with nulls
04dad99
update sqllogictest tpch Q16 query plan
8e4903f
Address review comments
863c3a7
Merge remote-tracking branch 'apache/main' into not_in_values
alamb de49fbc
cleanup tests
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,8 +26,8 @@ use datafusion_expr::expr::{Exists, InSubquery}; | |
use datafusion_expr::expr_rewriter::create_col_from_scalar_expr; | ||
use datafusion_expr::logical_plan::{JoinType, Subquery}; | ||
use datafusion_expr::{ | ||
exists, in_subquery, not_exists, not_in_subquery, BinaryExpr, Expr, Filter, | ||
LogicalPlan, LogicalPlanBuilder, Operator, | ||
exists, in_subquery, not_exists, not_in_subquery, BinaryExpr, Expr, ExprSchemable, | ||
Filter, LogicalPlan, LogicalPlanBuilder, Operator, | ||
}; | ||
use log::debug; | ||
use std::collections::BTreeSet; | ||
|
@@ -197,7 +197,7 @@ impl OptimizerRule for DecorrelatePredicateSubquery { | |
/// ``` | ||
fn build_join( | ||
query_info: &SubqueryInfo, | ||
left: &LogicalPlan, | ||
outer_query: &LogicalPlan, | ||
alias: Arc<AliasGenerator>, | ||
) -> Result<Option<LogicalPlan>> { | ||
let where_in_expr_opt = &query_info.where_in_expr; | ||
|
@@ -248,6 +248,38 @@ fn build_join( | |
.map(Option::Some) | ||
})?; | ||
|
||
// build a predicate for comparing the left and right expressions of a given pair of outer/subquery rows | ||
// from an IN or NOT IN predicate | ||
let build_in_predicate = |left: Box<Expr>, right: Box<Expr>| -> Result<Expr> { | ||
let right_col = create_col_from_scalar_expr(right.deref(), Some(subquery_alias))?; | ||
let eq_predicate = Expr::eq(left.deref().clone(), Expr::Column(right_col)); | ||
if !query_info.negated { | ||
// early exit if this is an IN predicate | ||
return Ok(eq_predicate); | ||
} | ||
|
||
match left.nullable(outer_query.schema())? { | ||
true => { | ||
// left expression is nullable; we know the predicate must take the form `left = right IS NOT FALSE` | ||
return Ok(eq_predicate.is_not_false()); | ||
} | ||
false => {} | ||
} | ||
let unqualified_right_col = create_col_from_scalar_expr(right.deref(), None)?; | ||
let subquery_col = query_info | ||
.query | ||
.subquery | ||
.schema() | ||
.field_from_column(&unqualified_right_col)?; | ||
|
||
match subquery_col.is_nullable() { | ||
// add "IS NOT FALSE" to a NOT IN equality predicate whose subquery expression is nullable | ||
// so that an unknown result is treated as a (possible) match | ||
true => Ok(eq_predicate.is_not_false()), | ||
false => Ok(eq_predicate), | ||
} | ||
}; | ||
|
||
if let Some(join_filter) = match (join_filter_opt, in_predicate_opt) { | ||
( | ||
Some(join_filter), | ||
|
@@ -257,8 +289,7 @@ fn build_join( | |
right, | ||
})), | ||
) => { | ||
let right_col = create_col_from_scalar_expr(right.deref(), subquery_alias)?; | ||
let in_predicate = Expr::eq(left.deref().clone(), Expr::Column(right_col)); | ||
let in_predicate = build_in_predicate(left, right)?; | ||
Some(in_predicate.and(join_filter)) | ||
} | ||
(Some(join_filter), _) => Some(join_filter), | ||
|
@@ -270,8 +301,7 @@ fn build_join( | |
right, | ||
})), | ||
) => { | ||
let right_col = create_col_from_scalar_expr(right.deref(), subquery_alias)?; | ||
let in_predicate = Expr::eq(left.deref().clone(), Expr::Column(right_col)); | ||
let in_predicate = build_in_predicate(left, right)?; | ||
Some(in_predicate) | ||
} | ||
_ => None, | ||
|
@@ -281,7 +311,7 @@ fn build_join( | |
true => JoinType::LeftAnti, | ||
false => JoinType::LeftSemi, | ||
}; | ||
let new_plan = LogicalPlanBuilder::from(left.clone()) | ||
let new_plan = LogicalPlanBuilder::from(outer_query.clone()) | ||
.join_on(sub_query_alias, join_type, Some(join_filter))? | ||
jackwener marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.build()?; | ||
debug!( | ||
|
@@ -349,6 +379,15 @@ mod tests { | |
)) | ||
} | ||
|
||
fn test_nullable_subquery_with_name(name: &str) -> Result<Arc<LogicalPlan>> { | ||
let table_scan = test_table_scan_nullable_with_name(name)?; | ||
Ok(Arc::new( | ||
LogicalPlanBuilder::from(table_scan) | ||
.project(vec![col("c")])? | ||
.build()?, | ||
)) | ||
} | ||
|
||
/// Test for several IN subquery expressions | ||
#[test] | ||
fn in_subquery_multiple() -> Result<()> { | ||
|
@@ -1077,6 +1116,148 @@ mod tests { | |
Ok(()) | ||
} | ||
|
||
/// Test for single NOT IN subquery filter and nullable subquery column | ||
#[test] | ||
fn not_in_nullable_subquery_simple() -> Result<()> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 beautiful unit tests |
||
let table_scan = test_table_scan()?; | ||
let plan = LogicalPlanBuilder::from(table_scan) | ||
.filter(not_in_subquery( | ||
col("c"), | ||
test_nullable_subquery_with_name("sq")?, | ||
))? | ||
.project(vec![col("test.b")])? | ||
.build()?; | ||
|
||
let expected = "Projection: test.b [b:UInt32]\ | ||
\n LeftAnti Join: Filter: test.c = __correlated_sq_1.c IS NOT FALSE [a:UInt32, b:UInt32, c:UInt32]\ | ||
\n TableScan: test [a:UInt32, b:UInt32, c:UInt32]\ | ||
\n SubqueryAlias: __correlated_sq_1 [c:UInt32;N]\ | ||
\n Projection: sq.c [c:UInt32;N]\ | ||
\n TableScan: sq [a:UInt32;N, b:UInt32;N, c:UInt32;N]"; | ||
|
||
assert_optimized_plan_eq_display_indent( | ||
Arc::new(DecorrelatePredicateSubquery::new()), | ||
&plan, | ||
expected, | ||
); | ||
Ok(()) | ||
} | ||
|
||
/// Test for single IN subquery filter and nullable subquery column | ||
#[test] | ||
fn in_nullable_subquery_simple() -> Result<()> { | ||
let table_scan = test_table_scan()?; | ||
let plan = LogicalPlanBuilder::from(table_scan) | ||
.filter(in_subquery( | ||
col("c"), | ||
test_nullable_subquery_with_name("sq")?, | ||
))? | ||
.project(vec![col("test.b")])? | ||
.build()?; | ||
|
||
let expected = "Projection: test.b [b:UInt32]\ | ||
\n LeftSemi Join: Filter: test.c = __correlated_sq_1.c [a:UInt32, b:UInt32, c:UInt32]\ | ||
\n TableScan: test [a:UInt32, b:UInt32, c:UInt32]\ | ||
\n SubqueryAlias: __correlated_sq_1 [c:UInt32;N]\ | ||
\n Projection: sq.c [c:UInt32;N]\ | ||
\n TableScan: sq [a:UInt32;N, b:UInt32;N, c:UInt32;N]"; | ||
|
||
assert_optimized_plan_eq_display_indent( | ||
Arc::new(DecorrelatePredicateSubquery::new()), | ||
&plan, | ||
expected, | ||
); | ||
Ok(()) | ||
} | ||
|
||
/// Test for single NOT IN subquery filter and nullable outer query column | ||
#[test] | ||
fn not_in_nullable_outer_query_simple() -> Result<()> { | ||
let table_scan = test_table_scan_nullable()?; | ||
let plan = LogicalPlanBuilder::from(table_scan) | ||
.filter(not_in_subquery(col("c"), test_subquery_with_name("sq")?))? | ||
.project(vec![col("test.b")])? | ||
.build()?; | ||
|
||
let expected = "Projection: test.b [b:UInt32;N]\ | ||
\n LeftAnti Join: Filter: test.c = __correlated_sq_1.c IS NOT FALSE [a:UInt32;N, b:UInt32;N, c:UInt32;N]\ | ||
\n TableScan: test [a:UInt32;N, b:UInt32;N, c:UInt32;N]\ | ||
\n SubqueryAlias: __correlated_sq_1 [c:UInt32]\ | ||
\n Projection: sq.c [c:UInt32]\ | ||
\n TableScan: sq [a:UInt32, b:UInt32, c:UInt32]"; | ||
|
||
assert_optimized_plan_eq_display_indent( | ||
Arc::new(DecorrelatePredicateSubquery::new()), | ||
&plan, | ||
expected, | ||
); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn not_in_nullable_subquery_both_side_expr() -> Result<()> { | ||
let table_scan = test_table_scan()?; | ||
let subquery_scan = test_table_scan_nullable_with_name("sq")?; | ||
|
||
let subquery = LogicalPlanBuilder::from(subquery_scan) | ||
.project(vec![col("c") * lit(2u32)])? | ||
.build()?; | ||
|
||
let plan = LogicalPlanBuilder::from(table_scan) | ||
.filter(not_in_subquery(col("c") + lit(1u32), Arc::new(subquery)))? | ||
.project(vec![col("test.b")])? | ||
.build()?; | ||
|
||
let expected = "Projection: test.b [b:UInt32]\ | ||
\n LeftAnti Join: Filter: test.c + UInt32(1) = __correlated_sq_1.sq.c * UInt32(2) IS NOT FALSE [a:UInt32, b:UInt32, c:UInt32]\ | ||
\n TableScan: test [a:UInt32, b:UInt32, c:UInt32]\ | ||
\n SubqueryAlias: __correlated_sq_1 [sq.c * UInt32(2):UInt32;N]\ | ||
\n Projection: sq.c * UInt32(2) [sq.c * UInt32(2):UInt32;N]\ | ||
\n TableScan: sq [a:UInt32;N, b:UInt32;N, c:UInt32;N]"; | ||
|
||
assert_optimized_plan_eq_display_indent( | ||
Arc::new(DecorrelatePredicateSubquery::new()), | ||
&plan, | ||
expected, | ||
); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn not_in_subquery_join_filter_and_inner_filter() -> Result<()> { | ||
let table_scan = test_table_scan()?; | ||
let subquery_scan = test_table_scan_nullable_with_name("sq")?; | ||
|
||
let subquery = LogicalPlanBuilder::from(subquery_scan) | ||
.filter( | ||
out_ref_col(DataType::UInt32, "test.a") | ||
.eq(col("sq.a")) | ||
.and(col("sq.a").add(lit(1u32)).eq(col("sq.b"))), | ||
)? | ||
.project(vec![col("c") * lit(2u32)])? | ||
.build()?; | ||
|
||
let plan = LogicalPlanBuilder::from(table_scan) | ||
.filter(not_in_subquery(col("c") + lit(1u32), Arc::new(subquery)))? | ||
.project(vec![col("test.b")])? | ||
.build()?; | ||
|
||
let expected = "Projection: test.b [b:UInt32]\ | ||
\n LeftAnti Join: Filter: test.c + UInt32(1) = __correlated_sq_1.sq.c * UInt32(2) IS NOT FALSE AND test.a = __correlated_sq_1.a [a:UInt32, b:UInt32, c:UInt32]\ | ||
\n TableScan: test [a:UInt32, b:UInt32, c:UInt32]\ | ||
\n SubqueryAlias: __correlated_sq_1 [sq.c * UInt32(2):UInt32;N, a:UInt32;N]\ | ||
\n Projection: sq.c * UInt32(2), sq.a [sq.c * UInt32(2):UInt32;N, a:UInt32;N]\ | ||
\n Filter: sq.a + UInt32(1) = sq.b [a:UInt32;N, b:UInt32;N, c:UInt32;N]\ | ||
\n TableScan: sq [a:UInt32;N, b:UInt32;N, c:UInt32;N]"; | ||
|
||
assert_optimized_plan_eq_display_indent( | ||
Arc::new(DecorrelatePredicateSubquery::new()), | ||
&plan, | ||
expected, | ||
); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn in_subquery_both_side_expr() -> Result<()> { | ||
let table_scan = test_table_scan()?; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use
field_with_unqualified_name
here? The unqualified name can be cloned fromright_col.name
.So that there is no need to create
unqualified_right_col
, and we do not need to modify thecreate_col_from_scalar_expr
function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I've made the suggested change.