Skip to content

Commit 9661260

Browse files
committed
add zintercard
1 parent 15f315a commit 9661260

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

redis/commands/core.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import hashlib
33
import time
44
import warnings
5+
from typing import List
56

67
from redis.exceptions import ConnectionError, DataError, NoScriptError, RedisError
78

@@ -3162,6 +3163,19 @@ def zinterstore(self, dest, keys, aggregate=None):
31623163
"""
31633164
return self._zaggregate("ZINTERSTORE", dest, keys, aggregate)
31643165

3166+
def zintercard(self, numkeys: int, keys: List[str], limit: int = 0) -> int:
3167+
"""
3168+
Return the cardinality of the intersect of multiple sorted sets
3169+
specified by ``keys`.
3170+
When LIMIT provided (defaults to 0 and means unlimited), if the intersection
3171+
cardinality reaches limit partway through the computation, the algorithm will
3172+
exit and yield limit as the cardinality
3173+
3174+
For more information check https://redis.io/commands/zintercard
3175+
"""
3176+
args = [numkeys] + keys + ["LIMIT", limit]
3177+
return self.execute_command("ZINTERCARD", *args)
3178+
31653179
def zlexcount(self, name, min, max):
31663180
"""
31673181
Return the number of items in the sorted set ``name`` between the

tests/test_commands.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,15 @@ def test_zinter(self, r):
19721972
(b"a1", 23),
19731973
]
19741974

1975+
@pytest.mark.onlynoncluster
1976+
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
1977+
def test_zintercard(self, unstable_r):
1978+
unstable_r.zadd("a", {"a1": 1, "a2": 2, "a3": 1})
1979+
unstable_r.zadd("b", {"a1": 2, "a2": 2, "a3": 2})
1980+
unstable_r.zadd("c", {"a1": 6, "a3": 5, "a4": 4})
1981+
assert unstable_r.zintercard(3, ["a", "b", "c"]) == 2
1982+
assert unstable_r.zintercard(3, ["a", "b", "c"], limit=1) == 1
1983+
19751984
@pytest.mark.onlynoncluster
19761985
def test_zinterstore_sum(self, r):
19771986
r.zadd("a", {"a1": 1, "a2": 1, "a3": 1})

0 commit comments

Comments
 (0)