@@ -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