jayzhan211 commented on code in PR #27347:
URL: https://github.com/apache/datafusion/pull/27347#discussion_r4571124191
##########
datafusion/physical-expr/src/equivalence/properties/union.rs:
##########
@@ -61,7 +61,21 @@ fn calculate_union_binary(
orderings.add_satisfied_orderings(rhs, &lhs)?;
let orderings = orderings.build();
+ // An equality holds at the output only if it holds in both inputs. Keep
+ // it in the result so the next fold step can still use it.
+ let mut eq_group = EquivalenceGroup::default();
+ for class in lhs.eq_group().iter() {
+ for (i, left) in class.iter().enumerate() {
+ for right in class.iter().skip(i + 1) {
+ if rhs.eq_group().exprs_equal(left, right) {
Review Comment:
@galaxytemple thanks for the update, and sorry: the diff I suggested carried
over a bug. `exprs_equal` treats two expressions as equal when they have the
same node type and equal children, without comparing the operator. So in an arm
with `y = z`, it says `y % 5` equals `z + 5`, and the union output then sorts
`ORDER BY y % 5` by `z + 5`, which gives wrong order. Main gets this query
right. Checking that both expressions sit in the same class of `rhs` fixes it,
and the full sqllogictest suite passes with it:
```suggestion
let rhs_class = rhs.eq_group().get_equivalence_class(left);
if rhs_class.is_some_and(|cls| cls.contains(right)) {
```
<details>
<summary>Regression test for union.slt (fails on the current head, passes on
main and with the fix); goes before DROP TABLE t_26625</summary>
```sql
# `y % 5 = z + 5` holds in the first arm only: the second arm has y = z,
# which does not make `y % 5` equal to `z + 5`.
statement ok
CREATE TABLE t_26625_l(a INT, b INT, c INT) AS VALUES
(0, 5, -5), (1, 6, -4), (2, 7, -3), (3, 13, -2), (4, 9, -1);
statement ok
CREATE TABLE t_26625_r(d INT) AS VALUES (1), (2), (3), (4), (5), (6);
query III
SELECT y % 5 AS k, y, z FROM (
(SELECT b AS y, c AS z FROM t_26625_l
WHERE a = c + 5 AND a = b % 5 ORDER BY z LIMIT 100)
UNION ALL
(SELECT d AS y, d AS z FROM t_26625_r ORDER BY z LIMIT 100)
) ORDER BY k, z;
----
0 5 -5
0 5 5
1 6 -4
1 1 1
1 6 6
2 7 -3
2 2 2
3 13 -2
3 3 3
4 9 -1
4 4 4
statement ok
DROP TABLE t_26625_l;
statement ok
DROP TABLE t_26625_r;
```
</details>
--
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]