Skip to content

fix(engine): Fix bug in SortMerge which caused rows to be skipped due to incorrect sorting - #18334

Merged
chaudum merged 2 commits into
mainfrom
chaudum/fix-sortmerge-bug
Jul 4, 2025
Merged

fix(engine): Fix bug in SortMerge which caused rows to be skipped due to incorrect sorting#18334
chaudum merged 2 commits into
mainfrom
chaudum/fix-sortmerge-bug

Conversation

@chaudum

@chaudum chaudum commented Jul 4, 2025

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:

This PR fixes two problems with the sorting of rows of input records:

  1. When sorting input indexes (batchIndexes []int) based on their current timestamp at index offset (timestamps []int64), the positional correspondence was not maintained leading to pull rows from wrong records.
  2. When calculating how many rows from the current batch could be yield, the comparison of the timestamp with the next highest timestamp was wrong.

Fixing the assertion in the testcases made these problems visible.

chaudum added 2 commits July 4, 2025 11:57
This will cause the tests to fail because of an incorrect
implementation:

```console
$ gotest -v ./pkg/engine/executor/... -test.run=TestSortMerge
=== RUN   TestSortMerge
=== RUN   TestSortMerge/invalid_column_name
=== RUN   TestSortMerge/ascending_timestamp
    sortmerge_test.go:89:
                Error Trace:    /home/christian/sandbox/grafana/loki/pkg/engine/executor/sortmerge_test.go:89
                Error:          Should be true
                Test:           TestSortMerge/ascending_timestamp
                Messages:       timestamps are not sorted in ASC order: [1000000000000001 1000000001000001 1000000002000000 1000000002000001 1000000003000000 1000000004000000 1000003000000001 1000003001000001 1000003000000000 1000003001000000 1000003002000000 1000003002000000 1000003002000001 1000003003000000 1000003004000000 1000006000000001 1000006001000001 1000006000000000 1000006001000000 1000006002000000 1000006002000000 1000006002000001 1000006003000000 1000006004000000 1000009000000001 1000009000000000 1000009001000000 1000009002000000 1000009002000000 1000012000000000]
=== RUN   TestSortMerge/descending_timestamp
    sortmerge_test.go:136:
                Error Trace:    /home/christian/sandbox/grafana/loki/pkg/engine/executor/sortmerge_test.go:136
                Error:          Should be true
                Test:           TestSortMerge/descending_timestamp
                Messages:       timestamps are not sorted in DESC order: [1000003000000000 1000002999000000 1000002998000000 1000000002000000 1000000001000000 1000000000000001 1000000000000000 1000000000000000 999999999000001 999999998000001 999999999000000 999999998000000 999997002000000 999997001000000 999997000000001 999997000000000 999997000000000 999996999000001 999996998000001 999996999000000 999996998000000 999994002000000 999994001000000 999994000000001 999994000000000 999994000000000 999993999000001 999993998000001 999991002000000 999991000000001]
--- FAIL: TestSortMerge (0.00s)
    --- PASS: TestSortMerge/invalid_column_name (0.00s)
    --- FAIL: TestSortMerge/ascending_timestamp (0.00s)
    --- FAIL: TestSortMerge/descending_timestamp (0.00s)
FAIL
FAIL    github.com/grafana/loki/v3/pkg/engine/executor  0.011s
FAIL

```
Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
@chaudum
chaudum requested a review from a team as a code owner July 4, 2025 10:09
@chaudum
chaudum enabled auto-merge (squash) July 4, 2025 10:23

@ashwanthgoli ashwanthgoli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@chaudum
chaudum merged commit 4fbe7c1 into main Jul 4, 2025
66 checks passed
@chaudum
chaudum deleted the chaudum/fix-sortmerge-bug branch July 4, 2025 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2 participants