bql subtract Amounts?

171 views
Skip to first unread message

Ben L

unread,
Dec 13, 2021, 9:13:17 PM12/13/21
to Beancount
Is there a way to subtract Amounts (same currency) in bql? 

select cost(position) - value(position);

I get:

TypeError: unsupported operand type(s) for -: 'Amount' and 'Amount'

I can structure the query so the results are only one currency. Is there a function that will report an Amount as just a number (lose the currency)? 

Thanks,

Ben

Martin Blais

unread,
Dec 14, 2021, 12:23:50 AM12/14/21
to Beancount
I don't think it's supported; you can try adding it (in branch master), should be easy, just check the currencies match



--
You received this message because you are subscribed to the Google Groups "Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beancount+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/3bb043d0-e8a9-4cef-ad93-1987100df5ccn%40googlegroups.com.

Ben L

unread,
Dec 14, 2021, 4:05:37 PM12/14/21
to Beancount
Happy to take a stab at it if you want to point where it look. I can't find where in the beancount code to start. Since the error raised is TypeError,  I searched that and for the "unsupported operand" error, but I can't find that in the code. Can you point me to where does the bql stuff for math (add, subtract, etc) get handled?

Thanks,

redst...@gmail.com

unread,
Dec 14, 2021, 4:29:59 PM12/14/21
to Beancount
I would start with this line:

class EvalSub(EvalBinaryOp):


 def __init__(self, left, right):

   f = lambda x, y: Decimal(x - y)
   super().__init__(f, left, right, Decimal)

It's in query_compile.py:210. I'd imagine subtraction done via Decimal should instead be modified to handle your case. Tests are here.

"unsupported operand type(s) for -:" is a python TypeError (not specific to Beancount), which is why you didn't find it in the code. I did "git grep operand" to find the above.

This would be a good addition, BTW. Thanks for taking a stab at it!

redst...@gmail.com

unread,
Dec 14, 2021, 4:37:49 PM12/14/21
to Beancount
See this comment right above those lines from Martin. Would be great to do get both these working if you're up for it :)

# Note: We ought to implement implicit type promotion here,
# e.g., int -> float -> Decimal.

# Note(2): This does not support multiplication on Amount, Position, Inventory.
# We need to rewrite the evaluator to support types in order to do this
# properly.

Ben L

unread,
Dec 14, 2021, 10:52:47 PM12/14/21
to Beancount
The changes I made were pretty minor and leveraged existing code for adding  / subtracting Amounts. See https://github.com/beancount/beancount/pull/688 and let me know what you think, etc.

Martin Blais

unread,
Dec 17, 2021, 11:11:25 PM12/17/21
to Beancount
Sorry just catching up on that thread.
I've been meaning to delete the beanquery from beancount/master and start using the beanquery repo.
To be fair, I haven't been able to switch over yet; I just added it to my environment, it works.
I should probably delete beancount/beancount/query
Not sure how to proceed yet, maybe this weekend there's time




Reply all
Reply to author
Forward
0 new messages