Skip to content

Commit a7dc477

Browse files
authored
Improve MySQL compatibility of REGEXP and REGEXP_LIKE() (#624)
## Summary This PR makes the `REGEXP` and `RLIKE` operators and `REGEXP_LIKE()` behave much more like MySQL. It brings the failures in the `REGEXP_LIKE()` test suite from #622 down **from 1,253 to 766** (MySQL cases: 302 → 262, ICU cases: 951 → 504, on PHP 8.5). It's the first PRs that split #368 into smaller chunks. The main changes: - **Unicode text** is matched by character, so `.` matches `é`, and case-insensitive matching works beyond ASCII. - **Binary data** that isn't valid UTF-8 is matched byte by byte, like in MySQL. - **Errors** are raised for invalid patterns, instead of PHP warnings or silently wrong results. - **Line breaks, escaped slashes, big integers, and operator precedence** work like in MySQL. - **Long subjects** no longer fail on PHP's small JIT stack. ## Why Apps use `REGEXP` with Unicode text, binary data, and big numbers, and the old matching often got these wrong. The functions in the next PR also need this engine. ## Upgrade note A `CHECK` constraint or `DEFAULT` expression with `REGEXP BINARY`, created before this change, can reject valid rows until its table is rebuilt, for example by adding a column. This should be rare, because MySQL rejects `REGEXP BINARY` on text. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Improved compatibility for `REGEXP`, `RLIKE`, and `REGEXP_LIKE` queries, including pattern flags, escaped characters, numeric values, and Unicode or binary data. * Corrected handling of `NULL` values and invalid patterns or match types. * Improved behavior for complex patterns that reach matching limits. * Corrected operator precedence and matching behavior for patterns containing null bytes, newlines, or large numeric values. * Improved consistency when matching patterns with multiple flags or unusual character encodings. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent deafe5d commit a7dc477

4 files changed

Lines changed: 986 additions & 789 deletions

File tree

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

Lines changed: 27 additions & 18 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,31 +5159,28 @@ 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() );
5172+
$not = null === $node->get_first_child_node( 'notRule' ) ? '' : 'NOT ';
51645173

51655174
/*
5166-
* If the query says REGEXP BINARY, the comparison is byte-by-byte
5167-
* and letter casing matters – lowercase and uppercase letters are
5168-
* represented using different byte codes.
5175+
* Call the REGEXP function directly, so that REGEXP BINARY can pass
5176+
* a flag for byte-by-byte, case-sensitive matching.
51695177
*
5170-
* The REGEXP function can't be easily made to accept two
5171-
* parameters, so we'll have to use a hack to get around this.
5178+
* The parentheses keep the MySQL precedence. In SQLite, REGEXP binds
5179+
* as loosely as "=", so "1 = 'a' REGEXP 'a'" would be "(1 = 'a') REGEXP 'a'".
51725180
*
5173-
* If the first character of the pattern is a null byte, we'll
5174-
* remove it and make the comparison case-sensitive. This should
5175-
* be reasonably safe since PHP does not allow null bytes in
5176-
* regular expressions anyway.
5181+
* PDO narrows integer UDF arguments to 32 bits, so pass the operands as text.
51775182
*/
5178-
if ( true === $is_binary ) {
5179-
return 'REGEXP CHAR(0) || ' . $this->translate( $node->get_first_child_node() );
5180-
}
5181-
return 'REGEXP ' . $this->translate( $node->get_first_child_node() );
5183+
return sprintf( '(%sREGEXP(CAST(%s AS TEXT), CAST(%s AS TEXT)%s))', $not, $pattern, $subject, $is_binary ? ', 1' : '' );
51825184
}
51835185

51845186
/**
@@ -5393,6 +5395,13 @@ private function translate_function_call( WP_Parser_Node $node ): string {
53935395
}
53945396
case 'VERSION':
53955397
return $this->quote_sqlite_value( $this->get_mysql_server_version_string() );
5398+
case 'REGEXP_LIKE':
5399+
// PDO narrows integer UDF arguments to 32 bits, so pass the subject and the pattern as text.
5400+
$text_args = min( count( $args ), 2 );
5401+
for ( $i = 0; $i < $text_args; $i++ ) {
5402+
$args[ $i ] = sprintf( 'CAST(%s AS TEXT)', $args[ $i ] );
5403+
}
5404+
return sprintf( '%s(%s)', $name, implode( ', ', $args ) );
53965405
default:
53975406
return $this->translate_sequence( $node->get_children() );
53985407
}

0 commit comments

Comments
 (0)