Help Center/ GaussDB/ Centralized_8.x/ SQL Reference/ Character Sets and Collations/ Rules for Combining Character Sets and Collations
Updated on 2024-06-03 GMT+08:00

Rules for Combining Character Sets and Collations

In B-compatible database (sql_compatibility = 'B'), if b_format_version is set to '5.7' and b_format_dev_version is set to 's2', expressions with different character sets and collations are processed based on certain priorities to determine the collations used for character string comparison and the character sets of the expressions.

Collation priority

The priorities of different expressions in descending order are as follows:

  • The COLLATE syntax has the highest priority.

  • Expressions with collation conflicts (for example, two character strings with different collations).

  • Columns of data types that support collation, user-defined variables, stored procedure parameters, and CASE expressions.

  • Specific system functions (such as version() and opengauss_version() function expressions).

  • String constants and bound parameters.

  • NULL expression.

  • If a data type of an expression does not support collation, this expression has the lowest priority.

When the collations of two expressions are different, the collation of the expression with the highest priority is used.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
gaussdb=#  CREATE TABLE t_utf8(c1 varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin);
gaussdb=# INSERT INTO t_utf8 VALUES('STRING');

-- If the utf8mb4_bin collation is used for comparison, the result is false.
gaussdb=# SELECT c1 = 'string' AS result FROM t_utf8;
 result
--------
 f
(1 row)

-- If the utf8mb4_general_ci collation is used for comparison, the result is true.
gaussdb=# SELECT c1 = 'string' COLLATE utf8mb4_general_ci AS result FROM t_utf8;
 result
--------
 t
(1 row)

-- Define the collation of the bound parameter $1 as collation_connection.
gaussdb=# PREPARE test_collation(text) AS SELECT c1 = $1 AS result FROM t_utf8;

-- The collation of the bound parameter is at the same level as that of the string constant. Even if the input expression contains an explicit collation, the collation of c1 is still used for comparison.
gaussdb=# EXECUTE test_collation('string' COLLATE utf8mb4_general_ci);
 result
--------
 f
(1 row)

-- The CASE expression is at the same level as the c1 column. Even if the expression contains an explicit collation, the collation of the c1 column is still used for comparison. The two collations are different. "same level" is displayed.
gaussdb=# SELECT CASE 'string' COLLATE utf8mb4_general_ci WHEN c1 THEN 'different level' ELSE 'same level' END AS result FROM t_utf8;
   result
------------
 same level
(1 row)

-- The IN subquery has the same level as the c1 column. Even if the expression contains an explicit collation, the collation of c1 is still used for comparison. The two collations are different.
gaussdb=# SELECT c1 FROM t_utf8 WHERE c1 in (SELECT 'string' COLLATE utf8mb4_general_ci);
 c1
----
(0 rows)

If the collations of two expressions with the same priority are different, the following processing method is used:

  • If the two character sets are the same, the collation suffixed with _bin is preferred.
  • If the two character sets are the same, the default collation is not preferred.
  • If the preceding conditions are not met, the two expressions are marked as a collation conflict, and the collations are marked as invalid.
    • If a conflict occurs because the COLLATE syntax specifies different collations for the same character set, an exception is displayed.

    • If the two conflicting collations are supported in B-compatible mode (sql_compatibility is set to 'B'), an exception occurs.
    • If an invalid collation is used for sorting operations (such as > and <), an exception occurs.

      During equivalent comparison of character strings, if the collation is invalid, the character strings are compared as binary values.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
gaussdb=# CREATE TABLE t_utf8mb4_charset(
    c_utf8_bin varchar(16) character set utf8mb4 collate utf8mb4_bin,
    c_utf8_uni varchar(16) character set utf8mb4 collate utf8mb4_unicode_ci,
    c_utf8_gen varchar(16) character set utf8mb4 collate utf8mb4_general_ci);
gaussdb=# INSERT INTO t_utf8mb4_charset VALUES('STRING', 'String', 'string');

-- The utf8mb4_bin collation is preferentially used for comparison. The result is false.
gaussdb=# SELECT c_utf8_bin = c_utf8_uni FROM t_utf8mb4_charset;

-- Collation conflict. Binary comparison is performed, and the result is false.
gaussdb=# SELECT c_utf8_uni = c_utf8_gen FROM t_utf8mb4_charset;

-- Conflict of the explicitly specified collation. An exception is reported.
gaussdb=# SELECT c_utf8_uni COLLATE utf8mb4_unicode_ci = c_utf8_gen COLLATE utf8mb4_general_ci FROM t_utf8mb4_charset;
  • Only the character sets of objects and expressions of the string type (excluding "char", name, and clob) can be different from those of the database.

  • Data types such as ARRAY, XML, JSON, and TSVECTOR contain text data. The character sets in the text data of the objects and expressions of these data types must be the character sets of the database.
  • According to the rules for combining character sets and collations, the character sets corresponding to C, POSIX, and DEFAULT collations are server_encoding.