Skip to content

fix(stream): align XPENDING ID parsing and PEL range with XRANGE (incomplete IDs, exclusive bounds)#3437

Open
songqing wants to merge 7 commits intoapache:unstablefrom
songqing:fix/xpending-parse-end-id
Open

fix(stream): align XPENDING ID parsing and PEL range with XRANGE (incomplete IDs, exclusive bounds)#3437
songqing wants to merge 7 commits intoapache:unstablefrom
songqing:fix/xpending-parse-end-id

Conversation

@songqing
Copy link
Copy Markdown
Contributor

@songqing songqing commented Apr 10, 2026

XPENDING extended form must use the same start/end ID rules as XRANGE:
parse the end with ParseRangeEnd so a bare millisecond includes all
sequence numbers in that ms; accept '(' for exclusive start/end and
honor them when scanning the pending entry list.

Refs: https://redis.io/docs/latest/commands/xpending/ ("in a similar way we do it with XRANGE")
Refs: https://redis.io/docs/latest/commands/xrange/ ("closed interval" / inclusive unless '(' prefix)

@git-hulk
Copy link
Copy Markdown
Member

@songqing Good catch.

Comment thread tests/gocase/unit/type/stream/stream_test.go
Copy link
Copy Markdown
Member

@jihuayu jihuayu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior of XPENDING seems to differ slightly from Redis. Could you take a look at these two failing test cases?

	t.Run("XPENDING with incomplete end ID should include the whole millisecond", func(t *testing.T) {
		streamKey := "xpending-incomplete-end-test"
		group := "grp"
		consumer := "con"
		ids := []string{"1-0", "1-1", "1-2"}

		require.NoError(t, rdb.Del(ctx, streamKey).Err())
		for i, id := range ids {
			require.NoError(t, rdb.XAdd(ctx, &redis.XAddArgs{Stream: streamKey, ID: id, Values: []string{"f", strconv.Itoa(i)}}).Err())
		}

		require.NoError(t, rdb.XGroupCreateMkStream(ctx, streamKey, group, "0").Err())
		_, err := rdb.XReadGroup(ctx, &redis.XReadGroupArgs{Group: group, Consumer: consumer, Streams: []string{streamKey, ">"}, Count: 10}).Result()
		require.NoError(t, err)

		result, err := rdb.Do(ctx, "XPENDING", streamKey, group, "1", "1", "10").Result()
		require.NoError(t, err)
		entries, ok := result.([]interface{})
		require.True(t, ok)
		require.Len(t, entries, 3, "XPENDING 1 1 should include every pending entry in millisecond 1, matching Redis")

		for i, entry := range entries {
			fields, ok := entry.([]interface{})
			require.True(t, ok)
			require.Len(t, fields, 4)
			gotID, ok := fields[0].(string)
			require.True(t, ok)
			require.Equal(t, ids[i], gotID)
			gotConsumer, ok := fields[1].(string)
			require.True(t, ok)
			require.Equal(t, consumer, gotConsumer)
			require.GreaterOrEqual(t, fields[2], int64(0))
			require.EqualValues(t, 1, fields[3])
		}

		require.NoError(t, rdb.Del(ctx, streamKey).Err())
	})

	t.Run("XPENDING with exclusive start should match Redis", func(t *testing.T) {
		streamKey := "xpending-exclusive-start-test"
		group := "grp"
		consumer := "con"
		ids := []string{"1-0", "1-1", "1-2"}

		require.NoError(t, rdb.Del(ctx, streamKey).Err())
		for i, id := range ids {
			require.NoError(t, rdb.XAdd(ctx, &redis.XAddArgs{Stream: streamKey, ID: id, Values: []string{"f", strconv.Itoa(i)}}).Err())
		}

		require.NoError(t, rdb.XGroupCreateMkStream(ctx, streamKey, group, "0").Err())
		_, err := rdb.XReadGroup(ctx, &redis.XReadGroupArgs{Group: group, Consumer: consumer, Streams: []string{streamKey, ">"}, Count: 10}).Result()
		require.NoError(t, err)

		result, err := rdb.Do(ctx, "XPENDING", streamKey, group, "(1-0", "+", "10").Result()
		require.NoError(t, err)
		entries, ok := result.([]interface{})
		require.True(t, ok)
		require.Len(t, entries, 2, "XPENDING (1-0 + 10 should exclude the first pending entry, matching Redis")

		for i, entry := range entries {
			fields, ok := entry.([]interface{})
			require.True(t, ok)
			require.Len(t, fields, 4)
			gotID, ok := fields[0].(string)
			require.True(t, ok)
			require.Equal(t, ids[i+1], gotID)
			gotConsumer, ok := fields[1].(string)
			require.True(t, ok)
			require.Equal(t, consumer, gotConsumer)
			require.GreaterOrEqual(t, fields[2], int64(0))
			require.EqualValues(t, 1, fields[3])
		}

		require.NoError(t, rdb.Del(ctx, streamKey).Err())
	})

@songqing songqing changed the title fix(stream): correct XPENDING end parsing and inclusive PEL range scan fix(stream): align XPENDING ID parsing and PEL range with XRANGE (incomplete IDs, exclusive bounds) Apr 10, 2026
@songqing
Copy link
Copy Markdown
Contributor Author

The behavior of XPENDING seems to differ slightly from Redis. Could you take a look at these two failing test cases?

ok, there still has another bug, fixed together.

@songqing songqing force-pushed the fix/xpending-parse-end-id branch from 007af9f to 41cab00 Compare April 10, 2026 04:52
@jihuayu jihuayu requested a review from git-hulk April 10, 2026 13:42
@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants