Skip to content

Commit dd0e1c7

Browse files
committed
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".
1 parent 68ce21d commit dd0e1c7

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5177,8 +5177,10 @@ private function translate_regexp_predicate( WP_Parser_Node $node, WP_Parser_Nod
51775177
*
51785178
* The parentheses keep the MySQL precedence. In SQLite, REGEXP binds
51795179
* as loosely as "=", so "1 = 'a' REGEXP 'a'" would be "(1 = 'a') REGEXP 'a'".
5180+
*
5181+
* PDO narrows integer UDF arguments to 32 bits, so pass the operands as text.
51805182
*/
5181-
return sprintf( '(%sREGEXP(%s, %s%s))', $not, $pattern, $subject, $is_binary ? ', 1' : '' );
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
}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,58 @@ public function testRegexpUnicodeNewlineHandling() {
530530
$this->assertSame( '1', $this->last_result[0]->r );
531531
}
532532

533+
/**
534+
* @dataProvider regexpBigIntegerCases
535+
*/
536+
public function testRegexpBigIntegerOperands( $sql, $expected ) {
537+
$this->assertQuery( "SELECT $sql AS r" );
538+
$this->assertSame( $expected, $this->last_result[0]->r );
539+
}
540+
541+
public static function regexpBigIntegerCases() {
542+
return array(
543+
// The smallest values that don't fit in 32 bits.
544+
'2^31, subject' => array( "REGEXP_LIKE(2147483648, '^2147483648\$')", '1' ),
545+
'-2^31 - 1, subject' => array( "REGEXP_LIKE(-2147483649, '^-2147483649\$')", '1' ),
546+
'largest BIGINT' => array( "REGEXP_LIKE(9223372036854775807, '^9223372036854775807\$')", '1' ),
547+
548+
'REGEXP_LIKE subject' => array( "REGEXP_LIKE(9999999999, '^9')", '1' ),
549+
'REGEXP_LIKE pattern' => array( "REGEXP_LIKE('9999999999', 9999999999)", '1' ),
550+
'operator subject' => array( "9999999999 REGEXP '^9'", '1' ),
551+
'operator pattern' => array( "'9999999999' REGEXP 9999999999", '1' ),
552+
'negated operator' => array( "9999999999 NOT REGEXP '^9'", '0' ),
553+
'REGEXP BINARY' => array( "9999999999 REGEXP BINARY '^9'", '1' ),
554+
);
555+
}
556+
557+
public function testRegexpBigIntegerColumn() {
558+
$this->assertQuery( 'CREATE TABLE phones (id INT PRIMARY KEY, phone BIGINT)' );
559+
$this->assertQuery( 'INSERT INTO phones VALUES (1, 15551234567), (2, 42), (3, NULL)' );
560+
$this->assertQuery(
561+
"SELECT
562+
phone REGEXP '^1' AS operator_result,
563+
REGEXP_LIKE(phone, '^1') AS like_result
564+
FROM phones ORDER BY id"
565+
);
566+
$this->assertEquals(
567+
array(
568+
(object) array(
569+
'operator_result' => '1',
570+
'like_result' => '1',
571+
),
572+
(object) array(
573+
'operator_result' => '0',
574+
'like_result' => '0',
575+
),
576+
(object) array(
577+
'operator_result' => null,
578+
'like_result' => null,
579+
),
580+
),
581+
$this->last_result
582+
);
583+
}
584+
533585
public function testInsertDateNow() {
534586
$this->assertQuery(
535587
"INSERT INTO _dates (option_name, option_value) VALUES ('first', now());"

0 commit comments

Comments
 (0)