AveryQi115 commented on PR #58046:
URL: https://github.com/apache/spark/pull/58046#issuecomment-6809709512
I hit the same bug from a different direction and verified this PR covers
it, so +1 to landing it.
A few suggestions:
1. **Another repro worth adding as a test.** No alias collision is needed;
it is enough that the
right filter reads a base column that the output does not carry, while
the output has an alias of
the same name:
```sql
SELECT a % 10 AS b FROM t WHERE b >= 0
EXCEPT
SELECT a % 10 AS b FROM t WHERE b > 3;
-- t = (11, 0), (21, 5), (32, 5): expected empty, master returns 1, 2
```
Today `b > 3` binds to the alias `b = a % 10`; with this PR `t.b` is not
traceable to the left
output, so the anti join is kept. A plan-level version for
`ReplaceOperatorSuite`:
`table.where($"b" >= 0).select(($"a" % 10).as("b"))` EXCEPT
`table.where($"b" > 3).select(($"a" % 10).as("b"))` should stay an
`Aggregate(LeftAnti Join)`.
2. **The correlated shape.** With
`spark.sql.optimizer.decorrelateSetOps.enabled=true`,
decorrelation appends the outer attribute to both branches' output, and
the name-based binding
captured the inner attribute of the same name, so `X EXCEPT X` returned
`X`:
```sql
SELECT o.order_id, (SELECT count(*) FROM (
(SELECT p.price % 10 FROM products p WHERE p.price > o.price)
EXCEPT
(SELECT p.price % 10 FROM products p WHERE p.price > o.price + 5))) AS n
FROM orders o;
-- orders = (1, 10), (2, 20), (3, 35); products = 15, 25, 40, 41
-- expected (1, 0), (2, 1), (3, 1); master returns (1, 3), (2, 3), (3, 2)
```
I checked that the exprId-based mapping here also keeps the anti join for
this shape (the inner
`p.price` is not in the left output). It would be good to pin it in
`DataFrameSetOperationsSuite` since nothing else exercises
`ReplaceExceptWithFilter` after
decorrelation.
3. **Positive case for the new binding.** Something like
`SELECT x AS y, y AS x FROM d EXCEPT SELECT x AS y, y AS x FROM d WHERE x
> 1` (swapped aliases)
shows the rewrite now binds `d.x` to the output column named `y`, i.e.
the filter is still
rewritten and the result is right (`d = (1,2), (2,1), (3,3)` -> `(1, 2)`).
4. Minor: the PR description could use a before/after optimized plan for the
JIRA repro, and the
JIRA's affected versions should include 4.1 / 4.2 / 4.3 (the rule body is
unchanged since
SPARK-23274).
Since this is a wrong-results fix that has been approved for a while, could
a committer take a look?
--
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]