Skip to content

Commit 7a7f948

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. Preserve numeric literal text for operator operands and cover NULL, invalid patterns, delimiters, numeric coercion, and UTF-8 handling.
1 parent 36d6b56 commit 7a7f948

3 files changed

Lines changed: 116 additions & 34 deletions

File tree

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

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3837,12 +3837,17 @@ private function translate( $node ): ?string {
38373837
return $this->translate_sequence( $node->get_children() );
38383838
case 'simpleExprBody':
38393839
return $this->translate_simple_expr_body( $node );
3840+
case 'predicate':
3841+
$operations = $node->get_first_child_node( 'predicateOperations' );
3842+
$token = null === $operations ? null : $operations->get_first_child_token();
3843+
if ( null !== $token && WP_MySQL_Lexer::REGEXP_SYMBOL === $token->id ) {
3844+
return $this->translate_regexp_predicate( $node, $operations );
3845+
}
3846+
return $this->translate_sequence( $node->get_children() );
38403847
case 'predicateOperations':
38413848
$token = $node->get_first_child_token();
38423849
if ( WP_MySQL_Lexer::LIKE_SYMBOL === $token->id ) {
38433850
return $this->translate_like( $node );
3844-
} elseif ( WP_MySQL_Lexer::REGEXP_SYMBOL === $token->id ) {
3845-
return $this->translate_regexp_functions( $node );
38463851
}
38473852
return $this->translate_sequence( $node->get_children() );
38483853
case 'runtimeFunctionCall':
@@ -4478,13 +4483,16 @@ private function translate_like( WP_Parser_Node $node ): string {
44784483
/**
44794484
* Translate MySQL REGEXP expression to SQLite.
44804485
*
4481-
* @param WP_Parser_Node $node The "predicateOperations" AST node.
4486+
* @param WP_Parser_Node $node The "predicate" AST node.
4487+
* @param WP_Parser_Node $operations The "predicateOperations" AST node.
44824488
* @return string The translated value.
44834489
* @throws WP_SQLite_Driver_Exception When the translation fails.
44844490
*/
4485-
private function translate_regexp_functions( WP_Parser_Node $node ): string {
4486-
$tokens = $node->get_descendant_tokens();
4491+
private function translate_regexp_predicate( WP_Parser_Node $node, WP_Parser_Node $operations ): string {
4492+
$tokens = $operations->get_descendant_tokens();
44874493
$is_binary = isset( $tokens[1] ) && WP_MySQL_Lexer::BINARY_SYMBOL === $tokens[1]->id;
4494+
$subject = $this->translate_regexp_string_arg( $node->get_first_child_node() );
4495+
$pattern = $this->translate_regexp_string_arg( $operations->get_first_child_node() );
44884496

44894497
/*
44904498
* If the query says REGEXP BINARY, the comparison is byte-by-byte
@@ -4500,9 +4508,11 @@ private function translate_regexp_functions( WP_Parser_Node $node ): string {
45004508
* regular expressions anyway.
45014509
*/
45024510
if ( true === $is_binary ) {
4503-
return 'REGEXP CHAR(0) || ' . $this->translate( $node->get_first_child_node() );
4511+
$pattern = 'CHAR(0) || ' . $pattern;
45044512
}
4505-
return 'REGEXP ' . $this->translate( $node->get_first_child_node() );
4513+
4514+
$operator = null === $node->get_first_child_node( 'notRule' ) ? 'REGEXP' : 'NOT REGEXP';
4515+
return "$subject $operator $pattern";
45064516
}
45074517

45084518
/**
@@ -4699,17 +4709,29 @@ private function translate_regexp_function_args( string $name, array $arg_nodes
46994709

47004710
$args = array();
47014711
foreach ( $arg_nodes as $index => $arg_node ) {
4702-
$numeric_literal = null;
47034712
if ( in_array( $index, $string_arg_indexes[ $name ], true ) ) {
4704-
$numeric_literal = $this->get_regexp_numeric_literal_string( $arg_node );
4713+
$args[] = $this->translate_regexp_string_arg( $arg_node );
4714+
} else {
4715+
$args[] = $this->translate( $arg_node );
47054716
}
4706-
$args[] = null === $numeric_literal
4707-
? $this->translate( $arg_node )
4708-
: $this->quote_sqlite_value( $numeric_literal );
47094717
}
47104718
return $args;
47114719
}
47124720

4721+
/**
4722+
* Translate a REGEXP string-domain argument while preserving numeric literal text.
4723+
*
4724+
* @param WP_Parser_Node $node Function argument or operator operand.
4725+
*
4726+
* @return string Translated argument.
4727+
*/
4728+
private function translate_regexp_string_arg( WP_Parser_Node $node ): string {
4729+
$numeric_literal = $this->get_regexp_numeric_literal_string( $node );
4730+
return null === $numeric_literal
4731+
? $this->translate( $node )
4732+
: $this->quote_sqlite_value( $numeric_literal );
4733+
}
4734+
47134735
/**
47144736
* Read an exact decimal or floating-point literal from an expression node.
47154737
*
@@ -4725,6 +4747,9 @@ private function get_regexp_numeric_literal_string( WP_Parser_Node $node ): ?str
47254747
if ( '(' === $value || ')' === $value ) {
47264748
continue;
47274749
}
4750+
if ( WP_MySQL_Lexer::BINARY_SYMBOL === $token->id ) {
4751+
continue;
4752+
}
47284753
if ( ( '+' === $value || '-' === $value ) && null === $number && '' === $sign ) {
47294754
$sign = '-' === $value ? '-' : '';
47304755
continue;

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

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public static function register_for( $pdo ): self {
123123
/** @var string|null Match type used by the cached regex. */
124124
private $regexp_cached_match_type = null;
125125

126+
/** @var bool|null Whether the cached regex uses byte-oriented matching. */
127+
private $regexp_cached_binary = null;
128+
126129
/** @var string|null Last compiled PCRE pattern. */
127130
private $regexp_cached_compiled = null;
128131

@@ -622,12 +625,20 @@ public function _if( $expression, $truthy, $falsy ) {
622625
/**
623626
* Method to emulate MySQL REGEXP() function.
624627
*
625-
* @param string $pattern Regular expression to match.
626-
* @param string $field Haystack.
628+
* @param string|null $pattern Regular expression to match.
629+
* @param string|null $field Haystack.
627630
*
628-
* @return integer 1 if matched, 0 if not matched.
631+
* @throws Exception If the pattern is not a valid regular expression.
632+
* @return int|null 1 if matched, 0 if not matched, or NULL for NULL input.
629633
*/
630634
public function regexp( $pattern, $field ) {
635+
if ( null === $pattern || null === $field ) {
636+
return null;
637+
}
638+
639+
$pattern = $this->regexp_string_arg( $pattern );
640+
$field = $this->regexp_string_arg( $field );
641+
631642
/*
632643
* If the original query says REGEXP BINARY
633644
* the comparison is byte-by-byte and letter casing now
@@ -642,17 +653,21 @@ public function regexp( $pattern, $field ) {
642653
* be reasonably safe since PHP does not allow null bytes in
643654
* regular expressions anyway.
644655
*/
645-
if ( "\x00" === $pattern[0] ) {
656+
$is_binary = '' !== $pattern && "\x00" === $pattern[0];
657+
if ( $is_binary ) {
646658
$pattern = substr( $pattern, 1 );
647-
$flags = '';
648-
} else {
649-
// Otherwise, the search is case-insensitive.
650-
$flags = 'i';
651659
}
652-
$pattern = str_replace( '/', '\/', $pattern );
653-
$pattern = '/' . $pattern . '/' . $flags;
654660

655-
return preg_match( $pattern, $field );
661+
$compiled = $this->regexp_compile( $pattern, $is_binary ? 'c' : '', $is_binary );
662+
$result = $this->regexp_run(
663+
function () use ( $compiled, $field ) {
664+
return preg_match( $compiled, $field );
665+
}
666+
);
667+
if ( false === $result ) {
668+
$this->regexp_fail( $pattern );
669+
}
670+
return $result;
656671
}
657672

658673
/**
@@ -1271,9 +1286,9 @@ public function _helper_like_to_glob_pattern( $pattern ) {
12711286
/**
12721287
* Compile a MySQL-style regex into a PCRE pattern string.
12731288
*
1274-
* Translates MySQL match_type flags (c/i/m/n/u) to PCRE modifiers and always
1275-
* appends the u (UTF-8) modifier. Case-insensitive is the default, matching
1276-
* the existing REGEXP operator.
1289+
* Translates MySQL match_type flags (c/i/m/n/u) to PCRE modifiers. The u
1290+
* (UTF-8) modifier is applied unless byte-oriented matching is requested.
1291+
* Case-insensitive is the default.
12771292
*
12781293
* MySQL's native engine is ICU; we use PHP's PCRE. The two diverge in a
12791294
* few corners:
@@ -1290,20 +1305,19 @@ public function _helper_like_to_glob_pattern( $pattern ) {
12901305
* `utf8mb4_0900_ai_ci` collation; callers that rely on a `_bin` or
12911306
* `_cs` collation must pass an explicit `c` match_type because this
12921307
* helper has no access to the session collation.
1293-
* - The `u` (UTF-8) PCRE modifier is always applied. Binary data with
1294-
* invalid UTF-8 bytes that matches fine under the legacy `REGEXP`
1295-
* operator raises "Invalid UTF-8 data in regular expression input."
1296-
* when routed through REGEXP_LIKE / _REPLACE / _SUBSTR / _INSTR.
1308+
* - The `u` (UTF-8) PCRE modifier is applied to REGEXP_* functions and
1309+
* nonbinary REGEXP operators. REGEXP BINARY remains byte-oriented.
12971310
*
1298-
* @param string|null $pattern The MySQL regex pattern.
1299-
* @param string $match_type MySQL match_type flag string.
1311+
* @param string|null $pattern The MySQL regex pattern.
1312+
* @param string $match_type MySQL match_type flag string.
1313+
* @param bool $binary_mode Whether to match bytes instead of UTF-8 characters.
13001314
*
13011315
* @throws Exception If the pattern is empty or the match_type string
13021316
* contains an unrecognized flag.
13031317
* @return string|null PCRE-ready pattern with delimiter and modifiers, or
13041318
* NULL when the pattern is NULL.
13051319
*/
1306-
private function regexp_compile( $pattern, $match_type ) {
1320+
private function regexp_compile( $pattern, $match_type, $binary_mode = false ) {
13071321
$match_type = $this->regexp_string_arg( $match_type );
13081322
if ( null !== $pattern ) {
13091323
$pattern = $this->regexp_string_arg( $pattern );
@@ -1315,6 +1329,7 @@ private function regexp_compile( $pattern, $match_type ) {
13151329
null !== $pattern
13161330
&& $pattern === $this->regexp_cached_pattern
13171331
&& $match_type === $this->regexp_cached_match_type
1332+
&& $binary_mode === $this->regexp_cached_binary
13181333
) {
13191334
return $this->regexp_cached_compiled;
13201335
}
@@ -1341,7 +1356,7 @@ private function regexp_compile( $pattern, $match_type ) {
13411356
}
13421357
}
13431358

1344-
$modifiers = 'u';
1359+
$modifiers = $binary_mode ? '' : 'u';
13451360
if ( ! $case_sensitive ) {
13461361
$modifiers .= 'i';
13471362
}
@@ -1367,6 +1382,7 @@ function () use ( $compiled ) {
13671382
}
13681383
$this->regexp_cached_pattern = $pattern;
13691384
$this->regexp_cached_match_type = $match_type;
1385+
$this->regexp_cached_binary = $binary_mode;
13701386
$this->regexp_cached_compiled = $compiled;
13711387
return $compiled;
13721388
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,47 @@ public function testRegexpLegacyOperatorRegression() {
287287
$this->assertSame( '0', $this->engine->get_query_results()[0]->r );
288288
}
289289

290+
public function testRegexpLegacyOperatorNullPropagation() {
291+
$this->assertQuery( "SELECT NULL REGEXP 'a' AS r" );
292+
$this->assertNull( $this->engine->get_query_results()[0]->r );
293+
$this->assertQuery( "SELECT 'a' RLIKE NULL AS r" );
294+
$this->assertNull( $this->engine->get_query_results()[0]->r );
295+
}
296+
297+
public function testRegexpLegacyOperatorInvalidPattern() {
298+
$this->assertQueryError(
299+
"SELECT 'abc' REGEXP '(abc'",
300+
'Invalid regular expression: (abc.'
301+
);
302+
$this->assertQueryError(
303+
"SELECT 'abc' RLIKE ''",
304+
'Illegal argument to a regular expression.'
305+
);
306+
}
307+
308+
public function testRegexpLegacyOperatorEscapedDelimiter() {
309+
$this->assertQuery( "SELECT '/' REGEXP '\\\\/' AS r" );
310+
$this->assertSame( '1', $this->engine->get_query_results()[0]->r );
311+
$this->assertQuery( "SELECT '/' REGEXP '\\\\Q/\\\\E' AS r" );
312+
$this->assertSame( '1', $this->engine->get_query_results()[0]->r );
313+
}
314+
315+
public function testRegexpLegacyOperatorNumericOperands() {
316+
$this->assertQuery( "SELECT 1.2300 REGEXP '00\$' AS r" );
317+
$this->assertSame( '1', $this->engine->get_query_results()[0]->r );
318+
$this->assertQuery( 'SELECT 1.23 NOT REGEXP 1.2300 AS r' );
319+
$this->assertSame( '1', $this->engine->get_query_results()[0]->r );
320+
}
321+
322+
public function testRegexpLegacyOperatorUtf8Handling() {
323+
$this->assertQueryError(
324+
"SELECT CAST(X'FF' AS CHAR) REGEXP 'a'",
325+
'Invalid UTF-8 data in regular expression input.'
326+
);
327+
$this->assertQuery( "SELECT CAST(X'FF' AS CHAR) REGEXP BINARY CAST(X'FF' AS CHAR) AS r" );
328+
$this->assertSame( '1', $this->engine->get_query_results()[0]->r );
329+
}
330+
290331
public function testRegexpLikeEscapedDelimiter() {
291332
$this->assertQuery( "SELECT REGEXP_LIKE('/', '\\\\/') AS r" );
292333
$this->assertSame( '1', $this->engine->get_query_results()[0]->r );

0 commit comments

Comments
 (0)