Skip to content

feat: change: and balance: queries match reported amounts; amt: accepts a commodity symbol - #2847

Merged
simonmichael merged 2 commits into
mainfrom
sum-query
Nov 9, 2026
Merged

simonmichael merged 2 commits into
mainfrom
sum-query

Conversation

@simonmichael

@simonmichael simonmichael commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Queries could match postings by amount (amt:), but not accounts by their reported change or balance.
This adds two query terms which test the calculated report items: change: tests a row's change (the sum of its postings), balance: its balance at the end of its period.

hledger bal expenses change:'>100'    # accounts which changed by more than 100
hledger bal assets balance:'<0'       # accounts with a negative balance
hledger reg gifts -M change:'>100'    # months with more than 100 in gifts
hledger areg checking balance:'<0'    # transactions after which checking was overdrawn
hledger print not:change:0            # entries with a tolerated imbalance or unbalanced virtual postings
  • Same comparison syntax as amt: (signed or magnitude). N may have a commodity symbol, eg change:'>$100' or balance:'<0 EUR', to compare just that commodity.
  • Each term's meaning is fixed by the kind of item matched, not by the command or its display options. balance: is the actual balance from the start of the journal, whether or not -H is used; it makes bal include the earlier postings, and matched accounts are shown even if they had no postings in the period. Amounts are tested with their real sign, as amt: does, not as displayed after --invert or bs/is sign normalisation.
  • Balance reports (bal, bs, is, cf, hledger-ui, hledger-web) test each account's own change or end balance, and leave out non-matching accounts as an account query would, so parent balances, totals and percentages include just the matching accounts. A depth limit tests parents' inclusive amounts.
  • Budget reports hide non-matching rows, leaving goals and totals unchanged.
  • register tests postings (or period summaries) and the running balance after them; aregister tests each transaction's change to the account and the balance after it. Hidden rows still count in running totals.
  • print (and descriptions, notes, codes) tests each entry's sum at cost, including unbalanced virtual postings, for both terms (an entry has no balance).
  • They combine with other terms only by AND (or not:); inside OR, a negated group, any: or all: it's an error.
  • amt: also accepts a commodity symbol now (amt:'>$100'), and the amt: docs are corrected: a multi-commodity posting matches if any of its amounts does.

Internally, the terms are moved from the report query into a new ReportSpec field, _rsRowQuery, which reports apply after aggregating. The posting and transaction matchers ignore them.

Docs: new "balance: and change: queries" section in the manual, and a line in hledger-web's search help (hledger.pot updated). Tests: hledger/test/query-balance-change.test (40 tests) and query-amt.test, plus unit tests and doctests in Hledger.Query.

The plan, with the revision from the original sum: design and implementation notes: doc/PLAN-balance-change-queries.md

AI usage: Claude Fable 5.1/Opus 5.5, ~208k output tokens

@simonmichael simonmichael added A-WISH Some kind of improvement request or proposal. queries Filtering options, query arguments.. labels Nov 8, 2026
@simonmichael simonmichael changed the title feat: sum: matches report row amounts feat: sum: matches report amounts Nov 8, 2026
@simonmichael simonmichael changed the title feat: sum: matches report amounts feat: new sum: query matches report amounts; amt: accepts a commodity symbol Nov 8, 2026
@simonmichael
simonmichael added this pull request to stack #2849 November 8, 2026 22:59
@simonmichael
simonmichael removed this pull request from stack #2849 November 8, 2026 22:59
@simonmichael simonmichael changed the title feat: new sum: query matches report amounts; amt: accepts a commodity symbol feat: change: and balance: queries match reported amounts; amt: accepts a commodity symbol Nov 9, 2026
@simonmichael

Copy link
Copy Markdown
Member Author

Revised: sum: is replaced by two terms with fixed meanings, change: and balance:, and the branch was rewritten accordingly (the description above is updated).

sum: tested "whatever amount the report displays": an account's change, or with -H its end balance, with the displayed sign. So its meaning depended on report options, and a query couldn't say which it meant. That complicated the assert directive design (#2848), which needed a balance/change keyword to pin down sum:'s meaning.

With change: and balance:, the query says what is tested, everywhere: balance: is the actual balance (from the start of the journal, whether or not -H is used), change: the change in the period, and both test real signs like amt:. Hidden register rows now count in running totals, so the balances shown and tested are the real ones. More details are in the plan's revision section.

The sum: query's plan (never committed), with a revision replacing
sum: by two terms with fixed meanings: change: (an item's change) and
balance: (its actual end balance), defined per kind of item matched
rather than per command. Agreed decisions: real signed amounts,
balance: fetching earlier postings, registers filtering after running
totals, entries' balance: equal to change:.

AI usage: Claude Opus 5.5/Fable 5.1, ~43k output tokens
…ounts

Two new query terms select report rows, or entries, by their summed
amounts, which amt: can't do since it tests individual postings:
change: by the row's change (the sum of its postings), balance: by its
balance at the end of its period. Eg:

    hledger bal expenses change:'>100'    # accounts which changed by more than 100
    hledger bal assets balance:'<0'       # accounts with a negative balance
    hledger reg gifts -M change:'>100'    # months with more than 100 in gifts
    hledger areg checking balance:'<0'    # transactions after which checking was overdrawn
    hledger print not:change:0            # entries with a tolerated imbalance or unbalanced virtual postings

The comparison works like amt:'s, and N may have a commodity symbol
(change:'>$100', balance:'<0 EUR') to compare just that commodity.
Each term's meaning is fixed by the kind of item matched, not by the
command or its display options. balance: means the actual balance,
from the start of the journal, whether or not -H is used (a balance:
term makes bal include the earlier postings, and shows matched
accounts even if they had no postings in the period). Amounts are
tested with their real sign, as with amt:, not as displayed after
--invert or incomestatement's sign normalisation.
Balance reports test each account's own amounts, leaving out
non-matching accounts as an account query would, so parent balances,
totals and percentages include just the matching accounts. Budget
reports hide the non-matching rows. register tests postings or period
summaries and their running balance; aregister tests each
transaction's change and the balance after it; hidden rows still count
in running totals. print (and descriptions, notes, codes) tests each
entry's sum at cost, including unbalanced virtual postings, for both
terms. They can be combined with other terms only by AND, and work in
hledger-ui and hledger-web's search too.

Also, amt: can now include a commodity symbol, to compare only amounts
in that commodity: amt:'>$100', amt:'<200 EUR'. A posting with no
amount in that commodity doesn't match. This avoids cur: and its regex
escaping (cur:'\$' amt:'>100'). The amt: manual section is corrected:
it said postings with multi-commodity amounts always match, but they
match if any of their amounts does.

Internally, the terms are moved from the report query to a new
ReportSpec field, _rsRowQuery, which the reports apply after
aggregating; the posting and transaction matchers ignore them.

AI usage: Claude Fable 5.1/Fable 5.2/Opus 5.5, ~212k output tokens
@simonmichael
simonmichael merged commit 8c9ef89 into main Nov 9, 2026
2 checks passed
@simonmichael
simonmichael deleted the sum-query branch November 9, 2026 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-WISH Some kind of improvement request or proposal. queries Filtering options, query arguments..

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant