From 30b46ea5c55cc9bd4e339343819f1113361f9e49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jake=C5=A1?= Date: Tue, 21 Apr 2026 17:00:16 +0200 Subject: [PATCH 1/6] Compile REGEXP patterns with MySQL's options and errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit REGEXP_LIKE() and the REGEXP operator wrapped the pattern in slashes and passed it to preg_match() as it was. Compile it in regexp_compile() instead: - Text is matched as UTF-8, so "." matches "é", and case-insensitive matching works beyond ASCII. - Any Unicode line break ends a line, like in MySQL. The "u" flag limits that to "\n". - Escaped slashes, as in '\/', work. - Quoted text without "\E" can end with a backslash, as in '\Qa\'. - An empty pattern is an error, like in MySQL, instead of matching everything. - Up to 64 compiled patterns are cached. regexp_fail() reports invalid patterns and PCRE limits as errors. Before, an invalid pattern caused a PHP warning, and a pattern over PCRE's limits silently didn't match. Data that is not valid UTF-8 is matched byte by byte and case-sensitively, like MySQL binary strings, though the match type of REGEXP_LIKE() can still ignore case. In such data, only CR and LF end a line, as the byte 0x85 is a part of characters like "ą". Register REGEXP_LIKE() with its exact argument counts, so a wrong count is an error, like in MySQL. --- ...s-wp-sqlite-pdo-user-defined-functions.php | 311 ++++++++-- .../tests/WP_MySQL_On_SQLite_Tests.php | 320 +++++++++- .../WP_MySQL_On_SQLite_Regexp_Like_Tests.php | 550 ++---------------- 3 files changed, 606 insertions(+), 575 deletions(-) diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php index a59ee925b..7021d3fba 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php @@ -22,10 +22,13 @@ class WP_SQLite_PDO_User_Defined_Functions { public static function register_for( $pdo ): self { $instance = new self(); foreach ( $instance->functions as $f => $t ) { - if ( $pdo instanceof Pdo\Sqlite ) { - $pdo->createFunction( $f, array( $instance, $t ) ); - } else { - $pdo->sqliteCreateFunction( $f, array( $instance, $t ) ); + $arities = $instance->function_arities[ $f ] ?? array( -1 ); + foreach ( $arities as $arity ) { + if ( $pdo instanceof Pdo\Sqlite ) { + $pdo->createFunction( $f, array( $instance, $t ), $arity ); + } else { + $pdo->sqliteCreateFunction( $f, array( $instance, $t ), $arity ); + } } } return $instance; @@ -87,6 +90,20 @@ public static function register_for( $pdo ): self { '_helper_like_to_glob_pattern' => '_helper_like_to_glob_pattern', ); + /** + * Exact argument counts for functions with optional arguments. + * + * Functions absent from this array are registered as variadic. + * + * @var array + */ + private $function_arities = array( + 'regexp_like' => array( 2, 3 ), + ); + + /** @var string[] Compiled PCRE patterns, keyed by byte mode, match type, and pattern. */ + private $regexp_cache = array(); + /** * First element of the RAND(N) LCG state (the value the output is derived from). * @@ -568,15 +585,20 @@ public function isnull( $field ) { /** * Method to emulate MySQL REGEXP operator. * - * SQLite runs "X REGEXP Y" as "regexp(Y, X)". MySQL runs the operator - * as REGEXP_LIKE(), so this method uses regexp_like(). + * SQLite runs "X REGEXP Y" as "regexp(Y, X)". * * @param string|null $pattern Regular expression to match. * @param string|null $field Haystack. * + * @throws Exception If the pattern is not a valid regular expression. * @return int|null 1 if matched, 0 if not matched, NULL if an argument is NULL. */ public function regexp( $pattern, $field ) { + if ( null === $pattern || null === $field ) { + return null; + } + $pattern = (string) $pattern; + /* * If the original query says REGEXP BINARY * the comparison is byte-by-byte and letter casing now @@ -591,62 +613,50 @@ public function regexp( $pattern, $field ) { * be reasonably safe since PHP does not allow null bytes in * regular expressions anyway. */ - if ( is_string( $pattern ) && 0 === strpos( $pattern, "\x00" ) ) { - return $this->regexp_like( $field, substr( $pattern, 1 ), 'c' ); + $is_binary = '' !== $pattern && "\x00" === $pattern[0]; + if ( $is_binary ) { + $pattern = substr( $pattern, 1 ); } - return $this->regexp_like( $field, $pattern ); + + /* + * Data that is not valid UTF-8 can only be a binary string in MySQL, + * and MySQL matches binary strings byte by byte and case-sensitively. + */ + $byte_mode = $is_binary || ! $this->regexp_is_utf8( $pattern ) || ! $this->regexp_is_utf8( $field ); + $compiled = $this->regexp_compile( $pattern, $byte_mode ? 'c' : '', $byte_mode ); + $result = preg_match( $compiled, $field ); + if ( false === $result ) { + $this->regexp_fail( $pattern ); + } + return $result; } /** * Method to emulate MySQL REGEXP_LIKE() function. * - * @param string|null $subject Haystack. - * @param string|null $pattern Regular expression to match. + * @param string|null $expr The subject string. + * @param string|null $pattern The regex pattern. * @param string|null $match_type Optional. Letters that change how the pattern matches. * Without "c" or "i", the match is case-insensitive. * - * @throws Exception When the match type has an unknown letter. - * @return int|null 1 if matched, 0 if not matched, NULL if an argument is NULL. + * @throws Exception If the match type has an unknown letter, or the pattern is invalid. + * @return int|null 1 on match, 0 on no match, NULL if any argument is NULL. */ - public function regexp_like( $subject, $pattern, $match_type = '' ) { - if ( null === $match_type ) { + public function regexp_like( $expr, $pattern, $match_type = '' ) { + $this->regexp_check_match_type( 'regexp_like', $match_type ); + if ( null === $expr || null === $pattern || null === $match_type ) { return null; } - /* - * Apply the letters in order, so that the last of "c" and "i" wins, like in MySQL. - * The "u" letter needs no flag, as PCRE treats only "\n" as a line break by default. - */ - $case_insensitive = true; - $flags = ''; - $length = strlen( $match_type ); - for ( $i = 0; $i < $length; $i++ ) { - $letter = $match_type[ $i ]; - if ( 'c' === $letter ) { - $case_insensitive = false; - } elseif ( 'i' === $letter ) { - $case_insensitive = true; - } elseif ( 'm' === $letter ) { - $flags .= 'm'; - } elseif ( 'n' === $letter ) { - $flags .= 's'; - } elseif ( 'u' !== $letter ) { - throw new Exception( 'Incorrect arguments to regexp_like' ); - } - } - if ( $case_insensitive ) { - $flags .= 'i'; - } - - // Like MySQL, reject an invalid match type even for a NULL subject or pattern. - if ( null === $subject || null === $pattern ) { - return null; + // Like the REGEXP operator, match data that is not valid UTF-8 byte by byte, + // and case-sensitively, unless the match type says otherwise. + $byte_mode = ! $this->regexp_is_utf8( $pattern ) || ! $this->regexp_is_utf8( $expr ); + $compiled = $this->regexp_compile( $pattern, $byte_mode ? 'c' . $match_type : $match_type, $byte_mode ); + $result = preg_match( $compiled, $expr ); + if ( false === $result ) { + $this->regexp_fail( $pattern ); } - - $pattern = str_replace( '/', '\/', $pattern ); - $pattern = '/' . $pattern . '/' . $flags; - - return preg_match( $pattern, $subject ); + return $result; } /** @@ -1030,4 +1040,209 @@ public function _helper_like_to_glob_pattern( $pattern ) { return $pattern; } + + /** + * Check that a match type has only known letters. + * + * Like MySQL, the functions check it before they return NULL for a NULL argument. + * + * @param string $function_name The function name, for the error message. + * @param string|null $match_type MySQL match_type flag string. + * + * @throws Exception If the match type has an unknown letter. + */ + private function regexp_check_match_type( $function_name, $match_type ) { + $match_type = (string) $match_type; + if ( strspn( $match_type, 'cimnu' ) < strlen( $match_type ) ) { + throw new Exception( "Incorrect arguments to $function_name" ); + } + } + + /** + * Compile a MySQL-style regex into a PCRE pattern string. + * + * Translates MySQL match_type flags (c/i/m/n/u) to PCRE modifiers. The u + * (UTF-8) modifier is applied unless byte-oriented matching is requested. + * Case-insensitive is the default. + * + * MySQL's native engine is ICU; we use PHP's PCRE. The two diverge in a + * few corners: + * + * - Some Unicode property shorthands and POSIX class spellings differ. + * - PCRE accepts both `(?...)` and `(?P...)`; MySQL accepts + * only the former and errors on the latter. + * - ICU supports multi-code-point case folds such as "ß" matching "ss"; + * PCRE's case-insensitive mode does not. + * + * Known limitations of this emulation: + * + * - The default (case-insensitive) is correct for the usual + * `utf8mb4_0900_ai_ci` collation; callers that rely on a `_bin` or + * `_cs` collation must pass an explicit `c` match_type because this + * helper has no access to the session collation. + * - Data that is not valid UTF-8, and REGEXP BINARY, are matched byte by + * byte and case-sensitively, like MySQL binary strings. + * + * @param string $pattern The MySQL regex pattern. + * @param string $match_type MySQL match_type flag string. + * @param bool $binary_mode Whether to match bytes instead of UTF-8 characters. + * + * @throws Exception If the pattern is empty or invalid. + * @return string PCRE-ready pattern with delimiter and modifiers. + */ + private function regexp_compile( $pattern, $match_type, $binary_mode = false ) { + $match_type = (string) $match_type; + $pattern = (string) $pattern; + if ( '' === $pattern ) { + throw new Exception( 'Illegal argument to a regular expression.' ); + } + $cache_key = ( $binary_mode ? 'bytes:' : 'text:' ) . $match_type . ':' . $pattern; + if ( isset( $this->regexp_cache[ $cache_key ] ) ) { + return $this->regexp_cache[ $cache_key ]; + } + + $case_sensitive = false; + $multiline = false; + $dotall = false; + $unix_lines = false; + $len = strlen( $match_type ); + for ( $i = 0; $i < $len; $i++ ) { + $flag = $match_type[ $i ]; + if ( 'c' === $flag ) { + $case_sensitive = true; + } elseif ( 'i' === $flag ) { + $case_sensitive = false; + } elseif ( 'm' === $flag ) { + $multiline = true; + } elseif ( 'n' === $flag ) { + $dotall = true; + } elseif ( 'u' === $flag ) { + $unix_lines = true; + } + } + + $modifiers = $binary_mode ? '' : 'u'; + if ( ! $case_sensitive ) { + $modifiers .= 'i'; + } + if ( $multiline ) { + $modifiers .= 'm'; + } + if ( $dotall ) { + $modifiers .= 's'; + } + + /* + * In binary data, MySQL ends lines at CR, LF, VT and FF. No PCRE setting + * matches that. (*ANY) also ends lines at the byte 0x85, which is a part of + * characters like "ą" (0xC4 0x85). (*ANYCRLF) misses only the rare VT and FF. + */ + if ( $unix_lines ) { + $newline = '(*LF)'; + } elseif ( $binary_mode ) { + $newline = '(*ANYCRLF)'; + } else { + $newline = '(*ANY)'; + } + $compiled = '/' . $newline . $this->regexp_escape_delimiter( $pattern ) . '/' . $modifiers; + + /* + * Compile the pattern by matching it against an empty string. Only a compile + * error emits a PHP warning, so silence it and report a MySQL error instead. + * A limit error concerns only the empty string, so the pattern is valid. + */ + set_error_handler( static function () {} ); + try { + $valid = false !== preg_match( $compiled, '' ) || PREG_INTERNAL_ERROR !== preg_last_error(); + } finally { + restore_error_handler(); + } + if ( ! $valid ) { + $this->regexp_fail( $pattern ); + } + // A query can use many patterns, such as one from a column, so drop the oldest one. + if ( count( $this->regexp_cache ) >= 64 ) { + array_shift( $this->regexp_cache ); + } + $this->regexp_cache[ $cache_key ] = $compiled; + return $compiled; + } + + /** + * Escape pattern delimiters that are not already escaped. + * + * @param string $pattern The regex pattern. + * + * @return string Pattern safe to wrap in slash delimiters. + */ + private function regexp_escape_delimiter( $pattern ) { + $escaped = ''; + $quoted = false; + $length = strlen( $pattern ); + for ( $i = 0; $i < $length; ++$i ) { + $character = $pattern[ $i ]; + if ( $quoted ) { + if ( '\\' === $character && $i + 1 < $length && 'E' === $pattern[ $i + 1 ] ) { + $escaped .= '\\E'; + $quoted = false; + ++$i; + } elseif ( '/' === $character ) { + $escaped .= '\\E\\/\\Q'; + } else { + $escaped .= $character; + } + continue; + } + if ( '\\' === $character && $i + 1 < $length ) { + $escaped .= $character . $pattern[ $i + 1 ]; + $quoted = 'Q' === $pattern[ $i + 1 ]; + ++$i; + } elseif ( '/' === $character ) { + $escaped .= '\\/'; + } else { + $escaped .= $character; + } + } + + // Close quoted text that runs to the end, so that a trailing backslash + // can't escape the delimiter. + if ( $quoted ) { + $escaped .= '\\E'; + } + return $escaped; + } + + /** + * Check whether a string is valid UTF-8. + * + * @param string $value The string. + * + * @return bool Whether the string is valid UTF-8. + */ + private function regexp_is_utf8( $value ) { + return 1 === preg_match( '//u', $value ); + } + + /** + * Translate a preg_* failure into a caller-friendly exception message. + * + * Uses preg_last_error() to distinguish invalid patterns from runtime + * limit failures. + * + * @param string $pattern The original MySQL regex pattern. + * + * @throws Exception Always. + * @return void + */ + private function regexp_fail( $pattern ) { + $err = preg_last_error(); + if ( + PREG_BACKTRACK_LIMIT_ERROR === $err + || PREG_RECURSION_LIMIT_ERROR === $err + || PREG_JIT_STACKLIMIT_ERROR === $err + ) { + throw new Exception( 'Regular expression evaluation exceeded internal limits.' ); + } + throw new Exception( 'Invalid regular expression: ' . $pattern . '.' ); + } } diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php index 15e90b261..1efa36494 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php @@ -276,24 +276,25 @@ public function testRegexpLike( $query, $expected_result ) { public static function regexpLikeQueries() { return array( - 'no match type' => array( "SELECT REGEXP_LIKE('ABC', 'b') AS result", '1' ), - 'empty match type' => array( "SELECT REGEXP_LIKE('ABC', 'b', '') AS result", '1' ), - 'case-sensitive' => array( "SELECT REGEXP_LIKE('ABC', 'b', 'c') AS result", '0' ), - 'case-insensitive' => array( "SELECT REGEXP_LIKE('ABC', 'b', 'i') AS result", '1' ), - 'last "i" wins' => array( "SELECT REGEXP_LIKE('ABC', 'b', 'ci') AS result", '1' ), - 'last "c" wins' => array( "SELECT REGEXP_LIKE('ABC', 'b', 'ic') AS result", '0' ), - 'single line' => array( "SELECT REGEXP_LIKE('a\\nb', '^b') AS result", '0' ), - 'multiline' => array( "SELECT REGEXP_LIKE('a\\nb', '^b', 'm') AS result", '1' ), - 'dot without line break' => array( "SELECT REGEXP_LIKE('a\\nb', 'a.b') AS result", '0' ), - 'dot with line break' => array( "SELECT REGEXP_LIKE('a\\nb', 'a.b', 'n') AS result", '1' ), - 'Unix line breaks' => array( "SELECT REGEXP_LIKE('a\\nb', '^b', 'mu') AS result", '1' ), - 'repeated letters' => array( "SELECT REGEXP_LIKE('a\\nb', '^b', 'mm') AS result", '1' ), - 'NULL subject' => array( "SELECT REGEXP_LIKE(NULL, 'b') AS result", null ), - 'NULL pattern' => array( "SELECT REGEXP_LIKE('ABC', NULL) AS result", null ), - 'NULL match type' => array( "SELECT REGEXP_LIKE('ABC', 'b', NULL) AS result", null ), - 'NULL REGEXP subject' => array( "SELECT NULL REGEXP 'b' AS result", null ), - 'NULL REGEXP pattern' => array( "SELECT 'ABC' REGEXP NULL AS result", null ), - 'NULL NOT REGEXP' => array( "SELECT NULL NOT REGEXP 'b' AS result", null ), + 'no match type' => array( "SELECT REGEXP_LIKE('ABC', 'b') AS result", '1' ), + 'empty match type' => array( "SELECT REGEXP_LIKE('ABC', 'b', '') AS result", '1' ), + 'case-sensitive' => array( "SELECT REGEXP_LIKE('ABC', 'b', 'c') AS result", '0' ), + 'case-insensitive' => array( "SELECT REGEXP_LIKE('ABC', 'b', 'i') AS result", '1' ), + 'last "i" wins' => array( "SELECT REGEXP_LIKE('ABC', 'b', 'ci') AS result", '1' ), + 'last "c" wins' => array( "SELECT REGEXP_LIKE('ABC', 'b', 'ic') AS result", '0' ), + 'single line' => array( "SELECT REGEXP_LIKE('a\\nb', '^b') AS result", '0' ), + 'multiline' => array( "SELECT REGEXP_LIKE('a\\nb', '^b', 'm') AS result", '1' ), + 'dot without line break' => array( "SELECT REGEXP_LIKE('a\\nb', 'a.b') AS result", '0' ), + 'dot with line break' => array( "SELECT REGEXP_LIKE('a\\nb', 'a.b', 'n') AS result", '1' ), + 'Unix line breaks' => array( "SELECT REGEXP_LIKE('a\\nb', '^b', 'mu') AS result", '1' ), + 'repeated letters' => array( "SELECT REGEXP_LIKE('a\\nb', '^b', 'mm') AS result", '1' ), + 'NULL subject' => array( "SELECT REGEXP_LIKE(NULL, 'b') AS result", null ), + 'NULL pattern' => array( "SELECT REGEXP_LIKE('ABC', NULL) AS result", null ), + 'NULL match type' => array( "SELECT REGEXP_LIKE('ABC', 'b', NULL) AS result", null ), + 'NULL match type, invalid pattern' => array( "SELECT REGEXP_LIKE('ABC', '(b', NULL) AS result", null ), + 'NULL REGEXP subject' => array( "SELECT NULL REGEXP 'b' AS result", null ), + 'NULL REGEXP pattern' => array( "SELECT 'ABC' REGEXP NULL AS result", null ), + 'NULL NOT REGEXP' => array( "SELECT NULL NOT REGEXP 'b' AS result", null ), ); } @@ -309,9 +310,292 @@ public static function regexpLikeInvalidMatchTypeQueries() { 'subject and pattern' => array( "SELECT REGEXP_LIKE('ABC', 'b', 'x')" ), 'NULL subject' => array( "SELECT REGEXP_LIKE(NULL, 'b', 'x')" ), 'NULL pattern' => array( "SELECT REGEXP_LIKE('ABC', NULL, 'x')" ), + 'number' => array( "SELECT REGEXP_LIKE('ABC', 'b', 1)" ), ); } + /** + * @dataProvider regexpLikeErrors + */ + public function testRegexpLikeErrors( $sql, $message ) { + $this->assertQueryError( "SELECT $sql", $message ); + } + + public static function regexpLikeErrors() { + return array( + 'invalid pattern' => array( "REGEXP_LIKE('abc', '(abc')", 'Invalid regular expression: (abc.' ), + 'extra argument' => array( + "REGEXP_LIKE('abc', 'abc', 'c', 'extra')", + 'SQLSTATE[HY000]: General error: 1 wrong number of arguments to function REGEXP_LIKE()', + ), + ); + } + + /** + * @dataProvider regexpMatchTypeFlagCases + */ + public function testRegexpMatchTypeMultipleFlags( $match_type, $expected ) { + // "^a.b$" matches the last two lines only when case is ignored, "^" matches after + // a line break ("m"), and "." matches a line break ("n"). + $this->assertQuery( "SELECT REGEXP_LIKE('X\\nA\\nB', '^a.b\$', '$match_type') AS r" ); + $this->assertSame( $expected, $this->last_result[0]->r ); + } + + public static function regexpMatchTypeFlagCases() { + return array( + '"i" after "c"' => array( 'cimn', '1' ), + '"c" after "i"' => array( 'imnc', '0' ), + 'no "m"' => array( 'cin', '0' ), + 'no "n"' => array( 'cim', '0' ), + ); + } + + /** + * @dataProvider regexpLikeBinaryDataCases + */ + public function testRegexpLikeMatchesBinaryDataByBytes( $value, $sql, $expected ) { + // Like the REGEXP operator, REGEXP_LIKE() matches data that is not valid UTF-8 + // byte by byte, and case-sensitively, unless the match type says otherwise. + $this->assertQuery( 'CREATE TABLE blobs (b BLOB)' ); + $this->assertQuery( "INSERT INTO blobs VALUES ($value)" ); + $this->assertQuery( "SELECT $sql AS r FROM blobs" ); + $this->assertSame( $expected, $this->last_result[0]->r ); + } + + public static function regexpLikeBinaryDataCases() { + return array( + 'any byte' => array( "X'61FF62'", "REGEXP_LIKE(b, 'a.b')", '1' ), + 'other case' => array( "X'61FF62'", "REGEXP_LIKE(b, 'A.B')", '0' ), + 'ignore case' => array( "X'61FF62'", "REGEXP_LIKE(b, 'A.B', 'i')", '1' ), + 'match case' => array( "X'61FF62'", "REGEXP_LIKE(b, 'a.b', 'c')", '1' ), + '0x85 in a character' => array( "X'78C485FF79'", "REGEXP_LIKE(b, 'x.*y')", '1' ), + 'CR' => array( "X'610D62FF'", "REGEXP_LIKE(b, 'a.b')", '0' ), + 'CR, Unix line break' => array( "X'610D62FF'", "REGEXP_LIKE(b, 'a.b', 'u')", '1' ), + ); + } + + public function testRegexpBacktrackLimit() { + // Classic exponential-backtracking pattern that exceeds PCRE's default + // backtrack limit; exercises the PREG_BACKTRACK_LIMIT_ERROR branch of + // regexp_fail(). + $subject = str_repeat( 'a', 30 ); + $this->assertQueryError( + "SELECT REGEXP_LIKE('$subject', '^(a?){30}a{30}\$')", + 'Regular expression evaluation exceeded internal limits.' + ); + } + + public function testRegexpLimitOnlyOnEmptySubject() { + // Only an empty subject exceeds the backtrack limit, so other subjects still match. + $pattern = 'x|(?:a?|b?){20}(?!$)'; + $this->assertQuery( + "SELECT 'x' REGEXP '$pattern' AS operator_result, REGEXP_LIKE('x', '$pattern') AS like_result" + ); + $this->assertEquals( + array( + (object) array( + 'operator_result' => '1', + 'like_result' => '1', + ), + ), + $this->last_result + ); + $this->assertQueryError( + "SELECT REGEXP_LIKE('', '$pattern')", + 'Regular expression evaluation exceeded internal limits.' + ); + } + + public function testRegexpLegacyOperatorRegression() { + // The legacy REGEXP operator must keep working alongside REGEXP_LIKE. + $this->assertQuery( "SELECT 'abc' REGEXP 'ABC' AS r" ); + $this->assertSame( '1', $this->last_result[0]->r ); + $this->assertQuery( "SELECT 'abc' REGEXP 'xyz' AS r" ); + $this->assertSame( '0', $this->last_result[0]->r ); + } + + public function testRegexpLegacyOperatorNullPropagation() { + $this->assertQuery( "SELECT NULL REGEXP 'a' AS r" ); + $this->assertNull( $this->last_result[0]->r ); + $this->assertQuery( "SELECT 'a' RLIKE NULL AS r" ); + $this->assertNull( $this->last_result[0]->r ); + } + + public function testRegexpLegacyOperatorInvalidPattern() { + $this->assertQueryError( + "SELECT 'abc' REGEXP '(abc'", + 'Invalid regular expression: (abc.' + ); + $this->assertQueryError( + "SELECT 'abc' RLIKE ''", + 'Illegal argument to a regular expression.' + ); + } + + public function testRegexpLegacyOperatorEscapedDelimiter() { + $this->assertQuery( "SELECT '/' REGEXP '\\\\/' AS r" ); + $this->assertSame( '1', $this->last_result[0]->r ); + $this->assertQuery( "SELECT '/' REGEXP '\\\\Q/\\\\E' AS r" ); + $this->assertSame( '1', $this->last_result[0]->r ); + } + + public function testRegexpLegacyOperatorNumericOperands() { + $this->assertQuery( "SELECT 123 REGEXP '^12' AS r" ); + $this->assertSame( '1', $this->last_result[0]->r ); + $this->assertQuery( "SELECT 'x123' NOT REGEXP 123 AS r" ); + $this->assertSame( '0', $this->last_result[0]->r ); + } + + public function testRegexpLegacyOperatorUtf8Handling() { + $this->assertQuery( "SELECT 'é' REGEXP '^.\$' AS r" ); + $this->assertSame( '1', $this->last_result[0]->r ); + $this->assertQuery( "SELECT CAST(X'FF' AS CHAR) REGEXP BINARY CAST(X'FF' AS CHAR) AS r" ); + $this->assertSame( '1', $this->last_result[0]->r ); + } + + public function testRegexpLegacyOperatorMatchesBinaryDataByBytes() { + // Non-UTF-8 data is matched like MySQL binary strings: byte by byte and case-sensitively. + $this->assertQuery( 'CREATE TABLE blobs (id INT PRIMARY KEY, b BLOB)' ); + $this->assertQuery( "INSERT INTO blobs VALUES (1, X'61FF62'), (2, NULL)" ); + $this->assertQuery( + "SELECT + b REGEXP 'a.b' AS any_byte, + b REGEXP '^a.b\$' AS anchored, + b REGEXP 'A' AS other_case, + b NOT REGEXP 'zzz' AS negated, + b REGEXP X'FF62' AS binary_pattern, + b REGEXP X'FE' AS other_byte + FROM blobs ORDER BY id" + ); + $this->assertEquals( + array( + (object) array( + 'any_byte' => '1', + 'anchored' => '1', + 'other_case' => '0', + 'negated' => '1', + 'binary_pattern' => '1', + 'other_byte' => '0', + ), + (object) array( + 'any_byte' => null, + 'anchored' => null, + 'other_case' => null, + 'negated' => null, + 'binary_pattern' => null, + 'other_byte' => null, + ), + ), + $this->last_result + ); + } + + /** + * @dataProvider regexpBinaryLineBreakCases + */ + public function testRegexpBinaryLineBreaks( $sql, $expected ) { + $this->assertQuery( "SELECT $sql AS r" ); + $this->assertSame( $expected, $this->last_result[0]->r ); + } + + public static function regexpBinaryLineBreakCases() { + // MySQL reads CR and LF as line breaks in binary data, but not the byte 0x85, + // which is a part of characters like "ą" (0xC4 0x85). + return array( + '0x85 in a character' => array( "CAST('xąy' AS BINARY) REGEXP BINARY 'x.*y'", '1' ), + '0x85 alone' => array( "X'788579' REGEXP BINARY 'x.y'", '1' ), + 'CR' => array( "X'780D79' REGEXP BINARY 'x.y'", '0' ), + 'LF' => array( "X'780A79' REGEXP BINARY 'x.y'", '0' ), + ); + } + + public function testRegexpLineBreaksInInvalidUtf8() { + // Data that is not valid UTF-8 is matched as binary data. + $this->assertQuery( 'CREATE TABLE blobs (id INT PRIMARY KEY, b BLOB)' ); + $this->assertQuery( "INSERT INTO blobs VALUES (1, X'78C485FF79')" ); + $this->assertQuery( "SELECT b REGEXP 'x.*y' AS r FROM blobs" ); + $this->assertSame( '1', $this->last_result[0]->r ); + } + + public function testRegexpCachedPatterns() { + // A pattern compiles differently for text and binary data, and for each match + // type. The second row reuses the compiled patterns of the first one. + $this->assertQuery( 'CREATE TABLE blobs (id INT PRIMARY KEY, b BLOB)' ); + $this->assertQuery( "INSERT INTO blobs VALUES (1, X'61FF62'), (2, X'61FF62')" ); + $this->assertQuery( + "SELECT + 'aXb' REGEXP 'A.B' AS text_result, + b REGEXP 'A.B' AS bytes_result, + REGEXP_LIKE('aXb', 'A.B', 'c') AS match_case, + REGEXP_LIKE('aXb', 'A.B', 'i') AS ignore_case + FROM blobs ORDER BY id" + ); + $row = (object) array( + 'text_result' => '1', + 'bytes_result' => '0', + 'match_case' => '0', + 'ignore_case' => '1', + ); + $this->assertEquals( array( $row, $row ), $this->last_result ); + } + + public function testRegexpMorePatternsThanCached() { + $this->assertQuery( 'CREATE TABLE patterns (id INT PRIMARY KEY, s TEXT, p TEXT)' ); + $values = array(); + for ( $i = 1; $i <= 200; $i++ ) { + $values[] = "($i, 'value $i', '^value $i\$')"; + } + $this->assertQuery( 'INSERT INTO patterns VALUES ' . implode( ', ', $values ) ); + $this->assertQuery( 'SELECT COUNT(*) AS matched FROM patterns WHERE s REGEXP p' ); + $this->assertSame( '200', $this->last_result[0]->matched ); + $this->assertQuery( 'SELECT COUNT(*) AS matched FROM patterns a JOIN patterns b ON a.s REGEXP b.p' ); + $this->assertSame( '200', $this->last_result[0]->matched ); + } + + public function testRegexpLikeEscapedDelimiter() { + $this->assertQuery( "SELECT REGEXP_LIKE('/', '\\\\/') AS r" ); + $this->assertSame( '1', $this->last_result[0]->r ); + $this->assertQuery( "SELECT REGEXP_LIKE('/', '\\\\Q/\\\\E', 'c') AS r" ); + $this->assertSame( '1', $this->last_result[0]->r ); + } + + /** + * @dataProvider regexpUnterminatedQuoteCases + */ + public function testRegexpUnterminatedQuote( $sql, $expected ) { + $this->assertQuery( "SELECT $sql AS r" ); + $this->assertSame( $expected, $this->last_result[0]->r ); + } + + public static function regexpUnterminatedQuoteCases() { + // Quoted text without "\E" runs to the end of the pattern, so a trailing backslash is literal. + return array( + 'trailing backslash' => array( "REGEXP_LIKE('a\\\\', '\\\\Qa\\\\')", '1' ), + 'trailing backslash, no match' => array( "REGEXP_LIKE('a', '\\\\Qa\\\\')", '0' ), + 'trailing backslash, operator' => array( "'a\\\\' REGEXP '\\\\Qa\\\\'", '1' ), + 'two trailing backslashes' => array( "REGEXP_LIKE('a\\\\\\\\', '^\\\\Qa\\\\\\\\')", '1' ), + 'literal dot' => array( "REGEXP_LIKE('a.b', '^\\\\Qa.b')", '1' ), + 'literal dot, no match' => array( "REGEXP_LIKE('axb', '^\\\\Qa.b')", '0' ), + 'empty' => array( "REGEXP_LIKE('', '\\\\Q')", '1' ), + ); + } + + public function testRegexpLikeNumericOperands() { + $this->assertQuery( 'SELECT REGEXP_LIKE(123, 2) AS r' ); + $this->assertSame( '1', $this->last_result[0]->r ); + } + + public function testRegexpUnicodeNewlineHandling() { + $this->assertQuery( "SELECT REGEXP_LIKE(CAST(X'610D62' AS CHAR), 'a.b', 'c') AS r" ); + $this->assertSame( '0', $this->last_result[0]->r ); + $this->assertQuery( "SELECT REGEXP_LIKE(CAST(X'610D62' AS CHAR), '^b', 'cm') AS r" ); + $this->assertSame( '1', $this->last_result[0]->r ); + $this->assertQuery( "SELECT REGEXP_LIKE(CAST(X'610D62' AS CHAR), 'a.b', 'cu') AS r" ); + $this->assertSame( '1', $this->last_result[0]->r ); + $this->assertQuery( "SELECT REGEXP_LIKE(CAST(X'61E280A862' AS CHAR), '^b', 'cm') AS r" ); + $this->assertSame( '1', $this->last_result[0]->r ); + } + public function testInsertDateNow() { $this->assertQuery( "INSERT INTO _dates (option_name, option_value) VALUES ('first', now());" diff --git a/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Like_Tests.php b/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Like_Tests.php index 302d9b9b7..b1fb7fbf6 100644 --- a/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Like_Tests.php +++ b/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Like_Tests.php @@ -17,30 +17,6 @@ protected function get_known_failures(): array { // Cases that fail in every environment. $failures = array( - 'mysql-server-suite:1527', - 'mysql-server-suite:1528', - 'mysql-server-suite:1529', - 'mysql-server-suite:1530', - 'mysql-server-suite:1531', - 'mysql-server-suite:1532', - 'mysql-server-suite:1533', - 'mysql-server-suite:1534', - 'mysql-server-suite:1695', - 'mysql-server-suite:1696', - 'mysql-server-suite:1697', - 'mysql-server-suite:1698', - 'mysql-server-suite:1699', - 'mysql-server-suite:1700', - 'mysql-server-suite:1701', - 'mysql-server-suite:1702', - 'mysql-server-suite:7921', - 'mysql-server-suite:7922', - 'mysql-server-suite:7923', - 'mysql-server-suite:7924', - 'mysql-server-suite:7925', - 'mysql-server-suite:7926', - 'mysql-server-suite:7929', - 'mysql-server-suite:7930', 'mysql-server-suite:9591', 'mysql-server-suite:9592', 'mysql-server-suite:9593', @@ -75,22 +51,16 @@ protected function get_known_failures(): array { 'mysql-server-suite:9654', 'mysql-server-suite:9655', 'mysql-server-suite:9656', + 'mysql-server-suite:9657', + 'mysql-server-suite:9658', + 'mysql-server-suite:9659', + 'mysql-server-suite:9660', + 'mysql-server-suite:9661', + 'mysql-server-suite:9662', 'mysql-server-suite:9665', 'mysql-server-suite:9666', 'mysql-server-suite:9667', 'mysql-server-suite:9668', - 'mysql-server-suite:10749', - 'mysql-server-suite:10750', - 'mysql-server-suite:12251', - 'mysql-server-suite:12252', - 'mysql-server-suite:12253', - 'mysql-server-suite:12254', - 'mysql-server-suite:12257', - 'mysql-server-suite:12258', - 'mysql-server-suite:12261', - 'mysql-server-suite:12262', - 'mysql-server-suite:12265', - 'mysql-server-suite:12266', 'mysql-server-suite:12325', 'mysql-server-suite:12326', 'mysql-server-suite:12333', @@ -105,8 +75,6 @@ protected function get_known_failures(): array { 'mysql-server-suite:12350', 'mysql-server-suite:12373', 'mysql-server-suite:12374', - 'mysql-server-suite:12449', - 'mysql-server-suite:12450', 'mysql-server-suite:12455', 'mysql-server-suite:12456', 'mysql-server-suite:12457', @@ -165,34 +133,6 @@ protected function get_known_failures(): array { 'mysql-server-suite:12610', 'mysql-server-suite:12611', 'mysql-server-suite:12612', - 'icu-regextst:53', - 'icu-regextst:54', - 'icu-regextst:58', - 'icu-regextst:60', - 'icu-regextst:90', - 'icu-regextst:92', - 'icu-regextst:94', - 'icu-regextst:96', - 'icu-regextst:98', - 'icu-regextst:100', - 'icu-regextst:102', - 'icu-regextst:105', - 'icu-regextst:106', - 'icu-regextst:107', - 'icu-regextst:108', - 'icu-regextst:109', - 'icu-regextst:110', - 'icu-regextst:111', - 'icu-regextst:112', - 'icu-regextst:113', - 'icu-regextst:114', - 'icu-regextst:115', - 'icu-regextst:116', - 'icu-regextst:117', - 'icu-regextst:118', - 'icu-regextst:125', - 'icu-regextst:127', - 'icu-regextst:129', 'icu-regextst:133', 'icu-regextst:134', 'icu-regextst:135', @@ -201,7 +141,6 @@ protected function get_known_failures(): array { 'icu-regextst:147', 'icu-regextst:148', 'icu-regextst:149', - 'icu-regextst:151', 'icu-regextst:165', 'icu-regextst:166', 'icu-regextst:209', @@ -220,10 +159,6 @@ protected function get_known_failures(): array { 'icu-regextst:232', 'icu-regextst:233', 'icu-regextst:234', - 'icu-regextst:285', - 'icu-regextst:286', - 'icu-regextst:297', - 'icu-regextst:298', 'icu-regextst:329', 'icu-regextst:330', 'icu-regextst:375', @@ -280,12 +215,6 @@ protected function get_known_failures(): array { 'icu-regextst:630', 'icu-regextst:632', 'icu-regextst:633', - 'icu-regextst:697', - 'icu-regextst:698', - 'icu-regextst:699', - 'icu-regextst:700', - 'icu-regextst:701', - 'icu-regextst:702', 'icu-regextst:705', 'icu-regextst:706', 'icu-regextst:709', @@ -298,28 +227,6 @@ protected function get_known_failures(): array { 'icu-regextst:716', 'icu-regextst:717', 'icu-regextst:718', - 'icu-regextst:737', - 'icu-regextst:738', - 'icu-regextst:739', - 'icu-regextst:740', - 'icu-regextst:741', - 'icu-regextst:742', - 'icu-regextst:763', - 'icu-regextst:764', - 'icu-regextst:765', - 'icu-regextst:766', - 'icu-regextst:767', - 'icu-regextst:768', - 'icu-regextst:781', - 'icu-regextst:782', - 'icu-regextst:783', - 'icu-regextst:784', - 'icu-regextst:785', - 'icu-regextst:786', - 'icu-regextst:813', - 'icu-regextst:814', - 'icu-regextst:829', - 'icu-regextst:830', 'icu-regextst:843', 'icu-regextst:844', 'icu-regextst:931', @@ -453,12 +360,6 @@ protected function get_known_failures(): array { 'icu-regextst:1136', 'icu-regextst:1141', 'icu-regextst:1142', - 'icu-regextst:1149', - 'icu-regextst:1150', - 'icu-regextst:1151', - 'icu-regextst:1152', - 'icu-regextst:1170', - 'icu-regextst:1171', 'icu-regextst:1179', 'icu-regextst:1180', 'icu-regextst:1181', @@ -479,10 +380,6 @@ protected function get_known_failures(): array { 'icu-regextst:1240', 'icu-regextst:1241', 'icu-regextst:1242', - 'icu-regextst:1243', - 'icu-regextst:1244', - 'icu-regextst:1245', - 'icu-regextst:1246', 'icu-regextst:1247', 'icu-regextst:1248', 'icu-regextst:1249', @@ -511,14 +408,6 @@ protected function get_known_failures(): array { 'icu-regextst:1337', 'icu-regextst:1338', 'icu-regextst:1339', - 'icu-regextst:1340', - 'icu-regextst:1341', - 'icu-regextst:1346', - 'icu-regextst:1347', - 'icu-regextst:1352', - 'icu-regextst:1353', - 'icu-regextst:1358', - 'icu-regextst:1359', 'icu-regextst:1370', 'icu-regextst:1371', 'icu-regextst:1372', @@ -601,8 +490,6 @@ protected function get_known_failures(): array { 'icu-regextst:1449', 'icu-regextst:1450', 'icu-regextst:1451', - 'icu-regextst:1452', - 'icu-regextst:1453', 'icu-regextst:1454', 'icu-regextst:1455', 'icu-regextst:1456', @@ -625,366 +512,18 @@ protected function get_known_failures(): array { 'icu-regextst:1507', 'icu-regextst:1508', 'icu-regextst:1509', - 'icu-regextst:1536', - 'icu-regextst:1537', - 'icu-regextst:1538', - 'icu-regextst:1539', - 'icu-regextst:1540', - 'icu-regextst:1541', - 'icu-regextst:1542', - 'icu-regextst:1543', - 'icu-regextst:1544', - 'icu-regextst:1545', - 'icu-regextst:1546', - 'icu-regextst:1547', - 'icu-regextst:1548', - 'icu-regextst:1549', - 'icu-regextst:1550', - 'icu-regextst:1551', - 'icu-regextst:1552', - 'icu-regextst:1553', - 'icu-regextst:1554', - 'icu-regextst:1555', - 'icu-regextst:1556', - 'icu-regextst:1557', - 'icu-regextst:1558', - 'icu-regextst:1559', - 'icu-regextst:1584', - 'icu-regextst:1585', - 'icu-regextst:1586', - 'icu-regextst:1587', - 'icu-regextst:1588', - 'icu-regextst:1589', - 'icu-regextst:1590', - 'icu-regextst:1591', - 'icu-regextst:1592', - 'icu-regextst:1593', - 'icu-regextst:1594', - 'icu-regextst:1595', - 'icu-regextst:1680', - 'icu-regextst:1681', - 'icu-regextst:1682', - 'icu-regextst:1683', - 'icu-regextst:1684', - 'icu-regextst:1685', - 'icu-regextst:1686', - 'icu-regextst:1687', - 'icu-regextst:1688', - 'icu-regextst:1689', - 'icu-regextst:1690', - 'icu-regextst:1691', - 'icu-regextst:1692', - 'icu-regextst:1693', - 'icu-regextst:1694', - 'icu-regextst:1695', - 'icu-regextst:1696', - 'icu-regextst:1697', - 'icu-regextst:1758', - 'icu-regextst:1759', - 'icu-regextst:1760', - 'icu-regextst:1761', - 'icu-regextst:1762', - 'icu-regextst:1763', - 'icu-regextst:1764', - 'icu-regextst:1765', - 'icu-regextst:1766', - 'icu-regextst:1767', - 'icu-regextst:1862', - 'icu-regextst:1863', - 'icu-regextst:1864', - 'icu-regextst:1865', - 'icu-regextst:1866', - 'icu-regextst:1867', - 'icu-regextst:1868', - 'icu-regextst:1869', - 'icu-regextst:1870', - 'icu-regextst:1871', - 'icu-regextst:1872', - 'icu-regextst:1873', - 'icu-regextst:1882', - 'icu-regextst:1883', - 'icu-regextst:1884', - 'icu-regextst:1885', - 'icu-regextst:1886', - 'icu-regextst:1887', - 'icu-regextst:1888', - 'icu-regextst:1889', - 'icu-regextst:1890', - 'icu-regextst:1891', - 'icu-regextst:1892', - 'icu-regextst:1893', - 'icu-regextst:2074', - 'icu-regextst:2075', - 'icu-regextst:2076', - 'icu-regextst:2077', - 'icu-regextst:2078', - 'icu-regextst:2079', - 'icu-regextst:2080', - 'icu-regextst:2081', - 'icu-regextst:2082', - 'icu-regextst:2083', - 'icu-regextst:2084', - 'icu-regextst:2085', - 'icu-regextst:2108', - 'icu-regextst:2109', - 'icu-regextst:2110', - 'icu-regextst:2111', - 'icu-regextst:2112', - 'icu-regextst:2113', - 'icu-regextst:2114', - 'icu-regextst:2115', - 'icu-regextst:2210', - 'icu-regextst:2211', - 'icu-regextst:2212', - 'icu-regextst:2213', - 'icu-regextst:2214', - 'icu-regextst:2215', - 'icu-regextst:2216', - 'icu-regextst:2217', - 'icu-regextst:2218', - 'icu-regextst:2219', - 'icu-regextst:2220', - 'icu-regextst:2221', - 'icu-regextst:2302', - 'icu-regextst:2303', - 'icu-regextst:2304', - 'icu-regextst:2305', - 'icu-regextst:2306', - 'icu-regextst:2307', - 'icu-regextst:2308', - 'icu-regextst:2309', - 'icu-regextst:2310', - 'icu-regextst:2311', - 'icu-regextst:2312', - 'icu-regextst:2313', - 'icu-regextst:2332', - 'icu-regextst:2333', - 'icu-regextst:2334', - 'icu-regextst:2335', - 'icu-regextst:2336', - 'icu-regextst:2337', - 'icu-regextst:2338', - 'icu-regextst:2339', - 'icu-regextst:2340', - 'icu-regextst:2341', - 'icu-regextst:2342', - 'icu-regextst:2343', - 'icu-regextst:2538', - 'icu-regextst:2539', - 'icu-regextst:2540', - 'icu-regextst:2541', - 'icu-regextst:2542', - 'icu-regextst:2543', - 'icu-regextst:2544', - 'icu-regextst:2545', - 'icu-regextst:2546', - 'icu-regextst:2547', - 'icu-regextst:2548', - 'icu-regextst:2549', 'icu-regextst:2768', 'icu-regextst:2769', 'icu-regextst:2770', 'icu-regextst:2771', 'icu-regextst:2772', 'icu-regextst:2773', - 'icu-regextst:2798', - 'icu-regextst:2799', - 'icu-regextst:2800', - 'icu-regextst:2801', - 'icu-regextst:2802', - 'icu-regextst:2803', - 'icu-regextst:2804', - 'icu-regextst:2805', - 'icu-regextst:2806', - 'icu-regextst:2807', - 'icu-regextst:2808', - 'icu-regextst:2809', - 'icu-regextst:2834', - 'icu-regextst:2835', - 'icu-regextst:2836', - 'icu-regextst:2837', - 'icu-regextst:2838', - 'icu-regextst:2839', - 'icu-regextst:2840', - 'icu-regextst:2841', - 'icu-regextst:2842', - 'icu-regextst:2843', - 'icu-regextst:2844', - 'icu-regextst:2845', - 'icu-regextst:2846', - 'icu-regextst:2847', - 'icu-regextst:2848', - 'icu-regextst:2849', - 'icu-regextst:2850', - 'icu-regextst:2851', - 'icu-regextst:2852', - 'icu-regextst:2853', - 'icu-regextst:2854', - 'icu-regextst:2855', - 'icu-regextst:2958', - 'icu-regextst:2959', - 'icu-regextst:2960', - 'icu-regextst:2961', - 'icu-regextst:2962', - 'icu-regextst:2963', - 'icu-regextst:2964', - 'icu-regextst:2965', - 'icu-regextst:2966', - 'icu-regextst:2967', - 'icu-regextst:2968', - 'icu-regextst:2969', - 'icu-regextst:2970', - 'icu-regextst:2971', - 'icu-regextst:2972', - 'icu-regextst:2973', - 'icu-regextst:2974', - 'icu-regextst:2975', - 'icu-regextst:2976', - 'icu-regextst:2977', - 'icu-regextst:2978', - 'icu-regextst:2979', - 'icu-regextst:2980', - 'icu-regextst:2981', - 'icu-regextst:2986', - 'icu-regextst:2987', - 'icu-regextst:2988', - 'icu-regextst:2989', - 'icu-regextst:2990', - 'icu-regextst:2991', - 'icu-regextst:2992', - 'icu-regextst:2993', - 'icu-regextst:2994', - 'icu-regextst:2995', - 'icu-regextst:2996', - 'icu-regextst:2997', - 'icu-regextst:3040', - 'icu-regextst:3041', - 'icu-regextst:3042', - 'icu-regextst:3043', - 'icu-regextst:3044', - 'icu-regextst:3045', - 'icu-regextst:3046', - 'icu-regextst:3047', - 'icu-regextst:3048', - 'icu-regextst:3049', - 'icu-regextst:3050', - 'icu-regextst:3051', - 'icu-regextst:3072', - 'icu-regextst:3073', - 'icu-regextst:3074', - 'icu-regextst:3075', - 'icu-regextst:3076', - 'icu-regextst:3077', - 'icu-regextst:3078', - 'icu-regextst:3079', - 'icu-regextst:3080', - 'icu-regextst:3081', - 'icu-regextst:3082', - 'icu-regextst:3083', - 'icu-regextst:3204', - 'icu-regextst:3205', - 'icu-regextst:3206', - 'icu-regextst:3207', - 'icu-regextst:3208', - 'icu-regextst:3209', - 'icu-regextst:3210', - 'icu-regextst:3211', - 'icu-regextst:3212', - 'icu-regextst:3213', - 'icu-regextst:3214', - 'icu-regextst:3215', 'icu-regextst:3338', 'icu-regextst:3339', 'icu-regextst:3340', 'icu-regextst:3341', 'icu-regextst:3342', 'icu-regextst:3343', - 'icu-regextst:3354', - 'icu-regextst:3355', - 'icu-regextst:3356', - 'icu-regextst:3357', - 'icu-regextst:3358', - 'icu-regextst:3359', - 'icu-regextst:3360', - 'icu-regextst:3361', - 'icu-regextst:3362', - 'icu-regextst:3363', - 'icu-regextst:3364', - 'icu-regextst:3365', - 'icu-regextst:3366', - 'icu-regextst:3367', - 'icu-regextst:3368', - 'icu-regextst:3369', - 'icu-regextst:3370', - 'icu-regextst:3371', - 'icu-regextst:3372', - 'icu-regextst:3373', - 'icu-regextst:3374', - 'icu-regextst:3375', - 'icu-regextst:3376', - 'icu-regextst:3377', - 'icu-regextst:3378', - 'icu-regextst:3379', - 'icu-regextst:3380', - 'icu-regextst:3381', - 'icu-regextst:3386', - 'icu-regextst:3387', - 'icu-regextst:3388', - 'icu-regextst:3389', - 'icu-regextst:3390', - 'icu-regextst:3391', - 'icu-regextst:3392', - 'icu-regextst:3393', - 'icu-regextst:3394', - 'icu-regextst:3395', - 'icu-regextst:3424', - 'icu-regextst:3425', - 'icu-regextst:3426', - 'icu-regextst:3427', - 'icu-regextst:3428', - 'icu-regextst:3429', - 'icu-regextst:3430', - 'icu-regextst:3431', - 'icu-regextst:3432', - 'icu-regextst:3433', - 'icu-regextst:3434', - 'icu-regextst:3435', - 'icu-regextst:3436', - 'icu-regextst:3437', - 'icu-regextst:3438', - 'icu-regextst:3439', - 'icu-regextst:3440', - 'icu-regextst:3441', - 'icu-regextst:3442', - 'icu-regextst:3443', - 'icu-regextst:3444', - 'icu-regextst:3445', - 'icu-regextst:3446', - 'icu-regextst:3447', - 'icu-regextst:3456', - 'icu-regextst:3457', - 'icu-regextst:3458', - 'icu-regextst:3459', - 'icu-regextst:3460', - 'icu-regextst:3461', - 'icu-regextst:3462', - 'icu-regextst:3463', - 'icu-regextst:3464', - 'icu-regextst:3465', - 'icu-regextst:3466', - 'icu-regextst:3467', - 'icu-regextst:3506', - 'icu-regextst:3507', - 'icu-regextst:3508', - 'icu-regextst:3509', - 'icu-regextst:3510', - 'icu-regextst:3511', - 'icu-regextst:3512', - 'icu-regextst:3513', - 'icu-regextst:3514', - 'icu-regextst:3515', - 'icu-regextst:3516', - 'icu-regextst:3517', 'icu-regextst:3518', 'icu-regextst:3519', 'icu-regextst:3520', @@ -993,28 +532,6 @@ protected function get_known_failures(): array { 'icu-regextst:3523', 'icu-regextst:3540', 'icu-regextst:3541', - 'icu-regextst:3606', - 'icu-regextst:3607', - 'icu-regextst:3608', - 'icu-regextst:3609', - 'icu-regextst:3610', - 'icu-regextst:3611', - 'icu-regextst:3672', - 'icu-regextst:3673', - 'icu-regextst:3674', - 'icu-regextst:3675', - 'icu-regextst:3676', - 'icu-regextst:3677', - 'icu-regextst:3678', - 'icu-regextst:3679', - 'icu-regextst:3704', - 'icu-regextst:3705', - 'icu-regextst:3706', - 'icu-regextst:3707', - 'icu-regextst:3708', - 'icu-regextst:3709', - 'icu-regextst:3710', - 'icu-regextst:3711', 'icu-regextst:3836', 'icu-regextst:3837', ); @@ -1107,16 +624,8 @@ protected function get_known_failures(): array { 'mysql-server-suite:9646', 'mysql-server-suite:9647', 'mysql-server-suite:9648', - 'mysql-server-suite:9657', - 'mysql-server-suite:9658', - 'mysql-server-suite:9659', - 'mysql-server-suite:9660', - 'mysql-server-suite:9661', - 'mysql-server-suite:9662', 'mysql-server-suite:9663', 'mysql-server-suite:9664', - 'icu-regextst:1033', - 'icu-regextst:1034', 'icu-regextst:1037', 'icu-regextst:1038', 'icu-regextst:2460', @@ -1146,6 +655,15 @@ protected function get_known_failures(): array { ); } + // PCRE2 rejects a set difference after a class, like "[\w--b]". PCRE 8 reads the "-" literally, which gives MySQL's results here. + if ( $is_pcre2 ) { + array_push( + $failures, + 'icu-regextst:1033', + 'icu-regextst:1034' + ); + } + // PCRE2 rejects unknown escapes, like "\j", unless PHP reads them literally. PCRE 8 reads them literally. if ( $is_pcre2 && ! $literal_escapes ) { array_push( @@ -1240,8 +758,6 @@ protected function get_known_failures(): array { 'icu-regextst:374', 'icu-regextst:377', 'icu-regextst:378', - 'icu-regextst:381', - 'icu-regextst:382', 'icu-regextst:580', 'icu-regextst:631', 'icu-regextst:719', @@ -1289,6 +805,12 @@ protected function get_known_failures(): array { 'icu-regextst:3543', 'icu-regextst:3544', 'icu-regextst:3545', + 'icu-regextst:3606', + 'icu-regextst:3607', + 'icu-regextst:3608', + 'icu-regextst:3609', + 'icu-regextst:3610', + 'icu-regextst:3611', 'icu-regextst:3838', 'icu-regextst:3839' ); @@ -1314,15 +836,6 @@ protected function get_known_failures(): array { ); } - // Before PCRE2 10.40, PHP read the unknown \p{alphabetic} literally, which gives MySQL's results here. Since then, it matches bytes of UTF-8 characters as Latin-1 letters. - if ( ! ( $literal_escapes && version_compare( $pcre_version, '10.40', '<' ) ) ) { - array_push( - $failures, - 'mysql-server-suite:12397', - 'mysql-server-suite:12398' - ); - } - // Binary Unicode properties, like \p{Alphabetic}, arrived in PCRE2 10.40. if ( version_compare( $pcre_version, '10.40', '<' ) ) { array_push( @@ -1370,7 +883,11 @@ protected function get_known_failures(): array { 'mysql-server-suite:12425', 'mysql-server-suite:12426', 'mysql-server-suite:12427', - 'mysql-server-suite:12428' + 'mysql-server-suite:12428', + 'mysql-server-suite:12449', + 'mysql-server-suite:12450', + 'icu-regextst:1452', + 'icu-regextst:1453' ); // PHP reads these unknown properties literally, which gives MySQL's results here. @@ -1379,6 +896,8 @@ protected function get_known_failures(): array { $failures, 'mysql-server-suite:12379', 'mysql-server-suite:12380', + 'mysql-server-suite:12397', + 'mysql-server-suite:12398', 'mysql-server-suite:12403', 'mysql-server-suite:12404', 'mysql-server-suite:12405', @@ -1442,6 +961,19 @@ protected function get_known_failures(): array { ); } + // Before PCRE2 10.43, \w didn't match combining marks, like the vowel signs of "नमस्ते", which ICU's \w does. + if ( version_compare( $pcre_version, '10.43', '<' ) ) { + array_push( + $failures, + 'mysql-server-suite:1533', + 'mysql-server-suite:1534', + 'mysql-server-suite:1701', + 'mysql-server-suite:1702', + 'mysql-server-suite:7923', + 'mysql-server-suite:7924' + ); + } + // Since PCRE2 10.43, these lookbehinds of variable length compile when PHP reads their "\u" and "\U" escapes literally, which gives MySQL's results here. if ( ! ( $literal_escapes && version_compare( $pcre_version, '10.43', '>=' ) ) ) { array_push( From 248cd2f12c35216b9a7476ac4dd16ef03fed3546 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jake=C5=A1?= Date: Wed, 11 Nov 2026 12:04:17 +0100 Subject: [PATCH 2/6] Pass REGEXP BINARY to the REGEXP function as a flag The operator translation marked REGEXP BINARY by prepending a NUL byte to the pattern. Any pattern value that started with a NUL byte was then silently matched as REGEXP BINARY: the byte was dropped and matching became case-sensitive. Translate REGEXP on the "predicate" node, which has both operands, and call the REGEXP function directly with an explicit flag instead. Escape NUL bytes in patterns, which PHP rejects before 8.2. Wrapping the call in parentheses also keeps the MySQL precedence, where REGEXP binds tighter than comparison operators. SQLite read 1 = 'a' REGEXP 'a' as (1 = 'a') REGEXP 'a'. CHECK constraints and DEFAULT expressions with REGEXP BINARY in existing databases keep the old NUL-byte marker until their table is rebuilt. --- .../src/sqlite/class-wp-mysql-on-sqlite.php | 38 ++++----- ...s-wp-sqlite-pdo-user-defined-functions.php | 52 +++++------- .../tests/WP_MySQL_On_SQLite_Tests.php | 83 +++++++++++++++++++ 3 files changed, 123 insertions(+), 50 deletions(-) diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php index bef1bedeb..9bfa7bac9 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php @@ -4431,12 +4431,17 @@ private function translate( $node ): ?string { return $this->translate_bit_expr( $node ); case 'simpleExprBody': return $this->translate_simple_expr_body( $node ); + case 'predicate': + $operations = $node->get_first_child_node( 'predicateOperations' ); + $token = null === $operations ? null : $operations->get_first_child_token(); + if ( null !== $token && WP_MySQL_Lexer::REGEXP_SYMBOL === $token->id ) { + return $this->translate_regexp_predicate( $node, $operations ); + } + return $this->translate_sequence( $node->get_children() ); case 'predicateOperations': $token = $node->get_first_child_token(); if ( WP_MySQL_Lexer::LIKE_SYMBOL === $token->id ) { return $this->translate_like( $node ); - } elseif ( WP_MySQL_Lexer::REGEXP_SYMBOL === $token->id ) { - return $this->translate_regexp_functions( $node ); } return $this->translate_sequence( $node->get_children() ); case 'runtimeFunctionCall': @@ -5154,31 +5159,26 @@ private function translate_like( WP_Parser_Node $node ): string { /** * Translate MySQL REGEXP expression to SQLite. * - * @param WP_Parser_Node $node The "predicateOperations" AST node. + * @param WP_Parser_Node $node The "predicate" AST node. + * @param WP_Parser_Node $operations The "predicateOperations" AST node. * @return string The translated value. * @throws WP_MySQL_On_SQLite_Exception When the translation fails. */ - private function translate_regexp_functions( WP_Parser_Node $node ): string { - $tokens = $node->get_descendant_tokens(); + private function translate_regexp_predicate( WP_Parser_Node $node, WP_Parser_Node $operations ): string { + $tokens = $operations->get_descendant_tokens(); $is_binary = isset( $tokens[1] ) && WP_MySQL_Lexer::BINARY_SYMBOL === $tokens[1]->id; + $subject = $this->translate( $node->get_first_child_node() ); + $pattern = $this->translate( $operations->get_first_child_node() ); + $not = null === $node->get_first_child_node( 'notRule' ) ? '' : 'NOT '; /* - * If the query says REGEXP BINARY, the comparison is byte-by-byte - * and letter casing matters – lowercase and uppercase letters are - * represented using different byte codes. + * Call the REGEXP function directly, so that REGEXP BINARY can pass + * a flag for byte-by-byte, case-sensitive matching. * - * The REGEXP function can't be easily made to accept two - * parameters, so we'll have to use a hack to get around this. - * - * If the first character of the pattern is a null byte, we'll - * remove it and make the comparison case-sensitive. This should - * be reasonably safe since PHP does not allow null bytes in - * regular expressions anyway. + * The parentheses keep the MySQL precedence. In SQLite, REGEXP binds + * as loosely as "=", so "1 = 'a' REGEXP 'a'" would be "(1 = 'a') REGEXP 'a'". */ - if ( true === $is_binary ) { - return 'REGEXP CHAR(0) || ' . $this->translate( $node->get_first_child_node() ); - } - return 'REGEXP ' . $this->translate( $node->get_first_child_node() ); + return sprintf( '(%sREGEXP(%s, %s%s))', $not, $pattern, $subject, $is_binary ? ', 1' : '' ); } /** diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php index 7021d3fba..89978c7ec 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php @@ -98,6 +98,7 @@ public static function register_for( $pdo ): self { * @var array */ private $function_arities = array( + 'regexp' => array( 2, 3 ), 'regexp_like' => array( 2, 3 ), ); @@ -589,40 +590,22 @@ public function isnull( $field ) { * * @param string|null $pattern Regular expression to match. * @param string|null $field Haystack. + * @param int $binary 1 for REGEXP BINARY, which matches bytes + * case-sensitively; 0 otherwise. * * @throws Exception If the pattern is not a valid regular expression. * @return int|null 1 if matched, 0 if not matched, NULL if an argument is NULL. */ - public function regexp( $pattern, $field ) { + public function regexp( $pattern, $field, $binary = 0 ) { if ( null === $pattern || null === $field ) { return null; } - $pattern = (string) $pattern; - - /* - * If the original query says REGEXP BINARY - * the comparison is byte-by-byte and letter casing now - * matters since lower- and upper-case letters have different - * byte codes. - * - * The REGEXP function can't be easily made to accept two - * parameters, so we'll have to use a hack to get around this. - * - * If the first character of the pattern is a null byte, we'll - * remove it and make the comparison case-sensitive. This should - * be reasonably safe since PHP does not allow null bytes in - * regular expressions anyway. - */ - $is_binary = '' !== $pattern && "\x00" === $pattern[0]; - if ( $is_binary ) { - $pattern = substr( $pattern, 1 ); - } /* * Data that is not valid UTF-8 can only be a binary string in MySQL, * and MySQL matches binary strings byte by byte and case-sensitively. */ - $byte_mode = $is_binary || ! $this->regexp_is_utf8( $pattern ) || ! $this->regexp_is_utf8( $field ); + $byte_mode = 1 === $binary || ! $this->regexp_is_utf8( $pattern ) || ! $this->regexp_is_utf8( $field ); $compiled = $this->regexp_compile( $pattern, $byte_mode ? 'c' : '', $byte_mode ); $result = preg_match( $compiled, $field ); if ( false === $result ) { @@ -1144,7 +1127,7 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false ) { } else { $newline = '(*ANY)'; } - $compiled = '/' . $newline . $this->regexp_escape_delimiter( $pattern ) . '/' . $modifiers; + $compiled = '/' . $newline . $this->regexp_escape_pattern( $pattern ) . '/' . $modifiers; /* * Compile the pattern by matching it against an empty string. Only a compile @@ -1169,13 +1152,19 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false ) { } /** - * Escape pattern delimiters that are not already escaped. + * Escape pattern delimiters and NUL bytes that are not already escaped. + * + * PHP before 8.2 rejects NUL bytes in patterns, so they become "\x00". * * @param string $pattern The regex pattern. * * @return string Pattern safe to wrap in slash delimiters. */ - private function regexp_escape_delimiter( $pattern ) { + private function regexp_escape_pattern( $pattern ) { + $special = array( + '/' => '\\/', + "\0" => '\\x00', + ); $escaped = ''; $quoted = false; $length = strlen( $pattern ); @@ -1186,19 +1175,20 @@ private function regexp_escape_delimiter( $pattern ) { $escaped .= '\\E'; $quoted = false; ++$i; - } elseif ( '/' === $character ) { - $escaped .= '\\E\\/\\Q'; + } elseif ( isset( $special[ $character ] ) ) { + $escaped .= '\\E' . $special[ $character ] . '\\Q'; } else { $escaped .= $character; } continue; } if ( '\\' === $character && $i + 1 < $length ) { - $escaped .= $character . $pattern[ $i + 1 ]; - $quoted = 'Q' === $pattern[ $i + 1 ]; + $next = $pattern[ $i + 1 ]; + $escaped .= isset( $special[ $next ] ) ? $special[ $next ] : $character . $next; + $quoted = 'Q' === $next; ++$i; - } elseif ( '/' === $character ) { - $escaped .= '\\/'; + } elseif ( isset( $special[ $character ] ) ) { + $escaped .= $special[ $character ]; } else { $escaped .= $character; } diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php index 1efa36494..18cd281ea 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php @@ -453,6 +453,89 @@ public function testRegexpLegacyOperatorUtf8Handling() { $this->assertSame( '1', $this->last_result[0]->r ); } + public function testRegexpNulBytePattern() { + // A pattern starting with a NUL byte is an ordinary case-insensitive pattern. + // NUL bytes also match in quoted text and after a backslash. + $this->assertQuery( 'CREATE TABLE nul_patterns (id INT PRIMARY KEY, s TEXT, p TEXT)' ); + $this->assertQuery( + "INSERT INTO nul_patterns VALUES + (1, X'610062', X'0042'), + (2, 'aBc', X'0062'), + (3, 'abc', 'B'), + (4, X'61002F62', CONCAT('\\\\Qa', X'00', '/b\\\\E')), + (5, X'610062', CONCAT('a\\\\', X'00', 'B'))" + ); + $this->assertQuery( + 'SELECT + s REGEXP p AS matched, + s NOT REGEXP p AS not_matched, + s REGEXP BINARY p AS binary_matched, + REGEXP_LIKE(s, p) AS like_matched + FROM nul_patterns ORDER BY id' + ); + $this->assertEquals( + array( + (object) array( + 'matched' => '1', + 'not_matched' => '0', + 'binary_matched' => '0', + 'like_matched' => '1', + ), + (object) array( + 'matched' => '0', + 'not_matched' => '1', + 'binary_matched' => '0', + 'like_matched' => '0', + ), + (object) array( + 'matched' => '1', + 'not_matched' => '0', + 'binary_matched' => '0', + 'like_matched' => '1', + ), + (object) array( + 'matched' => '1', + 'not_matched' => '0', + 'binary_matched' => '1', + 'like_matched' => '1', + ), + (object) array( + 'matched' => '1', + 'not_matched' => '0', + 'binary_matched' => '0', + 'like_matched' => '1', + ), + ), + $this->last_result + ); + } + + public function testRegexpLegacyOperatorPrecedence() { + // REGEXP binds tighter than comparison operators. + $this->assertQuery( + "SELECT + 1 = 'a' REGEXP 'a' AS a, + 0 = 'a' REGEXP 'b' AS b, + 'a' REGEXP 'a' = 1 AS c, + 1 = 'a' NOT REGEXP 'b' AS d, + 1 + 1 REGEXP '2' AS e, + NOT 'a' REGEXP 'b' AS f" + ); + $this->assertEquals( + array( + (object) array( + 'a' => '1', + 'b' => '1', + 'c' => '1', + 'd' => '1', + 'e' => '1', + 'f' => '1', + ), + ), + $this->last_result + ); + } + public function testRegexpLegacyOperatorMatchesBinaryDataByBytes() { // Non-UTF-8 data is matched like MySQL binary strings: byte by byte and case-sensitively. $this->assertQuery( 'CREATE TABLE blobs (id INT PRIMARY KEY, b BLOB)' ); From d7b82f29f1b4d73fa6b2da451449c7c8d61bdbdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jake=C5=A1?= Date: Wed, 11 Nov 2026 12:05:38 +0100 Subject: [PATCH 3/6] Pass REGEXP operands to the functions as text PDO narrows integer arguments of user-defined functions to 32 bits before PHP receives them. On a BIGINT column holding 15551234567, n REGEXP '^1' and REGEXP_LIKE(n, '^1') returned 0. PHP can't recover the value, so cast the subject and the pattern to text in the translator. SQLite now converts float operands to text instead of PHP. A whole number in a DOUBLE or FLOAT column reads as "20.0", where MySQL reads "20". --- .../src/sqlite/class-wp-mysql-on-sqlite.php | 11 +++- .../tests/WP_MySQL_On_SQLite_Tests.php | 52 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php index 9bfa7bac9..2437890e1 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php @@ -5177,8 +5177,10 @@ private function translate_regexp_predicate( WP_Parser_Node $node, WP_Parser_Nod * * The parentheses keep the MySQL precedence. In SQLite, REGEXP binds * as loosely as "=", so "1 = 'a' REGEXP 'a'" would be "(1 = 'a') REGEXP 'a'". + * + * PDO narrows integer UDF arguments to 32 bits, so pass the operands as text. */ - return sprintf( '(%sREGEXP(%s, %s%s))', $not, $pattern, $subject, $is_binary ? ', 1' : '' ); + return sprintf( '(%sREGEXP(CAST(%s AS TEXT), CAST(%s AS TEXT)%s))', $not, $pattern, $subject, $is_binary ? ', 1' : '' ); } /** @@ -5393,6 +5395,13 @@ private function translate_function_call( WP_Parser_Node $node ): string { } case 'VERSION': return $this->quote_sqlite_value( $this->get_mysql_server_version_string() ); + case 'REGEXP_LIKE': + // PDO narrows integer UDF arguments to 32 bits, so pass the subject and the pattern as text. + $text_args = min( count( $args ), 2 ); + for ( $i = 0; $i < $text_args; $i++ ) { + $args[ $i ] = sprintf( 'CAST(%s AS TEXT)', $args[ $i ] ); + } + return sprintf( '%s(%s)', $name, implode( ', ', $args ) ); default: return $this->translate_sequence( $node->get_children() ); } diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php index 18cd281ea..a5cf36c56 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php @@ -679,6 +679,58 @@ public function testRegexpUnicodeNewlineHandling() { $this->assertSame( '1', $this->last_result[0]->r ); } + /** + * @dataProvider regexpBigIntegerCases + */ + public function testRegexpBigIntegerOperands( $sql, $expected ) { + $this->assertQuery( "SELECT $sql AS r" ); + $this->assertSame( $expected, $this->last_result[0]->r ); + } + + public static function regexpBigIntegerCases() { + return array( + // The smallest values that don't fit in 32 bits. + '2^31, subject' => array( "REGEXP_LIKE(2147483648, '^2147483648\$')", '1' ), + '-2^31 - 1, subject' => array( "REGEXP_LIKE(-2147483649, '^-2147483649\$')", '1' ), + 'largest BIGINT' => array( "REGEXP_LIKE(9223372036854775807, '^9223372036854775807\$')", '1' ), + + 'REGEXP_LIKE subject' => array( "REGEXP_LIKE(9999999999, '^9')", '1' ), + 'REGEXP_LIKE pattern' => array( "REGEXP_LIKE('9999999999', 9999999999)", '1' ), + 'operator subject' => array( "9999999999 REGEXP '^9'", '1' ), + 'operator pattern' => array( "'9999999999' REGEXP 9999999999", '1' ), + 'negated operator' => array( "9999999999 NOT REGEXP '^9'", '0' ), + 'REGEXP BINARY' => array( "9999999999 REGEXP BINARY '^9'", '1' ), + ); + } + + public function testRegexpBigIntegerColumn() { + $this->assertQuery( 'CREATE TABLE phones (id INT PRIMARY KEY, phone BIGINT)' ); + $this->assertQuery( 'INSERT INTO phones VALUES (1, 15551234567), (2, 42), (3, NULL)' ); + $this->assertQuery( + "SELECT + phone REGEXP '^1' AS operator_result, + REGEXP_LIKE(phone, '^1') AS like_result + FROM phones ORDER BY id" + ); + $this->assertEquals( + array( + (object) array( + 'operator_result' => '1', + 'like_result' => '1', + ), + (object) array( + 'operator_result' => '0', + 'like_result' => '0', + ), + (object) array( + 'operator_result' => null, + 'like_result' => null, + ), + ), + $this->last_result + ); + } + public function testInsertDateNow() { $this->assertQuery( "INSERT INTO _dates (option_name, option_value) VALUES ('first', now());" From 5201edd49d8fb15a91a85e9e5fd779fa57b129e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jake=C5=A1?= Date: Wed, 11 Nov 2026 12:06:32 +0100 Subject: [PATCH 4/6] Document known REGEXP differences from MySQL Point to the known failures of the REGEXP test suites for the syntax that ICU and PCRE read differently, instead of listing it. List the differences that the suites don't show: case folding, CRLF line breaks, and named groups in PCRE, and binary data and the text of numbers in SQLite. --- ...s-wp-sqlite-pdo-user-defined-functions.php | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php index 89978c7ec..b51d0b7f1 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php @@ -1048,23 +1048,19 @@ private function regexp_check_match_type( $function_name, $match_type ) { * (UTF-8) modifier is applied unless byte-oriented matching is requested. * Case-insensitive is the default. * - * MySQL's native engine is ICU; we use PHP's PCRE. The two diverge in a - * few corners: - * - * - Some Unicode property shorthands and POSIX class spellings differ. - * - PCRE accepts both `(?...)` and `(?P...)`; MySQL accepts - * only the former and errors on the latter. - * - ICU supports multi-code-point case folds such as "ß" matching "ss"; - * PCRE's case-insensitive mode does not. - * - * Known limitations of this emulation: - * - * - The default (case-insensitive) is correct for the usual - * `utf8mb4_0900_ai_ci` collation; callers that rely on a `_bin` or - * `_cs` collation must pass an explicit `c` match_type because this - * helper has no access to the session collation. - * - Data that is not valid UTF-8, and REGEXP BINARY, are matched byte by - * byte and case-sensitively, like MySQL binary strings. + * MySQL uses ICU, which differs from PCRE in some syntax, like ICU-only escapes + * and nested sets. The known failures of the REGEXP test suites list these + * cases. Other known differences: + * + * - Text matching ignores case unless the `c` flag is passed, as the column + * collation isn't known here. + * - PCRE doesn't match "ß" with "ss" when ignoring case, `$` also matches + * between CR and LF, and `(?P...)` is valid. + * - Data that isn't valid UTF-8 is matched byte by byte and case-sensitively, + * like MySQL binary strings. Other binary values match as text, so case + * sensitivity can differ between rows. + * - SQLite converts numbers to text in its own format, so `20e0` reads "20.0" + * (MySQL: "20"), and `0.000001` reads "1.0e-06". * * @param string $pattern The MySQL regex pattern. * @param string $match_type MySQL match_type flag string. From d7183e11abc260bedf55dd0324f0b5f9cbc28bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jake=C5=A1?= Date: Wed, 11 Nov 2026 12:07:27 +0100 Subject: [PATCH 5/6] Reject unknown escapes in REGEXP patterns on PHP 7 PHP 7.3 and 7.4 read unknown escapes as literal text. '\u0041' silently matched "u0041", and '\p{Foo}' matched nothing instead of failing. PHP 7.2 did the same for escapes like '\y'. PHP 8 reports them as errors. The "X" modifier makes them errors on PHP 7, too, and does nothing on PHP 8. MySQL reads escapes like '\y' as literals, so PHP 7 was right for those. Now PHP 7 fails the same REGEXP_LIKE() cases as PHP 8. --- ...s-wp-sqlite-pdo-user-defined-functions.php | 7 +- .../tests/WP_MySQL_On_SQLite_Tests.php | 5 + .../WP_MySQL_On_SQLite_Regexp_Like_Tests.php | 372 ++++++++---------- 3 files changed, 178 insertions(+), 206 deletions(-) diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php index b51d0b7f1..40308b86a 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php @@ -1100,7 +1100,12 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false ) { } } - $modifiers = $binary_mode ? '' : 'u'; + /* + * PHP 7 can read an unknown escape as literal text. For "\y", that matches ICU, + * but "\u0041" matches "u0041", where ICU matches "A". The "X" modifier makes + * all of them errors, like in PHP 8. + */ + $modifiers = $binary_mode ? 'X' : 'uX'; if ( ! $case_sensitive ) { $modifiers .= 'i'; } diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php index a5cf36c56..340331074 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php @@ -324,6 +324,11 @@ public function testRegexpLikeErrors( $sql, $message ) { public static function regexpLikeErrors() { return array( 'invalid pattern' => array( "REGEXP_LIKE('abc', '(abc')", 'Invalid regular expression: (abc.' ), + 'unknown escape' => array( "REGEXP_LIKE('y', '\\\\y')", 'Invalid regular expression: \\y.' ), + 'ICU escape' => array( "REGEXP_LIKE('A', '\\\\u0041')", 'Invalid regular expression: \\u0041.' ), + 'bad property' => array( "REGEXP_LIKE('a', '\\\\p{Foo}')", 'Invalid regular expression: \\p{Foo}.' ), + 'operator escape' => array( "'y' REGEXP '\\\\y'", 'Invalid regular expression: \\y.' ), + 'binary escape' => array( "X'FF' REGEXP '\\\\y'", 'Invalid regular expression: \\y.' ), 'extra argument' => array( "REGEXP_LIKE('abc', 'abc', 'c', 'extra')", 'SQLSTATE[HY000]: General error: 1 wrong number of arguments to function REGEXP_LIKE()', diff --git a/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Like_Tests.php b/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Like_Tests.php index b1fb7fbf6..0e7b7a362 100644 --- a/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Like_Tests.php +++ b/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Like_Tests.php @@ -12,9 +12,6 @@ protected function get_known_failures(): array { $pcre_version = explode( ' ', PCRE_VERSION )[0]; $is_pcre2 = version_compare( $pcre_version, '10', '>=' ); - // PHP 7.3 and 7.4 read unknown escapes, like "\j", literally. - $literal_escapes = PHP_VERSION_ID >= 70300 && PHP_VERSION_ID < 80000; - // Cases that fail in every environment. $failures = array( 'mysql-server-suite:9591', @@ -63,21 +60,62 @@ protected function get_known_failures(): array { 'mysql-server-suite:9668', 'mysql-server-suite:12325', 'mysql-server-suite:12326', + 'mysql-server-suite:12327', + 'mysql-server-suite:12328', + 'mysql-server-suite:12329', + 'mysql-server-suite:12330', + 'mysql-server-suite:12331', + 'mysql-server-suite:12332', 'mysql-server-suite:12333', 'mysql-server-suite:12334', 'mysql-server-suite:12335', 'mysql-server-suite:12336', 'mysql-server-suite:12337', 'mysql-server-suite:12338', + 'mysql-server-suite:12339', + 'mysql-server-suite:12340', + 'mysql-server-suite:12341', + 'mysql-server-suite:12342', + 'mysql-server-suite:12343', + 'mysql-server-suite:12344', + 'mysql-server-suite:12345', + 'mysql-server-suite:12346', 'mysql-server-suite:12347', 'mysql-server-suite:12348', 'mysql-server-suite:12349', 'mysql-server-suite:12350', + 'mysql-server-suite:12351', + 'mysql-server-suite:12352', + 'mysql-server-suite:12353', + 'mysql-server-suite:12354', + 'mysql-server-suite:12355', + 'mysql-server-suite:12356', + 'mysql-server-suite:12357', + 'mysql-server-suite:12358', + 'mysql-server-suite:12359', + 'mysql-server-suite:12360', + 'mysql-server-suite:12361', + 'mysql-server-suite:12362', + 'mysql-server-suite:12363', + 'mysql-server-suite:12364', + 'mysql-server-suite:12365', + 'mysql-server-suite:12366', + 'mysql-server-suite:12367', + 'mysql-server-suite:12368', + 'mysql-server-suite:12369', + 'mysql-server-suite:12370', + 'mysql-server-suite:12371', + 'mysql-server-suite:12372', 'mysql-server-suite:12373', 'mysql-server-suite:12374', + 'mysql-server-suite:12375', + 'mysql-server-suite:12376', 'mysql-server-suite:12455', 'mysql-server-suite:12456', 'mysql-server-suite:12457', + 'mysql-server-suite:12458', + 'mysql-server-suite:12459', + 'mysql-server-suite:12460', 'mysql-server-suite:12461', 'mysql-server-suite:12462', 'mysql-server-suite:12463', @@ -90,25 +128,44 @@ protected function get_known_failures(): array { 'mysql-server-suite:12470', 'mysql-server-suite:12471', 'mysql-server-suite:12472', + 'mysql-server-suite:12473', + 'mysql-server-suite:12474', + 'mysql-server-suite:12475', + 'mysql-server-suite:12476', + 'mysql-server-suite:12477', + 'mysql-server-suite:12478', 'mysql-server-suite:12479', 'mysql-server-suite:12480', 'mysql-server-suite:12481', 'mysql-server-suite:12482', 'mysql-server-suite:12483', + 'mysql-server-suite:12484', 'mysql-server-suite:12485', 'mysql-server-suite:12486', + 'mysql-server-suite:12487', + 'mysql-server-suite:12488', 'mysql-server-suite:12489', 'mysql-server-suite:12490', 'mysql-server-suite:12491', + 'mysql-server-suite:12492', 'mysql-server-suite:12493', + 'mysql-server-suite:12494', 'mysql-server-suite:12495', 'mysql-server-suite:12496', 'mysql-server-suite:12497', + 'mysql-server-suite:12498', 'mysql-server-suite:12499', 'mysql-server-suite:12500', + 'mysql-server-suite:12501', + 'mysql-server-suite:12502', + 'mysql-server-suite:12503', + 'mysql-server-suite:12504', + 'mysql-server-suite:12505', + 'mysql-server-suite:12506', 'mysql-server-suite:12507', 'mysql-server-suite:12508', 'mysql-server-suite:12509', + 'mysql-server-suite:12510', 'mysql-server-suite:12589', 'mysql-server-suite:12590', 'mysql-server-suite:12591', @@ -161,8 +218,16 @@ protected function get_known_failures(): array { 'icu-regextst:234', 'icu-regextst:329', 'icu-regextst:330', + 'icu-regextst:365', + 'icu-regextst:366', + 'icu-regextst:367', + 'icu-regextst:368', + 'icu-regextst:373', + 'icu-regextst:374', 'icu-regextst:375', 'icu-regextst:376', + 'icu-regextst:377', + 'icu-regextst:378', 'icu-regextst:383', 'icu-regextst:384', 'icu-regextst:385', @@ -201,6 +266,7 @@ protected function get_known_failures(): array { 'icu-regextst:565', 'icu-regextst:566', 'icu-regextst:567', + 'icu-regextst:580', 'icu-regextst:581', 'icu-regextst:582', 'icu-regextst:583', @@ -213,6 +279,7 @@ protected function get_known_failures(): array { 'icu-regextst:628', 'icu-regextst:629', 'icu-regextst:630', + 'icu-regextst:631', 'icu-regextst:632', 'icu-regextst:633', 'icu-regextst:705', @@ -227,12 +294,20 @@ protected function get_known_failures(): array { 'icu-regextst:716', 'icu-regextst:717', 'icu-regextst:718', + 'icu-regextst:719', + 'icu-regextst:720', + 'icu-regextst:725', + 'icu-regextst:726', + 'icu-regextst:727', + 'icu-regextst:728', 'icu-regextst:843', 'icu-regextst:844', 'icu-regextst:931', 'icu-regextst:932', 'icu-regextst:933', 'icu-regextst:934', + 'icu-regextst:935', + 'icu-regextst:936', 'icu-regextst:941', 'icu-regextst:942', 'icu-regextst:949', @@ -305,6 +380,7 @@ protected function get_known_failures(): array { 'icu-regextst:1056', 'icu-regextst:1057', 'icu-regextst:1058', + 'icu-regextst:1065', 'icu-regextst:1066', 'icu-regextst:1081', 'icu-regextst:1082', @@ -340,6 +416,8 @@ protected function get_known_failures(): array { 'icu-regextst:1112', 'icu-regextst:1113', 'icu-regextst:1114', + 'icu-regextst:1115', + 'icu-regextst:1116', 'icu-regextst:1117', 'icu-regextst:1118', 'icu-regextst:1119', @@ -354,10 +432,16 @@ protected function get_known_failures(): array { 'icu-regextst:1128', 'icu-regextst:1129', 'icu-regextst:1130', + 'icu-regextst:1131', + 'icu-regextst:1132', 'icu-regextst:1133', 'icu-regextst:1134', 'icu-regextst:1135', 'icu-regextst:1136', + 'icu-regextst:1137', + 'icu-regextst:1138', + 'icu-regextst:1139', + 'icu-regextst:1140', 'icu-regextst:1141', 'icu-regextst:1142', 'icu-regextst:1179', @@ -408,6 +492,10 @@ protected function get_known_failures(): array { 'icu-regextst:1337', 'icu-regextst:1338', 'icu-regextst:1339', + 'icu-regextst:1364', + 'icu-regextst:1365', + 'icu-regextst:1366', + 'icu-regextst:1367', 'icu-regextst:1370', 'icu-regextst:1371', 'icu-regextst:1372', @@ -496,16 +584,28 @@ protected function get_known_failures(): array { 'icu-regextst:1457', 'icu-regextst:1458', 'icu-regextst:1459', + 'icu-regextst:1466', + 'icu-regextst:1467', 'icu-regextst:1468', 'icu-regextst:1469', 'icu-regextst:1470', 'icu-regextst:1471', + 'icu-regextst:1472', + 'icu-regextst:1473', + 'icu-regextst:1474', 'icu-regextst:1475', + 'icu-regextst:1476', + 'icu-regextst:1477', 'icu-regextst:1478', 'icu-regextst:1479', + 'icu-regextst:1480', + 'icu-regextst:1481', + 'icu-regextst:1482', 'icu-regextst:1483', 'icu-regextst:1484', 'icu-regextst:1485', + 'icu-regextst:1490', + 'icu-regextst:1491', 'icu-regextst:1494', 'icu-regextst:1495', 'icu-regextst:1506', @@ -518,22 +618,54 @@ protected function get_known_failures(): array { 'icu-regextst:2771', 'icu-regextst:2772', 'icu-regextst:2773', + 'icu-regextst:2774', + 'icu-regextst:2775', + 'icu-regextst:2776', + 'icu-regextst:2777', + 'icu-regextst:2778', + 'icu-regextst:2779', 'icu-regextst:3338', 'icu-regextst:3339', 'icu-regextst:3340', 'icu-regextst:3341', 'icu-regextst:3342', 'icu-regextst:3343', + 'icu-regextst:3344', + 'icu-regextst:3345', + 'icu-regextst:3396', + 'icu-regextst:3397', + 'icu-regextst:3398', + 'icu-regextst:3399', + 'icu-regextst:3400', + 'icu-regextst:3401', + 'icu-regextst:3402', + 'icu-regextst:3403', 'icu-regextst:3518', 'icu-regextst:3519', 'icu-regextst:3520', 'icu-regextst:3521', 'icu-regextst:3522', 'icu-regextst:3523', + 'icu-regextst:3524', + 'icu-regextst:3525', + 'icu-regextst:3538', + 'icu-regextst:3539', 'icu-regextst:3540', 'icu-regextst:3541', + 'icu-regextst:3542', + 'icu-regextst:3543', + 'icu-regextst:3544', + 'icu-regextst:3545', + 'icu-regextst:3606', + 'icu-regextst:3607', + 'icu-regextst:3608', + 'icu-regextst:3609', + 'icu-regextst:3610', + 'icu-regextst:3611', 'icu-regextst:3836', 'icu-regextst:3837', + 'icu-regextst:3838', + 'icu-regextst:3839', ); // PCRE2 rejects a range with a class at one end, like "[\d-z]". PCRE 8 reads the "-" literally. @@ -664,158 +796,6 @@ protected function get_known_failures(): array { ); } - // PCRE2 rejects unknown escapes, like "\j", unless PHP reads them literally. PCRE 8 reads them literally. - if ( $is_pcre2 && ! $literal_escapes ) { - array_push( - $failures, - 'icu-regextst:725', - 'icu-regextst:726', - 'icu-regextst:727', - 'icu-regextst:728', - 'icu-regextst:1137', - 'icu-regextst:1138', - 'icu-regextst:1139', - 'icu-regextst:1140' - ); - } - - // PCRE2 rejects escapes that are invalid in a class, like "[\z]". Before PCRE2 10.33, PHP read them literally. - if ( $is_pcre2 && ! ( $literal_escapes && version_compare( $pcre_version, '10.33', '<' ) ) ) { - array_push( - $failures, - 'icu-regextst:365', - 'icu-regextst:366', - 'icu-regextst:367', - 'icu-regextst:368' - ); - } - - // These patterns fail unless PHP reads their escapes, like "\N{...}" and "\u0041", literally, which gives MySQL's results here. - if ( ! $literal_escapes ) { - array_push( - $failures, - 'mysql-server-suite:12327', - 'mysql-server-suite:12328', - 'mysql-server-suite:12329', - 'mysql-server-suite:12330', - 'mysql-server-suite:12331', - 'mysql-server-suite:12332', - 'mysql-server-suite:12339', - 'mysql-server-suite:12340', - 'mysql-server-suite:12341', - 'mysql-server-suite:12342', - 'mysql-server-suite:12343', - 'mysql-server-suite:12344', - 'mysql-server-suite:12345', - 'mysql-server-suite:12346', - 'mysql-server-suite:12351', - 'mysql-server-suite:12352', - 'mysql-server-suite:12353', - 'mysql-server-suite:12354', - 'mysql-server-suite:12355', - 'mysql-server-suite:12356', - 'mysql-server-suite:12357', - 'mysql-server-suite:12358', - 'mysql-server-suite:12359', - 'mysql-server-suite:12360', - 'mysql-server-suite:12361', - 'mysql-server-suite:12362', - 'mysql-server-suite:12363', - 'mysql-server-suite:12364', - 'mysql-server-suite:12365', - 'mysql-server-suite:12366', - 'mysql-server-suite:12367', - 'mysql-server-suite:12368', - 'mysql-server-suite:12369', - 'mysql-server-suite:12370', - 'mysql-server-suite:12371', - 'mysql-server-suite:12372', - 'mysql-server-suite:12375', - 'mysql-server-suite:12376', - 'mysql-server-suite:12458', - 'mysql-server-suite:12459', - 'mysql-server-suite:12460', - 'mysql-server-suite:12473', - 'mysql-server-suite:12474', - 'mysql-server-suite:12475', - 'mysql-server-suite:12476', - 'mysql-server-suite:12477', - 'mysql-server-suite:12478', - 'mysql-server-suite:12484', - 'mysql-server-suite:12487', - 'mysql-server-suite:12488', - 'mysql-server-suite:12492', - 'mysql-server-suite:12494', - 'mysql-server-suite:12498', - 'mysql-server-suite:12501', - 'mysql-server-suite:12502', - 'mysql-server-suite:12503', - 'mysql-server-suite:12504', - 'mysql-server-suite:12505', - 'mysql-server-suite:12506', - 'mysql-server-suite:12510', - 'icu-regextst:373', - 'icu-regextst:374', - 'icu-regextst:377', - 'icu-regextst:378', - 'icu-regextst:580', - 'icu-regextst:631', - 'icu-regextst:719', - 'icu-regextst:720', - 'icu-regextst:935', - 'icu-regextst:936', - 'icu-regextst:1065', - 'icu-regextst:1115', - 'icu-regextst:1116', - 'icu-regextst:1131', - 'icu-regextst:1132', - 'icu-regextst:1364', - 'icu-regextst:1365', - 'icu-regextst:1366', - 'icu-regextst:1367', - 'icu-regextst:1466', - 'icu-regextst:1467', - 'icu-regextst:1474', - 'icu-regextst:1476', - 'icu-regextst:1477', - 'icu-regextst:1482', - 'icu-regextst:1490', - 'icu-regextst:1491', - 'icu-regextst:2774', - 'icu-regextst:2775', - 'icu-regextst:2776', - 'icu-regextst:2777', - 'icu-regextst:2778', - 'icu-regextst:2779', - 'icu-regextst:3344', - 'icu-regextst:3345', - 'icu-regextst:3396', - 'icu-regextst:3397', - 'icu-regextst:3398', - 'icu-regextst:3399', - 'icu-regextst:3400', - 'icu-regextst:3401', - 'icu-regextst:3402', - 'icu-regextst:3403', - 'icu-regextst:3524', - 'icu-regextst:3525', - 'icu-regextst:3538', - 'icu-regextst:3539', - 'icu-regextst:3542', - 'icu-regextst:3543', - 'icu-regextst:3544', - 'icu-regextst:3545', - 'icu-regextst:3606', - 'icu-regextst:3607', - 'icu-regextst:3608', - 'icu-regextst:3609', - 'icu-regextst:3610', - 'icu-regextst:3611', - 'icu-regextst:3838', - 'icu-regextst:3839' - ); - } - // PCRE 8 rejects this nested lookbehind as not fixed in length. if ( ! $is_pcre2 ) { array_push( @@ -842,6 +822,8 @@ protected function get_known_failures(): array { $failures, 'mysql-server-suite:12377', 'mysql-server-suite:12378', + 'mysql-server-suite:12379', + 'mysql-server-suite:12380', 'mysql-server-suite:12381', 'mysql-server-suite:12382', 'mysql-server-suite:12383', @@ -858,10 +840,16 @@ protected function get_known_failures(): array { 'mysql-server-suite:12394', 'mysql-server-suite:12395', 'mysql-server-suite:12396', + 'mysql-server-suite:12397', + 'mysql-server-suite:12398', 'mysql-server-suite:12399', 'mysql-server-suite:12400', 'mysql-server-suite:12401', 'mysql-server-suite:12402', + 'mysql-server-suite:12403', + 'mysql-server-suite:12404', + 'mysql-server-suite:12405', + 'mysql-server-suite:12406', 'mysql-server-suite:12407', 'mysql-server-suite:12408', 'mysql-server-suite:12409', @@ -884,50 +872,35 @@ protected function get_known_failures(): array { 'mysql-server-suite:12426', 'mysql-server-suite:12427', 'mysql-server-suite:12428', + 'mysql-server-suite:12429', + 'mysql-server-suite:12430', + 'mysql-server-suite:12431', + 'mysql-server-suite:12432', + 'mysql-server-suite:12433', + 'mysql-server-suite:12434', + 'mysql-server-suite:12435', + 'mysql-server-suite:12436', + 'mysql-server-suite:12437', + 'mysql-server-suite:12438', + 'mysql-server-suite:12439', + 'mysql-server-suite:12440', + 'mysql-server-suite:12441', + 'mysql-server-suite:12442', + 'mysql-server-suite:12443', + 'mysql-server-suite:12444', + 'mysql-server-suite:12445', + 'mysql-server-suite:12446', + 'mysql-server-suite:12447', + 'mysql-server-suite:12448', 'mysql-server-suite:12449', 'mysql-server-suite:12450', + 'mysql-server-suite:12451', + 'mysql-server-suite:12452', + 'mysql-server-suite:12453', + 'mysql-server-suite:12454', 'icu-regextst:1452', 'icu-regextst:1453' ); - - // PHP reads these unknown properties literally, which gives MySQL's results here. - if ( ! $literal_escapes ) { - array_push( - $failures, - 'mysql-server-suite:12379', - 'mysql-server-suite:12380', - 'mysql-server-suite:12397', - 'mysql-server-suite:12398', - 'mysql-server-suite:12403', - 'mysql-server-suite:12404', - 'mysql-server-suite:12405', - 'mysql-server-suite:12406', - 'mysql-server-suite:12429', - 'mysql-server-suite:12430', - 'mysql-server-suite:12431', - 'mysql-server-suite:12432', - 'mysql-server-suite:12433', - 'mysql-server-suite:12434', - 'mysql-server-suite:12435', - 'mysql-server-suite:12436', - 'mysql-server-suite:12437', - 'mysql-server-suite:12438', - 'mysql-server-suite:12439', - 'mysql-server-suite:12440', - 'mysql-server-suite:12441', - 'mysql-server-suite:12442', - 'mysql-server-suite:12443', - 'mysql-server-suite:12444', - 'mysql-server-suite:12445', - 'mysql-server-suite:12446', - 'mysql-server-suite:12447', - 'mysql-server-suite:12448', - 'mysql-server-suite:12451', - 'mysql-server-suite:12452', - 'mysql-server-suite:12453', - 'mysql-server-suite:12454' - ); - } } // Lookbehinds of variable length, like "(?<=bc?c?c?)", arrived in PCRE2 10.43. @@ -974,17 +947,6 @@ protected function get_known_failures(): array { ); } - // Since PCRE2 10.43, these lookbehinds of variable length compile when PHP reads their "\u" and "\U" escapes literally, which gives MySQL's results here. - if ( ! ( $literal_escapes && version_compare( $pcre_version, '10.43', '>=' ) ) ) { - array_push( - $failures, - 'icu-regextst:1472', - 'icu-regextst:1473', - 'icu-regextst:1480', - 'icu-regextst:1481' - ); - } - // Since PCRE2 10.45, a caseless \p{Lu} matches any cased letter, like ICU. if ( version_compare( $pcre_version, '10.45', '<' ) ) { array_push( From dccda037d565694166cfdedc2b5dd93a6f5eec13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jake=C5=A1?= Date: Wed, 11 Nov 2026 12:08:54 +0100 Subject: [PATCH 6/6] Retry a REGEXP match without JIT when its stack runs out PHP's JIT stack is small, so a repeated group like '(a|b)*' failed with "exceeded internal limits" on a subject of about 6,000 characters. MySQL matches such subjects, and trunk silently treated them as not matching. On PHP 7.3+, retry such a match with "(*NO_JIT)", which raises the limit to about 50,000 characters. PHP 7.2 has no "(*NO_JIT)", and matching without JIT can crash it, so it keeps the JIT limit of about 2,000 characters. Document the remaining limit. --- ...s-wp-sqlite-pdo-user-defined-functions.php | 41 +++++++++++++----- .../tests/WP_MySQL_On_SQLite_Tests.php | 43 +++++++++++++++++++ 2 files changed, 74 insertions(+), 10 deletions(-) diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php index 40308b86a..59ce53e1e 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php @@ -607,11 +607,7 @@ public function regexp( $pattern, $field, $binary = 0 ) { */ $byte_mode = 1 === $binary || ! $this->regexp_is_utf8( $pattern ) || ! $this->regexp_is_utf8( $field ); $compiled = $this->regexp_compile( $pattern, $byte_mode ? 'c' : '', $byte_mode ); - $result = preg_match( $compiled, $field ); - if ( false === $result ) { - $this->regexp_fail( $pattern ); - } - return $result; + return $this->regexp_match( $compiled, $pattern, $field ); } /** @@ -635,11 +631,7 @@ public function regexp_like( $expr, $pattern, $match_type = '' ) { // and case-sensitively, unless the match type says otherwise. $byte_mode = ! $this->regexp_is_utf8( $pattern ) || ! $this->regexp_is_utf8( $expr ); $compiled = $this->regexp_compile( $pattern, $byte_mode ? 'c' . $match_type : $match_type, $byte_mode ); - $result = preg_match( $compiled, $expr ); - if ( false === $result ) { - $this->regexp_fail( $pattern ); - } - return $result; + return $this->regexp_match( $compiled, $pattern, $expr ); } /** @@ -1061,6 +1053,8 @@ private function regexp_check_match_type( $function_name, $match_type ) { * sensitivity can differ between rows. * - SQLite converts numbers to text in its own format, so `20e0` reads "20.0" * (MySQL: "20"), and `0.000001` reads "1.0e-06". + * - A repeated group, like `(a|b)*`, can exceed PCRE's limits on a subject of + * about 50,000 characters, or 2,000 on PHP 7.2. * * @param string $pattern The MySQL regex pattern. * @param string $match_type MySQL match_type flag string. @@ -1214,6 +1208,33 @@ private function regexp_is_utf8( $value ) { return 1 === preg_match( '//u', $value ); } + /** + * Run preg_match(), and report a failure as a MySQL error. + * + * @param string $compiled PCRE-wrapped pattern. + * @param string $pattern The original MySQL regex pattern, for error messages. + * @param string $subject The subject string. + * + * @throws Exception On a preg failure. + * @return int 1 on match, 0 on no match. + */ + private function regexp_match( $compiled, $pattern, $subject ) { + $result = preg_match( $compiled, $subject ); + + /* + * PHP's JIT stack is small, so a repeated group, such as "(a|b)*", can fail on + * a subject of a few thousand characters. Matching without JIT goes further. + * PHP 7.2 has no "(*NO_JIT)", and matching without JIT can crash it. + */ + if ( false === $result && PREG_JIT_STACKLIMIT_ERROR === preg_last_error() && PHP_VERSION_ID >= 70300 ) { + $result = preg_match( $compiled[0] . '(*NO_JIT)' . substr( $compiled, 1 ), $subject ); + } + if ( false === $result ) { + $this->regexp_fail( $pattern ); + } + return $result; + } + /** * Translate a preg_* failure into a caller-friendly exception message. * diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php index 340331074..5b98e3408 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php @@ -411,6 +411,49 @@ public function testRegexpLimitOnlyOnEmptySubject() { ); } + public function testRegexpJitStackLimit() { + // A repeated group exhausts PHP's JIT stack after a few thousand characters. + $subject = str_repeat( 'a', 20000 ); + if ( PHP_VERSION_ID < 70300 ) { + // PHP 7.2 can't retry without JIT. With JIT off, its PCRE 8 recurses on + // the C stack, and the default limit of 100,000 overflows the stack. + $this->iniSet( 'pcre.recursion_limit', '1000' ); + $this->assertQueryError( + "SELECT REGEXP_LIKE('$subject', '^(a|b)*\$')", + 'Regular expression evaluation exceeded internal limits.' + ); + return; + } + $this->assertQuery( + "SELECT + '$subject' REGEXP '^(a|b)*\$' AS operator_result, + REGEXP_LIKE('$subject', '^(a|b)*\$') AS like_result" + ); + $this->assertEquals( + array( + (object) array( + 'operator_result' => '1', + 'like_result' => '1', + ), + ), + $this->last_result + ); + } + + public function testRegexpLimitWithoutJit() { + // Matching without JIT has a limit, too. + if ( PHP_VERSION_ID < 70300 ) { + // With JIT off, PHP 7.2's PCRE 8 recurses on the C stack, and the + // default limit of 100,000 overflows the stack. + $this->iniSet( 'pcre.recursion_limit', '1000' ); + } + $subject = str_repeat( 'a', 200000 ); + $this->assertQueryError( + "SELECT REGEXP_LIKE('$subject', '^(a|b)*\$')", + 'Regular expression evaluation exceeded internal limits.' + ); + } + public function testRegexpLegacyOperatorRegression() { // The legacy REGEXP operator must keep working alongside REGEXP_LIKE. $this->assertQuery( "SELECT 'abc' REGEXP 'ABC' AS r" );