Skip to content

Fix zero-ordering SORT when called against lists - #2094

Merged
antirez merged 1 commit into
redis:unstablefrom
mattsta:nosort
Dec 11, 2014
Merged

antirez merged 1 commit into
redis:unstablefrom
mattsta:nosort

Conversation

@mattsta

@mattsta mattsta commented Oct 23, 2014

Copy link
Copy Markdown
Contributor

People mostly use SORT against lists, but our prior
behavior was pretending lists were an unordered bag
requiring a forced-sort when no sort was requested.

We can just use the native list ordering to ensure
consistency across replicaion and scripting calls.

Tests added too (they fail before this commit).

Closes #2079
Closes #545 (again)

People mostly use SORT against lists, but our prior
behavior was pretending lists were an unordered bag
requiring a forced-sort when no sort was requested.

We can just use the native list ordering to ensure
consistency across replicaion and scripting calls.

Closes redis#2079
Closes redis#545 (again)
@antirez

antirez commented Oct 30, 2014

Copy link
Copy Markdown
Contributor

Hello Matt, from the code I see you changed the condition from:

 if ((storekey || c->flags & REDIS_LUA_CLIENT) &&
        (dontsort && sortval->type != REDIS_ZSET))

to

    if ((dontsort && sortval->type == REDIS_SET))

So before the sort was performed only if the following set of conditions where met at the same time:

  1. There is a STORE target or the command was invoked by Lua.
  2. AND at the same time, sorting was disabled and the sorted key was not a zset.

Now the new condition is:

  1. There is no explicit sorting and we are sorting a set.

If we compute the difference between the two, when the new code will force the sorting with the new condition, where the old condition would not sort it?

  1. When the type is a Set, no STORE option is used, and a normal client is invoking the command.

When instead the new code path would skip sorting while the old one would sort it?

  1. When the client is Lua or STORE is used, but the type of the key is zset or list.

I think that skipping sorting for Lua and STORE if the type is a list is good, but now we are sorting the output even when not needed, which is, when a Set is fetched via SORT by a normal client not using store.

So my proposal is to change the if condition into the following:

 if ((dontsort && sortval->type == REDIS_SET && (storekey || c->flags & REDIS_LUA_CLIENT)))

This way we still skip sorting for Lists and Sorted Sets, but we avoid sorting for sets when there is no need to do so and we are free to return unsorted elements.

@mattsta

mattsta commented Oct 30, 2014

Copy link
Copy Markdown
Contributor Author

proposal is to change the if condition into the following: [add always sort if LUA]

That certainly works too.

we avoid sorting for sets when there is no need to do so

The key word there is "need" — it seems confusing if a command returns different results in scripting vs. not-scripting. Forcing "always sort type SET" removes potential user confusion. 😕

@antirez

antirez commented Dec 11, 2014

Copy link
Copy Markdown
Contributor

Hello Matt, this is my reasoning and why I think it is important to don't sort in the case it's a normal client:

  1. The command returns an unsorted output for API.
  2. This should create no confusion, we have plenty of commands like that: all the Sets and Hash commands returning multiple items are like that.
  3. On top of 1+2, to add a performance hit (very serious, sorting is very expensive) to return sorted stuff when API says unsorted, is a waste of resources.

That's why I'm more inclined to change the conditional to what I proposed.

@antirez

antirez commented Dec 11, 2014

Copy link
Copy Markdown
Contributor

Btw, merging your original commit and doing the trivial change myself: it is easy and captures the full story given that here there were two possibilities :-)

antirez added a commit that referenced this pull request Dec 11, 2014
Fix zero-ordering SORT when called against lists
@antirez
antirez merged commit c50693d into redis:unstable Dec 11, 2014
antirez added a commit that referenced this pull request Dec 11, 2014
antirez added a commit that referenced this pull request Dec 11, 2014
@antirez

antirez commented Dec 11, 2014

Copy link
Copy Markdown
Contributor

Done, thank you. Back ported to 2.8 and testing as well.

antirez added a commit that referenced this pull request Dec 11, 2014
@mattsta
mattsta deleted the nosort branch December 11, 2014 15:44
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.

Redis "skip sorting" then store in SORT actually sorts stored results 2.6RC4 sort command is broken in Lua due to automatic resorting of output

2 participants