@@ -672,7 +672,8 @@ class Redis:
672
672
"SDIFF SINTER SMEMBERS SUNION" , lambda r : r and set (r ) or set ()
673
673
),
674
674
** string_keys_to_dict (
675
- "ZPOPMAX ZPOPMIN ZDIFF ZRANGE ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE" ,
675
+ "ZPOPMAX ZPOPMIN ZINTER ZDIFF ZRANGE ZRANGEBYSCORE ZREVRANGE "
676
+ "ZREVRANGEBYSCORE" ,
676
677
zset_score_pairs ,
677
678
),
678
679
** string_keys_to_dict (
@@ -3260,16 +3261,39 @@ def zincrby(self, name: KeyT, amount: float, value: EncodableT) -> Awaitable:
3260
3261
"""Increment the score of ``value`` in sorted set ``name`` by ``amount``"""
3261
3262
return self .execute_command ("ZINCRBY" , name , amount , value )
3262
3263
3264
+ def zinter (
3265
+ self ,
3266
+ keys : KeysT ,
3267
+ aggregate : Optional [str ] = None ,
3268
+ withscores : bool = False
3269
+ ) -> Awaitable :
3270
+ """
3271
+ Return the intersect of multiple sorted sets specified by ``keys``.
3272
+ With the ``aggregate`` option, it is possible to specify how the
3273
+ results of the union are aggregated. This option defaults to SUM,
3274
+ where the score of an element is summed across the inputs where it
3275
+ exists. When this option is set to either MIN or MAX, the resulting
3276
+ set will contain the minimum or maximum score of an element across
3277
+ the inputs where it exists.
3278
+ """
3279
+ return self ._zaggregate (
3280
+ "ZINTER" , None , keys , aggregate , withscores = withscores
3281
+ )
3282
+
3263
3283
def zinterstore (
3264
3284
self ,
3265
3285
dest : KeyT ,
3266
3286
keys : Union [Sequence [KeyT ], Mapping [AnyKeyT , float ]],
3267
3287
aggregate : Optional [str ] = None ,
3268
3288
) -> Awaitable :
3269
3289
"""
3270
- Intersect multiple sorted sets specified by ``keys`` into
3271
- a new sorted set, ``dest``. Scores in the destination will be
3272
- aggregated based on the ``aggregate``, or SUM if none is provided.
3290
+ Intersect multiple sorted sets specified by ``keys`` into a new
3291
+ sorted set, ``dest``. Scores in the destination will be aggregated
3292
+ based on the ``aggregate``. This option defaults to SUM, where the
3293
+ score of an element is summed across the inputs where it exists.
3294
+ When this option is set to either MIN or MAX, the resulting set will
3295
+ contain the minimum or maximum score of an element across the inputs
3296
+ where it exists.
3273
3297
"""
3274
3298
return self ._zaggregate ("ZINTERSTORE" , dest , keys , aggregate )
3275
3299
@@ -3593,11 +3617,15 @@ def zunionstore(
3593
3617
def _zaggregate (
3594
3618
self ,
3595
3619
command : str ,
3596
- dest : KeyT ,
3620
+ dest : Optional [ KeyT ] ,
3597
3621
keys : Union [Sequence [KeyT ], Mapping [AnyKeyT , float ]],
3598
3622
aggregate : Optional [str ] = None ,
3623
+ ** options ,
3599
3624
) -> Awaitable :
3600
- pieces : List [EncodableT ] = [command , dest , len (keys )]
3625
+ pieces : List [EncodableT ] = [command ]
3626
+ if dest is not None :
3627
+ pieces .append (dest )
3628
+ pieces .append (len (keys ))
3601
3629
if isinstance (keys , dict ):
3602
3630
keys , weights = keys .keys (), keys .values ()
3603
3631
else :
@@ -3607,8 +3635,13 @@ def _zaggregate(
3607
3635
pieces .append (b"WEIGHTS" )
3608
3636
pieces .extend (weights )
3609
3637
if aggregate :
3610
- pieces .append (b"AGGREGATE" )
3611
- pieces .append (aggregate )
3638
+ if aggregate .upper () in ["SUM" , "MIN" , "MAX" ]:
3639
+ pieces .append (b'AGGREGATE' )
3640
+ pieces .append (aggregate )
3641
+ else :
3642
+ raise DataError ("aggregate can be sum, min, or max" )
3643
+ if options .get ("withscores" , False ):
3644
+ pieces .append (b'WITHSCORES' )
3612
3645
return self .execute_command (* pieces )
3613
3646
3614
3647
# HYPERLOGLOG COMMANDS
0 commit comments