Skip to content

Commit aced3dc

Browse files
authored
fix: zdiff WITHSCORES param and resp3 response (#6013)
1 parent 23fb0fa commit aced3dc

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/server/zset_family.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,9 +2318,12 @@ void ZSetFamily::ZDiff(CmdArgList args, const CommandContext& cmd_cntx) {
23182318
return;
23192319
}
23202320

2321-
const bool with_scores = ArgS(args, args.size() - 1) == "WITHSCORES";
2322-
rb->StartArray(smvec.size() * (with_scores ? 2 : 1));
2321+
const bool with_scores = absl::EqualsIgnoreCase(ArgS(args, args.size() - 1), "WITHSCORES");
2322+
bool is_resp3 = rb->IsResp3();
2323+
rb->StartArray(smvec.size() * ((with_scores && !is_resp3) ? 2 : 1));
23232324
for (const auto& [score, key] : smvec) {
2325+
if (is_resp3)
2326+
rb->StartArray(with_scores ? 2 : 1);
23242327
rb->SendBulkString(key);
23252328
if (with_scores) {
23262329
rb->SendDouble(score);

src/server/zset_family_test.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,10 +1249,22 @@ TEST_F(ZSetFamilyTest, ZDiff) {
12491249
resp = Run({"zdiff", "1", "z1", "WITHSCORES"});
12501250
EXPECT_THAT(resp.GetVec(), ElementsAre("one", "1", "two", "2", "three", "3", "four", "4"));
12511251

1252-
resp = Run({"zdiff", "2", "z1", "z2", "WITHSCORES"});
1252+
resp = Run({"zdiff", "2", "z1", "z2", "withscores"});
12531253
EXPECT_THAT(resp.GetVec(), ElementsAre("two", "2", "three", "3", "four", "4"));
12541254
}
12551255

1256+
TEST_F(ZSetFamilyTest, ZDiff_Resp3) {
1257+
Run({"hello", "3"});
1258+
EXPECT_EQ(4, CheckedInt({"zadd", "z1", "1", "one", "2", "two", "3", "three", "4", "four"}));
1259+
1260+
auto resp = Run({"zdiff", "1", "z1", "withscores"});
1261+
ASSERT_THAT(resp, ArrLen(4));
1262+
ASSERT_THAT(resp.GetVec()[0].GetVec(), ElementsAre("one", DoubleArg(1)));
1263+
ASSERT_THAT(resp.GetVec()[1].GetVec(), ElementsAre("two", DoubleArg(2)));
1264+
ASSERT_THAT(resp.GetVec()[2].GetVec(), ElementsAre("three", DoubleArg(3)));
1265+
ASSERT_THAT(resp.GetVec()[3].GetVec(), ElementsAre("four", DoubleArg(4)));
1266+
}
1267+
12561268
TEST_F(ZSetFamilyTest, ZDiffStoreError) {
12571269
RespExpr resp;
12581270

0 commit comments

Comments
 (0)