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

MQE: add support for unary addition #9681

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* `cortex_alertmanager_alerts`
* `cortex_alertmanager_silences`
* [CHANGE] Distributor: Drop experimental `-distributor.direct-otlp-translation-enabled` flag, since direct OTLP translation is well tested at this point. #9647
* [FEATURE] Querier: add experimental streaming PromQL engine, enabled with `-querier.query-engine=mimir`. #9367 #9368 #9398 #9399 #9403 #9417 #9418 #9419 #9420 #9482 #9504 #9505 #9507 #9518 #9531 #9532 #9533 #9553 #9558 #9588 #9589 #9639 #9641 #9642 #9664
* [FEATURE] Querier: add experimental streaming PromQL engine, enabled with `-querier.query-engine=mimir`. #9367 #9368 #9398 #9399 #9403 #9417 #9418 #9419 #9420 #9482 #9504 #9505 #9507 #9518 #9531 #9532 #9533 #9553 #9558 #9588 #9589 #9639 #9641 #9642 #9664 #9681
* [FEATURE] Query-frontend: added experimental configuration options `query-frontend.cache-errors` and `query-frontend.results-cache-ttl-for-errors` to allow non-transient responses to be cached. When set to `true` error responses from hitting limits or bad data are cached for a short TTL. #9028
* [FEATURE] gRPC: Support S2 compression. #9322
* `-alertmanager.alertmanager-client.grpc-compression=s2`
Expand Down
10 changes: 10 additions & 0 deletions pkg/streamingpromql/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ func (q *Query) convertToInstantVectorOperator(expr parser.Expr, timeRange types
}

case *parser.UnaryExpr:
if e.Op == parser.ADD {
// Unary addition (+value): there's nothing to do, just return the inner expression.
return q.convertToInstantVectorOperator(e.Expr, timeRange)
}

if e.Op != parser.SUB {
return nil, compat.NewNotSupportedError(fmt.Sprintf("unary expression with '%s'", e.Op))
}
Expand Down Expand Up @@ -421,6 +426,11 @@ func (q *Query) convertToScalarOperator(expr parser.Expr, timeRange types.QueryT
return q.convertFunctionCallToScalarOperator(e, timeRange)

case *parser.UnaryExpr:
if e.Op == parser.ADD {
// Unary addition (+value): there's nothing to do, just return the inner expression.
return q.convertToScalarOperator(e.Expr, timeRange)
}

if e.Op != parser.SUB {
return nil, compat.NewNotSupportedError(fmt.Sprintf("unary expression with '%s'", e.Op))
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/streamingpromql/testdata/ours/functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ eval instant at 54m scalar(multiple_series)

clear

# Unary negation (-) and unary addition (+)
load 6m
metric{series="float"} 1 2 3 4 -5 0 NaN -NaN
metric{series="histogram"} {{schema:3 sum:4 count:23 buckets:[1 2 4] n_buckets:[3 5 8]}} {{schema:3 sum:14 count:27 buckets:[1 2 6] n_buckets:[3 5 10]}}
Expand All @@ -226,6 +227,11 @@ eval range from 0 to 42m step 6m -metric
eval range from 0 to 1m step 1m -metric{series="histogram"}
{series="histogram"} {{schema:3 sum:-4 count:-23 buckets:[-1 -2 -4] n_buckets:[-3 -5 -8]}} {{schema:3 sum:-4 count:-23 buckets:[-1 -2 -4] n_buckets:[-3 -5 -8]}}

# Unary addition should do nothing.
eval range from 0 to 42m step 6m +metric
metric{series="float"} 1 2 3 4 -5 0 NaN -NaN
metric{series="histogram"} {{schema:3 sum:4 count:23 buckets:[1 2 4] n_buckets:[3 5 8]}} {{schema:3 sum:14 count:27 buckets:[1 2 6] n_buckets:[3 5 10]}}

clear

load 1m
Expand Down
17 changes: 17 additions & 0 deletions pkg/streamingpromql/testdata/ours/literals.test
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@ eval range from 0m to 2m step 1m -(-Inf)

eval range from 0m to 2m step 1m -(NaN)
{} NaN NaN NaN

# Same thing as above, with with unary addition.
# Unary addition is a no-op.
eval range from 0m to 2m step 1m +(2)
{} 2 2 2

eval range from 0m to 2m step 1m +(-2)
{} -2 -2 -2

eval range from 0m to 2m step 1m +(Inf)
{} Inf Inf Inf

eval range from 0m to 2m step 1m +(-Inf)
{} -Inf -Inf -Inf

eval range from 0m to 2m step 1m +(NaN)
{} NaN NaN NaN
Loading