Skip to content

Fix COUNT(*) returning incorrect counts for unmatched aggregation rows - #1774

Open
godara-alt wants to merge 4 commits into
datajoint:masterfrom
godara-alt:fix/1709-count-star-left-aggregate
Open

godara-alt wants to merge 4 commits into
datajoint:masterfrom
godara-alt:fix/1709-count-star-left-aggregate

Conversation

@godara-alt

Copy link
Copy Markdown
Contributor

Summary

Fixes #1709.

Fixed an issue where COUNT(*) returned 1 instead of 0 for unmatched rows in left aggregations.

The aggregation now uses a presence marker projected from the grouped relation, allowing COUNT(*) to count only actual matching rows. This also handles cases where the grouping and grouped relations share all primary key attributes.

Added seven regression tests covering one-to-many and one-to-one relationships, restricted and nested aggregations, Top, HAVING, column-name collisions, and inner aggregation behavior.

Testing

Verified the changes through GitHub Actions on the fork.

All 11 test jobs passed.

Lint passed.

Tests passed across Python 3.10, 3.14, and 3.15.

MySQL 8.0 and 8.4 integration tests passed.

Windows unit tests passed.

Minimum dependency version tests passed.

Result: All 12 CI jobs passed successfully.

Validation runs:

Test workflow

Lint workflow

@dimitri-yatsenko dimitri-yatsenko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @godara-alt. The fix is correct. I compared the generated SQL and results with master on MySQL and PostgreSQL for a plain, restricted, one-to-one, renamed, join, union and nested-aggregation group, with ..., HAVING on the count, count(*)+1, sum(x)/count(*) and COUNT( * ). Unmatched entities now count 0; entities with matches, inner aggregation (exclude_nonmatching=True) and dj.U(...).aggr are unchanged. Six of the seven new tests fail without the change, as intended.

Release: this changes results for existing queries (unmatched entities go from 1 to 0, and HAVING filters and sums over a nested count change with them), so it belongs in 2.4 with #1709, not in the 2.3.7 patch release. I've set the milestone to v2.4; it will merge after 2.3.7 is tagged.

Smaller points, none blocking:

  • count(*) is found by matching text, so a string literal containing count(*) is rewritten too: s="'count(*)'" returns 'count(dj_aggr_count_present)'. Skipping quoted segments before the substitution would avoid it; fine to leave as an edge case.
  • count(1) (any constant count) still reports 1 for unmatched entities. Worth a sentence in the docs, or a follow-up.
  • The description says nested aggregations are tested, but none of the new tests builds one. They work in my check; a test would show the inner and outer markers sharing one name without clashing.
  • Every left aggregation that uses count(*) now puts the group in a derived table. Results are right; worth knowing for large tables.
  • After this and #1671 both merge, #1671's test_pruning_keeps_having_alias must look for count( rather than count(*). Whichever lands second will update it.

The documentation still tells readers to avoid count(*) in a left aggregation; we'll update it for 2.4.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants