Skip to content

fix: implement implicit GROUP BY for aggregate queries (issue #575) - #590

Merged
robcohen merged 1 commit into
mainfrom
fix/issue-575-implicit-group-by
Mar 29, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/issue-575-implicit-group-by

Conversation

@robcohen

Copy link
Copy Markdown
Member

Summary

  • Implemented implicit GROUP BY when aggregate functions are mixed with non-aggregated columns
  • Added extract_implicit_group_by_exprs() helper to extract non-aggregate expressions
  • Modified execute_select() to use implicit grouping when no explicit GROUP BY is provided

Problem

Query SELECT sum(number), currency, account ORDER BY account returned 1 row instead of 3.

Python beancount output (expected):

sum(num  cur       account
-------  ---  -----------------
-550.00  EUR  Assets:Bank
   5     ABC  Assets:Investment
  50.00  EUR  Expenses:Food

Rustledger output (before fix):

sum  currency  account
----  --------  -------------
-495  EUR       Expenses:Food

1 row(s)

Root Cause

When aggregate functions are mixed with non-aggregated columns and no explicit GROUP BY is provided, Python beancount implicitly groups by the non-aggregated columns. Rustledger was treating all postings as a single group.

Fix

In execute_select(), when an aggregate query has no explicit GROUP BY:

  1. Extract non-aggregate column expressions from SELECT targets
  2. Use them as implicit GROUP BY expressions
  3. Pass to group_postings() for proper grouping

Test plan

  • cargo test -p rustledger-query - all 189 tests pass
  • cargo clippy -p rustledger-query -- -D warnings - no warnings
  • Added 3 regression tests:
    • test_issue_575_implicit_group_by - main regression test
    • test_pure_aggregate_no_implicit_group_by - verifies pure aggregates still work
    • test_explicit_group_by_overrides_implicit - verifies explicit GROUP BY takes precedence

Fixes #575

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner March 29, 2026 16:00
Copilot AI review requested due to automatic review settings March 29, 2026 16:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Implements Python beancount-compatible implicit GROUP BY behavior for aggregate queries that mix aggregate functions with non-aggregated columns (fixing issue #575).

Changes:

  • Added implicit grouping logic in execute_select() for aggregate queries when no explicit GROUP BY is provided.
  • Introduced extract_implicit_group_by_exprs() helper to derive non-aggregate grouping expressions from SELECT targets.
  • Added integration/regression tests covering implicit grouping, pure aggregates, and explicit GROUP BY precedence.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
crates/rustledger-query/src/executor/execution.rs Computes an “effective” group-by for aggregate queries (explicit if present, otherwise implicit).
crates/rustledger-query/src/executor/aggregation.rs Adds helper to extract non-aggregate expressions from SELECT targets for implicit grouping.
crates/rustledger-query/tests/bql_integration_test.rs Adds regression tests for issue #575 and related grouping behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@robcohen
robcohen force-pushed the fix/issue-575-implicit-group-by branch from 6d45d94 to fceb899 Compare March 29, 2026 16:14
When aggregate functions are mixed with non-aggregated columns and no
explicit GROUP BY is provided, Python beancount implicitly groups by
the non-aggregated columns. This fix implements the same behavior.

For example, `SELECT sum(number), currency, account ORDER BY account`
now correctly returns one row per (currency, account) combination
instead of a single aggregated row.

Changes:
- Added `extract_implicit_group_by_exprs()` to extract non-aggregate
  column expressions from SELECT targets
- Modified `execute_select()` to use implicit GROUP BY when needed
- Added regression tests for issue #575

Fixes #575

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/issue-575-implicit-group-by branch from fceb899 to 5d42064 Compare March 29, 2026 16:22
@codecov

codecov Bot commented Mar 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.45455% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...rates/rustledger-query/src/executor/aggregation.rs 91.66% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 41.2ms ± 0.9ms 26.2 MB
beancount 864.5ms ± 7.8ms 50.1 MB
Comparison 21x faster 1.9x less

vs main branch (2026-03-29)

Metric main this PR Change
Time 43ms 41.2ms -4.2% ✅
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@robcohen
robcohen enabled auto-merge March 29, 2026 16:26
@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 98%
Total Files 783
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@robcohen
robcohen added this pull request to the merge queue Mar 29, 2026
Merged via the queue into main with commit 77af455 Mar 29, 2026
33 of 35 checks passed
@robcohen
robcohen deleted the fix/issue-575-implicit-group-by branch March 29, 2026 16:38
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.

[Compat]: Query returns just one row

2 participants