Skip to content

Commit c79b7bf

Browse files
committed
Pin down REGEXP_* edge cases shared across functions
Test behavior that all four REGEXP_* functions share: - They work on table columns in SELECT, WHERE, and UPDATE statements. - An empty pattern is an error. - A 4-byte UTF-8 character counts as one character. - Zero-width matches step forward one character, like in ICU.
1 parent 59016b3 commit c79b7bf

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

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

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,63 @@ public static function regexpOperators() {
266266
);
267267
}
268268

269+
public function testRegexpFunctionsWithTableColumns() {
270+
$this->assertQuery(
271+
"INSERT INTO _options (option_name, option_value) VALUES
272+
('test-ignore', 'unchanged'),
273+
('test-remove', 'unchanged'),
274+
('keep', 'unchanged');"
275+
);
276+
277+
$this->assertQuery(
278+
"SELECT
279+
option_name,
280+
REGEXP_REPLACE(option_name, '(-ignore|-remove)\$', '') AS replacement_result,
281+
REGEXP_SUBSTR(option_name, '[^-]+\$') AS substring_result,
282+
REGEXP_INSTR(option_name, '-') AS position_result
283+
FROM _options
284+
WHERE REGEXP_LIKE(option_name, '^test-')
285+
ORDER BY option_name"
286+
);
287+
$this->assertEquals(
288+
array(
289+
(object) array(
290+
'option_name' => 'test-ignore',
291+
'replacement_result' => 'test',
292+
'substring_result' => 'ignore',
293+
'position_result' => '5',
294+
),
295+
(object) array(
296+
'option_name' => 'test-remove',
297+
'replacement_result' => 'test',
298+
'substring_result' => 'remove',
299+
'position_result' => '5',
300+
),
301+
),
302+
$this->last_result
303+
);
304+
305+
$this->assertQuery(
306+
"UPDATE _options
307+
SET option_value = REGEXP_REPLACE(option_name, '^test-', '')
308+
WHERE REGEXP_LIKE(option_name, '^test-')"
309+
);
310+
$this->assertQuery( "SELECT option_name, option_value FROM _options WHERE option_value != 'unchanged' ORDER BY option_name" );
311+
$this->assertEquals(
312+
array(
313+
(object) array(
314+
'option_name' => 'test-ignore',
315+
'option_value' => 'ignore',
316+
),
317+
(object) array(
318+
'option_name' => 'test-remove',
319+
'option_value' => 'remove',
320+
),
321+
),
322+
$this->last_result
323+
);
324+
}
325+
269326
/**
270327
* @dataProvider regexpLikeQueries
271328
*/
@@ -1214,6 +1271,70 @@ public function testRegexpInstrNumericSubject() {
12141271
$this->assertSame( '2', $this->last_result[0]->r );
12151272
}
12161273

1274+
public function testRegexpEmptyPatternRejected() {
1275+
// MySQL rejects the empty pattern with ERROR 3685.
1276+
$this->assertQueryError(
1277+
"SELECT REGEXP_LIKE('abc', '')",
1278+
'Illegal argument to a regular expression.'
1279+
);
1280+
$this->assertQueryError(
1281+
"SELECT REGEXP_REPLACE('abc', '', 'x')",
1282+
'Illegal argument to a regular expression.'
1283+
);
1284+
$this->assertQueryError(
1285+
"SELECT REGEXP_SUBSTR('abc', '')",
1286+
'Illegal argument to a regular expression.'
1287+
);
1288+
$this->assertQueryError(
1289+
"SELECT REGEXP_INSTR('abc', '')",
1290+
'Illegal argument to a regular expression.'
1291+
);
1292+
}
1293+
1294+
/**
1295+
* @dataProvider regexpEmptyMatchSteppingCases
1296+
*/
1297+
public function testRegexpEmptyMatchStepping( $sql, $expected ) {
1298+
// Like in ICU, the search continues one character after an empty match, even
1299+
// when the pattern could match a non-empty string at the same position.
1300+
$this->assertQuery( "SELECT $sql AS r" );
1301+
$this->assertSame( $expected, $this->last_result[0]->r );
1302+
}
1303+
1304+
public static function regexpEmptyMatchSteppingCases() {
1305+
return array(
1306+
'lazy star' => array( "REGEXP_REPLACE('aaa', 'a*?', '[\$0]')", '[]a[]a[]a[]' ),
1307+
'lazy optional' => array( "REGEXP_REPLACE('aaa', 'a??', '[\$0]')", '[]a[]a[]a[]' ),
1308+
'empty alternative first' => array( "REGEXP_REPLACE('aaa', '|a', '[\$0]')", '[]a[]a[]a[]' ),
1309+
'boundary alternative' => array( "REGEXP_REPLACE('ab', '\\\\b|a', '[\$0]')", '[]ab[]' ),
1310+
'anchor alternative' => array( "REGEXP_REPLACE('abc', '^|b', '[\$0]')", '[]a[b]c' ),
1311+
'lookahead alternative' => array( "REGEXP_REPLACE('ab', '(?=b)|a', '[\$0]')", '[a][]b' ),
1312+
'greedy, ends with empty' => array( "REGEXP_REPLACE('aaa', 'a*', '[\$0]')", '[aaa][]' ),
1313+
'greedy, starts with empty' => array( "REGEXP_REPLACE('baaa', 'a*', '[\$0]')", '[]b[aaa][]' ),
1314+
'multibyte character' => array( "REGEXP_REPLACE('éa', 'a*?', '[\$0]')", '[]é[]a[]' ),
1315+
'astral character' => array( "REGEXP_REPLACE('a😀b', 'x*', '[\$0]')", '[]a[]😀[]b[]' ),
1316+
'replace second' => array( "REGEXP_REPLACE('aaa', 'a*?', '[\$0]', 1, 2)", 'a[]aa' ),
1317+
'replace past the matches' => array( "REGEXP_REPLACE('ab', '\\\\b|a', '[\$0]', 1, 3)", 'ab' ),
1318+
'substr, empty match' => array( "REGEXP_SUBSTR('aaa', 'a*?', 1, 2)", '' ),
1319+
'substr, one past the end' => array( "REGEXP_SUBSTR('aaa', 'a*?', 1, 4)", '' ),
1320+
'substr, after an empty' => array( "REGEXP_SUBSTR('baaa', 'a*', 1, 2)", 'aaa' ),
1321+
'substr, no more matches' => array( "REGEXP_SUBSTR('aaa', 'a*', 1, 3)", null ),
1322+
'instr, empty match' => array( "REGEXP_INSTR('aaa', 'a*?', 1, 3)", '3' ),
1323+
'instr, boundary' => array( "REGEXP_INSTR('ab', '\\\\b|a', 1, 2)", '3' ),
1324+
'instr, astral character' => array( "REGEXP_INSTR('a😀b', 'x*', 1, 3)", '3' ),
1325+
'instr, no more matches' => array( "REGEXP_INSTR('aaa', 'a*', 1, 3)", '0' ),
1326+
);
1327+
}
1328+
1329+
public function testRegexpAstralPlaneCharacter() {
1330+
// 4-byte UTF-8 encodes as one code point; char offsets should reflect that.
1331+
// "x😀y" has three characters (x at 1, 😀 at 2, y at 3).
1332+
$this->assertQuery( "SELECT REGEXP_SUBSTR('x😀y', '.', 2) AS r" );
1333+
$this->assertSame( '😀', $this->last_result[0]->r );
1334+
$this->assertQuery( "SELECT REGEXP_INSTR('😀z', 'z', 1, 1, 1) AS r" );
1335+
$this->assertSame( '3', $this->last_result[0]->r );
1336+
}
1337+
12171338
/**
12181339
* @dataProvider regexpBigIntegerCases
12191340
*/

0 commit comments

Comments
 (0)