Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ZINTERCARD #1857

Merged
merged 4 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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