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
10 changes: 10 additions & 0 deletions datafusion/sql/src/unparser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct QueryBuilder {
fetch: Option<ast::Fetch>,
locks: Vec<ast::LockClause>,
for_clause: Option<ast::ForClause>,
// If true, we need to unparse LogicalPlan::Union as a SQL `UNION` rather than a `UNION ALL`.
distinct_union: bool,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -75,6 +77,13 @@ impl QueryBuilder {
self.for_clause = value;
self
}
pub fn distinct_union(&mut self) -> &mut Self {
self.distinct_union = true;
self
}
pub fn is_distinct_union(&self) -> bool {
self.distinct_union
}
pub fn build(&self) -> Result<ast::Query, BuilderError> {
let order_by = self
.order_by_kind
Expand Down Expand Up @@ -112,6 +121,7 @@ impl QueryBuilder {
fetch: Default::default(),
locks: Default::default(),
for_clause: Default::default(),
distinct_union: false,
}
}
}
Expand Down
28 changes: 27 additions & 1 deletion datafusion/sql/src/unparser/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,23 @@ impl Unparser<'_> {
false,
);
}

// If this distinct is the parent of a Union and we're in a query context,
// then we need to unparse as a `UNION` rather than a `UNION ALL`.
if let Distinct::All(input) = distinct {
if matches!(input.as_ref(), LogicalPlan::Union(_)) {
if let Some(query_mut) = query.as_mut() {
query_mut.distinct_union();
return self.select_to_sql_recursively(
input.as_ref(),
query,
select,
relation,
);
}
}
}

let (select_distinct, input) = match distinct {
Distinct::All(input) => (ast::Distinct::Distinct, input.as_ref()),
Distinct::On(on) => {
Expand Down Expand Up @@ -829,14 +846,23 @@ impl Unparser<'_> {
return internal_err!("UNION operator requires at least 2 inputs");
}

let set_quantifier =
if query.as_ref().is_some_and(|q| q.is_distinct_union()) {
// Setting the SetQuantifier to None will unparse as a `UNION`
// rather than a `UNION ALL`.
ast::SetQuantifier::None
} else {
ast::SetQuantifier::All
};

// Build the union expression tree bottom-up by reversing the order
// note that we are also swapping left and right inputs because of the rev
let union_expr = input_exprs
.into_iter()
.rev()
.reduce(|a, b| SetExpr::SetOperation {
op: ast::SetOperator::Union,
set_quantifier: ast::SetQuantifier::All,
set_quantifier,
left: Box::new(b),
right: Box::new(a),
})
Expand Down
7 changes: 7 additions & 0 deletions datafusion/sql/tests/cases/plan_to_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ fn roundtrip_statement() -> Result<()> {
UNION ALL
SELECT j3_string AS col1, j3_id AS id FROM j3
) AS subquery GROUP BY col1, id ORDER BY col1 ASC, id ASC"#,
r#"SELECT col1, id FROM (
SELECT j1_string AS col1, j1_id AS id FROM j1
UNION
SELECT j2_string AS col1, j2_id AS id FROM j2
UNION
SELECT j3_string AS col1, j3_id AS id FROM j3
) AS subquery ORDER BY col1 ASC, id ASC"#,
"SELECT id, count(*) over (PARTITION BY first_name ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
last_name, sum(id) over (PARTITION BY first_name ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING),
first_name from person",
Expand Down