Skip to content

Commit 61b7b1f

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 18c5a66 commit 61b7b1f

1 file changed

Lines changed: 144 additions & 0 deletions

File tree

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

Lines changed: 144 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
*/
@@ -1301,6 +1358,93 @@ public function testRegexpInstrNumericSubject() {
13011358
$this->assertSame( '2', $this->last_result[0]->r );
13021359
}
13031360

1361+
public function testRegexpEmptyPatternRejected() {
1362+
// MySQL rejects the empty pattern with ERROR 3685.
1363+
$this->assertQueryError(
1364+
"SELECT REGEXP_LIKE('abc', '')",
1365+
'Illegal argument to a regular expression.'
1366+
);
1367+
$this->assertQueryError(
1368+
"SELECT REGEXP_REPLACE('abc', '', 'x')",
1369+
'Illegal argument to a regular expression.'
1370+
);
1371+
$this->assertQueryError(
1372+
"SELECT REGEXP_SUBSTR('abc', '')",
1373+
'Illegal argument to a regular expression.'
1374+
);
1375+
$this->assertQueryError(
1376+
"SELECT REGEXP_INSTR('abc', '')",
1377+
'Illegal argument to a regular expression.'
1378+
);
1379+
}
1380+
1381+
/**
1382+
* @dataProvider regexpEmptyMatchSteppingCases
1383+
*/
1384+
public function testRegexpEmptyMatchStepping( $sql, $expected ) {
1385+
// Like in ICU, the search continues one character after an empty match, even
1386+
// when the pattern could match a non-empty string at the same position.
1387+
$this->assertQuery( "SELECT $sql AS r" );
1388+
$this->assertSame( $expected, $this->last_result[0]->r );
1389+
}
1390+
1391+
public static function regexpEmptyMatchSteppingCases() {
1392+
return array(
1393+
'lazy star' => array( "REGEXP_REPLACE('aaa', 'a*?', '[\$0]')", '[]a[]a[]a[]' ),
1394+
'lazy optional' => array( "REGEXP_REPLACE('aaa', 'a??', '[\$0]')", '[]a[]a[]a[]' ),
1395+
'empty alternative first' => array( "REGEXP_REPLACE('aaa', '|a', '[\$0]')", '[]a[]a[]a[]' ),
1396+
'boundary alternative' => array( "REGEXP_REPLACE('ab', '\\\\b|a', '[\$0]')", '[]ab[]' ),
1397+
'anchor alternative' => array( "REGEXP_REPLACE('abc', '^|b', '[\$0]')", '[]a[b]c' ),
1398+
'lookahead alternative' => array( "REGEXP_REPLACE('ab', '(?=b)|a', '[\$0]')", '[a][]b' ),
1399+
'greedy, ends with empty' => array( "REGEXP_REPLACE('aaa', 'a*', '[\$0]')", '[aaa][]' ),
1400+
'greedy, starts with empty' => array( "REGEXP_REPLACE('baaa', 'a*', '[\$0]')", '[]b[aaa][]' ),
1401+
'multibyte character' => array( "REGEXP_REPLACE('éa', 'a*?', '[\$0]')", '[]é[]a[]' ),
1402+
'astral character' => array( "REGEXP_REPLACE('a😀b', 'x*', '[\$0]')", '[]a[]😀[]b[]' ),
1403+
'replace second' => array( "REGEXP_REPLACE('aaa', 'a*?', '[\$0]', 1, 2)", 'a[]aa' ),
1404+
'replace past the matches' => array( "REGEXP_REPLACE('ab', '\\\\b|a', '[\$0]', 1, 3)", 'ab' ),
1405+
'substr, empty match' => array( "REGEXP_SUBSTR('aaa', 'a*?', 1, 2)", '' ),
1406+
'substr, one past the end' => array( "REGEXP_SUBSTR('aaa', 'a*?', 1, 4)", '' ),
1407+
'substr, after an empty' => array( "REGEXP_SUBSTR('baaa', 'a*', 1, 2)", 'aaa' ),
1408+
'substr, no more matches' => array( "REGEXP_SUBSTR('aaa', 'a*', 1, 3)", null ),
1409+
'instr, empty match' => array( "REGEXP_INSTR('aaa', 'a*?', 1, 3)", '3' ),
1410+
'instr, boundary' => array( "REGEXP_INSTR('ab', '\\\\b|a', 1, 2)", '3' ),
1411+
'instr, astral character' => array( "REGEXP_INSTR('a😀b', 'x*', 1, 3)", '3' ),
1412+
'instr, no more matches' => array( "REGEXP_INSTR('aaa', 'a*', 1, 3)", '0' ),
1413+
);
1414+
}
1415+
1416+
public function testRegexpAstralPlaneCharacter() {
1417+
// 4-byte UTF-8 encodes as one code point; char offsets should reflect that.
1418+
// "x😀y" has three characters (x at 1, 😀 at 2, y at 3).
1419+
$this->assertQuery( "SELECT REGEXP_SUBSTR('x😀y', '.', 2) AS r" );
1420+
$this->assertSame( '😀', $this->last_result[0]->r );
1421+
$this->assertQuery( "SELECT REGEXP_INSTR('😀z', 'z', 1, 1, 1) AS r" );
1422+
$this->assertSame( '3', $this->last_result[0]->r );
1423+
}
1424+
1425+
public function testRegexpEscapedKInLookbehind() {
1426+
// "\K" is a literal "K". In PCRE, it could move the start of a match in a lookbehind
1427+
// to before the search, so that the next search found the same match forever.
1428+
$this->assertQuery(
1429+
"SELECT
1430+
REGEXP_REPLACE('ab', '(?<=\\\\Ka)', 'X') AS replace_result,
1431+
REGEXP_REPLACE('Kab', '(?<=\\\\Ka)', 'X') AS replace_after_k,
1432+
REGEXP_SUBSTR('ab', '(?<=\\\\Ka)', 1, 2) AS substr_result,
1433+
REGEXP_INSTR('Kab', '(?<=\\\\Ka)') AS instr_result"
1434+
);
1435+
$this->assertEquals(
1436+
array(
1437+
(object) array(
1438+
'replace_result' => 'ab',
1439+
'replace_after_k' => 'KaXb',
1440+
'substr_result' => null,
1441+
'instr_result' => '3',
1442+
),
1443+
),
1444+
$this->last_result
1445+
);
1446+
}
1447+
13041448
/**
13051449
* @dataProvider regexpBigIntegerCases
13061450
*/

0 commit comments

Comments
 (0)