-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Bug(LogQL): Fix mismatch results on scalar
and vector
binary ops
#10997
Conversation
Repect the Promql following behaviour of Binary operations with vector vs scalar 1. `scalar` op `vector` - vector 2. `vector` op `scalar` - vector Signed-off-by: Kaviraj <[email protected]>
Signed-off-by: Kaviraj <[email protected]>
Signed-off-by: Kaviraj <[email protected]>
Signed-off-by: Kaviraj <[email protected]>
scalar
and vector
binary opsscalar
and vector
binary ops
Signed-off-by: Kaviraj <[email protected]>
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.
LGTM
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.
Great start, thanks!
pkg/logql/syntax/ast.go
Outdated
// This matters because, either it's (vector op scalar) or (scalar op vector), the return sample value should | ||
// always be sample value of vector argument. | ||
// https://github.com/grafana/loki/issues/10741 | ||
func MergeBinOp(op string, left, right *promql.Sample, swap, notReturnBool, isVectorComparison bool) (*promql.Sample, error) { |
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.
I think it's more confusing to use notReturnBool
compared to filter
.
pkg/logql/syntax/ast.go
Outdated
// if a filter-enabled vector-wise comparison has returned non-nil, | ||
// ensure we return the left hand side's value (2) instead of the | ||
// comparison operator's result (1: the truthy answer) | ||
if notReturnBool { |
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.
The bool
flag doesn't have anything to do with whether we should perform swap
. For an example, try 1 < bool $METRIC
in prometheus -- it'll return a vector of values with 0
or 1
.
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.
I think it's been there because, swapping has no impact when return bool
(vector of 0 or 1)
Means, both
1 < bool $METRIC
and $METRIC > bool 1
will return exact same vector of values (0 or 1) without needed to swap. In other words, whether to return left
or right
sample is irrelevant in case of returning bool. Am I missing anything here?
Also confirmed by moving the swap out of filter
check. Added extra test to lock the behaviour.
1. revert `notReturnBool` -> `filter` 2. swap independent of `filter` flag Signed-off-by: Kaviraj <[email protected]>
Signed-off-by: Kaviraj <[email protected]>
Changlog to include this LogQL binary ops bug fix. #10997 Signed-off-by: Kaviraj <[email protected]>
…rafana#10997) **What this PR does / why we need it**: [According to LogQL doc,](https://grafana.com/docs/loki/latest/query/#comparison-operators) binary operators between `scalar` and `vector` should behave as follows (either it's `scalar` op `vector` or `vector` op `scalar`) ``` Between a vector and a scalar, these operators are applied to the value of every data sample in the vector ``` PromQL (where LogQL inspired from) [also works that way](https://prometheus.io/docs/prometheus/latest/querying/operators/). Currently LogQL violates this behaviour. It returns whatever on the `left` (even if it's scalar). Look into the attached issue for more details. Changes: 1. Fixed `MergeBinOps` by making it aware when to return left or right (depending on which is from vector originally) 2. Add tests in both `engine_test.go` and `evaluator_test.go` to lock this behaviour 3. Added appropriate comments to the types and functions. **Which issue(s) this PR fixes**: Fixes grafana#10741 **Special notes for your reviewer**: **Checklist** - [x] Reviewed the [`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md) guide (**required**) - [x] Tests updated - [ ] `CHANGELOG.md` updated - [ ] If the change is worth mentioning in the release notes, add `add-to-release-notes` label - [ ] Changes that require user attention or interaction to upgrade are documented in `docs/sources/setup/upgrade/_index.md` - [ ] For Helm chart changes bump the Helm chart version in `production/helm/loki/Chart.yaml` and update `production/helm/loki/CHANGELOG.md` and `production/helm/loki/README.md`. [Example PR](grafana@d10549e) - [ ] If the change is deprecating or removing a configuration option, update the `deprecated-config.yaml` and `deleted-config.yaml` files respectively in the `tools/deprecated-config-checker` directory. <!-- TODO(salvacorts): Add example PR --> --------- Signed-off-by: Kaviraj <[email protected]>
What this PR does / why we need it:
According to LogQL doc, binary operators between
scalar
andvector
should behave as follows (either it'sscalar
opvector
orvector
opscalar
)PromQL (where LogQL inspired from) also works that way.
Currently LogQL violates this behaviour. It returns whatever on the
left
(even if it's scalar). Look into the attached issue for more details.Changes:
MergeBinOps
by making it aware when to return left or right (depending on which is from vector originally)engine_test.go
andevaluator_test.go
to lock this behaviourWhich issue(s) this PR fixes:
Fixes #10741
Special notes for your reviewer:
Checklist
CONTRIBUTING.md
guide (required)CHANGELOG.md
updatedadd-to-release-notes
labeldocs/sources/setup/upgrade/_index.md
production/helm/loki/Chart.yaml
and updateproduction/helm/loki/CHANGELOG.md
andproduction/helm/loki/README.md
. Example PRdeprecated-config.yaml
anddeleted-config.yaml
files respectively in thetools/deprecated-config-checker
directory.