Skip to content

Apache DataFusion SQL Query Engine

Convert more cross joins to inner joins (Address performance/execution plan of TPCH query 19) (#3482)

Dhamotharan Sritharan7f26cff85a8c
diff --git a/datafusion/core/src/execution/context.rs b/datafusion/core/src/execution/context.rs
index 492782f534..fe8b0ec2e2 100644
--- a/datafusion/core/src/execution/context.rs
+++ b/datafusion/core/src/execution/context.rs
@@ -77,6 +77,7 @@ use crate::optimizer::filter_push_down::FilterPushDown;
use crate::optimizer::limit_push_down::LimitPushDown;
use crate::optimizer::optimizer::{OptimizerConfig, OptimizerRule};
use crate::optimizer::projection_push_down::ProjectionPushDown;
+use crate::optimizer::reduce_cross_join::ReduceCrossJoin;
use crate::optimizer::reduce_outer_join::ReduceOuterJoin;
use crate::optimizer::simplify_expressions::SimplifyExpressions;
use crate::optimizer::single_distinct_to_groupby::SingleDistinctToGroupBy;
@@ -1438,6 +1439,7 @@ impl SessionState {
Arc::new(ScalarSubqueryToJoin::new()),
Arc::new(SubqueryFilterToJoin::new()),
Arc::new(EliminateFilter::new()),
+ Arc::new(ReduceCrossJoin::new()),
Arc::new(CommonSubexprEliminate::new()),
Arc::new(EliminateLimit::new()),
Arc::new(ProjectionPushDown::new()),
diff --git a/datafusion/core/tests/sql/predicates.rs b/datafusion/core/tests/sql/predicates.rs
index 5b57bc9719..5b1fa92e5c 100644
--- a/datafusion/core/tests/sql/predicates.rs
+++ b/datafusion/core/tests/sql/predicates.rs
@@ -427,9 +427,9 @@ async fn multiple_or_predicates() -> Result<()> {
let expected =vec![
"Explain [plan_type:Utf8, plan:Utf8]",
" Projection: #lineitem.l_partkey [l_partkey:Int64]",
- " Projection: #part.p_partkey = #lineitem.l_partkey AS #part.p_partkey = #lineitem.l_partkey#lineitem.l_partkey#part.p_partkey, #part.p_size >= Int32(1) AS #part.p_size >= Int32(1)Int32(1)#part.p_size, #lineitem.l_partkey, #lineitem.l_quantity, #part.p_brand, #part.p_size [#part.p_partkey = #lineitem.l_partkey#lineitem.l_partkey#part.p_partkey:Boolean;N, #part.p_size >= Int32(1)Int32(1)#part.p_size:Boolean;N, l_partkey:Int64, l_quantity:Decimal128(15, 2), p_brand:Utf8, p_size:Int32]",
- " Filter: #part.p_partkey = #lineitem.l_partkey AND #part.p_brand = Utf8(\"Brand#12\") AND #lineitem.l_quantity >= Decimal128(Some(100),15,2) AND #lineitem.l_quantity <= Decimal128(Some(1100),15,2) AND #part.p_size <= Int32(5) OR #part.p_brand = Utf8(\"Brand#23\") AND #lineitem.l_quantity >= Decimal128(Some(1000),15,2) AND #lineitem.l_quantity <= Decimal128(Some(2000),15,2) AND #part.p_size <= Int32(10) OR #part.p_brand = Utf8(\"Brand#34\") AND #lineitem.l_quantity >= Decimal128(Some(2000),15,2) AND #lineitem.l_quantity <= Decimal128(Some(3000),15,2) AND #part.p_size <= Int32(15) [l_partkey:Int64, l_quantity:Decimal128(15, 2), p_partkey:Int64, p_brand:Utf8, p_size:Int32]",
- " CrossJoin: [l_partkey:Int64, l_quantity:Decimal128(15, 2), p_partkey:Int64, p_brand:Utf8, p_size:Int32]",
+ " Projection: #part.p_size >= Int32(1) AS #part.p_size >= Int32(1)Int32(1)#part.p_size, #lineitem.l_partkey, #lineitem.l_quantity, #part.p_brand, #part.p_size [#part.p_size >= Int32(1)Int32(1)#part.p_size:Boolean;N, l_partkey:Int64, l_quantity:Decimal128(15, 2), p_brand:Utf8, p_size:Int32]",
+ " Filter: #part.p_brand = Utf8(\"Brand#12\") AND #lineitem.l_quantity >= Decimal128(Some(100),15,2) AND #lineitem.l_quantity <= Decimal128(Some(1100),15,2) AND #part.p_size <= Int32(5) OR #part.p_brand = Utf8(\"Brand#23\") AND #lineitem.l_quantity >= Decimal128(Some(1000),15,2) AND #lineitem.l_quantity <= Decimal128(Some(2000),15,2) AND #part.p_size <= Int32(10) OR #part.p_brand = Utf8(\"Brand#34\") AND #lineitem.l_quantity >= Decimal128(Some(2000),15,2) AND #lineitem.l_quantity <= Decimal128(Some(3000),15,2) AND #part.p_size <= Int32(15) [l_partkey:Int64, l_quantity:Decimal128(15, 2), p_partkey:Int64, p_brand:Utf8, p_size:Int32]",
+ " Inner Join: #lineitem.l_partkey = #part.p_partkey [l_partkey:Int64, l_quantity:Decimal128(15, 2), p_partkey:Int64, p_brand:Utf8, p_size:Int32]",
" TableScan: lineitem projection=[l_partkey, l_quantity] [l_partkey:Int64, l_quantity:Decimal128(15, 2)]",
" Filter: #part.p_size >= Int32(1) [p_partkey:Int64, p_brand:Utf8, p_size:Int32]",
" TableScan: part projection=[p_partkey, p_brand, p_size], partial_filters=[#part.p_size >= Int32(1)] [p_partkey:Int64, p_brand:Utf8, p_size:Int32]",
@@ -443,3 +443,76 @@ async fn multiple_or_predicates() -> Result<()> {
);
Ok(())
}
+
+// Fix for issue#78 join predicates from inside of OR expr also pulled up properly.
+#[tokio::test]
+async fn tpch_q19_pull_predicates_to_innerjoin_simplified() -> Result<()> {
+ let ctx = SessionContext::new();
+
+ register_tpch_csv(&ctx, "part").await?;
+ register_tpch_csv(&ctx, "lineitem").await?;
+
+ let partsupp = r#"63700,7311,100,993.49,ven ideas. quickly even packages print. pending multipliers must have to are fluff"#;
+ register_tpch_csv_data(&ctx, "partsupp", partsupp).await?;
+
+ let sql = r#"
+select
+ p_partkey,
+ sum(l_extendedprice),
+ avg(l_discount),
+ count(distinct ps_suppkey)
+from
+ lineitem,
+ part,
+ partsupp
+where
+ (
+ p_partkey = l_partkey
+ and p_brand = 'Brand#12'
+ and p_partkey = ps_partkey
+ )
+ or
+ (
+ p_partkey = l_partkey
+ and p_brand = 'Brand#23'
+ and ps_partkey = p_partkey
+ )
+ group by p_partkey
+ ;"#;
+
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let plan = state.optimize(&plan)?;
+ let formatted = plan.display_indent_schema().to_string();
+ let actual: Vec<&str> = formatted.trim().lines().collect();
+ let expected =vec![
+ "Projection: #part.p_partkey, #SUM(lineitem.l_extendedprice), #AVG(lineitem.l_discount), #COUNT(DISTINCT partsupp.ps_suppkey) [p_partkey:Int64, SUM(lineitem.l_extendedprice):Decimal128(25, 2);N, AVG(lineitem.l_discount):Decimal128(19, 6);N, COUNT(DISTINCT partsupp.ps_suppkey):Int64;N]",
+ " Aggregate: groupBy=[[#part.p_partkey]], aggr=[[SUM(#lineitem.l_extendedprice), AVG(#lineitem.l_discount), COUNT(DISTINCT #partsupp.ps_suppkey)]] [p_partkey:Int64, SUM(lineitem.l_extendedprice):Decimal128(25, 2);N, AVG(lineitem.l_discount):Decimal128(19, 6);N, COUNT(DISTINCT partsupp.ps_suppkey):Int64;N]",
+ " Inner Join: #part.p_partkey = #partsupp.ps_partkey [l_partkey:Int64, l_extendedprice:Decimal128(15, 2), l_discount:Decimal128(15, 2), p_partkey:Int64, p_brand:Utf8, ps_partkey:Int64, ps_suppkey:Int64]",
+ " Inner Join: #lineitem.l_partkey = #part.p_partkey [l_partkey:Int64, l_extendedprice:Decimal128(15, 2), l_discount:Decimal128(15, 2), p_partkey:Int64, p_brand:Utf8]",
+ " TableScan: lineitem projection=[l_partkey, l_extendedprice, l_discount] [l_partkey:Int64, l_extendedprice:Decimal128(15, 2), l_discount:Decimal128(15, 2)]",
+ " Filter: #part.p_brand = Utf8(\"Brand#12\") OR #part.p_brand = Utf8(\"Brand#23\") [p_partkey:Int64, p_brand:Utf8]",
+ " TableScan: part projection=[p_partkey, p_brand], partial_filters=[#part.p_brand = Utf8(\"Brand#12\") OR #part.p_brand = Utf8(\"Brand#23\")] [p_partkey:Int64, p_brand:Utf8]",
+ " TableScan: partsupp projection=[ps_partkey, ps_suppkey] [ps_partkey:Int64, ps_suppkey:Int64]",
+ ];
+
+ assert_eq!(
+ expected, actual,
+ "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
+ expected, actual
+ );
+
+ // assert data
+ let results = execute_to_batches(&ctx, sql).await;
+ let expected = vec![
+ "+-----------+-------------------------------+--------------------------+-------------------------------------+",
+ "| p_partkey | SUM(lineitem.l_extendedprice) | AVG(lineitem.l_discount) | COUNT(DISTINCT partsupp.ps_suppkey) |",
+ "+-----------+-------------------------------+--------------------------+-------------------------------------+",
+ "| 63700 | 13309.60 | 0.100000 | 1 |",
+ "+-----------+-------------------------------+--------------------------+-------------------------------------+",
+ ];
+ assert_batches_eq!(expected, &results);
+
+ Ok(())
+}
diff --git a/datafusion/optimizer/src/lib.rs b/datafusion/optimizer/src/lib.rs
index 1713816599..bfb5634364 100644
--- a/datafusion/optimizer/src/lib.rs
+++ b/datafusion/optimizer/src/lib.rs
@@ -26,6 +26,7 @@ pub mod filter_push_down;
pub mod limit_push_down;
pub mod optimizer;
pub mod projection_push_down;
+pub mod reduce_cross_join;
pub mod reduce_outer_join;
pub mod scalar_subquery_to_join;
pub mod simplify_expressions;
diff --git a/datafusion/optimizer/src/reduce_cross_join.rs b/datafusion/optimizer/src/reduce_cross_join.rs
new file mode 100644
index 0000000000..3fce05a472
--- /dev/null
+++ b/datafusion/optimizer/src/reduce_cross_join.rs
@@ -0,0 +1,950 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Optimizer rule to reduce cross join to inner join if join predicates are available in filters.
+use crate::{OptimizerConfig, OptimizerRule};
+use datafusion_common::{Column, Result};
+use datafusion_expr::{
+ and,
+ logical_plan::{CrossJoin, Filter, Join, JoinType, LogicalPlan},
+ or,
+ utils::can_hash,
+ utils::from_plan,
+};
+use datafusion_expr::{Expr, Operator};
+
+use std::collections::HashSet;
+
+//use std::collections::HashMap;
+use datafusion_expr::logical_plan::JoinConstraint;
+use std::sync::Arc;
+
+#[derive(Default)]
+pub struct ReduceCrossJoin;
+
+impl ReduceCrossJoin {
+ #[allow(missing_docs)]
+ pub fn new() -> Self {
+ Self {}
+ }
+}
+
+impl OptimizerRule for ReduceCrossJoin {
+ fn optimize(
+ &self,
+ plan: &LogicalPlan,
+ _optimizer_config: &mut OptimizerConfig,
+ ) -> Result<LogicalPlan> {
+ let mut possible_join_keys: Vec<(Column, Column)> = vec![];
+ let mut all_join_keys = HashSet::new();
+
+ reduce_cross_join(self, plan, &mut possible_join_keys, &mut all_join_keys)
+ }
+
+ fn name(&self) -> &str {
+ "reduce_cross_join"
+ }
+}
+
+/// Attempt to reduce cross joins to inner joins.
+/// for queries:
+/// 'select ... from a, b where a.x = b.y and b.xx = 100;'
+/// 'select ... from a, b where (a.x = b.y and b.xx = 100) or (a.x = b.y and b.xx = 200);'
+/// 'select ... from a, b, c where (a.x = b.y and b.xx = 100 and a.z = c.z)
+/// or (a.x = b.y and b.xx = 200 and a.z=c.z);'
+/// For above queries, the join predicate is available in filters and they are moved to
+/// join nodes appropriately
+/// This fix helps to improve the performance of TPCH Q19. issue#78
+///
+fn reduce_cross_join(
+ _optimizer: &ReduceCrossJoin,
+ plan: &LogicalPlan,
+ possible_join_keys: &mut Vec<(Column, Column)>,
+ all_join_keys: &mut HashSet<(Column, Column)>,
+) -> Result<LogicalPlan> {
+ match plan {
+ LogicalPlan::Filter(Filter { input, predicate }) => {
+ // join keys are handled locally
+ let mut new_possible_join_keys: Vec<(Column, Column)> = vec![];
+ let mut new_all_join_keys = HashSet::new();
+
+ extract_possible_join_keys(predicate, &mut new_possible_join_keys);
+
+ let new_plan = reduce_cross_join(
+ _optimizer,
+ input,
+ &mut new_possible_join_keys,
+ &mut new_all_join_keys,
+ )?;
+
+ // if there are no join keys then do nothing.
+ if new_all_join_keys.is_empty() {
+ Ok(LogicalPlan::Filter(Filter {
+ predicate: predicate.clone(),
+ input: Arc::new(new_plan),
+ }))
+ } else {
+ // remove join expressions from filter
+ match remove_join_expressions(predicate, &new_all_join_keys)? {
+ Some(filter_expr) => Ok(LogicalPlan::Filter(Filter {
+ predicate: filter_expr,
+ input: Arc::new(new_plan),
+ })),
+ _ => Ok(new_plan),
+ }
+ }
+ }
+ LogicalPlan::CrossJoin(cross_join) => {
+ let left_plan = reduce_cross_join(
+ _optimizer,
+ &cross_join.left,
+ possible_join_keys,
+ all_join_keys,
+ )?;
+ let right_plan = reduce_cross_join(
+ _optimizer,
+ &cross_join.right,
+ possible_join_keys,
+ all_join_keys,
+ )?;
+ // can we find a match?
+ let left_schema = left_plan.schema();
+ let right_schema = right_plan.schema();
+ let mut join_keys = vec![];
+
+ for (l, r) in possible_join_keys {
+ if left_schema.field_from_column(l).is_ok()
+ && right_schema.field_from_column(r).is_ok()
+ && can_hash(left_schema.field_from_column(l).unwrap().data_type())
+ {
+ join_keys.push((l.clone(), r.clone()));
+ } else if left_schema.field_from_column(r).is_ok()
+ && right_schema.field_from_column(l).is_ok()
+ && can_hash(left_schema.field_from_column(r).unwrap().data_type())
+ {
+ join_keys.push((r.clone(), l.clone()));
+ }
+ }
+
+ // if there are no join keys then do nothing.
+ if join_keys.is_empty() {
+ Ok(LogicalPlan::CrossJoin(CrossJoin {
+ left: Arc::new(left_plan),
+ right: Arc::new(right_plan),
+ schema: cross_join.schema.clone(),
+ }))
+ } else {
+ // Keep track of join keys being pushed to Join nodes
+ all_join_keys.extend(join_keys.clone());
+
+ Ok(LogicalPlan::Join(Join {
+ left: Arc::new(left_plan),
+ right: Arc::new(right_plan),
+ join_type: JoinType::Inner,
+ join_constraint: JoinConstraint::On,
+ on: join_keys,
+ filter: None,
+ schema: cross_join.schema.clone(),
+ null_equals_null: false,
+ }))
+ }
+ }
+ _ => {
+ let expr = plan.expressions();
+
+ // apply the optimization to all inputs of the plan
+ let inputs = plan.inputs();
+ let new_inputs = inputs
+ .iter()
+ .map(|plan| {
+ reduce_cross_join(_optimizer, plan, possible_join_keys, all_join_keys)
+ })
+ .collect::<Result<Vec<_>>>()?;
+
+ from_plan(plan, &expr, &new_inputs)
+ }
+ }
+}
+
+fn intersect(
+ accum: &mut Vec<(Column, Column)>,
+ vec1: &[(Column, Column)],
+ vec2: &[(Column, Column)],
+) {
+ for x1 in vec1.iter() {
+ for x2 in vec2.iter() {
+ if x1.0 == x2.0 && x1.1 == x2.1 || x1.1 == x2.0 && x1.0 == x2.1 {
+ accum.push((x1.0.clone(), x1.1.clone()));
+ }
+ }
+ }
+}
+
+/// Extract join keys from a WHERE clause
+fn extract_possible_join_keys(expr: &Expr, accum: &mut Vec<(Column, Column)>) {
+ if let Expr::BinaryExpr { left, op, right } = expr {
+ match op {
+ Operator::Eq => {
+ if let (Expr::Column(l), Expr::Column(r)) =
+ (left.as_ref(), right.as_ref())
+ {
+ // Ensure that we don't add the same Join keys multiple times
+ if !(accum.contains(&(l.clone(), r.clone()))
+ || accum.contains(&(r.clone(), l.clone())))
+ {
+ accum.push((l.clone(), r.clone()));
+ }
+ }
+ }
+ Operator::And => {
+ extract_possible_join_keys(left, accum);
+ extract_possible_join_keys(right, accum)
+ }
+ // Fix for issue#78 join predicates from inside of OR expr also pulled up properly.
+ Operator::Or => {
+ let mut left_join_keys = vec![];
+ let mut right_join_keys = vec![];
+
+ extract_possible_join_keys(left, &mut left_join_keys);
+ extract_possible_join_keys(right, &mut right_join_keys);
+
+ intersect(accum, &left_join_keys, &right_join_keys)
+ }
+ _ => (),
+ }
+ }
+}
+
+/// Remove join expressions from a filter expression
+/// Returns Some() when there are few remaining predicates in filter_expr
+/// Returns None otherwise
+fn remove_join_expressions(
+ expr: &Expr,
+ join_columns: &HashSet<(Column, Column)>,
+) -> Result<Option<Expr>> {
+ match expr {
+ Expr::BinaryExpr { left, op, right } => match op {
+ Operator::Eq => match (left.as_ref(), right.as_ref()) {
+ (Expr::Column(l), Expr::Column(r)) => {
+ if join_columns.contains(&(l.clone(), r.clone()))
+ || join_columns.contains(&(r.clone(), l.clone()))
+ {
+ Ok(None)
+ } else {
+ Ok(Some(expr.clone()))
+ }
+ }
+ _ => Ok(Some(expr.clone())),
+ },
+ Operator::And => {
+ let l = remove_join_expressions(left, join_columns)?;
+ let r = remove_join_expressions(right, join_columns)?;
+ match (l, r) {
+ (Some(ll), Some(rr)) => Ok(Some(and(ll, rr))),
+ (Some(ll), _) => Ok(Some(ll)),
+ (_, Some(rr)) => Ok(Some(rr)),
+ _ => Ok(None),
+ }
+ }
+ // Fix for issue#78 join predicates from inside of OR expr also pulled up properly.
+ Operator::Or => {
+ let l = remove_join_expressions(left, join_columns)?;
+ let r = remove_join_expressions(right, join_columns)?;
+ match (l, r) {
+ (Some(ll), Some(rr)) => Ok(Some(or(ll, rr))),
+ (Some(ll), _) => Ok(Some(ll)),
+ (_, Some(rr)) => Ok(Some(rr)),
+ _ => Ok(None),
+ }
+ }
+ _ => Ok(Some(expr.clone())),
+ },
+ _ => Ok(Some(expr.clone())),
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::test::*;
+ use datafusion_expr::{
+ binary_expr, col, lit,
+ logical_plan::builder::LogicalPlanBuilder,
+ Operator::{And, Or},
+ };
+
+ fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: Vec<&str>) {
+ let rule = ReduceCrossJoin::new();
+ let optimized_plan = rule
+ .optimize(plan, &mut OptimizerConfig::new())
+ .expect("failed to optimize plan");
+ let formatted = optimized_plan.display_indent_schema().to_string();
+ let actual: Vec<&str> = formatted.trim().lines().collect();
+
+ assert_eq!(
+ expected, actual,
+ "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
+ expected, actual
+ );
+
+ assert_eq!(plan.schema(), optimized_plan.schema())
+ }
+
+ #[test]
+ fn reduce_cross_with_simple_and() -> Result<()> {
+ let t1 = test_table_scan_with_name("t1")?;
+ let t2 = test_table_scan_with_name("t2")?;
+
+ // could reduce to inner join since filter has Join predicates
+ let plan = LogicalPlanBuilder::from(t1)
+ .cross_join(&t2)?
+ .filter(binary_expr(
+ col("t1.a").eq(col("t2.a")),
+ And,
+ col("t2.c").lt(lit(20u32)),
+ ))?
+ .build()?;
+
+ let expected =vec![
+ "Filter: #t2.c < UInt32(20) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t1.a = #t2.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t1 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t2 [a:UInt32, b:UInt32, c:UInt32]",
+ ];
+
+ assert_optimized_plan_eq(&plan, expected);
+
+ Ok(())
+ }
+
+ #[test]
+ fn reduce_cross_with_simple_or() -> Result<()> {
+ let t1 = test_table_scan_with_name("t1")?;
+ let t2 = test_table_scan_with_name("t2")?;
+
+ // could not reduce to inner join since filter OR expression and there is no common
+ // Join predicates in left and right of OR expr.
+ let plan = LogicalPlanBuilder::from(t1)
+ .cross_join(&t2)?
+ .filter(binary_expr(
+ col("t1.a").eq(col("t2.a")),
+ Or,
+ col("t2.b").eq(col("t1.a")),
+ ))?
+ .build()?;
+
+ let expected =vec![
+ "Filter: #t1.a = #t2.a OR #t2.b = #t1.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " CrossJoin: [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t1 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t2 [a:UInt32, b:UInt32, c:UInt32]",
+ ];
+
+ assert_optimized_plan_eq(&plan, expected);
+
+ Ok(())
+ }
+
+ #[test]
+ fn reduce_cross_with_and() -> Result<()> {
+ let t1 = test_table_scan_with_name("t1")?;
+ let t2 = test_table_scan_with_name("t2")?;
+
+ // could reduce to inner join
+ let plan = LogicalPlanBuilder::from(t1)
+ .cross_join(&t2)?
+ .filter(binary_expr(
+ binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").lt(lit(20u32))),
+ And,
+ binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").eq(lit(10u32))),
+ ))?
+ .build()?;
+
+ let expected =vec![
+ "Filter: #t2.c < UInt32(20) AND #t2.c = UInt32(10) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t1.a = #t2.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t1 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t2 [a:UInt32, b:UInt32, c:UInt32]",
+ ];
+
+ assert_optimized_plan_eq(&plan, expected);
+
+ Ok(())
+ }
+
+ #[test]
+ fn reduce_cross_with_or() -> Result<()> {
+ let t1 = test_table_scan_with_name("t1")?;
+ let t2 = test_table_scan_with_name("t2")?;
+
+ // could reduce to inner join since Or predicates have common Join predicates
+ let plan = LogicalPlanBuilder::from(t1)
+ .cross_join(&t2)?
+ .filter(binary_expr(
+ binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").lt(lit(15u32))),
+ Or,
+ binary_expr(
+ col("t1.a").eq(col("t2.a")),
+ And,
+ col("t2.c").eq(lit(688u32)),
+ ),
+ ))?
+ .build()?;
+
+ let expected =vec![
+ "Filter: #t2.c < UInt32(15) OR #t2.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t1.a = #t2.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t1 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t2 [a:UInt32, b:UInt32, c:UInt32]",
+ ];
+ assert_optimized_plan_eq(&plan, expected);
+
+ Ok(())
+ }
+
+ #[test]
+ fn reduce_cross_not_possible_simple() -> Result<()> {
+ let t1 = test_table_scan_with_name("t1")?;
+ let t2 = test_table_scan_with_name("t2")?;
+
+ // could not reduce to inner join
+ let plan = LogicalPlanBuilder::from(t1)
+ .cross_join(&t2)?
+ .filter(binary_expr(
+ binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").lt(lit(15u32))),
+ Or,
+ binary_expr(
+ col("t1.b").eq(col("t2.b")),
+ And,
+ col("t2.c").eq(lit(688u32)),
+ ),
+ ))?
+ .build()?;
+
+ let expected =vec![
+ "Filter: #t1.a = #t2.a AND #t2.c < UInt32(15) OR #t1.b = #t2.b AND #t2.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " CrossJoin: [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t1 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t2 [a:UInt32, b:UInt32, c:UInt32]",
+ ];
+ assert_optimized_plan_eq(&plan, expected);
+
+ Ok(())
+ }
+
+ #[test]
+ fn reduce_cross_not_possible() -> Result<()> {
+ let t1 = test_table_scan_with_name("t1")?;
+ let t2 = test_table_scan_with_name("t2")?;
+
+ // could not reduce to inner join
+ let plan = LogicalPlanBuilder::from(t1)
+ .cross_join(&t2)?
+ .filter(binary_expr(
+ binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").lt(lit(15u32))),
+ Or,
+ binary_expr(col("t1.a").eq(col("t2.a")), Or, col("t2.c").eq(lit(688u32))),
+ ))?
+ .build()?;
+
+ let expected =vec![
+ "Filter: #t1.a = #t2.a AND #t2.c < UInt32(15) OR #t1.a = #t2.a OR #t2.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " CrossJoin: [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t1 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t2 [a:UInt32, b:UInt32, c:UInt32]",
+ ];
+ assert_optimized_plan_eq(&plan, expected);
+
+ Ok(())
+ }
+
+ #[test]
+ fn reduce_cross_join_multi_tables() -> Result<()> {
+ let t1 = test_table_scan_with_name("t1")?;
+ let t2 = test_table_scan_with_name("t2")?;
+ let t3 = test_table_scan_with_name("t3")?;
+ let t4 = test_table_scan_with_name("t4")?;
+
+ // could reduce to inner join
+ let plan1 = LogicalPlanBuilder::from(t1)
+ .cross_join(&t2)?
+ .filter(binary_expr(
+ binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").lt(lit(15u32))),
+ Or,
+ binary_expr(
+ col("t1.a").eq(col("t2.a")),
+ And,
+ col("t2.c").eq(lit(688u32)),
+ ),
+ ))?
+ .build()?;
+
+ let plan2 = LogicalPlanBuilder::from(t3)
+ .cross_join(&t4)?
+ .filter(binary_expr(
+ binary_expr(
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t4.c").lt(lit(15u32)),
+ ),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t3.c").eq(lit(688u32)),
+ ),
+ ),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t3.b").eq(col("t4.b")),
+ ),
+ ))?
+ .build()?;
+
+ let plan = LogicalPlanBuilder::from(plan1)
+ .cross_join(&plan2)?
+ .filter(binary_expr(
+ binary_expr(col("t3.a").eq(col("t1.a")), And, col("t4.c").lt(lit(15u32))),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t1.a")),
+ And,
+ col("t4.c").eq(lit(688u32)),
+ ),
+ ))?
+ .build()?;
+
+ let expected =vec![
+ "Filter: #t4.c < UInt32(15) OR #t4.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t1.a = #t3.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Filter: #t2.c < UInt32(15) OR #t2.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t1.a = #t2.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t1 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t2 [a:UInt32, b:UInt32, c:UInt32]",
+ " Filter: #t4.c < UInt32(15) OR #t3.c = UInt32(688) OR #t3.b = #t4.b [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t3.a = #t4.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t3 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t4 [a:UInt32, b:UInt32, c:UInt32]",
+ ];
+
+ assert_optimized_plan_eq(&plan, expected);
+
+ Ok(())
+ }
+
+ #[test]
+ fn reduce_cross_join_multi_tables_1() -> Result<()> {
+ let t1 = test_table_scan_with_name("t1")?;
+ let t2 = test_table_scan_with_name("t2")?;
+ let t3 = test_table_scan_with_name("t3")?;
+ let t4 = test_table_scan_with_name("t4")?;
+
+ // could reduce to inner join
+ let plan1 = LogicalPlanBuilder::from(t1)
+ .cross_join(&t2)?
+ .filter(binary_expr(
+ binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").lt(lit(15u32))),
+ Or,
+ binary_expr(
+ col("t1.a").eq(col("t2.a")),
+ And,
+ col("t2.c").eq(lit(688u32)),
+ ),
+ ))?
+ .build()?;
+
+ // could reduce to inner join
+ let plan2 = LogicalPlanBuilder::from(t3)
+ .cross_join(&t4)?
+ .filter(binary_expr(
+ binary_expr(
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t4.c").lt(lit(15u32)),
+ ),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t3.c").eq(lit(688u32)),
+ ),
+ ),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t3.b").eq(col("t4.b")),
+ ),
+ ))?
+ .build()?;
+
+ // could not reduce to inner join
+ let plan = LogicalPlanBuilder::from(plan1)
+ .cross_join(&plan2)?
+ .filter(binary_expr(
+ binary_expr(col("t3.a").eq(col("t1.a")), And, col("t4.c").lt(lit(15u32))),
+ Or,
+ binary_expr(col("t3.a").eq(col("t1.a")), Or, col("t4.c").eq(lit(688u32))),
+ ))?
+ .build()?;
+
+ let expected =vec![
+ "Filter: #t3.a = #t1.a AND #t4.c < UInt32(15) OR #t3.a = #t1.a OR #t4.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " CrossJoin: [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Filter: #t2.c < UInt32(15) OR #t2.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t1.a = #t2.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t1 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t2 [a:UInt32, b:UInt32, c:UInt32]",
+ " Filter: #t4.c < UInt32(15) OR #t3.c = UInt32(688) OR #t3.b = #t4.b [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t3.a = #t4.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t3 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t4 [a:UInt32, b:UInt32, c:UInt32]",
+ ];
+
+ assert_optimized_plan_eq(&plan, expected);
+
+ Ok(())
+ }
+
+ #[test]
+ fn reduce_cross_join_multi_tables_2() -> Result<()> {
+ let t1 = test_table_scan_with_name("t1")?;
+ let t2 = test_table_scan_with_name("t2")?;
+ let t3 = test_table_scan_with_name("t3")?;
+ let t4 = test_table_scan_with_name("t4")?;
+
+ // could reduce to inner join
+ let plan1 = LogicalPlanBuilder::from(t1)
+ .cross_join(&t2)?
+ .filter(binary_expr(
+ binary_expr(col("t1.a").eq(col("t2.a")), And, col("t2.c").lt(lit(15u32))),
+ Or,
+ binary_expr(
+ col("t1.a").eq(col("t2.a")),
+ And,
+ col("t2.c").eq(lit(688u32)),
+ ),
+ ))?
+ .build()?;
+
+ // could not reduce to inner join
+ let plan2 = LogicalPlanBuilder::from(t3)
+ .cross_join(&t4)?
+ .filter(binary_expr(
+ binary_expr(
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t4.c").lt(lit(15u32)),
+ ),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t3.c").eq(lit(688u32)),
+ ),
+ ),
+ Or,
+ binary_expr(col("t3.a").eq(col("t4.a")), Or, col("t3.b").eq(col("t4.b"))),
+ ))?
+ .build()?;
+
+ // could reduce to inner join
+ let plan = LogicalPlanBuilder::from(plan1)
+ .cross_join(&plan2)?
+ .filter(binary_expr(
+ binary_expr(col("t3.a").eq(col("t1.a")), And, col("t4.c").lt(lit(15u32))),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t1.a")),
+ And,
+ col("t4.c").eq(lit(688u32)),
+ ),
+ ))?
+ .build()?;
+
+ let expected =vec![
+ "Filter: #t4.c < UInt32(15) OR #t4.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t1.a = #t3.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Filter: #t2.c < UInt32(15) OR #t2.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t1.a = #t2.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t1 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t2 [a:UInt32, b:UInt32, c:UInt32]",
+ " Filter: #t3.a = #t4.a AND #t4.c < UInt32(15) OR #t3.a = #t4.a AND #t3.c = UInt32(688) OR #t3.a = #t4.a OR #t3.b = #t4.b [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " CrossJoin: [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t3 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t4 [a:UInt32, b:UInt32, c:UInt32]",
+ ];
+
+ assert_optimized_plan_eq(&plan, expected);
+
+ Ok(())
+ }
+
+ #[test]
+ fn reduce_cross_join_multi_tables_3() -> Result<()> {
+ let t1 = test_table_scan_with_name("t1")?;
+ let t2 = test_table_scan_with_name("t2")?;
+ let t3 = test_table_scan_with_name("t3")?;
+ let t4 = test_table_scan_with_name("t4")?;
+
+ // could not reduce to inner join
+ let plan1 = LogicalPlanBuilder::from(t1)
+ .cross_join(&t2)?
+ .filter(binary_expr(
+ binary_expr(col("t1.a").eq(col("t2.a")), Or, col("t2.c").lt(lit(15u32))),
+ Or,
+ binary_expr(
+ col("t1.a").eq(col("t2.a")),
+ And,
+ col("t2.c").eq(lit(688u32)),
+ ),
+ ))?
+ .build()?;
+
+ // could reduce to inner join
+ let plan2 = LogicalPlanBuilder::from(t3)
+ .cross_join(&t4)?
+ .filter(binary_expr(
+ binary_expr(
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t4.c").lt(lit(15u32)),
+ ),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t3.c").eq(lit(688u32)),
+ ),
+ ),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t3.b").eq(col("t4.b")),
+ ),
+ ))?
+ .build()?;
+
+ // could reduce to inner join
+ let plan = LogicalPlanBuilder::from(plan1)
+ .cross_join(&plan2)?
+ .filter(binary_expr(
+ binary_expr(col("t3.a").eq(col("t1.a")), And, col("t4.c").lt(lit(15u32))),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t1.a")),
+ And,
+ col("t4.c").eq(lit(688u32)),
+ ),
+ ))?
+ .build()?;
+
+ let expected =vec![
+ "Filter: #t4.c < UInt32(15) OR #t4.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t1.a = #t3.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Filter: #t1.a = #t2.a OR #t2.c < UInt32(15) OR #t1.a = #t2.a AND #t2.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " CrossJoin: [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t1 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t2 [a:UInt32, b:UInt32, c:UInt32]",
+ " Filter: #t4.c < UInt32(15) OR #t3.c = UInt32(688) OR #t3.b = #t4.b [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t3.a = #t4.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t3 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t4 [a:UInt32, b:UInt32, c:UInt32]",
+ ];
+
+ assert_optimized_plan_eq(&plan, expected);
+
+ Ok(())
+ }
+
+ #[test]
+ fn reduce_cross_join_multi_tables_4() -> Result<()> {
+ let t1 = test_table_scan_with_name("t1")?;
+ let t2 = test_table_scan_with_name("t2")?;
+ let t3 = test_table_scan_with_name("t3")?;
+ let t4 = test_table_scan_with_name("t4")?;
+
+ // could reduce to inner join
+ let plan1 = LogicalPlanBuilder::from(t1)
+ .cross_join(&t2)?
+ .filter(binary_expr(
+ binary_expr(col("t1.a").eq(col("t2.a")), Or, col("t2.c").lt(lit(15u32))),
+ And,
+ binary_expr(
+ col("t1.a").eq(col("t2.a")),
+ And,
+ col("t2.c").eq(lit(688u32)),
+ ),
+ ))?
+ .build()?;
+
+ // could reduce to inner join
+ let plan2 = LogicalPlanBuilder::from(t3).cross_join(&t4)?.build()?;
+
+ // could reduce to inner join
+ let plan = LogicalPlanBuilder::from(plan1)
+ .cross_join(&plan2)?
+ .filter(binary_expr(
+ binary_expr(
+ binary_expr(
+ col("t3.a").eq(col("t1.a")),
+ And,
+ col("t4.c").lt(lit(15u32)),
+ ),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t1.a")),
+ And,
+ col("t4.c").eq(lit(688u32)),
+ ),
+ ),
+ And,
+ binary_expr(
+ binary_expr(
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t4.c").lt(lit(15u32)),
+ ),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t3.c").eq(lit(688u32)),
+ ),
+ ),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t3.b").eq(col("t4.b")),
+ ),
+ ),
+ ))?
+ .build()?;
+
+ let expected =vec![
+ "Filter: #t4.c < UInt32(15) OR #t4.c = UInt32(688) AND #t4.c < UInt32(15) OR #t3.c = UInt32(688) OR #t3.b = #t4.b [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t1.a = #t3.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Filter: #t2.c < UInt32(15) AND #t2.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t1.a = #t2.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t1 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t2 [a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t3.a = #t4.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t3 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t4 [a:UInt32, b:UInt32, c:UInt32]",
+ ];
+
+ assert_optimized_plan_eq(&plan, expected);
+
+ Ok(())
+ }
+
+ #[test]
+ fn reduce_cross_join_multi_tables_5() -> Result<()> {
+ let t1 = test_table_scan_with_name("t1")?;
+ let t2 = test_table_scan_with_name("t2")?;
+ let t3 = test_table_scan_with_name("t3")?;
+ let t4 = test_table_scan_with_name("t4")?;
+
+ // could reduce to inner join
+ let plan1 = LogicalPlanBuilder::from(t1).cross_join(&t2)?.build()?;
+
+ // could reduce to inner join
+ let plan2 = LogicalPlanBuilder::from(t3).cross_join(&t4)?.build()?;
+
+ // could reduce to inner join
+ let plan = LogicalPlanBuilder::from(plan1)
+ .cross_join(&plan2)?
+ .filter(binary_expr(
+ binary_expr(
+ binary_expr(
+ binary_expr(
+ col("t3.a").eq(col("t1.a")),
+ And,
+ col("t4.c").lt(lit(15u32)),
+ ),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t1.a")),
+ And,
+ col("t4.c").eq(lit(688u32)),
+ ),
+ ),
+ And,
+ binary_expr(
+ binary_expr(
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t4.c").lt(lit(15u32)),
+ ),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t3.c").eq(lit(688u32)),
+ ),
+ ),
+ Or,
+ binary_expr(
+ col("t3.a").eq(col("t4.a")),
+ And,
+ col("t3.b").eq(col("t4.b")),
+ ),
+ ),
+ ),
+ And,
+ binary_expr(
+ binary_expr(
+ col("t1.a").eq(col("t2.a")),
+ Or,
+ col("t2.c").lt(lit(15u32)),
+ ),
+ And,
+ binary_expr(
+ col("t1.a").eq(col("t2.a")),
+ And,
+ col("t2.c").eq(lit(688u32)),
+ ),
+ ),
+ ))?
+ .build()?;
+
+ let expected =vec![
+ "Filter: #t4.c < UInt32(15) OR #t4.c = UInt32(688) AND #t4.c < UInt32(15) OR #t3.c = UInt32(688) OR #t3.b = #t4.b AND #t2.c < UInt32(15) AND #t2.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t1.a = #t3.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t1.a = #t2.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t1 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t2 [a:UInt32, b:UInt32, c:UInt32]",
+ " Inner Join: #t3.a = #t4.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t3 [a:UInt32, b:UInt32, c:UInt32]",
+ " TableScan: t4 [a:UInt32, b:UInt32, c:UInt32]",
+ ];
+
+ assert_optimized_plan_eq(&plan, expected);
+
+ Ok(())
+ }
+}
diff --git a/datafusion/optimizer/tests/integration-test.rs b/datafusion/optimizer/tests/integration-test.rs
index cb31600b97..cb171780cd 100644
--- a/datafusion/optimizer/tests/integration-test.rs
+++ b/datafusion/optimizer/tests/integration-test.rs
@@ -29,6 +29,7 @@ use datafusion_optimizer::limit_push_down::LimitPushDown;
use datafusion_optimizer::optimizer::Optimizer;
use datafusion_optimizer::pre_cast_lit_in_comparison::PreCastLitInComparisonExpressions;
use datafusion_optimizer::projection_push_down::ProjectionPushDown;
+use datafusion_optimizer::reduce_cross_join::ReduceCrossJoin;
use datafusion_optimizer::reduce_outer_join::ReduceOuterJoin;
use datafusion_optimizer::rewrite_disjunctive_predicate::RewriteDisjunctivePredicate;
use datafusion_optimizer::scalar_subquery_to_join::ScalarSubqueryToJoin;
@@ -115,6 +116,7 @@ fn test_sql(sql: &str) -> Result<LogicalPlan> {
Arc::new(EliminateFilter::new()),
Arc::new(CommonSubexprEliminate::new()),
Arc::new(EliminateLimit::new()),
+ Arc::new(ReduceCrossJoin::new()),
Arc::new(ProjectionPushDown::new()),
Arc::new(RewriteDisjunctivePredicate::new()),
Arc::new(FilterNullJoinKeys::default()),