beanquery icon indicating copy to clipboard operation
beanquery copied to clipboard

Support arithmetic operations on Amounts

Open dnicolodi opened this issue 2 years ago • 9 comments

In a mailing list thread https://groups.google.com/g/beancount/c/4EkTUWoLXwI and in beancount/beancount#688 it was proposed to add the possibility of performing arithmetic operations on Amounts. The only operations that make sense are multiplication and division of amount by an integer or a decimal, and addition and subtraction of amounts.

Multiplication and division by an integer or a decimal are not an issue as these operations always succeed and return another Amount object.

For sums and differences the problem is a bit more complex. What to do when amounts with incompatible commodities are summed or subtracted? Following the SQL philosophy, we do not want to raise an exception based on the data involved in the query. The only sensible thing is to return NULL. However this would be dangerous, because one very common thing to do is to sum these values into an Inventory, and in this case the problem would not be evident at all.

@IguanaBen, what is your use case for addition and subtractions of Amounts in BQL?

dnicolodi avatar Apr 07 '22 23:04 dnicolodi