Skip to content

fix sorting in sort_by_label and sort_by_label_desc - #7886

Open
SungJin1212 wants to merge 2 commits into
masterfrom
fix-sort-by-label
Open

SungJin1212 wants to merge 2 commits into
masterfrom
fix-sort-by-label

Conversation

@SungJin1212

Copy link
Copy Markdown
Member

What this PR does:

When sharded instant query has sort_by_label or sort_by_label_desc, the query frontend re-sorts the merged result by sample value instead of by the given labels. For example, the result of sort_by_label(sum by (job) (up), "job") comes back in value order rather than job order.

This PR adds a separate sort plan for them. It orders the merged series with Prometheus' own sort_by_label / sort_by_label_desc implementation.

Which issue(s) this PR fixes:
Fixes #

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
  • docs/configuration/v1-guarantees.md updated if this PR introduces experimental flags

@SungJin1212
SungJin1212 requested a review from a team as a code owner October 20, 2026 05:29
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>

@sandy2008 sandy2008 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. All six new TestMergeResponse_SortByLabel cases fail without the merge.go change and pass with it. One optional suggestion inline.

Comment thread pkg/querier/tripperware/merge.go Outdated
}

vec := make(promql.Vector, 0, len(output))
for _, s := range output {

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.

Optional: vec is filled in map iteration order, so funcSortByLabel sees a different input order on every merge. Upstream's comparator isn't a strict weak order when a series lacks a sort label (natsort.Compare("", "x") and Compare("x", "") are both false) or when two values are naturally equal (1 vs 01), so in those cases the merged order varies between runs. Merging {job="a"}, {job="b",foo="x"}, {job="c"}, {job="d",foo="y"} from two shards for sort_by_label(sum by (job, foo) (up), "foo") gave 4 different orders in 200 runs, some with foo="y" ahead of foo="x". Iterating over slices.Sorted(maps.Keys(output)) makes it deterministic, and the new tests still pass. (prometheus/prometheus#19466 is open upstream for the natural-equal tie-break.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, applied it to the latest commit.

Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants