Skip to content
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
2 changes: 1 addition & 1 deletion datafusion/expr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub use function::{
};
pub use literal::{lit, lit_timestamp_nano, Literal, TimestampLiteral};
pub use logical_plan::{
builder::{build_join_schema, union_with_alias, UNNAMED_TABLE},
builder::{build_join_schema, union, UNNAMED_TABLE},
Aggregate, CreateCatalog, CreateCatalogSchema, CreateExternalTable,
CreateMemoryTable, CreateView, CrossJoin, Distinct, DropTable, DropView,
EmptyRelation, Explain, Extension, Filter, Join, JoinConstraint, JoinType, Limit,
Expand Down
31 changes: 5 additions & 26 deletions datafusion/expr/src/logical_plan/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,19 +416,7 @@ impl LogicalPlanBuilder {

/// Apply a union, preserving duplicate rows
pub fn union(&self, plan: LogicalPlan) -> Result<Self> {
Ok(Self::from(union_with_alias(self.plan.clone(), plan, None)?))
}

pub fn union_with_alias(
&self,
plan: LogicalPlan,
alias: Option<String>,
) -> Result<Self> {
Ok(Self::from(union_with_alias(
self.plan.clone(),
plan,
alias,
)?))
Ok(Self::from(union(self.plan.clone(), plan)?))
}

/// Apply a union, removing duplicate rows
Expand All @@ -445,7 +433,7 @@ impl LogicalPlanBuilder {
};

Ok(Self::from(LogicalPlan::Distinct(Distinct {
input: Arc::new(union_with_alias(left_plan, right_plan, None)?),
input: Arc::new(union(left_plan, right_plan)?),
})))
}

Expand Down Expand Up @@ -892,11 +880,7 @@ pub fn project_with_column_index_alias(
}

/// Union two logical plans with an optional alias.
pub fn union_with_alias(
left_plan: LogicalPlan,
right_plan: LogicalPlan,
alias: Option<String>,
) -> Result<LogicalPlan> {
pub fn union(left_plan: LogicalPlan, right_plan: LogicalPlan) -> Result<LogicalPlan> {
let left_col_num = left_plan.schema().fields().len();

// the 2 queries should have same number of columns
Expand Down Expand Up @@ -927,7 +911,7 @@ pub fn union_with_alias(
})?;

Ok(DFField::new(
alias.as_deref(),
None,
left_field.name(),
data_type,
nullable,
Expand Down Expand Up @@ -962,14 +946,9 @@ pub fn union_with_alias(
return Err(DataFusionError::Plan("Empty UNION".to_string()));
}

let union_schema = Arc::new(match alias {
Some(ref alias) => union_schema.replace_qualifier(alias.as_str()),
None => union_schema.strip_qualifiers(),
});

Ok(LogicalPlan::Union(Union {
inputs,
schema: union_schema,
schema: Arc::new(union_schema),
}))
}

Expand Down
26 changes: 2 additions & 24 deletions datafusion/optimizer/src/filter_push_down.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,9 +833,8 @@ mod tests {
use async_trait::async_trait;
use datafusion_common::DFSchema;
use datafusion_expr::{
and, col, in_list, in_subquery, lit,
logical_plan::{builder::union_with_alias, JoinType},
sum, Expr, LogicalPlanBuilder, Operator, TableSource, TableType,
and, col, in_list, in_subquery, lit, logical_plan::JoinType, sum, Expr,
LogicalPlanBuilder, Operator, TableSource, TableType,
};
use std::sync::Arc;

Expand Down Expand Up @@ -1183,27 +1182,6 @@ mod tests {
Ok(())
}

#[test]
fn union_all_with_alias() -> Result<()> {
let table_scan = test_table_scan()?;
let union =
union_with_alias(table_scan.clone(), table_scan, Some("t".to_string()))?;

let plan = LogicalPlanBuilder::from(union)
.filter(col("t.a").eq(lit(1i64)))?
.build()?;

// filter appears below Union without relation qualifier
let expected = "\
Union\
\n Filter: a = Int64(1)\
\n TableScan: test\
\n Filter: a = Int64(1)\
\n TableScan: test";
assert_optimized_plan_eq(&plan, expected);
Ok(())
}

#[test]
fn union_all_on_projection() -> Result<()> {
let table_scan = test_table_scan()?;
Expand Down
9 changes: 3 additions & 6 deletions datafusion/optimizer/src/propagate_empty_relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,13 @@ impl OptimizerRule for PropagateEmptyRelation {
Ok(LogicalPlan::Projection(Projection::new_from_schema(
Arc::new(child),
optimized_children_plan.schema().clone(),
union.alias.clone(),
None,
)))
}
} else {
Ok(LogicalPlan::Union(Union {
inputs: new_inputs,
schema: union.schema.clone(),
alias: union.alias.clone(),
}))
}
}
Expand Down Expand Up @@ -383,11 +382,9 @@ mod tests {
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.build()?;

let plan = LogicalPlanBuilder::from(left)
.union_with_alias(right, Some("union".to_string()))?
.build()?;
let plan = LogicalPlanBuilder::from(left).union(right)?.build()?;

let expected = "Projection: union.a, union.b, union.c, alias=union\
let expected = "Projection: a, b, c\
\n TableScan: test";
assert_together_optimized_plan_eq(&plan, expected);
Ok(())
Expand Down