• Jorgen Loland's avatar
    BUG#12561818 - RERUN OF STORED FUNCTION GIVES ERROR 1172: · 92eab995
    Jorgen Loland authored
                   RESULT CONSISTED OF MORE THAN ONE ROW
    
    MySQL converts incorrect DATEs and DATETIMEs to '0000-00-00' on
    insertion by default. This means that this sequence is possible:
    
    CREATE TABLE t1(date_notnull DATE NOT NULL);
    INSERT INTO t1 values (NULL);
    SELECT * FROM t1;
    0000-00-00
    
    At the same time, ODBC drivers do not (or at least did not in the
    90's) understand the DATE and DATETIME value '0000-00-00'. Thus,
    to be able to query for the value 0000-00-00 it was decided in
    MySQL 4.x (or maybe even before that) that for the special case
    of DATE/DATETIME NOT NULL columns, the query "SELECT ... WHERE
    date_notnull IS NULL" should return rows with date_notnull ==
    '0000-00-00'. This is documented misbehavior that we do not want
    to change.
    
    The hack used to make MySQL return these rows is to convert 
    "date_notnull IS NULL" to "date_notnull = 0". This is, however,
    only done if the table date_notnull belongs to is not an inner
    table of an outer join. The rationale for this seems to be that
    if there is no join match for the row in the outer table,
    null-complemented rows would otherwise not be returned because
    the null-complemented DATE value is actually NULL. On the other
    hand, this means that the "return rows with 0000-00-00 when the
    query asks for IS NULL"-hack is not in effect for outer joins.
    
    In this bug, we have a LEFT JOIN that does not misbehave like 
    the documentation says it should. The fix is to rewrite
    
    "date_notnull IS NULL" to "date_notnull IS NULL OR 
                               date_notnull = 0"
    if dealing with an OUTER JOIN, otherwise 
    "date_notnull IS NULL" to "date_notnull = 0"
    as was done before.
    
    Note:
    The bug was originally reported as different result on first 
    and second execution of SP. The reason was that during first
    execution the query was correctly rewritten to an inner join
    due to a null-rejecting predicate. On second execution the
    "IS NULL" -> "= 0" rewrite was done because there was no outer
    join. The real problem, though, was incorrect date/datetime 
    IS NULL handling for OUTER JOINs.
    
    mysql-test/r/type_datetime.result:
      Add test for BUG#12561818
    mysql-test/t/type_datetime.test:
      Add test for BUG#12561818
    sql/sql_select.cc:
      Special handling of NULL for DATE/DATETIME NOT NULL columns:
      In the case of outer join,
      "date_notnull IS NULL" 
      is now rewritten to
      "date_notnull IS NULL OR date_notnull = 0"
    92eab995
sql_select.cc 571 KB