[MDEV-12519] NOT IN subquery predicate with recursive reference is ignored Created: 2017-04-18  Updated: 2017-04-22  Resolved: 2017-04-22

Status: Closed
Project: MariaDB Server
Component/s: Optimizer - CTE
Affects Version/s: 10.2.5
Fix Version/s: 10.2.6

Type: Bug Priority: Major
Reporter: Igor Babaev Assignee: Igor Babaev
Resolution: Fixed Votes: 0
Labels: None


 Description   

If in a CTE specification the WHERE condition of a recursive SELECT contains NOT IN subquery predicate with recursive reference then this predicate is completely ignored when the query that uses this CTE is executed.

This problem can be demonstrated with the following simple test case.

create table t1 (lp char(4) not null, rp char(4) not null);
insert into t1 values
   ('p1','p2'), ('p2','p3'), ('p3','p4'), ('p4','p5');
 
set standard_compliant_cte=0;
 
with recursive
reachables(p) as 
( 
   select lp from t1 where lp = 'p1'
   union 
   select t1.rp from reachables, t1 
       where t1.lp = reachables.p
)
select * from reachables;
 
with recursive 
reachables(p) as
(
   select lp from t1 where lp = 'p1'
   union 
   select t1.rp from reachables, t1 
      where 'p3' not in (select * from reachables) and
                 t1.lp = reachables.p
)
select * from reachables;

While for the first query the result set is correct:

MariaDB [test]> with recursive
    -> reachables(p) as 
    -> ( 
    ->    select lp from t1 where lp = 'p1'
    ->    union 
    ->    select t1.rp from reachables, t1 
    ->        where t1.lp = reachables.p
    -> )
    -> select * from reachables;
+----+
| p  |
+----+
| p1 |
| p2 |
| p3 |
| p4 |
| p5 |
+----+

the output from the second query is not what is expected:

MariaDB [test]> with recursive 
    -> reachables(p) as
    -> (
    ->    select lp from t1 where lp = 'p1'
    ->    union 
    ->    select t1.rp from reachables, t1 
    ->       where 'p3' not in (select * from reachables) and
    ->                  t1.lp = reachables.p
    -> )
    -> select * from reachables;
+----+
| p  |
+----+
| p1 |
| p2 |
| p3 |
| p4 |
| p5 |
+----+

Expected:

MariaDB [test]> with recursive 
    -> reachables(p) as
    -> (
    ->    select lp from t1 where lp = 'p1'
    ->    union 
    ->    select t1.rp from reachables, t1 
    ->       where 'p3' not in (select * from reachables) and
    ->                  t1.lp = reachables.p
    -> )
    -> select * from reachables;
+----+
| p  |
+----+
| p1 |
| p2 |
| p3 |
+----+



 Comments   
Comment by Igor Babaev [ 2017-04-18 ]

A possible cause of the problem can be seen here:

MariaDB [test]> explain format=json
    -> with recursive 
    -> reachables(p) as
    -> (
    ->    select lp from t1 where lp = 'p1'
    ->    union 
    ->    select t1.rp from reachables, t1 
    ->       where 'p3' not in (select * from reachables) and
    ->                  t1.lp = reachables.p
    -> )
    -> select * from reachables;
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| {
  "query_block": {
    "select_id": 1,
    "table": {
      "table_name": "<derived2>",
      "access_type": "ALL",
      "rows": 4,
      "filtered": 100,
      "materialized": {
        "query_block": {
          "recursive_union": {
            "table_name": "<union2,3>",
            "access_type": "ALL",
            "query_specifications": [
              {
                "query_block": {
                  "select_id": 2,
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "rows": 4,
                    "filtered": 100,
                    "attached_condition": "t1.lp = 'p1'"
                  }
                }
              },
              {
                "query_block": {
                  "select_id": 3,
                  "const_condition": "<cache>(!<in_optimizer>('p3',<exists>(subquery#4)))",
                  "table": {
                    "table_name": "t1",
                    "access_type": "ALL",
                    "rows": 4,
                    "filtered": 100
                  },
                  "table": {
                    "table_name": "<derived2>",
                    "access_type": "ref",
                    "possible_keys": ["key0"],
                    "key": "key0",
                    "key_length": "4",
                    "used_key_parts": ["p"],
                    "ref": ["test.t1.lp"],
                    "rows": 2,
                    "filtered": 100
                  },
                  "subqueries": [
                    {
                      "query_block": {
                        "select_id": 4,
                        "table": {
                          "table_name": "<derived2>",
                          "access_type": "ALL",
                          "rows": 4,
                          "filtered": 100,
                          "attached_condition": "<cache>(convert('p3' using latin1)) = reachables.p"
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    }
  }
} |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

The predicate

'p3' not in (select * from reachables)

is considered as a constant condition. It is evaluated only once at the first iteration. Then its result is cached and reused by each next iteration.

Comment by Oleksandr Byelkin [ 2017-04-21 ]

OK to push

Comment by Igor Babaev [ 2017-04-22 ]

The fix for this bug was pushed into the 10.2 tree

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