Skip to content

Commit 59016b3

Browse files
committed
Add REGEXP_INSTR() UDF
Implement MySQL's REGEXP_INSTR(expr, pattern [, pos [, occurrence [, return_option [, match_type]]]]). It returns the character position of a match, or of the character after it when return_option is 1, and 0 when there is no such match. Like in MySQL: - The search starts in the subject from pos on, so "^" matches at pos, and lookbehind doesn't see the text before it. - pos can't be one past the end, unlike in REGEXP_SUBSTR() and REGEXP_REPLACE(), but it isn't checked for an empty subject, where a match is reported at pos. - An invalid return_option is an error even when another argument is NULL. In data that is not valid UTF-8, positions count bytes.
1 parent bc06528 commit 59016b3

4 files changed

Lines changed: 1194 additions & 7 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5397,6 +5397,7 @@ private function translate_function_call( WP_Parser_Node $node ): string {
53975397
return $this->quote_sqlite_value( $this->get_mysql_server_version_string() );
53985398
case 'REGEXP_LIKE':
53995399
case 'REGEXP_SUBSTR':
5400+
case 'REGEXP_INSTR':
54005401
case 'REGEXP_REPLACE':
54015402
// PDO narrows integer UDF arguments to 32 bits, so pass the subject, the
54025403
// pattern, and REGEXP_REPLACE's replacement as text.

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php‎

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static function register_for( $pdo ): self {
6767
'regexp_like' => 'regexp_like',
6868
'regexp_replace' => 'regexp_replace',
6969
'regexp_substr' => 'regexp_substr',
70+
'regexp_instr' => 'regexp_instr',
7071
'field' => 'field',
7172
'log' => 'log',
7273
'least' => 'least',
@@ -104,6 +105,7 @@ public static function register_for( $pdo ): self {
104105
'regexp_like' => array( 2, 3 ),
105106
'regexp_replace' => array( 3, 4, 5, 6 ),
106107
'regexp_substr' => array( 2, 3, 4, 5 ),
108+
'regexp_instr' => array( 2, 3, 4, 5, 6 ),
107109
);
108110

109111
/** @var string[] Compiled PCRE patterns, keyed by byte mode, match type, and pattern. */
@@ -739,6 +741,68 @@ public function regexp_substr( $expr, $pattern, $pos = 1, $occurrence = 1, $matc
739741
return null === $match ? null : $match[0][0];
740742
}
741743

744+
/**
745+
* Method to emulate MySQL REGEXP_INSTR() function.
746+
*
747+
* Unlike the other functions, it searches the subject from pos on, as if it started
748+
* there. So "^" matches at pos, and lookbehinds don't see the text before it.
749+
*
750+
* @param string|null $expr The subject string.
751+
* @param string|null $pattern The regex pattern.
752+
* @param int|float|string|null $pos Optional. The 1-based character position to start at.
753+
* It isn't checked for an empty subject.
754+
* @param int|float|string|null $occurrence Optional. Which match to find. A value below 1
755+
* finds the first match.
756+
* @param int|float|string|null $return_option Optional. 0 for the position of the match, or 1
757+
* for the position after it.
758+
* @param string|null $match_type Optional. Letters that change how the pattern matches.
759+
*
760+
* @throws Exception If return_option is not 0 or 1, the match type has an unknown
761+
* letter, the pattern is invalid, or pos is out of range.
762+
* @return int|null The 1-based character position, 0 if there is no match, or NULL if
763+
* any argument is NULL.
764+
*/
765+
public function regexp_instr( $expr, $pattern, $pos = 1, $occurrence = 1, $return_option = 0, $match_type = '' ) {
766+
// Like MySQL, check return_option, and then the match type, before NULL arguments.
767+
if ( null !== $return_option && 0 !== (int) $return_option && 1 !== (int) $return_option ) {
768+
throw new Exception( 'Incorrect arguments to regexp_instr: return_option must be 1 or 0.' );
769+
}
770+
$this->regexp_check_match_type( 'regexp_instr', $match_type );
771+
if (
772+
null === $expr || null === $pattern || null === $pos
773+
|| null === $occurrence || null === $return_option || null === $match_type
774+
) {
775+
return null;
776+
}
777+
778+
// Like REGEXP_LIKE(), match data that is not valid UTF-8 byte by byte.
779+
$byte_mode = ! $this->regexp_is_utf8( $pattern ) || ! $this->regexp_is_utf8( $expr );
780+
$compiled = $this->regexp_compile( $pattern, $byte_mode ? 'c' . $match_type : $match_type, $byte_mode );
781+
782+
// MySQL doesn't check pos for an empty subject, and reports its matches at pos.
783+
if ( '' === $expr ) {
784+
$match = $this->regexp_find_match( $compiled, $pattern, '', 0, (int) $occurrence, $byte_mode );
785+
return null === $match ? 0 : (int) $pos;
786+
}
787+
788+
$byte_start = $this->regexp_char_to_byte_offset( $expr, (int) $pos, false, $byte_mode );
789+
$match = $this->regexp_find_match( $compiled, $pattern, substr( $expr, $byte_start ), 0, (int) $occurrence, $byte_mode );
790+
if ( null === $match ) {
791+
return 0;
792+
}
793+
794+
$byte_offset = $byte_start + $match[0][1];
795+
if ( 1 === (int) $return_option ) {
796+
$byte_offset += strlen( $match[0][0] );
797+
}
798+
if ( $byte_mode ) {
799+
return $byte_offset + 1;
800+
}
801+
802+
// Every byte except a UTF-8 continuation byte starts a character.
803+
return $byte_offset + 1 - preg_match_all( '/[\x80-\xBF]/', substr( $expr, 0, $byte_offset ) );
804+
}
805+
742806
/**
743807
* Method to emulate MySQL FIELD() function.
744808
*
@@ -1158,8 +1222,8 @@ private function regexp_check_match_type( $function_name, $match_type ) {
11581222
* sensitivity, and whether positions count bytes, can differ between rows.
11591223
* - SQLite converts numbers to text in its own format, so `20e0` reads "20.0"
11601224
* (MySQL: "20"), and `0.000001` reads "1.0e-06".
1161-
* - `pos` and `occurrence` use PHP's integer conversion, so `2.6` means 2
1162-
* (MySQL: 3), and `'2e1'` means 20 (MySQL: 2).
1225+
* - `pos`, `occurrence`, and `return_option` use PHP's integer conversion, so
1226+
* `2.6` means 2 (MySQL: 3), and `'2e1'` means 20 (MySQL: 2).
11631227
* - A repeated group, like `(a|b)*`, can exceed PCRE's limits on a subject of
11641228
* about 50,000 characters, or 2,000 on PHP 7.2.
11651229
*

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

Lines changed: 134 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,14 @@ public static function regexpBinaryDataCases() {
393393
'REGEXP_SUBSTR, pos one past end' => array( "X'C485FF'", "REGEXP_SUBSTR(b, 'x*', 4)", '' ),
394394
'REGEXP_SUBSTR, occurrence' => array( "X'C485FF'", "REGEXP_SUBSTR(b, '.', 1, 3)", "\xFF" ),
395395
'REGEXP_SUBSTR, empty matches' => array( "X'C485FF'", "REGEXP_SUBSTR(b, 'x*', 1, 4)", '' ),
396+
397+
'REGEXP_INSTR, other case' => array( "X'61FF41'", "REGEXP_INSTR(b, 'A')", '3' ),
398+
'REGEXP_INSTR, ignore case' => array( "X'61FF41'", "REGEXP_INSTR(b, 'A', 1, 1, 0, 'i')", '1' ),
399+
'REGEXP_INSTR, position' => array( "X'C485FF61'", "REGEXP_INSTR(b, 'a')", '4' ),
400+
'REGEXP_INSTR, end position' => array( "X'C485FF61'", "REGEXP_INSTR(b, '.\$', 1, 1, 1)", '5' ),
401+
'REGEXP_INSTR, pos' => array( "X'C485FF61'", "REGEXP_INSTR(b, '^.', 2)", '2' ),
402+
'REGEXP_INSTR, occurrence' => array( "X'C485FF'", "REGEXP_INSTR(b, '.', 1, 3)", '3' ),
403+
'REGEXP_INSTR, empty matches' => array( "X'C485FF'", "REGEXP_INSTR(b, 'x*', 1, 3)", '3' ),
396404
);
397405
}
398406

@@ -445,14 +453,16 @@ public function testRegexpJitStackLimit() {
445453
"SELECT
446454
'$subject' REGEXP '^(a|b)*\$' AS operator_result,
447455
REGEXP_LIKE('$subject', '^(a|b)*\$') AS like_result,
448-
LENGTH(REGEXP_SUBSTR('$subject', '(a|b)+')) AS substr_length"
456+
LENGTH(REGEXP_SUBSTR('$subject', '(a|b)+')) AS substr_length,
457+
REGEXP_INSTR('$subject', '(a|b)+\$', 1, 1, 1) AS instr_result"
449458
);
450459
$this->assertEquals(
451460
array(
452461
(object) array(
453462
'operator_result' => '1',
454463
'like_result' => '1',
455464
'substr_length' => '20000',
465+
'instr_result' => '20001',
456466
),
457467
),
458468
$this->last_result
@@ -537,7 +547,8 @@ public function testRegexpNulBytePattern() {
537547
s REGEXP p AS matched,
538548
s NOT REGEXP p AS not_matched,
539549
s REGEXP BINARY p AS binary_matched,
540-
REGEXP_LIKE(s, p) AS like_matched
550+
REGEXP_LIKE(s, p) AS like_matched,
551+
REGEXP_INSTR(s, p) AS position
541552
FROM nul_patterns ORDER BY id'
542553
);
543554
$this->assertEquals(
@@ -547,30 +558,35 @@ public function testRegexpNulBytePattern() {
547558
'not_matched' => '0',
548559
'binary_matched' => '0',
549560
'like_matched' => '1',
561+
'position' => '2',
550562
),
551563
(object) array(
552564
'matched' => '0',
553565
'not_matched' => '1',
554566
'binary_matched' => '0',
555567
'like_matched' => '0',
568+
'position' => '0',
556569
),
557570
(object) array(
558571
'matched' => '1',
559572
'not_matched' => '0',
560573
'binary_matched' => '0',
561574
'like_matched' => '1',
575+
'position' => '2',
562576
),
563577
(object) array(
564578
'matched' => '1',
565579
'not_matched' => '0',
566580
'binary_matched' => '1',
567581
'like_matched' => '1',
582+
'position' => '1',
568583
),
569584
(object) array(
570585
'matched' => '1',
571586
'not_matched' => '0',
572587
'binary_matched' => '0',
573588
'like_matched' => '1',
589+
'position' => '1',
574590
),
575591
),
576592
$this->last_result
@@ -1089,6 +1105,115 @@ public function testRegexpSubstrNumericSubject() {
10891105
$this->assertSame( '2', $this->last_result[0]->r );
10901106
}
10911107

1108+
/**
1109+
* @dataProvider regexpInstrCases
1110+
*/
1111+
public function testRegexpInstr( $sql, $expected ) {
1112+
$this->assertQuery( "SELECT $sql AS r" );
1113+
$this->assertSame( $expected, $this->last_result[0]->r );
1114+
}
1115+
1116+
public static function regexpInstrCases() {
1117+
return array(
1118+
'basic' => array( "REGEXP_INSTR('dog cat dog', 'dog')", '1' ),
1119+
'no match' => array( "REGEXP_INSTR('abc', 'xyz')", '0' ),
1120+
'second match' => array( "REGEXP_INSTR('dog cat dog', 'dog', 1, 2)", '9' ),
1121+
'pos skips first match' => array( "REGEXP_INSTR('dog cat dog', 'dog', 5)", '9' ),
1122+
'return_option=1 (end)' => array( "REGEXP_INSTR('dog cat dog', 'dog', 1, 1, 1)", '4' ),
1123+
'match_type c miss' => array( "REGEXP_INSTR('DOG', 'dog', 1, 1, 0, 'c')", '0' ),
1124+
'multibyte position' => array( "REGEXP_INSTR('café123', '[0-9]+')", '5' ),
1125+
'null expr' => array( "REGEXP_INSTR(NULL, 'abc')", null ),
1126+
'null pattern' => array( "REGEXP_INSTR('abc', NULL)", null ),
1127+
// A NULL match_type returns NULL before the pattern and pos are checked.
1128+
'null match_type first' => array( "REGEXP_INSTR('abc', '(abc', 0, 1, 0, NULL)", null ),
1129+
// NULL arguments return NULL before pos is checked.
1130+
'null expr, pos 0' => array( "REGEXP_INSTR(NULL, 'a', 0)", null ),
1131+
'null expr, pos -1' => array( "REGEXP_INSTR(NULL, 'a', -1)", null ),
1132+
'null expr, pos 5' => array( "REGEXP_INSTR(NULL, 'a', 5)", null ),
1133+
'null pattern, pos 0' => array( "REGEXP_INSTR('abc', NULL, 0)", null ),
1134+
'null occurrence, pos 0' => array( "REGEXP_INSTR('abc', 'a', 0, NULL)", null ),
1135+
);
1136+
}
1137+
1138+
/**
1139+
* @dataProvider regexpInstrErrors
1140+
*/
1141+
public function testRegexpInstrErrors( $sql, $message ) {
1142+
$this->assertQueryError( "SELECT $sql", $message );
1143+
}
1144+
1145+
public static function regexpInstrErrors() {
1146+
$return_option = 'Incorrect arguments to regexp_instr: return_option must be 1 or 0.';
1147+
return array(
1148+
'invalid pattern' => array( "REGEXP_INSTR('abc', '(abc')", 'Invalid regular expression: (abc.' ),
1149+
'invalid match type' => array( "REGEXP_INSTR('abc', 'a', 1, 1, 0, 'x')", 'Incorrect arguments to regexp_instr' ),
1150+
'invalid match type, NULL subject' => array( "REGEXP_INSTR(NULL, 'a', 1, 1, 0, 'x')", 'Incorrect arguments to regexp_instr' ),
1151+
'invalid match type, NULL return_option' => array( "REGEXP_INSTR('abc', 'a', 1, 1, NULL, 'x')", 'Incorrect arguments to regexp_instr' ),
1152+
'pos zero' => array( "REGEXP_INSTR('abc', 'a', 0)", 'Index out of bounds in regular expression search.' ),
1153+
'pos far past end' => array( "REGEXP_INSTR('abc', 'a', 10)", 'Index out of bounds in regular expression search.' ),
1154+
'invalid return_option' => array( "REGEXP_INSTR('abc', 'a', 1, 1, 2)", $return_option ),
1155+
'invalid return_option, NULL subject' => array( "REGEXP_INSTR(NULL, 'a', 1, 1, 2)", $return_option ),
1156+
'invalid return_option, NULL match type' => array( "REGEXP_INSTR('abc', 'a', 1, 1, 2, NULL)", $return_option ),
1157+
'invalid return_option and match type' => array( "REGEXP_INSTR('abc', 'a', 1, 1, 2, 'x')", $return_option ),
1158+
'extra argument' => array(
1159+
"REGEXP_INSTR('abc', 'a', 1, 1, 0, 'c', 'extra')",
1160+
'SQLSTATE[HY000]: General error: 1 wrong number of arguments to function REGEXP_INSTR()',
1161+
),
1162+
);
1163+
}
1164+
1165+
public function testRegexpInstrEmptySubjectSkipsPosCheck() {
1166+
// MySQL does not check pos for an empty subject and reports a match at pos.
1167+
$cases = array(
1168+
"'a', 2" => '0',
1169+
"'^', 5" => '5',
1170+
"'^', -3" => '-3',
1171+
);
1172+
foreach ( $cases as $args => $expected ) {
1173+
$this->assertQuery( "SELECT REGEXP_INSTR('', $args) AS r" );
1174+
$this->assertSame( $expected, $this->last_result[0]->r, $args );
1175+
}
1176+
1177+
$this->assertQueryError(
1178+
"SELECT REGEXP_INSTR('', '(', 2)",
1179+
'Invalid regular expression: (.'
1180+
);
1181+
}
1182+
1183+
public function testRegexpInstrOccurrenceClampedToOne() {
1184+
// MySQL clamps occurrence <= 0 to 1.
1185+
$this->assertQuery( "SELECT REGEXP_INSTR('abcabc', 'b', 1, 0) AS r" );
1186+
$this->assertSame( '2', $this->last_result[0]->r );
1187+
$this->assertQuery( "SELECT REGEXP_INSTR('abcabc', 'b', 1, -5) AS r" );
1188+
$this->assertSame( '2', $this->last_result[0]->r );
1189+
}
1190+
1191+
public function testRegexpInstrStraddlingMatch() {
1192+
// A match that starts before pos is not returned; the next match at or
1193+
// after pos is returned instead.
1194+
$this->assertQuery( "SELECT REGEXP_INSTR('abc123def', '[0-9]+', 5) AS r" );
1195+
$this->assertSame( '5', $this->last_result[0]->r );
1196+
}
1197+
1198+
public function testRegexpInstrMultibyteReturnOptionEnd() {
1199+
// Multibyte match ('é' is 2 bytes) with return_option=1 (one past end).
1200+
// 'aéb' char positions: a=1, é=2, b=3. 'é' matches at char 2, end char position = 3.
1201+
$this->assertQuery( "SELECT REGEXP_INSTR('aéb', 'é', 1, 1, 1) AS r" );
1202+
$this->assertSame( '3', $this->last_result[0]->r );
1203+
}
1204+
1205+
public function testRegexpInstrPatternIsRelativeToPos() {
1206+
$this->assertQuery( "SELECT REGEXP_INSTR('abc', '^b', 2) AS r" );
1207+
$this->assertSame( '2', $this->last_result[0]->r );
1208+
$this->assertQuery( "SELECT REGEXP_INSTR('ab', '(?<=a)b', 2) AS r" );
1209+
$this->assertSame( '0', $this->last_result[0]->r );
1210+
}
1211+
1212+
public function testRegexpInstrNumericSubject() {
1213+
$this->assertQuery( "SELECT REGEXP_INSTR(123, '2') AS r" );
1214+
$this->assertSame( '2', $this->last_result[0]->r );
1215+
}
1216+
10921217
/**
10931218
* @dataProvider regexpBigIntegerCases
10941219
*/
@@ -1108,6 +1233,8 @@ public static function regexpBigIntegerCases() {
11081233
'REGEXP_LIKE pattern' => array( "REGEXP_LIKE('9999999999', 9999999999)", '1' ),
11091234
'REGEXP_SUBSTR' => array( "REGEXP_SUBSTR(9999999999, '9+')", '9999999999' ),
11101235
'REGEXP_SUBSTR regex' => array( "REGEXP_SUBSTR('9999999999', 9999999999)", '9999999999' ),
1236+
'REGEXP_INSTR' => array( "REGEXP_INSTR(9999999999, '99', 1, 2)", '3' ),
1237+
'REGEXP_INSTR pattern' => array( "REGEXP_INSTR('x9999999999', 9999999999)", '2' ),
11111238
'REGEXP_REPLACE' => array( "REGEXP_REPLACE(9999999999, '9', '1')", '1111111111' ),
11121239
'REGEXP_REPLACE regex' => array( "REGEXP_REPLACE('9999999999', 9999999999, 'x')", 'x' ),
11131240
'replacement' => array( "REGEXP_REPLACE('a', 'a', 9999999999)", '9999999999' ),
@@ -1126,7 +1253,8 @@ public function testRegexpBigIntegerColumn() {
11261253
phone REGEXP '^1' AS operator_result,
11271254
REGEXP_LIKE(phone, '^1') AS like_result,
11281255
REGEXP_REPLACE(phone, '^1', '') AS replace_result,
1129-
REGEXP_SUBSTR(phone, '^[0-9]{3}') AS substr_result
1256+
REGEXP_SUBSTR(phone, '^[0-9]{3}') AS substr_result,
1257+
REGEXP_INSTR(phone, '4') AS instr_result
11301258
FROM phones ORDER BY id"
11311259
);
11321260
$this->assertEquals(
@@ -1136,18 +1264,21 @@ public function testRegexpBigIntegerColumn() {
11361264
'like_result' => '1',
11371265
'replace_result' => '5551234567',
11381266
'substr_result' => '155',
1267+
'instr_result' => '8',
11391268
),
11401269
(object) array(
11411270
'operator_result' => '0',
11421271
'like_result' => '0',
11431272
'replace_result' => '42',
11441273
'substr_result' => null,
1274+
'instr_result' => '1',
11451275
),
11461276
(object) array(
11471277
'operator_result' => null,
11481278
'like_result' => null,
11491279
'replace_result' => null,
11501280
'substr_result' => null,
1281+
'instr_result' => null,
11511282
),
11521283
),
11531284
$this->last_result

0 commit comments

Comments
 (0)