Skip to content

Add support for exponentiation (__pow__)#2733

Merged
Providence-o merged 10 commits intomainfrom
providence/support-exponentiation
Apr 8, 2026
Merged

Add support for exponentiation (__pow__)#2733
Providence-o merged 10 commits intomainfrom
providence/support-exponentiation

Conversation

@Providence-o
Copy link
Copy Markdown
Contributor

@Providence-o Providence-o commented Apr 1, 2026

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.

@evansd evansd changed the title Add support for exponentiation (__pow__)in ehrQL Add support for exponentiation (__pow__) Apr 3, 2026
Copy link
Copy Markdown
Contributor

@evansd evansd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

# We're deliberately not including the reverse operators here (e.g. `__radd__`) because
# I don't think it adds anything but confusion to have them in the docs
OPERATORS = {
"__eq__": "==",
"__ne__": "!=",
"__invert__": "~",
"__and__": "&",
"__or__": "|",
"__lt__": ">",
"__le__": ">=",
"__gt__": "<",
"__ge__": "<=",
"__neg__": "-",
"__add__": "+",
"__sub__": "-",
"__mul__": "*",
"__truediv__": "/",
"__floordiv__": "//",
}

And then add a docstring to the method itself, like we do for e.g. __truediv__:

Return a series with each value in this series divided by its correponding value
in `other` (or NULL if either is NULL).
Note that the result is always if a float even if the inputs are integers.

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.

Comment thread ehrql/utils/math_utils.py Outdated
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.
@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying databuilder-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: e32bd84
Status: ✅  Deploy successful!
Preview URL: https://9c9af024.databuilder.pages.dev
Branch Preview URL: https://providence-support-exponenti.databuilder.pages.dev

View logs

@Providence-o Providence-o merged commit 24ba869 into main Apr 8, 2026
10 checks passed
@Providence-o Providence-o deleted the providence/support-exponentiation branch April 8, 2026 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support exponentiation in ehrQL

2 participants