Skip to content

Apache DataFusion SQL Query Engine

feat: add support for RightAnti and RightSemi join types (#17604)

Bruno Volpato4e94346e7f89
diff --git a/datafusion/substrait/src/logical_plan/consumer/rel/join_rel.rs b/datafusion/substrait/src/logical_plan/consumer/rel/join_rel.rs
index ef78e1c971..5681c92326 100644
--- a/datafusion/substrait/src/logical_plan/consumer/rel/join_rel.rs
+++ b/datafusion/substrait/src/logical_plan/consumer/rel/join_rel.rs
@@ -145,6 +145,8 @@ fn from_substrait_jointype(join_type: i32) -> datafusion::common::Result<JoinTyp
join_rel::JoinType::LeftSemi => Ok(JoinType::LeftSemi),
join_rel::JoinType::LeftMark => Ok(JoinType::LeftMark),
join_rel::JoinType::RightMark => Ok(JoinType::RightMark),
+ join_rel::JoinType::RightAnti => Ok(JoinType::RightAnti),
+ join_rel::JoinType::RightSemi => Ok(JoinType::RightSemi),
_ => plan_err!("unsupported join type {substrait_join_type:?}"),
}
} else {
diff --git a/datafusion/substrait/src/logical_plan/producer/rel/join.rs b/datafusion/substrait/src/logical_plan/producer/rel/join.rs
index 3dbac636fe..835d3ee37a 100644
--- a/datafusion/substrait/src/logical_plan/producer/rel/join.rs
+++ b/datafusion/substrait/src/logical_plan/producer/rel/join.rs
@@ -115,8 +115,7 @@ fn to_substrait_jointype(join_type: JoinType) -> join_rel::JoinType {
JoinType::LeftSemi => join_rel::JoinType::LeftSemi,
JoinType::LeftMark => join_rel::JoinType::LeftMark,
JoinType::RightMark => join_rel::JoinType::RightMark,
- JoinType::RightAnti | JoinType::RightSemi => {
- unimplemented!()
- }
+ JoinType::RightAnti => join_rel::JoinType::RightAnti,
+ JoinType::RightSemi => join_rel::JoinType::RightSemi,
}
}
diff --git a/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs b/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
index bd5229554d..39e4984ab9 100644
--- a/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
+++ b/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
@@ -626,6 +626,66 @@ async fn roundtrip_exists_filter() -> Result<()> {
Ok(())
}
+#[tokio::test]
+async fn roundtrip_not_exists_filter_left_anti_join() -> Result<()> {
+ let plan = generate_plan_from_sql(
+ "SELECT ba.isbn, ba.author FROM book_author ba WHERE NOT EXISTS (SELECT 1 FROM book b WHERE b.isbn = ba.isbn)",
+ false,
+ true,
+ )
+ .await?;
+
+ assert_snapshot!(
+ plan,
+ @r#"
+ LeftAnti Join: book_author.isbn = book.isbn
+ TableScan: book_author projection=[isbn, author]
+ TableScan: book projection=[isbn]
+ "#
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn roundtrip_right_anti_join() -> Result<()> {
+ let plan = generate_plan_from_sql(
+ "SELECT * FROM book b RIGHT ANTI JOIN book_author ba ON b.isbn = ba.isbn",
+ false,
+ true,
+ )
+ .await?;
+
+ assert_snapshot!(
+ plan,
+ @r#"
+ RightAnti Join: book.isbn = book_author.isbn
+ TableScan: book projection=[isbn]
+ TableScan: book_author projection=[isbn, author]
+ "#
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn roundtrip_right_semi_join() -> Result<()> {
+ let plan = generate_plan_from_sql(
+ "SELECT * FROM book b RIGHT SEMI JOIN book_author ba ON b.isbn = ba.isbn",
+ false,
+ true,
+ )
+ .await?;
+
+ assert_snapshot!(
+ plan,
+ @r#"
+ RightSemi Join: book.isbn = book_author.isbn
+ TableScan: book projection=[isbn]
+ TableScan: book_author projection=[isbn, author]
+ "#
+ );
+ Ok(())
+}
+
#[tokio::test]
async fn inner_join() -> Result<()> {
let plan = generate_plan_from_sql(
@@ -1751,6 +1811,34 @@ async fn create_context() -> Result<SessionContext> {
ctx.register_csv("data2", "tests/testdata/data.csv", CsvReadOptions::new())
.await?;
+ // Register test tables for anti join tests
+ let book_fields = vec![
+ Field::new("isbn", DataType::Int64, false),
+ Field::new("title", DataType::Utf8, true),
+ Field::new("genre", DataType::Utf8, true),
+ ];
+ let book_schema = Schema::new(book_fields);
+ let mut book_options = CsvReadOptions::new();
+ book_options.schema = Some(&book_schema);
+ book_options.has_header = false;
+ ctx.register_csv("book", "tests/testdata/empty.csv", book_options)
+ .await?;
+
+ let book_author_fields = vec![
+ Field::new("isbn", DataType::Int64, true),
+ Field::new("author", DataType::Utf8, true),
+ ];
+ let book_author_schema = Schema::new(book_author_fields);
+ let mut book_author_options = CsvReadOptions::new();
+ book_author_options.schema = Some(&book_author_schema);
+ book_author_options.has_header = false;
+ ctx.register_csv(
+ "book_author",
+ "tests/testdata/empty.csv",
+ book_author_options,
+ )
+ .await?;
+
Ok(ctx)
}