Apache DataFusion SQL Query Engine
feat: expose intersect distinct/except distinct in dataframe api (#16578)
Chen Chongchen14df97baf35d
diff --git a/datafusion/core/src/dataframe/mod.rs b/datafusion/core/src/dataframe/mod.rsindex febafc9a74..a19e6f5581 100644--- a/datafusion/core/src/dataframe/mod.rs+++ b/datafusion/core/src/dataframe/mod.rs@@ -1710,6 +1710,40 @@ impl DataFrame {})}+ /// Calculate the distinct intersection of two [`DataFrame`]s. The two [`DataFrame`]s must have exactly the same schema+ ///+ /// ```+ /// # use datafusion::prelude::*;+ /// # use datafusion::error::Result;+ /// # use datafusion_common::assert_batches_sorted_eq;+ /// # #[tokio::main]+ /// # async fn main() -> Result<()> {+ /// let ctx = SessionContext::new();+ /// let df = ctx.read_csv("tests/data/example.csv", CsvReadOptions::new()).await?;+ /// let d2 = ctx.read_csv("tests/data/example_long.csv", CsvReadOptions::new()).await?;+ /// let df = df.intersect_distinct(d2)?;+ /// let expected = vec![+ /// "+---+---+---+",+ /// "| a | b | c |",+ /// "+---+---+---+",+ /// "| 1 | 2 | 3 |",+ /// "+---+---+---+"+ /// ];+ /// # assert_batches_sorted_eq!(expected, &df.collect().await?);+ /// # Ok(())+ /// # }+ /// ```+ pub fn intersect_distinct(self, dataframe: DataFrame) -> Result<DataFrame> {+ let left_plan = self.plan;+ let right_plan = dataframe.plan;+ let plan = LogicalPlanBuilder::intersect(left_plan, right_plan, false)?;+ Ok(DataFrame {+ session_state: self.session_state,+ plan,+ projection_requires_validation: true,+ })+ }+/// Calculate the exception of two [`DataFrame`]s. The two [`DataFrame`]s must have exactly the same schema////// ```@@ -1746,6 +1780,42 @@ impl DataFrame {})}+ /// Calculate the distinct exception of two [`DataFrame`]s. The two [`DataFrame`]s must have exactly the same schema+ ///+ /// ```+ /// # use datafusion::prelude::*;+ /// # use datafusion::error::Result;+ /// # use datafusion_common::assert_batches_sorted_eq;+ /// # #[tokio::main]+ /// # async fn main() -> Result<()> {+ /// let ctx = SessionContext::new();+ /// let df = ctx.read_csv("tests/data/example_long.csv", CsvReadOptions::new()).await?;+ /// let d2 = ctx.read_csv("tests/data/example.csv", CsvReadOptions::new()).await?;+ /// let result = df.except_distinct(d2)?;+ /// // those columns are not in example.csv, but in example_long.csv+ /// let expected = vec![+ /// "+---+---+---+",+ /// "| a | b | c |",+ /// "+---+---+---+",+ /// "| 4 | 5 | 6 |",+ /// "| 7 | 8 | 9 |",+ /// "+---+---+---+"+ /// ];+ /// # assert_batches_sorted_eq!(expected, &result.collect().await?);+ /// # Ok(())+ /// # }+ /// ```+ pub fn except_distinct(self, dataframe: DataFrame) -> Result<DataFrame> {+ let left_plan = self.plan;+ let right_plan = dataframe.plan;+ let plan = LogicalPlanBuilder::except(left_plan, right_plan, false)?;+ Ok(DataFrame {+ session_state: self.session_state,+ plan,+ projection_requires_validation: true,+ })+ }+/// Execute this `DataFrame` and write the results to `table_name`.////// Returns a single [RecordBatch] containing a single column anddiff --git a/datafusion/core/tests/dataframe/mod.rs b/datafusion/core/tests/dataframe/mod.rsindex c55f5c8b49..84a9cdb1fa 100644--- a/datafusion/core/tests/dataframe/mod.rs+++ b/datafusion/core/tests/dataframe/mod.rs@@ -1360,6 +1360,36 @@ async fn except() -> Result<()> {Ok(())}+#[tokio::test]+async fn except_distinct() -> Result<()> {+ let df = test_table().await?.select_columns(&["c1", "c3"])?;+ let d2 = df.clone();+ let plan = df.except_distinct(d2)?;+ let result = plan.logical_plan().clone();+ let expected = create_plan(+ "SELECT c1, c3 FROM aggregate_test_100+ EXCEPT DISTINCT SELECT c1, c3 FROM aggregate_test_100",+ )+ .await?;+ assert_same_plan(&result, &expected);+ Ok(())+}++#[tokio::test]+async fn intersect_distinct() -> Result<()> {+ let df = test_table().await?.select_columns(&["c1", "c3"])?;+ let d2 = df.clone();+ let plan = df.intersect_distinct(d2)?;+ let result = plan.logical_plan().clone();+ let expected = create_plan(+ "SELECT c1, c3 FROM aggregate_test_100+ INTERSECT DISTINCT SELECT c1, c3 FROM aggregate_test_100",+ )+ .await?;+ assert_same_plan(&result, &expected);+ Ok(())+}+#[tokio::test]async fn register_table() -> Result<()> {let df = test_table().await?.select_columns(&["c1", "c12"])?;