Skip to content

Commit e37ebd5

Browse files
authored
Add support for ZINTERCARD (#1857)
* add zintercard * fix pr comment * linters
1 parent 146e58e commit e37ebd5

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

redis/commands/core.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3153,6 +3153,19 @@ def zinterstore(self, dest, keys, aggregate=None):
31533153
"""
31543154
return self._zaggregate("ZINTERSTORE", dest, keys, aggregate)
31553155

3156+
def zintercard(self, numkeys: int, keys: List[str], limit: int = 0) -> int:
3157+
"""
3158+
Return the cardinality of the intersect of multiple sorted sets
3159+
specified by ``keys`.
3160+
When LIMIT provided (defaults to 0 and means unlimited), if the intersection
3161+
cardinality reaches limit partway through the computation, the algorithm will
3162+
exit and yield limit as the cardinality
3163+
3164+
For more information check https://redis.io/commands/zintercard
3165+
"""
3166+
args = [numkeys, *keys, "LIMIT", limit]
3167+
return self.execute_command("ZINTERCARD", *args)
3168+
31563169
def zlexcount(self, name, min, max):
31573170
"""
31583171
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
@@ -1979,6 +1979,15 @@ def test_zinter(self, r):
19791979
(b"a1", 23),
19801980
]
19811981

1982+
@pytest.mark.onlynoncluster
1983+
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
1984+
def test_zintercard(self, unstable_r):
1985+
unstable_r.zadd("a", {"a1": 1, "a2": 2, "a3": 1})
1986+
unstable_r.zadd("b", {"a1": 2, "a2": 2, "a3": 2})
1987+
unstable_r.zadd("c", {"a1": 6, "a3": 5, "a4": 4})
1988+
assert unstable_r.zintercard(3, ["a", "b", "c"]) == 2
1989+
assert unstable_r.zintercard(3, ["a", "b", "c"], limit=1) == 1
1990+
19821991
@pytest.mark.onlynoncluster
19831992
def test_zinterstore_sum(self, r):
19841993
r.zadd("a", {"a1": 1, "a2": 1, "a3": 1})

0 commit comments

Comments
 (0)