Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/logstorage/pipe_sort_topk.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ func topkLess(ps *pipeSort, a, b *pipeTopkRow) bool {
bb.B = marshalTimestampRFC3339NanoString(bb.B[:0], a.timestamp)
vA = bytesutil.ToUnsafeString(bb.B)
} else if isTimeB[i] {
bb.B = marshalTimestampRFC3339NanoString(bb.B[:0], a.timestamp)
bb.B = marshalTimestampRFC3339NanoString(bb.B[:0], b.timestamp)
vB = bytesutil.ToUnsafeString(bb.B)
}

Expand Down
58 changes: 58 additions & 0 deletions lib/logstorage/pipe_sort_topk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,64 @@ import (
"testing"
)

func TestTopkLess(t *testing.T) {
parseTimestamp := func(s string) int64 {
t.Helper()

timestamp, ok := TryParseTimestampRFC3339Nano(s)
if !ok {
t.Fatalf("cannot parse timestamp %q", s)
}
return timestamp
}

ps := &pipeSort{
byFields: []*bySortField{
{name: "_time"},
},
}
psDesc := &pipeSort{
byFields: []*bySortField{
{name: "_time"},
},
isDesc: true,
}

stringRow := func(s string) *pipeTopkRow {
return &pipeTopkRow{
byColumns: []string{s},
byColumnsIsTime: []bool{false},
}
}
timeRow := func(s string) *pipeTopkRow {
return &pipeTopkRow{
byColumns: []string{""},
byColumnsIsTime: []bool{true},
timestamp: parseTimestamp(s),
}
}
f := func(ps *pipeSort, a, b *pipeTopkRow, resultExpected bool) {
t.Helper()

result := topkLess(ps, a, b)
if result != resultExpected {
t.Fatalf("unexpected result for topkLess(%#v, %#v); got %v; want %v", a, b, result, resultExpected)
}
}

// string time is smaller than real time
f(ps, stringRow("2026-04-25T10:00:54Z"), timeRow("2026-04-25T10:01:54Z"), true)
f(ps, timeRow("2026-04-25T10:01:54Z"), stringRow("2026-04-25T10:00:54Z"), false)

// real time is smaller than string time
f(ps, timeRow("2026-04-25T10:00:54Z"), stringRow("2026-04-25T10:01:54Z"), true)
f(ps, stringRow("2026-04-25T10:01:54Z"), timeRow("2026-04-25T10:00:54Z"), false)

// string time vs real time with descending sort
f(psDesc, stringRow("2026-04-25T10:00:54Z"), timeRow("2026-04-25T10:01:54Z"), false)
f(psDesc, timeRow("2026-04-25T10:01:54Z"), stringRow("2026-04-25T10:00:54Z"), true)
Comment thread
cuongleqq marked this conversation as resolved.
}

func TestLessString(t *testing.T) {
f := func(a, b string, resultExpected bool) {
t.Helper()
Expand Down