Skip to content

Commit 2441dc4

Browse files
committed
fixup! Lex qualified names with MySQL's lexer states
Read the name after "." or "@@" in one check at the top of read_next_token(), as MySQL's lex_one_token() does with next_state, instead of checking the state in four branches. After "@@", a name that looks like a number, a literal, or a charset ("@@123", "@@b'01'", "@@_utf8mb4") is now a name, as in MySQL.
1 parent 24201b7 commit 2441dc4

4 files changed

Lines changed: 74 additions & 70 deletions

File tree

‎packages/mysql-on-sqlite/src/mysql/class-wp-mysql-lexer.php‎

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,7 +2203,8 @@ class WP_MySQL_Lexer {
22032203
* - STATE_IDENTIFIER_START: Set by a "." before a name character. The
22042204
* name is then always an identifier ("t.order", "t.1", "t.x'41'").
22052205
* - STATE_IDENTIFIER_OR_KEYWORD: Set by "@@" before a name character.
2206-
* The name then stays a keyword, even before a "." ("@@session.x").
2206+
* The name is then an identifier or a keyword, even before a "."
2207+
* ("@@session.x").
22072208
*
22082209
* A "." that ends a number sets no state, so "1.FROM" is a number and
22092210
* a keyword.
@@ -2460,6 +2461,22 @@ private function read_next_token(): ?int {
24602461
$state = $this->next_state;
24612462
$this->next_state = self::STATE_START;
24622463

2464+
// After "." or "@@", read a name, never a number or a literal, as in
2465+
// MySQL's MY_LEX_IDENT_START and MY_LEX_IDENT_OR_KEYWORD states. After
2466+
// ".", it is always an identifier ("t.order", "t.1", "t.x'41'"). After
2467+
// "@@", it may be a keyword, even before a "." ("@@session.x").
2468+
if ( self::STATE_START !== $state ) {
2469+
if ( self::STATE_IDENTIFIER_START === $state ) {
2470+
return $this->read_identifier();
2471+
}
2472+
if ( self::STATE_IDENTIFIER_OR_KEYWORD === $state ) {
2473+
$type = $this->read_identifier();
2474+
return self::IDENTIFIER === $type
2475+
? $this->determine_identifier_or_keyword_type( $this->get_current_token_bytes() )
2476+
: $type;
2477+
}
2478+
}
2479+
24632480
$byte = $this->sql[ $this->bytes_already_read ] ?? null;
24642481
$next_byte = $this->sql[ $this->bytes_already_read + 1 ] ?? null;
24652482

@@ -2495,25 +2512,18 @@ private function read_next_token(): ?int {
24952512
) {
24962513
$started_at = $this->bytes_already_read;
24972514
$type = $this->read_identifier();
2498-
2499-
// After a "." separator, it is always an identifier. Invalid input
2500-
// needs no check: the type is then null, and no keyword is empty.
2501-
if ( self::STATE_IDENTIFIER_START !== $state ) {
2515+
if ( self::IDENTIFIER === $type ) {
25022516
// Inline the keyword lookup on the hot identifier path: most
25032517
// identifiers are not keywords, so this avoids two method calls
25042518
// (token-bytes extraction + keyword determination) per token.
25052519
$keyword = self::TOKENS[ strtoupper(
25062520
substr( $this->sql, $started_at, $this->bytes_already_read - $started_at )
25072521
) ] ?? self::IDENTIFIER;
2508-
// Before "." and a name, a keyword is an identifier ("order.id"),
2509-
// except after "@@" ("session" in "@@session.x"). read_identifier()
2510-
// has just set STATE_IDENTIFIER_SEPARATOR if "." and a name follow.
2522+
// Before "." and a name, a keyword is an identifier ("order.id").
2523+
// read_identifier() has just set STATE_IDENTIFIER_SEPARATOR then.
25112524
if (
25122525
self::IDENTIFIER !== $keyword
2513-
&& (
2514-
self::STATE_IDENTIFIER_SEPARATOR !== $this->next_state
2515-
|| self::STATE_IDENTIFIER_OR_KEYWORD === $state
2516-
)
2526+
&& self::STATE_IDENTIFIER_SEPARATOR !== $this->next_state
25172527
) {
25182528
$type = $this->resolve_keyword_type( $keyword );
25192529
}
@@ -2525,12 +2535,7 @@ private function read_next_token(): ?int {
25252535
} elseif ( "'" === $byte || '"' === $byte || '`' === $byte ) {
25262536
$type = $this->read_quoted_text();
25272537
} elseif ( null !== $byte && strspn( $byte, self::DIGIT_MASK ) > 0 ) {
2528-
// After a "." separator, it is a qualified name part ("1" in "t.1").
2529-
if ( self::STATE_IDENTIFIER_START === $state ) {
2530-
$type = $this->read_identifier();
2531-
} else {
2532-
$type = $this->read_number();
2533-
}
2538+
$type = $this->read_number();
25342539
} elseif ( '.' === $byte ) {
25352540
if (
25362541
null !== $next_byte
@@ -2708,28 +2713,19 @@ private function read_next_token(): ?int {
27082713
} elseif ( null !== $byte && strspn( $byte, self::WHITESPACE_MASK ) > 0 ) {
27092714
$this->bytes_already_read += strspn( $this->sql, self::WHITESPACE_MASK, $this->bytes_already_read );
27102715
$type = self::WHITESPACE;
2711-
} elseif (
2712-
"'" === $next_byte
2713-
// Not after a "." separator, where the letter is a name ("x" in "t.x'41'").
2714-
&& self::STATE_IDENTIFIER_START !== $state
2715-
&& strspn( $byte, 'xXbBnN' ) > 0
2716-
) {
2717-
if ( 'n' === $byte || 'N' === $byte ) {
2718-
$this->bytes_already_read += 1; // n/N
2719-
$type = $this->read_quoted_text( "'" );
2720-
if ( self::SINGLE_QUOTED_TEXT === $type ) {
2721-
$type = self::NCHAR_TEXT;
2722-
}
2723-
} else {
2724-
$type = $this->read_number();
2716+
} elseif ( ( 'x' === $byte || 'X' === $byte || 'b' === $byte || 'B' === $byte ) && "'" === $next_byte ) {
2717+
$type = $this->read_number();
2718+
} elseif ( ( 'n' === $byte || 'N' === $byte ) && "'" === $next_byte ) {
2719+
$this->bytes_already_read += 1; // n/N
2720+
$type = $this->read_quoted_text( "'" );
2721+
if ( self::SINGLE_QUOTED_TEXT === $type ) {
2722+
$type = self::NCHAR_TEXT;
27252723
}
27262724
} elseif ( null === $byte ) {
27272725
$type = self::EOF;
27282726
} else {
27292727
$type = $this->read_identifier();
2730-
2731-
// After a "." separator, it is always an identifier.
2732-
if ( self::IDENTIFIER === $type && self::STATE_IDENTIFIER_START !== $state ) {
2728+
if ( self::IDENTIFIER === $type ) {
27332729
if ( '_' === $byte && isset( self::UNDERSCORE_CHARSETS[ strtolower( $this->get_current_token_bytes() ) ] ) ) {
27342730
$type = self::UNDERSCORE_CHARSET;
27352731
} else {

‎packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,12 @@ public function data_qualified_name_tokens(): array {
508508
'keyword before whitespace' => array( 'order .id', array( 'ORDER_SYMBOL', 'DOT_SYMBOL', 'IDENTIFIER' ) ),
509509
'system variable scope' => array( '@@session.sql_mode', array( 'AT_AT_SIGN_SYMBOL', 'SESSION_SYMBOL', 'DOT_SYMBOL', 'IDENTIFIER' ) ),
510510
'charset introducer' => array( '_utf8mb4.x', array( 'UNDERSCORE_CHARSET', 'DOT_SYMBOL', 'IDENTIFIER' ) ),
511+
512+
// After "@@", it is a name, never a number, a literal, or a charset.
513+
'system variable digits' => array( '@@123', array( 'AT_AT_SIGN_SYMBOL', 'IDENTIFIER' ) ),
514+
'system variable float-like' => array( '@@1e5', array( 'AT_AT_SIGN_SYMBOL', 'IDENTIFIER' ) ),
515+
'system variable before text' => array( "@@b'01'", array( 'AT_AT_SIGN_SYMBOL', 'IDENTIFIER', 'SINGLE_QUOTED_TEXT' ) ),
516+
'system variable charset' => array( '@@_utf8mb4', array( 'AT_AT_SIGN_SYMBOL', 'IDENTIFIER' ) ),
511517
);
512518
}
513519

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

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ class WP_MySQL_Lexer {
297297
* - STATE_IDENTIFIER_START: Set by a "." before a name character. The
298298
* name is then always an identifier ("t.order", "t.1", "t.x'41'").
299299
* - STATE_IDENTIFIER_OR_KEYWORD: Set by "@@" before a name character.
300-
* The name then stays a keyword, even before a "." ("@@session.x").
300+
* The name is then an identifier or a keyword, even before a "."
301+
* ("@@session.x").
301302
*
302303
* A "." that ends a number sets no state, so "1.FROM" is a number and
303304
* a keyword.
@@ -793,6 +794,22 @@ private function read_next_token(): ?int {
793794
$state = $this->next_state;
794795
$this->next_state = self::STATE_START;
795796

797+
// After "." or "@@", read a name, never a number or a literal, as in
798+
// MySQL's MY_LEX_IDENT_START and MY_LEX_IDENT_OR_KEYWORD states. After
799+
// ".", it is always an identifier ("t.order", "t.1", "t.x'41'"). After
800+
// "@@", it may be a keyword, even before a "." ("@@session.x").
801+
if ( self::STATE_START !== $state ) {
802+
if ( self::STATE_IDENTIFIER_START === $state ) {
803+
return $this->read_identifier();
804+
}
805+
if ( self::STATE_IDENTIFIER_OR_KEYWORD === $state ) {
806+
$type = $this->read_identifier();
807+
return self::IDENTIFIER === $type
808+
? $this->determine_identifier_or_keyword_type( $this->get_current_token_bytes() )
809+
: $type;
810+
}
811+
}
812+
796813
$byte = $this->sql[ $this->bytes_already_read ] ?? null;
797814
$next_byte = $this->sql[ $this->bytes_already_read + 1 ] ?? null;
798815

@@ -828,26 +845,19 @@ private function read_next_token(): ?int {
828845
) {
829846
$started_at = $this->bytes_already_read;
830847
$type = $this->read_identifier();
831-
832-
// After a "." separator, it is always an identifier. Invalid input
833-
// needs no check: the type is then null, and no keyword is empty.
834-
if ( self::STATE_IDENTIFIER_START !== $state ) {
848+
if ( self::IDENTIFIER === $type ) {
835849
// Inline the keyword lookup on the hot identifier path: most
836850
// identifiers are not keywords, so this avoids two method calls
837851
// (token-bytes extraction + keyword determination) per token.
838852
$word = strtoupper(
839853
substr( $this->sql, $started_at, $this->bytes_already_read - $started_at )
840854
);
841855
$keyword = self::KEYWORDS[ $word ] ?? self::IDENTIFIER;
842-
// Before "." and a name, a keyword is an identifier ("order.id"),
843-
// except after "@@" ("session" in "@@session.x"). read_identifier()
844-
// has just set STATE_IDENTIFIER_SEPARATOR if "." and a name follow.
856+
// Before "." and a name, a keyword is an identifier ("order.id").
857+
// read_identifier() has just set STATE_IDENTIFIER_SEPARATOR then.
845858
if (
846859
self::IDENTIFIER !== $keyword
847-
&& (
848-
self::STATE_IDENTIFIER_SEPARATOR !== $this->next_state
849-
|| self::STATE_IDENTIFIER_OR_KEYWORD === $state
850-
)
860+
&& self::STATE_IDENTIFIER_SEPARATOR !== $this->next_state
851861
) {
852862
$type = $this->resolve_keyword_type( $keyword, $word );
853863
}
@@ -859,12 +869,7 @@ private function read_next_token(): ?int {
859869
} elseif ( "'" === $byte || '"' === $byte || '`' === $byte ) {
860870
$type = $this->read_quoted_text();
861871
} elseif ( null !== $byte && strspn( $byte, self::DIGIT_MASK ) > 0 ) {
862-
// After a "." separator, it is a qualified name part ("1" in "t.1").
863-
if ( self::STATE_IDENTIFIER_START === $state ) {
864-
$type = $this->read_identifier();
865-
} else {
866-
$type = $this->read_number();
867-
}
872+
$type = $this->read_number();
868873
} elseif ( '.' === $byte ) {
869874
if (
870875
null !== $next_byte
@@ -1013,28 +1018,19 @@ private function read_next_token(): ?int {
10131018
} elseif ( null !== $byte && strspn( $byte, self::WHITESPACE_MASK ) > 0 ) {
10141019
$this->bytes_already_read += strspn( $this->sql, self::WHITESPACE_MASK, $this->bytes_already_read );
10151020
$type = self::WHITESPACE;
1016-
} elseif (
1017-
"'" === $next_byte
1018-
// Not after a "." separator, where the letter is a name ("x" in "t.x'41'").
1019-
&& self::STATE_IDENTIFIER_START !== $state
1020-
&& strspn( $byte, 'xXbBnN' ) > 0
1021-
) {
1022-
if ( 'n' === $byte || 'N' === $byte ) {
1023-
$this->bytes_already_read += 1;
1024-
$type = $this->read_quoted_text( "'" );
1025-
if ( self::SINGLE_QUOTED_TEXT === $type ) {
1026-
$type = self::NCHAR_TEXT;
1027-
}
1028-
} else {
1029-
$type = $this->read_number();
1021+
} elseif ( ( 'x' === $byte || 'X' === $byte || 'b' === $byte || 'B' === $byte ) && "'" === $next_byte ) {
1022+
$type = $this->read_number();
1023+
} elseif ( ( 'n' === $byte || 'N' === $byte ) && "'" === $next_byte ) {
1024+
$this->bytes_already_read += 1;
1025+
$type = $this->read_quoted_text( "'" );
1026+
if ( self::SINGLE_QUOTED_TEXT === $type ) {
1027+
$type = self::NCHAR_TEXT;
10301028
}
10311029
} elseif ( null === $byte ) {
10321030
$type = self::EOF;
10331031
} else {
10341032
$type = $this->read_identifier();
1035-
1036-
// After a "." separator, it is always an identifier.
1037-
if ( self::IDENTIFIER === $type && self::STATE_IDENTIFIER_START !== $state ) {
1033+
if ( self::IDENTIFIER === $type ) {
10381034
if ( '_' === $byte && isset( self::UNDERSCORE_CHARSETS[ strtolower( $this->get_current_token_bytes() ) ] ) ) {
10391035
$type = self::UNDERSCORE_CHARSET;
10401036
} else {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,12 @@ public function data_qualified_name_tokens(): array {
502502
'keyword before whitespace' => array( 'order .id', array( 'ORDER', 'DOT_SYMBOL', 'IDENTIFIER' ) ),
503503
'system variable scope' => array( '@@session.sql_mode', array( 'AT_SIGN_SYMBOL', 'AT_SIGN_SYMBOL', 'SESSION', 'DOT_SYMBOL', 'IDENTIFIER' ) ),
504504
'charset introducer' => array( '_utf8mb4.x', array( 'UNDERSCORE_CHARSET', 'DOT_SYMBOL', 'IDENTIFIER' ) ),
505+
506+
// After "@@", it is a name, never a number, a literal, or a charset.
507+
'system variable digits' => array( '@@123', array( 'AT_SIGN_SYMBOL', 'AT_SIGN_SYMBOL', 'IDENTIFIER' ) ),
508+
'system variable float-like' => array( '@@1e5', array( 'AT_SIGN_SYMBOL', 'AT_SIGN_SYMBOL', 'IDENTIFIER' ) ),
509+
'system variable before text' => array( "@@b'01'", array( 'AT_SIGN_SYMBOL', 'AT_SIGN_SYMBOL', 'IDENTIFIER', 'SINGLE_QUOTED_TEXT' ) ),
510+
'system variable charset' => array( '@@_utf8mb4', array( 'AT_SIGN_SYMBOL', 'AT_SIGN_SYMBOL', 'IDENTIFIER' ) ),
505511
);
506512
}
507513

0 commit comments

Comments
 (0)