Skip to content

Commit

Permalink
Add support for ZINTERCARD (redis#1857)
Browse files Browse the repository at this point in the history
* add zintercard

* fix pr comment

* linters
  • Loading branch information
dvora-h committed Feb 2, 2022
1 parent 0567fd8 commit 9e098aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3153,6 +3153,19 @@ def zinterstore(self, dest, keys, aggregate=None):
"""
return self._zaggregate("ZINTERSTORE", dest, keys, aggregate)

def zintercard(self, numkeys: int, keys: List[str], limit: int = 0) -> int:
"""
Return the cardinality of the intersect of multiple sorted sets
specified by ``keys`.
When LIMIT provided (defaults to 0 and means unlimited), if the intersection
cardinality reaches limit partway through the computation, the algorithm will
exit and yield limit as the cardinality
For more information check https://redis.io/commands/zintercard
"""
args = [numkeys, *keys, "LIMIT", limit]
return self.execute_command("ZINTERCARD", *args)

def zlexcount(self, name, min, max):
"""
Return the number of items in the sorted set ``name`` between the
Expand Down
9 changes: 9 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,15 @@ def test_zinter(self, r):
(b"a1", 23),
]

@pytest.mark.onlynoncluster
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
def test_zintercard(self, unstable_r):
unstable_r.zadd("a", {"a1": 1, "a2": 2, "a3": 1})
unstable_r.zadd("b", {"a1": 2, "a2": 2, "a3": 2})
unstable_r.zadd("c", {"a1": 6, "a3": 5, "a4": 4})
assert unstable_r.zintercard(3, ["a", "b", "c"]) == 2
assert unstable_r.zintercard(3, ["a", "b", "c"], limit=1) == 1

@pytest.mark.onlynoncluster
def test_zinterstore_sum(self, r):
r.zadd("a", {"a1": 1, "a2": 1, "a3": 1})
Expand Down

0 comments on commit 9e098aa

Please sign in to comment.