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

ORDER BY in view definition leads to wrong result with GROUP BY on query using view

Details

    Description

      Starting with MariaDB 10.2 the SQL snippet below does no longer return the same result on all three SELECTs at its end as expected, instead the last query, which uses the view with ORDER BY in it, returns an empty result set now:

      DROP TABLE IF EXISTS t1;
      CREATE TABLE t1 (
        id INT PRIMARY KEY AUTO_INCREMENT,
        dt datetime,
        INDEX(dt),
        foo int
      );
       
      INSERT INTO t1 VALUES (1,'2020-09-26 12:00:00',1);
      INSERT INTO t1 VALUES (2,'2020-09-26 13:00:00',1);
      INSERT INTO t1 VALUES (3,'2020-09-27 13:00:00',1);
      INSERT INTO t1 VALUES (4,'2020-09-27 12:00:00',1);
      INSERT INTO t1 VALUES (5,'2020-09-28 12:00:00',1);
      INSERT INTO t1 VALUES (6,'2020-09-28 13:00:00',1);
      INSERT INTO t1 VALUES (7,'2020-09-25 12:00:00',1);
      INSERT INTO t1 VALUES (8,'2020-09-25 13:00:00',1);
      INSERT INTO t1 VALUES (9,'2020-09-26 13:00:00',1);
       
      DROP VIEW IF EXISTS v1;
      CREATE VIEW v1 AS SELECT * FROM t1;
       
      DROP VIEW IF EXISTS v2;
      CREATE VIEW v2 AS SELECT * FROM t1 ORDER BY dt;
       
      SELECT dt, sum(foo) AS foo FROM t1 WHERE dt>DATE_SUB('2020-09-27 00:00:00', INTERVAL 3 DAY) GROUP BY dt;
       
      SELECT dt, sum(foo) AS foo FROM v1 WHERE dt>DATE_SUB('2020-09-27 00:00:00', INTERVAL 3 DAY) GROUP BY dt;
       
      SELECT dt, sum(foo) AS foo FROM v2 WHERE dt>DATE_SUB('2020-09-27 00:00:00', INTERVAL 3 DAY) GROUP BY dt;
      

      Attachments

        Activity

          hholzgra Hartmut Holzgraefe created issue -
          varun Varun Gupta (Inactive) made changes -
          Field Original Value New Value
          Assignee Varun Gupta [ varun ]
          varun Varun Gupta (Inactive) made changes -
          Fix Version/s 10.2 [ 14601 ]
          Fix Version/s 10.3 [ 22126 ]
          Fix Version/s 10.4 [ 22408 ]
          Fix Version/s 10.5 [ 23123 ]
          varun Varun Gupta (Inactive) made changes -
          Status Open [ 1 ] In Progress [ 3 ]
          varun Varun Gupta (Inactive) made changes -
          Assignee Varun Gupta [ varun ] Sergei Petrunia [ psergey ]
          Status In Progress [ 3 ] In Review [ 10002 ]
          varun Varun Gupta (Inactive) made changes -
          Assignee Sergei Petrunia [ psergey ] Michael Widenius [ monty ]

          Shouldn't the change you did to Item_direct_view_ref be done to Item_direct_ref ?

          Item_direct_view_ref::val_time_packed() should probably be written as the other val methods in Item_direct_view_ref:

          double val_real()

          { if (check_null_ref()) return 0; else return Item_direct_ref::val_real(); }

          A suggestion to make this kind of bugs a bit easier to avoid in the future:
          In Item_direct_ref, we should keep all val methods the same as we have in Item_ref, including adding a comment what we are overloading.

          I have attached a "fixed" patch to this Jira entry with the above changes. Feel free to push it after you have checked and agreed to my changes.

          monty Michael Widenius added a comment - Shouldn't the change you did to Item_direct_view_ref be done to Item_direct_ref ? Item_direct_view_ref::val_time_packed() should probably be written as the other val methods in Item_direct_view_ref: double val_real() { if (check_null_ref()) return 0; else return Item_direct_ref::val_real(); } A suggestion to make this kind of bugs a bit easier to avoid in the future: In Item_direct_ref, we should keep all val methods the same as we have in Item_ref, including adding a comment what we are overloading. I have attached a "fixed" patch to this Jira entry with the above changes. Feel free to push it after you have checked and agreed to my changes.
          monty Michael Widenius made changes -
          Attachment mdev-23826-new.diff [ 55616 ]

          See comments

          monty Michael Widenius added a comment - See comments
          monty Michael Widenius made changes -
          Status In Review [ 10002 ] Stalled [ 10000 ]
          varun Varun Gupta (Inactive) made changes -
          Assignee Michael Widenius [ monty ] Varun Gupta [ varun ]
          varun Varun Gupta (Inactive) made changes -
          Component/s Optimizer [ 10200 ]
          Fix Version/s 10.2.37 [ 25112 ]
          Fix Version/s 10.3.28 [ 25111 ]
          Fix Version/s 10.4.18 [ 25110 ]
          Fix Version/s 10.5.9 [ 25109 ]
          Fix Version/s 10.2 [ 14601 ]
          Fix Version/s 10.3 [ 22126 ]
          Fix Version/s 10.4 [ 22408 ]
          Fix Version/s 10.5 [ 23123 ]
          Resolution Fixed [ 1 ]
          Status Stalled [ 10000 ] Closed [ 6 ]
          serg Sergei Golubchik made changes -
          Workflow MariaDB v3 [ 113931 ] MariaDB v4 [ 158407 ]

          People

            varun Varun Gupta (Inactive)
            hholzgra Hartmut Holzgraefe
            Votes:
            0 Vote for this issue
            Watchers:
            4 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.