Skip to content

Commit 0b3ce1a

Browse files
committed
Fix ZUNIONSTORE nan-to-zero handling again
Real redis handles things differently for ZINTERSTORE versus ZUNIONSTORE (see antirez/redis#3954), and dd10a73 fixed it for ZINTERSTORE but then broken it for ZUNIONSTORE.
1 parent e4dad7c commit 0b3ce1a

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

fakeredis/_server.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,10 @@ def _zunioninter(self, func, dest, numkeys, *args):
21542154
for s, w in sorted(zip(sets, weights), key=lambda x: len(x[0])):
21552155
for member, score in s.items():
21562156
score *= w
2157+
# Redis only does this step for ZUNIONSTORE. See
2158+
# https://github.com/antirez/redis/issues/3954.
2159+
if func == 'ZUNIONSTORE' and math.isnan(score):
2160+
score = 0.0
21572161
if member not in out_members:
21582162
continue
21592163
if member in out:

test_fakeredis.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,6 +2619,14 @@ def test_zunionstore_weights(self):
26192619
[(b'one', 3), (b'two', 6), (b'four', 8)])
26202620

26212621
def test_zunionstore_nan_to_zero(self):
2622+
self.zadd('foo', {'x': math.inf})
2623+
self.zadd('foo2', {'x': math.inf})
2624+
self.redis.zunionstore('bar', {'foo': 1.0, 'foo2': 0.0})
2625+
# This is different to test_zinterstore_nan_to_zero because of a quirk
2626+
# in redis. See https://github.com/antirez/redis/issues/3954.
2627+
self.assertEqual(self.redis.zscore('bar', 'x'), math.inf)
2628+
2629+
def test_zunionstore_nan_to_zero2(self):
26222630
self.zadd('foo', {'zero': 0})
26232631
self.zadd('foo2', {'one': 1})
26242632
self.zadd('foo3', {'one': 1})
@@ -2701,6 +2709,12 @@ def test_zinterstore_nokey(self):
27012709
with self.assertRaises(redis.ResponseError):
27022710
self.redis.zinterstore('baz', [], aggregate='MAX')
27032711

2712+
def test_zinterstore_nan_to_zero(self):
2713+
self.zadd('foo', {'x': math.inf})
2714+
self.zadd('foo2', {'x': math.inf})
2715+
self.redis.zinterstore('bar', {'foo': 1.0, 'foo2': 0.0})
2716+
self.assertEqual(self.redis.zscore('bar', 'x'), 0.0)
2717+
27042718
def test_zunionstore_nokey(self):
27052719
with self.assertRaises(redis.ResponseError):
27062720
self.redis.zunionstore('baz', [], aggregate='MAX')

0 commit comments

Comments
 (0)