Globalization: charsets, collations and locales
Globalization covers internationalization (supporting many languages and regions) and localization (adapting to the language and conventions of one region). In CoraDB it is controlled by three settings:
| Setting | Decides | Set by |
|---|---|---|
| Charset (character set) | How characters are stored as bytes: ISO-8859-1, UTF-8 or EUC-KR | The database locale; per column, literal or expression with CHARSET |
| Collation | How strings are compared and sorted, and which casing rules apply | The database locale; per column, table or expression with COLLATE |
| Locale | Language and region: default charset and collation, month and day names, number format | coradb createdb <db> <locale>, for example en_US.utf8 |
| Topic | Page |
|---|---|
| Concepts, available collations, configuration guide | This page |
| Charset and collation of columns, tables, literals and expressions | Charset and collation rules |
| Installing locales and custom collations (LDML) | Locale setting |
| Time zone parameters and the time zone library | Time zone setting |
The e-commerce sample database uses the UTF-8 charset with the binary collation utf8_bin:
SELECT CHARSET('a') AS cs, COLLATION('a') AS co;| cs | co |
|---|---|
| utf8 | utf8_bin |
Character data
Section titled “Character data”Strings are stored in CHAR, VARCHAR (STRING) and ENUM columns, each with a charset and a collation.
The charset controls how characters are stored as bytes. CoraDB supports ISO-8859-1, UTF-8 and EUC-KR. UTF-8 covers Unicode code points up to 10FFFF, encoded in one to four bytes. The character "Ç", for example, is one byte (C7) in ISO-8859-1 and two bytes (C3 87) in UTF-8, and does not exist in EUC-KR:
SELECT HEX(_utf8'Ç') AS utf8_bytes, LENGTH(_utf8'Ç') AS chars, OCTET_LENGTH(_utf8'Ç') AS bytes;| utf8_bytes | chars | bytes |
|---|---|---|
| C387 | 1 | 2 |
The collation decides how strings compare: whether 'ABC' and 'abc' are equal (case-insensitive) or different (case-sensitive), how they sort, how LIKE matches, and the ranges of index scans. In CoraDB every collation belongs to one charset: utf8_en_ci and iso88591_en_ci are both case-insensitive, but for different charsets. A literal with a collation therefore also gets that collation's charset: 'a' COLLATE iso88591_en_ci means _iso88591'a' COLLATE iso88591_en_ci.
SELECT ('ABC' = 'abc') AS bin, ('ABC' COLLATE utf8_en_ci = 'abc') AS ci;| bin | ci |
|---|---|
| 0 | 1 |
CHAR values are padded to their length. The padding character is a space (ASCII 32, U+0020), except in EUC-KR, where it is the two-byte character A1 A1.
Related terms
Section titled “Related terms”-
Character set: A group of encoded symbols (giving a specific number to a certain symbol)
-
Collation: A set of rules for comparison of characters in the character set and for sorting data
-
Locale: A set of parameters that defines any special variant preferences such as number format, calendar format (month and day in characters), date/time format, and collation depending on the operator's language and country. Locale defines the linguistic localization. Character set of locale defines how the month in characters and other data are encoded. A locale identifier consists of at least a language identifier and a region identifier, and it is expressed as language[_territory][.codeset] (For example, Australian English using UTF-8 encoding is written as en_AU.UTF-8).
-
Unicode normalization: The specification by the Unicode character encoding standard where some sequences of code points represent essentially the same character. When normalization is enabled, CoraDB uses Normalization Form C (NFC, composed) for input and Normalization Form D (NFD, decomposed) for output, but it does not apply canonical equivalence.
For example, canonical equivalence is applied in general NFC rule so codepoint 212A (Kelvin K) is converted to codepoint 4B (ASCII code uppercase K). Since CoraDB does not perform the conversion by using the canonical equivalence rule to make normalization algorithm quicker and easier, it does not perform reverse-conversion, too.
-
Canonical equivalence: A basic equivalence between characters or sequences of characters, which cannot be visually distinguished when they are correctly rendered. For example, let's see 'Å' ('A' with an angstrom). 'Å' (Unicode U + 212B) and Latin 'A' (Unicode U + 00C5) have same A and different codepoints, however, the decomposed result is 'A' and U+030A, so it is canonical equivalence.
-
Compatibility equivalence: A weaker equivalence between characters or sequences of characters that represent the same abstract character. For example, let's see number '2' (Unicode U + 0032) and superscript '²'(Unicode U + 00B2). '²' is a different format of number '2', however, it is visually distinguished and has a different meaning, so it is not canonical equivalence. When normalizing '2²' with NFC, '2²' is maintained since it uses canonical equivalence. However, with NFKC, '²' is decomposed to '2' which is compatibility equivalence and then it can be recomposed to '22'. Unicode normalization of CoraDB does not apply the compatibility equivalence rule.
For explanation on Unicode normalization, see Unicode normalization. For more details, see http://unicode.org/reports/tr15/.
Normalization is off by default (unicode_input_normalization and unicode_output_normalization are no).
Locale attributes
Section titled “Locale attributes”Locale is defined by following attributes.
- Charset (codeset): How bytes are interpreted into single characters (Unicode codepoints)
- Collations: Among all collations defined in locale of LDML(UNICODE Locale Data Markup Language) file, the last one is the default collation. Locale data may contain several collations.
- Alphabet (casing rules): One locale data may have up 2 alphabets, one for identifier and one for user data. One locale data can have two types of alphabets.
- Calendar: Names of weekdays, months, day periods (AM/PM)
- Numbering settings: Symbols for digit grouping
- Text conversion data: For corasql conversion. Option.
- Unicode normalization data: Data converted by normalizing several characters with the same shape into one based on a specified rule. After normalization, characters with the same shape will have the same code value even though the locale is different. Each locale can activate/deactivate the normalization functionality.
Collation properties
Section titled “Collation properties”A collation is an assembly of information which defines an order for characters and strings. In CoraDB, collation has the following properties.
- Strength: This is a measure of how "different" basic comparable items (characters) are. This affects selectivity. In LDML files, collation strength is configurable and has four levels. For example a Case insensitive collation should be set with level = "secondary" (2) or "primary" (1).
- Whether it supports or not expansions and contractions
Each column has a collation, so when applying LOWER, UPPER functions the casing rules of locale which defines the collation's default language is used.
Depending on collation properties some CoraDB optimizations may be disabled for some collations:
- LIKE rewrite: is disabled for collations which maps several different character to the same weight (case insensitive collations for example) and for collations with expansions.
- Covering index scan: disabled for collations which maps several different character to the same weight (see Covering index).
For more information, see Collation settings impacting CoraDB features .
Collation naming rules
Section titled “Collation naming rules”The collation name in CoraDB follows the conversion:
<charset>_<lang specific>_<desc1>_<desc2>_...- <charset>: The full charset name as used by CoraDB. iso88591, utf8, euckr.
- <lang specific>: a region/language specific. The language code is expected as two characters; en, de, es, fr, it, ja, km, ko, tr, vi, zh, ro. "gen" if it does not address a specific language, but a more general sorting rule.
- <desc1>_<desc2>_...: They have the following meaning. Most of them apply only to LDML collations.
- ci: case insensitive In LDML, can be obtained using the settings: strength="secondary" caseLevel="off" caseFirst="off".
- cs: case sensitive; By default all collations are case sensitive. In LDML, can be obtained using at least: strength="tertiary".
- bin: it means that the sorting order under such collation is almost the same with the order of codepoints; If memory (byte) comparison is used, then almost the same result is obtained. Space character and EUC double-byte padding character are always sorted as zero in "bin" collation. No collations with such setting are currently configured in LDML (they are already available as built-in), but a similar one can be obtained using the maximum setting strength="quaternary" or strength="identical".
- ai: accent insensitive; this means that 'Á' is sorted the same as 'A'. Due to particularities of the UCA based algorithms, an accent insensitive collation is also a case insensitive collation. In LDML, can be obtained using: strength="primary".
- uca: this signals a UCA based collation; this is used only to differentiate such collations from similar built-in variants. All LDML collations are based on UCA, but in order to keep shorter names only two collations ( 'utf8_ko_cs_uca' , 'utf8_tr_cs_uca' ) have this description in their names, in order to differentiate them from 'utf8_ko_cs' and 'utf8_tr_cs' collations.
- exp: this collations use a full-word matching/compare algorithm, contrary to the rest of collations which use character-by-character compare. This collation uses a more complex algorithm, with multiple passes which is much slower, but may prove useful for alphabetical sorts. In LDML, the Expansion needs to be explicit by adding CoraDBExpansions="use".
- ab: accent backwards; it is particularity of French-Canadian sorting, where level 2 of UCA (used to store accents weights) is compared from end of string towards the beginning. This setting requires the expansion setting. The "backwards" setting allows for the following sorting:
- Normal Accent Ordering: cote < coté < côte < côté
- Backward Accent Ordering: cote < côte < coté < côté
- cbm: contraction boundary match; it is a particularity of collations with Expansion and Contraction and refers to how it behaves at string matching when a Contraction is found. Suppose the collation has defined the Contraction "ch"; then normally, the pattern "bac" will not match the string"bachxxx" But when the collation is configured to allow "matching the characters starting a contraction", the above matching will return a positive. Only one collation is configured in this manner - 'utf8_ja_exp_cbm' - Japanese sorting requires a lot of contractions.
The collation names are not dynamically generated. They are user defined (configured in LDML), and should reflect the settings of the collation.
The name of collation influences the internal numeric id of the collation. For instance, in CoraDB only 256 collations are allowed, and the numeric IDs are assigned as:
- 0 -31: built-in collations (for these collations the name and id are hard-coded)
- 32 - 46: LDML collations having "gen" as "language" part
- 47 - 255: the rest of LDML collations
Only the nine built-in collations plus binary are available in a new installation. The LDML collations in the table below become available after you compile the locales: copy coradb_locales.all.txt in $CORADB/conf to coradb_locales.txt and run the make_locale script (.sh on Linux, .bat on Windows); see Step 2: compiling locale.
To add newly compiled collations to an existing database, run coradb synccolldb <dbname>; see Synchronization of database collations with system collations.
With all locales compiled, CoraDB has the following collations. The collations of a database are listed by SHOW COLLATION or the db_collation catalog view; see Viewing collation information.
CoraDB collation
Section titled “CoraDB collation”| Collation | Locale for casing | Character range |
|---|---|---|
| iso88591_bin | en_US - English | ASCII + ISO88591 (C0-FE, except D7, F7) |
| iso88591_en_cs | en_US - English | ASCII + ISO88591 (C0-FE, except D7, F7) |
| iso88591_en_ci | en_US - English | ASCII + ISO88591 (C0-FE, except D7, F7) |
| utf8_bin | en_US - English | ASCII |
| euckr_bin | ko_KR - Korean, same as en_US - English | ASCII |
| utf8_en_cs | en_US - English | ASCII |
| utf8_en_ci | en_US - English | ASCII |
| utf8_tr_cs | tr_TR - Turkish | Turkish alphabet |
| utf8_ko_cs | ko_KR - Korean, same as en_US - English | ASCII |
| utf8_gen | de_DE - German, generic Unicode casing customized with German rules | All Unicode codepoints in range 0000-FFFF |
| utf8_gen_ai_ci | de_DE - German, generic Unicode casing customized with German rules | All Unicode codepoints in range 0000-FFFF |
| utf8_gen_ci | de_DE - German, generic Unicode casing customized with German rules | All Unicode codepoints in range 0000-FFFF |
| utf8_de_exp_ai_ci | de_DE - German, generic Unicode casing customized with German rules | All Unicode codepoints in range 0000-FFFF |
| utf8_de_exp | de_DE - German, generic Unicode casing customized with German rules | All Unicode codepoints in range 0000-FFFF |
| utf8_ro_cs | ro_RO - Romanian, same as generic Unicode casing | All Unicode codepoints in range 0000-FFFF |
| utf8_es_cs | es_ES - Spanish, same as generic Unicode casing | All Unicode codepoints in range 0000-FFFF |
| utf8_fr_exp_ab | fr_FR - French, same as generic Unicode casing | All Unicode codepoints in range 0000-FFFF |
| utf8_ja_exp | ja_JP - Japanese, same as generic Unicode casing | All Unicode codepoints in range 0000-FFFF |
| utf8_ja_exp_cbm | ja_JP - Japanese, same as generic Unicode casing | All Unicode codepoints in range 0000-FFFF |
| utf8_km_exp | km_KH - Cambodian, same as generic Unicode casing | All Unicode codepoints in range 0000-FFFF |
| utf8_ko_cs_uca | ko_KR - Korean, same as generic Unicode casing | All Unicode codepoints in range 0000-FFFF |
| utf8_tr_cs_uca | tr_TR - Turkish, generic Unicode casing customized with Turkish rules | All Unicode codepoints in range 0000-FFFF |
| utf8_vi_cs | vi_VN - Vietnamese, same as generic Unicode casing | All Unicode codepoints in range 0000-FFFF |
| binary | None (invariant to casing operations) | Any byte value (zero is nul-terminator) |
The Turkish casing rules changes the casing for character i,I,ı,İ. The German casing rules changes the casing for ß.
The nine collations iso88591_bin, iso88591_en_cs, iso88591_en_ci, utf8_bin, euckr_bin, utf8_en_cs, utf8_en_ci, utf8_tr_cs and utf8_ko_cs, and binary, are built in and available without running make_locale.
If a collation is included in more than one locale (.ldml) file, the locale for casing (default locale of collation) is the locale in which it is first included. The order of loading is the locales order from $CORADB/conf/coradb_locales.txt. The above locale casing for collations utf8_gen, utf8_gen_ci, utf8_gen_ai_ci, assumes the default order (alphabetical) in coradb_locales.txt, so the default locale for all generic LDML collations is de_DE (German).
Configuration guide for characters
Section titled “Configuration guide for characters”Database designers should take into account character data properties when designing the database structure. The following is the summarized guide when configuring aspects related to CoraDB character data.
Locale
Section titled “Locale”- By default, en_US gives best performance. If you have a plan to use only English, this is recommended.
- A CHAR column reserves the maximum bytes per character: 4 in UTF-8 and 3 in EUC-KR, so UTF-8 multiplies its storage by 4.
- If user string literals have different charset and collation from system, query strings will grow as the string literals are decorated with them.
- If localized (non-ASCII) characters will be used for identifiers, then use an .utf8 locale
- Once established the UTF-8 charset for DB, it is best to use a LDML locale (this ensures that identifier names containing most Unicode characters are correctly cased) than a system locale.
- Setting a locale affects also conversion functions(intl_date_lang, intl_number_lang).
- When you set the locale during creating DB, there should be no concern on charset and collation of string-literals or user tables columns; all of them can be changed at run-time (with CAST in queries) or ALTER .. CHANGE for a permanent change.
CHAR and VARCHAR
Section titled “CHAR and VARCHAR”- Generally, use VARCHAR if there are large variations in actual number of characters in user data.
- CHAR type is fixed length type. Therefore, Even if you store only English character in CHAR type, it requires 4 bytes storage in UTF-8 and 3 bytes in EUC-KR.
- The precision of columns refers to the number of characters (glyphs).
- After choosing precision, charset and collation should be set according to most used scenarios.
Choosing charset
Section titled “Choosing charset”- Even if your text contains non-ASCII character, use utf8 or euckr charsets only if application requires character counting, inserting, replacing.
- For CHAR data, the main concern should be storage requirement (4x or utf8, 3x for euckr).
- For both CHAR and VARCHAR data, there is some overhead when inserting/updating data: counting the precision (number of characters) of each instance is more consuming for non-ISO charsets.
- In queries, charset of expressions may be converted using CAST operator.
Choosing collation
Section titled “Choosing collation”- If no collation dependent operations are performed (string searching, sorting, comparisons, casing), then choose the bin collation for that charset or binary collation
- Collation may be easily overridden using CAST operator, and COLLATE modifier if charset is unchanged between original charset of expression and the new collation.
- Collation controls also the casing rules of strings
- Collations with expansions are slower, but are more flexible and they perform whole-word sorting
Normalization
Section titled “Normalization”- If your client applications send text data to CoraDB in decomposed form, then configure unicode_input_normalization = yes, so that CoraDB re-composes it and handles it in composed form
- If your client can handle data only in decomposed form, than set unicode_output_normalization = yes, so that CoraDB always sends in decomposed form.
- If the client "knows" both forms, then leave unicode_output_normalization = no
CAST vs COLLATE
Section titled “CAST vs COLLATE”- When building statements, the CAST operator is more costly than COLLATE modifier (even more when charset conversion occurs).
- COLLATE modifier does not add an additional execution operator; using COLLATE modifier should enhance execution speed over using CAST operator.
- COLLATE modifier can be used only when charset is not changed
Remarks
Section titled “Remarks”- Query plans printing: collation is not displayed in plans for results with late binding.
- Only the Unicode code-points in range 0000-FFFF (Basic Multilingual Plan) are normalized.
- Some locales use space character as separator for digit grouping (thousands, millions, ..). Space is allowed but not working properly in some cases of localized conversion from string to number.