Skip to content

fix LC_COLLATE issue #176 - #799

Closed
charsyam wants to merge 1 commit into
redis:unstablefrom
charsyam:strcoll
Closed

charsyam wants to merge 1 commit into
redis:unstablefrom
charsyam:strcoll

Conversation

@charsyam

Copy link
Copy Markdown
Contributor

sortCompare has a bug in sort.c

if server.sort_alpha is true, sort_alpha has nothing to do with sort_bypattern.

so it has to compare with strcoll instead of compareStringObject.

and add locale config parameter to set locale easily.

@charsyam charsyam mentioned this pull request Nov 29, 2012
@baina

baina commented Nov 29, 2012

Copy link
Copy Markdown

The Redis server itself stores all data as a binary objects, so it is not dependent on the encoding. The server will just store what is sent by the client (including UTF-8 chars).

Here are a few experiments:

$ echo téléphone | hexdump -C
00000000 74 c3 a9 6c c3 a9 70 68 6f 6e 65 0a |t..l..phone.|
c3a9 is the representation of the 'é' char.

$ redis-cli

set t téléphone
OK
get t
"t\xc3\xa9l\xc3\xa9phone"
Actually the data is correctly stored in the Redis server. However, when it is launched in a terminal, the Redis client interprets the output and applies the sdscatrepr function to transform non printable chars (whose definition is locale dependent, and may be broken for multibyte chars anyway).

A simple workaround is to launch redis-cli with the 'raw' option:

$ redis-cli --raw

get t
téléphone
Your own application will probably use one of the client libraries rather than redis-cli, so it should not be a problem in practice.

@charsyam

Copy link
Copy Markdown
Contributor Author

@bania. I don't think so. of course, redis server itself stores all data as a binary. but this problem is not about just storing.. for example. in Polish
redis> LPUSH test adam
(integer) 1
redis> LPUSH test łukasz
(integer) 2
redis> LPUSH test zuzanna
(integer) 3

redis> SORT test ALPHA

  1. "adam"
  2. "zuzanna"
  3. "\xc5\x82ukasz"

łukasz has to be ahead of zuzanna.
but SORT result is wrong. we expect below result.

redis> SORT test ALPHA

  1. "adam"
  2. "\xc5\x82ukasz"
  3. "zuzanna"

and my patch is about this problem :) Thank you.

@baina

baina commented Nov 29, 2012

Copy link
Copy Markdown

@charsyam You're right! Does your patch support redis hash sort(sorting by hash values)?
Thank you.

@charsyam

Copy link
Copy Markdown
Contributor Author

@baina I don't know. I just found miss cases of sortCompare in sortCommand.

@charsyam

charsyam commented Jan 9, 2013

Copy link
Copy Markdown
Contributor Author

@antirez I think this issue is obviously bug in special environment to use special letters.

@charsyam charsyam mentioned this pull request Nov 21, 2013
@yoav-steinberg

Copy link
Copy Markdown
Contributor

Was fixed with similar approach just using env variable instead of configuration. See 123b221.

oranagra added a commit that referenced this pull request Aug 21, 2022
Till now Redis officially supported tuning it via environment variable see #1074.
But we had other requests to allow changing it at runtime, see #799, and #11041.

Note that `strcoll()` is used as Lua comparison function and also for comparison of
certain string objects in Redis, which leads to a problem that, in different regions,
for some characters, the result may be different. Below is an example.
```
127.0.0.1:6333> SORT test alpha
1) "<"
2) ">"
3) ","
4) "*"
127.0.0.1:6333> CONFIG GET locale-collate
1) "locale-collate"
2) ""
127.0.0.1:6333> CONFIG SET locale-collate 1
(error) ERR CONFIG SET failed (possibly related to argument 'locale')
127.0.0.1:6333> CONFIG SET locale-collate C
OK
127.0.0.1:6333> SORT test alpha
1) "*"
2) ","
3) "<"
4) ">"
```
That will cause accidental code compatibility issues for Lua scripts and some
Redis commands. This commit creates a new config parameter to control the
local environment which only affects `Collate` category. Above shows how it
affects `SORT` command, and below shows the influence on Lua scripts.
```
127.0.0.1:6333> CONFIG GET locale-collate
1) " locale-collate"
2) "C"
127.0.0.1:6333> EVAL "return ',' < '*'" 0
(nil)
127.0.0.1:6333> CONFIG SET locale-collate ""
OK
127.0.0.1:6333> EVAL "return ',' < '*'" 0
(integer) 1
```

Co-authored-by: calvincjli <[email protected]>
Co-authored-by: Oran Agra <[email protected]>
enjoy-binbin pushed a commit to enjoy-binbin/redis that referenced this pull request Jul 31, 2023
Till now Redis officially supported tuning it via environment variable see redis#1074.
But we had other requests to allow changing it at runtime, see redis#799, and redis#11041.

Note that `strcoll()` is used as Lua comparison function and also for comparison of
certain string objects in Redis, which leads to a problem that, in different regions,
for some characters, the result may be different. Below is an example.
```
127.0.0.1:6333> SORT test alpha
1) "<"
2) ">"
3) ","
4) "*"
127.0.0.1:6333> CONFIG GET locale-collate
1) "locale-collate"
2) ""
127.0.0.1:6333> CONFIG SET locale-collate 1
(error) ERR CONFIG SET failed (possibly related to argument 'locale')
127.0.0.1:6333> CONFIG SET locale-collate C
OK
127.0.0.1:6333> SORT test alpha
1) "*"
2) ","
3) "<"
4) ">"
```
That will cause accidental code compatibility issues for Lua scripts and some
Redis commands. This commit creates a new config parameter to control the
local environment which only affects `Collate` category. Above shows how it
affects `SORT` command, and below shows the influence on Lua scripts.
```
127.0.0.1:6333> CONFIG GET locale-collate
1) " locale-collate"
2) "C"
127.0.0.1:6333> EVAL "return ',' < '*'" 0
(nil)
127.0.0.1:6333> CONFIG SET locale-collate ""
OK
127.0.0.1:6333> EVAL "return ',' < '*'" 0
(integer) 1
```

Co-authored-by: calvincjli <[email protected]>
Co-authored-by: Oran Agra <[email protected]>
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