Bug #44583 REPLACE() treats column name as constant
Submitted: 30 Apr 2009 19:37 Modified: 5 May 2009 20:46
Reporter: Paul DuBois Email Updates:
Status: Won't fix Impact on me:
None 
Category:MySQL Server: General Severity:S3 (Non-critical)
Version:5.0 OS:Any
Assigned to: CPU Architecture:Any

[30 Apr 2009 19:37] Paul DuBois
Description:
I have this query:

SELECT TABLE_NAME, REPLACE('Table: ?','?',TABLE_NAME)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'mysql';

I expected the REPLACE() result to contain the current table name in each row. Instead, each row contains the first table name:

+---------------------------+------------------------------------+
| TABLE_NAME                | REPLACE('Table: ?','?',TABLE_NAME) |
+---------------------------+------------------------------------+
| columns_priv              | Table: columns_priv                | 
| db                        | Table: columns_priv                | 
| func                      | Table: columns_priv                | 
| help_category             | Table: columns_priv                | 
| help_keyword              | Table: columns_priv                | 
| help_relation             | Table: columns_priv                | 
| help_topic                | Table: columns_priv                | 
| host                      | Table: columns_priv                | 
| proc                      | Table: columns_priv                | 
| procs_priv                | Table: columns_priv                | 
| tables_priv               | Table: columns_priv                | 
| time_zone                 | Table: columns_priv                | 
| time_zone_leap_second     | Table: columns_priv                | 
| time_zone_name            | Table: columns_priv                | 
| time_zone_transition      | Table: columns_priv                | 
| time_zone_transition_type | Table: columns_priv                | 
| user                      | Table: columns_priv                | 
+---------------------------+------------------------------------+

How to repeat:
See above.

Suggested fix:
Either REPLACE() is treating a non-constant as a constant (incorrectly), or my understanding of how it should work is flawed.
[30 Apr 2009 19:45] Paul DuBois
Perhaps the problem is specific to INFORMATION_SCHEMA tables. Here's a test with a non-I_S table:

use test;
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (TABLE_NAME CHAR(5));
INSERT INTO t1 VALUES('a'),('b'),('c');
SELECT TABLE_NAME, REPLACE('Table: ?','?',TABLE_NAME)
FROM t1;

Which produces the expected result:

+------------+------------------------------------+
| TABLE_NAME | REPLACE('Table: ?','?',TABLE_NAME) |
+------------+------------------------------------+
| a          | Table: a                           | 
| b          | Table: b                           | 
| c          | Table: c                           | 
+------------+------------------------------------+
[30 Apr 2009 20:28] MySQL Verification Team
Results

Attachment: output_IS.txt (text/plain), 8.01 KiB.

[30 Apr 2009 20:32] MySQL Verification Team
Thank you for the bug report. Only repeatable on 5.0 (see previous attached file).
[30 Apr 2009 23:24] Paul DuBois
I confirm I see this only in 5.0 as well. Sorry for the overly-broad version tag.
[5 May 2009 20:47] Omer Barnir
Issue fixed in 5.1