Skip to content

Commit 2f49f80

Browse files
authored
Fix subquery over avg (#499)
* Fix subquery over avg The refactor in #477 introduces a bug where subquery on top of avg produces wrong results. This is because the parents are calculated before the query is rewritten, so the new nodes end up with no parents. Signed-off-by: Filip Petkovski <[email protected]> * Fix lint Signed-off-by: Filip Petkovski <[email protected]> --------- Signed-off-by: Filip Petkovski <[email protected]>
1 parent 097e6e9 commit 2f49f80

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

engine/distributed_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ func TestDistributedAggregations(t *testing.T) {
232232
{name: "absent for existing metric with aggregation", query: `sum(absent(foo))`},
233233
{name: "absent for existing metric", query: `absent(bar{pod="nginx-1"})`},
234234
{name: "absent for existing metric with aggregation", query: `sum(absent(bar{pod="nginx-1"}))`},
235+
{name: "subquery with sum/count", query: `max_over_time((sum(bar) / count(bar))[30s:15s])`},
236+
{name: "subquery with avg", query: `max_over_time(avg(bar)[30s:15s])`},
235237
{name: "subquery with window within engine range", query: `max_over_time(sum_over_time(bar[30s])[30s:15s])`},
236238
{name: "subquery with window outside of engine range", query: `max_over_time(sum_over_time(bar[1m])[10m:1m])`},
237239
{name: "subquery with misaligned ranges", rangeStart: time.Unix(7, 0), query: `max_over_time(sum(bar)[30s:15s])`},

logicalplan/distribute.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,6 @@ func (m DistributedExecutionOptimizer) Optimize(plan Node, opts *query.Options)
178178
}
179179
minEngineOverlap := labelRanges.minOverlap()
180180

181-
// TODO(fpetkovski): Consider changing TraverseBottomUp to pass in a list of parents in the transform function.
182-
parents := make(map[*Node]*Node)
183-
TraverseBottomUp(nil, &plan, func(parent, current *Node) (stop bool) {
184-
parents[current] = parent
185-
return false
186-
})
187-
188181
// Preprocess rewrite distributable averages as sum/count
189182
var warns = annotations.New()
190183
TraverseBottomUp(nil, &plan, func(parent, current *Node) (stop bool) {
@@ -217,6 +210,12 @@ func (m DistributedExecutionOptimizer) Optimize(plan Node, opts *query.Options)
217210
return !(isDistributive(parent, m.SkipBinaryPushdown, engineLabels, warns) || isAvgAggregation(parent))
218211
})
219212

213+
// TODO(fpetkovski): Consider changing TraverseBottomUp to pass in a list of parents in the transform function.
214+
parents := make(map[*Node]*Node)
215+
TraverseBottomUp(nil, &plan, func(parent, current *Node) (stop bool) {
216+
parents[current] = parent
217+
return false
218+
})
220219
TraverseBottomUp(nil, &plan, func(parent, current *Node) (stop bool) {
221220
// If the current operation is not distributive, stop the traversal.
222221
if !isDistributive(current, m.SkipBinaryPushdown, engineLabels, warns) {

0 commit comments

Comments
 (0)