Skip to content

Commit

Permalink
Dataframe supports except and update readme (#1261)
Browse files Browse the repository at this point in the history
  • Loading branch information
xudong963 authored Nov 12, 2021
1 parent 7060961 commit 0798ca4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ DataFusion also includes a simple command-line interactive SQL utility. See the
- [ ] Lists
- [x] Subqueries
- [x] Common table expressions
- [ ] Set Operations
- [x] Set Operations
- [x] UNION ALL
- [x] UNION
- [x] INTERSECT
- [x] INTERSECT ALL
- [ ] EXCEPT
- [ ] EXCEPT ALL
- [x] EXCEPT
- [x] EXCEPT ALL
- [x] Joins
- [x] INNER JOIN
- [x] LEFT JOIN
Expand Down
15 changes: 15 additions & 0 deletions datafusion/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,19 @@ pub trait DataFrame: Send + Sync {
/// # }
/// ```
fn intersect(&self, dataframe: Arc<dyn DataFrame>) -> Result<Arc<dyn DataFrame>>;

/// Calculate the exception of two [`DataFrame`]s. The two [`DataFrame`]s must have exactly the same schema
///
/// ```
/// # use datafusion::prelude::*;
/// # use datafusion::error::Result;
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// let mut ctx = ExecutionContext::new();
/// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new()).await?;
/// let df = df.except(df.clone())?;
/// # Ok(())
/// # }
/// ```
fn except(&self, dataframe: Arc<dyn DataFrame>) -> Result<Arc<dyn DataFrame>>;
}
23 changes: 23 additions & 0 deletions datafusion/src/execution/dataframe_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ impl DataFrame for DataFrameImpl {
&LogicalPlanBuilder::intersect(left_plan, right_plan, true)?,
)))
}

fn except(&self, dataframe: Arc<dyn DataFrame>) -> Result<Arc<dyn DataFrame>> {
let left_plan = self.to_logical_plan();
let right_plan = dataframe.to_logical_plan();
Ok(Arc::new(DataFrameImpl::new(
self.ctx_state.clone(),
&LogicalPlanBuilder::except(left_plan, right_plan, true)?,
)))
}
}

#[cfg(test)]
Expand Down Expand Up @@ -461,6 +470,20 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn except() -> Result<()> {
let df = test_table().await?.select_columns(&["c1", "c3"])?;
let plan = df.except(df.clone())?;
let result = plan.to_logical_plan();
let expected = create_plan(
"SELECT c1, c3 FROM aggregate_test_100
EXCEPT ALL SELECT c1, c3 FROM aggregate_test_100",
)
.await?;
assert_same_plan(&result, &expected);
Ok(())
}

/// Compare the formatted string representation of two plans for equality
fn assert_same_plan(plan1: &LogicalPlan, plan2: &LogicalPlan) {
assert_eq!(format!("{:?}", plan1), format!("{:?}", plan2));
Expand Down

0 comments on commit 0798ca4

Please sign in to comment.