-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Esql skip null metrics #133087
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
Merged
not-napoleon
merged 46 commits into
elastic:main
from
not-napoleon:esql-skip-null-metrics
Sep 8, 2025
Merged
Esql skip null metrics #133087
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
44eabdc
initial outline of rule
not-napoleon cb9ebe7
add ability to know if an attribute is a metric
not-napoleon 201014a
actually collect the metrics
not-napoleon 989d6ce
simplify the lambdas. I think this is more correct
not-napoleon cbfbbbb
add the rule to the analisys chain
not-napoleon 7b620c4
[CI] Auto commit changes from spotless
abf4d35
start of tests
not-napoleon 9c9047f
Merge remote-tracking branch 'refs/remotes/not-napoleon/esql-skip-nul…
not-napoleon 641fec9
[CI] Auto commit changes from spotless
7edbf66
remove redundant check
not-napoleon a9fa160
Merge remote-tracking branch 'refs/remotes/not-napoleon/esql-skip-nul…
not-napoleon 0932461
test passes!
not-napoleon 0451b10
more tests
not-napoleon 540bbdc
more tests
not-napoleon a222e27
test non-ts query
not-napoleon 96fef3f
move the rule to the logical plan optimizer
not-napoleon 7d773cb
move the rule to the correct package
not-napoleon 3cb8bf8
Merge branch 'main' into esql-skip-null-metrics
not-napoleon 2bf80cd
[CI] Auto commit changes from spotless
f255066
Merge branch 'main' into esql-skip-null-metrics
not-napoleon 9eb5544
Merge branch 'main' into esql-skip-null-metrics
not-napoleon 190a420
Merge branch 'main' into esql-skip-null-metrics
not-napoleon 8b77bd8
Merge branch 'main' into esql-skip-null-metrics
not-napoleon 4e0a8a0
Update docs/changelog/133087.yaml
not-napoleon 4ea5f18
Fix changelog area
not-napoleon 96161bd
Update docs/changelog/133087.yaml
not-napoleon a1b0298
Merge branch 'main' into esql-skip-null-metrics
not-napoleon e57204c
Merge branch 'main' into esql-skip-null-metrics
not-napoleon 5e64278
Merge branch 'main' into esql-skip-null-metrics
not-napoleon 62bbf94
Merge branch 'main' into esql-skip-null-metrics
not-napoleon 2f9093c
response to PR feedback
not-napoleon 484b171
Merge remote-tracking branch 'refs/remotes/not-napoleon/esql-skip-nul…
not-napoleon 7f3f934
response to PR feedback
not-napoleon a7e9dd8
[CI] Auto commit changes from spotless
fc00a2e
do everything in one transformation pass
not-napoleon dffc121
Merge branch 'main' into esql-skip-null-metrics
not-napoleon db91bc8
Put the filter immediately below (before) the aggregation
not-napoleon db14fe4
use the correct source in the conditional
not-napoleon 9db9af5
make IgnoreNullMetrics a local rule
not-napoleon 79f3202
move the rule to the correct package
not-napoleon 7462fd8
refactor to use OptimizerRules.OptimizerRule
not-napoleon 4eb33cb
[CI] Auto commit changes from spotless
a1b5454
skip InfernonNullAggConstraint for TS aggs
not-napoleon 48e7dbe
Merge remote-tracking branch 'refs/remotes/not-napoleon/esql-skip-nul…
not-napoleon 108d7d8
Merge branch 'main' into esql-skip-null-metrics
not-napoleon 3b08b8d
Merge branch 'main' into esql-skip-null-metrics
not-napoleon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pr: 133087 | ||
summary: Esql skip null metrics | ||
area: ES|QL | ||
type: enhancement | ||
issues: | ||
- 129524 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...in/java/org/elasticsearch/xpack/esql/optimizer/rules/logical/local/IgnoreNullMetrics.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.esql.optimizer.rules.logical.local; | ||
|
||
import org.elasticsearch.index.IndexMode; | ||
import org.elasticsearch.xpack.esql.core.expression.Attribute; | ||
import org.elasticsearch.xpack.esql.core.expression.Expression; | ||
import org.elasticsearch.xpack.esql.expression.predicate.logical.Or; | ||
import org.elasticsearch.xpack.esql.expression.predicate.nulls.IsNotNull; | ||
import org.elasticsearch.xpack.esql.optimizer.rules.logical.OptimizerRules; | ||
import org.elasticsearch.xpack.esql.plan.logical.EsRelation; | ||
import org.elasticsearch.xpack.esql.plan.logical.Filter; | ||
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; | ||
import org.elasticsearch.xpack.esql.plan.logical.TimeSeriesAggregate; | ||
import org.elasticsearch.xpack.esql.plan.logical.UnresolvedRelation; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* <p><strong>NOTE: THIS RULE IS INTENDED TO BE RUN ONLY ONCE. DO NOT INCLUDE IT IN MULTI-RUN BATCHES.</strong> The expectation is that | ||
* this will be run as part of the {@link org.elasticsearch.xpack.esql.optimizer.LocalLogicalPlanOptimizer} in the local rewrite batch</p> | ||
* <p> | ||
* TSDB often ends up storing many null values for metrics columns (since not every time series contains every metric). However, loading | ||
* many null values can negatively impact query performance. To reduce that, this rule applies filters to remove null values on all | ||
* metrics involved in the query. In the case that there are multiple metrics, the not null checks are OR'd together, so we accept rows | ||
* where any of the metrics have values. | ||
* </p> | ||
*/ | ||
public final class IgnoreNullMetrics extends OptimizerRules.OptimizerRule<TimeSeriesAggregate> { | ||
@Override | ||
public LogicalPlan rule(TimeSeriesAggregate agg) { | ||
Set<Attribute> metrics = new HashSet<>(); | ||
agg.forEachExpression(Attribute.class, attr -> { | ||
if (attr.isMetric()) { | ||
metrics.add(attr); | ||
} | ||
}); | ||
if (metrics.isEmpty()) { | ||
return agg; | ||
} | ||
Expression conditional = null; | ||
for (Attribute metric : metrics) { | ||
// Create an is not null check for each metric | ||
if (conditional == null) { | ||
conditional = new IsNotNull(agg.source(), metric); | ||
} else { | ||
// Join the is not null checks with OR nodes | ||
conditional = new Or(agg.source(), conditional, new IsNotNull(agg.source(), metric)); | ||
} | ||
} | ||
return agg.replaceChild(new Filter(agg.source(), agg.child(), conditional)); | ||
} | ||
|
||
/** | ||
* Scans the given {@link LogicalPlan} to see if it is a "metrics mode" query | ||
*/ | ||
private static boolean isMetricsQuery(LogicalPlan logicalPlan) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think it's unused. |
||
if (logicalPlan instanceof EsRelation r) { | ||
return r.indexMode() == IndexMode.TIME_SERIES; | ||
} | ||
if (logicalPlan instanceof UnresolvedRelation r) { | ||
return r.indexMode() == IndexMode.TIME_SERIES; | ||
} | ||
return false; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.