Skip to content

Commit

Permalink
Fixed ZDIFF return empty array with one key, should return the orgina…
Browse files Browse the repository at this point in the history
…l set (#738)

Co-authored-by: Tal Zaccai <[email protected]>
  • Loading branch information
Vijay-Nirmal and TalZaccai authored Oct 23, 2024
1 parent eabadb5 commit 8a368b5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
7 changes: 7 additions & 0 deletions libs/server/Storage/Session/ObjectStore/SortedSetOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,13 @@ public unsafe GarnetStatus SortedSetDifference(ArgSlice[] keys, out Dictionary<b
{
return GarnetStatus.WRONGTYPE;
}

if (keys.Length == 1)
{
pairs = firstSortedSet.Dictionary;
return GarnetStatus.OK;
}

// read the rest of the keys
for (var item = 1; item < keys.Length; item++)
{
Expand Down
37 changes: 37 additions & 0 deletions test/Garnet.test/RespSortedSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,43 @@ public void CheckSortedSetOperationsOnWrongTypeObjectSE()
RespTestsUtils.CheckCommandOnWrongTypeObjectSE(() => db.SortedSetScores(keys[1], values[1]));
}

[Test]
public void CanDoZDiff()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);

var key1 = new RedisKey("key1");
var key2 = new RedisKey("key2");
var key1Values = new[] { new SortedSetEntry("Hello", 1), new SortedSetEntry("World", 2) };
var key2Values = new[] { new SortedSetEntry("Hello", 5), new SortedSetEntry("Mundo", 7) };
var expectedValue = new SortedSetEntry("World", 2);

db.SortedSetAdd(key1, key1Values);
db.SortedSetAdd(key2, key2Values);

var diff = db.SortedSetCombine(SetOperation.Difference, [key1, key2]);
ClassicAssert.AreEqual(1, diff.Length);
ClassicAssert.AreEqual(expectedValue.Element.ToString(), diff[0].ToString());

var diffWithScore = db.SortedSetCombineWithScores(SetOperation.Difference, [key1, key2]);
ClassicAssert.AreEqual(1, diffWithScore.Length);
ClassicAssert.AreEqual(expectedValue.Element.ToString(), diffWithScore[0].Element.ToString());
ClassicAssert.AreEqual(expectedValue.Score, diffWithScore[0].Score);

// With only one key, it should return the same elements
diffWithScore = db.SortedSetCombineWithScores(SetOperation.Difference, [key1]);
ClassicAssert.AreEqual(2, diffWithScore.Length);
ClassicAssert.AreEqual(key1Values[0].Element.ToString(), diffWithScore[0].Element.ToString());
ClassicAssert.AreEqual(key1Values[0].Score, diffWithScore[0].Score);
ClassicAssert.AreEqual(key1Values[1].Element.ToString(), diffWithScore[1].Element.ToString());
ClassicAssert.AreEqual(key1Values[1].Score, diffWithScore[1].Score);

// With no value key, it should return an empty array
diffWithScore = db.SortedSetCombineWithScores(SetOperation.Difference, [new RedisKey("key3")]);
ClassicAssert.AreEqual(0, diffWithScore.Length);
}

#endregion

#region LightClientTests
Expand Down

0 comments on commit 8a368b5

Please sign in to comment.