Skip to content

Commit

Permalink
correct plan in UT.
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwener committed Nov 24, 2022
1 parent 7ee0130 commit 0ea7de4
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 256 deletions.
131 changes: 71 additions & 60 deletions datafusion/optimizer/src/decorrelate_where_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,15 @@ mod tests {
.build()?;
debug!("plan to optimize:\n{}", plan.display_indent());

let expected = r#"Projection: customer.c_custkey [c_custkey:Int64]
LeftSemi Join: customer.c_custkey = __sq_2.o_custkey [c_custkey:Int64, c_name:Utf8]
LeftSemi Join: customer.c_custkey = __sq_1.o_custkey [c_custkey:Int64, c_name:Utf8]
TableScan: customer [c_custkey:Int64, c_name:Utf8]
Projection: orders.o_custkey AS o_custkey, alias=__sq_1 [o_custkey:Int64]
TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]
Projection: orders.o_custkey AS o_custkey, alias=__sq_2 [o_custkey:Int64]
TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]"#;
let expected = "Projection: customer.c_custkey [c_custkey:Int64]\
\n LeftSemi Join: customer.c_custkey = __sq_2.o_custkey [c_custkey:Int64, c_name:Utf8]\
\n LeftSemi Join: customer.c_custkey = __sq_1.o_custkey [c_custkey:Int64, c_name:Utf8]\
\n TableScan: customer [c_custkey:Int64, c_name:Utf8]\
\n SubqueryAlias: __sq_1 [o_custkey:Int64]\
\n Projection: orders.o_custkey AS o_custkey [o_custkey:Int64]\
\n TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]\
\n SubqueryAlias: __sq_2 [o_custkey:Int64]\n Projection: orders.o_custkey AS o_custkey [o_custkey:Int64]\
\n TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]";
assert_optimized_plan_eq(&DecorrelateWhereIn::new(), &plan, expected);
Ok(())
}
Expand Down Expand Up @@ -296,14 +297,16 @@ mod tests {
.project(vec![col("customer.c_custkey")])?
.build()?;

let expected = r#"Projection: customer.c_custkey [c_custkey:Int64]
LeftSemi Join: customer.c_custkey = __sq_2.o_custkey [c_custkey:Int64, c_name:Utf8]
TableScan: customer [c_custkey:Int64, c_name:Utf8]
Projection: orders.o_custkey AS o_custkey, alias=__sq_2 [o_custkey:Int64]
LeftSemi Join: orders.o_orderkey = __sq_1.l_orderkey [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]
TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]
Projection: lineitem.l_orderkey AS l_orderkey, alias=__sq_1 [l_orderkey:Int64]
TableScan: lineitem [l_orderkey:Int64, l_partkey:Int64, l_suppkey:Int64, l_linenumber:Int32, l_quantity:Float64, l_extendedprice:Float64]"#;
let expected = "Projection: customer.c_custkey [c_custkey:Int64]\
\n LeftSemi Join: customer.c_custkey = __sq_2.o_custkey [c_custkey:Int64, c_name:Utf8]\
\n TableScan: customer [c_custkey:Int64, c_name:Utf8]\
\n SubqueryAlias: __sq_2 [o_custkey:Int64]\
\n Projection: orders.o_custkey AS o_custkey [o_custkey:Int64]\
\n LeftSemi Join: orders.o_orderkey = __sq_1.l_orderkey [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]\
\n TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]\
\n SubqueryAlias: __sq_1 [l_orderkey:Int64]\
\n Projection: lineitem.l_orderkey AS l_orderkey [l_orderkey:Int64]\
\n TableScan: lineitem [l_orderkey:Int64, l_partkey:Int64, l_suppkey:Int64, l_linenumber:Int32, l_quantity:Float64, l_extendedprice:Float64]";

assert_optimized_plan_eq(&DecorrelateWhereIn::new(), &plan, expected);
Ok(())
Expand All @@ -328,12 +331,13 @@ mod tests {
.project(vec![col("customer.c_custkey")])?
.build()?;

let expected = r#"Projection: customer.c_custkey [c_custkey:Int64]
LeftSemi Join: customer.c_custkey = __sq_1.o_custkey [c_custkey:Int64, c_name:Utf8]
TableScan: customer [c_custkey:Int64, c_name:Utf8]
Projection: orders.o_custkey AS o_custkey, alias=__sq_1 [o_custkey:Int64]
Filter: orders.o_orderkey = Int32(1) [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]
TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]"#;
let expected = "Projection: customer.c_custkey [c_custkey:Int64]\
\n LeftSemi Join: customer.c_custkey = __sq_1.o_custkey [c_custkey:Int64, c_name:Utf8]\
\n TableScan: customer [c_custkey:Int64, c_name:Utf8]\
\n SubqueryAlias: __sq_1 [o_custkey:Int64]\
\n Projection: orders.o_custkey AS o_custkey [o_custkey:Int64]\
\n Filter: orders.o_orderkey = Int32(1) [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]\
\n TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]";

assert_optimized_plan_eq(&DecorrelateWhereIn::new(), &plan, expected);
Ok(())
Expand All @@ -355,12 +359,13 @@ mod tests {
.build()?;

// Query will fail, but we can still transform the plan
let expected = r#"Projection: customer.c_custkey [c_custkey:Int64]
LeftSemi Join: customer.c_custkey = __sq_1.o_custkey [c_custkey:Int64, c_name:Utf8]
TableScan: customer [c_custkey:Int64, c_name:Utf8]
Projection: orders.o_custkey AS o_custkey, alias=__sq_1 [o_custkey:Int64]
Filter: customer.c_custkey = customer.c_custkey [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]
TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]"#;
let expected = "Projection: customer.c_custkey [c_custkey:Int64]\
\n LeftSemi Join: customer.c_custkey = __sq_1.o_custkey [c_custkey:Int64, c_name:Utf8]\
\n TableScan: customer [c_custkey:Int64, c_name:Utf8]\
\n SubqueryAlias: __sq_1 [o_custkey:Int64]\
\n Projection: orders.o_custkey AS o_custkey [o_custkey:Int64]\
\n Filter: customer.c_custkey = customer.c_custkey [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]\
\n TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]";

assert_optimized_plan_eq(&DecorrelateWhereIn::new(), &plan, expected);
Ok(())
Expand All @@ -381,12 +386,13 @@ mod tests {
.project(vec![col("customer.c_custkey")])?
.build()?;

let expected = r#"Projection: customer.c_custkey [c_custkey:Int64]
LeftSemi Join: customer.c_custkey = __sq_1.o_custkey [c_custkey:Int64, c_name:Utf8]
TableScan: customer [c_custkey:Int64, c_name:Utf8]
Projection: orders.o_custkey AS o_custkey, alias=__sq_1 [o_custkey:Int64]
Filter: orders.o_custkey = orders.o_custkey [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]
TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]"#;
let expected = "Projection: customer.c_custkey [c_custkey:Int64]\
\n LeftSemi Join: customer.c_custkey = __sq_1.o_custkey [c_custkey:Int64, c_name:Utf8]\
\n TableScan: customer [c_custkey:Int64, c_name:Utf8]\
\n SubqueryAlias: __sq_1 [o_custkey:Int64]\
\n Projection: orders.o_custkey AS o_custkey [o_custkey:Int64]\
\n Filter: orders.o_custkey = orders.o_custkey [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]\
\n TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]";

assert_optimized_plan_eq(&DecorrelateWhereIn::new(), &plan, expected);
Ok(())
Expand All @@ -407,11 +413,12 @@ mod tests {
.project(vec![col("customer.c_custkey")])?
.build()?;

let expected = r#"Projection: customer.c_custkey [c_custkey:Int64]
LeftSemi Join: customer.c_custkey = __sq_1.o_custkey Filter: customer.c_custkey != orders.o_custkey [c_custkey:Int64, c_name:Utf8]
TableScan: customer [c_custkey:Int64, c_name:Utf8]
Projection: orders.o_custkey AS o_custkey, alias=__sq_1 [o_custkey:Int64]
TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]"#;
let expected = "Projection: customer.c_custkey [c_custkey:Int64]\
\n LeftSemi Join: customer.c_custkey = __sq_1.o_custkey Filter: customer.c_custkey != orders.o_custkey [c_custkey:Int64, c_name:Utf8]\
\n TableScan: customer [c_custkey:Int64, c_name:Utf8]\
\n SubqueryAlias: __sq_1 [o_custkey:Int64]\
\n Projection: orders.o_custkey AS o_custkey [o_custkey:Int64]\
\n TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]";

assert_optimized_plan_eq(&DecorrelateWhereIn::new(), &plan, expected);
Ok(())
Expand Down Expand Up @@ -583,12 +590,13 @@ mod tests {
.project(vec![col("customer.c_custkey")])?
.build()?;

let expected = r#"Projection: customer.c_custkey [c_custkey:Int64]
Filter: customer.c_custkey = Int32(1) [c_custkey:Int64, c_name:Utf8]
LeftSemi Join: customer.c_custkey = __sq_1.o_custkey [c_custkey:Int64, c_name:Utf8]
TableScan: customer [c_custkey:Int64, c_name:Utf8]
Projection: orders.o_custkey AS o_custkey, alias=__sq_1 [o_custkey:Int64]
TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]"#;
let expected = "Projection: customer.c_custkey [c_custkey:Int64]\
\n Filter: customer.c_custkey = Int32(1) [c_custkey:Int64, c_name:Utf8]\
\n LeftSemi Join: customer.c_custkey = __sq_1.o_custkey [c_custkey:Int64, c_name:Utf8]\
\n TableScan: customer [c_custkey:Int64, c_name:Utf8]\
\n SubqueryAlias: __sq_1 [o_custkey:Int64]\
\n Projection: orders.o_custkey AS o_custkey [o_custkey:Int64]\
\n TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]";

assert_optimized_plan_eq(&DecorrelateWhereIn::new(), &plan, expected);
Ok(())
Expand Down Expand Up @@ -640,11 +648,12 @@ mod tests {
.project(vec![col("test.b")])?
.build()?;

let expected = r#"Projection: test.b [b:UInt32]
LeftSemi Join: test.c = __sq_1.c, test.a = __sq_1.a [a:UInt32, b:UInt32, c:UInt32]
TableScan: test [a:UInt32, b:UInt32, c:UInt32]
Projection: sq.c AS c, sq.a AS a, alias=__sq_1 [c:UInt32, a:UInt32]
TableScan: sq [a:UInt32, b:UInt32, c:UInt32]"#;
let expected = "Projection: test.b [b:UInt32]\
\n LeftSemi Join: test.c = __sq_1.c, test.a = __sq_1.a [a:UInt32, b:UInt32, c:UInt32]\
\n TableScan: test [a:UInt32, b:UInt32, c:UInt32]\
\n SubqueryAlias: __sq_1 [c:UInt32, a:UInt32]\
\n Projection: sq.c AS c, sq.a AS a [c:UInt32, a:UInt32]\
\n TableScan: sq [a:UInt32, b:UInt32, c:UInt32]";

assert_optimized_plan_eq(&DecorrelateWhereIn::new(), &plan, expected);
Ok(())
Expand All @@ -659,11 +668,12 @@ mod tests {
.project(vec![col("test.b")])?
.build()?;

let expected = r#"Projection: test.b [b:UInt32]
LeftSemi Join: test.c = __sq_1.c [a:UInt32, b:UInt32, c:UInt32]
TableScan: test [a:UInt32, b:UInt32, c:UInt32]
Projection: sq.c AS c, alias=__sq_1 [c:UInt32]
TableScan: sq [a:UInt32, b:UInt32, c:UInt32]"#;
let expected = "Projection: test.b [b:UInt32]\
\n LeftSemi Join: test.c = __sq_1.c [a:UInt32, b:UInt32, c:UInt32]\
\n TableScan: test [a:UInt32, b:UInt32, c:UInt32]\
\n SubqueryAlias: __sq_1 [c:UInt32]\
\n Projection: sq.c AS c [c:UInt32]\
\n TableScan: sq [a:UInt32, b:UInt32, c:UInt32]";

assert_optimized_plan_eq(&DecorrelateWhereIn::new(), &plan, expected);
Ok(())
Expand All @@ -678,11 +688,12 @@ mod tests {
.project(vec![col("test.b")])?
.build()?;

let expected = r#"Projection: test.b [b:UInt32]
LeftAnti Join: test.c = __sq_1.c [a:UInt32, b:UInt32, c:UInt32]
TableScan: test [a:UInt32, b:UInt32, c:UInt32]
Projection: sq.c AS c, alias=__sq_1 [c:UInt32]
TableScan: sq [a:UInt32, b:UInt32, c:UInt32]"#;
let expected = "Projection: test.b [b:UInt32]\
\n LeftAnti Join: test.c = __sq_1.c [a:UInt32, b:UInt32, c:UInt32]\
\n TableScan: test [a:UInt32, b:UInt32, c:UInt32]\
\n SubqueryAlias: __sq_1 [c:UInt32]\
\n Projection: sq.c AS c [c:UInt32]\
\n TableScan: sq [a:UInt32, b:UInt32, c:UInt32]";

assert_optimized_plan_eq(&DecorrelateWhereIn::new(), &plan, expected);
Ok(())
Expand Down
41 changes: 22 additions & 19 deletions datafusion/optimizer/src/filter_push_down.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,14 +1193,15 @@ mod tests {
.build()?;

// filter appears below Union
let expected = "\
Union\
\n Projection: test.a AS b, alias=test2\
\n Filter: test.a = Int64(1)\
\n TableScan: test\
\n Projection: test.a AS b, alias=test2\
\n Filter: test.a = Int64(1)\
\n TableScan: test";
let expected = "Union\
\n SubqueryAlias: test2\
\n Projection: test.a AS b\
\n Filter: test.a = Int64(1)\
\n TableScan: test\
\n SubqueryAlias: test2\
\n Projection: test.a AS b\
\n Filter: test.a = Int64(1)\
\n TableScan: test";
assert_optimized_plan_eq(&plan, expected);
Ok(())
}
Expand Down Expand Up @@ -2299,22 +2300,24 @@ mod tests {
.project(vec![col("b.a")])?
.build()?;

let expected_before = "\
Projection: b.a\
let expected_before = "Projection: b.a\
\n Filter: b.a = Int64(1)\
\n Projection: b.a, alias=b\
\n Projection: Int64(0) AS a, alias=b\
\n EmptyRelation";
\n SubqueryAlias: b\
\n Projection: b.a\
\n SubqueryAlias: b\
\n Projection: Int64(0) AS a\
\n EmptyRelation";
assert_eq!(format!("{:?}", plan), expected_before);

// Ensure that the predicate without any columns (0 = 1) is
// still there.
let expected_after = "\
Projection: b.a\
\n Projection: b.a, alias=b\
\n Projection: Int64(0) AS a, alias=b\
\n Filter: Int64(0) = Int64(1)\
\n EmptyRelation";
let expected_after = "Projection: b.a\
\n Filter: b.a = Int64(1)\
\n SubqueryAlias: b\
\n Projection: b.a\
\n SubqueryAlias: b\
\n Projection: Int64(0) AS a\
\n EmptyRelation";
assert_optimized_plan_eq(&plan, expected_after);

Ok(())
Expand Down
10 changes: 6 additions & 4 deletions datafusion/optimizer/src/inline_table_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ mod tests {
pub struct CustomSource {
plan: LogicalPlan,
}

impl CustomSource {
fn new() -> Self {
Self {
Expand All @@ -120,6 +121,7 @@ mod tests {
}
}
}

impl TableSource for CustomSource {
fn as_any(&self) -> &dyn std::any::Any {
self
Expand Down Expand Up @@ -156,10 +158,10 @@ mod tests {
.optimize(&plan, &mut OptimizerConfig::new())
.expect("failed to optimize plan");
let formatted_plan = format!("{:?}", optimized_plan);
let expected = "\
Filter: x.a = Int32(1)\
\n Projection: y.a, alias=x\
\n TableScan: y";
let expected = "Filter: x.a = Int32(1)\
\n SubqueryAlias: x\
\n Projection: y.a\
\n TableScan: y";

assert_eq!(formatted_plan, expected);
assert_eq!(plan.schema(), optimized_plan.schema());
Expand Down
Loading

0 comments on commit 0ea7de4

Please sign in to comment.