Skip to content

Commit 20e04a4

Browse files
authored
chore(engine/logical): push SORT operation towards bottom of plan (#19239)
1 parent 8499840 commit 20e04a4

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

pkg/engine/planner/logical/planner.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,6 @@ func buildPlanForLogQuery(
132132
return nil, fmt.Errorf("forward search log queries are not supported: %w", errUnimplemented)
133133
}
134134

135-
if !isMetricQuery {
136-
// SORT -> SortMerge
137-
// We always sort DESC. ASC timestamp sorting is not supported for logs
138-
// queries, and metric queries do not need sorting.
139-
builder = builder.Sort(*timestampColumnRef(), false, false)
140-
}
141-
142135
// SELECT -> Filter
143136
start := params.Start()
144137
end := params.End()
@@ -159,6 +152,11 @@ func buildPlanForLogQuery(
159152

160153
// Metric queries do not apply a limit.
161154
if !isMetricQuery {
155+
// SORT -> SortMerge
156+
// We always sort DESC. ASC timestamp sorting is not supported for logs
157+
// queries, and metric queries do not need sorting.
158+
builder = builder.Sort(*timestampColumnRef(), false, false)
159+
162160
// LIMIT -> Limit
163161
limit := params.Limit()
164162
builder = builder.Limit(0, limit)

pkg/engine/planner/logical/planner_test.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ func TestConvertAST_Success(t *testing.T) {
102102
%10 = NOT_MATCH_RE builtin.message "(a|b|c)"
103103
%11 = AND %9 %10
104104
%12 = MAKETABLE [selector=%3, predicates=[%6, %11], shard=0_of_1]
105-
%13 = SORT %12 [column=builtin.timestamp, asc=false, nulls_first=false]
106-
%14 = GTE builtin.timestamp 1970-01-01T01:00:00Z
107-
%15 = SELECT %13 [predicate=%14]
108-
%16 = LT builtin.timestamp 1970-01-01T02:00:00Z
109-
%17 = SELECT %15 [predicate=%16]
110-
%18 = SELECT %17 [predicate=%6]
111-
%19 = SELECT %18 [predicate=%11]
105+
%13 = GTE builtin.timestamp 1970-01-01T01:00:00Z
106+
%14 = SELECT %12 [predicate=%13]
107+
%15 = LT builtin.timestamp 1970-01-01T02:00:00Z
108+
%16 = SELECT %14 [predicate=%15]
109+
%17 = SELECT %16 [predicate=%6]
110+
%18 = SELECT %17 [predicate=%11]
111+
%19 = SORT %18 [column=builtin.timestamp, asc=false, nulls_first=false]
112112
%20 = LIMIT %19 [skip=0, fetch=1000]
113113
RETURN %20
114114
`
@@ -309,14 +309,14 @@ RETURN %11
309309
// Assert against the SSA representation for log query
310310
expected := `%1 = EQ label.app "test"
311311
%2 = MAKETABLE [selector=%1, predicates=[], shard=0_of_1]
312-
%3 = SORT %2 [column=builtin.timestamp, asc=false, nulls_first=false]
313-
%4 = GTE builtin.timestamp 1970-01-01T01:00:00Z
314-
%5 = SELECT %3 [predicate=%4]
315-
%6 = LT builtin.timestamp 1970-01-01T02:00:00Z
316-
%7 = SELECT %5 [predicate=%6]
317-
%8 = PARSE %7 [kind=logfmt]
318-
%9 = EQ ambiguous.level "error"
319-
%10 = SELECT %8 [predicate=%9]
312+
%3 = GTE builtin.timestamp 1970-01-01T01:00:00Z
313+
%4 = SELECT %2 [predicate=%3]
314+
%5 = LT builtin.timestamp 1970-01-01T02:00:00Z
315+
%6 = SELECT %4 [predicate=%5]
316+
%7 = PARSE %6 [kind=logfmt]
317+
%8 = EQ ambiguous.level "error"
318+
%9 = SELECT %7 [predicate=%8]
319+
%10 = SORT %9 [column=builtin.timestamp, asc=false, nulls_first=false]
320320
%11 = LIMIT %10 [skip=0, fetch=1000]
321321
RETURN %11
322322
`
@@ -345,16 +345,16 @@ RETURN %11
345345
%2 = MATCH_STR builtin.message "error"
346346
%3 = EQ ambiguous.label "value"
347347
%4 = MAKETABLE [selector=%1, predicates=[%2, %3], shard=0_of_1]
348-
%5 = SORT %4 [column=builtin.timestamp, asc=false, nulls_first=false]
349-
%6 = GTE builtin.timestamp 1970-01-01T01:00:00Z
350-
%7 = SELECT %5 [predicate=%6]
351-
%8 = LT builtin.timestamp 1970-01-01T02:00:00Z
352-
%9 = SELECT %7 [predicate=%8]
353-
%10 = SELECT %9 [predicate=%2]
354-
%11 = SELECT %10 [predicate=%3]
355-
%12 = PARSE %11 [kind=logfmt]
356-
%13 = EQ ambiguous.level "debug"
357-
%14 = SELECT %12 [predicate=%13]
348+
%5 = GTE builtin.timestamp 1970-01-01T01:00:00Z
349+
%6 = SELECT %4 [predicate=%5]
350+
%7 = LT builtin.timestamp 1970-01-01T02:00:00Z
351+
%8 = SELECT %6 [predicate=%7]
352+
%9 = SELECT %8 [predicate=%2]
353+
%10 = SELECT %9 [predicate=%3]
354+
%11 = PARSE %10 [kind=logfmt]
355+
%12 = EQ ambiguous.level "debug"
356+
%13 = SELECT %11 [predicate=%12]
357+
%14 = SORT %13 [column=builtin.timestamp, asc=false, nulls_first=false]
358358
%15 = LIMIT %14 [skip=0, fetch=1000]
359359
RETURN %15
360360
`

0 commit comments

Comments
 (0)