Skip to content

Commit 869f386

Browse files
committed
Read delimiters that start with a quote in the query corpus extractor
multi_statement.test sets the delimiter to 'abcd', with the quotes. The extractor handled the opening quote before it looked for the delimiter, so it never found it, and the rest of the file was lost. As in mysqltest, it now looks for the delimiter first. The corpus grows from 78,147 to 78,153 rows. The pinned failures go from 142 to 144, because 2 of the new rows have several statements in one string, which MySQL 8.4.10 also rejects without its multi-statement client flag.
1 parent a18d7d5 commit 869f386

4 files changed

Lines changed: 32 additions & 6 deletions

File tree

‎packages/mysql-parser/data/mysql-server-query-corpus/mysql-latest.csv‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61770,6 +61770,20 @@ ORDER BY 1"
6177061770
"select 2;
6177161771
select 3;
6177261772
select 4"
61773+
"select 5;
61774+
select 6;
61775+
select 50, 'abc';"
61776+
"select ""abcd'"";"
61777+
"select ""'abcd"";"
61778+
"select 5"
61779+
"select 'finish'"
61780+
"create table t1 (i int);
61781+
insert into t1 values (1);
61782+
select * from t1 where i = 1;
61783+
insert into t1 values (2),(3),(4);
61784+
select * from t1 where i = 2;
61785+
select * from t1 where i = 3"
61786+
"show status like 'Slow_queries'"
6177361787
"CALL mtr.add_suppression(""Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT."")"
6177461788
"CALL mtr.add_suppression(""REVOKE/GRANT failed while storing table level ""
6177561789
""and column level grants in the privilege ""
@@ -117736,7 +117750,6 @@ SELECT * FROM t1 UNION SELECT * FROM t2) q"
117736117750
"create table t1 (a int, index (a), b int)"
117737117751
"insert t1 values (1,1),(2,2),(3,3),(4,4),(5,5)"
117738117752
"insert t1 select a+1, a+b from t1"
117739-
"show status like 'Slow_queries'"
117740117753
"select count(*) from t1 where a=7"
117741117754
"select count(*) from t1 where b=13"
117742117755
"select count(*) from t1 where b=13 union select count(*) from t1 where a=7"

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class WP_MySQL_Server_Suite_Parser_Tests extends TestCase {
1616
* The exact corpus tally. The parser should reject a row marked as a syntax error,
1717
* and accept any other row. A failure is a row where it does the opposite.
1818
*
19-
* 43 failures are regular rows that the parser rejects. They are multi-statement
19+
* 45 failures are regular rows that the parser rejects. They are multi-statement
2020
* input, which needs the CLIENT_MULTI_STATEMENTS client flag. MySQL rejects them
2121
* too without it.
2222
*
@@ -29,8 +29,8 @@ class WP_MySQL_Server_Suite_Parser_Tests extends TestCase {
2929
* The exact failing set is pinned in FAILURES_PATH; update it when the accepted
3030
* language legitimately changes.
3131
*/
32-
const EXPECTED_QUERIES = 78147;
33-
const EXPECTED_FAILURES = 142;
32+
const EXPECTED_QUERIES = 78153;
33+
const EXPECTED_FAILURES = 144;
3434

3535
public function test_corpus_acceptance_rate(): void {
3636
$parser = WP_MySQL_Parser_Factory::create_parser();
@@ -99,6 +99,7 @@ public function data_query_texts(): array {
9999
'carriage return in a version comment' => array( "DO 1 /*!80034\r+1*/" ),
100100
'a delimiter set with --delimiter' => array( "CREATE PROCEDURE p_flush_status()\nBEGIN\nDECLARE x INT DEFAULT 3000;\nWHILE x DO\nSET x = x-1;\nFLUSH STATUS;\nEND WHILE;\nEND " ),
101101
'a query after --delimiter ;' => array( 'DROP PROCEDURE p_flush_status' ),
102+
'a delimiter that starts with a quote' => array( "select \"abcd'\";" ),
102103
'a query after an indented --error' => array( 'call po_add_order_line (99, 1, 100, 1)' ),
103104
'a query after an indented comment' => array( "UPDATE t0 SET c0 = 1 WHERE (t0.c0 IS NULL)>>('' COLLATE 'utf8mb4_0900_ai_ci')" ),
104105
'a query after an indented command' => array( "DO version_tokens_lock_exclusive('vt3', 'vt5', 'vt1', 4)" ),

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,12 @@ partition by range columns(a,b)
539539
insert into t1 values (1),(2),(3);
540540
select * from t1 where i = 2;
541541
drop table t1;"
542+
"create table t1 (i int);
543+
insert into t1 values (1);
544+
select * from t1 where i = 1;
545+
insert into t1 values (2),(3),(4);
546+
select * from t1 where i = 2;
547+
select * from t1 where i = 3"
542548
"create table t4 (c1 int) engine=myisam pack_keys=2"
543549
"create view v1 as select a from t3;
544550
create procedure bug10100pt(level int, lim int)
@@ -615,6 +621,9 @@ end"
615621
"select 2;
616622
select 3;
617623
select 4"
624+
"select 5;
625+
select 6;
626+
select 50, 'abc';"
618627
"select ?"
619628
"select ? from t1"
620629
"select @update_failed;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,11 @@ function ( $command ) {
353353
for ( $i = 0; $i < strlen( $line ); $i++ ) {
354354
$char = $line[ $i ];
355355

356+
// As in mysqltest, a delimiter outside quotes ends a command, even if it starts with a quote.
357+
$is_delimiter = null === $quotes && $char === $delimiter[0] && substr( $line, $i, strlen( $delimiter ) ) === $delimiter;
358+
356359
// Handle quotes.
357-
if ( '\'' === $char || '"' === $char || '`' === $char ) {
360+
if ( ! $is_delimiter && ( '\'' === $char || '"' === $char || '`' === $char ) ) {
358361
$prefix = substr( $line, 0, $i );
359362
$slashes = strlen( $prefix ) - strlen( rtrim( $prefix, '\\' ) );
360363
$is_escaped = 1 === $slashes % 2;
@@ -368,7 +371,7 @@ function ( $command ) {
368371
}
369372

370373
// Found delimiter - end query or command.
371-
if ( $char === $delimiter[0] && substr( $line, $i, strlen( $delimiter ) ) === $delimiter && null === $quotes ) {
374+
if ( $is_delimiter ) {
372375
$i += strlen( $delimiter ) - 1;
373376
$char = $line[ $i ] ?? null;
374377

0 commit comments

Comments
 (0)