Skip to content

feat(optimizer)!: annotate regexpreplace for mysql#7948

Open
PiyaDaswadkar wants to merge 2 commits into
tobymao:mainfrom
PiyaDaswadkar:piya/annotate-function-regexpreplace
Open

feat(optimizer)!: annotate regexpreplace for mysql#7948
PiyaDaswadkar wants to merge 2 commits into
tobymao:mainfrom
PiyaDaswadkar:piya/annotate-function-regexpreplace

Conversation

@PiyaDaswadkar

Copy link
Copy Markdown
Contributor

This PR annotate the function RegexpReplace for mysql

https://dev.mysql.com/doc/refman/9.7/en/regexp.html#function_regexp-replace

Screenshot From 2026-07-24 20-57-28

Comment thread sqlglot/typing/mysql.py Outdated
}
},
**{
expr_type: {"returns": exp.DType.LONGTEXT}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like REGEXP_REPLACE has signatures for binary inputs that all return LONGBLOB, so we'll need to use an annotator to conditionally set the return type based on the input arg types.

All three of this, expression, and replacement must have the same general type family (either text or binary).

There are 4 possible text and 6 possible binary input types:

mysql> CREATE TABLE regex_test_inputs (
    ->   c_text TEXT, 
    ->   c_tinytext TINYTEXT,
    ->   c_mediumtext MEDIUMTEXT,
    ->   c_longtext LONGTEXT,
    ->   c_binary BINARY(3), 
    ->   c_varbinary VARBINARY(3),
    ->   c_blob BLOB,
    ->   c_tinyblob TINYBLOB,
    ->   c_mediumblob MEDIUMBLOB,
    ->   c_longblob LONGBLOB 
    ->  );
Query OK, 0 rows affected (0.010 sec)

mysql>  
    ->  INSERT INTO regex_test_inputs VALUES (
    ->   'abc',
    ->   'abc',
    ->   'abc',
    ->   'abc',
    ->   X'616263', -- abc 
    ->   X'616263',
    ->   X'616263',
    ->   X'616263',
    ->   X'616263',
    ->   X'616263' 
    ->  );
Query OK, 1 row affected (0.001 sec)

mysql>  
    -> CREATE TABLE regex_test_results AS SELECT
    ->   REGEXP_REPLACE(c_text, 'a', 'z') AS from_text,
    ->   REGEXP_REPLACE(c_tinytext, 'a', 'z') AS from_tinytext,
    ->   REGEXP_REPLACE(c_mediumtext, 'a', 'z') AS from_mediumtext,
    ->   REGEXP_REPLACE(c_longtext, 'a', 'z') AS from_longtext,
    ->   REGEXP_REPLACE(c_binary, X'62', X'78') AS from_binary,
    ->   REGEXP_REPLACE(c_varbinary,X'62', X'78') AS from_varbinary, 
    ->   REGEXP_REPLACE(c_tinyblob, X'62', X'78') AS from_tinyblob,
    ->   REGEXP_REPLACE(c_blob, X'62', X'78') AS from_blob,
    ->   REGEXP_REPLACE(c_mediumblob, X'62', X'78') AS from_mediumblob,
    ->   REGEXP_REPLACE(c_longblob, X'62', X'78') AS from_longblob 
    -> FROM regex_test_inputs;
Query OK, 1 row affected (0.005 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> DESCRIBE regex_test_results;
+-----------------+----------+------+-----+---------+-------+
| Field           | Type     | Null | Key | Default | Extra |
+-----------------+----------+------+-----+---------+-------+
| from_text       | longtext | YES  |     | NULL    |       |
| from_tinytext   | longtext | YES  |     | NULL    |       |
| from_mediumtext | longtext | YES  |     | NULL    |       |
| from_longtext   | longtext | YES  |     | NULL    |       |
| from_binary     | longblob | YES  |     | NULL    |       |
| from_varbinary  | longblob | YES  |     | NULL    |       |
| from_tinyblob   | longblob | YES  |     | NULL    |       |
| from_blob       | longblob | YES  |     | NULL    |       |
| from_mediumblob | longblob | YES  |     | NULL    |       |
| from_longblob   | longblob | YES  |     | NULL    |       |
+-----------------+----------+------+-----+---------+-------+
10 rows in set (0.003 sec)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. I’ve added the binary-family fixtures for REGEXP_REPLACE, covering the same optional argument combinations as the text-family tests. The tests now cover both LONGTEXT and LONGBLOB return types based on the argument family.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants