Skip to content

Redis "skip sorting" then store in SORT actually sorts stored results #2079

Closed

Description

Redis 2.8.17 on a mac, installed via Homebrew.

I would expect to see testb have keys in the same order as those returned by sort testa by nosort, where the nosort key doesn't exist, but the documentation doesn't say anything either way.

127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> lpush testa 2 1 4 3 5
(integer) 5
127.0.0.1:6379[1]> lrange testa 0 -1
1) "5"
2) "3"
3) "4"
4) "1"
5) "2"
127.0.0.1:6379[1]> sort testa by nosort
1) "5"
2) "3"
3) "4"
4) "1"
5) "2"
127.0.0.1:6379[1]> sort testa by nosort store testb
(integer) 5
127.0.0.1:6379[1]> lrange testb 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"

Also, applying a limit instead seems to happen after whatever default sort has been applied to the data being stored.

127.0.0.1:6379[1]> sort testa by nosort limit 0 3 store testb
(integer) 3
127.0.0.1:6379[1]> lrange testb 0 -1
1) "1"
2) "2"
3) "3"

I may be misunderstanding things here, but if the two options are exclusive then the documentation could be cleaned up, or an error raised when they're seen.

Activity

  1. mattsta commented on Oct 22, 2014

    @mattsta
    Contributor

    A code comment mentions:

        /* For the STORE option, or when SORT is called from a Lua script,
         * we want to force a specific ordering even when no explicit ordering
         * was asked (SORT BY nosort). This guarantees that replication / AOF
         * is deterministic.
         *
         * However in the case 'dontsort' is true, but the type to sort is a
         * sorted set, we don't need to do anything as ordering is guaranteed
         * in this special case. */

    So, SORT + NOSORT + STORE = stored, but sorted.

    The problem here is: SORT takes one of three argument types (list, set, zset). Of those three types, the SET type has no ordering, so we must sort before storing (or else when the SORT+STORE happens on the replica, the replica would have a different value).

    As for the other two types: ZSET has a native ordering (sorted by score) and LIST has a native ordering (ordered by list position).

    I believe we can change the check from "force sort everything when SORT+NOSORT+STORE" to "sort only SET when SORT+NOSORT+STORE"

    We only need to actually sort if we are using a SET. LIST and ZSET are fine unsorted, since they will be stable across calls/servers in a fixed order.

    This changes the code to (*nb: also need to update the comment above this code):

    diff --git a/src/sort.c b/src/sort.c
    index fedf0cf..46e7851 100644
    --- a/src/sort.c
    +++ b/src/sort.c
    @@ -293,8 +293,7 @@ void sortCommand(redisClient *c) {
          * However in the case 'dontsort' is true, but the type to sort is a
          * sorted set, we don't need to do anything as ordering is guaranteed
          * in this special case. */
    -    if ((storekey || c->flags & REDIS_LUA_CLIENT) &&
    -        (dontsort && sortval->type != REDIS_ZSET))
    +    if ((dontsort && sortval->type == REDIS_SET))
         {
             /* Force ALPHA sorting */
             dontsort = 0;

    The change says "force sorting if the user asked for NOSORT and the source is a SET," otherwise we continue to do exactly what the user requested.

    What about the REDIS_LUA_CLIENT check? I don't think that's needed anymore since the only sort order we care about is forcing a SET to have a known ordering. The other two types (LIST, ZSET) will always have the same nosort order because they are stored by position or score.

    Also, with the change above, we can now:

    127.0.0.1:9999> sort testa by nosort limit 0 3 store testb
    (integer) 3
    127.0.0.1:9999> lrange testb 0 -1
    1) "5"
    2) "3"
    3) "4"

    All tests pass with the change above. If there's any more logic behind not changing it this way (maybe around replication problems or reproducible script problems), I'd love to hear it.

    This is also the exact same problem and the same fix for https://github.com/antirez/redis/issues/545

  2. mattsta commented on Oct 22, 2014

    @mattsta
    Contributor

    Alternatively, we could remove all of that "special sort exception" code and return an error if the user asks to sort a SET with NOSORT (since the return contents won't be stable across calls/replication/scripting).

    The deny-all-set-nosort fix is also simple, but it breaks existing tests since we check for "SET automatically sorts even when we request no sorting:"

    diff --git a/src/sort.c b/src/sort.c
    index fedf0cf..ce4d14c 100644
    --- a/src/sort.c
    +++ b/src/sort.c
    @@ -249,7 +249,13 @@ void sortCommand(redisClient *c) {
                 /* If the BY pattern does not contain '*', i.e. it is constant,
                  * we don't need to sort nor to lookup the weight keys. */
                 if (strchr(c->argv[j+1]->ptr,'*') == NULL) {
    -                dontsort = 1;
    +                if (sortval->type == REDIS_SET) {
    +                    addReplyError(c,"Must specify sort order for SET source.");
    +                    syntax_error++;
    +                    break;
    +                } else {
    +                    dontsort = 1;
    +                }
                 } else {
                     /* If BY is specified with a real patter, we can't accept
                      * it in cluster mode. */
  3. mattsta commented on Oct 22, 2014

    @mattsta
    Contributor

    anyone asking for ordering on it can't reasonably complain at anything they get back as long as it contains all the keys...

    Great point :)

    So, the first option remains the best unless anybody can think of a reason why removing restrictions from the force-sort condition will break replication or scripting consistency.

  4. added 2 commits that reference this issue on Dec 11, 2014
    77bbc0d
    e945a54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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