From 5ad88f58498611aeb86dbde5c694189872ab3620 Mon Sep 17 00:00:00 2001 From: dvora-h Date: Wed, 5 Jan 2022 12:21:17 +0200 Subject: [PATCH 1/2] add sintercard --- redis/commands/core.py | 14 ++++++++++++++ tests/test_commands.py | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/redis/commands/core.py b/redis/commands/core.py index 4f0accd957..b5d2e4e40f 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -2,6 +2,7 @@ import hashlib import time import warnings +from typing import List from redis.exceptions import ConnectionError, DataError, NoScriptError, RedisError @@ -2397,6 +2398,19 @@ def sinter(self, keys, *args): args = list_or_args(keys, args) return self.execute_command("SINTER", *args) + def sintercard(self, numkeys: int, keys: List[str], limit: int = 0) -> int: + """ + Return the cardinality of the intersect of multiple 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/sintercard + """ + args = [numkeys] + keys + ["LIMIT", limit] + return self.execute_command("SINTERCARD", *args) + def sinterstore(self, dest, keys, *args): """ Store the intersection of sets specified by ``keys`` into a new diff --git a/tests/test_commands.py b/tests/test_commands.py index b28b63ea6e..a40fc0e8b0 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1741,6 +1741,15 @@ def test_sinter(self, r): r.sadd("b", "2", "3") assert r.sinter("a", "b") == {b"2", b"3"} + @pytest.mark.onlynoncluster + # @skip_if_server_version_lt("7.0.0") turn on after redis 7 release + def test_sintercard(self, unstable_r): + unstable_r.sadd("a", 1, 2, 3) + unstable_r.sadd("b", 1, 2, 3) + unstable_r.sadd("c", 1, 3, 4) + assert unstable_r.sintercard(3, ["a", "b", "c"]) == 2 + assert unstable_r.sintercard(3, ["a", "b", "c"], limit=1) == 1 + @pytest.mark.onlynoncluster def test_sinterstore(self, r): r.sadd("a", "1", "2", "3") From f6b21de6d63d8c672b43eaeeb84542334ca9bde2 Mon Sep 17 00:00:00 2001 From: dvora-h Date: Wed, 12 Jan 2022 14:51:22 +0200 Subject: [PATCH 2/2] fix pr comment --- redis/commands/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redis/commands/core.py b/redis/commands/core.py index b5d2e4e40f..ebc0f1f378 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -2408,7 +2408,7 @@ def sintercard(self, numkeys: int, keys: List[str], limit: int = 0) -> int: For more information check https://redis.io/commands/sintercard """ - args = [numkeys] + keys + ["LIMIT", limit] + args = [numkeys, *keys, "LIMIT", limit] return self.execute_command("SINTERCARD", *args) def sinterstore(self, dest, keys, *args):