Skip to content

Commit a5ad4c5

Browse files
committed
fix: Allow referencing CTEs in UNION
1 parent 97c7336 commit a5ad4c5

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

datafusion/core/src/sql/planner.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
379379
}
380380
}
381381
}
382-
SetExpr::Query(q) => self.query_to_plan(*q),
382+
SetExpr::Query(q) => self.query_to_plan_with_alias(*q, None, ctes),
383383
_ => Err(DataFusionError::NotImplemented(format!(
384384
"Query {} not implemented yet",
385385
set_expr
@@ -4861,4 +4861,23 @@ mod tests {
48614861
let expected = "Projection: CAST(Int64(158412331400600000) AS Timestamp(Nanosecond, None))\n EmptyRelation";
48624862
quick_test(sql, expected);
48634863
}
4864+
4865+
#[test]
4866+
fn test_union_ctes() {
4867+
let sql = "\
4868+
\n WITH w AS (SELECT 1 l)\
4869+
\n SELECT w.l\
4870+
\n FROM w\
4871+
\n UNION ALL (SELECT w.l FROM w)\
4872+
\n;";
4873+
let expected = "\
4874+
Union\
4875+
\n Projection: #w.l\
4876+
\n Projection: Int64(1) AS l, alias=w\
4877+
\n EmptyRelation\
4878+
\n Projection: #w.l\
4879+
\n Projection: Int64(1) AS l, alias=w\
4880+
\n EmptyRelation";
4881+
quick_test(sql, expected);
4882+
}
48644883
}

0 commit comments

Comments
 (0)