Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-3738

LP:1029835 - Incorect results in union & subqueries

    Details

    • Type: Bug
    • Status: Closed
    • Resolution: Not a Bug
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:

      Description

      The following query on the attached table & data yields different results in MySQL 5.5 & MariaDB 5.5:

      SELECT id, date_i, date_ontop, date_until
      FROM
      (
      SELECT id, date_i, date_ontop, date_until
      FROM news
      WHERE
      date_i <= CURRENT_TIMESTAMP
      AND
      date_ontop >= CURRENT_TIMESTAMP
      ORDER BY date_i DESC
      ) AS ontop

      UNION

      SELECT id, date_i, date_ontop, date_until
      FROM
      (
      SELECT id, date_i, date_ontop, date_until
      FROM news
      WHERE
      date_i <= CURRENT_TIMESTAMP
      AND
      ( date_until >= CURRENT_TIMESTAMP OR date_until IS NULL)
      AND
      ( date_ontop IS NULL OR date_ontop < CURRENT_TIMESTAMP )
      ORDER BY date_i DESC
      ) AS therest
      LIMIT 0, 18

      In MariaDB 5.2 it gives the same results as MySQL 5.5.

      It may be related to Bug#1010116 because if I switch optimizer_switch='derived_merge=off' it give the correct results.

        Gliffy Diagrams

          Attachments

            Activity

            Hide
            gabrielpreda Gabriel PREDA added a comment -

            Table definition and data
            LPexportBug1029835_different_results.sql

            Show
            gabrielpreda Gabriel PREDA added a comment - Table definition and data LPexportBug1029835_different_results.sql
            Hide
            gabrielpreda Gabriel PREDA added a comment -

            Re: Incorect results in union & subqueries

            Show
            gabrielpreda Gabriel PREDA added a comment - Re: Incorect results in union & subqueries
            Hide
            gabrielpreda Gabriel PREDA added a comment -

            Re: Incorect results in union & subqueries
            Correction.

            Event with that optimizer_switch ONLY some of the top IDs are in correct order...

            Show
            gabrielpreda Gabriel PREDA added a comment - Re: Incorect results in union & subqueries Correction. Event with that optimizer_switch ONLY some of the top IDs are in correct order...
            Hide
            elenst Elena Stepanova added a comment -

            Re: Incorect results in union & subqueries
            Hi,

            Please check MySQL documentation in regard to exectations about ORDER BY inside UNION subqueries (http://dev.mysql.com/doc/refman/5.5/en/union.html). It is very specific about the type of query you attempt to use:

            <quote>

            use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows. Therefore, the use of ORDER BY in this context is typically in conjunction with LIMIT, so that it is used to determine the subset of the selected rows to retrieve for the SELECT, even though it does not necessarily affect the order of those rows in the final UNION result. If ORDER BY appears without LIMIT in a SELECT, it is optimized away because it will have no effect anyway.

            To use an ORDER BY or LIMIT clause to sort or limit the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY or LIMIT after the last one. The following example uses both clauses:

            (SELECT a FROM t1 WHERE a=10 AND B=1)
            UNION
            (SELECT a FROM t2 WHERE a=11 AND B=2)
            ORDER BY a LIMIT 10;

            </quote>

            The fact that it used to work as you wanted it to was just a pure luck, you rely on a non-existing feature. Luckily, in your case the solution looks fairly obvious, you just need to move the ORDER BY from the subqueries to the upper level.

            Show
            elenst Elena Stepanova added a comment - Re: Incorect results in union & subqueries Hi, Please check MySQL documentation in regard to exectations about ORDER BY inside UNION subqueries ( http://dev.mysql.com/doc/refman/5.5/en/union.html ). It is very specific about the type of query you attempt to use: <quote> use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows. Therefore, the use of ORDER BY in this context is typically in conjunction with LIMIT, so that it is used to determine the subset of the selected rows to retrieve for the SELECT, even though it does not necessarily affect the order of those rows in the final UNION result. If ORDER BY appears without LIMIT in a SELECT, it is optimized away because it will have no effect anyway. To use an ORDER BY or LIMIT clause to sort or limit the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY or LIMIT after the last one. The following example uses both clauses: (SELECT a FROM t1 WHERE a=10 AND B=1) UNION (SELECT a FROM t2 WHERE a=11 AND B=2) ORDER BY a LIMIT 10; </quote> The fact that it used to work as you wanted it to was just a pure luck, you rely on a non-existing feature. Luckily, in your case the solution looks fairly obvious, you just need to move the ORDER BY from the subqueries to the upper level.
            Hide
            gabrielpreda Gabriel PREDA added a comment -

            Re: Incorect results in union & subqueries
            Tested on Percona Server 5.5.25a-27.1, Release rel27.1, Revision 277 and it is not present there.

            Show
            gabrielpreda Gabriel PREDA added a comment - Re: Incorect results in union & subqueries Tested on Percona Server 5.5.25a-27.1, Release rel27.1, Revision 277 and it is not present there.
            Hide
            gabrielpreda Gabriel PREDA added a comment -

            Re: Incorect results in union & subqueries
            Thanx.

            My fault.

            Show
            gabrielpreda Gabriel PREDA added a comment - Re: Incorect results in union & subqueries Thanx. My fault.
            Hide
            ratzpo Rasmus Johansson added a comment -

            Launchpad bug id: 1029835

            Show
            ratzpo Rasmus Johansson added a comment - Launchpad bug id: 1029835

              People

              • Assignee:
                Unassigned
                Reporter:
                gabrielpreda Gabriel PREDA
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: