From 551caac91a637efa38ede27f1bd3c3a1a5f0d6f8 Mon Sep 17 00:00:00 2001 From: Vladislav Oleshko Date: Sun, 25 Aug 2024 13:37:35 +0300 Subject: [PATCH] chore(server): Sort correctly in ZINTER --- src/server/zset_family.cc | 2 +- src/server/zset_family_test.cc | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/server/zset_family.cc b/src/server/zset_family.cc index 87ca46143866..04d77f9be0fb 100644 --- a/src/server/zset_family.cc +++ b/src/server/zset_family.cc @@ -2133,7 +2133,7 @@ void ZSetFamily::ZInter(CmdArgList args, ConnectionContext* cntx) { std::sort(scored_array.begin(), scored_array.end(), [](const std::pair& a, const std::pair& b) { - return a.second < b.second; + return tie(a.second, a.first) < tie(b.second, b.first); }); auto* rb = static_cast(cntx->reply_builder()); diff --git a/src/server/zset_family_test.cc b/src/server/zset_family_test.cc index a32371014da4..60df38983343 100644 --- a/src/server/zset_family_test.cc +++ b/src/server/zset_family_test.cc @@ -669,6 +669,14 @@ TEST_F(ZSetFamilyTest, ZInter) { resp = Run({"zinter", "3", "z3", "z4", "z5"}); EXPECT_THAT(resp, ArrLen(0)); + + // zinter output sorts keys with equal scores lexicographically + Run({"del", "z1", "z2", "z3", "z4", "z5"}); + Run({"zadd", "z1", "1", "e", "1", "a", "1", "b", "1", "x"}); + Run({"zadd", "z2", "1", "e", "1", "a", "1", "b", "1", "y"}); + Run({"zadd", "z3", "1", "e", "1", "a", "1", "b", "1", "z"}); + Run({"zadd", "z4", "1", "e", "1", "a", "1", "b", "1", "o"}); + EXPECT_THAT(Run({"zinter", "4", "z1", "z2", "z3", "z4"}).GetVec(), ElementsAre("a", "b", "e")); } TEST_F(ZSetFamilyTest, ZInterCard) {