Skip to content

[Bug]: REGEXP positional functions use byte offsets for utf8mb4 strings #28202

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

#27573 covers empty subjects, #25299 covers binary operands, and #27217 covers dynamic parameter provenance. None covers utf8mb4 character positions.

Branch Name

main

Commit ID

e7bb0572235ec4bac81eb098eb0ad8900f0065ab

Other Environment Information

  • Deployment: local standalone MatrixOne, 1 CN / 1 TN / 1 Log service
  • Client: MySQL text protocol
  • Reference: MySQL 8.3.0

Actual Behavior

The positional REGEXP functions interpret utf8mb4 strings as byte arrays instead of character sequences.

SELECT REGEXP_INSTR('甲😀乙😀丙', '😀');
-- MatrixOne: 4
-- MySQL:    2

SELECT REGEXP_INSTR('甲😀乙😀丙', '😀', 1, 2, 0);
-- MatrixOne: 11
-- MySQL:    4

SELECT REGEXP_INSTR('甲😀乙😀丙', '😀', 1, 2, 1);
-- MatrixOne: 15
-- MySQL:    5

The same byte/character mismatch affects the input pos argument. Position 3 is the 乙 character, so the search must skip the first emoji. MatrixOne starts at byte offset 2 and still selects or replaces the first emoji:

SELECT REGEXP_SUBSTR('甲😀乙😀丙', '😀', 3, 1);
-- MatrixOne: first emoji (character 2)
-- MySQL:    second emoji (character 4)

SELECT REGEXP_REPLACE('甲😀乙😀丙', '😀', 'X', 3, 1);
-- MatrixOne: 甲X乙😀丙
-- MySQL:    甲😀乙X丙

The behavior is identical for constants and values read from a VARCHAR column, and was reproduced three times.

Expected Behavior

For nonbinary utf8mb4 strings, pos, start positions, and end positions must be one-based character positions, matching MySQL. Byte offsets must not leak into SQL-visible REGEXP positions.

MySQL reference: https://dev.mysql.com/doc/refman/8.4/en/regexp.html#function_regexp-instr

Steps to Reproduce

CREATE DATABASE regexp_utf8_probe;
USE regexp_utf8_probe;
CREATE TABLE t (id INT PRIMARY KEY, s VARCHAR(40));
INSERT INTO t VALUES (1, '甲😀乙😀丙');

SELECT REGEXP_INSTR(s, '😀'),
       REGEXP_INSTR(s, '😀', 1, 2, 0),
       REGEXP_INSTR(s, '😀', 1, 2, 1),
       REGEXP_INSTR(s, '😀', 3, 1, 0)
FROM t;

SELECT REGEXP_SUBSTR(s, '😀', 3, 1),
       REGEXP_REPLACE(s, '😀', 'X', 3, 1)
FROM t;

Additional information

regexpSet.regularInstr validates pos against len(str), slices with str[pos-1:], and returns indexes from Go's regexp engine directly. All three operations are byte-based. The substring and replacement helpers use the same byte-index approach for their positional arguments.

Activity

  1. added this to the MO-202609 milestone on Sep 5, 2026
  2. added
    kind/bug
    on Sep 5, 2026
  3. added theissue type on Sep 5, 2026
  4. self-assigned this
    on Sep 5, 2026
  5. Ariznawlll commented on Sep 7, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:bcd5ed833b5400fe95f322e4028adb2bde3de0a4
    • 部署方式:本地 standalone MatrixOne,MySQL 协议端口 6001
    • 测试日期:2026-09-07

    原问题验证

    1. 创建字符串 甲😀乙😀丙,执行 REGEXP_INSTR 的首次匹配、第二次匹配、指定起始位置和 end 参数场景。
    2. 执行 REGEXP_SUBSTR 与 REGEXP_REPLACE,核对字符位置而非 UTF-8 字节位置。
    3. 连续执行 3 轮,每轮结束删除测试数据库。

    Happy Path 覆盖

    1. REGEXP_INSTR 返回 2、4、5、4,第二个 emoji 的字符区间正确。
    2. REGEXP_SUBSTR 返回 😀;从第 3 个字符开始替换一次得到 甲😀乙X丙。

    Unhappy Path 覆盖

    1. 重复匹配和非 1 起始位置均使用多字节字符边界;3/3 轮无字节偏移、越界或会话错误。
    2. 每轮连接健康检查通过并完成数据库清理。

    白盒验证

    不适用。本轮验证公开 REGEXP SQL 接口,未使用内部故障注入。

    回归测试

    已加入 MOTR PR #145:suites/14_issue_regression/t/issue_28202_regexp_utf8_positions.test。

    测试证据

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions