Skip to content

[BUG] SRANDMEMBER with a negative count should return an Array when using RESP3 #8503

@Harrison88

Description

@Harrison88

Describe the bug

SRANDMEMBER with a negative count is supposed to return elements from the set, with replacement, so it's possible to repeat elements. When using RESP3, the type returned is a Set. That's correct when count is positive and elements can't be repeated, but incorrect when count is negative. I believe when count is negative, an Array should be returned.

The current behavior causes varying results depending on the parser used. Hiredis just returns a "set" with duplicate elements, since there is no Set type that enforces the uniqueness of elements. Justredis returns a Set with only unique elements. My project doesn't return anything at all, because the way it parses a set gets stuck when it contains duplicate elements.

To reproduce

On redis-cli:

127.0.0.1:6379> HELLO 3
1# "server" => "redis"
2# "version" => "255.255.255"
3# "proto" => (integer) 3
4# "id" => (integer) 1273
5# "mode" => "standalone"
6# "role" => "master"
7# "modules" => (empty array)

127.0.0.1:6379> SADD set_key a b c
(integer) 3

127.0.0.1:6379> SRANDMEMBER set_key -4
1~ "c"
2~ "b"
3~ "b"
4~ "c"

Expected behavior

When count is negative, SRANDMEMBER should return an Array.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions