phpmyadmin / phpmyadmin/sql-parser
[Bug]: Problem exporting an unsual index (IFNULL expression)
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 485
- Forks
- 119
- PR merge metrics
- No merged PRs in 30d
Description
Describe the bug
I added this index to a table:
ALTER TABLE drafts
ADD UNIQUE unique_draft (type,user,(IFNULL(touser,'_CHAT_'))) USING BTREE;
and when I try to export it fails with this error (on hosting phpmyadmin version 5.1.0, mysql is Percona 8.0.43-34):
Fatal error: Uncaught TypeError: str_replace(): Argument phpmyadmin/phpmyadmin#3 ($subject) must be of type array|string, null given in /var/www/html/phpMyAdmin/vendor/phpmyadmin/sql-parser/src/Context.php:608
Stack trace:
#0 /var/www/html/phpMyAdmin/vendor/phpmyadmin/sql-parser/src/Context.php(608): str_replace()
phpmyadmin/phpmyadmin#1 /var/www/html/phpMyAdmin/vendor/phpmyadmin/sql-parser/src/Components/Key.php(203): PhpMyAdmin\SqlParser\Context::escape()
phpmyadmin/phpmyadmin#2 /var/www/html/phpMyAdmin/vendor/phpmyadmin/sql-parser/src/Component.php(74): PhpMyAdmin\SqlParser\Components\Key::build()
phpmyadmin/phpmyadmin#3 /var/www/html/phpMyAdmin/vendor/phpmyadmin/sql-parser/src/Components/CreateDefinition.php(360): PhpMyAdmin\SqlParser\Component->__toString()
phpmyadmin/phpmyadmin#4 /var/www/html/phpMyAdmin/vendor/phpmyadmin/sql-parser/src/Component.php(74): PhpMyAdmin\SqlParser\Components\CreateDefinition::build()
phpmyadmin/phpmyadmin#5 [internal function]: PhpMyAdmin\SqlParser\Component->__toString()
phpmyadmin/phpmyadmin#6 /var/www/html/phpMyAdmin/vendor/phpmyadmin/sql-parser/src/Components/CreateDefinition.php(339): implode()
phpmyadmin/phpmyadmin#7 /var/www/html/phpMyAdmin/vendor/phpmyadmin/sql-parser/src/Statements/CreateStatement.php(380): PhpMyAdmin\SqlParser\Components\CreateDefinition::build()
phpmyadmin/phpmyadmin#8 /var/www/html/phpMyAdmin/libraries/classes/Plugins/Export/ExportSql.php(2951): PhpMyAdmin\SqlParser\Statements\CreateStatement->build()
phpmyadmin/phpmyadmin#9 /var/www/html/phpMyAdmin/libraries/classes/Plugins/Export/ExportSql.php(1638): PhpMyAdmin\Plugins\Export\ExportSql->replaceWithAliases()
phpmyadmin/phpmyadmin#10 /var/www/html/phpMyAdmin/libraries/classes/Plugins/Export/ExportSql.php(2138): PhpMyAdmin\Plugins\Export\ExportSql->getTableDef()
phpmyadmin/phpmyadmin#11 /var/www/html/phpMyAdmin/libraries/classes/Export.php(813): PhpMyAdmin\Plugins\Export\ExportSql->exportStructure()
phpmyadmin/phpmyadmin#12 /var/www/html/phpMyAdmin/libraries/classes/Controllers/ExportController.php(549): PhpMyAdmin\Export->exportDatabase()
phpmyadmin/phpmyadmin#13 /var/www/html/phpMyAdmin/libraries/classes/Routing.php(186): PhpMyAdmin\Controllers\ExportController->index()
phpmyadmin/phpmyadmin#14 /var/www/html/phpMyAdmin/index.php(50): PhpMyAdmin\Routing::callControllerForRoute()
phpmyadmin/phpmyadmin#15 {main}
thrown in /var/www/html/phpMyAdmin/vendor/phpmyadmin/sql-parser/src/Context.php on line 608
On version 5.2.1 with mysql 8.0.41 it won't fails but the export result is like this
ALTER TABLE drafts ADD UNIQUE KEY unique_draft (type,user,(_CHAT_));
which is wrong.
How to Reproduce
Table structure is:
CREATE TABLE drafts (
type enum('CHAT','MSGS') CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
user varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
touser varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL,
text text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
date datetime NOT NULL
);
ALTER TABLE drafts ADD UNIQUE unique_draft (type, user,(IFNULL(touser,'_CHAT_'))) USING BTREE;
Expected behavior
I expect it to export the index like the original one
Screenshots
No response
Operating System
No response
Web Server
No response
Database Server
MySQL
Database version
No response
PHP version
No response
phpMyAdmin version
5.1.0
browser
No response
Additional context
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The failure is reported through vendor/phpmyadmin/sql-parser/src/Context.php:608 and Components/Key.php:203 during the ExportSql.php export path. Reproduce it with the supplied drafts table and index definition, then inspect the parser/build path; done means export no longer throws and preserves IFNULL(touser,'CHAT') rather than emitting CHAT.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mysql, php
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100