Skip to content

Commit f4eedfe

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 4071d42 commit f4eedfe

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
*/
@@ -1298,6 +1355,93 @@ public function testRegexpInstrNumericSubject() {
12981355
$this->assertSame( '2', $this->last_result[0]->r );
12991356
}
13001357

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

0 commit comments

Comments
 (0)