Commit d9e28bcf authored by Pieter Noordhuis's avatar Pieter Noordhuis
Browse files

Fix ZUNIONSTORE/ZINTERSTORE to never store a NaN score.

When +inf and -inf are added, the result is NaN. We don't want NaN
scores in a sorted set, so agreed on the result of this operation being
zero.
parent cbf7e107
...@@ -541,6 +541,10 @@ int qsortCompareZsetopsrcByCardinality(const void *s1, const void *s2) { ...@@ -541,6 +541,10 @@ int qsortCompareZsetopsrcByCardinality(const void *s1, const void *s2) {
inline static void zunionInterAggregate(double *target, double val, int aggregate) { inline static void zunionInterAggregate(double *target, double val, int aggregate) {
if (aggregate == REDIS_AGGR_SUM) { if (aggregate == REDIS_AGGR_SUM) {
*target = *target + val; *target = *target + val;
/* The result of adding two doubles is NaN when one variable
* is +inf and the other is -inf. When these numbers are added,
* we maintain the convention of the result being 0.0. */
if (isnan(*target)) *target = 0.0;
} else if (aggregate == REDIS_AGGR_MIN) { } else if (aggregate == REDIS_AGGR_MIN) {
*target = val < *target ? val : *target; *target = val < *target ? val : *target;
} else if (aggregate == REDIS_AGGR_MAX) { } else if (aggregate == REDIS_AGGR_MAX) {
......
...@@ -433,6 +433,30 @@ start_server {tags {"zset"}} { ...@@ -433,6 +433,30 @@ start_server {tags {"zset"}} {
list [r zinterstore zsetc 2 zseta zsetb aggregate max] [r zrange zsetc 0 -1 withscores] list [r zinterstore zsetc 2 zseta zsetb aggregate max] [r zrange zsetc 0 -1 withscores]
} {2 {b 2 c 3}} } {2 {b 2 c 3}}
foreach cmd {ZUNIONSTORE ZINTERSTORE} {
test "$cmd with +inf/-inf scores" {
r zadd zsetinf1 +inf key
r zadd zsetinf2 +inf key
r $cmd zsetinf3 2 zsetinf1 zsetinf2
assert_equal inf [r zscore zsetinf3 key]
r zadd zsetinf1 -inf key
r zadd zsetinf2 +inf key
r $cmd zsetinf3 2 zsetinf1 zsetinf2
assert_equal 0 [r zscore zsetinf3 key]
r zadd zsetinf1 +inf key
r zadd zsetinf2 -inf key
r $cmd zsetinf3 2 zsetinf1 zsetinf2
assert_equal 0 [r zscore zsetinf3 key]
r zadd zsetinf1 -inf key
r zadd zsetinf2 -inf key
r $cmd zsetinf3 2 zsetinf1 zsetinf2
assert_equal -inf [r zscore zsetinf3 key]
}
}
tags {"slow"} { tags {"slow"} {
test {ZSETs skiplist implementation backlink consistency test} { test {ZSETs skiplist implementation backlink consistency test} {
set diff 0 set diff 0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment