Skip to content

Commit

Permalink
make ZUNION, ZUNIONSTORE to avoid command_keys as well
Browse files Browse the repository at this point in the history
  • Loading branch information
zach-iee committed Jul 4, 2023
1 parent b011b6b commit 4b228cf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,14 @@ def determine_slot(self, *args):
if len(eval_keys) == 0:
return random.randrange(0, REDIS_CLUSTER_HASH_SLOTS)
keys = eval_keys
elif command == "ZUNIONSTORE":
# https://github.com/redis/redis/pull/12380
# command syntax: ZUNIONSTORE destination numkeys key [key ...]
# [WEIGHTS weight [weight ...]] [AGGREGATE <SUM | MIN | MAX>]
if len(args) <= 3:
raise RedisClusterException(f"Invalid args in ZUNIONSTORE: {args}")
num_actual_keys = args[2]
keys = (args[1],) + args[3 : 3 + num_actual_keys]
else:
keys = self._get_command_keys(*args)
if keys is None or len(keys) == 0:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2006,10 +2006,30 @@ def test_cluster_zunion(self, r):
]

def test_cluster_zunionstore_sum(self, r):
assert r.zunionstore("{foo}d", ["{foo}" + str(i) for i in range(0, 256)]) == 0

r.zadd("{foo}a", {"a1": 1, "a2": 1, "a3": 1})
r.zadd("{foo}b", {"a1": 2, "a2": 2, "a3": 2})
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})

result_key = "{foo}d"
failed_keys = ["{foo1}a", "{foo}b", "{foo}c"]
with pytest.raises(
RedisClusterException,
match="ZUNIONSTORE - all keys must map to the same key slot",
):
r.zunionstore(result_key, failed_keys)

result_key = "{foo1}d"
failed_keys = ["{foo}a", "{foo}b"]
with pytest.raises(
RedisClusterException,
match="ZUNIONSTORE - all keys must map to the same key slot",
):
r.zunionstore(result_key, failed_keys)

assert r.zunionstore("{foo}d", ["{foo}a", "{foo}b", "{foo}c"]) == 4
assert r.zunionstore("{foo}e", ["{foo}a"]) == 3
assert r.zrange("{foo}d", 0, -1, withscores=True) == [
(b"a2", 3),
(b"a4", 4),
Expand Down

0 comments on commit 4b228cf

Please sign in to comment.