Skip to content

boolean queries can give wrong results #2371

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
simonmichael opened this issue Apr 19, 2025 · 4 comments
Closed

boolean queries can give wrong results #2371

simonmichael opened this issue Apr 19, 2025 · 4 comments
Labels
A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. affects4-many Affects potentially a significant number of users. annoyance4-major Major usability/doc bug, or any regression or crash. queries Filtering options, query arguments..

Comments

@simonmichael
Copy link
Owner

simonmichael commented Apr 19, 2025

I found an example that's easy to reproduce:

$ hledger -f examples/sample2.journal reg expr:'(checking and amt:>0) or credit'
2025-01-01 starting balances    assets:bank:checking   1000.00 USD   1000.00 USD
                                li:credit card         -400.00 USD    600.00 USD
2025-01-01 pay rent             assets:bank:checking   -800.00 USD   -200.00 USD
2025-01-02 salary               assets:bank:checking   1000.00 USD    800.00 USD
2025-01-03 pay half of credi..  assets:bank:checking   -200.00 USD    600.00 USD
                                li:credit card          200.00 USD    800.00 USD
2025-01-04 shopping             assets:bank:checking   -250.00 USD    550.00 USD

This should not be showing the negative checking postings. I expected to see:

2025-01-01 starting balances    assets:bank:checking   1000.00 USD   1000.00 USD
                                li:credit card         -400.00 USD    600.00 USD
2025-01-02 salary               assets:bank:checking   1000.00 USD   1600.00 USD
2025-01-03 pay half of credi..  li:credit card          200.00 USD   1800.00 USD

The problem is with the filterQuery calls in journalValueAndFilterPostingsWith. It doesn't know how to handle a non-trivial boolean query, and ends up altering it:

ghci> filterQuery queryIsAmtOrSym $ Or [ And [ Acct ( toRegex' "checking" ) , Amt Gt 0 ] , Acct ( toRegex' "credit" ) ]
Any
ghci> filterQuery (not.queryIsAmtOrSym) $ Or [ And [ Acct ( toRegex' "checking" ) , Amt Gt 0 ] , Acct ( toRegex' "credit" ) ]
Or
    [ Acct
        ( Regexp "checking" )
    , Acct
        ( Regexp "credit" )
    ]
@simonmichael simonmichael added A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. queries Filtering options, query arguments.. annoyance4-major Major usability/doc bug, or any regression or crash. affects4-many Affects potentially a significant number of users. labels Apr 19, 2025
@simonmichael simonmichael changed the title boolean queries don't always work right boolean queries can give wrong results Apr 19, 2025
@simonmichael
Copy link
Owner Author

And for the record, removing those filterQuery calls (just using q for both) fixes the above case, but breaks others, like this one:

hledger -f examples/sample2.journal reg not:desc:pay

@simonmichael
Copy link
Owner Author

simonmichael commented Apr 19, 2025

The query is split into two "equivalent" queries which can be applied in stages, before and after valuation:

We make sure to first filter by amt: and cur: terms, then value the 'Journal', then filter by the remaining terms.

which I believe was done to accommodate #1625 (2021), making cur: and amt: match the pre-valuation amounts. But this is incompatible with boolean queries (#1989, 2023), which can't be split this way without changing their meaning.

@simonmichael
Copy link
Owner Author

Posted a draft fix, #2373, with some ideas.

@simonmichael
Copy link
Owner Author

Fixed by #2387.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. affects4-many Affects potentially a significant number of users. annoyance4-major Major usability/doc bug, or any regression or crash. queries Filtering options, query arguments..
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant