sarutak commented on PR #60296:
URL: https://github.com/apache/spark/pull/60296#issuecomment-6843913194

   Thank you @raviston and @dongjoon-hyun for the reviews and for running the 
cases. I ran Q2, Q3 and items 1 to 4 on master (fceb1571d99), on the routing 
change alone (b161fd240fc) and on 00ab7708f45, with ANSI on, and also on 
00ab7708f45 with #60366 (b5d2f2345c3) applied; on e77a0ba7ae1 I ran Q2, Q3 and 
items 1 to 3. Item 3 needs `ConvertToLocalRelation`, which the test session 
excludes, so I ran it with `spark.sql.optimizer.excludedRules` empty.
   
   | Query | master | routing change alone | e77a0ba7ae1 | 00ab7708f45 | 
00ab7708f45 with #60366 |
   |---|---|---|---|---|---|
   | Q2 | `(1, 1), (2, 1)` | `DIVIDE_BY_ZERO` | `DIVIDE_BY_ZERO` | 
`DIVIDE_BY_ZERO` | `(1, 1), (2, 1)` |
   | Q3 | `(1, 0.1), (2, 0.05)` | `DIVIDE_BY_ZERO` | `DIVIDE_BY_ZERO` | 
`DIVIDE_BY_ZERO` | `(1, 0.1), (2, 0.05)` |
   | [1] | `(1, 7), (2, 8)` | `(1, 7), (2, 8)` | `INTERNAL_ERROR` | `(1, 7), 
(2, 8)` | `(1, 7), (2, 8)` |
   | [2] | `(1, 5.0), (2, 10.0)` | `(1, 5.0), (2, 10.0)` | `DIVIDE_BY_ZERO` | 
`(1, 5.0), (2, 10.0)` | `(1, 5.0), (2, 10.0)` |
   | [3] | `(1, 1), (2, 1)` | `(1, 1), (2, 1)` | `(1, NULL), (2, NULL)` | `(1, 
1), (2, 1)` | `(1, 1), (2, 1)` |
   | [4] | `(1, 50), (2, 100)` | `(1, 50), (2, 100)` | not run | `(1, 50), (2, 
100)` | `(1, 50), (2, 100)` |
   
   For [4] I used `inv = lambda x: 100 // x` as a `LongType` UDF, with `b(k, v) 
= (1, 100), (1, 200), (2, 300)`.
   
   So items 1 to 3 come from the collapse and the stripping of top `Project`s, 
as the review says. Q2 and Q3 do not come only from the collapse, though: the 
routing change alone fails the same way. It sends Q2 and Q3 to 
`DecorrelateInnerQuery`, which also evaluates the lower aggregate on zero rows 
through `RewriteCorrelatedScalarSubquery.evalAggregateOnZeroTups` and 
`tryEvalExpr`. The `raise_error` case in the first comment did not fail on any 
of them, since `RaiseError` is not foldable.
   
   @raviston, thank you for filing SPARK-61444 and opening #60325. This PR does 
not add its own fallback. On 00ab7708f45, Q2 and Q3 still fail with 
`DIVIDE_BY_ZERO` while the query is optimized. They come from 
`DecorrelateInnerQuery`, which folds the whole expression of the lower 
aggregate on its values over no rows, so I opened #60366 (SPARK-61482), which 
makes it evaluate only the aggregate functions on zero rows and compute the 
expression over them at runtime. With #60366 applied, Q2 and Q3 return the 
results in the last column, also without #60325. So this PR depends on #60366 
and should be merged after it. Once #60366 is merged, I will rebase this PR on 
it and regenerate the golden files, which show the error for these queries 
until then. #60325 still fixes the cases that `constructLeftJoins` folds, such 
as `10 / COALESCE(MAX(b.v), 0)` directly in the subquery, which this PR does 
not change.
   
   Following item 5, I reverted the collapse and the stripping of top 
`Project`s in 5677e41df85. The description now numbers the changes again: 
change 1 is the routing change and change 2 is the new check for item 7 below, 
not the collapse. The collapse and the stripping will go to a separate PR for 
`master`, which will address items 1, 2, 3, 4, 6 and 8 there. 00ab7708f45 then:
   
   - keeps the old plan for item 7, see the reply there.
   - adds Q2, Q3, a failing `CAST`, [1], [2] and a query whose outer row 
without matching rows another condition removes (see below) to the golden file, 
and [3] to `SubquerySuite` with `ConvertToLocalRelation` enabled, next to 
`COUNT(*)` over a lower `MAX`, `SUM`, `COUNT` and `COLLECT_LIST` over a lower 
`MAX`, and an uncorrelated lower `COUNT`. The queries for [1] and [2] and the 
first `SubquerySuite` query fail on e77a0ba7ae1.
   - keeps the previous routing where the domain joins of 
`DecorrelateInnerQuery`, which compute the outer query again, are known not to 
work, so that such queries do not get worse than on master. For a streaming 
query, `COUNT(*)` over `MAX(b.v)` over a `MemoryStream` fails with the domain 
joins (`UNSUPPORTED_STREAMING_OPERATOR_WITHOUT_WATERMARK` in append mode, a 
stream-stream left outer join error in update mode), as `SUM(c)` over 
`COUNT(*)` does on master, while master runs it with the count bug. With a 
random correlated key, the domain joins return `NULL` for about 40% of 1000 
rows, where master returns 1 for all; a non-deterministic CTE referenced twice 
returns `NULL` or `0` instead of 1 for about 60% of 2000 rows with the domain 
joins and `spark.sql.exchange.reuse=false`. For outer references that are not 
orderable, such as a map or a variant, the rewritten plan fails validation. 
00ab7708f45 returns the previous results in these cases, and `SubquerySuite` 
has them. The check is
  conservative, e.g. an unused non-deterministic column also keeps the previous 
result. The AI review marked its spec item that a streaming query gets the same 
rewrite as a batch query as no concern, but without this check such a query 
fails in append and update mode (measured with the routing change without this 
check, on top of #60366).
   - narrows the routing change. b161fd240fc replaced the old check, a lower 
aggregate with a function with a default result such as `COUNT`, with the 
correlation check, so it sent a subquery over an uncorrelated lower `COUNT` 
from `DecorrelateInnerQuery` to `constructLeftJoins`. There, e.g. `COUNT(*) 
FILTER (WHERE (SELECT MAX(c2) FROM t3 WHERE t3.c2 = c) IS NOT NULL)` over 
`SELECT COUNT(*) c FROM t2` with a correlated predicate above it fails with 
`INTERNAL_ERROR` on b161fd240fc and e77a0ba7ae1, and returns the correct result 
on master. 00ab7708f45 keeps the old check and adds the correlation check next 
to it, so the routing change only moves subqueries to `DecorrelateInnerQuery`. 
The golden file has this query and a similar `FILTER`, `SUM(m) FILTER (WHERE 
(SELECT MAX(c2) FROM t3 WHERE t3.c2 = m) IS NOT NULL)`, over a correlated lower 
`MAX` and `COUNT`.
   
   With the routing change, the lower aggregate's row for an outer row without 
matching rows is no longer lost in the subqueries that now go to 
`DecorrelateInnerQuery`, which is what fixes the results, so expressions over 
it, in the lower aggregate, in a node between the two such as a `Project` or a 
`Filter`, or in an argument or `FILTER` of the top aggregate, are now evaluated 
for such rows. The description and the migration guide now state what this 
means:
   
   - such an expression can fail there, e.g. `SUM(d)` over `10 / 
COALESCE(MAX(b.v), 0) AS d` and `SUM(10 / COALESCE(s, 0))` over `MAX(b.v) AS 
s`, with #60366 applied, fail at runtime for an outer row without matching 
rows, where master returned `NULL`. It can also fail when another condition of 
the query removes that row: `SELECT a.k FROM a WHERE a.k = 1 AND (SELECT SUM(d) 
FROM (SELECT 10 / COALESCE(MAX(b.v), 0) d FROM b WHERE b.k = a.k)) IS NOT NULL` 
returns `1` on master and fails with `DIVIDE_BY_ZERO` at runtime on 00ab7708f45 
with #60366, because the expression is evaluated for `a.k = 2`, which has no 
matching rows. Master already does the same for a lower `COUNT`, which goes to 
`DecorrelateInnerQuery` there: with `SUM(10 / c)` over `COUNT(*) AS c`, the 
same query fails with `DIVIDE_BY_ZERO` at runtime on master.
   - this includes a Python UDF over an output of the lower aggregate, as in 
item 4. On 00ab7708f45 alone, `SUM(d)` over `inv(MAX(b.v)) AS d` fails with 
`TypeError` even when every outer row has matching rows, since 
`DecorrelateInnerQuery` keeps `inv(NULL)` in a conditional branch, and Python 
UDFs in a conditional branch run for every row. Master returns `(1, 0), (2, 
0)`. With #60366 applied, the UDF runs on the value over no rows only for an 
outer row without matching rows: the query returns `(1, 0), (2, 0)` again, and 
with `a(k) = (1), (5)` it fails with `TypeError` at runtime for `k = 5`, where 
master returned `NULL`. `SUM(inv(s))` over `MAX(b.v) AS s` fails with 
`TypeError` for `k = 5` on 00ab7708f45, with or without #60366, where master 
returned `NULL`; when every outer row matches, it returns `(1, 0), (2, 0)` as 
on master. #60366 also fixes the same failure that master already has with a 
lower `COUNT`, e.g. `SUM(d)` over `inv(COUNT(*)) AS d` and the same in a 
`LATERAL` subquery
 . `(SELECT inv(COUNT(*)) FROM b WHERE b.k = a.k)` with a single aggregate 
still fails with `ZeroDivisionError`, as on master, since `constructLeftJoins` 
handles it.
   
   The AI review's P1 is the same issue as item 5 and is resolved by the 
revert, except the shapes it lists with an expression in the lower aggregate 
that fails on zero rows, such as `COUNT(*)` over `10 / COALESCE(MAX(b2), 0)`, 
which fail on 00ab7708f45 until #60366 as Q2 above; for its P3, the comment at 
the `handleCountBugInDecorrelate` check is rewritten and now names `SUM(s)` and 
`COLLECT_LIST(s)` over `MAX` as shapes that stay in `constructLeftJoins`, and 
the Scaladoc of `sameResultOverEmptyLowerAggregate` says that the check is 
conservative and leaves other shapes with the same property, such as 
`STDDEV_SAMP(s)`, to `DecorrelateInnerQuery`.
   


-- 
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]

Reply via email to