[MDEV-26965] Left join to derived table on multiple clauses with aggregation returns missing/incorrect results Created: 2021-11-03  Updated: 2022-02-01  Resolved: 2022-01-31

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.6.4, 10.3, 10.4, 10.5, 10.6, 10.7
Fix Version/s: 10.3.33, 10.4.23, 10.5.14, 10.6.6, 10.7.2

Type: Bug Priority: Critical
Reporter: Luke Cousins Assignee: Alice Sherepa
Resolution: Fixed Votes: 4
Labels: optimizer, regression
Environment:

Ubuntu Focal


Attachments: File dataset.sql     PNG File screenshot-1.png     PNG File screenshot-2.png     PNG File screenshot-3.png     PNG File screenshot-4.png    
Issue Links:
Duplicate
duplicates MDEV-27132 Wrong result from query when using sp... Closed
Relates
relates to MDEV-27132 Wrong result from query when using sp... Closed
relates to MDEV-27510 Query returns wrong result when using... Closed

 Description   

I couldn't work out how to reopen an issue, so sorry for making a new one.

The problem in MDEV-25714 and MDEV-21614 seems to be back in 10.6.4 (we recently upgraded from 10.5.6).

Using `SET optimizer_switch="split_materialized=off";` fixes the problem, so this is our temporary work-around.

Thanks for your help.



 Comments   
Comment by Alice Sherepa [ 2021-11-03 ]

Could you please add the test case to demonstrate the problem, tests from MDEV-25714 and MDEV-21614 return correct results on 10.6.4

Comment by Luke Cousins [ 2021-11-11 ]

Apologies for the delay. I will get a test case together for you. Our production query is very complicated, and I'm struggling to produce a simpler query that demonstrates the problem, but I'll make sure I get back to you soon with something suitable.

Comment by Matthieu Lombard [ 2022-01-06 ]

I have the same problem in version 10.3.32.
We just switch from 10.3.23 to 10.3.32.
I tried all versions between and problem seems to appear in 10.3.29 version. 10.3.28 is still good.
Using `SET optimizer_switch="split_materialized=off";` fixes the problem too.

To reproduce with the joined dataset.sql, try to execute this request :

SELECT m.id AS mission_id, a.applicants_users_id AS applicant_id
     ,sub.sent
FROM t_missions m
INNER JOIN t_applications a ON a.offers_id = m.offers_id
INNER JOIN t_user u ON u.id = a.applicants_users_id
LEFT JOIN (
    SELECT m2.id AS mission_id, a2.applicants_users_id AS applicant_id, TRUE as sent
    FROM t_missions m2
    INNER JOIN t_applications a2 ON a2.offers_id = m2.offers_id
        AND a2.application_date IS NOT NULL
    WHERE m2.mission_status = 2
    GROUP BY a2.offers_id, a2.applicants_users_id
) sub ON sub.applicant_id = a.applicants_users_id AND sub.mission_id = m.id;

It give us :

With JOIN clause in subquery : WRONG RESULT

Without some clauses in subquery, (comment `AND a2.application_date IS NOT NULL` or `WHERE...` or `ORDER BY...`) : GOOD RESULT

(In both case, subquery return same results, so final result should be the same too)

Without indexes in applications table, we got good result too.
Problem appear when LATERAL DERIVED select type is used.

Thanks for help !

Comment by Alice Sherepa [ 2022-01-31 ]

Matthieu Thanks! I repeated the bug, currently, it is fixed by the commit 97425f740faf83ac2d by Igor Bababev, MDEV-27132.

Comment by Igor Babaev [ 2022-01-31 ]

Closed as a duplicate of MDEV-27132 that is already fixed.

Comment by Igor Babaev [ 2022-01-31 ]

The patch for MDEV-27132 really fixes the problem with the above query. However the patch for MDEV-27510 provides a solution that covers a wider circle of queries that might employ the split optimization and return wrong result sets.

Comment by Matthieu Lombard [ 2022-02-01 ]

Thanks!

Generated at Thu Feb 08 09:49:18 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.