|
32 | 32 | public final class IgnoreNullMetrics extends Rule<LogicalPlan, LogicalPlan> { |
33 | 33 | @Override |
34 | 34 | 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; |
48 | 44 | } |
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 | + } |
67 | 54 | } |
| 55 | + Expression finalConditional = conditional; |
| 56 | + return agg.transformUp(p -> isMetricsQuery((LogicalPlan) p), p -> new Filter(p.source(), p, finalConditional)); |
68 | 57 | }); |
69 | | - return metrics; |
70 | 58 | } |
71 | 59 |
|
72 | 60 | /** |
|
0 commit comments