Skip to content

Commit 7a2cb7d

Browse files
committed
Default the lexer to MySQL 8.4.10
The lexer defaulted to MySQL 8.0.38, while its grammar, keywords, and query corpus come from MySQL 8.4.10. Use 8.4.10 as the default, and in the corpus test and the lexer test helpers. Lexing the corpus as 8.4.10 fixes three queries from MySQL's own tests for six-digit version comments, which the lexer read the MySQL 8.0 way.
1 parent 1da9445 commit 7a2cb7d

5 files changed

Lines changed: 30 additions & 18 deletions

File tree

‎packages/mysql-parser/src/class-wp-mysql-lexer.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class WP_MySQL_Lexer {
363363
*/
364364
public function __construct(
365365
string $sql,
366-
int $mysql_version = 80038,
366+
int $mysql_version = 80410,
367367
array $sql_modes = array()
368368
) {
369369
$this->sql = $sql;

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function test_tokenize_invalid_input(): void {
7070
* @return string[] Terminal names, in input order.
7171
*/
7272
private static function token_names( string $sql, array $sql_modes = array() ): array {
73-
$tokens = ( new WP_MySQL_Lexer( $sql, 80400, $sql_modes ) )->remaining_tokens();
73+
$tokens = ( new WP_MySQL_Lexer( $sql, 80410, $sql_modes ) )->remaining_tokens();
7474
$names = array();
7575
foreach ( $tokens as $token ) {
7676
$names[] = $token->get_name();
@@ -88,28 +88,28 @@ public function test_emits_bison_terminals_with_end_markers(): void {
8888
public function test_ignore_space_preserves_function_name_ranges(): void {
8989
// Without "(", COUNT is an identifier whose range excludes trailing whitespace.
9090
$sql = 'SELECT COUNT FROM t';
91-
$tokens = ( new WP_MySQL_Lexer( $sql, 80400, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
91+
$tokens = ( new WP_MySQL_Lexer( $sql, 80410, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
9292
$this->assertSame( 'IDENT', $tokens[1]->get_name(), $sql );
9393
$this->assertSame( 'COUNT', $tokens[1]->get_value(), $sql );
9494
$this->assertSame( 5, $tokens[1]->length, $sql );
9595
$this->assertSame( 13, $tokens[2]->start, $sql );
9696

9797
$sql = "SELECT COUNT\t\n FROM t";
98-
$tokens = ( new WP_MySQL_Lexer( $sql, 80400, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
98+
$tokens = ( new WP_MySQL_Lexer( $sql, 80410, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
9999
$this->assertSame( 'IDENT', $tokens[1]->get_name(), $sql );
100100
$this->assertSame( 'COUNT', $tokens[1]->get_value(), $sql );
101101
$this->assertSame( 5, $tokens[1]->length, $sql );
102102
$this->assertSame( 15, $tokens[2]->start, $sql );
103103

104104
// With "(", COUNT is a function whose range still excludes the whitespace.
105-
$tokens = ( new WP_MySQL_Lexer( 'SELECT COUNT (1)', 80400, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
105+
$tokens = ( new WP_MySQL_Lexer( 'SELECT COUNT (1)', 80410, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
106106
$this->assertSame( 5, $tokens[1]->length );
107107
$this->assertSame( 'COUNT', $tokens[1]->get_name() );
108108
$this->assertSame( 'LEFT_PAREN', $tokens[2]->get_name() );
109109
$this->assertSame( 13, $tokens[2]->start );
110110

111111
$sql = "SELECT COUNT \t\n";
112-
$tokens = ( new WP_MySQL_Lexer( $sql, 80400, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
112+
$tokens = ( new WP_MySQL_Lexer( $sql, 80410, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
113113
$this->assertSame( 'IDENT', $tokens[1]->get_name(), $sql );
114114
$this->assertSame( 'COUNT', $tokens[1]->get_value(), $sql );
115115
$this->assertSame( 5, $tokens[1]->length, $sql );
@@ -190,6 +190,22 @@ public function data_version_comments(): array {
190190
return $cases;
191191
}
192192

193+
public function test_mysql_version_defaults_to_mysql_8_4_10(): void {
194+
$lexer = new WP_MySQL_Lexer( 'SELECT /*!080400 1 + */ 2' );
195+
$this->assertSame( 80410, $lexer->get_mysql_version() );
196+
197+
// A six-digit MySQL 8.4 version comment is executed by default.
198+
$this->assertSame(
199+
array( 'SELECT', 'NUM', 'PLUS', 'NUM', 'END_OF_INPUT', 'YYEOF' ),
200+
array_map(
201+
static function ( WP_MySQL_Token $token ): string {
202+
return $token->get_name();
203+
},
204+
$lexer->remaining_tokens()
205+
)
206+
);
207+
}
208+
193209
public function test_at_name_splits_into_at_and_ident(): void {
194210
$tokens = ( new WP_MySQL_Lexer( 'SELECT @var1' ) )->remaining_tokens();
195211
$this->assertSame( 'AT_SIGN', $tokens[1]->get_name() );
@@ -749,7 +765,7 @@ public function test_ansi_quotes_changes_double_quote_tokenization(
749765
array $sql_modes,
750766
string $expected_token_name
751767
): void {
752-
$tokens = ( new WP_MySQL_Lexer( $sql, 80400, $sql_modes ) )->remaining_tokens();
768+
$tokens = ( new WP_MySQL_Lexer( $sql, 80410, $sql_modes ) )->remaining_tokens();
753769
$this->assertSame( $expected_token_name, $tokens[0]->get_name(), $sql );
754770
}
755771

@@ -782,17 +798,17 @@ public function data_national_strings(): array {
782798

783799
public function test_composite_ansi_mode_expands_to_lexer_component_modes(): void {
784800
// ANSI_QUOTES: a double-quoted sequence is a quoted identifier.
785-
$lexer = new WP_MySQL_Lexer( '"foo"', 80400, array( 'ANSI' ) );
801+
$lexer = new WP_MySQL_Lexer( '"foo"', 80410, array( 'ANSI' ) );
786802
$this->assertTrue( $lexer->next_token() );
787803
$this->assertSame( WP_MySQL_Lexer::IDENT_QUOTED, $lexer->get_token()->id );
788804

789805
// PIPES_AS_CONCAT: "||" is the string concatenation operator.
790-
$lexer = new WP_MySQL_Lexer( '||', 80400, array( 'ANSI' ) );
806+
$lexer = new WP_MySQL_Lexer( '||', 80410, array( 'ANSI' ) );
791807
$this->assertTrue( $lexer->next_token() );
792808
$this->assertSame( WP_MySQL_Lexer::OR_OR_SYM, $lexer->get_token()->id );
793809

794810
// IGNORE_SPACE: whitespace is permitted between a function name and "(".
795-
$lexer = new WP_MySQL_Lexer( 'COUNT (1)', 80400, array( 'ANSI' ) );
811+
$lexer = new WP_MySQL_Lexer( 'COUNT (1)', 80410, array( 'ANSI' ) );
796812
$this->assertTrue( $lexer->next_token() );
797813
$this->assertSame( WP_MySQL_Lexer::KEYWORDS['COUNT'], $lexer->get_token()->id );
798814
}
@@ -808,7 +824,7 @@ public function test_quoted_identifiers_treat_backslash_literally(
808824
int $expected_token_id,
809825
string $expected_value
810826
): void {
811-
$lexer = new WP_MySQL_Lexer( $sql, 80400, $sql_modes );
827+
$lexer = new WP_MySQL_Lexer( $sql, 80410, $sql_modes );
812828
$this->assertTrue( $lexer->next_token() );
813829

814830
$token = $lexer->get_token();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class WP_MySQL_Server_Suite_Parser_Tests extends TestCase {
2020
* legitimately changes.
2121
*/
2222
const EXPECTED_QUERIES = 69158;
23-
const EXPECTED_FAILURES = 170;
23+
const EXPECTED_FAILURES = 167;
2424

2525
public function test_corpus_acceptance_rate(): void {
2626
$parser = WP_MySQL_Parser_Factory::create_parser();
@@ -36,7 +36,7 @@ public function test_corpus_acceptance_rate(): void {
3636
++$total;
3737

3838
// Pin the MySQL version. The failure set in FAILURES_PATH depends on it.
39-
$tokens = ( new WP_MySQL_Lexer( $query, 80038 ) )->remaining_tokens();
39+
$tokens = ( new WP_MySQL_Lexer( $query, 80410 ) )->remaining_tokens();
4040
if ( null === $parser->parse( $tokens ) ) {
4141
$failures[] = $query;
4242
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class WP_MySQL_Token_Tests extends TestCase {
1515
* @return WP_MySQL_Token The first matching token.
1616
*/
1717
private static function first_token( string $sql, string $name, array $sql_modes = array() ): WP_MySQL_Token {
18-
foreach ( ( new WP_MySQL_Lexer( $sql, 80400, $sql_modes ) )->remaining_tokens() as $token ) {
18+
foreach ( ( new WP_MySQL_Lexer( $sql, 80410, $sql_modes ) )->remaining_tokens() as $token ) {
1919
if ( $token->get_name() === $name ) {
2020
return $token;
2121
}

‎packages/mysql-parser/tests/data/corpus-failures.csv‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,10 +653,6 @@ UNLOCK TABLES"
653653
"SELECT 'a%' NOT LIKE 'a!%' ESCAPE '!', 'a%' NOT LIKE 'a!' || '%' ESCAPE '!'"
654654
"SELECT 'a%' NOT LIKE 'a!%' ESCAPE '$', 'a%' NOT LIKE 'a!' || '%' ESCAPE '$'"
655655
"SELECT * from ""full"""
656-
"SELECT 1 /*!080100 +1*/ AS should_return_2"
657-
"SELECT 1 /*!080100
658-
+1*/ AS should_return_2"
659-
"SELECT 1 /*!080100 +1*/ AS should_return_2"
660656
"SELECT 1 /*!99999 /* */ */"
661657
"SET sql_mode = 'NO_ENGINE_SUBSTITUTION';
662658
create procedure test_resignal()

0 commit comments

Comments
 (0)