Skip to content

Commit f79b9b3

Browse files
committed
Read MySQL test files in their character sets
The query corpus extractor decoded every MySQL test file as Latin-1. Most test files are UTF-8, so non-ASCII text was double-encoded ("geprüft" became "geprüft"), and 0x85 bytes inside UTF-8 characters split lines. Test sections that select another character set with --character_set were decoded twice. Each line is now read in the client character set, which tests select with SET NAMES and similar statements. mysqltest's --character_set doesn't change it for the current connection, so it only applies when the line isn't valid in the client character set, or when there is none. The default is UTF-8. A string literal with a character set introducer is read in that character set, and text that isn't valid in any of these is read as MySQL's latin1. Lines with only ASCII characters don't change. A test that sends UTF-8 bytes after SET NAMES latin1 is read as latin1, because that's how MySQL reads it. The extractor no longer uses the deprecated utf8_encode() and HTML-ENTITIES conversion, and it passes the fputcsv() escape character that PHP 8.4 requires. The decoding moved to its own file, so that tests can load it. The regenerated MySQL 8.4.10 corpus has 69,070 queries. A few statements that a misread quote used to merge with others are now separate queries. 166 queries fail 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.
1 parent 5cd508e commit f79b9b3

6 files changed

Lines changed: 1711 additions & 1524 deletions

File tree

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

Lines changed: 1249 additions & 1411 deletions
Large diffs are not rendered by default.
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
require_once __DIR__ . '/../tools/query-corpus-decoding.php';
6+
7+
/**
8+
* Tests for reading the lines of MySQL server test files as UTF-8 text.
9+
*/
10+
class WP_MySQL_Query_Corpus_Decoding_Tests extends TestCase {
11+
/**
12+
* @dataProvider data_lines_in_character_sets
13+
*/
14+
public function test_line_is_read_in_its_character_set( string $charset, string $line, string $expected ): void {
15+
$this->assertSame(
16+
$expected,
17+
convert_to_utf8( $line, array( QUERY_CORPUS_CHARSET_ENCODINGS[ $charset ] ) )
18+
);
19+
}
20+
21+
public function test_line_is_read_in_the_first_valid_encoding(): void {
22+
$koi8r_bytes = "SELECT '\xD4\xC5\xD3\xD4'";
23+
24+
// The client character set goes first, even when it reads the bytes differently.
25+
$this->assertSame( "SELECT 'тест'", convert_to_utf8( $koi8r_bytes, array( 'KOI8-R', 'Windows-1251' ) ) );
26+
$this->assertSame( "SELECT 'ФЕУФ'", convert_to_utf8( $koi8r_bytes, array( 'Windows-1251', 'KOI8-R' ) ) );
27+
28+
// Bytes that aren't valid in the client character set fall back to the next one.
29+
$this->assertSame( "SELECT 'тест'", convert_to_utf8( $koi8r_bytes, array( 'UTF-8', 'KOI8-R' ) ) );
30+
$this->assertSame( "SELECT 'é'", convert_to_utf8( "SELECT 'é'", array( 'UTF-8', 'KOI8-R' ) ) );
31+
32+
// Latin1 takes any bytes, so nothing is tried after it.
33+
$this->assertSame( "SELECT 'é'", convert_to_utf8( "SELECT 'é'", array( 'Windows-1252', 'UTF-8' ) ) );
34+
35+
// Bytes that aren't valid in any of the encodings are read as latin1.
36+
$this->assertSame( "SELECT 'é'", convert_to_utf8( "SELECT '\xE9'", array( 'UTF-8', 'SJIS' ) ) );
37+
}
38+
39+
/**
40+
* @dataProvider data_introduced_literals
41+
*/
42+
public function test_introduced_literal_is_read_in_its_character_set( string $encoding, string $line, string $expected ): void {
43+
$this->assertSame( $expected, convert_to_utf8( $line, array( $encoding ) ) );
44+
}
45+
46+
/**
47+
* @return array<string,array{0:string,1:string,2:string}>
48+
*/
49+
public function data_introduced_literals(): array {
50+
return array(
51+
'utf8mb3 in a koi8r line' => array( 'KOI8-R', "SELECT _utf8mb3'тест'", "SELECT _utf8mb3'тест'" ),
52+
'koi8r in a UTF-8 line' => array( 'UTF-8', "SELECT _koi8r'\xD4\xC5\xD3\xD4'", "SELECT _koi8r'тест'" ),
53+
'latin1 in a UTF-8 line' => array( 'UTF-8', "SELECT _latin1'é'", "SELECT _latin1'é'" ),
54+
'whitespace is kept' => array( 'UTF-8', "SELECT _koi8r \t'\xD4'", "SELECT _koi8r \t'т'" ),
55+
'uppercase introducer' => array( 'UTF-8', "SELECT _KOI8R'\xD4'", "SELECT _KOI8R'т'" ),
56+
'double quotes' => array( 'UTF-8', "SELECT _koi8r\"\xD4\"", 'SELECT _koi8r"т"' ),
57+
'rest of the line' => array( 'KOI8-R', "SELECT '\xD4', _utf8mb3'т', '\xD4'", "SELECT 'т', _utf8mb3'т', 'т'" ),
58+
'several introducers' => array( 'UTF-8', "SELECT _koi8r'\xD4', _latin1'\xE9', 'é'", "SELECT _koi8r'т', _latin1'é', 'é'" ),
59+
'escaped quote' => array( 'UTF-8', "SELECT _koi8r'\xD4\\'\xD4', 'é'", "SELECT _koi8r'т\\'т', 'é'" ),
60+
'doubled quote' => array( 'UTF-8', "SELECT _koi8r'\xD4''\xD4', 'é'", "SELECT _koi8r'т''т', 'é'" ),
61+
'escaped backslash at the end' => array( 'UTF-8', "SELECT _koi8r'\xD4\\\\', 'é'", "SELECT _koi8r'т\\\\', 'é'" ),
62+
63+
// Bytes that aren't valid in the character set use the encodings of the line.
64+
'invalid in its character set' => array( 'KOI8-R', "SELECT _utf8mb3'\xD4'", "SELECT _utf8mb3'т'" ),
65+
66+
// These aren't literals with an introducer that this file can read.
67+
'unsupported introducer' => array( 'KOI8-R', "SELECT _ucs2'\xD4'", "SELECT _ucs2'т'" ),
68+
'end of a column name' => array( 'UTF-8', "SELECT a_koi8r'\xD4'", "SELECT a_koi8r'Ô'" ),
69+
'hexadecimal literal' => array( 'UTF-8', "SELECT _koi8r x'D4', '\xD4'", "SELECT _koi8r x'D4', 'Ô'" ),
70+
'unterminated literal' => array( 'UTF-8', "SELECT _koi8r'\xD4", "SELECT _koi8r'Ô" ),
71+
);
72+
}
73+
74+
/**
75+
* @dataProvider data_client_charset_statements
76+
*/
77+
public function test_read_client_charset( string $line, ?string $expected ): void {
78+
$this->assertSame( $expected, read_client_charset( $line ) );
79+
}
80+
81+
/**
82+
* @return array<string,array{0:string,1:?string}>
83+
*/
84+
public function data_client_charset_statements(): array {
85+
return array(
86+
'SET NAMES' => array( 'SET NAMES latin1;', 'latin1' ),
87+
'lowercase and spaces' => array( ' set names KOI8R ;', 'koi8r' ),
88+
'single quotes' => array( "SET NAMES 'latin1';", 'latin1' ),
89+
'double quotes' => array( 'SET NAMES "latin1";', 'latin1' ),
90+
'custom delimiter' => array( 'set names koi8r|', 'koi8r' ),
91+
'long custom delimiter' => array( 'set names koi8r//', 'koi8r' ),
92+
'collation' => array( 'SET NAMES utf8mb3 COLLATE utf8mb3_bin;', 'utf8mb3' ),
93+
'another variable' => array( 'SET NAMES utf8mb3, @@character_set_results=NULL;', 'utf8mb3' ),
94+
'default' => array( 'SET NAMES DEFAULT;', 'default' ),
95+
'SET CHARACTER SET' => array( 'SET CHARACTER SET koi8r;', 'koi8r' ),
96+
'SET CHARSET' => array( 'SET CHARSET koi8r;', 'koi8r' ),
97+
'character_set_client' => array( 'SET character_set_client = latin1;', 'latin1' ),
98+
'character_set_client no space' => array( 'SET character_set_client=latin1;', 'latin1' ),
99+
'quoted character_set_client' => array( "SET character_set_client= 'latin1';", 'latin1' ),
100+
'SESSION' => array( 'SET SESSION character_set_client=latin1;', 'latin1' ),
101+
'LOCAL' => array( 'SET LOCAL character_set_client=latin1;', 'latin1' ),
102+
'system variable' => array( 'SET @@character_set_client= cp1251;', 'cp1251' ),
103+
'system variable with SESSION' => array( 'SET @@session.character_set_client=cp1251;', 'cp1251' ),
104+
'system variable with LOCAL' => array( 'SET @@local.character_set_client=cp1251;', 'cp1251' ),
105+
'user variable' => array( 'SET character_set_client = @old_cs_client;', '@old_cs_client' ),
106+
107+
'not character_set_client' => array( 'SET character_set_results = koi8r;', null ),
108+
'not a character set' => array( "SET names='foo2';", null ),
109+
'user variable named names' => array( 'SET @names = 1;', null ),
110+
'comment' => array( '# SET NAMES latin1;', null ),
111+
'mysqltest command' => array( '--character_set latin1', null ),
112+
'text in another statement' => array( "SELECT 'SET NAMES latin1';", null ),
113+
);
114+
}
115+
116+
/**
117+
* MySQL's latin1 keeps the bytes that Windows-1252 leaves undefined as C1 controls.
118+
* On PHP below 8.1, mbstring replaces them with U+FFFD or rejects them as invalid.
119+
*
120+
* @dataProvider data_charsets_with_undefined_latin1_bytes
121+
*/
122+
public function test_undefined_latin1_bytes_are_c1_controls( string $charset ): void {
123+
$this->assertSame(
124+
"SELECT '\u{81}\u{8D}\u{8F}\u{90}\u{9D}€'",
125+
convert_to_utf8( "SELECT '\x81\x8D\x8F\x90\x9D\x80'", array( QUERY_CORPUS_CHARSET_ENCODINGS[ $charset ] ) )
126+
);
127+
}
128+
129+
/**
130+
* @return array<string,array{0:string}>
131+
*/
132+
public function data_charsets_with_undefined_latin1_bytes(): array {
133+
return array(
134+
'declared latin1' => array( 'latin1' ),
135+
'fallback from utf8mb4' => array( 'utf8mb4' ),
136+
'fallback from tis620' => array( 'tis620' ),
137+
);
138+
}
139+
140+
/**
141+
* @return array<string,array{0:string,1:string,2:string}>
142+
*/
143+
public function data_lines_in_character_sets(): array {
144+
return array(
145+
'ASCII is unchanged' => array( 'koi8r', "SELECT 'a\\'b'", "SELECT 'a\\'b'" ),
146+
'big5' => array( 'big5', "SELECT '\xA4\x40'", "SELECT '一'" ),
147+
'binary as UTF-8' => array( 'binary', "SELECT 'é'", "SELECT 'é'" ),
148+
'binary that is not UTF-8' => array( 'binary', "SELECT '\xE9'", "SELECT 'é'" ),
149+
'cp1251' => array( 'cp1251', "SELECT '\xF2\xE5\xF1\xF2'", "SELECT 'тест'" ),
150+
'gb18030' => array( 'gb18030', "SELECT '\xC4\xE3'", "SELECT '你'" ),
151+
'gbk' => array( 'gbk', "SELECT '\xC4\xE3'", "SELECT '你'" ),
152+
'koi8r' => array( 'koi8r', "SELECT '\xD4\xC5\xD3\xD4'", "SELECT 'тест'" ),
153+
'latin1' => array( 'latin1', "SELECT '\x80\xE9'", "SELECT 'ێ'" ),
154+
'latin2' => array( 'latin2', "SELECT '\xB1'", "SELECT 'ą'" ),
155+
'ujis' => array( 'ujis', "SELECT '\xA4\xA2'", "SELECT 'あ'" ),
156+
'utf8' => array( 'utf8', "SELECT 'é'", "SELECT 'é'" ),
157+
'utf8mb3' => array( 'utf8mb3', "SELECT 'é'", "SELECT 'é'" ),
158+
'utf8mb4' => array( 'utf8mb4', "SELECT '🐬'", "SELECT '🐬'" ),
159+
160+
// The second byte of this character is a backslash in ASCII.
161+
'sjis with a backslash byte' => array( 'sjis', "SELECT '\x83\x5C'", "SELECT 'ソ'" ),
162+
163+
// A line that isn't valid in its character set is read as latin1.
164+
'invalid utf8mb4' => array( 'utf8mb4', "SELECT '\xE9'", "SELECT 'é'" ),
165+
'invalid sjis' => array( 'sjis', "SELECT '\xFD'", "SELECT 'ý'" ),
166+
167+
// TIS-620 maps to the Unicode Thai block, with four unassigned bytes.
168+
'tis620' => array( 'tis620', "SELECT '\xBE\xB9\xD4\xB4\xD2'", "SELECT 'พนิดา'" ),
169+
'first and last tis620 byte' => array( 'tis620', "SELECT '\xA1\xFB'", "SELECT 'ก๛'" ),
170+
'unassigned tis620 byte' => array( 'tis620', "SELECT '\xA1\xDB'", "SELECT '¡Û'" ),
171+
'tis620 byte below the Thai block' => array( 'tis620', "SELECT '\xA0\xA1'", "SELECT '\u{A0}¡'" ),
172+
'tis620 byte above the Thai block' => array( 'tis620', "SELECT '\xFC\xA1'", "SELECT 'ü¡'" ),
173+
);
174+
}
175+
}

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

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class WP_MySQL_Server_Suite_Parser_Tests extends TestCase {
1919
* set is pinned in FAILURES_PATH; update it when the accepted language
2020
* legitimately changes.
2121
*/
22-
const EXPECTED_QUERIES = 69158;
23-
const EXPECTED_FAILURES = 167;
22+
const EXPECTED_QUERIES = 69070;
23+
const EXPECTED_FAILURES = 166;
2424

2525
public function test_corpus_acceptance_rate(): void {
2626
$parser = WP_MySQL_Parser_Factory::create_parser();
@@ -53,23 +53,63 @@ public function test_corpus_acceptance_rate(): void {
5353
$this->assertSame( $this->read_expected_failures(), $failures );
5454
}
5555

56+
/**
57+
* Pin the text of queries from tests that use other character sets, because
58+
* the tally can't see a change in the text of a query that still parses.
59+
*
60+
* @dataProvider data_query_texts
61+
*/
62+
public function test_corpus_contains_query_text( string $query ): void {
63+
static $corpus = null;
64+
if ( null === $corpus ) {
65+
$corpus = array_flip( $this->read_queries( self::CORPUS_PATH ) );
66+
}
67+
$this->assertArrayHasKey( $query, $corpus );
68+
}
69+
70+
/**
71+
* @return array<string,array{0:string}>
72+
*/
73+
public function data_query_texts(): array {
74+
return array(
75+
'SET NAMES overrides --character_set' => array( 'create table `été` (field1 int)' ),
76+
'SET NAMES without --character_set' => array( 'create database бд' ),
77+
'SET NAMES with --character_set latin1' => array( 'CREATE TABLE t1(колонка1 INT)' ),
78+
'SET NAMES before --character_set' => array( "insert into t1 select repeat('я',300)" ),
79+
'UTF-8 bytes that MySQL reads as latin1' => array( 'insert into t1 (id, name) values (1, "óra")' ),
80+
'UTF-8 literal in a koi8r line' => array( "CREATE VIEW v2 AS SELECT _utf8mb3'тест' as c1" ),
81+
'koi8r literal in a UTF-8 line' => array( "CREATE VIEW v2 AS SELECT _koi8r'тест' as c1" ),
82+
);
83+
}
84+
5685
/**
5786
* Read the pinned set of failing queries, sorted to match the live set.
5887
*
5988
* @return string[] The expected failing queries.
6089
*/
6190
private function read_expected_failures(): array {
62-
$handle = fopen( self::FAILURES_PATH, 'r' );
63-
$expected = array();
91+
$expected = $this->read_queries( self::FAILURES_PATH );
92+
sort( $expected, SORT_STRING );
93+
return $expected;
94+
}
95+
96+
/**
97+
* Read the queries from a CSV file.
98+
*
99+
* @param string $path The path to the CSV file.
100+
* @return string[] The queries.
101+
*/
102+
private function read_queries( string $path ): array {
103+
$handle = fopen( $path, 'r' );
104+
$queries = array();
64105
while ( ( $record = fgetcsv( $handle, null, ',', '"', '\\' ) ) !== false ) {
65106
$query = $record[0] ?? null;
66107
if ( null === $query || '' === $query ) {
67108
continue;
68109
}
69-
$expected[] = $query;
110+
$queries[] = $query;
70111
}
71112
fclose( $handle );
72-
sort( $expected, SORT_STRING );
73-
return $expected;
113+
return $queries;
74114
}
75115
}

0 commit comments

Comments
 (0)