Skip to content

Commit fc00a2e

Browse files
committed
do everything in one transformation pass
1 parent a7e9dd8 commit fc00a2e

File tree

1 file changed

+20
-32
lines changed

1 file changed

+20
-32
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/logical/IgnoreNullMetrics.java

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,41 +32,29 @@
3232
public final class IgnoreNullMetrics extends Rule<LogicalPlan, LogicalPlan> {
3333
@Override
3434
public LogicalPlan apply(LogicalPlan logicalPlan) {
35-
Set<Attribute> metrics = collectMetrics(logicalPlan);
36-
if (metrics.isEmpty()) {
37-
return logicalPlan;
38-
}
39-
40-
Expression conditional = null;
41-
for (Attribute metric : metrics) {
42-
// Create an is not null check for each metric
43-
if (conditional == null) {
44-
conditional = new IsNotNull(logicalPlan.source(), metric);
45-
} else {
46-
// Join the is not null checks with OR nodes
47-
conditional = new Or(logicalPlan.source(), conditional, new IsNotNull(Source.EMPTY, metric));
35+
return logicalPlan.transformUp(TimeSeriesAggregate.class, agg -> {
36+
Set<Attribute> metrics = new HashSet<>();
37+
agg.forEachExpression(Attribute.class, attr -> {
38+
if (attr.isMetric()) {
39+
metrics.add(attr);
40+
}
41+
});
42+
if (metrics.isEmpty()) {
43+
return agg;
4844
}
49-
}
50-
51-
Expression finalConditional = conditional;
52-
// Find where to put the new Filter node; We want it right after the relation leaf node
53-
// We want to add the filter right after the TS command, which is a "leaf" node.
54-
// So we want to find the first node above the leaf node.
55-
return logicalPlan.transformUp(p -> isMetricsQuery((LogicalPlan) p), p -> new Filter(p.source(), p, finalConditional));
56-
}
57-
58-
private Set<Attribute> collectMetrics(LogicalPlan logicalPlan) {
59-
Set<Attribute> metrics = new HashSet<>();
60-
logicalPlan.forEachDown(p -> {
61-
if (p instanceof TimeSeriesAggregate) {
62-
p.forEachExpression(Attribute.class, attr -> {
63-
if (attr.isMetric()) {
64-
metrics.add(attr);
65-
}
66-
});
45+
Expression conditional = null;
46+
for (Attribute metric : metrics) {
47+
// Create an is not null check for each metric
48+
if (conditional == null) {
49+
conditional = new IsNotNull(logicalPlan.source(), metric);
50+
} else {
51+
// Join the is not null checks with OR nodes
52+
conditional = new Or(logicalPlan.source(), conditional, new IsNotNull(Source.EMPTY, metric));
53+
}
6754
}
55+
Expression finalConditional = conditional;
56+
return agg.transformUp(p -> isMetricsQuery((LogicalPlan) p), p -> new Filter(p.source(), p, finalConditional));
6857
});
69-
return metrics;
7058
}
7159

7260
/**

0 commit comments

Comments
 (0)