Skip to content
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

Merged
merged 4 commits into from
Mar 12, 2024

Conversation

iambriccardo
Copy link
Member

@iambriccardo iambriccardo commented Mar 7, 2024

This PR adds support for unary operator - on numeric scalars and timeseries/formulas, which was requested by some users.

@iambriccardo iambriccardo requested a review from evanh March 7, 2024 09:21
@iambriccardo iambriccardo marked this pull request as ready for review March 7, 2024 09:21
@iambriccardo iambriccardo requested a review from a team as a code owner March 7, 2024 09:21
unary_op, coefficient = children
if unary_op:
if isinstance(coefficient, float) or isinstance(coefficient, int):
return -coefficient
Copy link
Member Author

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.

Copy link
Member

@evanh evanh left a 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

@@ -109,6 +113,10 @@ class InvalidMQLQueryError(Exception):
"/": ArithmeticOperator.DIVIDE.value,
}

UNARY_OPERATORS: Mapping[str, str] = {
"-": ArithmeticOperator.MINUS.value,
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Perfect!

elif isinstance(coefficient, Formula) or isinstance(
coefficient, Timeseries
):
return Formula(function_name=unary_op[0], parameters=[0, coefficient])
Copy link
Member

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().

Copy link
Member Author

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!

@iambriccardo iambriccardo requested a review from evanh March 7, 2024 19:12
@iambriccardo iambriccardo merged commit b91ca0d into main Mar 12, 2024
9 checks passed
@iambriccardo iambriccardo deleted the riccardo/feat/implement-unary branch March 12, 2024 07:47
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.

2 participants