Yibo-Dong opened a new issue, #27768:
URL: https://github.com/apache/datafusion/issues/27768
### Describe the bug
When a correlated `EXISTS` or `IN` subquery is the left input of `EXCEPT` or
`INTERSECT`, and its correlated column (`r.k` below) is not in the subquery's
select list, the query fails with `Schema error: No field named
__correlated_sq_1.k`. The queries are valid. The same subquery runs when it
also selects the correlated column, and equivalent rewrites without the set
operation return results.
### To Reproduce
Tested with `datafusion-cli` built from main at
`4c7a78b0ffb157fd104df327910caaef26cf361b` (2026-11-26), default configuration.
```sql
CREATE TABLE l(k INT) AS VALUES (1), (2), (3);
CREATE TABLE r(k INT, v INT) AS VALUES (1, 10), (1, 11), (2, 20);
-- Correlated subquery as the left input of EXCEPT / INTERSECT
SELECT k FROM l WHERE EXISTS (SELECT r.v FROM r WHERE r.k = l.k EXCEPT
SELECT 10) ORDER BY k;
SELECT k FROM l WHERE EXISTS (SELECT r.v FROM r WHERE r.k = l.k INTERSECT
SELECT 10) ORDER BY k;
SELECT k FROM l WHERE k IN (SELECT r.k FROM r WHERE r.v > l.k EXCEPT SELECT
1) ORDER BY k;
-- Equivalent formulations (work)
SELECT k FROM l WHERE EXISTS (SELECT 1 FROM r WHERE r.k = l.k AND r.v <> 10)
ORDER BY k;
SELECT k FROM l WHERE EXISTS (SELECT 1 FROM r WHERE r.k = l.k AND r.v = 10)
ORDER BY k;
SELECT k FROM l WHERE k IN (SELECT r.k FROM r WHERE r.v > l.k AND r.k <> 1)
ORDER BY k;
-- Same EXCEPT query, but the subquery also projects the correlated column
r.k (works)
SELECT k FROM l WHERE EXISTS (SELECT r.v, r.k FROM r WHERE r.k = l.k EXCEPT
SELECT 10, 1) ORDER BY k;
```
Output:
```
Schema error: No field named __correlated_sq_1.k. Did you mean 'l.k'?
Valid fields are l.k.
Schema error: No field named __correlated_sq_1.k. Did you mean 'l.k'?
Valid fields are l.k.
Schema error: No field named __correlated_sq_1.v.
Valid fields are l.k.
+---+
| k |
+---+
| 1 |
| 2 |
+---+
+---+
| k |
+---+
| 1 |
+---+
+---+
| k |
+---+
| 2 |
+---+
+---+
| k |
+---+
| 1 |
| 2 |
+---+
```
### Expected behavior
The three set-operation queries should run and return the same rows as their
equivalent formulations:
- `EXISTS (... EXCEPT SELECT 10)`: for `k = 1` the subquery is `{10, 11}
EXCEPT {10} = {11}` (non-empty), for `k = 2` it is `{20}`, and for `k = 3` it
is empty. Expected rows: `1`, `2`.
- `EXISTS (... INTERSECT SELECT 10)`: non-empty only for `k = 1`. Expected
row: `1`.
- `k IN (SELECT r.k FROM r WHERE r.v > l.k EXCEPT SELECT 1)`: for every `k`
in `l` the subquery is `{1, 2} EXCEPT {1} = {2}`. Expected row: `2`.
### Additional context
_No response_
### Willingness to contribute
I cannot contribute a fix for this bug at this time
--
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]