Skip to content

Oprava počtu řádků při GROUP BY přes více sloupců (Nette Database Table)#1294

Open
radimvaculik wants to merge 2 commits into
masterfrom
fix/issue-991-grouped-count
Open

Oprava počtu řádků při GROUP BY přes více sloupců (Nette Database Table)#1294
radimvaculik wants to merge 2 commits into
masterfrom
fix/issue-991-grouped-count

Conversation

@radimvaculik

Copy link
Copy Markdown
Member

Problem

NetteDatabaseTableDataSource::getCount() generated invalid SQL for selections grouped by multiple columns. When a GROUP BY was present it built COUNT(DISTINCT col1, col2, ...), which:

  • MySQL — is valid but ignores rows where any of the grouped columns is NULL, so the count is undercounted (returns 0 when every row has a NULL in one of the grouping columns).
  • SQLite — is not supported at all and throws wrong number of arguments to function COUNT().

Fixes #991.

Fix

Count the number of groups via a subquery instead of relying on COUNT(DISTINCT ...):

SELECT COUNT(*) FROM (<original grouped query>) AS datagrid_count

This matches the approach already used by NetteDatabaseDataSource::getCount() and returns the correct count regardless of NULL values in the grouping columns. The duplicated GROUP BY handling in both branches of the method was consolidated into a single early check.

Tests

Added testGetCountWithGroupByMultipleColumns() reproducing the issue scenario (grouping by four columns with NULL values, 4 rows → 3 groups). getCount() now returns 3, consistent with getData().

make qa (PHPStan + phpcs) and the full test suite pass.

getCount() nahrazuje neplatné COUNT(DISTINCT sloupec1, sloupec2, ...) počítáním
skupin přes poddotaz, takže vrací správný počet i když seskupovací sloupce obsahují
NULL hodnoty. Řeší issue #991.
@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 48.95%. Comparing base (d9144ab) to head (c92fc68).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1294      +/-   ##
==========================================
+ Coverage   48.77%   48.95%   +0.17%     
==========================================
  Files          61       62       +1     
  Lines        2901     2905       +4     
==========================================
+ Hits         1415     1422       +7     
+ Misses       1486     1483       -3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Metoda Selection::getExplorer() existuje až v nette/database 3.2.9, ale CI běží
s 3.2.8. Použití zavedeného reflection helperu getContext() funguje napříč
podporovanými verzemi.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect rows count when using GROUP BY with multiple columns - Nette\Database\Table

1 participant