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

LP:1029835 - Incorect results in union & subqueries

Details

    • Bug
    • Status: Closed (View Workflow)
    • Resolution: Not a Bug
    • None
    • None
    • None

    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.

      Attachments

        Activity

          Re: Incorect results in union & subqueries

          gabrielpreda Gabriel PREDA (Inactive) added a comment - Re: Incorect results in union & subqueries

          Table definition and data
          LPexportBug1029835_different_results.sql

          gabrielpreda Gabriel PREDA (Inactive) added a comment - Table definition and data LPexportBug1029835_different_results.sql
          gabrielpreda Gabriel PREDA (Inactive) created issue -

          Re: Incorect results in union & subqueries
          Correction.

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

          gabrielpreda Gabriel PREDA (Inactive) 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...

          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.

          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.

          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.

          gabrielpreda Gabriel PREDA (Inactive) 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.

          Re: Incorect results in union & subqueries
          Thanx.

          My fault.

          gabrielpreda Gabriel PREDA (Inactive) added a comment - Re: Incorect results in union & subqueries Thanx. My fault.

          Launchpad bug id: 1029835

          ratzpo Rasmus Johansson (Inactive) added a comment - Launchpad bug id: 1029835
          ratzpo Rasmus Johansson (Inactive) made changes -
          Field Original Value New Value
          Resolution Not a Bug [ 6 ]
          Status Closed [ 6 ] Reopened [ 4 ]
          ratzpo Rasmus Johansson (Inactive) made changes -
          Labels Launchpad
          ratzpo Rasmus Johansson (Inactive) made changes -
          Resolution Not a Bug [ 6 ]
          Status Reopened [ 4 ] Closed [ 6 ]
          ratzpo Rasmus Johansson (Inactive) made changes -
          Key IMT-6626 MDEV-2155
          Project ImportTest [ 10200 ] MariaDB Development [ 10000 ]
          Workflow jira [ 20275 ] defaullt [ 22999 ]
          ratzpo Rasmus Johansson (Inactive) made changes -
          Key IMT-6626 MDEV-3284
          Project ImportTest [ 10200 ] MariaDB Development [ 10000 ]
          Workflow jira [ 20275 ] defaullt [ 24128 ]
          ratzpo Rasmus Johansson (Inactive) made changes -
          Key IMT-6626 MDEV-3738
          Project ImportTest [ 10200 ] MariaDB Development [ 10000 ]
          Workflow jira [ 20275 ] defaullt [ 24582 ]
          serg Sergei Golubchik made changes -
          Workflow defaullt [ 24582 ] MariaDB v2 [ 45996 ]
          ratzpo Rasmus Johansson (Inactive) made changes -
          Workflow MariaDB v2 [ 45996 ] MariaDB v3 [ 66676 ]
          serg Sergei Golubchik made changes -
          Workflow MariaDB v3 [ 66676 ] MariaDB v4 [ 145085 ]

          People

            Unassigned Unassigned
            gabrielpreda Gabriel PREDA (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.