Skip to content

Commit

Permalink
chore(server): Sort correctly in ZINTER (#3566)
Browse files Browse the repository at this point in the history
  • Loading branch information
dranikpg authored Aug 25, 2024
1 parent 20b8817 commit fce7970
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/server/zset_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2123,7 +2123,7 @@ void ZSetFamily::ZInter(CmdArgList args, ConnectionContext* cntx) {

std::sort(scored_array.begin(), scored_array.end(),
[](const std::pair<std::string, double>& a, const std::pair<std::string, double>& b) {
return a.second < b.second;
return tie(a.second, a.first) < tie(b.second, b.first);
});

auto* rb = static_cast<RedisReplyBuilder*>(cntx->reply_builder());
Expand Down
8 changes: 8 additions & 0 deletions src/server/zset_family_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,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) {
Expand Down

0 comments on commit fce7970

Please sign in to comment.