Skip to content

Commit 9ff0f65

Browse files
committed
Read the default client character set from the MySQL test suite
mysqltest connects in the character set that the test suite configures. That is utf8mb4 since MySQL 8.0 and latin1 before it, so reading tests as UTF-8 by default doesn't match how older versions read them. The query corpus extractor now reads the default from mysql-test/include/default_client.cnf, so it doesn't need to know the MySQL version. DEFAULT and character sets that the map doesn't list use it too. It stops with an error if the file has no known character set. The 8.4.10 corpus doesn't change, and neither does the output for 8.0.46. The output changes for 25 queries on 5.7.44, 16 on 5.6.51, and 13 on 5.5.63.
1 parent 029f91d commit 9ff0f65

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,31 @@ public function data_introduced_literals(): array {
9393
);
9494
}
9595

96+
/**
97+
* @dataProvider data_client_configs
98+
*/
99+
public function test_read_default_client_charset( string $config, ?string $expected ): void {
100+
$this->assertSame( $expected, read_default_client_charset( $config ) );
101+
}
102+
103+
/**
104+
* @return array<string,array{0:string,1:?string}>
105+
*/
106+
public function data_client_configs(): array {
107+
return array(
108+
'mysqltest section' => array( "[mysql]\ndefault-character-set=utf8mb4\n\n[mysqltest]\ndefault-character-set=utf8mb4\n", 'utf8mb4' ),
109+
'no mysqltest section' => array( "[mysql]\ndefault-character-set=latin1\n\n[mysqlshow]\ndefault-character-set=latin1\n", 'latin1' ),
110+
'comment mentioning it' => array( "# We use default-character-set=latin1 here.\n[mysql]\ndefault-character-set=utf8mb4\n", 'utf8mb4' ),
111+
'CRLF line endings' => array( "[mysql]\r\ndefault-character-set=latin1\r\n", 'latin1' ),
112+
'spaces' => array( "[mysql]\ndefault-character-set = latin1\n", 'latin1' ),
113+
'uppercase' => array( "[mysql]\ndefault-character-set=LATIN1\n", 'latin1' ),
114+
115+
'another option' => array( "[mysqld]\ncharacter-set-server=latin1\n", null ),
116+
'empty value' => array( "[mysql]\ndefault-character-set=\ncollation=latin1\n", null ),
117+
'empty file' => array( '', null ),
118+
);
119+
}
120+
96121
/**
97122
* @dataProvider data_client_charset_statements
98123
*/

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ function ( $command ) {
182182
exit( 1 );
183183
}
184184

185+
// mysqltest connects in the default character set of the test suite.
186+
$client_config = $tests_dir . '/../include/default_client.cnf';
187+
$default_encoding = QUERY_CORPUS_CHARSET_ENCODINGS[ read_default_client_charset( file_get_contents( $client_config ) ) ?? '' ] ?? null;
188+
if ( null === $default_encoding ) {
189+
fwrite( STDERR, "error: no known default character set in $client_config.\n" );
190+
exit( 1 );
191+
}
192+
185193
$queries = array();
186194
foreach ( scandir( $tests_dir ) as $i => $file ) {
187195
if ( substr( $file, -5 ) !== '.test' ) {
@@ -205,8 +213,7 @@ function ( $command ) {
205213
$skip_next = false;
206214

207215
// Track the encoding of the client character set that a statement selects.
208-
// mysqltest connects in utf8mb4.
209-
$client_encoding = 'UTF-8';
216+
$client_encoding = $default_encoding;
210217

211218
$query = '';
212219
$contents = file_get_contents( $tests_dir . '/' . $file );
@@ -262,10 +269,10 @@ function ( $command ) {
262269
}
263270

264271
// Track the client character set, unless the statement is expected to fail.
265-
// "DEFAULT" and character sets that the map doesn't list are read as UTF-8.
272+
// "DEFAULT" and character sets that the map doesn't list use the default encoding.
266273
$client_charset = read_client_charset( $line );
267274
if ( null !== $client_charset && ! $skip_next ) {
268-
$client_encoding = QUERY_CORPUS_CHARSET_ENCODINGS[ $client_charset ] ?? 'UTF-8';
275+
$client_encoding = QUERY_CORPUS_CHARSET_ENCODINGS[ $client_charset ] ?? $default_encoding;
269276
}
270277

271278
$line = convert_to_utf8( $line, array( $client_encoding, 'UTF-8' ) );

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@
8383
"\xBE" => 'ľ',
8484
);
8585

86+
/**
87+
* Read the character set that mysqltest connects in from "default_client.cnf".
88+
*
89+
* The client programs in this file all use the same character set. Before MySQL 8.0,
90+
* the file has no section for mysqltest, but its built-in default is the same.
91+
*
92+
* @param string $config The contents of the file.
93+
* @return string|null The lowercase character set name; null when the file has none.
94+
*/
95+
function read_default_client_charset( string $config ): ?string {
96+
$is_set = preg_match( '/^default-character-set[ \t]*=[ \t]*(\w+)/m', $config, $matches );
97+
return $is_set ? strtolower( $matches[1] ) : null;
98+
}
99+
86100
/**
87101
* Read the character set from a statement that sets the client character set.
88102
*

0 commit comments

Comments
 (0)