You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
2.6RC4 sort command is broken in Lua due to automatic resorting of output #545
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);
@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.
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:
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.
Activity
exogen commentedon Sep 4, 2012
Just chiming in that I ran into this issue today, and @rodcope's suggested change looks good.
antirez commentedon Sep 4, 2012
Sorry I overlooked this... fixing it and adding a test to prevent further problems in this regard.
antirez commentedon Sep 4, 2012
@exogen @rodcope sorry I can't reproduce the issue actually:
And the if statement appears to make sense since:
Will actually enter the true branch if:
Please can you show me how to reproduce? Thanks.
exogen commentedon Sep 4, 2012
@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.
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:
Instead they've been sorted so that the weights can't be associated with the original elements. Hope that makes sense!
exogen commentedon Sep 4, 2012
Just adding that you can of course see the expected result order by issuing the command without Lua:
antirez commentedon Sep 4, 2012
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.
Scripting: Force SORT BY constant determinism inside SORT itself.
Scripting: Force SORT BY constant determinism inside SORT itself.
antirez commentedon Sep 4, 2012
The issue is now fixed by commit 5ddee9b, Thanks!
p.s. in the process the code is now simpler and faster.
mtlima commentedon Oct 9, 2014
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
127.0.0.1:6391> sort test by nosort
127.0.0.1:6391> eval "return redis.call('sort', 'test', 'by', 'nosort');" 0
melo commentedon Oct 17, 2014
I can confirm this behaviour on a ElasticCache Redis instance (2.8.6):
Ran same commands as @mtlima, got the same results.
Also reproduced on a 2.6.4:
Same behaviour.
antirez commentedon Oct 17, 2014
Thanks for re-reporting. Reopening the issue.
mattsta commentedon Oct 17, 2014
This fixes it for the example above, but not sure if it breaks anything else yet:
Fix zero-ordering SORT when called against lists
Fix zero-ordering SORT when called against lists
Merge pull request redis#545 from felixhandte/c++-alignas