Skip to content

Apply amt:/cur: queries after valuation, not before (to fix boolean queries) #2373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

simonmichael
Copy link
Owner

@simonmichael simonmichael commented Apr 19, 2025

This is a draft for discussion. It reverses #1625 (matching amt:/cur: before valuation) to fix #2371 (some boolean queries not working).

Here's the current description:

Until now, at least since #1625 (2021), cur: and amt: matched the
pre-cost/value-conversion commodity symbols and amounts.

To do that, we were separating the cur:/amt: parts from the rest of
the report query. But with boolean queries #1989 (2023), it's not
possible to do that reliably without changing the query's meaning (?).

So we no longer do that, which means that cur: and amt: now match the
post valuation amounts. This is a breaking change for reports:

  • register and all balance reports (using journalValueAndFilterPostingsWith)
    and aregister, hledger-ui & hledger-web register views (using accountTransactionsReport)
    no longer pre-filter by amt:/cur:. amt:/cur: with these reports now
    match the post-valuation amounts and commodity symbols, not the
    pre-valuation ones.

  • print, codes, descriptions, notes, hledger-web journal view (using entriesReport)
    now apply all queries after valuation, not before it
    (for consistency with the above).

This fixes boolean queries, or at least some of them. There are other cases where reports transform the report query (eg with filterQuery), which have not been fully audited. A common case is removing the depth: limit from the query. It's not yet clear if these cases can also break boolean queries - more auditing, testing and producing repros will help.

"Early amt:/cur:" (before valuation) has been the status quo for years. So this needs more thought and ideas about whether and how to preserve compatibility. As it stands, this would break some existing reports, and might make those reports hard or impossible to produce at all (testing needed).

Some options to consider ?

  1. Try to match with amt:/cur: both before and after valuation.
    (We do this with acct:, trying to match both before and after account name aliasing.)
    Even if advisable, I'm not sure how feasible it would be to make this robust.
    Eg, where A amounts have been converted to value in B:

    • reg -V cur:A matches the B amounts (because they were formerly A)
    • reg -V cur:B also matches the B amounts
    • reg -V expr:'(cur:A AND cash) OR checking' is still problematic; cur:A is part of a more complex query, can't extract and prefilter with it
  2. Keep the old behaviour for simple queries, but disable it when a problematic boolean query is detected (one with amt:/cur: in subexpressions).
    Inconsistent behaviour would probably be bad.
    Eg:

    • reg -V expr:'cur:A AND cash' cur:A is in a top level AND, can extract it and prefilter with it (and exclude it from the postfilter so it doesn't exclude the B result)
    • reg -V expr:'(cur:A AND cash) OR checking' cur:A is in a subquery, can't extract/prefilter with it; must leave it in the postfilter (it will exclude the B result)
  3. Disallow amt:/cur: in boolean queries or subqueries.
    (We already disallow date: in OR subexpressions.)
    This might be over-restrictive.
    Eg:

    • reg -V expr:'cur:A AND cash' "Error: amt: and cur: are not supported in boolean queries"
    • reg -V expr:'(cur:A AND cash) OR checking' "Error: amt: and cur: must be part of a top-level AND query"
    • reg -V cur:A cash works as before
  4. Think harder about

    • more safe/limited/reasonable ways to extract the amt:/cur: queries for pre-filtering, preserving the old behaviour without altering boolean queries.
    • why those reports were doing that pre-filtering, and if it's really needed. Reports often make a variant of the user's query, eg removing a depth limit, or changing the period in order to calculate starting balances, while preserving the rest of the query as-is.
    • how to ease the transition, if behaviour is changed

…es (WIP)

Until now, at least since #1625 (2021), cur: and amt: matched the
pre-cost/value-conversion commodity symbols and amounts.

To do that, we were separating the cur:/amt: parts from the rest of
the report query. But with boolean queries #1989 (2023), it's not
possible to do that reliably without changing the query's meaning (?).

So we no longer do that, which means that cur: and amt: now match the
post valuation amounts. This is a breaking change for reports:

- register and all balance reports (using journalValueAndFilterPostingsWith)
  and aregister, hledger-ui & hledger-web register views (using accountTransactionsReport)
  no longer pre-filter by amt:/cur:. amt:/cur: with these reports now
  match the post-valuation amounts and commodity symbols, not the
  pre-valuation ones.

- print, codes, descriptions, notes, hledger-web journal view (using entriesReport)
  now apply all queries after valuation, not before it
  (for consistency with the above).
@simonmichael simonmichael changed the title sm 2371 queries apply amt:/cur: queries after valuation, not before (fix #2371) Apr 19, 2025
@simonmichael simonmichael added queries Filtering options, query arguments.. valuation needs-testing To unblock: needs more developer testing or general usage needs-design To unblock: needs more thought/planning, leading to a spec/plan needs-review To unblock: needs more code/docs/design review by someone labels Apr 19, 2025
@simonmichael simonmichael changed the title apply amt:/cur: queries after valuation, not before (fix #2371) Apply amt:/cur: queries after valuation, not before (fix #2371) Apr 19, 2025
@simonmichael simonmichael changed the title Apply amt:/cur: queries after valuation, not before (fix #2371) Apply amt:/cur: queries after valuation, not before (to fix boolean queries) Apr 19, 2025
@simonmichael simonmichael added needs-discussion To unblock: needs more discussion/review/exploration needs-impact-analysis To unblock: needs analysis of interactions with other features, users, ecosystem needs-research To unblock: needs some research/investigation labels Apr 19, 2025
@simonmichael
Copy link
Owner Author

I forgot about this and haven't been testing it further. But I probably wouldn't have noticed any problem. I think that people will notice only if it's shipped in a release, probably after a few months' delay.

To do:

  • Add a few more tests demonstrating the changed behaviour of amt: and cur:
  • Post these examples widely (mail list, chat, mastodon..) and ask for comment.

@simonmichael
Copy link
Owner Author

Or we could:

  1. Change cur: and amt: to work after valuation, so they can be used safely in boolean queries. But also add ocur: and oamt: (origcur: and origamt: ?) which work the old way, ie before valuation, and are not allowed in boolean queries. This would give an escape hatch when someone finds the new behaviour breaking their setup.

@simonmichael simonmichael added needs-docs To unblock: needs corresponding documentation or doc updates needs-code To unblock: needs code/code updates and removed needs-discussion To unblock: needs more discussion/review/exploration needs-impact-analysis To unblock: needs analysis of interactions with other features, users, ecosystem needs-testing To unblock: needs more developer testing or general usage needs-review To unblock: needs more code/docs/design review by someone needs-design To unblock: needs more thought/planning, leading to a spec/plan needs-research To unblock: needs some research/investigation labels May 13, 2025
@simonmichael simonmichael deleted the sm-2371-queries branch May 15, 2025 05:06
@simonmichael simonmichael restored the sm-2371-queries branch May 15, 2025 21:28
@simonmichael simonmichael reopened this May 15, 2025
@simonmichael simonmichael deleted the sm-2371-queries branch May 17, 2025 09:58
@simonmichael
Copy link
Owner Author

Abandoned in favour of #2387, which also fixes #2371.

@simonmichael simonmichael removed needs-docs To unblock: needs corresponding documentation or doc updates needs-code To unblock: needs code/code updates labels May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
queries Filtering options, query arguments.. valuation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

boolean queries can give wrong results
1 participant