DEV Community

Michael
Michael

Posted on

Detecting Chinese Characters in GBase 8a with REGEXP

To find rows containing Chinese characters in the China-domestically developed GBase 8a MPP database from GBASE, REGEXP is enough — provided the column uses the UTF8mb4 character set. This post shows two equivalent forms from the gbase client.

Environment

A test table tc with 7 rows mixing ASCII, digits and Chinese text:

gbase> select * from tc;
+--------------+
| name         |
+--------------+
| First        |
| 南大通用     |
| 123          |
| AAAA         |
| 中国         |
| 汉           |
| 666中国888   |
+--------------+
7 rows in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

Two detection methods

Both queries return the same 4 rows:

gbase> select * from testdb.tc where name REGEXP unhex('5be4b8802de9bea55d');
+--------------+
| name         |
+--------------+
| 南大通用     |
| 中国         |
| 汉           |
| 666中国888   |
+--------------+
4 rows in set (Elapsed: 00:00:00.01)

gbase> select * from tc where name REGEXP '[一-龥]';
+--------------+
| name         |
+--------------+
| 南大通用     |
| 中国         |
| 汉           |
| 666中国888   |
+--------------+
4 rows in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

The second form writes the CJK range [一-龥] (U+4E00–U+9FA5) directly into the SQL, which means the client connection's character set has to carry those bytes correctly. The first form avoids that entirely: unhex('5be4b8802de9bea55d') is the UTF8mb4 byte sequence of that same range — the opening bracket, the first character, the hyphen between the two Chinese characters, the last character and the closing bracket — expressed as hex, so no non-ASCII literal ever reaches the parser.

Looking ahead, the hex form is the safer default in a gbase cluster whenever SQL is passed through shells, scripts or drivers with uncertain encoding: it is pure ASCII, yet matches exactly the same range in the database. The trade-off is readability — keep a comment with the decoded range next to it, otherwise the next person to read the query has no idea what it matches.

Top comments (0)