Commit 9ac9cd5
[SPARK-40618][SQL] Fix bug in MergeScalarSubqueries rule with nested subqueries
### What changes were proposed in this pull request?
There is a bug in the `MergeScalarSubqueries` rule for queries with subquery expressions nested inside each other, wherein the rule attempts to merge the nested subquery with its enclosing parent subquery. The result is not a valid plan and raises an exception in the optimizer. Here is a minimal reproducing case:
```
sql("create table test(col int) using csv")
checkAnswer(sql("select(select sum((select sum(col) from test)) from test)"), Row(null))
```
To fix, we disable the optimization for subqueries with nested subqueries inside them for now.
### Why are the changes needed?
This fixes a bug.
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
Updated existing unit tests and added the reproducing case as a new test case.
Closes apache#38052 from dtenedor/fix-merge-subquery-bug.
Authored-by: Daniel Tenedorio <daniel.tenedorio@databricks.com>
Signed-off-by: Gengliang Wang <gengliang@apache.org>1 parent 7d010b1 commit 9ac9cd5
2 files changed
Lines changed: 21 additions & 5 deletions
File tree
- sql
- catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer
- core/src/test/scala/org/apache/spark/sql
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
213 | 219 | | |
214 | 220 | | |
215 | 221 | | |
| |||
Lines changed: 15 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2157 | 2157 | | |
2158 | 2158 | | |
2159 | 2159 | | |
2160 | | - | |
| 2160 | + | |
2161 | 2161 | | |
2162 | 2162 | | |
2163 | 2163 | | |
| |||
2189 | 2189 | | |
2190 | 2190 | | |
2191 | 2191 | | |
2192 | | - | |
2193 | | - | |
| 2192 | + | |
| 2193 | + | |
2194 | 2194 | | |
2195 | 2195 | | |
2196 | | - | |
2197 | | - | |
| 2196 | + | |
| 2197 | + | |
2198 | 2198 | | |
2199 | 2199 | | |
2200 | 2200 | | |
| |||
2327 | 2327 | | |
2328 | 2328 | | |
2329 | 2329 | | |
| 2330 | + | |
| 2331 | + | |
| 2332 | + | |
| 2333 | + | |
| 2334 | + | |
| 2335 | + | |
| 2336 | + | |
| 2337 | + | |
| 2338 | + | |
| 2339 | + | |
2330 | 2340 | | |
0 commit comments