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

Mimir query engine: binary operations between two scalars #9277

Merged
merged 4 commits into from
Sep 12, 2024

Conversation

charleskorn
Copy link
Contributor

@charleskorn charleskorn commented Sep 12, 2024

What this PR does

This PR adds support for binary operations between two scalars in MQE.

For example, expressions like 2 + 3 and scalar(metric) * 2 are now supported.

Which issue(s) this PR fixes or relates to

(none)

Checklist

  • Tests updated.
  • [n/a] Documentation added.
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX].
  • [n/a] about-versioning.md updated with experimental features.

@charleskorn charleskorn marked this pull request as ready for review September 12, 2024 03:33
@charleskorn charleskorn requested a review from a team as a code owner September 12, 2024 03:33
Copy link
Contributor

@jhesketh jhesketh left a comment

Choose a reason for hiding this comment

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

It would be good to see a test with scalar(series) (or am I blind)

pkg/streamingpromql/engine_test.go Show resolved Hide resolved
//
// So we can just compute the result of each pairwise operation without examining the timestamps of each sample.
//
// We store the result in the slice from the left operator, and return the right operator's slice once we're done.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think for safety we should have a check such as len(leftValues) != len(rightValues) and raise an error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We rely on this fact in other places, and don't have checks there either (eg. scalar / vector binary operations).

If the lengths are different, the code will panic, which seems like a reasonable thing to do for a bug where an invariant has been broken.

Copy link
Contributor

Choose a reason for hiding this comment

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

It only panics if the right side is shorter than the left. I don't think the overhead here is very high, what do you think about it adding it to the other scalar operators too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure this makes sense as a precedent to set: we don't do similar validation-type checks in other situations (eg. checking that the data points from an instant vector operator are within the query time range, or align to the requested step), and if those situations were to occur, we'd have similarly undefined behaviour.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, okay. I still think it's reasonable, but I don't hold it strongly. LGTM :-)

@charleskorn
Copy link
Contributor Author

It would be good to see a test with scalar(series) (or am I blind)

There were existing tests that exercised scalar(), but I've added one specifically that uses scalar(metric) + 2 to test it with binary operations between two scalars.

//
// So we can just compute the result of each pairwise operation without examining the timestamps of each sample.
//
// We store the result in the slice from the left operator, and return the right operator's slice once we're done.
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, okay. I still think it's reasonable, but I don't hold it strongly. LGTM :-)

@charleskorn charleskorn merged commit 4627dd9 into main Sep 12, 2024
29 checks passed
@charleskorn charleskorn deleted the charleskorn/mqe-scalar-scalar-binops branch September 12, 2024 05:59
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