adriangb opened a new issue, #27927: URL: https://github.com/apache/datafusion/issues/27927
### Describe the bug On `main`, the right branch of a set operation can no longer refer to its own `SELECT` aliases in `GROUP BY` or `HAVING`. This started with https://github.com/apache/datafusion/pull/27473. In some cases the query does not fail, but silently returns wrong results (see "Additional context"). ### To Reproduce ```console $ datafusion-cli -q -c "CREATE TABLE t(b INT) AS VALUES (1), (2)" -c "SELECT 1 AS c1, 2 AS c2 UNION ALL SELECT b AS m, count(*) FROM t GROUP BY m" Error: Schema error: No field named m. Valid fields are c1, c2, t.b. ``` The second branch alone works: ```console $ datafusion-cli -q -c "CREATE TABLE t(b INT) AS VALUES (1), (2)" -c "SELECT b AS m, count(*) FROM t GROUP BY m" +---+----------+ | m | count(*) | +---+----------+ | 1 | 1 | | 2 | 1 | +---+----------+ ``` Workaround: use the left branch's name, `SELECT b AS c1, count(*) FROM t GROUP BY c1`. ### Expected behavior The query works, as it does on the parent commit of that PR: ``` +----+----+ | c1 | c2 | +----+----+ | 1 | 2 | | 2 | 1 | | 1 | 1 | +----+----+ ``` ### Additional context I built `datafusion-cli` at three commits and ran the query above on each: | Commit | Result | | --- | --- | | 1841fb6ec45e8861a9029a904e92e78d60ec0cbe (parent of the PR's merge commit) | works | | e2dc8b048b542e7abdc296254aa54e26eb89d76d (merge commit) | `No field named m` | | 4b9d354429bde6b778f20889ddf70d29f0096ae5 (`main` today) | `No field named m` | The merge commit is not in 56.0.0 or in `branch-56`, so no release has this yet. **Wrong results.** If the left branch has a column with the same name at a different position, `HAVING` silently uses the wrong expression: ```sql SELECT 1 AS p, 2 AS q UNION ALL SELECT count(*) AS q, sum(b) AS p FROM t HAVING p > 2 ``` With the same table, the parent commit returns `(1, 2)` and `(2, 3)`. The merge commit and `main` return only `(1, 2)`. Here `p` in the `HAVING` seems to resolve to `count(*)` (2) instead of `sum(b)` (3). Other cases, all with the same before and after: - `UNION`, `INTERSECT` and `EXCEPT` fail in the same way as `UNION ALL`. - `HAVING`, `QUALIFY` and `DISTINCT ON` on a right-branch alias fail too. - A single-column set operation is not affected, because [the left schema is only passed down for more than one column](https://github.com/apache/datafusion/blob/4b9d354429bde6b778f20889ddf70d29f0096ae5/datafusion/sql/src/set_expr.rs#L44-L45). **Cause.** `project_with_validation` renames the aliases of the right branch to the left branch's names (`Expr::Alias(alias) => alias.name.clone_from(field.name())`). `select_to_plan` calls it through `self.project(...)` first, and then builds `alias_map` and the schema for `GROUP BY` / `HAVING` from that projection. So the original alias is already gone when these clauses are resolved. The PR fixed duplicate names in the right branch (https://github.com/apache/datafusion/issues/10244), e.g. `SELECT 0 AS a, 0 AS b UNION ALL SELECT 1 AS x, 2 AS x`, and a fix here should keep that working. One idea, which I have not tested: resolve the clauses against the original aliases, and do the rename only on the final projection of the branch. @AdamGS, could you take a look when you have time? Thanks! ### Willingness to contribute I am happy to help test a fix. -- 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]
