Bug #2594 INSERT...SELECT sometimes ignores DEFAULT when result column is NULL
Submitted: 30 Jan 2004 21:04 Modified: 3 Feb 2004 4:23
Reporter: Marek Musial Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S1 (Critical)
Version:3.23.58 OS:Linux (Linux 2.4.21 (SuSE))
Assigned to: CPU Architecture:Any

[30 Jan 2004 21:04] Marek Musial
Description:
In an INSERT...SELECT statement with the target table having NOT NULL and DEFAULT attributes, NULL values resulting from a calculation do not insert the default value but the calculation result from a previous row. NULL values resulting from a simple column name do insert the default, and the problem did not occur in 
3.23.53.

How to repeat:
create temporary table master (
  id int(7)     unsigned NOT NULL default 0,
  active int(1) unsigned NOT NULL default 0,
  PRIMARY KEY (id)
) type=heap;

create temporary table res (
  id int(7)     unsigned NOT NULL default 0,
  active int(1) unsigned NOT NULL default 0,
  val double(10,2)       NOT NULL default 0,
  val_e double(10,2)     NOT NULL default 0,
  PRIMARY KEY (id)
) type=heap;

create temporary table sum (
  id  int(7) unsigned NOT NULL default 0,
  val double(10,2)    NOT NULL default 0,
  PRIMARY KEY (id)
) type=heap;

insert into master (id, active) values (1, 1), (2, 1);

insert into sum (id, val) values (1, 42);

insert into res 
select master.id, master.active, sum.val, master.active * sum.val
from master left join sum on sum.id=master.id;

select * from res;

results in:
+----+--------+-------+-------+
| id | active | val   | val_e |
+----+--------+-------+-------+
|  1 |      1 | 42.00 | 42.00 |
|  2 |      1 |  0.00 | 42.00 |
+----+--------+-------+-------+

I would expect val_e(2) = 0.00, because the query returns NULL for this field. Obviously, it has worked for val(2).
[31 Jan 2004 7:04] MySQL Verification Team
Fixed in 4.0:

id      active  val     val_e
1       1       42.00   42.00
2       1       0.00    0.00