[MDEV-6786] Wrong join query result with outer_join_with_cache = off Created: 2014-09-25  Updated: 2015-02-19  Resolved: 2015-02-19

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.0.10, 10.0.11, 10.0.13, 10.1.0
Fix Version/s: 10.0.16

Type: Bug Priority: Major
Reporter: Igor Kosulin Assignee: Sergei Petrunia
Resolution: Fixed Votes: 0
Labels: optimizer, outer-join
Environment:

OS: doesn't matter (works on both Linux or Windows)
Hardware: doesn't matter


Issue Links:
Relates
relates to MDEV-5719 Wrong result with GROUP BY and LEFT O... Closed

 Description   

Reproducing

  1. Create two simple tables and fill them with rows:

    CREATE TABLE `table1` (
        `id` INT AUTO_INCREMENT,
        `link` INT,
        `field1` INT,
        PRIMARY KEY (`id`)
    )
    ENGINE=InnoDB;
     
    CREATE TABLE `table2` (
        `id` INT AUTO_INCREMENT,
        `link` INT,
        `field2` INT,
        PRIMARY KEY (`id`)
    )
    ENGINE=InnoDB;
     
    INSERT INTO `table1` (`link`, `field1`) VALUES (3, 1);
    INSERT INTO `table1` (`link`, `field1`) VALUES (1, 2);
    INSERT INTO `table1` (`link`, `field1`) VALUES (2, 3);
     
    INSERT INTO `table2` (`link`, `field2`) VALUES (2, 1);
    INSERT INTO `table2` (`link`, `field2`) VALUES (3, 1);

  2. (optional) Enable outer_join_with_cache optimization, if it's disabled by default:

    SET SESSION optimizer_switch='outer_join_with_cache=on';

  3. Select all rows, joining by `link`, ordering by `field1`, using group_concat function in select query:

    select
        field1, t1.link, field2, group_concat('any string or field')
    from table1 t1
        left join table2 t2 on t1.link = t2.link
    group by field1

    Notice everything works fine now:

    field1 link field2 group_concat
    1 3 1 any string of field
    2 1 NULL any string of field
    3 2 1 any string of field
  4. Disable outer_join_with_cache optimization:

    SET SESSION optimizer_switch='outer_join_with_cache=off';

  5. Repeat step 3.
    Notice that the result of the query has been changed: there is NULL value instead of 1 - it's wrong:
    field1 link field2 group_concat
    1 3 NULL any string of field
    2 1 NULL any string of field
    3 2 1 any string of field
  6. If your database is small you can control this behavior by switching outer_join_with_cache on and off, otherwise it seems like outer_join_with_cache is always turned off, so it always reproduces on big tables, whether outer_join_with_cache is on or off.

Other points

  1. Using group_concat seems to be a necessary condition for reproducing this bug.
  2. If you will order the query in step 3 by `t1.link`:

    select
        field1, t1.link, field2, group_concat('any string or field')
    from table1 t1
        left join table2 t2 on t1.link = t2.link
    group by field1
    order by t1.link

    everything will work fine:

    field1 link field2 group_concat
    2 1 NULL any string of field
    3 2 1 any string of field
    1 3 1 any string of field


 Comments   
Comment by Elena Stepanova [ 2014-09-26 ]

Thanks for the report and for the complete test case.

The problem is probably the same as, or related to, MDEV-5719. I'm assigning it to psergey to check the provided test case after the fix for MDEV-5719.

Comment by Sergei Petrunia [ 2015-02-19 ]

Yes, confirm - this is the same problem as MDEV-5719 (which is fixed).

Generated at Thu Feb 08 07:14:34 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.