Skip to content
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

More powerful expr: queries ? #2095

Open
simonmichael opened this issue Oct 5, 2023 · 7 comments
Open

More powerful expr: queries ? #2095

simonmichael opened this issue Oct 5, 2023 · 7 comments
Labels
A-WISH Some kind of improvement request, hare-brained proposal, or plea. needs:design To unblock: needs more thought/planning, leading to a spec/plan needs:mockup/screenshot To unblock: needs a rough mockup, eg in plain text, or a screenshot queries Filtering options, query arguments..

Comments

@simonmichael
Copy link
Owner

simonmichael commented Oct 5, 2023

We added the more expressive "expr:" queries recently, but they don't give as much extra power as you'd think; eg they don't let us select whole transactions by the amount posted to a particular account (real world example). Eg it would be nice to be able to print whole transactions which post between $100 and $200 to assets:foo.

@simonmichael simonmichael added A-WISH Some kind of improvement request, hare-brained proposal, or plea. queries Filtering options, query arguments.. needs:design To unblock: needs more thought/planning, leading to a spec/plan needs:mockup/screenshot To unblock: needs a rough mockup, eg in plain text, or a screenshot labels Oct 5, 2023
@simonmichael
Copy link
Owner Author

@simonmichael
Copy link
Owner Author

B> I'm looking for a way to find transactions which are pending at a given time, i.e. given a point in time T, have postings with posting dates both before and after T. Is there a builtin way to do this with hledger?

B> Would there be interest in adding such a feature? One idea I had is to have a filter that matches on posting date, which could be used to implement this easily by using something like pdate:..T and pdate:T..

S> which posting(s) would that match ?

B> I would assume it matches transactions with both a posting with date before T and a posting with date after T (since for print, the filter AND composition is on transaction level)

B> but I see that this could be too complex to understand, so perhaps for now I'll just use a custom haskell script adapted to my usecase

S> I see, so that would be "(any posting dated <T) and (any posting dated >=T)"

S> a general way to apply queries to any/all postings within transactions seems possibly useful, also

S> expr:"any:date:..T and any:date:T.."

S> starts to get a bit complex indeed, and IME such things are not needed often in practice, but would be interesting to try

S> it's more justifiable if it's general and composable - not just adding another few special cases

@pmiam
Copy link

pmiam commented Jul 22, 2024

I am interested in querying transactions consisting of postings that are tagged with certain key=value pairs exclusively. The print command offers some support for this but it is incomplete. Consider the example

Y 2024

2024-01-01 * Taco House|two brothers dinner
           X:service:food  EUR 10 ; iou:bob, you owe me, bro
           X:service:food  EUR 10 ; me
           L:cc:visa

2024-12-31 * Taco House|three professionals dinner
           X:service:food  EUR 10 ; iou:bob, I'm handling your finances, bro
           X:service:food  EUR 10 ; iou:steve
           X:service:food  EUR 10
           I:gift                 ; iou:steve, I owe steve for the both of us

If I want to query specifically ious in transactions involving bob and steve, I can simply filter the journal because -- in this case -- multiple tag queries seem to implicitly AND.

hledger print tag:iou=bob tag:iou=steve | hledger -f- -c"EUR 1,000.00" --pivot=iou

However, I am struggling to query what bob owes me without consideration to steve. That is, I can't figure out how to filter for transactions exclusively matching the tag:iou=bob query.

I have to filter with not:tag:iou=steve which naturally does not scale. I've likely missed something, but I wonder if this requires an expr feature?

@simonmichael
Copy link
Owner Author

simonmichael commented Jul 22, 2024 via email

@pmiam
Copy link

pmiam commented Jul 23, 2024

Thank you for your quick response, Simon. The bookkeeping in this example could certainly be better, I use it here as one case where querying for related postings might be useful.

Using reg or bal and filtering for a single iou value shows the iou balance for that name. However, this eagerly seeks out every posting the name is iou'd on. Say I want to see bob's iou balance for only when steve is involved in the transactions.

e.g. bob and I are usually lenient with each-other, but when I pay steve I want to be sure bob pays his share.

For this senario, my suggested print filter is very helpful.

However, when I want to see bob's iou balance for when bob and steve and nobody other than bob and steve are involved in the transactions, I have had to filter the journal with an unwieldy number of not: prefixes.

Good news! While writing this response I found my answer. I forgot that queries interpret regular expressions. So, to see bob and only bob simply write

hledger print tag:iou=bob not:tag:iou="[^bob]"

This doesn't necessarily scale effortlessly, but it's certainly better than selective exclusion. I still wonder if some shorthand query syntax could be added as a convenience for this sort of thing.

perhaps exc:tag:iou=bob?

EDIT: This would be specific to commands that return whole transactions. perhaps an --exclusive flag on those commands would be more appropriate

@pmiam
Copy link

pmiam commented Jul 24, 2024

perhaps an --exclusive flag on those commands would be more appropriate

upon further reflection, I think the existing query syntax is completely sufficient. There's more power in explicitly written regexps e.g. hledger print not:tag:iou="[^bob|^steve]" than there is convenience in modified query behavior.

Also, expanding the query interface adds more opportunities to mess up a report.

I'd be happy to contribute an example like this to the documentation if you consider it appropriate.

@simonmichael
Copy link
Owner Author

simonmichael commented Jul 24, 2024

Hi @pmiam .. I see, you're looking for a way to "match whole transactions which contain only the specified values for this tag and no others".

Your regexps above seem to be to be using character classes, not quite correctly - and yet they're working here too in my quick tests, and just now I don't understand why.

Negative lookahead would (might ?) help with this, but hledger's regexps don't support them at present.

(After playing around with it, I agree this is quite a hard query to achieve otherwise.)

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, hare-brained proposal, or plea. needs:design To unblock: needs more thought/planning, leading to a spec/plan needs:mockup/screenshot To unblock: needs a rough mockup, eg in plain text, or a screenshot queries Filtering options, query arguments..
Projects
None yet
Development

No branches or pull requests

2 participants