Repository navigation
Commit 6ca5528
authored
Fix character set handling in the MySQL lexer and query corpus (#609)
This PR fixes character set handling in the `mysql-parser` package, in
both the lexer and the query corpus. It was split out from the MySQL
version support work, but it doesn't depend on it.
## Lexer
**The lexer now accepts two kinds of names that MySQL accepts:**
- **4-byte characters in unquoted names, like `🐬` or `𩸽`.** The lexer
stopped at U+FFFF, the limit in the MySQL documentation, but MySQL
accepts any multibyte character of the connection character set. This
was checked in the MySQL 5.5 and 5.7 lexer sources and on MySQL 8.4.
- **Non-ASCII characters in names after `@`.** The scan stopped at the
first non-ASCII byte, so `SET @é = 1` failed to parse, and `SELECT @x🐬`
would read `@x` with the alias `🐬`. MySQL reads these as one name. The
same scan reads hosts like `u@é.example.com`.
**Some things don't change:**
- **Names that MySQL can't store:** MySQL converts names to `utf8mb3`,
which can't store 4-byte characters. A quoted alias like `'🐬'` fails
since MySQL 8.0.17, and unquoted and backticked names become `?`, so
tables `🐬` and `😀` collide. The lexer follows the syntax and leaves this
to the code that uses the parser.
- **Invalid UTF-8:** MySQL accepts it in names, but the lexer keeps
rejecting it.
- **The driver:** Its own lexer in `mysql-on-sqlite` still rejects
4-byte names and still stops at non-ASCII bytes after `@`.
## Query corpus extractor
The extractor decoded every MySQL test file as Latin-1, but most of them
are UTF-8. Non-ASCII text came out double-encoded (`geprüft` became
`geprüft`), `0x85` bytes inside UTF-8 characters split lines, and
sections using `--character_set` were decoded twice.
**It now reads test files as MySQL reads them:**
- **Client character set:** Each line uses the character set that the
test selects with `SET NAMES`, `SET CHARACTER SET`, or
`character_set_client`. The default comes from the test suite's
`default_client.cnf`, which is utf8mb4 on MySQL 8.4.
- **String literals:** A literal with an introducer, like `_koi8r'...'`,
is read in that character set.
- **Invalid text:** Text that isn't valid in these character sets is
read as MySQL's latin1, with the same result on all PHP versions.
- **`--character_set`:** This mysqltest command is ignored. It only
affects connections that a test opens later, and no query depends on
that.
- **Line endings:** Only LF and CRLF end a line, as in mysqltest.
**Two edge cases are expected:**
- **Queries that look double-encoded:** A test that sends UTF-8 bytes
after `SET NAMES latin1` is read as latin1, because that's how MySQL
reads it. Queries like `"óra"` look wrong for this reason.
- **Introducers inside strings:** The extractor doesn't track strings
here, so `"_latin1'é'"` would be read as an introducer. No query
extracted from the MySQL 8.4.10 or 5.7.44 tests does this.
**The extractor also has some cleanup:**
- The decoding moved to its own file, with tests.
- The cp1250 character set has its own table, because mbstring doesn't
support it.
- The deprecated `utf8_encode()` and `HTML-ENTITIES` conversion are
gone, and `fputcsv()` gets the escape character that PHP 8.4 requires.
## Regenerated corpus
The MySQL 8.4.10 corpus now gives these results:
- **Queries:** 69,076 instead of 69,158. The total falls because
statements that two test files have in different encodings now read the
same and count once, such as 242 queries shared by the
`ctype_gb18030_encoding_cn` and `ctype_gb18030_encoding_utf8` tests.
Statements that a misread quote used to merge are now separate, and six
`comments.test` queries that differ only in a line separator are back.
- **Failures:** 166 instead of 167. The failing queries are the same,
now with correct text, except for one former failure that was several
statements merged into one.
See: https://bugs.mysql.com/bug.php?id=93626
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
* **New Features**
* MySQL identifiers can include valid four-byte UTF-8 characters,
including characters outside the Basic Multilingual Plane, and are
preserved during parsing.
* User-variable names can include dots and non-ASCII characters and are
recognized as a single token.
* **Bug Fixes**
* Improved parsing of SQL containing Unicode identifiers and
user-variable names, including SQL using different character sets.
* **Tests**
* Added coverage for Unicode parsing, variable names, character-set
handling, and query-corpus contents.
* Updated the MySQL query-corpus test totals.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent f05c1f9 commit 6ca5528
9 files changed
Lines changed: 2016 additions & 1551 deletions
File tree
- packages/mysql-parser
- data/mysql-server-query-corpus
- src
- tests
- data
- tools
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
667 | 667 | | |
668 | 668 | | |
669 | 669 | | |
670 | | - | |
671 | | - | |
672 | | - | |
673 | | - | |
674 | | - | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
675 | 675 | | |
676 | 676 | | |
677 | 677 | | |
| |||
697 | 697 | | |
698 | 698 | | |
699 | 699 | | |
700 | | - | |
701 | | - | |
702 | | - | |
703 | | - | |
704 | | - | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
705 | 713 | | |
706 | | - | |
707 | | - | |
| 714 | + | |
708 | 715 | | |
709 | 716 | | |
710 | 717 | | |
| |||
803 | 810 | | |
804 | 811 | | |
805 | 812 | | |
806 | | - | |
| 813 | + | |
807 | 814 | | |
808 | 815 | | |
809 | 816 | | |
| |||
1026 | 1033 | | |
1027 | 1034 | | |
1028 | 1035 | | |
1029 | | - | |
| 1036 | + | |
1030 | 1037 | | |
1031 | 1038 | | |
1032 | | - | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
1033 | 1043 | | |
1034 | 1044 | | |
1035 | 1045 | | |
| |||
1047 | 1057 | | |
1048 | 1058 | | |
1049 | 1059 | | |
1050 | | - | |
| 1060 | + | |
1051 | 1061 | | |
1052 | 1062 | | |
1053 | 1063 | | |
1054 | 1064 | | |
1055 | 1065 | | |
1056 | | - | |
| 1066 | + | |
1057 | 1067 | | |
1058 | 1068 | | |
1059 | 1069 | | |
| |||
1086 | 1096 | | |
1087 | 1097 | | |
1088 | 1098 | | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
1089 | 1117 | | |
1090 | 1118 | | |
1091 | 1119 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
217 | 217 | | |
218 | 218 | | |
219 | 219 | | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
220 | 267 | | |
221 | 268 | | |
222 | 269 | | |
| |||
425 | 472 | | |
426 | 473 | | |
427 | 474 | | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
428 | 530 | | |
429 | 531 | | |
430 | 532 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| 105 | + | |
| 106 | + | |
105 | 107 | | |
106 | 108 | | |
107 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
108 | 114 | | |
109 | 115 | | |
110 | 116 | | |
| |||
0 commit comments