Skip to content

bug: The ZRANGESTORE command returns an unexpected value when using the BYSCORE and REV parameter #3750

@luoming1224

Description

@luoming1224

Expected Behavior

If you use the following command to write a zset type key
zadd myzset 1 a 2 b 3 c 4 d 5 e
Then use the following zrangestore command to query the data, expecting to return a result of 3
zrangestore {myzset}2 myzset 5 3 BYSCORE REV

> zadd myzset 1 a 2 b 3 c 4 d 5 e
> (integer) 5
> zrangestore {myzset}2 myzset 5 3 BYSCORE REV
> (integer) 3

Current Behavior

Using go-redis actually returns a value of 0

Possible Solution

Due to the processing of SCORE and REV parameters in Redis, there is no need for special handling before the go redis client issues commands.
The bug fixing method only requires modifying the following code

func (z ZRangeArgs) appendArgs(args []interface{}) []interface{} {
	// For Rev+ByScore/ByLex, we need to adjust the position of <Start> and <Stop>.
	if z.Rev && (z.ByScore || z.ByLex) {
		args = append(args, z.Key, z.Stop, z.Start)
	} else {
		args = append(args, z.Key, z.Start, z.Stop)
	}
        ......
	return args
}

The modified code is as follows

func (z ZRangeArgs) appendArgs(args []interface{}) []interface{} {
	args = append(args, z.Key, z.Start, z.Stop)
        ......
	return args
}

Steps to Reproduce

Use the following code to reproduce

client := redis.NewClient(&redis.Options{
		Addr:     "xxxx:6379",
		Password: "xxxxxx",
	})
	defer client.Close()
	ctx := context.Background()

	src := "myzset"
	dst := "{myzset}2"

	client.Del(ctx, src)
	client.ZAdd(ctx, src,
		redis.Z{Score: 1, Member: "a"},
		redis.Z{Score: 2, Member: "b"},
		redis.Z{Score: 3, Member: "c"},
		redis.Z{Score: 4, Member: "d"},
		redis.Z{Score: 5, Member: "e"},
	)
	res, err := client.ZRangeStore(ctx, dst, redis.ZRangeArgs{
		Key:     src,
		Start:   5,
		Stop:    3,
		ByScore: true,
		Rev:     true,
	}).Result()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(res)

Context (Environment)

go-redis version: v9.18.0

Detailed Description

Possible Implementation

Metadata

Metadata

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions