Skip to content

2.6RC4 sort command is broken in Lua due to automatic resorting of output #545

Closed

Description

@rodcope

When calling a command like "sort mykey by nosort get foo:*" from a Lua script, the results are ordered incorrectly because Redis automatically resorts the resulting values in alphabetical order due to a little logic bug in scripting.c.

In scripting.c at the bottom of luaRedisGenericCommand(), I see this:
/* Skip this step if command is SORT but output was already sorted */
if (cmd->proc != sortCommand || server.sort_dontsort)
luaSortArray(lua);

The logic is a little backwards. It should probably be something like this:
/* Skip this step if command is SORT but output was already sorted */
if (cmd->proc != sortCommand && !server.sort_dontsort)
luaSortArray(lua);

Making this change fixed the problem for me.

Activity

  1. exogen commented on Sep 4, 2012

    @exogen

    Just chiming in that I ran into this issue today, and @rodcope's suggested change looks good.

  2. antirez commented on Sep 4, 2012

    @antirez
    Contributor

    Sorry I overlooked this... fixing it and adding a test to prevent further problems in this regard.

  3. antirez commented on Sep 4, 2012

    @antirez
    Contributor

    @exogen @rodcope sorry I can't reproduce the issue actually:

    redis 127.0.0.1:6379> lpush l 1 2 5 3 50 4 7 2
    (integer) 8
    redis 127.0.0.1:6379> eval "return redis.call('sort','l')" 0
    1) "1"
    2) "2"
    3) "2"
    4) "3"
    5) "4"
    6) "5"
    7) "7"
    8) "50"
    redis 127.0.0.1:6379> eval "return redis.call('sort','l')" 0
    1) "1"
    2) "2"
    3) "2"
    4) "3"
    5) "4"
    6) "5"
    7) "7"
    8) "50"
    redis 127.0.0.1:6379> eval "return redis.call('sort','l','by','x')" 0
    1) "1"
    2) "2"
    3) "2"
    4) "3"
    5) "4"
    6) "5"
    7) "50"
    8) "7"
    

    And the if statement appears to make sense since:

    if (cmd->proc != sortCommand || server.sort_dontsort)
    

    Will actually enter the true branch if:

    • the command is not sortCommand
    • OR, the sort_dontsort is true, so SORT did NOT sorted the output.

    Please can you show me how to reproduce? Thanks.

  4. exogen commented on Sep 4, 2012

    @exogen

    @antirez Thanks for looking into this! I should have been more specific: the problem becomes apparent when you GET multiple keys, expecting interleaved results, e.g.

    redis 127.0.0.1:6379> lpush list 1 2 5 3 50 4 7 2
    (integer) 8
    redis 127.0.0.1:6379> eval "return redis.call('sort', 'list', 'by', 'nosort', 'get', '#', 'get', 'weight:*')" 0
     1) (nil)
     2) (nil)
     3) (nil)
     4) (nil)
     5) (nil)
     6) (nil)
     7) (nil)
     8) (nil)
     9) "1"
    10) "2"
    11) "2"
    12) "3"
    13) "4"
    14) "5"
    15) "50"
    16) "7"
    

    I haven't added any weight keys because they're not necessary to demonstrate the problem, so they're all nil (fine). But you can see that the results should be interleaved like so:

     1) "1"
     2) (nil)
     3) "2"
     4) (nil)
     5) "2"
     6) (nil)
     7) "3"
     8) (nil)
     9) "4"
    10) (nil)
    11) "5"
    12) (nil)
    13) "50"
    14) (nil)
    15) "7"
    16) (nil)
    

    Instead they've been sorted so that the weights can't be associated with the original elements. Hope that makes sense!

  5. exogen commented on Sep 4, 2012

    @exogen

    Just adding that you can of course see the expected result order by issuing the command without Lua:

    redis 127.0.0.1:6379> sort list by nosort get # get weight:*
     1) "2"
     2) (nil)
     3) "7"
     4) (nil)
     5) "4"
     6) (nil)
     7) "50"
     8) (nil)
     9) "3"
    10) (nil)
    11) "5"
    12) (nil)
    13) "2"
    14) (nil)
    15) "1"
    16) (nil)
    
  6. antirez commented on Sep 4, 2012

    @antirez
    Contributor

    Ok now I see the problem... we are sorting a key-val thing as a linear array in this case.
    Fixing ASAP. Thanks! Also checking if this applies to other commands as well.

  7. antirez commented on Sep 4, 2012

    @antirez
    Contributor

    The issue is now fixed by commit 5ddee9b, Thanks!

    p.s. in the process the code is now simpler and faster.

  8. mtlima commented on Oct 9, 2014

    @mtlima

    I can actually still reproduce this in version 2.8.4:

    127.0.0.1:6391> rpush test 3 2 1
    (integer) 3
    127.0.0.1:6391> lrange test 0 -1

    1. "3"
    2. "2"
    3. "1"
      127.0.0.1:6391> sort test by nosort
    4. "3"
    5. "2"
    6. "1"
      127.0.0.1:6391> eval "return redis.call('sort', 'test', 'by', 'nosort');" 0
    7. "1"
    8. "2"
    9. "3"
  9. melo commented on Oct 17, 2014

    @melo
    Contributor

    I can confirm this behaviour on a ElasticCache Redis instance (2.8.6):

    > info server
    # Server
    redis_version:2.8.6
    redis_git_sha1:00000000
    redis_git_dirty:0
    redis_build_id:37f1586c49770bbe
    redis_mode:standalone
    os:Amazon ElastiCache
    arch_bits:64
    multiplexing_api:epoll
    gcc_version:0.0.0
    process_id:1
    tcp_port:6379
    uptime_in_seconds:2420387
    uptime_in_days:28
    

    Ran same commands as @mtlima, got the same results.

    Also reproduced on a 2.6.4:

    > info server
    # Server
    redis_version:2.6.4
    redis_git_sha1:00000000
    redis_mode:standalone
    os:Linux 2.6.32-71.el6.x86_64 x86_64
    arch_bits:64
    multiplexing_api:epoll
    gcc_version:4.4.6
    tcp_port:6380
    uptime_in_seconds:208869
    uptime_in_days:2
    

    Same behaviour.

  10. antirez commented on Oct 17, 2014

    @antirez
    Contributor

    Thanks for re-reporting. Reopening the issue.

  11. mattsta commented on Oct 17, 2014

    @mattsta
    Contributor

    This fixes it for the example above, but not sure if it breaks anything else yet:

    diff --git a/src/sort.c b/src/sort.c
    index fedf0cf..d0939bb 100644
    --- a/src/sort.c
    +++ b/src/sort.c
    @@ -297,7 +297,6 @@ void sortCommand(redisClient *c) {
             (dontsort && sortval->type != REDIS_ZSET))
         {
             /* Force ALPHA sorting */
    -        dontsort = 0;
             alpha = 1;
             sortby = NULL;
         }
  12. added 2 commits that reference this issue on Dec 11, 2014
    77bbc0d
    e945a54
  13. added a commit that references this issue on Oct 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions