Skip to content

Commit b2e5bff

Browse files
committed
Align REGEXP operators with REGEXP functions
Use the shared regex compiler, coercion, and error handling for REGEXP and RLIKE while preserving byte-oriented REGEXP BINARY behavior. Cover NULL, invalid patterns, delimiters, numeric coercion, and UTF-8 handling.
1 parent ef5c21e commit b2e5bff

3 files changed

Lines changed: 144 additions & 42 deletions

File tree

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4431,12 +4431,17 @@ private function translate( $node ): ?string {
44314431
return $this->translate_bit_expr( $node );
44324432
case 'simpleExprBody':
44334433
return $this->translate_simple_expr_body( $node );
4434+
case 'predicate':
4435+
$operations = $node->get_first_child_node( 'predicateOperations' );
4436+
$token = null === $operations ? null : $operations->get_first_child_token();
4437+
if ( null !== $token && WP_MySQL_Lexer::REGEXP_SYMBOL === $token->id ) {
4438+
return $this->translate_regexp_predicate( $node, $operations );
4439+
}
4440+
return $this->translate_sequence( $node->get_children() );
44344441
case 'predicateOperations':
44354442
$token = $node->get_first_child_token();
44364443
if ( WP_MySQL_Lexer::LIKE_SYMBOL === $token->id ) {
44374444
return $this->translate_like( $node );
4438-
} elseif ( WP_MySQL_Lexer::REGEXP_SYMBOL === $token->id ) {
4439-
return $this->translate_regexp_functions( $node );
44404445
}
44414446
return $this->translate_sequence( $node->get_children() );
44424447
case 'runtimeFunctionCall':
@@ -5154,13 +5159,16 @@ private function translate_like( WP_Parser_Node $node ): string {
51545159
/**
51555160
* Translate MySQL REGEXP expression to SQLite.
51565161
*
5157-
* @param WP_Parser_Node $node The "predicateOperations" AST node.
5162+
* @param WP_Parser_Node $node The "predicate" AST node.
5163+
* @param WP_Parser_Node $operations The "predicateOperations" AST node.
51585164
* @return string The translated value.
51595165
* @throws WP_MySQL_On_SQLite_Exception When the translation fails.
51605166
*/
5161-
private function translate_regexp_functions( WP_Parser_Node $node ): string {
5162-
$tokens = $node->get_descendant_tokens();
5167+
private function translate_regexp_predicate( WP_Parser_Node $node, WP_Parser_Node $operations ): string {
5168+
$tokens = $operations->get_descendant_tokens();
51635169
$is_binary = isset( $tokens[1] ) && WP_MySQL_Lexer::BINARY_SYMBOL === $tokens[1]->id;
5170+
$subject = $this->translate( $node->get_first_child_node() );
5171+
$pattern = $this->translate( $operations->get_first_child_node() );
51645172

51655173
/*
51665174
* If the query says REGEXP BINARY, the comparison is byte-by-byte
@@ -5176,9 +5184,11 @@ private function translate_regexp_functions( WP_Parser_Node $node ): string {
51765184
* regular expressions anyway.
51775185
*/
51785186
if ( true === $is_binary ) {
5179-
return 'REGEXP CHAR(0) || ' . $this->translate( $node->get_first_child_node() );
5187+
$pattern = 'CHAR(0) || ' . $pattern;
51805188
}
5181-
return 'REGEXP ' . $this->translate( $node->get_first_child_node() );
5189+
5190+
$operator = null === $node->get_first_child_node( 'notRule' ) ? 'REGEXP' : 'NOT REGEXP';
5191+
return "$subject $operator $pattern";
51825192
}
51835193

51845194
/**

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

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,8 @@ public static function register_for( $pdo ): self {
107107
'regexp_instr' => array( 2, 3, 4, 5, 6 ),
108108
);
109109

110-
/** @var string|null Last validated regex pattern. */
111-
private $regexp_cached_pattern = null;
112-
113-
/** @var string|null Match type used by the cached regex. */
114-
private $regexp_cached_match_type = null;
110+
/** @var array|null Pattern, match type, and byte mode of the last compiled regex. */
111+
private $regexp_cached_key = null;
115112

116113
/** @var string|null Last compiled PCRE pattern. */
117114
private $regexp_cached_compiled = null;
@@ -597,12 +594,18 @@ public function isnull( $field ) {
597594
/**
598595
* Method to emulate MySQL REGEXP() function.
599596
*
600-
* @param string $pattern Regular expression to match.
601-
* @param string $field Haystack.
597+
* @param string|null $pattern Regular expression to match.
598+
* @param string|null $field Haystack.
602599
*
603-
* @return integer 1 if matched, 0 if not matched.
600+
* @throws Exception If the pattern is not a valid regular expression.
601+
* @return int|null 1 if matched, 0 if not matched, or NULL for NULL input.
604602
*/
605603
public function regexp( $pattern, $field ) {
604+
if ( null === $pattern || null === $field ) {
605+
return null;
606+
}
607+
$pattern = (string) $pattern;
608+
606609
/*
607610
* If the original query says REGEXP BINARY
608611
* the comparison is byte-by-byte and letter casing now
@@ -617,17 +620,22 @@ public function regexp( $pattern, $field ) {
617620
* be reasonably safe since PHP does not allow null bytes in
618621
* regular expressions anyway.
619622
*/
620-
if ( "\x00" === $pattern[0] ) {
623+
$is_binary = '' !== $pattern && "\x00" === $pattern[0];
624+
if ( $is_binary ) {
621625
$pattern = substr( $pattern, 1 );
622-
$flags = '';
623-
} else {
624-
// Otherwise, the search is case-insensitive.
625-
$flags = 'i';
626626
}
627-
$pattern = str_replace( '/', '\/', $pattern );
628-
$pattern = '/' . $pattern . '/' . $flags;
629627

630-
return preg_match( $pattern, $field );
628+
/*
629+
* Data that is not valid UTF-8 can only be a binary string in MySQL,
630+
* and MySQL matches binary strings byte by byte and case-sensitively.
631+
*/
632+
$byte_mode = $is_binary || ! $this->regexp_is_utf8( $pattern ) || ! $this->regexp_is_utf8( $field );
633+
$compiled = $this->regexp_compile( $pattern, $byte_mode ? 'c' : '', $byte_mode );
634+
$result = preg_match( $compiled, $field );
635+
if ( false === $result ) {
636+
$this->regexp_fail( $pattern );
637+
}
638+
return $result;
631639
}
632640

633641
/**
@@ -1206,9 +1214,9 @@ public function _helper_like_to_glob_pattern( $pattern ) {
12061214
/**
12071215
* Compile a MySQL-style regex into a PCRE pattern string.
12081216
*
1209-
* Translates MySQL match_type flags (c/i/m/n/u) to PCRE modifiers and always
1210-
* appends the u (UTF-8) modifier. Case-insensitive is the default, matching
1211-
* the existing REGEXP operator.
1217+
* Translates MySQL match_type flags (c/i/m/n/u) to PCRE modifiers. The u
1218+
* (UTF-8) modifier is applied unless byte-oriented matching is requested.
1219+
* Case-insensitive is the default.
12121220
*
12131221
* MySQL's native engine is ICU; we use PHP's PCRE. The two diverge in a
12141222
* few corners:
@@ -1225,29 +1233,27 @@ public function _helper_like_to_glob_pattern( $pattern ) {
12251233
* `utf8mb4_0900_ai_ci` collation; callers that rely on a `_bin` or
12261234
* `_cs` collation must pass an explicit `c` match_type because this
12271235
* helper has no access to the session collation.
1228-
* - The `u` (UTF-8) PCRE modifier is always applied. Binary data with
1229-
* invalid UTF-8 bytes that matches fine under the legacy `REGEXP`
1230-
* operator raises "Invalid UTF-8 data in regular expression input."
1231-
* when routed through REGEXP_LIKE / _REPLACE / _SUBSTR / _INSTR.
1236+
* - The `u` (UTF-8) PCRE modifier is applied to REGEXP_* functions, so
1237+
* they reject data that is not valid UTF-8 from the start position on.
1238+
* The REGEXP operators match such data, and REGEXP BINARY, byte by byte,
1239+
* like MySQL binary strings.
12321240
*
1233-
* @param string $pattern The MySQL regex pattern.
1234-
* @param string $match_type MySQL match_type flag string.
1241+
* @param string $pattern The MySQL regex pattern.
1242+
* @param string $match_type MySQL match_type flag string.
1243+
* @param bool $binary_mode Whether to match bytes instead of UTF-8 characters.
12351244
*
12361245
* @throws Exception If the pattern is empty or the match_type string
12371246
* contains an unrecognized flag.
12381247
* @return string PCRE-ready pattern with delimiter and modifiers.
12391248
*/
1240-
private function regexp_compile( $pattern, $match_type ) {
1249+
private function regexp_compile( $pattern, $match_type, $binary_mode = false ) {
12411250
$match_type = (string) $match_type;
12421251
$pattern = (string) $pattern;
12431252
if ( '' === $pattern ) {
12441253
throw new Exception( 'Illegal argument to a regular expression.' );
12451254
}
1246-
if (
1247-
null !== $pattern
1248-
&& $pattern === $this->regexp_cached_pattern
1249-
&& $match_type === $this->regexp_cached_match_type
1250-
) {
1255+
$cache_key = array( $pattern, $match_type, $binary_mode );
1256+
if ( $cache_key === $this->regexp_cached_key ) {
12511257
return $this->regexp_cached_compiled;
12521258
}
12531259

@@ -1273,7 +1279,7 @@ private function regexp_compile( $pattern, $match_type ) {
12731279
}
12741280
}
12751281

1276-
$modifiers = 'u';
1282+
$modifiers = $binary_mode ? '' : 'u';
12771283
if ( ! $case_sensitive ) {
12781284
$modifiers .= 'i';
12791285
}
@@ -1300,9 +1306,8 @@ private function regexp_compile( $pattern, $match_type ) {
13001306
if ( false === $valid ) {
13011307
$this->regexp_fail( $pattern );
13021308
}
1303-
$this->regexp_cached_pattern = $pattern;
1304-
$this->regexp_cached_match_type = $match_type;
1305-
$this->regexp_cached_compiled = $compiled;
1309+
$this->regexp_cached_key = $cache_key;
1310+
$this->regexp_cached_compiled = $compiled;
13061311
return $compiled;
13071312
}
13081313

@@ -1344,6 +1349,17 @@ private function regexp_escape_delimiter( $pattern ) {
13441349
return $escaped;
13451350
}
13461351

1352+
/**
1353+
* Check whether a string is valid UTF-8.
1354+
*
1355+
* @param string $value The string.
1356+
*
1357+
* @return bool Whether the string is valid UTF-8.
1358+
*/
1359+
private function regexp_is_utf8( $value ) {
1360+
return 1 === preg_match( '//u', $value );
1361+
}
1362+
13471363
/**
13481364
* Convert a 1-based character position into a byte offset into the UTF-8 string.
13491365
*

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,82 @@ public function testRegexpLegacyOperatorRegression() {
430430
$this->assertSame( '0', $this->last_result[0]->r );
431431
}
432432

433+
public function testRegexpLegacyOperatorNullPropagation() {
434+
$this->assertQuery( "SELECT NULL REGEXP 'a' AS r" );
435+
$this->assertNull( $this->last_result[0]->r );
436+
$this->assertQuery( "SELECT 'a' RLIKE NULL AS r" );
437+
$this->assertNull( $this->last_result[0]->r );
438+
}
439+
440+
public function testRegexpLegacyOperatorInvalidPattern() {
441+
$this->assertQueryError(
442+
"SELECT 'abc' REGEXP '(abc'",
443+
'Invalid regular expression: (abc.'
444+
);
445+
$this->assertQueryError(
446+
"SELECT 'abc' RLIKE ''",
447+
'Illegal argument to a regular expression.'
448+
);
449+
}
450+
451+
public function testRegexpLegacyOperatorEscapedDelimiter() {
452+
$this->assertQuery( "SELECT '/' REGEXP '\\\\/' AS r" );
453+
$this->assertSame( '1', $this->last_result[0]->r );
454+
$this->assertQuery( "SELECT '/' REGEXP '\\\\Q/\\\\E' AS r" );
455+
$this->assertSame( '1', $this->last_result[0]->r );
456+
}
457+
458+
public function testRegexpLegacyOperatorNumericOperands() {
459+
$this->assertQuery( "SELECT 123 REGEXP '^12' AS r" );
460+
$this->assertSame( '1', $this->last_result[0]->r );
461+
$this->assertQuery( "SELECT 'x123' NOT REGEXP 123 AS r" );
462+
$this->assertSame( '0', $this->last_result[0]->r );
463+
}
464+
465+
public function testRegexpLegacyOperatorUtf8Handling() {
466+
$this->assertQuery( "SELECT 'é' REGEXP '^.\$' AS r" );
467+
$this->assertSame( '1', $this->last_result[0]->r );
468+
$this->assertQuery( "SELECT CAST(X'FF' AS CHAR) REGEXP BINARY CAST(X'FF' AS CHAR) AS r" );
469+
$this->assertSame( '1', $this->last_result[0]->r );
470+
}
471+
472+
public function testRegexpLegacyOperatorMatchesBinaryDataByBytes() {
473+
// Non-UTF-8 data is matched like MySQL binary strings: byte by byte and case-sensitively.
474+
$this->assertQuery( 'CREATE TABLE blobs (id INT PRIMARY KEY, b BLOB)' );
475+
$this->assertQuery( "INSERT INTO blobs VALUES (1, X'61FF62'), (2, NULL)" );
476+
$this->assertQuery(
477+
"SELECT
478+
b REGEXP 'a.b' AS any_byte,
479+
b REGEXP '^a.b\$' AS anchored,
480+
b REGEXP 'A' AS other_case,
481+
b NOT REGEXP 'zzz' AS negated,
482+
b REGEXP X'FF62' AS binary_pattern,
483+
b REGEXP X'FE' AS other_byte
484+
FROM blobs ORDER BY id"
485+
);
486+
$this->assertEquals(
487+
array(
488+
(object) array(
489+
'any_byte' => '1',
490+
'anchored' => '1',
491+
'other_case' => '0',
492+
'negated' => '1',
493+
'binary_pattern' => '1',
494+
'other_byte' => '0',
495+
),
496+
(object) array(
497+
'any_byte' => null,
498+
'anchored' => null,
499+
'other_case' => null,
500+
'negated' => null,
501+
'binary_pattern' => null,
502+
'other_byte' => null,
503+
),
504+
),
505+
$this->last_result
506+
);
507+
}
508+
433509
public function testRegexpLikeEscapedDelimiter() {
434510
$this->assertQuery( "SELECT REGEXP_LIKE('/', '\\\\/') AS r" );
435511
$this->assertSame( '1', $this->last_result[0]->r );

0 commit comments

Comments
 (0)