Skip to content

Commit d47dbe7

Browse files
committed
make ZUNION, ZUNIONSTORE to avoid command_keys as well
1 parent b011b6b commit d47dbe7

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

redis/cluster.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,22 @@ def determine_slot(self, *args):
968968
if len(eval_keys) == 0:
969969
return random.randrange(0, REDIS_CLUSTER_HASH_SLOTS)
970970
keys = eval_keys
971+
elif command == "ZUNIONSTORE":
972+
# https://sendbird.slack.com/archives/GFLAQKVDZ/p1688047132982659?thread_ts=1688046729.152689&cid=GFLAQKVDZ
973+
# command syntax: ZUNIONSTORE destination numkeys key [key ...]
974+
# [WEIGHTS weight [weight ...]] [AGGREGATE <SUM | MIN | MAX>]
975+
if len(args) <= 3:
976+
raise RedisClusterException(f"Invalid args in ZUNIONSTORE: {args}")
977+
num_actual_keys = args[2]
978+
keys = (args[1],) + args[3 : 3 + num_actual_keys]
979+
elif command == "ZUNION":
980+
# https://sendbird.slack.com/archives/GFLAQKVDZ/p1688047132982659?thread_ts=1688046729.152689&cid=GFLAQKVDZ
981+
# command syntax: ZUNION numkeys key [key ...] [WEIGHTS weight [weight ...]]
982+
# [AGGREGATE <SUM | MIN | MAX>] [WITHSCORES]
983+
if len(args) <= 2:
984+
raise RedisClusterException(f"Invalid args in ZUNION: {args}")
985+
num_actual_keys = args[1]
986+
keys = args[2 : 2 + num_actual_keys]
971987
else:
972988
keys = self._get_command_keys(*args)
973989
if keys is None or len(keys) == 0:

tests/test_cluster.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,7 +1981,16 @@ def test_cluster_zunion(self, r):
19811981
r.zadd("{foo}a", {"a1": 1, "a2": 1, "a3": 1})
19821982
r.zadd("{foo}b", {"a1": 2, "a2": 2, "a3": 2})
19831983
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
1984+
1985+
failed_keys = ["{foo1}a", "{foo}b", "{foo}c"]
1986+
with pytest.raises(
1987+
RedisClusterException,
1988+
match="ZUNION - all keys must map to the same key slot",
1989+
):
1990+
r.zunion(failed_keys)
1991+
19841992
# sum
1993+
assert r.zunion(["{foo}a"]) == [b"a1", b"a2", b"a3"]
19851994
assert r.zunion(["{foo}a", "{foo}b", "{foo}c"]) == [b"a2", b"a4", b"a3", b"a1"]
19861995
assert r.zunion(["{foo}a", "{foo}b", "{foo}c"], withscores=True) == [
19871996
(b"a2", 3),
@@ -2009,7 +2018,25 @@ def test_cluster_zunionstore_sum(self, r):
20092018
r.zadd("{foo}a", {"a1": 1, "a2": 1, "a3": 1})
20102019
r.zadd("{foo}b", {"a1": 2, "a2": 2, "a3": 2})
20112020
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
2021+
2022+
result_key = "{foo}d"
2023+
failed_keys = ["{foo1}a", "{foo}b", "{foo}c"]
2024+
with pytest.raises(
2025+
RedisClusterException,
2026+
match="ZUNIONSTORE - all keys must map to the same key slot",
2027+
):
2028+
r.zunionstore(result_key, failed_keys)
2029+
2030+
result_key = "{foo1}d"
2031+
failed_keys = ["{foo}a", "{foo}b"]
2032+
with pytest.raises(
2033+
RedisClusterException,
2034+
match="ZUNIONSTORE - all keys must map to the same key slot",
2035+
):
2036+
r.zunionstore(result_key, failed_keys)
2037+
20122038
assert r.zunionstore("{foo}d", ["{foo}a", "{foo}b", "{foo}c"]) == 4
2039+
assert r.zunionstore("{foo}e", ["{foo}a"]) == 3
20132040
assert r.zrange("{foo}d", 0, -1, withscores=True) == [
20142041
(b"a2", 3),
20152042
(b"a4", 4),

0 commit comments

Comments
 (0)