Skip to content

Apache DataFusion SQL Query Engine

Add support for UNION sql (#1068)

carlosa8dedc87a60f
diff --git a/README.md b/README.md
index ed7e8bd0b2..b2c56a3cbf 100644
--- a/README.md
+++ b/README.md
@@ -212,7 +212,7 @@ DataFusion also includes a simple command-line interactive SQL utility. See the
- [x] Common table expressions
- [ ] Set Operations
- [x] UNION ALL
- - [ ] UNION
+ - [x] UNION
- [ ] INTERSECT
- [ ] MINUS
- [x] Joins
diff --git a/datafusion/src/dataframe.rs b/datafusion/src/dataframe.rs
index da64ffa7d5..5b157d0ac1 100644
--- a/datafusion/src/dataframe.rs
+++ b/datafusion/src/dataframe.rs
@@ -147,6 +147,21 @@ pub trait DataFrame: Send + Sync {
/// ```
fn union(&self, dataframe: Arc<dyn DataFrame>) -> Result<Arc<dyn DataFrame>>;
+ /// Calculate the union distinct two [`DataFrame`]s. The two [`DataFrame`]s must have exactly the same schema
+ ///
+ /// ```
+ /// # use datafusion::prelude::*;
+ /// # use datafusion::error::Result;
+ /// # fn main() -> Result<()> {
+ /// let mut ctx = ExecutionContext::new();
+ /// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new())?;
+ /// let df = df.union(df.clone())?;
+ /// let df = df.distinct()?;
+ /// # Ok(())
+ /// # }
+ /// ```
+ fn distinct(&self) -> Result<Arc<dyn DataFrame>>;
+
/// Sort the DataFrame by the specified sorting expressions. Any expression can be turned into
/// a sort expression by calling its [sort](../logical_plan/enum.Expr.html#method.sort) method.
///
diff --git a/datafusion/src/execution/dataframe_impl.rs b/datafusion/src/execution/dataframe_impl.rs
index cd39dd6f3e..e57db17efc 100644
--- a/datafusion/src/execution/dataframe_impl.rs
+++ b/datafusion/src/execution/dataframe_impl.rs
@@ -214,6 +214,15 @@ impl DataFrame for DataFrameImpl {
.build()?;
Ok(Arc::new(DataFrameImpl::new(self.ctx_state.clone(), &plan)))
}
+
+ fn distinct(&self) -> Result<Arc<dyn DataFrame>> {
+ Ok(Arc::new(DataFrameImpl::new(
+ self.ctx_state.clone(),
+ &LogicalPlanBuilder::from(self.to_logical_plan())
+ .distinct()?
+ .build()?,
+ )))
+ }
}
#[cfg(test)]
diff --git a/datafusion/src/logical_plan/builder.rs b/datafusion/src/logical_plan/builder.rs
index d902d6f7f2..20908d7df8 100644
--- a/datafusion/src/logical_plan/builder.rs
+++ b/datafusion/src/logical_plan/builder.rs
@@ -291,6 +291,15 @@ impl LogicalPlanBuilder {
Ok(Self::from(union_with_alias(self.plan.clone(), plan, None)?))
}
+ /// Apply deduplication: Only distinct (different) values are returned)
+ pub fn distinct(&self) -> Result<Self> {
+ let projection_expr = expand_wildcard(self.plan.schema(), &self.plan)?;
+ let plan = LogicalPlanBuilder::from(self.plan.clone())
+ .aggregate(projection_expr, vec![])?
+ .build()?;
+ Self::from(plan).project(vec![Expr::Wildcard])
+ }
+
/// Apply a join with on constraint
pub fn join(
&self,
diff --git a/datafusion/src/sql/planner.rs b/datafusion/src/sql/planner.rs
index 140ae26437..78601bcd76 100644
--- a/datafusion/src/sql/planner.rs
+++ b/datafusion/src/sql/planner.rs
@@ -170,8 +170,14 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
let right_plan = self.set_expr_to_plan(right.as_ref(), None, ctes)?;
union_with_alias(left_plan, right_plan, alias)
}
+ (SetOperator::Union, false) => {
+ let left_plan = self.set_expr_to_plan(left.as_ref(), None, ctes)?;
+ let right_plan = self.set_expr_to_plan(right.as_ref(), None, ctes)?;
+ let union_plan = union_with_alias(left_plan, right_plan, alias)?;
+ LogicalPlanBuilder::from(union_plan).distinct()?.build()
+ }
_ => Err(DataFusionError::NotImplemented(format!(
- "Only UNION ALL is supported, found {}",
+ "Only UNION ALL and UNION [DISTINCT] are supported, found {}",
op
))),
},
@@ -3440,7 +3446,7 @@ mod tests {
let sql = "SELECT order_id from orders EXCEPT SELECT order_id FROM orders";
let err = logical_plan(sql).expect_err("query should have failed");
assert_eq!(
- "NotImplemented(\"Only UNION ALL is supported, found EXCEPT\")",
+ "NotImplemented(\"Only UNION ALL and UNION [DISTINCT] are supported, found EXCEPT\")",
format!("{:?}", err)
);
}
diff --git a/datafusion/tests/sql.rs b/datafusion/tests/sql.rs
index d3aa38d235..d8e580cd82 100644
--- a/datafusion/tests/sql.rs
+++ b/datafusion/tests/sql.rs
@@ -5066,3 +5066,13 @@ async fn avro_explain() {
];
assert_eq!(expected, actual);
}
+
+#[tokio::test]
+async fn union_distinct() -> Result<()> {
+ let mut ctx = ExecutionContext::new();
+ let sql = "SELECT 1 as x UNION SELECT 1 as x";
+ let actual = execute(&mut ctx, sql).await;
+ let expected = vec![vec!["1"]];
+ assert_eq!(expected, actual);
+ Ok(())
+}