Skip to content

Apache DataFusion SQL Query Engine

Refactor joins test to sqllogic (#6525)

Armin Primadi365336259eea
diff --git a/datafusion/core/tests/sql/joins.rs b/datafusion/core/tests/sql/joins.rs
index 118aeb043e..054db2c3f7 100644
--- a/datafusion/core/tests/sql/joins.rs
+++ b/datafusion/core/tests/sql/joins.rs
@@ -17,160 +17,6 @@
use super::*;
-#[tokio::test]
-async fn test_join_timestamp() -> Result<()> {
- let ctx = SessionContext::new();
-
- // register time table
- let timestamp_schema = Arc::new(Schema::new(vec![Field::new(
- "time",
- DataType::Timestamp(TimeUnit::Nanosecond, None),
- true,
- )]));
- let timestamp_data = RecordBatch::try_new(
- timestamp_schema.clone(),
- vec![Arc::new(TimestampNanosecondArray::from(vec![
- 131964190213133,
- 131964190213134,
- 131964190213135,
- ]))],
- )?;
- ctx.register_batch("timestamp", timestamp_data)?;
-
- let sql = "SELECT * \
- FROM timestamp as a \
- JOIN (SELECT * FROM timestamp) as b \
- ON a.time = b.time \
- ORDER BY a.time";
- let actual = execute_to_batches(&ctx, sql).await;
-
- let expected = vec![
- "+-------------------------------+-------------------------------+",
- "| time | time |",
- "+-------------------------------+-------------------------------+",
- "| 1970-01-02T12:39:24.190213133 | 1970-01-02T12:39:24.190213133 |",
- "| 1970-01-02T12:39:24.190213134 | 1970-01-02T12:39:24.190213134 |",
- "| 1970-01-02T12:39:24.190213135 | 1970-01-02T12:39:24.190213135 |",
- "+-------------------------------+-------------------------------+",
- ];
- assert_batches_eq!(expected, &actual);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn test_join_float32() -> Result<()> {
- let ctx = SessionContext::new();
-
- // register population table
- let population_schema = Arc::new(Schema::new(vec![
- Field::new("city", DataType::Utf8, true),
- Field::new("population", DataType::Float32, true),
- ]));
- let population_data = RecordBatch::try_new(
- population_schema.clone(),
- vec![
- Arc::new(StringArray::from(vec![Some("a"), Some("b"), Some("c")])),
- Arc::new(Float32Array::from(vec![838.698, 1778.934, 626.443])),
- ],
- )?;
- ctx.register_batch("population", population_data)?;
-
- let sql = "SELECT * \
- FROM population as a \
- JOIN (SELECT * FROM population) as b \
- ON a.population = b.population \
- ORDER BY a.population";
- let actual = execute_to_batches(&ctx, sql).await;
-
- let expected = vec![
- "+------+------------+------+------------+",
- "| city | population | city | population |",
- "+------+------------+------+------------+",
- "| c | 626.443 | c | 626.443 |",
- "| a | 838.698 | a | 838.698 |",
- "| b | 1778.934 | b | 1778.934 |",
- "+------+------------+------+------------+",
- ];
- assert_batches_eq!(expected, &actual);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn test_join_float64() -> Result<()> {
- let ctx = SessionContext::new();
-
- // register population table
- let population_schema = Arc::new(Schema::new(vec![
- Field::new("city", DataType::Utf8, true),
- Field::new("population", DataType::Float64, true),
- ]));
- let population_data = RecordBatch::try_new(
- population_schema.clone(),
- vec![
- Arc::new(StringArray::from(vec![Some("a"), Some("b"), Some("c")])),
- Arc::new(Float64Array::from(vec![838.698, 1778.934, 626.443])),
- ],
- )?;
- ctx.register_batch("population", population_data)?;
-
- let sql = "SELECT * \
- FROM population as a \
- JOIN (SELECT * FROM population) as b \
- ON a.population = b.population \
- ORDER BY a.population";
- let actual = execute_to_batches(&ctx, sql).await;
-
- let expected = vec![
- "+------+------------+------+------------+",
- "| city | population | city | population |",
- "+------+------------+------+------------+",
- "| c | 626.443 | c | 626.443 |",
- "| a | 838.698 | a | 838.698 |",
- "| b | 1778.934 | b | 1778.934 |",
- "+------+------------+------+------------+",
- ];
- assert_batches_eq!(expected, &actual);
-
- Ok(())
-}
-
-// TODO Tests to prove correct implementation of INNER JOIN's with qualified names.
-// https://issues.apache.org/jira/projects/ARROW/issues/ARROW-11432.
-#[tokio::test]
-#[ignore]
-async fn inner_join_qualified_names() -> Result<()> {
- // Setup the statements that test qualified names function correctly.
- let equivalent_sql = [
- "SELECT t1.a, t1.b, t1.c, t2.a, t2.b, t2.c
- FROM t1
- INNER JOIN t2 ON t1.a = t2.a
- ORDER BY t1.a",
- "SELECT t1.a, t1.b, t1.c, t2.a, t2.b, t2.c
- FROM t1
- INNER JOIN t2 ON t2.a = t1.a
- ORDER BY t1.a",
- ];
-
- let expected = vec![
- "+---+----+----+---+-----+-----+",
- "| a | b | c | a | b | c |",
- "+---+----+----+---+-----+-----+",
- "| 1 | 10 | 50 | 1 | 100 | 500 |",
- "| 2 | 20 | 60 | 2 | 200 | 600 |",
- "| 4 | 40 | 80 | 4 | 400 | 800 |",
- "+---+----+----+---+-----+-----+",
- ];
-
- for sql in equivalent_sql.iter() {
- let ctx = create_join_context_qualified("t1", "t2")?;
- let actual = execute_to_batches(&ctx, sql).await;
- assert_batches_eq!(expected, &actual);
- }
- Ok(())
-}
-
#[tokio::test]
#[ignore]
/// TODO: need to repair. Wrong Test: ambiguous column name: a
@@ -191,84 +37,6 @@ async fn nestedjoin_with_alias() -> Result<()> {
Ok(())
}
-#[tokio::test]
-async fn nestedjoin_without_alias() -> Result<()> {
- let sql = "select * from (select 1 as a, 2 as b) c INNER JOIN (select 1 as a, 3 as d) e on c.a = e.a;";
- let expected = vec![
- "+---+---+---+---+",
- "| a | b | a | d |",
- "+---+---+---+---+",
- "| 1 | 2 | 1 | 3 |",
- "+---+---+---+---+",
- ];
- let ctx = SessionContext::new();
- let actual = execute_to_batches(&ctx, sql).await;
- assert_batches_eq!(expected, &actual);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn join_tables_with_duplicated_column_name_not_in_on_constraint() -> Result<()> {
- let ctx = SessionContext::new();
- let batch = RecordBatch::try_from_iter(vec![
- ("id", Arc::new(Int32Array::from(vec![1, 2, 3])) as _),
- (
- "country",
- Arc::new(StringArray::from(vec!["Germany", "Sweden", "Japan"])) as _,
- ),
- ])
- .unwrap();
- ctx.register_batch("countries", batch)?;
-
- let batch = RecordBatch::try_from_iter(vec![
- (
- "id",
- Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7])) as _,
- ),
- (
- "city",
- Arc::new(StringArray::from(vec![
- "Hamburg",
- "Stockholm",
- "Osaka",
- "Berlin",
- "Göteborg",
- "Tokyo",
- "Kyoto",
- ])) as _,
- ),
- (
- "country_id",
- Arc::new(Int32Array::from(vec![1, 2, 3, 1, 2, 3, 3])) as _,
- ),
- ])
- .unwrap();
-
- ctx.register_batch("cities", batch)?;
-
- // city.id is not in the on constraint, but the output result will contain both city.id and
- // country.id
- let sql = "SELECT t1.id, t2.id, t1.city, t2.country FROM cities AS t1 JOIN countries AS t2 ON t1.country_id = t2.id ORDER BY t1.id";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+----+----+-----------+---------+",
- "| id | id | city | country |",
- "+----+----+-----------+---------+",
- "| 1 | 1 | Hamburg | Germany |",
- "| 2 | 2 | Stockholm | Sweden |",
- "| 3 | 3 | Osaka | Japan |",
- "| 4 | 1 | Berlin | Germany |",
- "| 5 | 2 | Göteborg | Sweden |",
- "| 6 | 3 | Tokyo | Japan |",
- "| 7 | 3 | Kyoto | Japan |",
- "+----+----+-----------+---------+",
- ];
-
- assert_batches_eq!(expected, &actual);
- Ok(())
-}
-
#[tokio::test]
async fn join_timestamp() -> Result<()> {
let ctx = SessionContext::new();
@@ -317,76 +85,6 @@ async fn join_timestamp() -> Result<()> {
Ok(())
}
-#[tokio::test]
-async fn left_join_should_not_panic_with_empty_side() -> Result<()> {
- let ctx = SessionContext::new();
-
- let t1_schema = Schema::new(vec![
- Field::new("t1_id", DataType::Int64, true),
- Field::new("t1_value", DataType::Utf8, false),
- ]);
- let t1_data = RecordBatch::try_new(
- Arc::new(t1_schema),
- vec![
- Arc::new(Int64Array::from(vec![5247, 3821, 6321, 8821, 7748])),
- Arc::new(StringArray::from(vec!["a", "b", "c", "d", "e"])),
- ],
- )?;
- ctx.register_batch("t1", t1_data)?;
-
- let t2_schema = Schema::new(vec![
- Field::new("t2_id", DataType::Int64, true),
- Field::new("t2_value", DataType::Boolean, true),
- ]);
- let t2_data = RecordBatch::try_new(
- Arc::new(t2_schema),
- vec![
- Arc::new(Int64Array::from(vec![358, 2820, 3804, 7748])),
- Arc::new(BooleanArray::from(vec![
- Some(true),
- Some(false),
- None,
- None,
- ])),
- ],
- )?;
- ctx.register_batch("t2", t2_data)?;
-
- let expected_left_join = vec![
- "+-------+----------+-------+----------+",
- "| t1_id | t1_value | t2_id | t2_value |",
- "+-------+----------+-------+----------+",
- "| 5247 | a | | |",
- "| 3821 | b | | |",
- "| 6321 | c | | |",
- "| 8821 | d | | |",
- "| 7748 | e | 7748 | |",
- "+-------+----------+-------+----------+",
- ];
-
- let results_left_join =
- execute_to_batches(&ctx, "SELECT * FROM t1 LEFT JOIN t2 ON t1_id = t2_id").await;
- assert_batches_sorted_eq!(expected_left_join, &results_left_join);
-
- let expected_right_join = vec![
- "+-------+----------+-------+----------+",
- "| t2_id | t2_value | t1_id | t1_value |",
- "+-------+----------+-------+----------+",
- "| | | 3821 | b |",
- "| | | 5247 | a |",
- "| | | 6321 | c |",
- "| | | 8821 | d |",
- "| 7748 | | 7748 | e |",
- "+-------+----------+-------+----------+",
- ];
-
- let result_right_join =
- execute_to_batches(&ctx, "SELECT * FROM t2 RIGHT JOIN t1 ON t1_id = t2_id").await;
- assert_batches_sorted_eq!(expected_right_join, &result_right_join);
-
- Ok(())
-}
-
#[tokio::test]
async fn left_join_using_2() -> Result<()> {
let results = execute_with_partition(
@@ -645,251 +343,6 @@ async fn hash_join_with_dictionary() -> Result<()> {
Ok(())
}
-#[tokio::test]
-async fn reduce_left_join_1() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- // reduce to inner join
- let sql = "select * from t1 left join t2 on t1.t1_id = t2.t2_id where t2.t2_id < 100";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Inner Join: t1.t1_id = t2.t2_id [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " Filter: t1.t1_id < UInt32(100) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " Filter: t2.t2_id < UInt32(100) [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_name, t2_int] [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn reduce_left_join_2() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- // reduce to inner join
- let sql = "select * from t1 left join t2 on t1.t1_id = t2.t2_id where t2.t2_int < 10 or (t1.t1_int > 2 and t2.t2_name != 'w')";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
-
- // filter expr: `t2.t2_int < 10 or (t1.t1_int > 2 and t2.t2_name != 'w')`
- // could be write to: `(t1.t1_int > 2 or t2.t2_int < 10) and (t2.t2_name != 'w' or t2.t2_int < 10)`
- // the right part `(t2.t2_name != 'w' or t2.t2_int < 10)` could be push down left join side and remove in filter.
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Inner Join: t1.t1_id = t2.t2_id Filter: t2.t2_int < UInt32(10) OR t1.t1_int > UInt32(2) AND t2.t2_name != Utf8(\"w\") [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " Filter: t2.t2_int < UInt32(10) OR t2.t2_name != Utf8(\"w\") [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_name, t2_int] [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn reduce_left_join_3() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- // reduce subquery to inner join
- let sql = "select * from (select t1.* from t1 left join t2 on t1.t1_id = t2.t2_id where t2.t2_int < 3) t3 left join t2 on t3.t1_int = t2.t2_int where t3.t1_id < 100";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Left Join: t3.t1_int = t2.t2_int [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " SubqueryAlias: t3 [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " Projection: t1.t1_id, t1.t1_name, t1.t1_int [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " Inner Join: t1.t1_id = t2.t2_id [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N]",
- " Filter: t1.t1_id < UInt32(100) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " Projection: t2.t2_id [t2_id:UInt32;N]",
- " Filter: t2.t2_int < UInt32(3) AND t2.t2_id < UInt32(100) [t2_id:UInt32;N, t2_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_int] [t2_id:UInt32;N, t2_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_name, t2_int] [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn reduce_right_join_1() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- // reduce to inner join
- let sql = "select * from t1 right join t2 on t1.t1_id = t2.t2_id where t1.t1_int is not null";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Inner Join: t1.t1_id = t2.t2_id [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " Filter: t1.t1_int IS NOT NULL [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_name, t2_int] [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn reduce_right_join_2() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- // reduce to inner join
- let sql = "select * from t1 right join t2 on t1.t1_id = t2.t2_id where not(t1.t1_int = t2.t2_int)";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Inner Join: t1.t1_id = t2.t2_id Filter: t1.t1_int != t2.t2_int [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_name, t2_int] [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn reduce_full_join_to_right_join() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- // reduce to right join
- let sql = "select * from t1 full join t2 on t1.t1_id = t2.t2_id where t2.t2_name is not null";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Right Join: t1.t1_id = t2.t2_id [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " Filter: t2.t2_name IS NOT NULL [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_name, t2_int] [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn reduce_full_join_to_left_join() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- // reduce to left join
- let sql =
- "select * from t1 full join t2 on t1.t1_id = t2.t2_id where t1.t1_name != 'b'";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Left Join: t1.t1_id = t2.t2_id [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " Filter: t1.t1_name != Utf8(\"b\") [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_name, t2_int] [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn reduce_full_join_to_inner_join() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- // reduce to inner join
- let sql = "select * from t1 full join t2 on t1.t1_id = t2.t2_id where t1.t1_name != 'b' and t2.t2_name = 'x'";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Inner Join: t1.t1_id = t2.t2_id [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " Filter: t1.t1_name != Utf8(\"b\") [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " Filter: t2.t2_name = Utf8(\"x\") [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_name, t2_int] [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn sort_merge_equijoin() -> Result<()> {
- let ctx = create_sort_merge_join_context("t1_id", "t2_id")?;
- let equivalent_sql = [
- "SELECT t1_id, t1_name, t2_name FROM t1 JOIN t2 ON t1_id = t2_id ORDER BY t1_id",
- "SELECT t1_id, t1_name, t2_name FROM t1 JOIN t2 ON t2_id = t1_id ORDER BY t1_id",
- ];
- let expected = vec![
- "+-------+---------+---------+",
- "| t1_id | t1_name | t2_name |",
- "+-------+---------+---------+",
- "| 11 | a | z |",
- "| 22 | b | y |",
- "| 44 | d | x |",
- "+-------+---------+---------+",
- ];
- for sql in equivalent_sql.iter() {
- let actual = execute_to_batches(&ctx, sql).await;
- assert_batches_eq!(expected, &actual);
- }
-
- Ok(())
-}
-
#[tokio::test]
async fn sort_merge_join_on_date32() -> Result<()> {
let ctx = create_sort_merge_join_datatype_context()?;
@@ -1122,124 +575,16 @@ async fn left_semi_join() -> Result<()> {
}
#[tokio::test]
-async fn left_semi_join_pushdown() -> Result<()> {
- let ctx = create_left_semi_anti_join_context_with_null_ids("t1_id", "t2_id", false)
- .unwrap();
-
- // assert logical plan
- let sql = "SELECT t1.t1_id, t1.t1_name FROM t1 LEFT SEMI JOIN t2 ON (t1.t1_id = t2.t2_id and t2.t2_int > 1)";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftSemi Join: t1.t1_id = t2.t2_id [t1_id:UInt32;N, t1_name:Utf8;N]",
- " TableScan: t1 projection=[t1_id, t1_name] [t1_id:UInt32;N, t1_name:Utf8;N]",
- " Projection: t2.t2_id [t2_id:UInt32;N]",
- " Filter: t2.t2_int > UInt32(1) [t2_id:UInt32;N, t2_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_int] [t2_id:UInt32;N, t2_int:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn left_anti_join() -> Result<()> {
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_left_semi_anti_join_context_with_null_ids(
- "t1_id",
- "t2_id",
- repartition_joins,
- )
- .unwrap();
-
- let sql = "SELECT t1_id, t1_name FROM t1 WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE t1_id = t2_id) ORDER BY t1_id";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+-------+---------+",
- "| t1_id | t1_name |",
- "+-------+---------+",
- "| 33 | c |",
- "| | e |",
- "+-------+---------+",
- ];
- assert_batches_eq!(expected, &actual);
-
- let sql = "SELECT t1_id FROM t1 EXCEPT SELECT t2_id FROM t2 ORDER BY t1_id";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+-------+",
- "| t1_id |",
- "+-------+",
- "| 33 |",
- "+-------+",
- ];
- assert_batches_eq!(expected, &actual);
-
- let sql = "SELECT t1_id, t1_name FROM t1 LEFT ANTI JOIN t2 ON (t1_id = t2_id) ORDER BY t1_id";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+-------+---------+",
- "| t1_id | t1_name |",
- "+-------+---------+",
- "| 33 | c |",
- "| | e |",
- "+-------+---------+",
- ];
- assert_batches_eq!(expected, &actual);
- }
-
- Ok(())
-}
-
-#[tokio::test]
-async fn error_left_anti_join() -> Result<()> {
- // https://github.com/apache/arrow-datafusion/issues/4366
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_left_semi_anti_join_context_with_null_ids(
- "t1_id",
- "t2_id",
- repartition_joins,
- )
- .unwrap();
-
- let sql = "SELECT t1_id, t1_name FROM t1 WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE t1_id = t2_id and t1_id > 11) ORDER BY t1_id";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+-------+---------+",
- "| t1_id | t1_name |",
- "+-------+---------+",
- "| 11 | a |",
- "| 11 | a |",
- "| 33 | c |",
- "| | e |",
- "+-------+---------+",
- ];
- assert_batches_eq!(expected, &actual);
- }
-
- Ok(())
-}
-
-#[tokio::test]
-#[ignore = "Test ignored, will be enabled after fixing the NAAJ bug"]
-// https://github.com/apache/arrow-datafusion/issues/4211
-async fn null_aware_left_anti_join() -> Result<()> {
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_left_semi_anti_join_context_with_null_ids(
- "t1_id",
- "t2_id",
- repartition_joins,
- )
+#[ignore = "Test ignored, will be enabled after fixing the NAAJ bug"]
+// https://github.com/apache/arrow-datafusion/issues/4211
+async fn null_aware_left_anti_join() -> Result<()> {
+ let test_repartition_joins = vec![true, false];
+ for repartition_joins in test_repartition_joins {
+ let ctx = create_left_semi_anti_join_context_with_null_ids(
+ "t1_id",
+ "t2_id",
+ repartition_joins,
+ )
.unwrap();
let sql = "SELECT t1_id, t1_name FROM t1 WHERE t1_id NOT IN (SELECT t2_id FROM t2) ORDER BY t1_id";
@@ -1355,1333 +700,3 @@ async fn right_semi_join() -> Result<()> {
Ok(())
}
-
-#[tokio::test]
-async fn join_and_aggregate_on_same_key() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", true)?;
- let sql = "select distinct(t1.t1_id) from t1 inner join t2 on t1.t1_id = t2.t2_id";
-
- // assert logical plan
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan().unwrap();
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Aggregate: groupBy=[[t1.t1_id]], aggr=[[]] [t1_id:UInt32;N]",
- " Projection: t1.t1_id [t1_id:UInt32;N]",
- " Inner Join: t1.t1_id = t2.t2_id [t1_id:UInt32;N, t2_id:UInt32;N]",
- " TableScan: t1 projection=[t1_id] [t1_id:UInt32;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- let msg = format!("Creating physical plan for '{sql}'");
- let dataframe = ctx.sql(sql).await.expect(&msg);
- let physical_plan = dataframe.create_physical_plan().await?;
- let expected =
- vec![
- "AggregateExec: mode=Single, gby=[t1_id@0 as t1_id], aggr=[]",
- " ProjectionExec: expr=[t1_id@0 as t1_id]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: \"t1_id\", index: 0 }, Column { name: \"t2_id\", index: 0 })]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t1_id\", index: 0 }], 2), input_partitions=2",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t2_id\", index: 0 }], 2), input_partitions=2",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ];
-
- let formatted = displayable(physical_plan.as_ref()).indent().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- let sql = "select count(*) from (select * from t1 inner join t2 on t1.t1_id = t2.t2_id) group by t1_id";
-
- // assert logical plan
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan().unwrap();
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Projection: COUNT(UInt8(1)) [COUNT(UInt8(1)):Int64;N]",
- " Aggregate: groupBy=[[t1.t1_id]], aggr=[[COUNT(UInt8(1))]] [t1_id:UInt32;N, COUNT(UInt8(1)):Int64;N]",
- " Projection: t1.t1_id [t1_id:UInt32;N]",
- " Inner Join: t1.t1_id = t2.t2_id [t1_id:UInt32;N, t2_id:UInt32;N]",
- " TableScan: t1 projection=[t1_id] [t1_id:UInt32;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- let msg = format!("Creating physical plan for '{sql}'");
- let dataframe = ctx.sql(sql).await.expect(&msg);
- let physical_plan = dataframe.create_physical_plan().await?;
- let expected =
- vec![
- "ProjectionExec: expr=[COUNT(UInt8(1))@1 as COUNT(UInt8(1))]",
- " AggregateExec: mode=Single, gby=[t1_id@0 as t1_id], aggr=[COUNT(UInt8(1))]",
- " ProjectionExec: expr=[t1_id@0 as t1_id]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: \"t1_id\", index: 0 }, Column { name: \"t2_id\", index: 0 })]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t1_id\", index: 0 }], 2), input_partitions=2",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t2_id\", index: 0 }], 2), input_partitions=2",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ];
-
- let formatted = displayable(physical_plan.as_ref()).indent().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- let sql =
- "select count(distinct t1.t1_id) from t1 inner join t2 on t1.t1_id = t2.t2_id";
-
- // assert logical plan
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan().unwrap();
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Projection: COUNT(alias1) AS COUNT(DISTINCT t1.t1_id) [COUNT(DISTINCT t1.t1_id):Int64;N]",
- " Aggregate: groupBy=[[]], aggr=[[COUNT(alias1)]] [COUNT(alias1):Int64;N]",
- " Aggregate: groupBy=[[t1.t1_id AS alias1]], aggr=[[]] [alias1:UInt32;N]",
- " Projection: t1.t1_id [t1_id:UInt32;N]",
- " Inner Join: t1.t1_id = t2.t2_id [t1_id:UInt32;N, t2_id:UInt32;N]",
- " TableScan: t1 projection=[t1_id] [t1_id:UInt32;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- // the Partial and FinalPartitioned Aggregate are not combined to Single Aggregate due to group by exprs are different
- // TODO improve ReplaceDistinctWithAggregate rule to avoid unnecessary alias Cast
- let msg = format!("Creating physical plan for '{sql}'");
- let dataframe = ctx.sql(sql).await.expect(&msg);
- let physical_plan = dataframe.create_physical_plan().await?;
- let expected =
- vec![
- "ProjectionExec: expr=[COUNT(alias1)@0 as COUNT(DISTINCT t1.t1_id)]",
- " AggregateExec: mode=Final, gby=[], aggr=[COUNT(alias1)]",
- " CoalescePartitionsExec",
- " AggregateExec: mode=Partial, gby=[], aggr=[COUNT(alias1)]",
- " AggregateExec: mode=FinalPartitioned, gby=[alias1@0 as alias1], aggr=[]",
- " AggregateExec: mode=Partial, gby=[t1_id@0 as alias1], aggr=[]",
- " ProjectionExec: expr=[t1_id@0 as t1_id]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: \"t1_id\", index: 0 }, Column { name: \"t2_id\", index: 0 })]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t1_id\", index: 0 }], 2), input_partitions=2",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t2_id\", index: 0 }], 2), input_partitions=2",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ];
-
- let formatted = displayable(physical_plan.as_ref()).indent().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn reduce_cross_join_with_expr_join_key_all() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- // reduce to inner join
- let sql = "select * from t1 cross join t2 where t1.t1_id + 12 = t2.t2_id + 1";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Inner Join: CAST(t1.t1_id AS Int64) + Int64(12) = CAST(t2.t2_id AS Int64) + Int64(1) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_name, t2_int] [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn reduce_cross_join_with_cast_expr_join_key() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- let sql =
- "select t1.t1_id, t2.t2_id, t1.t1_name from t1 cross join t2 where t1.t1_id + 11 = cast(t2.t2_id as BIGINT)";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Projection: t1.t1_id, t2.t2_id, t1.t1_name [t1_id:UInt32;N, t2_id:UInt32;N, t1_name:Utf8;N]",
- " Inner Join: CAST(t1.t1_id AS Int64) + Int64(11) = CAST(t2.t2_id AS Int64) [t1_id:UInt32;N, t1_name:Utf8;N, t2_id:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name] [t1_id:UInt32;N, t1_name:Utf8;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn reduce_cross_join_with_wildcard_and_expr() -> Result<()> {
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_join_context("t1_id", "t2_id", repartition_joins)?;
-
- let sql = "select *,t1.t1_id+11 from t1,t2 where t1.t1_id+11=t2.t2_id";
-
- // assert logical plan
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Projection: t1.t1_id, t1.t1_name, t1.t1_int, t2.t2_id, t2.t2_name, t2.t2_int, CAST(t1.t1_id AS Int64) + Int64(11) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N, t1.t1_id + Int64(11):Int64;N]",
- " Inner Join: CAST(t1.t1_id AS Int64) + Int64(11) = CAST(t2.t2_id AS Int64) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N, t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_name, t2_int] [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]"
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- // assert physical plan
- let msg = format!("Creating physical plan for '{sql}'");
- let dataframe = ctx.sql(sql).await.expect(&msg);
- let physical_plan = dataframe.create_physical_plan().await?;
- let expected = if repartition_joins {
- vec![
- "ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, t2_id@3 as t2_id, t2_name@4 as t2_name, t2_int@5 as t2_int, CAST(t1_id@0 AS Int64) + 11 as t1.t1_id + Int64(11)]",
- " ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, t2_id@4 as t2_id, t2_name@5 as t2_name, t2_int@6 as t2_int]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: \"t1.t1_id + Int64(11)\", index: 3 }, Column { name: \"CAST(t2.t2_id AS Int64)\", index: 3 })]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t1.t1_id + Int64(11)\", index: 3 }], 2), input_partitions=2",
- " ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, CAST(t1_id@0 AS Int64) + 11 as t1.t1_id + Int64(11)]",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"CAST(t2.t2_id AS Int64)\", index: 3 }], 2), input_partitions=2",
- " ProjectionExec: expr=[t2_id@0 as t2_id, t2_name@1 as t2_name, t2_int@2 as t2_int, CAST(t2_id@0 AS Int64) as CAST(t2.t2_id AS Int64)]",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ]
- } else {
- vec![
- "ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, t2_id@3 as t2_id, t2_name@4 as t2_name, t2_int@5 as t2_int, CAST(t1_id@0 AS Int64) + 11 as t1.t1_id + Int64(11)]",
- " ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, t2_id@4 as t2_id, t2_name@5 as t2_name, t2_int@6 as t2_int]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(Column { name: \"t1.t1_id + Int64(11)\", index: 3 }, Column { name: \"CAST(t2.t2_id AS Int64)\", index: 3 })]",
- " CoalescePartitionsExec",
- " ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, CAST(t1_id@0 AS Int64) + 11 as t1.t1_id + Int64(11)]",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " ProjectionExec: expr=[t2_id@0 as t2_id, t2_name@1 as t2_name, t2_int@2 as t2_int, CAST(t2_id@0 AS Int64) as CAST(t2.t2_id AS Int64)]",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ]
- };
- let formatted = displayable(physical_plan.as_ref()).indent().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
- }
-
- Ok(())
-}
-
-#[tokio::test]
-async fn both_side_expr_key_inner_join() -> Result<()> {
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_join_context("t1_id", "t2_id", repartition_joins)?;
-
- let sql = "SELECT t1.t1_id, t2.t2_id, t1.t1_name \
- FROM t1 \
- INNER JOIN t2 \
- ON t1.t1_id + cast(12 as INT UNSIGNED) = t2.t2_id + cast(1 as INT UNSIGNED)";
-
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(sql).await.expect(&msg);
- let physical_plan = dataframe.create_physical_plan().await?;
-
- let expected = if repartition_joins {
- vec![
- "ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name]",
- " ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@3 as t2_id]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: \"t1.t1_id + UInt32(12)\", index: 2 }, Column { name: \"t2.t2_id + UInt32(1)\", index: 1 })]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t1.t1_id + UInt32(12)\", index: 2 }], 2), input_partitions=2",
- " ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_id@0 + 12 as t1.t1_id + UInt32(12)]",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t2.t2_id + UInt32(1)\", index: 1 }], 2), input_partitions=2",
- " ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 + 1 as t2.t2_id + UInt32(1)]",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ]
- } else {
- vec![
- "ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name]",
- " ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@3 as t2_id]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(Column { name: \"t1.t1_id + UInt32(12)\", index: 2 }, Column { name: \"t2.t2_id + UInt32(1)\", index: 1 })]",
- " CoalescePartitionsExec",
- " ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_id@0 + 12 as t1.t1_id + UInt32(12)]",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 + 1 as t2.t2_id + UInt32(1)]",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ]
- };
- let formatted = displayable(physical_plan.as_ref()).indent().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
- }
-
- Ok(())
-}
-
-#[tokio::test]
-async fn left_side_expr_key_inner_join() -> Result<()> {
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_join_context("t1_id", "t2_id", repartition_joins)?;
-
- let sql = "SELECT t1.t1_id, t2.t2_id, t1.t1_name \
- FROM t1 \
- INNER JOIN t2 \
- ON t1.t1_id + cast(11 as INT UNSIGNED) = t2.t2_id";
-
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(sql).await.expect(&msg);
- let physical_plan = dataframe.create_physical_plan().await?;
-
- let expected = if repartition_joins {
- vec![
- "ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name]",
- " ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@3 as t2_id]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: \"t1.t1_id + UInt32(11)\", index: 2 }, Column { name: \"t2_id\", index: 0 })]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t1.t1_id + UInt32(11)\", index: 2 }], 2), input_partitions=2",
- " ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_id@0 + 11 as t1.t1_id + UInt32(11)]",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t2_id\", index: 0 }], 2), input_partitions=2",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ]
- } else {
- vec![
- "ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name]",
- " ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@3 as t2_id]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(Column { name: \"t1.t1_id + UInt32(11)\", index: 2 }, Column { name: \"t2_id\", index: 0 })]",
- " CoalescePartitionsExec",
- " ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_id@0 + 11 as t1.t1_id + UInt32(11)]",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ]
- };
- let formatted = displayable(physical_plan.as_ref()).indent().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
- }
-
- Ok(())
-}
-
-#[tokio::test]
-async fn right_side_expr_key_inner_join() -> Result<()> {
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_join_context("t1_id", "t2_id", repartition_joins)?;
-
- let sql = "SELECT t1.t1_id, t2.t2_id, t1.t1_name \
- FROM t1 \
- INNER JOIN t2 \
- ON t1.t1_id = t2.t2_id - cast(11 as INT UNSIGNED)";
-
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(sql).await.expect(&msg);
- let physical_plan = dataframe.create_physical_plan().await?;
-
- let expected = if repartition_joins {
- vec![
- "ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name]",
- " ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@2 as t2_id]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: \"t1_id\", index: 0 }, Column { name: \"t2.t2_id - UInt32(11)\", index: 1 })]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t1_id\", index: 0 }], 2), input_partitions=2",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t2.t2_id - UInt32(11)\", index: 1 }], 2), input_partitions=2",
- " ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 - 11 as t2.t2_id - UInt32(11)]",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ]
- } else {
- vec![
- "ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name]",
- " ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@2 as t2_id]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(Column { name: \"t1_id\", index: 0 }, Column { name: \"t2.t2_id - UInt32(11)\", index: 1 })]",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 - 11 as t2.t2_id - UInt32(11)]",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ]
- };
- let formatted = displayable(physical_plan.as_ref()).indent().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
- }
-
- Ok(())
-}
-
-#[tokio::test]
-async fn select_wildcard_with_expr_key_inner_join() -> Result<()> {
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_join_context("t1_id", "t2_id", repartition_joins)?;
-
- let sql = "SELECT * \
- FROM t1 \
- INNER JOIN t2 \
- ON t1.t1_id = t2.t2_id - cast(11 as INT UNSIGNED)";
-
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(sql).await.expect(&msg);
- let physical_plan = dataframe.create_physical_plan().await?;
-
- let expected = if repartition_joins {
- vec![
- "ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, t2_id@3 as t2_id, t2_name@4 as t2_name, t2_int@5 as t2_int]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: \"t1_id\", index: 0 }, Column { name: \"t2.t2_id - UInt32(11)\", index: 3 })]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t1_id\", index: 0 }], 2), input_partitions=2",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " RepartitionExec: partitioning=Hash([Column { name: \"t2.t2_id - UInt32(11)\", index: 3 }], 2), input_partitions=2",
- " ProjectionExec: expr=[t2_id@0 as t2_id, t2_name@1 as t2_name, t2_int@2 as t2_int, t2_id@0 - 11 as t2.t2_id - UInt32(11)]",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ]
- } else {
- vec![
- "ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, t2_id@3 as t2_id, t2_name@4 as t2_name, t2_int@5 as t2_int]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(Column { name: \"t1_id\", index: 0 }, Column { name: \"t2.t2_id - UInt32(11)\", index: 3 })]",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " ProjectionExec: expr=[t2_id@0 as t2_id, t2_name@1 as t2_name, t2_int@2 as t2_int, t2_id@0 - 11 as t2.t2_id - UInt32(11)]",
- " RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ]
- };
- let formatted = displayable(physical_plan.as_ref()).indent().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
- }
-
- Ok(())
-}
-
-#[tokio::test]
-async fn join_with_type_coercion_for_equi_expr() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- let sql = "select t1.t1_id, t1.t1_name, t2.t2_id from t1 inner join t2 on t1.t1_id + 11 = t2.t2_id";
-
- // assert logical plan
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan().unwrap();
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Inner Join: CAST(t1.t1_id AS Int64) + Int64(11) = CAST(t2.t2_id AS Int64) [t1_id:UInt32;N, t1_name:Utf8;N, t2_id:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name] [t1_id:UInt32;N, t1_name:Utf8;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn join_only_with_filter() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- let sql = "select t1.t1_id, t1.t1_name, t2.t2_id from t1 inner join t2 on t1.t1_id * 4 < t2.t2_id";
-
- // assert logical plan
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan().unwrap();
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Inner Join: Filter: CAST(t1.t1_id AS Int64) * Int64(4) < CAST(t2.t2_id AS Int64) [t1_id:UInt32;N, t1_name:Utf8;N, t2_id:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name] [t1_id:UInt32;N, t1_name:Utf8;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn type_coercion_join_with_filter_and_equi_expr() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- let sql = "select t1.t1_id, t1.t1_name, t2.t2_id \
- from t1 \
- inner join t2 \
- on t1.t1_id * 5 = t2.t2_id and t1.t1_id * 4 < t2.t2_id";
-
- // assert logical plan
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan().unwrap();
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " Inner Join: CAST(t1.t1_id AS Int64) * Int64(5) = CAST(t2.t2_id AS Int64) Filter: CAST(t1.t1_id AS Int64) * Int64(4) < CAST(t2.t2_id AS Int64) [t1_id:UInt32;N, t1_name:Utf8;N, t2_id:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name] [t1_id:UInt32;N, t1_name:Utf8;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- Ok(())
-}
-
-#[tokio::test]
-async fn test_cross_join_to_groupby_with_different_key_ordering() -> Result<()> {
- // Regression test for GH #4873
- let col1 = Arc::new(StringArray::from(vec![
- "A", "A", "A", "A", "A", "A", "A", "A", "BB", "BB", "BB", "BB",
- ])) as ArrayRef;
-
- let col2 =
- Arc::new(UInt64Array::from(vec![1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6])) as ArrayRef;
-
- let col3 =
- Arc::new(UInt64Array::from(vec![1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])) as ArrayRef;
-
- let schema = Arc::new(Schema::new(vec![
- Field::new("col1", DataType::Utf8, true),
- Field::new("col2", DataType::UInt64, true),
- Field::new("col3", DataType::UInt64, true),
- ])) as SchemaRef;
-
- let batch = RecordBatch::try_new(schema.clone(), vec![col1, col2, col3]).unwrap();
- let mem_table = MemTable::try_new(schema, vec![vec![batch]]).unwrap();
-
- // Create context and register table
- let ctx = SessionContext::new();
- ctx.register_table("tbl", Arc::new(mem_table)).unwrap();
-
- let sql = "select col1, col2, coalesce(sum_col3, 0) as sum_col3 \
- from (select distinct col2 from tbl) AS q1 \
- cross join (select distinct col1 from tbl) AS q2 \
- left outer join (SELECT col1, col2, sum(col3) as sum_col3 FROM tbl GROUP BY col1, col2) AS q3 \
- USING(col2, col1) \
- ORDER BY col1, col2";
-
- let expected = vec![
- "+------+------+----------+",
- "| col1 | col2 | sum_col3 |",
- "+------+------+----------+",
- "| A | 1 | 2.0 |",
- "| A | 2 | 2.0 |",
- "| A | 3 | 2.0 |",
- "| A | 4 | 2.0 |",
- "| A | 5 | 0.0 |",
- "| A | 6 | 0.0 |",
- "| BB | 1 | 0.0 |",
- "| BB | 2 | 0.0 |",
- "| BB | 3 | 0.0 |",
- "| BB | 4 | 0.0 |",
- "| BB | 5 | 2.0 |",
- "| BB | 6 | 2.0 |",
- "+------+------+----------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn subquery_to_join_with_both_side_expr() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- let sql = "select t1.t1_id, t1.t1_name, t1.t1_int from t1 where t1.t1_id + 12 in (select t2.t2_id + 1 from t2)";
-
- // assert logical plan
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan().unwrap();
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftSemi Join: CAST(t1.t1_id AS Int64) + Int64(12) = __correlated_sq_1.CAST(t2_id AS Int64) + Int64(1) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " SubqueryAlias: __correlated_sq_1 [CAST(t2_id AS Int64) + Int64(1):Int64;N]",
- " Projection: CAST(t2.t2_id AS Int64) + Int64(1) AS CAST(t2_id AS Int64) + Int64(1) [CAST(t2_id AS Int64) + Int64(1):Int64;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- let expected = vec![
- "+-------+---------+--------+",
- "| t1_id | t1_name | t1_int |",
- "+-------+---------+--------+",
- "| 11 | a | 1 |",
- "| 33 | c | 3 |",
- "| 44 | d | 4 |",
- "+-------+---------+--------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn subquery_to_join_with_muti_filter() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- let sql = "select t1.t1_id, t1.t1_name, t1.t1_int from t1 where t1.t1_id + 12 in
- (select t2.t2_id + 1 from t2 where t1.t1_int <= t2.t2_int and t2.t2_int > 0)";
-
- // assert logical plan
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan().unwrap();
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftSemi Join: CAST(t1.t1_id AS Int64) + Int64(12) = __correlated_sq_1.CAST(t2_id AS Int64) + Int64(1) Filter: t1.t1_int <= __correlated_sq_1.t2_int [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " SubqueryAlias: __correlated_sq_1 [CAST(t2_id AS Int64) + Int64(1):Int64;N, t2_int:UInt32;N]",
- " Projection: CAST(t2.t2_id AS Int64) + Int64(1) AS CAST(t2_id AS Int64) + Int64(1), t2.t2_int [CAST(t2_id AS Int64) + Int64(1):Int64;N, t2_int:UInt32;N]",
- " Filter: t2.t2_int > UInt32(0) [t2_id:UInt32;N, t2_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_int] [t2_id:UInt32;N, t2_int:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- let expected = vec![
- "+-------+---------+--------+",
- "| t1_id | t1_name | t1_int |",
- "+-------+---------+--------+",
- "| 11 | a | 1 |",
- "| 33 | c | 3 |",
- "+-------+---------+--------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn three_projection_exprs_subquery_to_join() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- let sql = "select t1.t1_id, t1.t1_name, t1.t1_int from t1 where t1.t1_id + 12 in
- (select t2.t2_id + 1 from t2 where t1.t1_int <= t2.t2_int and t1.t1_name != t2.t2_name and t2.t2_int > 0)";
-
- // assert logical plan
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan().unwrap();
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftSemi Join: CAST(t1.t1_id AS Int64) + Int64(12) = __correlated_sq_1.CAST(t2_id AS Int64) + Int64(1) Filter: t1.t1_int <= __correlated_sq_1.t2_int AND t1.t1_name != __correlated_sq_1.t2_name [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " SubqueryAlias: __correlated_sq_1 [CAST(t2_id AS Int64) + Int64(1):Int64;N, t2_int:UInt32;N, t2_name:Utf8;N]",
- " Projection: CAST(t2.t2_id AS Int64) + Int64(1) AS CAST(t2_id AS Int64) + Int64(1), t2.t2_int, t2.t2_name [CAST(t2_id AS Int64) + Int64(1):Int64;N, t2_int:UInt32;N, t2_name:Utf8;N]",
- " Filter: t2.t2_int > UInt32(0) [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_name, t2_int] [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- let expected = vec![
- "+-------+---------+--------+",
- "| t1_id | t1_name | t1_int |",
- "+-------+---------+--------+",
- "| 11 | a | 1 |",
- "| 33 | c | 3 |",
- "+-------+---------+--------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn in_subquery_to_join_with_correlated_outer_filter() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- let sql = "select t1.t1_id, t1.t1_name, t1.t1_int from t1 where t1.t1_id + 12 in
- (select t2.t2_id + 1 from t2 where t1.t1_int > 0)";
-
- // assert logical plan
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan().unwrap();
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftSemi Join: CAST(t1.t1_id AS Int64) + Int64(12) = __correlated_sq_1.CAST(t2_id AS Int64) + Int64(1) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " Filter: t1.t1_int > UInt32(0) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " SubqueryAlias: __correlated_sq_1 [CAST(t2_id AS Int64) + Int64(1):Int64;N]",
- " Projection: CAST(t2.t2_id AS Int64) + Int64(1) AS CAST(t2_id AS Int64) + Int64(1) [CAST(t2_id AS Int64) + Int64(1):Int64;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
- Ok(())
-}
-
-#[tokio::test]
-async fn not_in_subquery_to_join_with_correlated_outer_filter() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- let sql = "select t1.t1_id, t1.t1_name, t1.t1_int from t1 where t1.t1_id + 12 not in
- (select t2.t2_id + 1 from t2 where t1.t1_int > 0)";
-
- // assert logical plan
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan().unwrap();
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftAnti Join: CAST(t1.t1_id AS Int64) + Int64(12) = __correlated_sq_1.CAST(t2_id AS Int64) + Int64(1) Filter: t1.t1_int > UInt32(0) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " SubqueryAlias: __correlated_sq_1 [CAST(t2_id AS Int64) + Int64(1):Int64;N]",
- " Projection: CAST(t2.t2_id AS Int64) + Int64(1) AS CAST(t2_id AS Int64) + Int64(1) [CAST(t2_id AS Int64) + Int64(1):Int64;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
- Ok(())
-}
-
-#[tokio::test]
-async fn in_subquery_to_join_with_outer_filter() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- let sql = "select t1.t1_id, t1.t1_name, t1.t1_int from t1 where t1.t1_id + 12 in
- (select t2.t2_id + 1 from t2 where t1.t1_int <= t2.t2_int and t1.t1_name != t2.t2_name) and t1.t1_id > 0";
-
- // assert logical plan
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan().unwrap();
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftSemi Join: CAST(t1.t1_id AS Int64) + Int64(12) = __correlated_sq_1.CAST(t2_id AS Int64) + Int64(1) Filter: t1.t1_int <= __correlated_sq_1.t2_int AND t1.t1_name != __correlated_sq_1.t2_name [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " Filter: t1.t1_id > UInt32(0) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " SubqueryAlias: __correlated_sq_1 [CAST(t2_id AS Int64) + Int64(1):Int64;N, t2_int:UInt32;N, t2_name:Utf8;N]",
- " Projection: CAST(t2.t2_id AS Int64) + Int64(1) AS CAST(t2_id AS Int64) + Int64(1), t2.t2_int, t2.t2_name [CAST(t2_id AS Int64) + Int64(1):Int64;N, t2_int:UInt32;N, t2_name:Utf8;N]",
- " TableScan: t2 projection=[t2_id, t2_name, t2_int] [t2_id:UInt32;N, t2_name:Utf8;N, t2_int:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- let expected = vec![
- "+-------+---------+--------+",
- "| t1_id | t1_name | t1_int |",
- "+-------+---------+--------+",
- "| 11 | a | 1 |",
- "| 33 | c | 3 |",
- "+-------+---------+--------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn two_in_subquery_to_join_with_outer_filter() -> Result<()> {
- let ctx = create_join_context("t1_id", "t2_id", false)?;
-
- let sql = "select t1.t1_id, t1.t1_name, t1.t1_int from t1 where t1.t1_id + 12 in
- (select t2.t2_id + 1 from t2)
- and t1.t1_int in(select t2.t2_int + 1 from t2)
- and t1.t1_id > 0";
-
- // assert logical plan
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan().unwrap();
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftSemi Join: CAST(t1.t1_int AS Int64) = __correlated_sq_2.CAST(t2_int AS Int64) + Int64(1) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " LeftSemi Join: CAST(t1.t1_id AS Int64) + Int64(12) = __correlated_sq_1.CAST(t2_id AS Int64) + Int64(1) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " Filter: t1.t1_id > UInt32(0) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " SubqueryAlias: __correlated_sq_1 [CAST(t2_id AS Int64) + Int64(1):Int64;N]",
- " Projection: CAST(t2.t2_id AS Int64) + Int64(1) AS CAST(t2_id AS Int64) + Int64(1) [CAST(t2_id AS Int64) + Int64(1):Int64;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- " SubqueryAlias: __correlated_sq_2 [CAST(t2_int AS Int64) + Int64(1):Int64;N]",
- " Projection: CAST(t2.t2_int AS Int64) + Int64(1) AS CAST(t2_int AS Int64) + Int64(1) [CAST(t2_int AS Int64) + Int64(1):Int64;N]",
- " TableScan: t2 projection=[t2_int] [t2_int:UInt32;N]",
- ];
-
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- let expected = vec![
- "+-------+---------+--------+",
- "| t1_id | t1_name | t1_int |",
- "+-------+---------+--------+",
- "| 44 | d | 4 |",
- "+-------+---------+--------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn right_as_inner_table_nested_loop_join() -> Result<()> {
- let ctx = create_nested_loop_join_context()?;
-
- // Distribution: left is `UnspecifiedDistribution`, right is `SinglePartition`.
- let sql = "SELECT t1.t1_id, t2.t2_id
- FROM t1 INNER JOIN t2 ON t1.t1_id > t2.t2_id
- WHERE t1.t1_id > 10 AND t2.t2_int > 1";
-
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(sql).await.expect(&msg);
- let physical_plan = dataframe.create_physical_plan().await?;
-
- // right is single partition side, so it will be visited many times.
- let expected = vec![
- "NestedLoopJoinExec: join_type=Inner, filter=BinaryExpr { left: Column { name: \"t1_id\", index: 0 }, op: Gt, right: Column { name: \"t2_id\", index: 1 } }",
- " CoalesceBatchesExec: target_batch_size=4096",
- " FilterExec: t1_id@0 > 10",
- " RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " CoalescePartitionsExec",
- " ProjectionExec: expr=[t2_id@0 as t2_id]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " FilterExec: t2_int@1 > 1",
- " RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ];
- let formatted = displayable(physical_plan.as_ref()).indent().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- let expected = vec![
- "+-------+-------+",
- "| t1_id | t2_id |",
- "+-------+-------+",
- "| 22 | 11 |",
- "| 33 | 11 |",
- "| 44 | 11 |",
- "+-------+-------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn left_as_inner_table_nested_loop_join() -> Result<()> {
- let ctx = create_nested_loop_join_context()?;
-
- // Distribution: left is `SinglePartition`, right is `UnspecifiedDistribution`.
- let sql = "SELECT t1.t1_id,t2.t2_id FROM (select t1_id from t1 where t1.t1_id > 22) as t1
- RIGHT JOIN (select t2_id from t2 where t2.t2_id > 11) as t2
- ON t1.t1_id < t2.t2_id";
-
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(sql).await.expect(&msg);
- let physical_plan = dataframe.create_physical_plan().await?;
-
- // left is single partition side, so it will be visited many times.
- let expected = vec![
- "NestedLoopJoinExec: join_type=Right, filter=BinaryExpr { left: Column { name: \"t1_id\", index: 0 }, op: Lt, right: Column { name: \"t2_id\", index: 1 } }",
- " CoalescePartitionsExec",
- " CoalesceBatchesExec: target_batch_size=4096",
- " FilterExec: t1_id@0 > 22",
- " RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- " CoalesceBatchesExec: target_batch_size=4096",
- " FilterExec: t2_id@0 > 11",
- " RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1",
- " MemoryExec: partitions=1, partition_sizes=[1]",
- ];
- let formatted = displayable(physical_plan.as_ref()).indent().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
-
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
-
- let expected = vec![
- "+-------+-------+",
- "| t1_id | t2_id |",
- "+-------+-------+",
- "| | 22 |",
- "| 33 | 44 |",
- "| 33 | 55 |",
- "| 44 | 55 |",
- "+-------+-------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
-
- Ok(())
-}
-
-#[tokio::test]
-async fn exists_subquery_to_join_expr_filter() -> Result<()> {
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_join_context("t1_id", "t2_id", repartition_joins)?;
-
- // exists subquery to LeftSemi join
- let sql = "SELECT * FROM t1 WHERE EXISTS(SELECT t2_id FROM t2 WHERE t1.t1_id + 1 > t2.t2_id * 2)";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftSemi Join: Filter: CAST(t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " SubqueryAlias: __correlated_sq_1 [t2_id:UInt32;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
- let expected = vec![
- "+-------+---------+--------+",
- "| t1_id | t1_name | t1_int |",
- "+-------+---------+--------+",
- "| 22 | b | 2 |",
- "| 33 | c | 3 |",
- "| 44 | d | 4 |",
- "+-------+---------+--------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
- }
-
- Ok(())
-}
-
-#[tokio::test]
-async fn exists_subquery_to_join_inner_filter() -> Result<()> {
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_join_context("t1_id", "t2_id", repartition_joins)?;
-
- // exists subquery to LeftSemi join
- let sql = "SELECT * FROM t1 WHERE EXISTS(SELECT t2_id FROM t2 WHERE t1.t1_id + 1 > t2.t2_id * 2 AND t2.t2_int < 3)";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
-
- // `t2.t2_int < 3` will be kept in the subquery filter.
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftSemi Join: Filter: CAST(t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " SubqueryAlias: __correlated_sq_1 [t2_id:UInt32;N]",
- " Projection: t2.t2_id [t2_id:UInt32;N]",
- " Filter: t2.t2_int < UInt32(3) [t2_id:UInt32;N, t2_int:UInt32;N]",
- " TableScan: t2 projection=[t2_id, t2_int] [t2_id:UInt32;N, t2_int:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
- let expected = vec![
- "+-------+---------+--------+",
- "| t1_id | t1_name | t1_int |",
- "+-------+---------+--------+",
- "| 44 | d | 4 |",
- "+-------+---------+--------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
- }
-
- Ok(())
-}
-
-#[tokio::test]
-async fn exists_subquery_to_join_outer_filter() -> Result<()> {
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_join_context("t1_id", "t2_id", repartition_joins)?;
-
- // exists subquery to LeftSemi join
- let sql = "SELECT * FROM t1 WHERE EXISTS(SELECT t2_id FROM t2 WHERE t1.t1_id + 1 > t2.t2_id * 2 AND t1.t1_int < 3)";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
-
- // `t1.t1_int < 3` will be moved to the filter of t1.
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftSemi Join: Filter: CAST(t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " Filter: t1.t1_int < UInt32(3) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " SubqueryAlias: __correlated_sq_1 [t2_id:UInt32;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
- let expected = vec![
- "+-------+---------+--------+",
- "| t1_id | t1_name | t1_int |",
- "+-------+---------+--------+",
- "| 22 | b | 2 |",
- "+-------+---------+--------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
- }
-
- Ok(())
-}
-
-#[tokio::test]
-async fn not_exists_subquery_to_join_expr_filter() -> Result<()> {
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_join_context("t1_id", "t2_id", repartition_joins)?;
-
- // not exists subquery to LeftAnti join
- let sql = "SELECT * FROM t1 WHERE NOT EXISTS(SELECT t2_id FROM t2 WHERE t1.t1_id + 1 > t2.t2_id * 2)";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftAnti Join: Filter: CAST(t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " SubqueryAlias: __correlated_sq_1 [t2_id:UInt32;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
- let expected = vec![
- "+-------+---------+--------+",
- "| t1_id | t1_name | t1_int |",
- "+-------+---------+--------+",
- "| 11 | a | 1 |",
- "+-------+---------+--------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
- }
-
- Ok(())
-}
-
-#[tokio::test]
-async fn exists_distinct_subquery_to_join() -> Result<()> {
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_join_context("t1_id", "t2_id", repartition_joins)?;
-
- let sql = "SELECT * FROM t1 WHERE NOT EXISTS(SELECT DISTINCT t2_int FROM t2 WHERE t1.t1_id + 1 > t2.t2_id * 2)";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftAnti Join: Filter: CAST(t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " SubqueryAlias: __correlated_sq_1 [t2_id:UInt32;N]",
- " Aggregate: groupBy=[[t2.t2_id]], aggr=[[]] [t2_id:UInt32;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
- let expected = vec![
- "+-------+---------+--------+",
- "| t1_id | t1_name | t1_int |",
- "+-------+---------+--------+",
- "| 11 | a | 1 |",
- "+-------+---------+--------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
- }
-
- Ok(())
-}
-
-#[tokio::test]
-async fn exists_distinct_subquery_to_join_with_expr() -> Result<()> {
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_join_context("t1_id", "t2_id", repartition_joins)?;
-
- // `t2_id + t2_int` is in the subquery project.
- let sql = "SELECT * FROM t1 WHERE NOT EXISTS(SELECT DISTINCT t2_id + t2_int, t2_int FROM t2 WHERE t1.t1_id + 1 > t2.t2_id * 2)";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftAnti Join: Filter: CAST(t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " SubqueryAlias: __correlated_sq_1 [t2_id:UInt32;N]",
- " Aggregate: groupBy=[[t2.t2_id]], aggr=[[]] [t2_id:UInt32;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
- let expected = vec![
- "+-------+---------+--------+",
- "| t1_id | t1_name | t1_int |",
- "+-------+---------+--------+",
- "| 11 | a | 1 |",
- "+-------+---------+--------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
- }
-
- Ok(())
-}
-
-#[tokio::test]
-async fn exists_distinct_subquery_to_join_with_literal() -> Result<()> {
- let test_repartition_joins = vec![true, false];
- for repartition_joins in test_repartition_joins {
- let ctx = create_join_context("t1_id", "t2_id", repartition_joins)?;
-
- // `1` is in the subquery project.
- let sql = "SELECT * FROM t1 WHERE NOT EXISTS(SELECT DISTINCT 1, t2_int FROM t2 WHERE t1.t1_id + 1 > t2.t2_id * 2)";
- let msg = format!("Creating logical plan for '{sql}'");
- let dataframe = ctx.sql(&("explain ".to_owned() + sql)).await.expect(&msg);
- let plan = dataframe.into_optimized_plan()?;
-
- let expected = vec![
- "Explain [plan_type:Utf8, plan:Utf8]",
- " LeftAnti Join: Filter: CAST(t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_1.t2_id AS Int64) * Int64(2) [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " TableScan: t1 projection=[t1_id, t1_name, t1_int] [t1_id:UInt32;N, t1_name:Utf8;N, t1_int:UInt32;N]",
- " SubqueryAlias: __correlated_sq_1 [t2_id:UInt32;N]",
- " Aggregate: groupBy=[[t2.t2_id]], aggr=[[]] [t2_id:UInt32;N]",
- " TableScan: t2 projection=[t2_id] [t2_id:UInt32;N]",
- ];
- let formatted = plan.display_indent_schema().to_string();
- let actual: Vec<&str> = formatted.trim().lines().collect();
- assert_eq!(
- expected, actual,
- "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
- );
- let expected = vec![
- "+-------+---------+--------+",
- "| t1_id | t1_name | t1_int |",
- "+-------+---------+--------+",
- "| 11 | a | 1 |",
- "+-------+---------+--------+",
- ];
-
- let results = execute_to_batches(&ctx, sql).await;
- assert_batches_sorted_eq!(expected, &results);
- }
-
- Ok(())
-}
diff --git a/datafusion/core/tests/sql/mod.rs b/datafusion/core/tests/sql/mod.rs
index 943254ca46..42de0d23a3 100644
--- a/datafusion/core/tests/sql/mod.rs
+++ b/datafusion/core/tests/sql/mod.rs
@@ -407,45 +407,6 @@ fn create_right_semi_anti_join_context_with_null_ids(
Ok(ctx)
}
-fn create_join_context_qualified(
- left_name: &str,
- right_name: &str,
-) -> Result<SessionContext> {
- let ctx = SessionContext::new();
-
- let t1_schema = Arc::new(Schema::new(vec![
- Field::new("a", DataType::UInt32, true),
- Field::new("b", DataType::UInt32, true),
- Field::new("c", DataType::UInt32, true),
- ]));
- let t1_data = RecordBatch::try_new(
- t1_schema,
- vec![
- Arc::new(UInt32Array::from(vec![1, 2, 3, 4])),
- Arc::new(UInt32Array::from(vec![10, 20, 30, 40])),
- Arc::new(UInt32Array::from(vec![50, 60, 70, 80])),
- ],
- )?;
- ctx.register_batch(left_name, t1_data)?;
-
- let t2_schema = Arc::new(Schema::new(vec![
- Field::new("a", DataType::UInt32, true),
- Field::new("b", DataType::UInt32, true),
- Field::new("c", DataType::UInt32, true),
- ]));
- let t2_data = RecordBatch::try_new(
- t2_schema,
- vec![
- Arc::new(UInt32Array::from(vec![1, 2, 9, 4])),
- Arc::new(UInt32Array::from(vec![100, 200, 300, 400])),
- Arc::new(UInt32Array::from(vec![500, 600, 700, 800])),
- ],
- )?;
- ctx.register_batch(right_name, t2_data)?;
-
- Ok(ctx)
-}
-
fn create_hashjoin_datatype_context() -> Result<SessionContext> {
let ctx = SessionContext::new();
@@ -516,58 +477,6 @@ fn create_hashjoin_datatype_context() -> Result<SessionContext> {
Ok(ctx)
}
-fn create_sort_merge_join_context(
- column_left: &str,
- column_right: &str,
-) -> Result<SessionContext> {
- let mut config = ConfigOptions::new();
- config.optimizer.prefer_hash_join = false;
-
- let ctx = SessionContext::with_config(config.into());
-
- let t1_schema = Arc::new(Schema::new(vec![
- Field::new(column_left, DataType::UInt32, true),
- Field::new("t1_name", DataType::Utf8, true),
- Field::new("t1_int", DataType::UInt32, true),
- ]));
- let t1_data = RecordBatch::try_new(
- t1_schema,
- vec![
- Arc::new(UInt32Array::from(vec![11, 22, 33, 44])),
- Arc::new(StringArray::from(vec![
- Some("a"),
- Some("b"),
- Some("c"),
- Some("d"),
- ])),
- Arc::new(UInt32Array::from(vec![1, 2, 3, 4])),
- ],
- )?;
- ctx.register_batch("t1", t1_data)?;
-
- let t2_schema = Arc::new(Schema::new(vec![
- Field::new(column_right, DataType::UInt32, true),
- Field::new("t2_name", DataType::Utf8, true),
- Field::new("t2_int", DataType::UInt32, true),
- ]));
- let t2_data = RecordBatch::try_new(
- t2_schema,
- vec![
- Arc::new(UInt32Array::from(vec![11, 22, 44, 55])),
- Arc::new(StringArray::from(vec![
- Some("z"),
- Some("y"),
- Some("x"),
- Some("w"),
- ])),
- Arc::new(UInt32Array::from(vec![3, 1, 3, 3])),
- ],
- )?;
- ctx.register_batch("t2", t2_data)?;
-
- Ok(ctx)
-}
-
fn create_sort_merge_join_datatype_context() -> Result<SessionContext> {
let mut config = ConfigOptions::new();
config.optimizer.prefer_hash_join = false;
@@ -643,56 +552,6 @@ fn create_sort_merge_join_datatype_context() -> Result<SessionContext> {
Ok(ctx)
}
-fn create_nested_loop_join_context() -> Result<SessionContext> {
- let ctx = SessionContext::with_config(
- SessionConfig::new()
- .with_target_partitions(4)
- .with_batch_size(4096),
- );
-
- let t1_schema = Arc::new(Schema::new(vec![
- Field::new("t1_id", DataType::UInt32, true),
- Field::new("t1_name", DataType::Utf8, true),
- Field::new("t1_int", DataType::UInt32, true),
- ]));
- let t1_data = RecordBatch::try_new(
- t1_schema,
- vec![
- Arc::new(UInt32Array::from(vec![11, 22, 33, 44])),
- Arc::new(StringArray::from(vec![
- Some("a"),
- Some("b"),
- Some("c"),
- Some("d"),
- ])),
- Arc::new(UInt32Array::from(vec![1, 2, 3, 4])),
- ],
- )?;
- ctx.register_batch("t1", t1_data)?;
-
- let t2_schema = Arc::new(Schema::new(vec![
- Field::new("t2_id", DataType::UInt32, true),
- Field::new("t2_name", DataType::Utf8, true),
- Field::new("t2_int", DataType::UInt32, true),
- ]));
- let t2_data = RecordBatch::try_new(
- t2_schema,
- vec![
- Arc::new(UInt32Array::from(vec![11, 22, 44, 55])),
- Arc::new(StringArray::from(vec![
- Some("z"),
- Some("y"),
- Some("x"),
- Some("w"),
- ])),
- Arc::new(UInt32Array::from(vec![3, 1, 3, 3])),
- ],
- )?;
- ctx.register_batch("t2", t2_data)?;
-
- Ok(ctx)
-}
-
fn get_tpch_table_schema(table: &str) -> Schema {
match table {
"customer" => Schema::new(vec![
diff --git a/datafusion/core/tests/sqllogictests/test_files/joins.slt b/datafusion/core/tests/sqllogictests/test_files/joins.slt
index eb8f72470c..8e8527fc3d 100644
--- a/datafusion/core/tests/sqllogictests/test_files/joins.slt
+++ b/datafusion/core/tests/sqllogictests/test_files/joins.slt
@@ -15,6 +15,69 @@
# specific language governing permissions and limitations
# under the License.
+
+####
+# Configuration
+####
+
+statement ok
+set datafusion.execution.target_partitions = 2;
+
+statement ok
+set datafusion.execution.batch_size = 4096;
+
+statement ok
+set datafusion.explain.logical_plan_only = true;
+
+
+####
+# Data Setup
+####
+
+statement ok
+set datafusion.execution.target_partitions = 1;
+
+statement ok
+CREATE TABLE join_t1(t1_id INT UNSIGNED, t1_name VARCHAR, t1_int INT UNSIGNED)
+AS VALUES
+(11, 'a', 1),
+(22, 'b', 2),
+(33, 'c', 3),
+(44, 'd', 4);
+
+statement ok
+CREATE TABLE join_t2(t2_id INT UNSIGNED, t2_name VARCHAR, t2_int INT UNSIGNED)
+AS VALUES
+(11, 'z', 3),
+(22, 'y', 1),
+(44, 'x', 3),
+(55, 'w', 3);
+
+# Left semi anti join
+
+statement ok
+CREATE TABLE lsaj_t1(t1_id INT UNSIGNED, t1_name VARCHAR, t1_int INT UNSIGNED)
+AS VALUES
+(11, 'a', 1),
+(11, 'a', 1),
+(22, 'b', 2),
+(33, 'c', 3),
+(44, 'd', 4),
+(NULL, 'e', 0);
+
+statement ok
+CREATE TABLE lsaj_t2(t2_id INT UNSIGNED, t2_name VARCHAR, t2_int INT UNSIGNED)
+AS VALUES
+(11, 'z', 3),
+(11, 'z', 3),
+(22, 'y', 1),
+(44, 'x', 3),
+(55, 'w', 3),
+(NULL, 'v', 0);
+
+statement ok
+set datafusion.execution.target_partitions = 2;
+
##########
## Joins Tests
##########
@@ -622,3 +685,1655 @@ DROP TABLE t1
statement ok
DROP TABLE t2
+
+# Join timestamp
+
+statement ok
+CREATE TABLE timestamp(time TIMESTAMP) AS VALUES
+ (131964190213133),
+ (131964190213134),
+ (131964190213135);
+
+query PP
+SELECT *
+FROM timestamp as a
+JOIN (SELECT * FROM timestamp) as b
+ON a.time = b.time
+ORDER BY a.time
+----
+1970-01-02T12:39:24.190213133 1970-01-02T12:39:24.190213133
+1970-01-02T12:39:24.190213134 1970-01-02T12:39:24.190213134
+1970-01-02T12:39:24.190213135 1970-01-02T12:39:24.190213135
+
+statement ok
+DROP TABLE timestamp;
+
+# Join float32
+
+statement ok
+CREATE TABLE population(city VARCHAR, population FLOAT) AS VALUES
+ ('a', 838.698),
+ ('b', 1778.934),
+ ('c', 626.443);
+
+query TRTR
+SELECT *
+FROM population as a
+JOIN (SELECT * FROM population) as b
+ON a.population = b.population
+ORDER BY a.population
+----
+c 626.443 c 626.443
+a 838.698 a 838.698
+b 1778.934 b 1778.934
+
+statement ok
+DROP TABLE population;
+
+# Join float64
+
+statement ok
+CREATE TABLE population(city VARCHAR, population DOUBLE) AS VALUES
+ ('a', 838.698),
+ ('b', 1778.934),
+ ('c', 626.443);
+
+query TRTR
+SELECT *
+FROM population as a
+JOIN (SELECT * FROM population) as b
+ON a.population = b.population
+ORDER BY a.population
+----
+c 626.443 c 626.443
+a 838.698 a 838.698
+b 1778.934 b 1778.934
+
+statement ok
+DROP TABLE population;
+
+# Inner join qualified names
+
+statement ok
+CREATE TABLE t1 (a INT, b INT, c INT) AS VALUES
+ (1, 10, 50),
+ (2, 20, 60),
+ (3, 30, 70),
+ (4, 40, 80);
+
+statement ok
+CREATE TABLE t2 (a INT, b INT, c INT) AS VALUES
+ (1, 100, 500),
+ (2, 200, 600),
+ (9, 300, 700),
+ (4, 400, 800);
+
+query IIIIII
+SELECT t1.a, t1.b, t1.c, t2.a, t2.b, t2.c
+FROM t1
+INNER JOIN t2 ON t1.a = t2.a
+ORDER BY t1.a
+----
+1 10 50 1 100 500
+2 20 60 2 200 600
+4 40 80 4 400 800
+
+query IIIIII
+SELECT t1.a, t1.b, t1.c, t2.a, t2.b, t2.c
+FROM t1
+INNER JOIN t2 ON t2.a = t1.a
+ORDER BY t1.a
+----
+1 10 50 1 100 500
+2 20 60 2 200 600
+4 40 80 4 400 800
+
+statement ok
+DROP TABLE t1;
+
+statement ok
+DROP TABLE t2;
+
+# TODO: nestedjoin_with_alias
+
+# Nested join without alias
+
+query IIII
+select * from (select 1 as a, 2 as b) c INNER JOIN (select 1 as a, 3 as d) e on c.a = e.a
+----
+1 2 1 3
+
+# Join tables with duplicated column name not in on constraint
+
+statement ok
+CREATE TABLE countries (id INT, country VARCHAR) AS VALUES
+ (1, 'Germany'),
+ (2, 'Sweden'),
+ (3, 'Japan');
+
+statement ok
+CREATE TABLE cities (id INT, country_id INT, city VARCHAR) AS VALUES
+ (1, 1, 'Hamburg'),
+ (2, 2, 'Stockholm'),
+ (3, 3, 'Osaka'),
+ (4, 1, 'Berlin'),
+ (5, 2, 'Göteborg'),
+ (6, 3, 'Tokyo'),
+ (7, 3, 'Kyoto');
+
+query IITT
+SELECT t1.id, t2.id, t1.city, t2.country FROM cities AS t1 JOIN countries AS t2 ON t1.country_id = t2.id ORDER BY t1.id
+----
+1 1 Hamburg Germany
+2 2 Stockholm Sweden
+3 3 Osaka Japan
+4 1 Berlin Germany
+5 2 Göteborg Sweden
+6 3 Tokyo Japan
+7 3 Kyoto Japan
+
+statement ok
+DROP TABLE countries;
+
+statement ok
+DROP TABLE cities;
+
+# TODO: join_timestamp
+
+# Left join and right join should not panic with empty side
+
+statement ok
+CREATE TABLE t1 (t1_id BIGINT, ti_value VARCHAR NOT NULL) AS VALUES
+ (5247, 'a'),
+ (3821, 'b'),
+ (6321, 'c'),
+ (8821, 'd'),
+ (7748, 'e');
+
+statement ok
+CREATE TABLE t2 (t2_id BIGINT, t2_value BOOLEAN) AS VALUES
+ (358, true),
+ (2820, false),
+ (3804, NULL),
+ (7748, NULL);
+
+query ITIB rowsort
+SELECT * FROM t1 LEFT JOIN t2 ON t1_id = t2_id
+----
+3821 b NULL NULL
+5247 a NULL NULL
+6321 c NULL NULL
+7748 e 7748 NULL
+8821 d NULL NULL
+
+query IBIT rowsort
+SELECT * FROM t2 RIGHT JOIN t1 ON t1_id = t2_id
+----
+7748 NULL 7748 e
+NULL NULL 3821 b
+NULL NULL 5247 a
+NULL NULL 6321 c
+NULL NULL 8821 d
+
+statement ok
+DROP TABLE t1;
+
+statement ok
+DROP TABLE t2;
+
+# TODO: left_join_using_2
+
+# TODO: left_join_using_join_key_projection
+
+# TODO: left_join_2
+
+# TODO: join_partitioned
+
+# TODO: hash_join_with_date32
+
+# TODO: hash_join_with_date64
+
+# TODO: hash_join_with_decimal
+
+# TODO: hash_join_with_dictionary
+
+
+###
+# Configuration setup
+###
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+# Reduce left join 1 (to inner join)
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+LEFT JOIN join_t2 on join_t1.t1_id = join_t2.t2_id
+WHERE join_t2.t2_id < 100
+----
+logical_plan
+Inner Join: join_t1.t1_id = join_t2.t2_id
+--Filter: join_t1.t1_id < UInt32(100)
+----TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--Filter: join_t2.t2_id < UInt32(100)
+----TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+
+# Reduce left join 2 (to inner join)
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+LEFT JOIN join_t2 on join_t1.t1_id = join_t2.t2_id
+WHERE join_t2.t2_int < 10 or (join_t1.t1_int > 2 and join_t2.t2_name != 'w')
+----
+logical_plan
+Inner Join: join_t1.t1_id = join_t2.t2_id Filter: join_t2.t2_int < UInt32(10) OR join_t1.t1_int > UInt32(2) AND join_t2.t2_name != Utf8("w")
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--Filter: join_t2.t2_int < UInt32(10) OR join_t2.t2_name != Utf8("w")
+----TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+
+# Reduce left join 3 (to inner join)
+
+query TT
+EXPLAIN
+SELECT *
+FROM (
+ SELECT join_t1.*
+ FROM join_t1
+ LEFT JOIN join_t2 ON join_t1.t1_id = join_t2.t2_id
+ WHERE join_t2.t2_int < 3
+) t3
+LEFT JOIN join_t2 on t3.t1_int = join_t2.t2_int
+WHERE t3.t1_id < 100
+----
+logical_plan
+Left Join: t3.t1_int = join_t2.t2_int
+--SubqueryAlias: t3
+----Projection: join_t1.t1_id, join_t1.t1_name, join_t1.t1_int
+------Inner Join: join_t1.t1_id = join_t2.t2_id
+--------Filter: join_t1.t1_id < UInt32(100)
+----------TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--------Projection: join_t2.t2_id
+----------Filter: join_t2.t2_int < UInt32(3) AND join_t2.t2_id < UInt32(100)
+------------TableScan: join_t2 projection=[t2_id, t2_int]
+--TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+
+# Reduce right join 1 (to inner join)
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+RIGHT JOIN join_t2 on join_t1.t1_id = join_t2.t2_id
+WHERE join_t1.t1_int IS NOT NULL
+----
+logical_plan
+Inner Join: join_t1.t1_id = join_t2.t2_id
+--Filter: join_t1.t1_int IS NOT NULL
+----TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+
+# Reduce right join 2 (to inner join)
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+RIGHT JOIN join_t2 on join_t1.t1_id = join_t2.t2_id
+WHERE NOT (join_t1.t1_int = join_t2.t2_int)
+----
+logical_plan
+Inner Join: join_t1.t1_id = join_t2.t2_id Filter: join_t1.t1_int != join_t2.t2_int
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+
+# Reduce full join to right join
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+FULL JOIN join_t2 on join_t1.t1_id = join_t2.t2_id
+WHERE join_t2.t2_name IS NOT NULL
+----
+logical_plan
+Right Join: join_t1.t1_id = join_t2.t2_id
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--Filter: join_t2.t2_name IS NOT NULL
+----TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+
+# Reduce full join to left join
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+FULL JOIN join_t2 ON join_t1.t1_id = join_t2.t2_id
+WHERE join_t1.t1_name != 'b'
+----
+logical_plan
+Left Join: join_t1.t1_id = join_t2.t2_id
+--Filter: join_t1.t1_name != Utf8("b")
+----TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+
+# Reduce full join to inner join
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+FULL JOIN join_t2 on join_t1.t1_id = join_t2.t2_id
+WHERE join_t1.t1_name != 'b' and join_t2.t2_name = 'x'
+----
+logical_plan
+Inner Join: join_t1.t1_id = join_t2.t2_id
+--Filter: join_t1.t1_name != Utf8("b")
+----TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--Filter: join_t2.t2_name = Utf8("x")
+----TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+
+###
+# Configuration teardown
+###
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+
+# Sort merge equijoin
+
+query ITT
+SELECT t1_id, t1_name, t2_name
+FROM join_t1
+JOIN join_t2 ON t1_id = t2_id
+ORDER BY t1_id
+----
+11 a z
+22 b y
+44 d x
+
+query ITT
+SELECT t1_id, t1_name, t2_name
+FROM join_t1
+JOIN join_t2 ON t2_id = t1_id
+ORDER BY t1_id
+----
+11 a z
+22 b y
+44 d x
+
+# TODO: sort_merge_join_on_date32
+
+# TODO: sort_merge_join_on_decimal
+
+
+# TODO: Left semi join
+
+# Left semi join pushdown
+
+query TT
+EXPLAIN
+SELECT lsaj_t1.t1_id, lsaj_t1.t1_name
+FROM lsaj_t1
+LEFT SEMI JOIN lsaj_t2 ON (lsaj_t1.t1_id = lsaj_t2.t2_id and lsaj_t2.t2_int > 1)
+----
+logical_plan
+LeftSemi Join: lsaj_t1.t1_id = lsaj_t2.t2_id
+--TableScan: lsaj_t1 projection=[t1_id, t1_name]
+--Projection: lsaj_t2.t2_id
+----Filter: lsaj_t2.t2_int > UInt32(1)
+------TableScan: lsaj_t2 projection=[t2_id, t2_int]
+
+# Left anti join
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+query IT
+SELECT t1_id, t1_name
+FROM lsaj_t1
+WHERE NOT EXISTS (SELECT 1 FROM lsaj_t2 WHERE t1_id = t2_id)
+ORDER BY t1_id
+----
+33 c
+NULL e
+
+query I
+SELECT t1_id
+FROM lsaj_t1
+EXCEPT SELECT t2_id FROM lsaj_t2
+ORDER BY t1_id
+----
+33
+
+query IT
+SELECT t1_id, t1_name
+FROM lsaj_t1
+LEFT ANTI JOIN lsaj_t2 ON (t1_id = t2_id)
+ORDER BY t1_id
+----
+33 c
+NULL e
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+query IT
+SELECT t1_id, t1_name
+FROM lsaj_t1
+WHERE NOT EXISTS (SELECT 1 FROM lsaj_t2 WHERE t1_id = t2_id)
+ORDER BY t1_id
+----
+33 c
+NULL e
+
+query I
+SELECT t1_id
+FROM lsaj_t1
+EXCEPT SELECT t2_id FROM lsaj_t2
+ORDER BY t1_id
+----
+33
+
+query IT
+SELECT t1_id, t1_name
+FROM lsaj_t1
+LEFT ANTI JOIN lsaj_t2 ON (t1_id = t2_id)
+ORDER BY t1_id
+----
+33 c
+NULL e
+
+# Error left anti join
+# https://github.com/apache/arrow-datafusion/issues/4366
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+query IT
+SELECT t1_id, t1_name
+FROM lsaj_t1
+WHERE NOT EXISTS (SELECT 1 FROM lsaj_t2 WHERE t1_id = t2_id and t1_id > 11)
+ORDER BY t1_id
+----
+11 a
+11 a
+33 c
+NULL e
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+query IT
+SELECT t1_id, t1_name
+FROM lsaj_t1
+WHERE NOT EXISTS (SELECT 1 FROM lsaj_t2 WHERE t1_id = t2_id and t1_id > 11)
+ORDER BY t1_id
+----
+11 a
+11 a
+33 c
+NULL e
+
+# TODO: null_aware_left_anti_join
+
+# TODO: right_semi_join
+
+# Join and aggregate on same key
+
+statement ok
+set datafusion.explain.logical_plan_only = false;
+
+query TT
+EXPLAIN
+select distinct(join_t1.t1_id)
+from join_t1
+inner join join_t2 on join_t1.t1_id = join_t2.t2_id
+----
+logical_plan
+Aggregate: groupBy=[[join_t1.t1_id]], aggr=[[]]
+--Projection: join_t1.t1_id
+----Inner Join: join_t1.t1_id = join_t2.t2_id
+------TableScan: join_t1 projection=[t1_id]
+------TableScan: join_t2 projection=[t2_id]
+physical_plan
+AggregateExec: mode=Single, gby=[t1_id@0 as t1_id], aggr=[]
+--ProjectionExec: expr=[t1_id@0 as t1_id]
+----CoalesceBatchesExec: target_batch_size=4096
+------HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: "t1_id", index: 0 }, Column { name: "t2_id", index: 0 })]
+--------CoalesceBatchesExec: target_batch_size=4096
+----------RepartitionExec: partitioning=Hash([Column { name: "t1_id", index: 0 }], 2), input_partitions=2
+------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+--------------MemoryExec: partitions=1, partition_sizes=[1]
+--------CoalesceBatchesExec: target_batch_size=4096
+----------RepartitionExec: partitioning=Hash([Column { name: "t2_id", index: 0 }], 2), input_partitions=2
+------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+--------------MemoryExec: partitions=1, partition_sizes=[1]
+
+query TT
+EXPLAIN
+select count(*)
+from (select * from join_t1 inner join join_t2 on join_t1.t1_id = join_t2.t2_id)
+group by t1_id
+----
+logical_plan
+Projection: COUNT(UInt8(1))
+--Aggregate: groupBy=[[join_t1.t1_id]], aggr=[[COUNT(UInt8(1))]]
+----Projection: join_t1.t1_id
+------Inner Join: join_t1.t1_id = join_t2.t2_id
+--------TableScan: join_t1 projection=[t1_id]
+--------TableScan: join_t2 projection=[t2_id]
+physical_plan
+ProjectionExec: expr=[COUNT(UInt8(1))@1 as COUNT(UInt8(1))]
+--AggregateExec: mode=Single, gby=[t1_id@0 as t1_id], aggr=[COUNT(UInt8(1))]
+----ProjectionExec: expr=[t1_id@0 as t1_id]
+------CoalesceBatchesExec: target_batch_size=4096
+--------HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: "t1_id", index: 0 }, Column { name: "t2_id", index: 0 })]
+----------CoalesceBatchesExec: target_batch_size=4096
+------------RepartitionExec: partitioning=Hash([Column { name: "t1_id", index: 0 }], 2), input_partitions=2
+--------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+----------------MemoryExec: partitions=1, partition_sizes=[1]
+----------CoalesceBatchesExec: target_batch_size=4096
+------------RepartitionExec: partitioning=Hash([Column { name: "t2_id", index: 0 }], 2), input_partitions=2
+--------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+----------------MemoryExec: partitions=1, partition_sizes=[1]
+
+query TT
+EXPLAIN
+select count(distinct join_t1.t1_id)
+from join_t1
+inner join join_t2 on join_t1.t1_id = join_t2.t2_id
+----
+logical_plan
+Projection: COUNT(alias1) AS COUNT(DISTINCT join_t1.t1_id)
+--Aggregate: groupBy=[[]], aggr=[[COUNT(alias1)]]
+----Aggregate: groupBy=[[join_t1.t1_id AS alias1]], aggr=[[]]
+------Projection: join_t1.t1_id
+--------Inner Join: join_t1.t1_id = join_t2.t2_id
+----------TableScan: join_t1 projection=[t1_id]
+----------TableScan: join_t2 projection=[t2_id]
+physical_plan
+ProjectionExec: expr=[COUNT(alias1)@0 as COUNT(DISTINCT join_t1.t1_id)]
+--AggregateExec: mode=Final, gby=[], aggr=[COUNT(alias1)]
+----CoalescePartitionsExec
+------AggregateExec: mode=Partial, gby=[], aggr=[COUNT(alias1)]
+--------AggregateExec: mode=FinalPartitioned, gby=[alias1@0 as alias1], aggr=[]
+----------AggregateExec: mode=Partial, gby=[t1_id@0 as alias1], aggr=[]
+------------ProjectionExec: expr=[t1_id@0 as t1_id]
+--------------CoalesceBatchesExec: target_batch_size=4096
+----------------HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: "t1_id", index: 0 }, Column { name: "t2_id", index: 0 })]
+------------------CoalesceBatchesExec: target_batch_size=4096
+--------------------RepartitionExec: partitioning=Hash([Column { name: "t1_id", index: 0 }], 2), input_partitions=2
+----------------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+------------------------MemoryExec: partitions=1, partition_sizes=[1]
+------------------CoalesceBatchesExec: target_batch_size=4096
+--------------------RepartitionExec: partitioning=Hash([Column { name: "t2_id", index: 0 }], 2), input_partitions=2
+----------------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+------------------------MemoryExec: partitions=1, partition_sizes=[1]
+
+statement ok
+set datafusion.explain.logical_plan_only = true;
+
+# Reduce cross join with expr join key all (to inner join)
+
+query TT
+EXPLAIN
+select *
+from join_t1
+cross join join_t2
+where join_t1.t1_id + 12 = join_t2.t2_id + 1
+----
+logical_plan
+Inner Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = CAST(join_t2.t2_id AS Int64) + Int64(1)
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+
+# Reduce cross join with cast expr join key (to inner join)
+
+query TT
+EXPLAIN
+select join_t1.t1_id, join_t2.t2_id, join_t1.t1_name
+from join_t1
+cross join join_t2 where join_t1.t1_id + 11 = cast(join_t2.t2_id as BIGINT)
+----
+logical_plan
+Projection: join_t1.t1_id, join_t2.t2_id, join_t1.t1_name
+--Inner Join: CAST(join_t1.t1_id AS Int64) + Int64(11) = CAST(join_t2.t2_id AS Int64)
+----TableScan: join_t1 projection=[t1_id, t1_name]
+----TableScan: join_t2 projection=[t2_id]
+
+
+#####
+# Config setup
+#####
+
+statement ok
+set datafusion.explain.logical_plan_only = false;
+
+# Reduce cross join with wildcard and expr (to inner join)
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+query TT
+EXPLAIN
+select *, join_t1.t1_id + 11
+from join_t1, join_t2
+where join_t1.t1_id + 11 = join_t2.t2_id
+----
+logical_plan
+Projection: join_t1.t1_id, join_t1.t1_name, join_t1.t1_int, join_t2.t2_id, join_t2.t2_name, join_t2.t2_int, CAST(join_t1.t1_id AS Int64) + Int64(11)
+--Inner Join: CAST(join_t1.t1_id AS Int64) + Int64(11) = CAST(join_t2.t2_id AS Int64)
+----TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+----TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+physical_plan
+ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, t2_id@3 as t2_id, t2_name@4 as t2_name, t2_int@5 as t2_int, CAST(t1_id@0 AS Int64) + 11 as join_t1.t1_id + Int64(11)]
+--ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, t2_id@4 as t2_id, t2_name@5 as t2_name, t2_int@6 as t2_int]
+----CoalesceBatchesExec: target_batch_size=4096
+------HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(Column { name: "join_t1.t1_id + Int64(11)", index: 3 }, Column { name: "CAST(join_t2.t2_id AS Int64)", index: 3 })]
+--------CoalescePartitionsExec
+----------ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, CAST(t1_id@0 AS Int64) + 11 as join_t1.t1_id + Int64(11)]
+------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+--------------MemoryExec: partitions=1, partition_sizes=[1]
+--------ProjectionExec: expr=[t2_id@0 as t2_id, t2_name@1 as t2_name, t2_int@2 as t2_int, CAST(t2_id@0 AS Int64) as CAST(join_t2.t2_id AS Int64)]
+----------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+------------MemoryExec: partitions=1, partition_sizes=[1]
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+query TT
+EXPLAIN
+select *, join_t1.t1_id + 11
+from join_t1, join_t2
+where join_t1.t1_id + 11 = join_t2.t2_id
+----
+logical_plan
+Projection: join_t1.t1_id, join_t1.t1_name, join_t1.t1_int, join_t2.t2_id, join_t2.t2_name, join_t2.t2_int, CAST(join_t1.t1_id AS Int64) + Int64(11)
+--Inner Join: CAST(join_t1.t1_id AS Int64) + Int64(11) = CAST(join_t2.t2_id AS Int64)
+----TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+----TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+physical_plan
+ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, t2_id@3 as t2_id, t2_name@4 as t2_name, t2_int@5 as t2_int, CAST(t1_id@0 AS Int64) + 11 as join_t1.t1_id + Int64(11)]
+--ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, t2_id@4 as t2_id, t2_name@5 as t2_name, t2_int@6 as t2_int]
+----CoalesceBatchesExec: target_batch_size=4096
+------HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: "join_t1.t1_id + Int64(11)", index: 3 }, Column { name: "CAST(join_t2.t2_id AS Int64)", index: 3 })]
+--------CoalesceBatchesExec: target_batch_size=4096
+----------RepartitionExec: partitioning=Hash([Column { name: "join_t1.t1_id + Int64(11)", index: 3 }], 2), input_partitions=2
+------------ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, CAST(t1_id@0 AS Int64) + 11 as join_t1.t1_id + Int64(11)]
+--------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+----------------MemoryExec: partitions=1, partition_sizes=[1]
+--------CoalesceBatchesExec: target_batch_size=4096
+----------RepartitionExec: partitioning=Hash([Column { name: "CAST(join_t2.t2_id AS Int64)", index: 3 }], 2), input_partitions=2
+------------ProjectionExec: expr=[t2_id@0 as t2_id, t2_name@1 as t2_name, t2_int@2 as t2_int, CAST(t2_id@0 AS Int64) as CAST(join_t2.t2_id AS Int64)]
+--------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+----------------MemoryExec: partitions=1, partition_sizes=[1]
+
+# Both side expr key inner join
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+query TT
+EXPLAIN
+SELECT join_t1.t1_id, join_t2.t2_id, join_t1.t1_name
+FROM join_t1
+INNER JOIN join_t2 ON join_t1.t1_id + cast(12 as INT UNSIGNED) = join_t2.t2_id + cast(1 as INT UNSIGNED)
+----
+logical_plan
+Projection: join_t1.t1_id, join_t2.t2_id, join_t1.t1_name
+--Inner Join: join_t1.t1_id + UInt32(12) = join_t2.t2_id + UInt32(1)
+----TableScan: join_t1 projection=[t1_id, t1_name]
+----TableScan: join_t2 projection=[t2_id]
+physical_plan
+ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name]
+--ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@3 as t2_id]
+----CoalesceBatchesExec: target_batch_size=4096
+------HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(Column { name: "join_t1.t1_id + UInt32(12)", index: 2 }, Column { name: "join_t2.t2_id + UInt32(1)", index: 1 })]
+--------CoalescePartitionsExec
+----------ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_id@0 + 12 as join_t1.t1_id + UInt32(12)]
+------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+--------------MemoryExec: partitions=1, partition_sizes=[1]
+--------ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 + 1 as join_t2.t2_id + UInt32(1)]
+----------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+------------MemoryExec: partitions=1, partition_sizes=[1]
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+query TT
+EXPLAIN
+SELECT join_t1.t1_id, join_t2.t2_id, join_t1.t1_name
+FROM join_t1
+INNER JOIN join_t2 ON join_t1.t1_id + cast(12 as INT UNSIGNED) = join_t2.t2_id + cast(1 as INT UNSIGNED)
+----
+logical_plan
+Projection: join_t1.t1_id, join_t2.t2_id, join_t1.t1_name
+--Inner Join: join_t1.t1_id + UInt32(12) = join_t2.t2_id + UInt32(1)
+----TableScan: join_t1 projection=[t1_id, t1_name]
+----TableScan: join_t2 projection=[t2_id]
+physical_plan
+ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name]
+--ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@3 as t2_id]
+----CoalesceBatchesExec: target_batch_size=4096
+------HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: "join_t1.t1_id + UInt32(12)", index: 2 }, Column { name: "join_t2.t2_id + UInt32(1)", index: 1 })]
+--------CoalesceBatchesExec: target_batch_size=4096
+----------RepartitionExec: partitioning=Hash([Column { name: "join_t1.t1_id + UInt32(12)", index: 2 }], 2), input_partitions=2
+------------ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_id@0 + 12 as join_t1.t1_id + UInt32(12)]
+--------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+----------------MemoryExec: partitions=1, partition_sizes=[1]
+--------CoalesceBatchesExec: target_batch_size=4096
+----------RepartitionExec: partitioning=Hash([Column { name: "join_t2.t2_id + UInt32(1)", index: 1 }], 2), input_partitions=2
+------------ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 + 1 as join_t2.t2_id + UInt32(1)]
+--------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+----------------MemoryExec: partitions=1, partition_sizes=[1]
+
+# Left side expr key inner join
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+query TT
+EXPLAIN
+SELECT join_t1.t1_id, join_t2.t2_id, join_t1.t1_name
+FROM join_t1
+INNER JOIN join_t2
+ON join_t1.t1_id + cast(11 as INT UNSIGNED) = join_t2.t2_id
+----
+logical_plan
+Projection: join_t1.t1_id, join_t2.t2_id, join_t1.t1_name
+--Inner Join: join_t1.t1_id + UInt32(11) = join_t2.t2_id
+----TableScan: join_t1 projection=[t1_id, t1_name]
+----TableScan: join_t2 projection=[t2_id]
+physical_plan
+ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name]
+--ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@3 as t2_id]
+----CoalesceBatchesExec: target_batch_size=4096
+------HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(Column { name: "join_t1.t1_id + UInt32(11)", index: 2 }, Column { name: "t2_id", index: 0 })]
+--------CoalescePartitionsExec
+----------ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_id@0 + 11 as join_t1.t1_id + UInt32(11)]
+------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+--------------MemoryExec: partitions=1, partition_sizes=[1]
+--------MemoryExec: partitions=1, partition_sizes=[1]
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+query TT
+EXPLAIN
+SELECT join_t1.t1_id, join_t2.t2_id, join_t1.t1_name
+FROM join_t1
+INNER JOIN join_t2
+ON join_t1.t1_id + cast(11 as INT UNSIGNED) = join_t2.t2_id
+----
+logical_plan
+Projection: join_t1.t1_id, join_t2.t2_id, join_t1.t1_name
+--Inner Join: join_t1.t1_id + UInt32(11) = join_t2.t2_id
+----TableScan: join_t1 projection=[t1_id, t1_name]
+----TableScan: join_t2 projection=[t2_id]
+physical_plan
+ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name]
+--ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@3 as t2_id]
+----CoalesceBatchesExec: target_batch_size=4096
+------HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: "join_t1.t1_id + UInt32(11)", index: 2 }, Column { name: "t2_id", index: 0 })]
+--------CoalesceBatchesExec: target_batch_size=4096
+----------RepartitionExec: partitioning=Hash([Column { name: "join_t1.t1_id + UInt32(11)", index: 2 }], 2), input_partitions=2
+------------ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_id@0 + 11 as join_t1.t1_id + UInt32(11)]
+--------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+----------------MemoryExec: partitions=1, partition_sizes=[1]
+--------CoalesceBatchesExec: target_batch_size=4096
+----------RepartitionExec: partitioning=Hash([Column { name: "t2_id", index: 0 }], 2), input_partitions=2
+------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+--------------MemoryExec: partitions=1, partition_sizes=[1]
+
+# Right side expr key inner join
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+query TT
+EXPLAIN
+SELECT join_t1.t1_id, join_t2.t2_id, join_t1.t1_name
+FROM join_t1
+INNER JOIN join_t2
+ON join_t1.t1_id = join_t2.t2_id - cast(11 as INT UNSIGNED)
+----
+logical_plan
+Projection: join_t1.t1_id, join_t2.t2_id, join_t1.t1_name
+--Inner Join: join_t1.t1_id = join_t2.t2_id - UInt32(11)
+----TableScan: join_t1 projection=[t1_id, t1_name]
+----TableScan: join_t2 projection=[t2_id]
+physical_plan
+ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name]
+--ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@2 as t2_id]
+----CoalesceBatchesExec: target_batch_size=4096
+------HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(Column { name: "t1_id", index: 0 }, Column { name: "join_t2.t2_id - UInt32(11)", index: 1 })]
+--------MemoryExec: partitions=1, partition_sizes=[1]
+--------ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 - 11 as join_t2.t2_id - UInt32(11)]
+----------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+------------MemoryExec: partitions=1, partition_sizes=[1]
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+query TT
+EXPLAIN
+SELECT join_t1.t1_id, join_t2.t2_id, join_t1.t1_name
+FROM join_t1
+INNER JOIN join_t2
+ON join_t1.t1_id = join_t2.t2_id - cast(11 as INT UNSIGNED)
+----
+logical_plan
+Projection: join_t1.t1_id, join_t2.t2_id, join_t1.t1_name
+--Inner Join: join_t1.t1_id = join_t2.t2_id - UInt32(11)
+----TableScan: join_t1 projection=[t1_id, t1_name]
+----TableScan: join_t2 projection=[t2_id]
+physical_plan
+ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name]
+--ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@2 as t2_id]
+----CoalesceBatchesExec: target_batch_size=4096
+------HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: "t1_id", index: 0 }, Column { name: "join_t2.t2_id - UInt32(11)", index: 1 })]
+--------CoalesceBatchesExec: target_batch_size=4096
+----------RepartitionExec: partitioning=Hash([Column { name: "t1_id", index: 0 }], 2), input_partitions=2
+------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+--------------MemoryExec: partitions=1, partition_sizes=[1]
+--------CoalesceBatchesExec: target_batch_size=4096
+----------RepartitionExec: partitioning=Hash([Column { name: "join_t2.t2_id - UInt32(11)", index: 1 }], 2), input_partitions=2
+------------ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 - 11 as join_t2.t2_id - UInt32(11)]
+--------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+----------------MemoryExec: partitions=1, partition_sizes=[1]
+
+# Select wildcard with expr key inner join
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+INNER JOIN join_t2
+ON join_t1.t1_id = join_t2.t2_id - cast(11 as INT UNSIGNED)
+----
+logical_plan
+Inner Join: join_t1.t1_id = join_t2.t2_id - UInt32(11)
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+physical_plan
+ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, t2_id@3 as t2_id, t2_name@4 as t2_name, t2_int@5 as t2_int]
+--CoalesceBatchesExec: target_batch_size=4096
+----HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(Column { name: "t1_id", index: 0 }, Column { name: "join_t2.t2_id - UInt32(11)", index: 3 })]
+------MemoryExec: partitions=1, partition_sizes=[1]
+------ProjectionExec: expr=[t2_id@0 as t2_id, t2_name@1 as t2_name, t2_int@2 as t2_int, t2_id@0 - 11 as join_t2.t2_id - UInt32(11)]
+--------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+----------MemoryExec: partitions=1, partition_sizes=[1]
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+INNER JOIN join_t2
+ON join_t1.t1_id = join_t2.t2_id - cast(11 as INT UNSIGNED)
+----
+logical_plan
+Inner Join: join_t1.t1_id = join_t2.t2_id - UInt32(11)
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+physical_plan
+ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_int@2 as t1_int, t2_id@3 as t2_id, t2_name@4 as t2_name, t2_int@5 as t2_int]
+--CoalesceBatchesExec: target_batch_size=4096
+----HashJoinExec: mode=Partitioned, join_type=Inner, on=[(Column { name: "t1_id", index: 0 }, Column { name: "join_t2.t2_id - UInt32(11)", index: 3 })]
+------CoalesceBatchesExec: target_batch_size=4096
+--------RepartitionExec: partitioning=Hash([Column { name: "t1_id", index: 0 }], 2), input_partitions=2
+----------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+------------MemoryExec: partitions=1, partition_sizes=[1]
+------CoalesceBatchesExec: target_batch_size=4096
+--------RepartitionExec: partitioning=Hash([Column { name: "join_t2.t2_id - UInt32(11)", index: 3 }], 2), input_partitions=2
+----------ProjectionExec: expr=[t2_id@0 as t2_id, t2_name@1 as t2_name, t2_int@2 as t2_int, t2_id@0 - 11 as join_t2.t2_id - UInt32(11)]
+------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+--------------MemoryExec: partitions=1, partition_sizes=[1]
+
+#####
+# Config teardown
+#####
+
+statement ok
+set datafusion.explain.logical_plan_only = true;
+
+
+# Join with type coercion for equi expr
+
+query TT
+EXPLAIN
+select join_t1.t1_id, join_t1.t1_name, join_t2.t2_id
+from join_t1
+inner join join_t2 on join_t1.t1_id + 11 = join_t2.t2_id
+----
+logical_plan
+Inner Join: CAST(join_t1.t1_id AS Int64) + Int64(11) = CAST(join_t2.t2_id AS Int64)
+--TableScan: join_t1 projection=[t1_id, t1_name]
+--TableScan: join_t2 projection=[t2_id]
+
+# Join only with filter
+
+query TT
+EXPLAIN
+select join_t1.t1_id, join_t1.t1_name, join_t2.t2_id
+from join_t1
+inner join join_t2 on join_t1.t1_id * 4 < join_t2.t2_id
+----
+logical_plan
+Inner Join: Filter: CAST(join_t1.t1_id AS Int64) * Int64(4) < CAST(join_t2.t2_id AS Int64)
+--TableScan: join_t1 projection=[t1_id, t1_name]
+--TableScan: join_t2 projection=[t2_id]
+
+# Type coercion join with filter and equi expr
+
+query TT
+EXPLAIN
+select join_t1.t1_id, join_t1.t1_name, join_t2.t2_id
+from join_t1
+inner join join_t2
+on join_t1.t1_id * 5 = join_t2.t2_id and join_t1.t1_id * 4 < join_t2.t2_id
+----
+logical_plan
+Inner Join: CAST(join_t1.t1_id AS Int64) * Int64(5) = CAST(join_t2.t2_id AS Int64) Filter: CAST(join_t1.t1_id AS Int64) * Int64(4) < CAST(join_t2.t2_id AS Int64)
+--TableScan: join_t1 projection=[t1_id, t1_name]
+--TableScan: join_t2 projection=[t2_id]
+
+# Test cross join to groupby with different key ordering
+
+statement ok
+CREATE TABLE tbl(col1 VARCHAR, col2 BIGINT UNSIGNED, col3 BIGINT UNSIGNED)
+AS VALUES
+('A', 1, 1),
+('A', 1, 1),
+('A', 2, 1),
+('A', 2, 1),
+('A', 3, 1),
+('A', 3, 1),
+('A', 4, 1),
+('A', 4, 1),
+('BB', 5, 1),
+('BB', 5, 1),
+('BB', 6, 1),
+('BB', 6, 1);
+
+query TIR
+select col1, col2, coalesce(sum_col3, 0) as sum_col3
+from (select distinct col2 from tbl) AS q1
+cross join (select distinct col1 from tbl) AS q2
+left outer join (SELECT col1, col2, sum(col3) as sum_col3 FROM tbl GROUP BY col1, col2) AS q3
+USING(col2, col1)
+ORDER BY col1, col2
+----
+A 1 2
+A 2 2
+A 3 2
+A 4 2
+A 5 0
+A 6 0
+BB 1 0
+BB 2 0
+BB 3 0
+BB 4 0
+BB 5 2
+BB 6 2
+
+statement ok
+DROP TABLE tbl;
+
+# Subquery to join with both side expr
+
+query TT
+EXPLAIN
+select join_t1.t1_id, join_t1.t1_name, join_t1.t1_int
+from join_t1
+where join_t1.t1_id + 12 in (select join_t2.t2_id + 1 from join_t2)
+----
+logical_plan
+LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_5.CAST(t2_id AS Int64) + Int64(1)
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--SubqueryAlias: __correlated_sq_5
+----Projection: CAST(join_t2.t2_id AS Int64) + Int64(1) AS CAST(t2_id AS Int64) + Int64(1)
+------TableScan: join_t2 projection=[t2_id]
+
+query ITI rowsort
+select join_t1.t1_id, join_t1.t1_name, join_t1.t1_int
+from join_t1
+where join_t1.t1_id + 12 in (select join_t2.t2_id + 1 from join_t2)
+----
+11 a 1
+33 c 3
+44 d 4
+
+# Subquery to join with multi filter
+
+query TT
+EXPLAIN
+select join_t1.t1_id, join_t1.t1_name, join_t1.t1_int
+from join_t1
+where join_t1.t1_id + 12 in
+ (
+ select join_t2.t2_id + 1
+ from join_t2
+ where join_t1.t1_int <= join_t2.t2_int and join_t2.t2_int > 0
+ )
+----
+logical_plan
+LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_7.CAST(t2_id AS Int64) + Int64(1) Filter: join_t1.t1_int <= __correlated_sq_7.t2_int
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--SubqueryAlias: __correlated_sq_7
+----Projection: CAST(join_t2.t2_id AS Int64) + Int64(1) AS CAST(t2_id AS Int64) + Int64(1), join_t2.t2_int
+------Filter: join_t2.t2_int > UInt32(0)
+--------TableScan: join_t2 projection=[t2_id, t2_int]
+
+query ITI rowsort
+select join_t1.t1_id, join_t1.t1_name, join_t1.t1_int
+from join_t1
+where join_t1.t1_id + 12 in
+ (
+ select join_t2.t2_id + 1
+ from join_t2
+ where join_t1.t1_int <= join_t2.t2_int and join_t2.t2_int > 0
+ )
+----
+11 a 1
+33 c 3
+
+# Three projection exprs subquery to join
+
+query TT
+EXPLAIN
+select join_t1.t1_id, join_t1.t1_name, join_t1.t1_int
+from join_t1
+where join_t1.t1_id + 12 in
+ (
+ select join_t2.t2_id + 1
+ from join_t2
+ where join_t1.t1_int <= join_t2.t2_int
+ and join_t1.t1_name != join_t2.t2_name
+ and join_t2.t2_int > 0
+ )
+----
+logical_plan
+LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_9.CAST(t2_id AS Int64) + Int64(1) Filter: join_t1.t1_int <= __correlated_sq_9.t2_int AND join_t1.t1_name != __correlated_sq_9.t2_name
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--SubqueryAlias: __correlated_sq_9
+----Projection: CAST(join_t2.t2_id AS Int64) + Int64(1) AS CAST(t2_id AS Int64) + Int64(1), join_t2.t2_int, join_t2.t2_name
+------Filter: join_t2.t2_int > UInt32(0)
+--------TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+
+query ITI rowsort
+select join_t1.t1_id, join_t1.t1_name, join_t1.t1_int
+from join_t1
+where join_t1.t1_id + 12 in
+ (
+ select join_t2.t2_id + 1
+ from join_t2
+ where join_t1.t1_int <= join_t2.t2_int
+ and join_t1.t1_name != join_t2.t2_name
+ and join_t2.t2_int > 0
+ )
+----
+11 a 1
+33 c 3
+
+# In subquery to join with correlated outer filter
+
+query TT
+EXPLAIN
+select join_t1.t1_id, join_t1.t1_name, join_t1.t1_int
+from join_t1
+where join_t1.t1_id + 12 in
+ (select join_t2.t2_id + 1 from join_t2 where join_t1.t1_int > 0)
+----
+logical_plan
+LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_11.CAST(t2_id AS Int64) + Int64(1)
+--Filter: join_t1.t1_int > UInt32(0)
+----TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--SubqueryAlias: __correlated_sq_11
+----Projection: CAST(join_t2.t2_id AS Int64) + Int64(1) AS CAST(t2_id AS Int64) + Int64(1)
+------TableScan: join_t2 projection=[t2_id]
+
+# Not in subquery to join with correlated outer filter
+
+query TT
+EXPLAIN
+select join_t1.t1_id, join_t1.t1_name, join_t1.t1_int
+from join_t1
+where join_t1.t1_id + 12 not in
+ (select join_t2.t2_id + 1 from join_t2 where join_t1.t1_int > 0)
+----
+logical_plan
+LeftAnti Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_12.CAST(t2_id AS Int64) + Int64(1) Filter: join_t1.t1_int > UInt32(0)
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--SubqueryAlias: __correlated_sq_12
+----Projection: CAST(join_t2.t2_id AS Int64) + Int64(1) AS CAST(t2_id AS Int64) + Int64(1)
+------TableScan: join_t2 projection=[t2_id]
+
+# In subquery to join with outer filter
+
+query TT
+EXPLAIN
+select join_t1.t1_id, join_t1.t1_name, join_t1.t1_int
+from join_t1
+where join_t1.t1_id + 12 in
+ (
+ select join_t2.t2_id + 1
+ from join_t2
+ where join_t1.t1_int <= join_t2.t2_int
+ and join_t1.t1_name != join_t2.t2_name
+ )
+ and join_t1.t1_id > 0
+----
+logical_plan
+LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_13.CAST(t2_id AS Int64) + Int64(1) Filter: join_t1.t1_int <= __correlated_sq_13.t2_int AND join_t1.t1_name != __correlated_sq_13.t2_name
+--Filter: join_t1.t1_id > UInt32(0)
+----TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--SubqueryAlias: __correlated_sq_13
+----Projection: CAST(join_t2.t2_id AS Int64) + Int64(1) AS CAST(t2_id AS Int64) + Int64(1), join_t2.t2_int, join_t2.t2_name
+------TableScan: join_t2 projection=[t2_id, t2_name, t2_int]
+
+query ITI rowsort
+select join_t1.t1_id, join_t1.t1_name, join_t1.t1_int
+from join_t1
+where join_t1.t1_id + 12 in
+ (
+ select join_t2.t2_id + 1
+ from join_t2
+ where join_t1.t1_int <= join_t2.t2_int
+ and join_t1.t1_name != join_t2.t2_name
+ )
+ and join_t1.t1_id > 0
+----
+11 a 1
+33 c 3
+
+# Two in subquery to join with outer filter
+
+query TT
+EXPLAIN
+select join_t1.t1_id, join_t1.t1_name, join_t1.t1_int
+from join_t1
+where join_t1.t1_id + 12 in (select join_t2.t2_id + 1 from join_t2)
+ and join_t1.t1_int in (select join_t2.t2_int + 1 from join_t2)
+ and join_t1.t1_id > 0
+----
+logical_plan
+LeftSemi Join: CAST(join_t1.t1_int AS Int64) = __correlated_sq_16.CAST(t2_int AS Int64) + Int64(1)
+--LeftSemi Join: CAST(join_t1.t1_id AS Int64) + Int64(12) = __correlated_sq_15.CAST(t2_id AS Int64) + Int64(1)
+----Filter: join_t1.t1_id > UInt32(0)
+------TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+----SubqueryAlias: __correlated_sq_15
+------Projection: CAST(join_t2.t2_id AS Int64) + Int64(1) AS CAST(t2_id AS Int64) + Int64(1)
+--------TableScan: join_t2 projection=[t2_id]
+--SubqueryAlias: __correlated_sq_16
+----Projection: CAST(join_t2.t2_int AS Int64) + Int64(1) AS CAST(t2_int AS Int64) + Int64(1)
+------TableScan: join_t2 projection=[t2_int]
+
+query ITI
+select join_t1.t1_id, join_t1.t1_name, join_t1.t1_int
+from join_t1
+where join_t1.t1_id + 12 in (select join_t2.t2_id + 1 from join_t2)
+ and join_t1.t1_int in (select join_t2.t2_int + 1 from join_t2)
+ and join_t1.t1_id > 0
+----
+44 d 4
+
+
+#####
+# Configuration setup
+#####
+
+statement ok
+set datafusion.explain.logical_plan_only = false;
+
+statement ok
+set datafusion.execution.target_partitions = 4;
+
+# Right as inner table nested loop join
+
+query TT
+EXPLAIN
+SELECT join_t1.t1_id, join_t2.t2_id
+FROM join_t1
+INNER JOIN join_t2 ON join_t1.t1_id > join_t2.t2_id
+WHERE join_t1.t1_id > 10 AND join_t2.t2_int > 1
+----
+logical_plan
+Inner Join: Filter: join_t1.t1_id > join_t2.t2_id
+--Filter: join_t1.t1_id > UInt32(10)
+----TableScan: join_t1 projection=[t1_id]
+--Projection: join_t2.t2_id
+----Filter: join_t2.t2_int > UInt32(1)
+------TableScan: join_t2 projection=[t2_id, t2_int]
+physical_plan
+NestedLoopJoinExec: join_type=Inner, filter=BinaryExpr { left: Column { name: "t1_id", index: 0 }, op: Gt, right: Column { name: "t2_id", index: 1 } }
+--CoalesceBatchesExec: target_batch_size=4096
+----FilterExec: t1_id@0 > 10
+------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
+--------MemoryExec: partitions=1, partition_sizes=[1]
+--CoalescePartitionsExec
+----ProjectionExec: expr=[t2_id@0 as t2_id]
+------CoalesceBatchesExec: target_batch_size=4096
+--------FilterExec: t2_int@1 > 1
+----------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
+------------MemoryExec: partitions=1, partition_sizes=[1]
+
+query II
+SELECT join_t1.t1_id, join_t2.t2_id
+FROM join_t1
+INNER JOIN join_t2 ON join_t1.t1_id > join_t2.t2_id
+WHERE join_t1.t1_id > 10 AND join_t2.t2_int > 1
+----
+22 11
+33 11
+44 11
+
+# Left as inner table nested loop join
+
+query TT
+EXPLAIN
+SELECT join_t1.t1_id, join_t2.t2_id
+FROM (select t1_id from join_t1 where join_t1.t1_id > 22) as join_t1
+RIGHT JOIN (select t2_id from join_t2 where join_t2.t2_id > 11) as join_t2
+ ON join_t1.t1_id < join_t2.t2_id
+----
+logical_plan
+Right Join: Filter: join_t1.t1_id < join_t2.t2_id
+--SubqueryAlias: join_t1
+----Filter: join_t1.t1_id > UInt32(22)
+------TableScan: join_t1 projection=[t1_id]
+--SubqueryAlias: join_t2
+----Filter: join_t2.t2_id > UInt32(11)
+------TableScan: join_t2 projection=[t2_id]
+physical_plan
+NestedLoopJoinExec: join_type=Right, filter=BinaryExpr { left: Column { name: "t1_id", index: 0 }, op: Lt, right: Column { name: "t2_id", index: 1 } }
+--CoalescePartitionsExec
+----CoalesceBatchesExec: target_batch_size=4096
+------FilterExec: t1_id@0 > 22
+--------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
+----------MemoryExec: partitions=1, partition_sizes=[1]
+--CoalesceBatchesExec: target_batch_size=4096
+----FilterExec: t2_id@0 > 11
+------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
+--------MemoryExec: partitions=1, partition_sizes=[1]
+
+query II
+SELECT join_t1.t1_id, join_t2.t2_id
+FROM (select t1_id from join_t1 where join_t1.t1_id > 22) as join_t1
+RIGHT JOIN (select t2_id from join_t2 where join_t2.t2_id > 11) as join_t2
+ ON join_t1.t1_id < join_t2.t2_id
+----
+33 44
+33 55
+44 55
+NULL 22
+
+#####
+# Configuration teardown
+#####
+
+statement ok
+set datafusion.execution.target_partitions = 2;
+
+statement ok
+set datafusion.explain.logical_plan_only = true;
+
+
+# Exists subquery to join expr filter
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+WHERE EXISTS (
+ SELECT t2_id
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+logical_plan
+LeftSemi Join: Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_19.t2_id AS Int64) * Int64(2)
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--SubqueryAlias: __correlated_sq_19
+----TableScan: join_t2 projection=[t2_id]
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+query ITI
+SELECT *
+FROM join_t1
+WHERE EXISTS (
+ SELECT t2_id
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+22 b 2
+33 c 3
+44 d 4
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+query ITI
+SELECT *
+FROM join_t1
+WHERE EXISTS (
+ SELECT t2_id
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+22 b 2
+33 c 3
+44 d 4
+
+# Exists subquery to join inner filter
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+WHERE EXISTS (
+ SELECT t2_id
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2 AND join_t2.t2_int < 3
+)
+----
+logical_plan
+LeftSemi Join: Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_22.t2_id AS Int64) * Int64(2)
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--SubqueryAlias: __correlated_sq_22
+----Projection: join_t2.t2_id
+------Filter: join_t2.t2_int < UInt32(3)
+--------TableScan: join_t2 projection=[t2_id, t2_int]
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+query ITI
+SELECT *
+FROM join_t1
+WHERE EXISTS (
+ SELECT t2_id
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2 AND join_t2.t2_int < 3
+)
+----
+44 d 4
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+query ITI
+SELECT *
+FROM join_t1
+WHERE EXISTS (
+ SELECT t2_id
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2 AND join_t2.t2_int < 3
+)
+----
+44 d 4
+
+# Exists subquery to join outer filter
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+WHERE EXISTS (
+ SELECT t2_id
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2 AND join_t1.t1_int < 3
+)
+----
+logical_plan
+LeftSemi Join: Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_25.t2_id AS Int64) * Int64(2)
+--Filter: join_t1.t1_int < UInt32(3)
+----TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--SubqueryAlias: __correlated_sq_25
+----TableScan: join_t2 projection=[t2_id]
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+query ITI
+SELECT *
+FROM join_t1
+WHERE EXISTS (
+ SELECT t2_id
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2 AND join_t1.t1_int < 3
+)
+----
+22 b 2
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+query ITI
+SELECT *
+FROM join_t1
+WHERE EXISTS (
+ SELECT t2_id
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2 AND join_t1.t1_int < 3
+)
+----
+22 b 2
+
+# Not exists subquery to join expr filter
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+WHERE NOT EXISTS (
+ SELECT t2_id
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+logical_plan
+LeftAnti Join: Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_28.t2_id AS Int64) * Int64(2)
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--SubqueryAlias: __correlated_sq_28
+----TableScan: join_t2 projection=[t2_id]
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+query ITI
+SELECT *
+FROM join_t1
+WHERE NOT EXISTS (
+ SELECT t2_id
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+11 a 1
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+query ITI
+SELECT *
+FROM join_t1
+WHERE NOT EXISTS (
+ SELECT t2_id
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+11 a 1
+
+# Exists distinct subquery to join
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+WHERE NOT EXISTS (
+ SELECT DISTINCT t2_int
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+logical_plan
+LeftAnti Join: Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_31.t2_id AS Int64) * Int64(2)
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--SubqueryAlias: __correlated_sq_31
+----Aggregate: groupBy=[[join_t2.t2_id]], aggr=[[]]
+------TableScan: join_t2 projection=[t2_id]
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+query ITI
+SELECT *
+FROM join_t1
+WHERE NOT EXISTS (
+ SELECT DISTINCT t2_int
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+11 a 1
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+query ITI
+SELECT *
+FROM join_t1
+WHERE NOT EXISTS (
+ SELECT DISTINCT t2_int
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+11 a 1
+
+# Exists distinct subquery to join with expr
+
+query TT
+EXPLAIN
+SELECT *
+FROM join_t1
+WHERE NOT EXISTS(
+ SELECT DISTINCT t2_id + t2_int, t2_int
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+logical_plan
+LeftAnti Join: Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_34.t2_id AS Int64) * Int64(2)
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--SubqueryAlias: __correlated_sq_34
+----Aggregate: groupBy=[[join_t2.t2_id]], aggr=[[]]
+------TableScan: join_t2 projection=[t2_id]
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+query ITI
+SELECT *
+FROM join_t1
+WHERE NOT EXISTS(
+ SELECT DISTINCT t2_id + t2_int, t2_int
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+11 a 1
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+query ITI
+SELECT *
+FROM join_t1
+WHERE NOT EXISTS(
+ SELECT DISTINCT t2_id + t2_int, t2_int
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+11 a 1
+
+# Exists distinct subquery left anti join with literal
+
+statement ok
+set datafusion.optimizer.repartition_joins = false;
+
+query TT
+EXPLAIN
+SELECT * FROM join_t1
+WHERE NOT EXISTS(
+ SELECT DISTINCT 1, t2_int
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+logical_plan
+LeftAnti Join: Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_37.t2_id AS Int64) * Int64(2)
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--SubqueryAlias: __correlated_sq_37
+----Aggregate: groupBy=[[join_t2.t2_id]], aggr=[[]]
+------TableScan: join_t2 projection=[t2_id]
+
+query ITI
+SELECT * FROM join_t1
+WHERE NOT EXISTS(
+ SELECT DISTINCT 1, t2_int
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+11 a 1
+
+statement ok
+set datafusion.optimizer.repartition_joins = true;
+
+query TT
+EXPLAIN
+SELECT * FROM join_t1
+WHERE NOT EXISTS(
+ SELECT DISTINCT 1, t2_int
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+logical_plan
+LeftAnti Join: Filter: CAST(join_t1.t1_id AS Int64) + Int64(1) > CAST(__correlated_sq_39.t2_id AS Int64) * Int64(2)
+--TableScan: join_t1 projection=[t1_id, t1_name, t1_int]
+--SubqueryAlias: __correlated_sq_39
+----Aggregate: groupBy=[[join_t2.t2_id]], aggr=[[]]
+------TableScan: join_t2 projection=[t2_id]
+
+query ITI
+SELECT * FROM join_t1
+WHERE NOT EXISTS(
+ SELECT DISTINCT 1, t2_int
+ FROM join_t2
+ WHERE join_t1.t1_id + 1 > join_t2.t2_id * 2
+)
+----
+11 a 1
+
+statement ok
+set datafusion.explain.logical_plan_only = false;