Skip to content

Commit deafe5d

Browse files
authored
Add MySQL-judged REGEXP test suites (#622)
## Summary This PR adds **two extensive REGEXP test suites, judged by MySQL 8.4**. Each case expects the result that a real MySQL server returned, so the tests show exactly where the driver differs from MySQL. The cases come from two sources: - **MySQL server test suite:** 12,684 cases, built from the `REGEXP`, `RLIKE`, and `REGEXP_*()` patterns in MySQL's own tests. - **ICU's `regextst.txt`:** 3,965 cases from the regex tests of ICU, the library that MySQL uses for regular expressions. ICU's copyright and license (Unicode License V3) are kept next to the data. Each case stores MySQL's results for `REGEXP_LIKE()`, `REGEXP_INSTR()`, `REGEXP_SUBSTR()`, and `REGEXP_REPLACE()`. There is one test class per function, and it lists the cases that fail today in `get_known_failures()`. A test fails when an unlisted case fails, **and also when a listed case starts to pass**, so the list must shrink as REGEXP support improves. Cases that fail only with some PHP or PCRE versions are listed under version checks that name the cause. To measure the current state, this PR also adds **a basic `REGEXP_LIKE()`**. It reuses the existing matching of the `REGEXP` operator and adds match types. Like in MySQL, it returns `NULL` for a `NULL` argument, and so does the operator now. With PHP 8.5 and PCRE2 10.49, it fails 302 of the 12,684 MySQL cases and 951 of the 3,965 ICU cases. The tests of the other functions are skipped until the functions exist. To rebuild the data from a MySQL server, run `tests/tools/generate-regexp-test-data.php`. ## Why REGEXP support needs improvements, which will come in later PRs. These suites give us a baseline to measure them: a fix shows up as known failures that now pass, and a regression as a new failure. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * Added support for MySQL-compatible `REGEXP_LIKE` matching, including case sensitivity, multiline and dot-all options, and `NULL` results when inputs are `NULL`. * The `REGEXP` and `NOT REGEXP` operators now return `NULL` when their inputs require it. * **Tests** * Added broad compatibility checks for regular expression behavior. Support for `REGEXP_INSTR`, `REGEXP_REPLACE`, and `REGEXP_SUBSTR` remains unavailable. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent bcde35e commit deafe5d

11 files changed

Lines changed: 19097 additions & 11 deletions

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php‎

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public static function register_for( $pdo ): self {
6161
'localtimestamp' => 'now',
6262
'isnull' => 'isnull',
6363
'regexp' => 'regexp',
64+
'regexp_like' => 'regexp_like',
6465
'field' => 'field',
6566
'log' => 'log',
6667
'least' => 'least',
@@ -565,12 +566,15 @@ public function isnull( $field ) {
565566
}
566567

567568
/**
568-
* Method to emulate MySQL REGEXP() function.
569+
* Method to emulate MySQL REGEXP operator.
569570
*
570-
* @param string $pattern Regular expression to match.
571-
* @param string $field Haystack.
571+
* SQLite runs "X REGEXP Y" as "regexp(Y, X)". MySQL runs the operator
572+
* as REGEXP_LIKE(), so this method uses regexp_like().
572573
*
573-
* @return integer 1 if matched, 0 if not matched.
574+
* @param string|null $pattern Regular expression to match.
575+
* @param string|null $field Haystack.
576+
*
577+
* @return int|null 1 if matched, 0 if not matched, NULL if an argument is NULL.
574578
*/
575579
public function regexp( $pattern, $field ) {
576580
/*
@@ -587,17 +591,62 @@ public function regexp( $pattern, $field ) {
587591
* be reasonably safe since PHP does not allow null bytes in
588592
* regular expressions anyway.
589593
*/
590-
if ( "\x00" === $pattern[0] ) {
591-
$pattern = substr( $pattern, 1 );
592-
$flags = '';
593-
} else {
594-
// Otherwise, the search is case-insensitive.
595-
$flags = 'i';
594+
if ( is_string( $pattern ) && 0 === strpos( $pattern, "\x00" ) ) {
595+
return $this->regexp_like( $field, substr( $pattern, 1 ), 'c' );
596+
}
597+
return $this->regexp_like( $field, $pattern );
598+
}
599+
600+
/**
601+
* Method to emulate MySQL REGEXP_LIKE() function.
602+
*
603+
* @param string|null $subject Haystack.
604+
* @param string|null $pattern Regular expression to match.
605+
* @param string|null $match_type Optional. Letters that change how the pattern matches.
606+
* Without "c" or "i", the match is case-insensitive.
607+
*
608+
* @throws Exception When the match type has an unknown letter.
609+
* @return int|null 1 if matched, 0 if not matched, NULL if an argument is NULL.
610+
*/
611+
public function regexp_like( $subject, $pattern, $match_type = '' ) {
612+
if ( null === $match_type ) {
613+
return null;
596614
}
615+
616+
/*
617+
* Apply the letters in order, so that the last of "c" and "i" wins, like in MySQL.
618+
* The "u" letter needs no flag, as PCRE treats only "\n" as a line break by default.
619+
*/
620+
$case_insensitive = true;
621+
$flags = '';
622+
$length = strlen( $match_type );
623+
for ( $i = 0; $i < $length; $i++ ) {
624+
$letter = $match_type[ $i ];
625+
if ( 'c' === $letter ) {
626+
$case_insensitive = false;
627+
} elseif ( 'i' === $letter ) {
628+
$case_insensitive = true;
629+
} elseif ( 'm' === $letter ) {
630+
$flags .= 'm';
631+
} elseif ( 'n' === $letter ) {
632+
$flags .= 's';
633+
} elseif ( 'u' !== $letter ) {
634+
throw new Exception( 'Incorrect arguments to regexp_like' );
635+
}
636+
}
637+
if ( $case_insensitive ) {
638+
$flags .= 'i';
639+
}
640+
641+
// Like MySQL, reject an invalid match type even for a NULL subject or pattern.
642+
if ( null === $subject || null === $pattern ) {
643+
return null;
644+
}
645+
597646
$pattern = str_replace( '/', '\/', $pattern );
598647
$pattern = '/' . $pattern . '/' . $flags;
599648

600-
return preg_match( $pattern, $field );
649+
return preg_match( $pattern, $subject );
601650
}
602651

603652
/**

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,52 @@ public static function regexpOperators() {
266266
);
267267
}
268268

269+
/**
270+
* @dataProvider regexpLikeQueries
271+
*/
272+
public function testRegexpLike( $query, $expected_result ) {
273+
$this->assertQuery( $query );
274+
$this->assertSame( $expected_result, $this->last_result[0]->result );
275+
}
276+
277+
public static function regexpLikeQueries() {
278+
return array(
279+
'no match type' => array( "SELECT REGEXP_LIKE('ABC', 'b') AS result", '1' ),
280+
'empty match type' => array( "SELECT REGEXP_LIKE('ABC', 'b', '') AS result", '1' ),
281+
'case-sensitive' => array( "SELECT REGEXP_LIKE('ABC', 'b', 'c') AS result", '0' ),
282+
'case-insensitive' => array( "SELECT REGEXP_LIKE('ABC', 'b', 'i') AS result", '1' ),
283+
'last "i" wins' => array( "SELECT REGEXP_LIKE('ABC', 'b', 'ci') AS result", '1' ),
284+
'last "c" wins' => array( "SELECT REGEXP_LIKE('ABC', 'b', 'ic') AS result", '0' ),
285+
'single line' => array( "SELECT REGEXP_LIKE('a\\nb', '^b') AS result", '0' ),
286+
'multiline' => array( "SELECT REGEXP_LIKE('a\\nb', '^b', 'm') AS result", '1' ),
287+
'dot without line break' => array( "SELECT REGEXP_LIKE('a\\nb', 'a.b') AS result", '0' ),
288+
'dot with line break' => array( "SELECT REGEXP_LIKE('a\\nb', 'a.b', 'n') AS result", '1' ),
289+
'Unix line breaks' => array( "SELECT REGEXP_LIKE('a\\nb', '^b', 'mu') AS result", '1' ),
290+
'repeated letters' => array( "SELECT REGEXP_LIKE('a\\nb', '^b', 'mm') AS result", '1' ),
291+
'NULL subject' => array( "SELECT REGEXP_LIKE(NULL, 'b') AS result", null ),
292+
'NULL pattern' => array( "SELECT REGEXP_LIKE('ABC', NULL) AS result", null ),
293+
'NULL match type' => array( "SELECT REGEXP_LIKE('ABC', 'b', NULL) AS result", null ),
294+
'NULL REGEXP subject' => array( "SELECT NULL REGEXP 'b' AS result", null ),
295+
'NULL REGEXP pattern' => array( "SELECT 'ABC' REGEXP NULL AS result", null ),
296+
'NULL NOT REGEXP' => array( "SELECT NULL NOT REGEXP 'b' AS result", null ),
297+
);
298+
}
299+
300+
/**
301+
* @dataProvider regexpLikeInvalidMatchTypeQueries
302+
*/
303+
public function testRegexpLikeWithInvalidMatchType( $query ) {
304+
$this->assertQueryError( $query, 'Incorrect arguments to regexp_like' );
305+
}
306+
307+
public static function regexpLikeInvalidMatchTypeQueries() {
308+
return array(
309+
'subject and pattern' => array( "SELECT REGEXP_LIKE('ABC', 'b', 'x')" ),
310+
'NULL subject' => array( "SELECT REGEXP_LIKE(NULL, 'b', 'x')" ),
311+
'NULL pattern' => array( "SELECT REGEXP_LIKE('ABC', NULL, 'x')" ),
312+
);
313+
}
314+
269315
public function testInsertDateNow() {
270316
$this->assertQuery(
271317
"INSERT INTO _dates (option_name, option_value) VALUES ('first', now());"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
require_once __DIR__ . '/WP_MySQL_On_SQLite_Regexp_Test_Case.php';
4+
5+
/**
6+
* Check REGEXP_INSTR() against MySQL's results for the REGEXP test cases.
7+
*/
8+
class WP_MySQL_On_SQLite_Regexp_Instr_Tests extends WP_MySQL_On_SQLite_Regexp_Test_Case {
9+
const CHECKS = array( 'regexp_instr', 'regexp_instr_end' );
10+
11+
protected function setUp(): void {
12+
$this->markTestSkipped( 'REGEXP_INSTR() is not supported yet.' );
13+
}
14+
}

0 commit comments

Comments
 (0)