Bug #71380 FORMAT(x,y) is incorrectly truncated when used with a sorted JOIN
Submitted: 14 Jan 2014 19:35 Modified: 15 Jan 2014 7:15
Reporter: Arthur O'Dwyer Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:5.5.31, 5.5.35, 5.6.15 OS:Any
Assigned to: CPU Architecture:Any

[14 Jan 2014 19:35] Arthur O'Dwyer
Description:
Some FORMAT() builtins sometimes give truncated results when they're in the project list of a JOIN.

I think what's happening is that MySQL is incorrectly computing the max_length of the Item_func_format, and then using that incorrect (too-short) max_length when it goes to populate the intermediate temporary table and/or the file used for sorting the output. The result is that the output is truncated for such JOINs.

As demonstrated below, there's no problem with a plain old JOIN, but as soon as you ORDER or GROUP the output, MySQL will sort it, which means you'll see truncation.

How to repeat:
CREATE TABLE f (value FLOAT);
INSERT INTO f VALUES (340282346638528860000000000000000000000);
CREATE TABLE dummy (i INT);
INSERT INTO dummy VALUES ();
SELECT FORMAT(value,0) a FROM (f JOIN dummy) ORDER BY value;
SELECT FORMAT(value,0) a FROM (f JOIN dummy) GROUP BY a;

  +---------------------------------------------------+
  | a                                                 |
  +---------------------------------------------------+
  | 340,282,346,638,528,860,000,000,000,000,000,000,0 |
  +---------------------------------------------------+

SELECT FORMAT(value,0) a FROM (f JOIN dummy);

  +-----------------------------------------------------+
  | a                                                   |
  +-----------------------------------------------------+
  | 340,282,346,638,528,860,000,000,000,000,000,000,000 |
  +-----------------------------------------------------+

EXPLAIN SELECT FORMAT(value,0) FROM (f JOIN dummy) ORDER BY value;

  +----+-------------+-------+------+---------------+------+---------+------+------+---------------------------------+
  | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                           |
  +----+-------------+-------+------+---------------+------+---------+------+------+---------------------------------+
  |  1 | SIMPLE      | f     | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using temporary; Using filesort |
  |  1 | SIMPLE      | dummy | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using join buffer               |
  +----+-------------+-------+------+---------------+------+---------+------+------+---------------------------------+

Suggested fix:
The bug might be wherever Item_field or Item_float set their max_lengths; or the bug might be that Item_func_format assumes its output will be "about the same length as the input" instead of hard-coding something like "40" for arguments of type FLOAT.
[15 Jan 2014 7:15] MySQL Verification Team
Hello Arthur,

Thank you for the report and testcase.
Verified as described.

Thanks,
Umesh