Skip to content

Commit 3d11fc9

Browse files
committed
Lex dollar-quoted routine bodies
MySQL 8.1 added dollar-quoted text for routines in other languages, such as CREATE FUNCTION ... LANGUAGE JAVASCRIPT AS $$ ... $$. The parse table already knows the token, but the lexer read "$$" as an identifier, so 102 queries in the corpus failed. The rule follows MySQL, including two details that were checked on MySQL 8.4.10: empty text is a syntax error, and the byte that breaks a closing delimiter match is skipped, so "$$a$" doesn't close "$a$". https://github.com/mysql/mysql-server/blob/mysql-8.4.10/sql/sql_lex.cc#L2158 https://github.com/mysql/mysql-server/blob/mysql-8.4.10/sql/sql_lex.cc#L1144
1 parent b747efe commit 3d11fc9

8 files changed

Lines changed: 226 additions & 581 deletions

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class WP_MySQL_Lexer {
7676
const IDENT_QUOTED = 484;
7777
const TEXT_STRING = 827;
7878
const NCHAR_STRING = 611;
79+
const DOLLAR_QUOTED_STRING_SYM = 1204;
7980
const UNDERSCORE_CHARSET = 852;
8081
const BIN_NUM = 292;
8182
const HEX_NUM = 474;
@@ -1007,6 +1008,16 @@ private function read_next_token(): ?int {
10071008
}
10081009
} elseif ( null === $byte ) {
10091010
$type = self::EOF;
1011+
} elseif ( '$' === $byte ) {
1012+
$type = $this->read_identifier();
1013+
1014+
// From MySQL 8.1, a second "$" in the name ends the opening delimiter of
1015+
// dollar-quoted text. Without one, it's an identifier, such as "$a" or "$".
1016+
$name = $this->get_current_token_bytes();
1017+
$tag_end = strpos( $name, '$', 1 );
1018+
if ( false !== $tag_end && $this->mysql_version >= 80100 ) {
1019+
$type = $this->read_dollar_quoted_text( substr( $name, 0, $tag_end + 1 ) );
1020+
}
10101021
} else {
10111022
$type = $this->read_identifier();
10121023
if ( self::IDENT === $type ) {
@@ -1370,6 +1381,53 @@ private function read_quoted_text(): ?int {
13701381
return $is_identifier_quote ? self::IDENT_QUOTED : self::TEXT_STRING;
13711382
}
13721383

1384+
/**
1385+
* Read dollar-quoted text, which has the form $tag$text$tag$.
1386+
*
1387+
* Rules:
1388+
* 1. The tag can be empty. Otherwise, it consists of unquoted identifier
1389+
* characters other than "$", and it is case-sensitive.
1390+
* 2. The text can't be empty, and it has no escape sequences. It ends with
1391+
* the first closing delimiter.
1392+
*
1393+
* @param string $delimiter The opening delimiter, at the start of the current token.
1394+
*/
1395+
private function read_dollar_quoted_text( string $delimiter ): ?int {
1396+
// A "." that read_identifier() found after the name is a part of the text.
1397+
$this->next_state = self::STATE_START;
1398+
1399+
$delimiter_length = strlen( $delimiter );
1400+
$text_start = $this->token_starts_at + $delimiter_length;
1401+
$at = $text_start;
1402+
while ( $at < $this->sql_length ) {
1403+
$at = strpos( $this->sql, '$', $at );
1404+
if ( false === $at ) {
1405+
break;
1406+
}
1407+
1408+
$matched = 1;
1409+
while (
1410+
$matched < $delimiter_length
1411+
&& ( $this->sql[ $at + $matched ] ?? null ) === $delimiter[ $matched ]
1412+
) {
1413+
++$matched;
1414+
}
1415+
if ( $matched === $delimiter_length ) {
1416+
// MySQL rejects empty text, unlike an empty string as a routine body.
1417+
if ( $at === $text_start ) {
1418+
break;
1419+
}
1420+
$this->bytes_already_read = $at + $delimiter_length;
1421+
return self::DOLLAR_QUOTED_STRING_SYM;
1422+
}
1423+
1424+
// As in MySQL, the byte that breaks the match is not checked again,
1425+
// so "$$a$" doesn't close "$a$".
1426+
$at += $matched + 1;
1427+
}
1428+
return null; // Invalid input.
1429+
}
1430+
13731431
private function read_line_comment(): int {
13741432
$this->bytes_already_read += strcspn( $this->sql, "\r\n", $this->bytes_already_read );
13751433
return self::COMMENT;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ public function get_value(): string {
182182
*/
183183
$value = preg_replace( '/\\\\(.)/s', '$1', $value );
184184
}
185+
if ( WP_MySQL_Lexer::DOLLAR_QUOTED_STRING_SYM === $this->id ) {
186+
// Remove the bounding "$tag$" delimiters. The text has no escape sequences.
187+
$delimiter_length = strpos( $value, '$', 1 ) + 1;
188+
return substr( $value, $delimiter_length, -$delimiter_length );
189+
}
185190
return $value;
186191
}
187192

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

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,128 @@ public function data_national_strings(): array {
898898
);
899899
}
900900

901+
/**
902+
* @dataProvider data_dollar_quoted_text
903+
*/
904+
public function test_dollar_quoted_text( string $sql, array $expected ): void {
905+
$tokens = ( new WP_MySQL_Lexer( $sql ) )->remaining_tokens();
906+
$this->assertSame( 'YYEOF', array_pop( $tokens )->get_name(), $sql );
907+
$this->assertSame( 'END_OF_INPUT', array_pop( $tokens )->get_name(), $sql );
908+
909+
$actual = array();
910+
foreach ( $tokens as $token ) {
911+
$actual[] = array( $token->get_name(), $token->get_bytes() );
912+
}
913+
$this->assertSame( $expected, $actual, $sql );
914+
915+
// The pull iterator follows the same path as remaining_tokens().
916+
$lexer = new WP_MySQL_Lexer( $sql );
917+
foreach ( $tokens as $token ) {
918+
$this->assertTrue( $lexer->next_token(), $sql );
919+
$this->assertSame( $token->id, $lexer->get_token()->id, $sql );
920+
$this->assertSame( $token->get_bytes(), $lexer->get_token()->get_bytes(), $sql );
921+
}
922+
}
923+
924+
public function data_dollar_quoted_text(): array {
925+
$dollar = 'DOLLAR_QUOTED_STRING_SYM';
926+
return array(
927+
// The tag can be empty, or contain identifier characters other than "$".
928+
'empty tag' => array( '$$ a $$', array( array( $dollar, '$$ a $$' ) ) ),
929+
'tag' => array( '$tag$ a $tag$', array( array( $dollar, '$tag$ a $tag$' ) ) ),
930+
'digits in a tag' => array( '$1$ a $1$', array( array( $dollar, '$1$ a $1$' ) ) ),
931+
'underscore in a tag' => array( '$_$ a $_$', array( array( $dollar, '$_$ a $_$' ) ) ),
932+
'a tag with a non-ASCII character' => array( '$∂$ a $∂$', array( array( $dollar, '$∂$ a $∂$' ) ) ),
933+
'a tag with a 4-byte character' => array( '$😀$ a $😀$', array( array( $dollar, '$😀$ a $😀$' ) ) ),
934+
935+
// The text can contain anything except for the closing delimiter.
936+
'a line break in the text' => array( "\$\$ a\nb \$\$", array( array( $dollar, "\$\$ a\nb \$\$" ) ) ),
937+
'quotes and comments in the text' => array( "\$\$ ' \" ` /* -- # \$\$", array( array( $dollar, "\$\$ ' \" ` /* -- # \$\$" ) ) ),
938+
'other delimiters in the text' => array( '$a$ $$ $b$ $a$', array( array( $dollar, '$a$ $$ $b$ $a$' ) ) ),
939+
'a tag is case-sensitive' => array( '$a$ $A$ $a$', array( array( $dollar, '$a$ $A$ $a$' ) ) ),
940+
'a partial tag in the text' => array( '$ab$ $a $ab$', array( array( $dollar, '$ab$ $a $ab$' ) ) ),
941+
'a dollar sign in the text' => array( '$$ a$b $$', array( array( $dollar, '$$ a$b $$' ) ) ),
942+
'multibyte characters in the text' => array( '$😀$ $😔 $😀$', array( array( $dollar, '$😀$ $😔 $😀$' ) ) ),
943+
944+
// As in MySQL, the byte that breaks a closing delimiter match is skipped.
945+
'a dollar sign just before a close' => array( '$a$ $$a$ $a$', array( array( $dollar, '$a$ $$a$ $a$' ) ) ),
946+
'a broken match that ends with "$"' => array( '$ab$ $a$ab$ $ab$', array( array( $dollar, '$ab$ $a$ab$ $ab$' ) ) ),
947+
948+
// Dollar-quoted text ends with the closing delimiter.
949+
'a name right after the text' => array( '$$a$$b', array( array( $dollar, '$$a$$' ), array( 'IDENT', 'b' ) ) ),
950+
'a name after a tagged text' => array( '$a$b$a$c', array( array( $dollar, '$a$b$a$' ), array( 'IDENT', 'c' ) ) ),
951+
'a number after the text' => array( '$$a$$.5', array( array( $dollar, '$$a$$' ), array( 'DECIMAL_NUM', '.5' ) ) ),
952+
'two texts' => array( '$$a$$ $$b$$', array( array( $dollar, '$$a$$' ), array( $dollar, '$$b$$' ) ) ),
953+
'after a space and a comment' => array( ' /* a */ $$b$$', array( array( $dollar, '$$b$$' ) ) ),
954+
'after WITH' => array( 'WITH $$a$$', array( array( 'WITH', 'WITH' ), array( $dollar, '$$a$$' ) ) ),
955+
956+
// Without a second "$" in the identifier characters, it's an identifier.
957+
'a name with a leading dollar sign' => array( '$a', array( array( 'IDENT', '$a' ) ) ),
958+
'a name with digits' => array( '$1', array( array( 'IDENT', '$1' ) ) ),
959+
'a lone dollar sign' => array( '$', array( array( 'IDENT', '$' ) ) ),
960+
'two lone dollar signs' => array( '$ $', array( array( 'IDENT', '$' ), array( 'IDENT', '$' ) ) ),
961+
'a dollar sign before a dot' => array( '$a.$', array( array( 'IDENT', '$a' ), array( 'DOT', '.' ), array( 'IDENT', '$' ) ) ),
962+
963+
// Only a token that starts with "$" can open dollar-quoted text.
964+
'dollar signs inside a name' => array( 'a$$b$$', array( array( 'IDENT', 'a$$b$$' ) ) ),
965+
'a name after a dot' => array( 't.$$', array( array( 'IDENT', 't' ), array( 'DOT', '.' ), array( 'IDENT', '$$' ) ) ),
966+
'a tagged name after a dot' => array( 't.$a$', array( array( 'IDENT', 't' ), array( 'DOT', '.' ), array( 'IDENT', '$a$' ) ) ),
967+
'a user variable' => array( '@$$', array( array( 'AT_SIGN', '@' ), array( 'IDENT', '$$' ) ) ),
968+
'a system variable' => array( '@@$$', array( array( 'AT_SIGN', '@' ), array( 'AT_SIGN', '@' ), array( 'IDENT', '$$' ) ) ),
969+
'a quoted name' => array( '`$$`', array( array( 'IDENT_QUOTED', '`$$`' ) ) ),
970+
'a string' => array( "'\$\$'", array( array( 'TEXT_STRING', "'\$\$'" ) ) ),
971+
);
972+
}
973+
974+
/**
975+
* @dataProvider data_invalid_dollar_quoted_text
976+
*/
977+
public function test_invalid_dollar_quoted_text( string $sql ): void {
978+
$tokens = ( new WP_MySQL_Lexer( "SELECT $sql" ) )->remaining_tokens();
979+
$this->assertCount( 1, $tokens, $sql );
980+
$this->assertSame( 'SELECT', $tokens[0]->get_name(), $sql );
981+
982+
// The pull iterator follows the same path as remaining_tokens().
983+
$lexer = new WP_MySQL_Lexer( "SELECT $sql" );
984+
$this->assertTrue( $lexer->next_token(), $sql );
985+
$this->assertFalse( $lexer->next_token(), $sql );
986+
}
987+
988+
public function data_invalid_dollar_quoted_text(): array {
989+
return array(
990+
'empty text' => array( '$$$$' ),
991+
'empty text with a tag' => array( '$a$$a$' ),
992+
'no text' => array( '$$' ),
993+
'no closing delimiter' => array( '$$ a' ),
994+
'half a delimiter' => array( '$$ a $' ),
995+
'no text after a tag' => array( '$a$' ),
996+
'a partial closing tag' => array( '$ab$ c $a' ),
997+
'a tag without its "$"' => array( '$a$ b $a' ),
998+
'another tag' => array( '$a$ b $b$' ),
999+
'a different tag case' => array( '$a$ b $A$' ),
1000+
'a dollar sign in a tag' => array( '$a$b' ),
1001+
'a skipped close' => array( '$a$ $$a$' ),
1002+
'a broken match at end' => array( '$ab$ $a$ab$' ),
1003+
);
1004+
}
1005+
1006+
public function test_dollar_quoted_text_requires_mysql_8_1(): void {
1007+
$tokens = ( new WP_MySQL_Lexer( '$$ a $$', 80099 ) )->remaining_tokens();
1008+
$this->assertSame(
1009+
array( '$$', 'a', '$$', '', '' ),
1010+
array_map(
1011+
static function ( WP_MySQL_Token $token ): string {
1012+
return $token->get_bytes();
1013+
},
1014+
$tokens
1015+
)
1016+
);
1017+
$this->assertSame( 'IDENT', $tokens[0]->get_name() );
1018+
1019+
$tokens = ( new WP_MySQL_Lexer( '$$ a $$', 80100 ) )->remaining_tokens();
1020+
$this->assertSame( 'DOLLAR_QUOTED_STRING_SYM', $tokens[0]->get_name() );
1021+
}
1022+
9011023
public function test_composite_ansi_mode_expands_to_lexer_component_modes(): void {
9021024
// ANSI_QUOTES: a double-quoted sequence is a quoted identifier.
9031025
$lexer = new WP_MySQL_Lexer( '"foo"', 80410, array( 'ANSI' ) );

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,30 @@ public function test_parses_representative_statements(): void {
121121
}
122122
}
123123

124+
public function test_dollar_quoted_routine_body_is_a_routine_string(): void {
125+
$statements = array(
126+
'CREATE FUNCTION f() RETURNS INT LANGUAGE JAVASCRIPT AS $$ return 1; $$',
127+
'CREATE PROCEDURE p() LANGUAGE JAVASCRIPT AS $tag$ return 1; $tag$',
128+
);
129+
foreach ( $statements as $sql ) {
130+
$ast = self::parse( $sql );
131+
$this->assertNotNull( $ast, "Failed to parse: $sql" );
132+
$this->assertSame(
133+
' return 1; ',
134+
$ast->get_first_descendant_node( 'routine_string' )
135+
->get_first_descendant_token( WP_MySQL_Lexer::DOLLAR_QUOTED_STRING_SYM )
136+
->get_value()
137+
);
138+
}
139+
}
140+
124141
public function test_rejects_statements_invalid_in_mysql_84(): void {
125142
$statements = array(
143+
'SELECT $$a$$', // Dollar quotes are only for routine bodies.
144+
'SET @x = $$a$$', // Dollar quotes are only for routine bodies.
145+
'SELECT $a$b', // Unterminated dollar-quoted text.
146+
'CREATE FUNCTION f() RETURNS INT LANGUAGE JAVASCRIPT AS $$ a', // Unterminated.
147+
'CREATE FUNCTION f() RETURNS INT LANGUAGE JAVASCRIPT AS $$$$', // Empty body.
126148
'RESET MASTER', // Removed in MySQL 8.4.
127149
'SHOW SLAVE STATUS', // Removed in MySQL 8.4.
128150
'SELECT 1; SELECT 2', // Multi-statement input.

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

Lines changed: 1 addition & 1 deletion
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 = 69076;
23-
const EXPECTED_FAILURES = 166;
23+
const EXPECTED_FAILURES = 64;
2424

2525
public function test_corpus_acceptance_rate(): void {
2626
$parser = WP_MySQL_Parser_Factory::create_parser();

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ public function test_get_value_unquotes_national_strings(): void {
3434
$this->assertSame( "new\nline", self::first_token( "SELECT n'new\\nline'", 'NCHAR_STRING' )->get_value() );
3535
}
3636

37+
/**
38+
* @dataProvider data_dollar_quoted_values
39+
*/
40+
public function test_get_value_removes_dollar_quotes( string $sql, string $expected ): void {
41+
$this->assertSame( $expected, self::first_token( $sql, 'DOLLAR_QUOTED_STRING_SYM' )->get_value() );
42+
}
43+
44+
public function data_dollar_quoted_values(): array {
45+
return array(
46+
'empty tag' => array( '$$ a $$', ' a ' ),
47+
'tag' => array( '$tag$ a $tag$', ' a ' ),
48+
'a tag with a 4-byte character' => array( '$😀$ a $😀$', ' a ' ),
49+
'other delimiters in the text' => array( '$a$ $$ $b$ $a$', ' $$ $b$ ' ),
50+
'no escape sequences' => array( "\$\$ a\\nb '' \$\$", " a\\nb '' " ),
51+
);
52+
}
53+
3754
public function test_get_value_honors_no_backslash_escapes_mode(): void {
3855
$token = self::first_token( "SELECT 'a\\nb'", 'TEXT_STRING', array( 'NO_BACKSLASH_ESCAPES' ) );
3956
$this->assertSame( 'a\\nb', $token->get_value() );

0 commit comments

Comments
 (0)