goldmansachs/reladomo

Divide by 0 error

Opened this issue · 5 comments

In an Operation due to the non-existent NULLIF clause if there is a division then the query throws a divide by 0 error.
For Example:
PersonFinder.age().dividedBy(PersonFinder.daysLived()).greaterThan(365.0) can throw a divide by zero error even if daysLived().notEq(0.0) condition is added.

https://stackoverflow.com/questions/861778/how-to-avoid-the-divide-by-zero-error-in-sql

I can see more than one way to handle this. My preference would be to implement filterGreaterThan because dividedBy can always be re-written as a multiply, which is also faster:

PersonFinder.age().filterGreaterThan(PersonFinder.daysLived().times(365.0))

filter methods also have a wider use case applicability than division. Multiply, however, may run into overflow issues (highly unlikely with a floating point calculation).

Alternatively, another method like dividedByIgnoreZero or dividedByNullIfZero could be added.

I like filterGreaterThan() because it also solves the problem which is what I was essentially trying to by-pass. Currently, I cannot do greaterThan() as combination of attribute and computation.
For Example: when we need to do percentage computations like daysLived > 0.3*age.

It will also provide functionality to do PersonFinder.age().greaterThan(PersonFinder.daysLived())

Is it possible to get this enhancement?

Not likely for the next release (17.1.0, next week). Are you working on this?