nathanb9 commented on code in PR #22998:
URL: https://github.com/apache/datafusion/pull/22998#discussion_r3488613735
##########
datafusion/expr/src/expr_rewriter/mod.rs:
##########
@@ -80,6 +92,48 @@ pub fn normalize_col(expr: Expr, plan: &LogicalPlan) ->
Result<Expr> {
.data()
}
+/// Resolve a normalized column to itself, or -- when it is the merged key of a
+/// `RIGHT` / `FULL` `USING` / `NATURAL` join referenced *unqualified* -- to
the
+/// value of the side that is never NULL-padded.
+///
+/// `merged_keys` carries the `(left, right, join_type)` triples of the plan's
+/// `RIGHT` / `FULL` USING / NATURAL joins. The merged key resolves to the
right
+/// key for `RIGHT`, and to `COALESCE(left, right)` for `FULL`. The COALESCE is
+/// built as `CASE WHEN left IS NOT NULL THEN left ELSE right END` (the form
+/// `coalesce` is simplified to, buildable here without depending on the
+/// functions crate), aliased to the key name so the output column keeps its
+/// name. `LEFT` / `INNER` never reach here -- their merged key is the left
+/// column the normalization already produced.
+pub fn merged_using_key_or_column(
+ col: Column,
+ was_unqualified: bool,
+ merged_keys: &[(Column, Column, JoinType)],
+) -> Result<Expr> {
+ if was_unqualified
+ && let Some((l, r, join_type)) =
+ merged_keys.iter().find(|(l, r, _)| l == &col || r == &col)
+ {
+ match join_type {
+ // The left key is NULL-padded on right-only rows; the right key is
+ // always present, so the merged key is just the right column.
+ JoinType::Right => return Ok(Expr::Column(r.clone())),
Review Comment:
This makes `SELECT k, b.k FROM a RIGHT JOIN b USING(k)` fail because both
select items become `b.k`. Could we alias the merged key here, e.g.
`Expr::Column(r.clone()).alias(col.name.clone())`, so `k` stays distinct from
an explicit `b.k`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]