From 59692ed781ae40aaf3a8e866e28d7c2de9d915f9 Mon Sep 17 00:00:00 2001 From: Nick Chomey <88559987+nickchomey@users.noreply.github.com> Date: Wed, 28 Oct 2026 08:15:37 -0600 Subject: [PATCH 1/3] Add SUBSTRING_INDEX() support via a PHP user-defined function --- ...s-wp-sqlite-pdo-user-defined-functions.php | 58 +++++++++++++++++++ .../tests/WP_MySQL_On_SQLite_Query_Tests.php | 33 +++++++++++ 2 files changed, 91 insertions(+) diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php index 7b8828e7b..b041a0425 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php @@ -81,6 +81,7 @@ public static function register_for( $pdo ): self { 'utc_timestamp' => 'utc_timestamp', 'version' => 'version', 'reverse' => 'reverse', + 'substring_index' => 'substring_index', // Internal helper functions. '_helper_like_to_glob_pattern' => '_helper_like_to_glob_pattern', @@ -927,6 +928,63 @@ public function reverse( $str ) { return strrev( $str ); } + /** + * Method to emulate MySQL SUBSTRING_INDEX() function. + * + * Returns the substring of a string before a given number of + * occurrences of a delimiter. A positive count searches from the left + * and a negative count searches from the right. A count of zero, an + * empty delimiter, or fewer delimiters than requested (in the searched + * direction) yields defined fallbacks matching MySQL. Any NULL input + * yields NULL. + * + * This is a local addition to the vendored SQLite Database Integration + * plugin so plugins relying on MySQL string functions keep working. + * Propose it upstream rather than extending it here. + * + * @param string|null $str The string to search. + * @param string|null $delim The delimiter to search for. + * @param mixed $count How many delimiters to skip; negative counts from the right. + * + * @return string|null The substring, or NULL on NULL input. + */ + public function substring_index( $str, $delim, $count ) { + if ( null === $str || null === $delim || null === $count ) { + return null; + } + $str = (string) $str; + $delim = (string) $delim; + $count = (int) $count; + if ( '' === $delim || 0 === $count ) { + return ''; + } + $delim_len = strlen( $delim ); + if ( $count > 0 ) { + $offset = 0; + for ( $i = 0; $i < $count; $i++ ) { + $pos = strpos( $str, $delim, $offset ); + if ( false === $pos ) { + return $str; + } + $offset = $pos + $delim_len; + } + return substr( $str, 0, $offset - $delim_len ); + } + $count = -$count; + $end = strlen( $str ); + for ( $i = 0; $i < $count; $i++ ) { + $pos = strrpos( substr( $str, 0, $end ), $delim ); + if ( false === $pos ) { + return $str; + } + if ( $i === $count - 1 ) { + return substr( $str, $pos + $delim_len ); + } + $end = $pos; + } + return $str; + } + /** * A helper to convert a LIKE pattern to a GLOB pattern for "LIKE BINARY" support. diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Query_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Query_Tests.php index 15e4fb447..46ccfb7f8 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Query_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Query_Tests.php @@ -339,6 +339,39 @@ public function testSubstringSimple() { $this->assertEquals( 0, count( $actual ) ); } + public function testSubstringIndex() { + $query = <<<'QUERY' +SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2) AS two_from_left, + SUBSTRING_INDEX('www.mysql.com', '.', -2) AS two_from_right, + SUBSTRING_INDEX('www.mysql.com', '.', 7) AS beyond_last, + SUBSTRING_INDEX('nosuchdelim', '.', 3) AS no_match, + SUBSTRING_INDEX('a--b--c', '--', -1) AS multi_char +QUERY; + + $this->assertQuery( $query ); + $actual = $this->last_result; + $this->assertEquals( 'www.mysql', $actual[0]->two_from_left ); + $this->assertEquals( 'mysql.com', $actual[0]->two_from_right ); + $this->assertEquals( 'www.mysql.com', $actual[0]->beyond_last ); + $this->assertEquals( 'nosuchdelim', $actual[0]->no_match ); + $this->assertEquals( 'c', $actual[0]->multi_char ); + } + + public function testSubstringIndexDomainExtraction() { + $this->assertQuery( + "INSERT INTO wp_options (option_name, option_value, autoload) VALUES ('_substring_index_probe', 'https://example.com/a?x=1', 'no');" + ); + $query = <<<'QUERY' +SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(option_value, '/', 3), '://', -1), '/', 1) AS domain +FROM wp_options WHERE option_name = '_substring_index_probe' +QUERY; + + $this->assertQuery( $query ); + $actual = $this->last_result; + $this->assertEquals( 1, count( $actual ) ); + $this->assertEquals( 'example.com', $actual[0]->domain ); + } + public function testCharLengthComplex() { $query = <<<'QUERY' SELECT option_name, From e6edb62c06ba8a5ed2a814b209517c4aa6c38d7c Mon Sep 17 00:00:00 2001 From: Nick Chomey <88559987+nickchomey@users.noreply.github.com> Date: Wed, 28 Oct 2026 09:37:05 -0600 Subject: [PATCH 2/3] test(sqlite): cover SUBSTRING_INDEX edge cases Add regression coverage for NULL arguments, zero count, and an empty delimiter. Remove the stale local-only implementation note. Fixes #581. --- .../class-wp-sqlite-pdo-user-defined-functions.php | 6 +----- .../tests/WP_MySQL_On_SQLite_Query_Tests.php | 12 +++++++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php index b041a0425..c663ab63e 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php @@ -81,7 +81,7 @@ public static function register_for( $pdo ): self { 'utc_timestamp' => 'utc_timestamp', 'version' => 'version', 'reverse' => 'reverse', - 'substring_index' => 'substring_index', + 'substring_index' => 'substring_index', // Internal helper functions. '_helper_like_to_glob_pattern' => '_helper_like_to_glob_pattern', @@ -938,10 +938,6 @@ public function reverse( $str ) { * direction) yields defined fallbacks matching MySQL. Any NULL input * yields NULL. * - * This is a local addition to the vendored SQLite Database Integration - * plugin so plugins relying on MySQL string functions keep working. - * Propose it upstream rather than extending it here. - * * @param string|null $str The string to search. * @param string|null $delim The delimiter to search for. * @param mixed $count How many delimiters to skip; negative counts from the right. diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Query_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Query_Tests.php index 46ccfb7f8..83b276158 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Query_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Query_Tests.php @@ -345,7 +345,12 @@ public function testSubstringIndex() { SUBSTRING_INDEX('www.mysql.com', '.', -2) AS two_from_right, SUBSTRING_INDEX('www.mysql.com', '.', 7) AS beyond_last, SUBSTRING_INDEX('nosuchdelim', '.', 3) AS no_match, - SUBSTRING_INDEX('a--b--c', '--', -1) AS multi_char + SUBSTRING_INDEX('a--b--c', '--', -1) AS multi_char, + SUBSTRING_INDEX(NULL, '.', 1) AS null_string, + SUBSTRING_INDEX('a.b', NULL, 1) AS null_delimiter, + SUBSTRING_INDEX('a.b', '.', NULL) AS null_count, + SUBSTRING_INDEX('a.b', '.', 0) AS zero_count, + SUBSTRING_INDEX('a.b', '', 1) AS empty_delimiter QUERY; $this->assertQuery( $query ); @@ -355,6 +360,11 @@ public function testSubstringIndex() { $this->assertEquals( 'www.mysql.com', $actual[0]->beyond_last ); $this->assertEquals( 'nosuchdelim', $actual[0]->no_match ); $this->assertEquals( 'c', $actual[0]->multi_char ); + $this->assertNull( $actual[0]->null_string ); + $this->assertNull( $actual[0]->null_delimiter ); + $this->assertNull( $actual[0]->null_count ); + $this->assertSame( '', $actual[0]->zero_count ); + $this->assertSame( '', $actual[0]->empty_delimiter ); } public function testSubstringIndexDomainExtraction() { From 5b5b5b0c1d0effd6f2cc7f00c04a85a1797fb67a Mon Sep 17 00:00:00 2001 From: Nick Chomey <88559987+nickchomey@users.noreply.github.com> Date: Wed, 28 Oct 2026 12:15:06 -0600 Subject: [PATCH 3/3] fix(sqlite): preserve SUBSTRING_INDEX semantics Round fractional counts using MySQL-compatible conversion, match multibyte negative-count behavior for overlapping delimiters, and preserve binary semantics. Bound reverse searches to the original string instead of copying shorter prefixes. --- .../src/sqlite/class-wp-mysql-on-sqlite.php | 21 ++++++ ...s-wp-sqlite-pdo-user-defined-functions.php | 75 ++++++++++++++++--- .../tests/WP_MySQL_On_SQLite_Query_Tests.php | 8 ++ 3 files changed, 95 insertions(+), 9 deletions(-) diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php index ce49a9bc0..89e09a4e5 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php @@ -5360,6 +5360,12 @@ private function translate_function_call( WP_Parser_Node $node ): string { return sprintf( 'CAST(STRFTIME(%s, %s) AS FLOAT)', $format, $date ); } return sprintf( 'STRFTIME(%s, %s)', $format, $date ); + case 'SUBSTRING_INDEX': + $argument_nodes = isset( $nodes[1] ) ? $nodes[1]->get_child_nodes() : array(); + if ( isset( $argument_nodes[0] ) && $this->contains_binary_token( $argument_nodes[0] ) ) { + return sprintf( '_helper_substr_index_binary(%s)', implode( ', ', $args ) ); + } + return $this->translate_sequence( $node->get_children() ); case 'CHAR_LENGTH': // @TODO LENGTH and CHAR_LENGTH aren't always the same in MySQL for utf8 characters. return 'LENGTH(' . $args[0] . ')'; @@ -5388,6 +5394,21 @@ private function translate_function_call( WP_Parser_Node $node ): string { } } + /** + * Check whether an expression contains the MySQL BINARY operator or type. + * + * @param WP_Parser_Node $node The expression to inspect. + * @return bool Whether the expression contains a BINARY token. + */ + private function contains_binary_token( WP_Parser_Node $node ): bool { + foreach ( $node->get_descendants() as $descendant ) { + if ( $descendant instanceof WP_Parser_Token && WP_MySQL_Lexer::BINARY_SYMBOL === $descendant->id ) { + return true; + } + } + return false; + } + /** * Get the configured MySQL version in dotted notation. * diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php index c663ab63e..96a5242f4 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php @@ -84,6 +84,7 @@ public static function register_for( $pdo ): self { 'substring_index' => 'substring_index', // Internal helper functions. + '_helper_substr_index_binary' => 'substring_index_binary', '_helper_like_to_glob_pattern' => '_helper_like_to_glob_pattern', ); @@ -945,12 +946,39 @@ public function reverse( $str ) { * @return string|null The substring, or NULL on NULL input. */ public function substring_index( $str, $delim, $count ) { + return $this->substring_index_with_binary_semantics( $str, $delim, $count, false ); + } + + /** + * Internal callback for SUBSTRING_INDEX() when its first argument is binary. + * + * @param string|null $str The string to search. + * @param string|null $delim The delimiter to search for. + * @param mixed $count How many delimiters to skip; negative counts from the right. + * + * @return string|null The substring, or NULL on NULL input. + */ + public function substring_index_binary( $str, $delim, $count ) { + return $this->substring_index_with_binary_semantics( $str, $delim, $count, true ); + } + + /** + * Find a SUBSTRING_INDEX() result using the selected collation semantics. + * + * @param string|null $str The string to search. + * @param string|null $delim The delimiter to search for. + * @param mixed $count How many delimiters to skip; negative counts from the right. + * @param bool $binary Whether the first argument uses binary semantics. + * + * @return string|null The substring, or NULL on NULL input. + */ + private function substring_index_with_binary_semantics( $str, $delim, $count, bool $binary ): ?string { if ( null === $str || null === $delim || null === $count ) { return null; } $str = (string) $str; $delim = (string) $delim; - $count = (int) $count; + $count = (int) round( (float) $count ); if ( '' === $delim || 0 === $count ) { return ''; } @@ -966,19 +994,48 @@ public function substring_index( $str, $delim, $count ) { } return substr( $str, 0, $offset - $delim_len ); } - $count = -$count; - $end = strlen( $str ); + + if ( $binary ) { + $length = strlen( $str ); + $end = $length; + $count = -$count; + for ( $i = 0; $i < $count; $i++ ) { + if ( $end < $delim_len ) { + return $str; + } + $offset = $end - $delim_len - $length; + $pos = strrpos( $str, $delim, $offset ); + if ( false === $pos ) { + return $str; + } + if ( $i === $count - 1 ) { + return substr( $str, $pos + $delim_len ); + } + $end = $pos; + } + return $str; + } + + $delimiter_count = 0; + $offset = 0; + while ( false !== ( $pos = strpos( $str, $delim, $offset ) ) ) { + ++$delimiter_count; + $offset = $pos + $delim_len; + } + $count += $delimiter_count + 1; + if ( $count <= 0 ) { + return $str; + } + + $offset = 0; for ( $i = 0; $i < $count; $i++ ) { - $pos = strrpos( substr( $str, 0, $end ), $delim ); + $pos = strpos( $str, $delim, $offset ); if ( false === $pos ) { return $str; } - if ( $i === $count - 1 ) { - return substr( $str, $pos + $delim_len ); - } - $end = $pos; + $offset = $pos + $delim_len; } - return $str; + return substr( $str, $offset ); } /** diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Query_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Query_Tests.php index 83b276158..15a8187f9 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Query_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Query_Tests.php @@ -346,6 +346,10 @@ public function testSubstringIndex() { SUBSTRING_INDEX('www.mysql.com', '.', 7) AS beyond_last, SUBSTRING_INDEX('nosuchdelim', '.', 3) AS no_match, SUBSTRING_INDEX('a--b--c', '--', -1) AS multi_char, + SUBSTRING_INDEX('a.b.c', '.', 1.6) AS fractional_positive, + SUBSTRING_INDEX('a.b.c', '.', -1.6) AS fractional_negative, + SUBSTRING_INDEX('aaaaa', 'aa', -2) AS overlapping_multibyte, + SUBSTRING_INDEX(BINARY 'aaaaa', 'aa', -2) AS overlapping_binary, SUBSTRING_INDEX(NULL, '.', 1) AS null_string, SUBSTRING_INDEX('a.b', NULL, 1) AS null_delimiter, SUBSTRING_INDEX('a.b', '.', NULL) AS null_count, @@ -360,6 +364,10 @@ public function testSubstringIndex() { $this->assertEquals( 'www.mysql.com', $actual[0]->beyond_last ); $this->assertEquals( 'nosuchdelim', $actual[0]->no_match ); $this->assertEquals( 'c', $actual[0]->multi_char ); + $this->assertSame( 'a.b', $actual[0]->fractional_positive ); + $this->assertSame( 'b.c', $actual[0]->fractional_negative ); + $this->assertSame( 'aaa', $actual[0]->overlapping_multibyte ); + $this->assertSame( 'aa', $actual[0]->overlapping_binary ); $this->assertNull( $actual[0]->null_string ); $this->assertNull( $actual[0]->null_delimiter ); $this->assertNull( $actual[0]->null_count );