Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DataFrame support for INTERSECT and update readme #1258

Merged
merged 1 commit into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ DataFusion also includes a simple command-line interactive SQL utility. See the
- [ ] Set Operations
- [x] UNION ALL
- [x] UNION
- [ ] INTERSECT
- [ ] INTERSECT ALL
- [x] INTERSECT
- [x] INTERSECT ALL
- [ ] EXCEPT
- [ ] EXCEPT ALL
- [x] Joins
Expand Down
15 changes: 15 additions & 0 deletions datafusion/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,19 @@ pub trait DataFrame: Send + Sync {
/// # }
/// ```
fn registry(&self) -> Arc<dyn FunctionRegistry>;

/// Calculate the intersection 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.intersect(df.clone())?;
/// # Ok(())
/// # }
/// ```
fn intersect(&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 @@ -231,6 +231,15 @@ impl DataFrame for DataFrameImpl {
.build()?,
)))
}

fn intersect(&self, dataframe: Arc<dyn DataFrame>) -> Result<Arc<dyn DataFrame>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like there is some code duplication between this and the one in sql planner, perhaps better to move this into logical plan builder so it can be reused in both places.

let left_plan = self.to_logical_plan();
let right_plan = dataframe.to_logical_plan();
Ok(Arc::new(DataFrameImpl::new(
self.ctx_state.clone(),
&LogicalPlanBuilder::intersect(left_plan, right_plan, true)?,
)))
}
}

#[cfg(test)]
Expand Down Expand Up @@ -438,6 +447,20 @@ mod tests {
task.await.expect("task completed successfully");
}

#[tokio::test]
async fn intersect() -> Result<()> {
let df = test_table().await?.select_columns(&["c1", "c3"])?;
let plan = df.intersect(df.clone())?;
let result = plan.to_logical_plan();
let expected = create_plan(
"SELECT c1, c3 FROM aggregate_test_100
INTERSECT 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