You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "fornwall (via GitHub)" <gi...@apache.org> on 2026/10/19 20:30:02 UTC

[I] Correlated scalar subquery ignores `HAVING` that is true for empty input [datafusion]

fornwall opened a new issue, #26505:
URL: https://github.com/apache/datafusion/issues/26505

   ### Describe the bug
   
   When a correlated scalar subquery aggregates without `GROUP BY` (for example with `sum` or `max`), its `HAVING` clause is ignored if the predicate is true for an empty input, such as `HAVING sum(y.b) IS NULL` or `HAVING count(*) = 0`.
   
   
   When the `HAVING` clause should filter out the aggregate row, the subquery returns the aggregate value instead of `NULL`.
   
   ### To Reproduce
   
   ```shell
   datafusion-cli -c "
   SELECT x.a,
          (SELECT sum(y.b) FROM (VALUES (1, 10)) AS y(a, b) WHERE y.a = x.a HAVING sum(y.b) IS NULL) AS v
   FROM (VALUES (1), (2)) AS x(a)
   ORDER BY x.a"
   ```
   
   This currently gives:
   
   ```
   +---+------+
   | a | v    |
   +---+------+
   | 1 | 10   |
   | 2 | NULL |
   +---+------+
   ```
   
   ### Expected behavior
   
   `v` should be `NULL` for both rows, which is what PostgreSQL 17 and SQLite return:
   
   - `a = 1`: `sum(y.b)` is 10, so `sum(y.b) IS NULL` is false and the subquery returns no row, giving `NULL`.
   - `a = 2`: there are no matching rows, so `sum(y.b)` is `NULL`. The `HAVING` passes and the subquery returns `NULL`.
   
   `EXPLAIN` shows no filter for the `HAVING` anywhere in the plan.
   
   ### Additional context
   
   - Also happens with `max`/`min`, and with `HAVING count(*) = 0` while the subquery selects `sum(y.b)`.
   - Correct results when the subquery is uncorrelated (`WHERE y.a = 1`), when the `HAVING` is false for empty input (for example `HAVING sum(y.b) > 100`), and when the subquery selects `count(*)` instead (`HAVING count(*) = 0` gives `NULL, 0`).
   - Related but different: #26503 (`count(*)` with an outer column referenced in `HAVING`).
   
   Found with DataFusion CLI v55.1.0, `main` at 7e102d74c.
   
   ### Willingness to contribute
   
   None


-- 
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: github-unsubscribe@datafusion.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Correlated scalar subquery ignores `HAVING` that is true for empty input [datafusion]

Posted by "jayzhan211 (via GitHub) via github" <gi...@datafusion.apache.org>.
jayzhan211 closed issue #26505: Correlated scalar subquery ignores `HAVING` that is true for empty input
URL: https://github.com/apache/datafusion/issues/26505


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Correlated scalar subquery ignores `HAVING` that is true for empty input [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #26505:
URL: https://github.com/apache/datafusion/issues/26505#issuecomment-6479352374

   When fixing this bug, I think we should make the query error rather than trying to make it work / give correct results. To make it work and have confidence it will be correct, I think we need a more general approach than patches. See here for details
   - https://github.com/apache/datafusion/issues/5492#issuecomment-6479090126
   


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Correlated scalar subquery ignores `HAVING` that is true for empty input [datafusion]

Posted by "abp002 (via GitHub)" <gi...@apache.org>.
abp002 commented on issue #26505:
URL: https://github.com/apache/datafusion/issues/26505#issuecomment-6349368346

   take


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Correlated scalar subquery ignores `HAVING` that is true for empty input [datafusion]

Posted by "abp002 (via GitHub)" <gi...@apache.org>.
abp002 commented on issue #26505:
URL: https://github.com/apache/datafusion/issues/26505#issuecomment-6348088147

   take
   


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Correlated scalar subquery ignores `HAVING` that is true for empty input [datafusion]

Posted by "0xamlab (via GitHub)" <gi...@apache.org>.
0xamlab commented on issue #26505:
URL: https://github.com/apache/datafusion/issues/26505#issuecomment-6317223500

   Reproduced on main (6c1727fc, cli v55.1.0): v=10 for a=1 where PostgreSQL 17 returns NULL, and EXPLAIN shows no HAVING filter. Root cause: PullUpCorrelatedExpr pulls a HAVING that is true on empty input out of the subquery and removes the Filter (datafusion/optimizer/src/decorrelate.rs:435), intending to re-apply it via the count-bug compensation CASE. But build_join skips that compensation for aggregates whose empty-input value is NULL (sum/max/min) at datafusion/optimizer/src/scalar_subquery_to_join.rs:407, so the HAVING is never re-applied. count(*) escapes because its empty-input value 0 is non-NULL, hence HAVING count(*) = 0 still returns NULL, 0.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org