hrandfield and zrandmember with count should return emptyarray when key does not exist. (#9178)

due to a copy-paste bug, it used to reply with null response rather than empty array.
this commit includes new tests that are looking at the RESP response directly in
order to be able to tell the difference between them.

Co-authored-by: Oran Agra <oran@redislabs.com>
(cherry picked from commit a418a2d3fc0250c094802d7e8ea64d96eedfda07)
This commit is contained in:
Binbin 2021-07-05 15:41:57 +08:00 committed by Oran Agra
parent 432e056659
commit c6b3966d02
5 changed files with 47 additions and 4 deletions

View File

@ -986,7 +986,7 @@ void hrandfieldWithCountCommand(client *c, long l, int withvalues) {
int uniq = 1; int uniq = 1;
robj *hash; robj *hash;
if ((hash = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp])) if ((hash = lookupKeyReadOrReply(c,c->argv[1],shared.emptyarray))
== NULL || checkType(c,hash,OBJ_HASH)) return; == NULL || checkType(c,hash,OBJ_HASH)) return;
size = hashTypeLength(hash); size = hashTypeLength(hash);
@ -1175,7 +1175,7 @@ void hrandfieldWithCountCommand(client *c, long l, int withvalues) {
} }
} }
/* HRANDFIELD [<count> WITHVALUES] */ /* HRANDFIELD key [<count> [WITHVALUES]] */
void hrandfieldCommand(client *c) { void hrandfieldCommand(client *c) {
long l; long l;
int withvalues = 0; int withvalues = 0;

View File

@ -3992,7 +3992,7 @@ void zrandmemberWithCountCommand(client *c, long l, int withscores) {
int uniq = 1; int uniq = 1;
robj *zsetobj; robj *zsetobj;
if ((zsetobj = lookupKeyReadOrReply(c, c->argv[1], shared.null[c->resp])) if ((zsetobj = lookupKeyReadOrReply(c, c->argv[1], shared.emptyarray))
== NULL || checkType(c, zsetobj, OBJ_ZSET)) return; == NULL || checkType(c, zsetobj, OBJ_ZSET)) return;
size = zsetLength(zsetobj); size = zsetLength(zsetobj);
@ -4177,7 +4177,7 @@ void zrandmemberWithCountCommand(client *c, long l, int withscores) {
} }
} }
/* ZRANDMEMBER [<count> WITHSCORES] */ /* ZRANDMEMBER key [<count> [WITHSCORES]] */
void zrandmemberCommand(client *c) { void zrandmemberCommand(client *c) {
long l; long l;
int withscores = 0; int withscores = 0;

View File

@ -72,6 +72,19 @@ start_server {tags {"hash"}} {
r hrandfield nonexisting_key 100 r hrandfield nonexisting_key 100
} {} } {}
# Make sure we can distinguish between an empty array and a null response
r readraw 1
test "HRANDFIELD count of 0 is handled correctly - emptyarray" {
r hrandfield myhash 0
} {*0}
test "HRANDFIELD with <count> against non existing key - emptyarray" {
r hrandfield nonexisting_key 100
} {*0}
r readraw 0
foreach {type contents} " foreach {type contents} "
hashtable {{a 1} {b 2} {c 3} {d 4} {e 5} {6 f} {7 g} {8 h} {9 i} {[randstring 70 90 alpha] 10}} hashtable {{a 1} {b 2} {c 3} {d 4} {e 5} {6 f} {7 g} {8 h} {9 i} {[randstring 70 90 alpha] 10}}
ziplist {{a 1} {b 2} {c 3} {d 4} {e 5} {6 f} {7 g} {8 h} {9 i} {10 j}} " { ziplist {{a 1} {b 2} {c 3} {d 4} {e 5} {6 f} {7 g} {8 h} {9 i} {10 j}} " {

View File

@ -403,10 +403,27 @@ start_server {
assert {[lsort $union] eq [lsort $content]} assert {[lsort $union] eq [lsort $content]}
} }
test "SRANDMEMBER count of 0 is handled correctly" {
r srandmember myset 0
} {}
test "SRANDMEMBER with <count> against non existing key" { test "SRANDMEMBER with <count> against non existing key" {
r srandmember nonexisting_key 100 r srandmember nonexisting_key 100
} {} } {}
# Make sure we can distinguish between an empty array and a null response
r readraw 1
test "SRANDMEMBER count of 0 is handled correctly - emptyarray" {
r srandmember myset 0
} {*0}
test "SRANDMEMBER with <count> against non existing key - emptyarray" {
r srandmember nonexisting_key 100
} {*0}
r readraw 0
foreach {type contents} { foreach {type contents} {
hashtable { hashtable {
1 5 10 50 125 50000 33959417 4775547 65434162 1 5 10 50 125 50000 33959417 4775547 65434162

View File

@ -1681,6 +1681,19 @@ start_server {tags {"zset"}} {
r zrandmember nonexisting_key 100 r zrandmember nonexisting_key 100
} {} } {}
# Make sure we can distinguish between an empty array and a null response
r readraw 1
test "ZRANDMEMBER count of 0 is handled correctly - emptyarray" {
r zrandmember myzset 0
} {*0}
test "ZRANDMEMBER with <count> against non existing key - emptyarray" {
r zrandmember nonexisting_key 100
} {*0}
r readraw 0
foreach {type contents} " foreach {type contents} "
skiplist {1 a 2 b 3 c 4 d 5 e 6 f 7 g 7 h 9 i 10 [randstring 70 90 alpha]} skiplist {1 a 2 b 3 c 4 d 5 e 6 f 7 g 7 h 9 i 10 [randstring 70 90 alpha]}
ziplist {1 a 2 b 3 c 4 d 5 e 6 f 7 g 7 h 9 i 10 j} " { ziplist {1 a 2 b 3 c 4 d 5 e 6 f 7 g 7 h 9 i 10 j} " {