-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(mql): Add support for unary operator #179
Conversation
unary_op, coefficient = children | ||
if unary_op: | ||
if isinstance(coefficient, float) or isinstance(coefficient, int): | ||
return -coefficient |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can more efficiently negate directly the scalar instead of using a formula.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some changes necessary here I think.
Also a couple test cases I would add:
x - -y
Two minus signs next to each other-sum(mri)
Should output a Formula instead of a Timeseries
snuba_sdk/mql/mql.py
Outdated
@@ -109,6 +113,10 @@ class InvalidMQLQueryError(Exception): | |||
"/": ArithmeticOperator.DIVIDE.value, | |||
} | |||
|
|||
UNARY_OPERATORS: Mapping[str, str] = { | |||
"-": ArithmeticOperator.MINUS.value, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clickhouse uses the negate
function for this, Clickhouse expects the minus
function to have 2 arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect!
snuba_sdk/mql/mql.py
Outdated
elif isinstance(coefficient, Formula) or isinstance( | ||
coefficient, Timeseries | ||
): | ||
return Formula(function_name=unary_op[0], parameters=[0, coefficient]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this have two parameters? Is that to support minus()
? If so, we should use negate()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I didn't know there was that function, will use that!
This PR adds support for unary operator
-
on numeric scalars and timeseries/formulas, which was requested by some users.