simonmichael / hledger

Robust, fast, intuitive plain text accounting tool with CLI, TUI and web interfaces.
https://hledger.org
GNU General Public License v3.0
2.85k stars 307 forks source link

in expr: queries, open-ended date periods are not OR'd correctly #2177

Closed johanvanl closed 4 months ago

johanvanl commented 4 months ago

Logging a bug from a discussion I posted on the HLedger Google Groups.

Simplifying the example even more.

Version I am running: hledger 1.32.2-g47d26d99a-20240105, windows-x86_64

Sample Journal:

2023-10-09
  assets:day_to_day_accounts:bank  -30
  expenses:housing:rent

2023-11-26
  assets:day_to_day_accounts:bank  -200
  expenses:housing:rent

2023-12-26
  assets:day_to_day_accounts:bank  -200
  expenses:housing:rent

2023-12-27
  assets:day_to_day_accounts:bank
  expenses:day_to_day:food  25

2024-01-09
  assets:day_to_day_accounts:bank
  expenses:day_to_day:food  25

2024-01-26
  assets:day_to_day_accounts:bank  -200
  expenses:housing:rent

I have an expression that should return any 'expense' from 2024, and only 'expenses:housing:rent' from previous years. As follows:

expr:"( expenses:housing:rent AND date:..2024-01-01 ) OR ( ^expenses: AND date:2024-01-01..tomorrow )"

This should return all the transactions from the example Journal except for the one 'expenses:day_to_day:food' transaction from 2023.

Which is what print does, if running as follows:

hledger -f finances.hledger print expr:"( expenses:housing:rent AND date:..2024-01-01 ) OR ( ^expenses: AND date:2024-01-01..tomorrow )"

As follows:

2023-10-09
    assets:day_to_day_accounts:bank             -30
    expenses:housing:rent

2023-11-26
    assets:day_to_day_accounts:bank            -200
    expenses:housing:rent

2023-12-26
    assets:day_to_day_accounts:bank            -200
    expenses:housing:rent

2024-01-09
    assets:day_to_day_accounts:bank
    expenses:day_to_day:food                     25

2024-01-26
    assets:day_to_day_accounts:bank            -200
    expenses:housing:rent

The other commands (I originally picked up the issue using is, but reg shows the issue even better), besides print, only picks up the 2 transactions from 2024. For example, register:

hledger -f finances.hledger reg expr:"( expenses:housing:rent AND date:..2024-01-01 ) OR ( ^expenses: AND date:2024-01-01..tomorrow )"

Outputs:

2024-01-09                      ex:day_to_day:food              25            25
2024-01-26                      ex:housing:rent                200           225

Why is the expression interpreted differently for different commands?

simonmichael commented 4 months ago

@chrislemaire , would you have time to look into this (and #2178) ? Seems like possibly a weakness in #1989.

johanvanl commented 4 months ago

Same issue as #2178 which was logged just after, after looking at that issue, my issue can be simplified even further.

Journal

2023-12-26
  assets:day_to_day_accounts:bank  -200
  expenses:housing:rent

2024-01-26
  assets:day_to_day_accounts:bank  -200
  expenses:housing:rent

Print Command works

hledger -f finances.hledger print expr:"date:..2024-01-01 OR date:2024-01-01..tomorrow"

IS Command doesn't work

hledger -f finances.hledger is expr:"date:..2024-01-01 OR date:2024-01-01..tomorrow"

simonmichael commented 4 months ago

Notes:

simonmichael commented 4 months ago

To work around: specify both ends of the date: periods when using OR.

simonmichael commented 4 months ago

Fixed in master. Hledger.Data.Date's spanUnion has always been wrong with open-ended date spans; now there's both spanUnion and spanExtend.

Thanks for the report!