Skip to content

Commit 333bbc9

Browse files
committed
lib/logstorage: properly apply time_offset option to subqueries.
Previously the time_offset option from the top query was mistakenly applied to all the subqueries. This is a follow-up for 8b22bc6 Updates #78 Updates #414
1 parent 8b22bc6 commit 333bbc9

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

lib/logstorage/parser.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -592,42 +592,40 @@ func (q *Query) GetFilterTimeRange() (int64, int64) {
592592

593593
// AddTimeFilter adds global filter _time:[start ... end] to q.
594594
func (q *Query) AddTimeFilter(start, end int64) {
595-
startStr := marshalTimestampRFC3339NanoString(nil, start)
596-
endStr := marshalTimestampRFC3339NanoString(nil, end)
597-
598-
start = subNoOverflowInt64(start, q.opts.timeOffset)
599-
end = subNoOverflowInt64(end, q.opts.timeOffset)
600-
601-
end = getMatchingEndTime(end, string(endStr))
602-
ft := &filterTime{
603-
minTimestamp: start,
604-
maxTimestamp: end,
605-
stringRepr: fmt.Sprintf("[%s,%s]", startStr, endStr), // should be matched with parsing logic
606-
}
607-
608595
q.visitSubqueries(func(q *Query) {
609-
q.addTimeFilterNoSubqueries(ft)
596+
q.addTimeFilterNoSubqueries(start, end)
610597
})
611598
}
612599

613-
func (q *Query) addTimeFilterNoSubqueries(ft *filterTime) {
600+
func (q *Query) addTimeFilterNoSubqueries(start, end int64) {
614601
if q.opts.ignoreGlobalTimeFilter != nil && *q.opts.ignoreGlobalTimeFilter {
615602
return
616603
}
617604

618-
ftCopy := *ft
605+
timeOffset := q.opts.timeOffset
606+
607+
startStr := marshalTimestampRFC3339NanoString(nil, start)
608+
endStr := marshalTimestampRFC3339NanoString(nil, end)
609+
ft := &filterTime{
610+
minTimestamp: subNoOverflowInt64(start, timeOffset),
611+
maxTimestamp: getMatchingEndTime(subNoOverflowInt64(end, timeOffset), string(endStr)),
612+
stringRepr: fmt.Sprintf("[%s,%s]", startStr, endStr),
613+
}
614+
619615
fa, ok := q.f.(*filterAnd)
620616
if ok {
621617
filters := make([]filter, len(fa.filters)+1)
622-
filters[0] = &ftCopy
618+
filters[0] = ft
623619
copy(filters[1:], fa.filters)
624620
fa.filters = filters
625621
} else {
626622
q.f = &filterAnd{
627-
filters: []filter{&ftCopy, q.f},
623+
filters: []filter{ft, q.f},
628624
}
629625
}
630626

627+
q.f = flattenFiltersAnd(q.f)
628+
631629
// Remove `*` filters after adding the `_time` filter, since they are no longer needed.
632630
q.f = removeStarFilters(q.f)
633631

lib/logstorage/parser_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,12 @@ func TestQuery_AddTimeFilter(t *testing.T) {
220220

221221
// time_offset option
222222
f(`options(time_offset=1d3h534ms) *`, `options(time_offset=1d3h534ms) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z]`)
223-
f(`options(time_offset = -1.5h) _time:1.3d`, `options(time_offset=-1.5h) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] _time:1.3d`)
224-
f(`options(time_offset = -1.5h) _time:1.3d offset 3.5h`, `options(time_offset=-1.5h) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] _time:1.3d offset 3.5h`)
225-
f(`options(time_offset = -1.5h) _time:1.3d offset -3.5h`, `options(time_offset=-1.5h) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] _time:1.3d offset -3.5h`)
226-
f(`options(time_offset=1h) id:in(_time:5m | keep id)`, `options(time_offset=1h) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] id:in(_time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] _time:5m | fields id)`)
227-
f(`id:in(options(time_offset=1h) _time:5m | keep id)`, `_time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] id:in(options(time_offset=1h) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] _time:5m | fields id)`)
228-
f(`options(time_offset=1d) id:in(options(time_offset=1h) _time:5m | keep id)`, `options(time_offset=1d) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] id:in(options(time_offset=1h) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] _time:5m | fields id)`)
223+
f(`options(time_offset = -1.5h) _time:2024Z`, `options(time_offset=-1.5h) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] _time:2024Z`)
224+
f(`options(time_offset = -1.5h) _time:2025Z offset 3.5h`, `options(time_offset=-1.5h) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] _time:2025Z offset 3.5h`)
225+
f(`options(time_offset = -1.5h) _time:2025Z offset -3.5h`, `options(time_offset=-1.5h) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] _time:2025Z offset -3.5h`)
226+
f(`options(time_offset=1h) id:in(_time:2025Z | keep id)`, `options(time_offset=1h) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] id:in(_time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] _time:2025Z | fields id)`)
227+
f(`id:in(options(time_offset=1h) _time:2025Z | keep id)`, `_time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] id:in(options(time_offset=1h) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] _time:2025Z | fields id)`)
228+
f(`options(time_offset=1d) id:in(options(time_offset=1h) _time:2025Z | keep id)`, `options(time_offset=1d) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] id:in(options(time_offset=1h) _time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] _time:2025Z | fields id)`)
229229

230230
// join pipe
231231
f(`foo | join by (x) (bar)`, `_time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] foo | join by (x) (_time:[2024-12-25T14:56:43Z,2025-01-13T12:45:34Z] bar)`)
@@ -1319,6 +1319,13 @@ func TestParseQuery_Success(t *testing.T) {
13191319
f(`_time:1h (Offset)`, `_time:1h "Offset"`) // "offset" is a search word, since it is in parens
13201320
f(`_time:1h "and"`, `_time:1h "and"`) // "and" is a search word, since it is quoted
13211321

1322+
// multiple _time filters
1323+
f(`_time:1h _time:2025Z`, `_time:1h _time:2025Z`)
1324+
f(`_time:<10h _time:2025Z`, `_time:<10h _time:2025Z`)
1325+
f(`_time:2025Z _time:[2024-10Z, 2025-03Z]`, `_time:2025Z _time:[2024-10Z,2025-03Z]`)
1326+
f(`options(time_offset=1h) _time:2025Z _time:[2024-10Z, 2025-03Z]`, `options(time_offset=1h) _time:2025Z _time:[2024-10Z,2025-03Z]`)
1327+
f(`_time:2025Z _time:2024Z _time:10y`, `_time:2025Z _time:2024Z _time:10y`)
1328+
13221329
// dayRange filters
13231330
f(`_time:day_range[08:00, 20:30)`, `_time:day_range[08:00, 20:30)`)
13241331
f(`_time:day_range(08:00, 20:30)`, `_time:day_range(08:00, 20:30)`)

0 commit comments

Comments
 (0)