Skip to content

Commit 193362a

Browse files
committed
Keep IGNORE_SPACE whitespace out of function-name token ranges
When resolving a function keyword (SYM_FN), the lexer peeks for a following "(" and, under SQL_MODE_IGNORE_SPACE, skips intervening whitespace first. It skipped by advancing bytes_already_read and never restored it. When no "(" followed, the keyword was emitted as an IDENTIFIER whose length — derived from bytes_already_read in produce() — now covered the trailing whitespace, so the extracted value was e.g. "COUNT " instead of "COUNT". Under this ANSI-style mode a column or table named after a function would resolve to the wrong identifier. Peek with a local index instead of mutating bytes_already_read, so the token's byte range ends at the keyword and the next scan consumes the whitespace.
1 parent c5d9ab6 commit 193362a

5 files changed

Lines changed: 75 additions & 8 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,11 +3078,12 @@ private function resolve_keyword_type( int $type ): int {
30783078

30793079
// Determine function calls.
30803080
if ( isset( self::FUNCTIONS[ $type ] ) ) {
3081-
// Skip any whitespace character if the SQL mode says they should be ignored.
3081+
// Keep ignored whitespace outside the current token range.
3082+
$peek = $this->bytes_already_read;
30823083
if ( $this->is_sql_mode_active( self::SQL_MODE_IGNORE_SPACE ) ) {
3083-
$this->bytes_already_read += strspn( $this->sql, self::WHITESPACE_MASK, $this->bytes_already_read );
3084+
$peek += strspn( $this->sql, self::WHITESPACE_MASK, $peek );
30843085
}
3085-
if ( '(' !== ( $this->sql[ $this->bytes_already_read ] ?? null ) ) {
3086+
if ( '(' !== ( $this->sql[ $peek ] ?? null ) ) {
30863087
return self::IDENTIFIER;
30873088
}
30883089
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,38 @@ public function data_underscore_charset_after_dot(): array {
422422
);
423423
}
424424

425+
public function test_ignore_space_preserves_function_name_ranges(): void {
426+
// Without "(", COUNT is an identifier whose range excludes trailing whitespace.
427+
$sql = 'SELECT COUNT FROM t';
428+
$tokens = ( new WP_MySQL_Lexer( $sql, 80400, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
429+
$this->assertSame( WP_MySQL_Lexer::IDENTIFIER, $tokens[1]->id, $sql );
430+
$this->assertSame( 'COUNT', $tokens[1]->get_value(), $sql );
431+
$this->assertSame( 5, $tokens[1]->length, $sql );
432+
$this->assertSame( 13, $tokens[2]->start, $sql );
433+
434+
$sql = "SELECT COUNT\t\n FROM t";
435+
$tokens = ( new WP_MySQL_Lexer( $sql, 80400, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
436+
$this->assertSame( WP_MySQL_Lexer::IDENTIFIER, $tokens[1]->id, $sql );
437+
$this->assertSame( 'COUNT', $tokens[1]->get_value(), $sql );
438+
$this->assertSame( 5, $tokens[1]->length, $sql );
439+
$this->assertSame( 15, $tokens[2]->start, $sql );
440+
441+
// With "(", COUNT is a function whose range still excludes the whitespace.
442+
$tokens = ( new WP_MySQL_Lexer( 'SELECT COUNT (1)', 80400, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
443+
$this->assertSame( WP_MySQL_Lexer::COUNT_SYMBOL, $tokens[1]->id );
444+
$this->assertSame( 5, $tokens[1]->length );
445+
$this->assertSame( WP_MySQL_Lexer::OPEN_PAR_SYMBOL, $tokens[2]->id );
446+
$this->assertSame( 13, $tokens[2]->start );
447+
448+
$sql = "SELECT COUNT \t\n";
449+
$tokens = ( new WP_MySQL_Lexer( $sql, 80400, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
450+
$this->assertSame( WP_MySQL_Lexer::IDENTIFIER, $tokens[1]->id, $sql );
451+
$this->assertSame( 'COUNT', $tokens[1]->get_value(), $sql );
452+
$this->assertSame( 5, $tokens[1]->length, $sql );
453+
$this->assertSame( WP_MySQL_Lexer::EOF, $tokens[2]->id, $sql );
454+
$this->assertSame( strlen( $sql ), $tokens[2]->start, $sql );
455+
}
456+
425457
/**
426458
* @dataProvider data_ansi_quotes_tokenization
427459
*/

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,11 +1354,12 @@ private function resolve_keyword_type( int $type, string $word ): int {
13541354
// Function keywords (declared with SYM_FN in MySQL's lex.h) are keywords
13551355
// only when directly followed by an opening parenthesis.
13561356
if ( isset( self::FUNCTIONS[ $word ] ) ) {
1357-
// Skip any whitespace character if the SQL mode says they should be ignored.
1357+
// Keep ignored whitespace outside the current token range.
1358+
$peek = $this->bytes_already_read;
13581359
if ( $this->is_sql_mode_active( self::SQL_MODE_IGNORE_SPACE ) ) {
1359-
$this->bytes_already_read += strspn( $this->sql, self::WHITESPACE_MASK, $this->bytes_already_read );
1360+
$peek += strspn( $this->sql, self::WHITESPACE_MASK, $peek );
13601361
}
1361-
if ( '(' !== ( $this->sql[ $this->bytes_already_read ] ?? null ) ) {
1362+
if ( '(' !== ( $this->sql[ $peek ] ?? null ) ) {
13621363
return self::IDENTIFIER;
13631364
}
13641365
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,38 @@ public function test_emits_bison_terminals_with_end_markers(): void {
8585
);
8686
}
8787

88+
public function test_ignore_space_preserves_function_name_ranges(): void {
89+
// Without "(", COUNT is an identifier whose range excludes trailing whitespace.
90+
$sql = 'SELECT COUNT FROM t';
91+
$tokens = ( new WP_MySQL_Lexer( $sql, 80400, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
92+
$this->assertSame( 'IDENTIFIER', $tokens[1]->get_name(), $sql );
93+
$this->assertSame( 'COUNT', $tokens[1]->get_value(), $sql );
94+
$this->assertSame( 5, $tokens[1]->length, $sql );
95+
$this->assertSame( 13, $tokens[2]->start, $sql );
96+
97+
$sql = "SELECT COUNT\t\n FROM t";
98+
$tokens = ( new WP_MySQL_Lexer( $sql, 80400, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
99+
$this->assertSame( 'IDENTIFIER', $tokens[1]->get_name(), $sql );
100+
$this->assertSame( 'COUNT', $tokens[1]->get_value(), $sql );
101+
$this->assertSame( 5, $tokens[1]->length, $sql );
102+
$this->assertSame( 15, $tokens[2]->start, $sql );
103+
104+
// 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();
106+
$this->assertSame( 5, $tokens[1]->length );
107+
$this->assertSame( 'COUNT', $tokens[1]->get_name() );
108+
$this->assertSame( 'OPEN_PAR_SYMBOL', $tokens[2]->get_name() );
109+
$this->assertSame( 13, $tokens[2]->start );
110+
111+
$sql = "SELECT COUNT \t\n";
112+
$tokens = ( new WP_MySQL_Lexer( $sql, 80400, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
113+
$this->assertSame( 'IDENTIFIER', $tokens[1]->get_name(), $sql );
114+
$this->assertSame( 'COUNT', $tokens[1]->get_value(), $sql );
115+
$this->assertSame( 5, $tokens[1]->length, $sql );
116+
$this->assertSame( 'END_OF_INPUT', $tokens[2]->get_name(), $sql );
117+
$this->assertSame( strlen( $sql ), $tokens[2]->start, $sql );
118+
}
119+
88120
public function test_at_name_splits_into_at_and_ident(): void {
89121
$tokens = ( new WP_MySQL_Lexer( 'SELECT @var1' ) )->remaining_tokens();
90122
$this->assertSame( 'AT_SIGN_SYMBOL', $tokens[1]->get_name() );

‎packages/php-ext-wp-mysql-parser/src/lib.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,12 +904,13 @@ impl WpMySqlNativeLexer {
904904
}
905905

906906
if lex::is_function_token(token_type) {
907+
let mut peek = self.bytes_already_read;
907908
if self.is_sql_mode_active(SQL_MODE_IGNORE_SPACE) {
908-
self.bytes_already_read = span_while(&self.sql, self.bytes_already_read, |byte| {
909+
peek = span_while(&self.sql, peek, |byte| {
909910
byte_in(byte, lex::WHITESPACE_MASK.as_bytes())
910911
});
911912
}
912-
if self.byte_at(self.bytes_already_read) != Some(b'(') {
913+
if self.byte_at(peek) != Some(b'(') {
913914
return lex::IDENTIFIER;
914915
}
915916
}

0 commit comments

Comments
 (0)