Skip to content

Commit de72a3b

Browse files
committed
Read cp1250 in MySQL test files
Tests select cp1250 with SET NAMES, and with SET CHARACTER SET cp1250_latin2, an old name that still selects it. mbstring doesn't support cp1250, so these lines were read in other character sets, and three queries had the wrong text: "ááèè" instead of "ááčč", and "ŁĽŞŻ" instead of "ŁĄŞŻ". The extractor now reads cp1250 with its own table, which matches MySQL's CONVERT() for every byte. The new queries also pin SET CHARACTER SET, because they come out right only when that statement is tracked.
1 parent f92d7a6 commit de72a3b

4 files changed

Lines changed: 119 additions & 18 deletions

File tree

‎packages/mysql-parser/data/mysql-server-query-corpus/mysql-latest.csv‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7819,7 +7819,7 @@ UNIQUE KEY (str)
78197819
"select * from t1 where str like 'aa%'"
78207820
"create table t1 (a varchar(15) collate cp1250_czech_cs NOT NULL, primary key(a))"
78217821
"insert into t1 values(""abcdefghá"")"
7822-
"insert into t1 values(""ááèè"")"
7822+
"insert into t1 values(""ááčč"")"
78237823
"select a from t1 where a like ""abcdefghá"""
78247824
"set names cp1250 collate cp1250_czech_cs"
78257825
"SET NAMES cp1250"
@@ -63781,8 +63781,8 @@ WHERE a1.a = 1 OR a1.a = 2"
6378163781
"CREATE DATABASE mysqltest1 CHARACTER SET LATIN2"
6378263782
"CREATE TABLE t1 (a VARCHAR(255) CHARACTER SET LATIN2)"
6378363783
"SET CHARACTER SET cp1250_latin2"
63784-
"INSERT INTO t1 VALUES ('ŁĽŞŻ')"
63785-
"INSERT INTO t1 VALUES ('ŁĽŞŻ' '')"
63784+
"INSERT INTO t1 VALUES ('ŁĄŞŻ')"
63785+
"INSERT INTO t1 VALUES ('ŁĄŞŻ' '')"
6378663786
"INSERT INTO t1 () SELECT * FROM t1"
6378763787
"INSERT INTO t1 SELECT HIGH_PRIORITY * FROM t1"
6378863788
"DELETE QUICK FROM t1 WHERE i = 0"

‎packages/mysql-parser/tests/WP_MySQL_Query_Corpus_Decoding_Tests.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ public function test_line_is_read_in_the_first_valid_encoding(): void {
3636
$this->assertSame( "SELECT 'é'", convert_to_utf8( "SELECT '\xE9'", array( 'UTF-8', 'SJIS' ) ) );
3737
}
3838

39+
/**
40+
* Test every non-ASCII byte that MySQL's cp1250 defines. The expected text comes
41+
* from MySQL, as the result of CONVERT(_cp1250 x'..' USING utf32) for each byte.
42+
*/
43+
public function test_cp1250_characters_match_mysql(): void {
44+
$bytes = '';
45+
for ( $byte = 0x80; $byte <= 0xFF; $byte += 1 ) {
46+
if ( false === strpos( "\x81\x83\x88\x90\x98", chr( $byte ) ) ) {
47+
$bytes .= chr( $byte );
48+
}
49+
}
50+
51+
$this->assertSame(
52+
"€‚„…†‡‰Š‹ŚŤŽŹ‘’“”•–—™š›śťžź\u{A0}ˇ˘Ł¤Ą¦§"
53+
. "¨©Ş«¬\u{AD}®Ż°±˛ł´µ¶·¸ąş»Ľ˝ľżŔÁÂĂÄĹĆÇČÉĘ"
54+
. 'ËĚÍÎĎĐŃŇÓÔŐÖ×ŘŮÚŰÜÝŢßŕáâăäĺćçčéęëěíîďđńň'
55+
. 'óôőö÷řůúűüýţ˙',
56+
convert_to_utf8( $bytes, array( QUERY_CORPUS_CHARSET_ENCODINGS['cp1250'] ) )
57+
);
58+
}
59+
3960
/**
4061
* @dataProvider data_introduced_literals
4162
*/
@@ -51,6 +72,7 @@ public function data_introduced_literals(): array {
5172
'utf8mb3 in a koi8r line' => array( 'KOI8-R', "SELECT _utf8mb3'тест'", "SELECT _utf8mb3'тест'" ),
5273
'koi8r in a UTF-8 line' => array( 'UTF-8', "SELECT _koi8r'\xD4\xC5\xD3\xD4'", "SELECT _koi8r'тест'" ),
5374
'latin1 in a UTF-8 line' => array( 'UTF-8', "SELECT _latin1'é'", "SELECT _latin1'é'" ),
75+
'cp1250 in a UTF-8 line' => array( 'UTF-8', "SELECT _cp1250'\xE8'", "SELECT _cp1250'č'" ),
5476
'whitespace is kept' => array( 'UTF-8', "SELECT _koi8r \t'\xD4'", "SELECT _koi8r \t'т'" ),
5577
'uppercase introducer' => array( 'UTF-8', "SELECT _KOI8R'\xD4'", "SELECT _KOI8R'т'" ),
5678
'double quotes' => array( 'UTF-8', "SELECT _koi8r\"\xD4\"", 'SELECT _koi8r"т"' ),
@@ -94,6 +116,7 @@ public function data_client_charset_statements(): array {
94116
'default' => array( 'SET NAMES DEFAULT;', 'default' ),
95117
'SET CHARACTER SET' => array( 'SET CHARACTER SET koi8r;', 'koi8r' ),
96118
'SET CHARSET' => array( 'SET CHARSET koi8r;', 'koi8r' ),
119+
'old character set name' => array( 'SET CHARACTER SET cp1250_latin2;', 'cp1250_latin2' ),
97120
'character_set_client' => array( 'SET character_set_client = latin1;', 'latin1' ),
98121
'character_set_client no space' => array( 'SET character_set_client=latin1;', 'latin1' ),
99122
'quoted character_set_client' => array( "SET character_set_client= 'latin1';", 'latin1' ),
@@ -146,6 +169,8 @@ public function data_lines_in_character_sets(): array {
146169
'big5' => array( 'big5', "SELECT '\xA4\x40'", "SELECT '一'" ),
147170
'binary as UTF-8' => array( 'binary', "SELECT 'é'", "SELECT 'é'" ),
148171
'binary that is not UTF-8' => array( 'binary', "SELECT '\xE9'", "SELECT 'é'" ),
172+
'cp1250' => array( 'cp1250', "SELECT '\xE1\xE1\xE8\xE8'", "SELECT 'ááčč'" ),
173+
'cp1250_latin2' => array( 'cp1250_latin2', "SELECT '\xA3\xA5\xAA\xAF'", "SELECT 'ŁĄŞŻ'" ),
149174
'cp1251' => array( 'cp1251', "SELECT '\xF2\xE5\xF1\xF2'", "SELECT 'тест'" ),
150175
'gb18030' => array( 'gb18030', "SELECT '\xC4\xE3'", "SELECT '你'" ),
151176
'gbk' => array( 'gbk', "SELECT '\xC4\xE3'", "SELECT '你'" ),
@@ -170,6 +195,9 @@ public function data_lines_in_character_sets(): array {
170195
'unassigned tis620 byte' => array( 'tis620', "SELECT '\xA1\xDB'", "SELECT '¡Û'" ),
171196
'tis620 byte below the Thai block' => array( 'tis620', "SELECT '\xA0\xA1'", "SELECT '\u{A0}¡'" ),
172197
'tis620 byte above the Thai block' => array( 'tis620', "SELECT '\xFC\xA1'", "SELECT 'ü¡'" ),
198+
199+
// MySQL's cp1250 leaves five bytes undefined, so these lines are read as latin1.
200+
'undefined cp1250 bytes' => array( 'cp1250', "SELECT '\xE8\x81\x83\x88\x90\x98'", "SELECT 'è\u{81}ƒˆ\u{90}˜'" ),
173201
);
174202
}
175203
}

‎packages/mysql-parser/tests/WP_MySQL_Server_Suite_Parser_Tests.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public function data_query_texts(): array {
7474
return array(
7575
'SET NAMES overrides --character_set' => array( 'create table `été` (field1 int)' ),
7676
'SET NAMES without --character_set' => array( 'create database бд' ),
77+
'SET NAMES cp1250' => array( 'insert into t1 values("ááčč")' ),
7778
'SET NAMES with --character_set latin1' => array( 'CREATE TABLE t1(колонка1 INT)' ),
79+
'SET CHARACTER SET' => array( "INSERT INTO t1 VALUES ('ŁĄŞŻ')" ),
7880
'SET NAMES before --character_set' => array( "insert into t1 select repeat('я',300)" ),
7981
'UTF-8 bytes that MySQL reads as latin1' => array( 'insert into t1 (id, name) values (1, "óra")' ),
8082
'UTF-8 literal in a koi8r line' => array( "CREATE VIEW v2 AS SELECT _utf8mb3'тест' as c1" ),

‎packages/mysql-parser/tools/query-corpus-decoding.php‎

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,71 @@
1616

1717
// MySQL character sets that tests select, and the mbstring encodings to read them
1818
// with. MySQL's latin1 is Windows-1252, binary data is read as UTF-8 when valid, and
19-
// TIS-620, which mbstring doesn't support, is converted by convert_bytes_to_utf8().
19+
// TIS-620 and Windows-1250, which mbstring doesn't support, are converted by
20+
// convert_bytes_to_utf8(). "cp1250_latin2" is an old name that selects cp1250.
2021
const QUERY_CORPUS_CHARSET_ENCODINGS = array(
21-
'big5' => 'BIG-5',
22-
'binary' => 'UTF-8',
23-
'cp1251' => 'Windows-1251',
24-
'gb18030' => 'GB18030',
25-
'gbk' => 'CP936',
26-
'koi8r' => 'KOI8-R',
27-
'latin1' => 'Windows-1252',
28-
'latin2' => 'ISO-8859-2',
29-
'sjis' => 'SJIS',
30-
'tis620' => 'TIS-620',
31-
'ujis' => 'EUC-JP',
32-
'utf8' => 'UTF-8',
33-
'utf8mb3' => 'UTF-8',
34-
'utf8mb4' => 'UTF-8',
22+
'big5' => 'BIG-5',
23+
'binary' => 'UTF-8',
24+
'cp1250' => 'Windows-1250',
25+
'cp1250_latin2' => 'Windows-1250',
26+
'cp1251' => 'Windows-1251',
27+
'gb18030' => 'GB18030',
28+
'gbk' => 'CP936',
29+
'koi8r' => 'KOI8-R',
30+
'latin1' => 'Windows-1252',
31+
'latin2' => 'ISO-8859-2',
32+
'sjis' => 'SJIS',
33+
'tis620' => 'TIS-620',
34+
'ujis' => 'EUC-JP',
35+
'utf8' => 'UTF-8',
36+
'utf8mb3' => 'UTF-8',
37+
'utf8mb4' => 'UTF-8',
38+
);
39+
40+
// The Windows-1250 characters of the bytes that differ from ISO-8859-2.
41+
const QUERY_CORPUS_CP1250_CHARACTERS = array(
42+
"\x80" => '€',
43+
"\x82" => '‚',
44+
"\x84" => '„',
45+
"\x85" => '…',
46+
"\x86" => '†',
47+
"\x87" => '‡',
48+
"\x89" => '‰',
49+
"\x8A" => 'Š',
50+
"\x8B" => '‹',
51+
"\x8C" => 'Ś',
52+
"\x8D" => 'Ť',
53+
"\x8E" => 'Ž',
54+
"\x8F" => 'Ź',
55+
"\x91" => '‘',
56+
"\x92" => '’',
57+
"\x93" => '“',
58+
"\x94" => '”',
59+
"\x95" => '•',
60+
"\x96" => '–',
61+
"\x97" => '—',
62+
"\x99" => '™',
63+
"\x9A" => 'š',
64+
"\x9B" => '›',
65+
"\x9C" => 'ś',
66+
"\x9D" => 'ť',
67+
"\x9E" => 'ž',
68+
"\x9F" => 'ź',
69+
"\xA1" => 'ˇ',
70+
"\xA5" => 'Ą',
71+
"\xA6" => '¦',
72+
"\xA9" => '©',
73+
"\xAB" => '«',
74+
"\xAC" => '¬',
75+
"\xAE" => '®',
76+
"\xB1" => '±',
77+
"\xB5" => 'µ',
78+
"\xB6" => '',
79+
"\xB7" => '·',
80+
"\xB9" => 'ą',
81+
"\xBB" => '»',
82+
"\xBC" => 'Ľ',
83+
"\xBE" => 'ľ',
3584
);
3685

3786
/**
@@ -116,13 +165,35 @@ function ( array $matches ): string {
116165
$bytes
117166
);
118167
}
168+
} elseif ( 'Windows-1250' === $encoding ) {
169+
// MySQL's cp1250 leaves these five bytes undefined.
170+
if ( ! preg_match( '/[\x81\x83\x88\x90\x98]/', $bytes ) ) {
171+
return convert_cp1250_to_utf8( $bytes );
172+
}
119173
} elseif ( mb_check_encoding( $bytes, $encoding ) ) {
120174
return mb_convert_encoding( $bytes, 'UTF-8', $encoding );
121175
}
122176
}
123177
return convert_latin1_to_utf8( $bytes );
124178
}
125179

180+
/**
181+
* Convert bytes in MySQL's cp1250 to UTF-8.
182+
*
183+
* @param string $bytes The cp1250 bytes, without any undefined ones.
184+
* @return string The bytes in UTF-8.
185+
*/
186+
function convert_cp1250_to_utf8( string $bytes ): string {
187+
return preg_replace_callback(
188+
'/[\x80-\xff]/',
189+
function ( array $matches ): string {
190+
return QUERY_CORPUS_CP1250_CHARACTERS[ $matches[0] ]
191+
?? mb_convert_encoding( $matches[0], 'UTF-8', 'ISO-8859-2' );
192+
},
193+
$bytes
194+
);
195+
}
196+
126197
/**
127198
* Convert bytes in MySQL's latin1 to UTF-8.
128199
*

0 commit comments

Comments
 (0)