Add support for exponentiation (__pow__)#2733
Conversation
__pow__)in ehrQL__pow__)
evansd
left a comment
There was a problem hiding this comment.
This is great work, thanks Providence. When I first wrote this ticket I hadn't thought very hard about what all the edge cases would be, and how much extra work it would take to handle all of those. You've done exactly the right thing here though.
There are just a couple of small changes needed before merging. One is just to add a feat: prefix to one of the commits so we get an appropriate new release.
The other is to add the __pow__ method to the documentation. I'd completely forgotten that while this happens automatically for "normal" methods it doesn't for "magic" methods, which I why I didn't include it in the original ticket.
You'll need to add a line to the list of operators below:
Lines 28 to 46 in 95733bb
And then add a docstring to the method itself, like we do for e.g. __truediv__:
Lines 854 to 857 in 95733bb
Make sure to mention that we return NULL for any operations which might result in a complex value.
You'll need to regenerate and commit the docs changes, like you've already done with the spec tests.
The power operations should always return a float as it ensures correct behaviour in all cases. Example: For integers, negative exponents will produce float data types (2 ** -1 = 0.5)
This covers the limitations of exponentiation in python and defines cases where we expect `None` to be returned
MSSQL does not support the modulo operator on float data types. A workaround is needed to check whether the exponent is a whole number. Instead of `rhs % 1 != 0` used in the in-memory and base SQL query engines, the exponent (rhs) is compared with its floor divided equivalent. If they differ, the value is a float data type and the edge case will be handled by the MSSQL query engine.
Instead of pre-checking argument values, we now perform the calculation directly and handle the results. This captures the in-memory implementation better and also makes a clearer distinction from the SQL implementation, which helps to make sure the Hypothesis tests are more robust.
c1a7317 to
e32bd84
Compare
Deploying databuilder-docs with
|
| Latest commit: |
e32bd84
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://9c9af024.databuilder.pages.dev |
| Branch Preview URL: | https://providence-support-exponenti.databuilder.pages.dev |
Fixes #2688
This adds support for exponentiating a numeric series.
We want to be able to support users who require exponentiation in their analysis code. Like in this use case.
This support only covers instances where there are two arguments and where a real number will be produced from the operation.