Skip to content

Commit

Permalink
fix: union_distinct shouldn't remove child distinct (#7346)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwener authored Aug 23, 2023
1 parent 77e8f43 commit 304cb02
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions datafusion/expr/src/logical_plan/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,15 +593,8 @@ impl LogicalPlanBuilder {

/// Apply a union, removing duplicate rows
pub fn union_distinct(self, plan: LogicalPlan) -> Result<Self> {
// unwrap top-level Distincts, to avoid duplication
let left_plan: LogicalPlan = match self.plan {
LogicalPlan::Distinct(Distinct { input }) => (*input).clone(),
_ => self.plan,
};
let right_plan: LogicalPlan = match plan {
LogicalPlan::Distinct(Distinct { input }) => (*input).clone(),
_ => plan,
};
let left_plan: LogicalPlan = self.plan;
let right_plan: LogicalPlan = plan;

Ok(Self::from(LogicalPlan::Distinct(Distinct {
input: Arc::new(union(left_plan, right_plan)?),
Expand Down Expand Up @@ -1629,13 +1622,16 @@ mod tests {
.union_distinct(plan.build()?)?
.build()?;

// output has only one union
let expected = "\
Distinct:\
\n Union\
\n TableScan: employee_csv projection=[state, salary]\
\n TableScan: employee_csv projection=[state, salary]\
\n TableScan: employee_csv projection=[state, salary]\
\n Distinct:\
\n Union\
\n Distinct:\
\n Union\
\n TableScan: employee_csv projection=[state, salary]\
\n TableScan: employee_csv projection=[state, salary]\
\n TableScan: employee_csv projection=[state, salary]\
\n TableScan: employee_csv projection=[state, salary]";

assert_eq!(expected, format!("{plan:?}"));
Expand Down

0 comments on commit 304cb02

Please sign in to comment.