[MDEV-19269] Pushdown into IN subquery is not made on the second execution of stmt Created: 2019-04-17  Updated: 2023-12-05

Status: Stalled
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.4
Fix Version/s: 10.4

Type: Bug Priority: Major
Reporter: Galina Shalygina (Inactive) Assignee: Igor Babaev
Resolution: Unresolved Votes: 0
Labels: None


 Description   

create table t1 (a int, b int);
create table t2 (x int, y int);
 
insert into t1 values (1,1),(2,2);
insert into t2 values (1,1),(2,2),(2,3);
 
prepare stmt from "
EXPLAIN FORMAT=JSON
SELECT * FROM t1
WHERE a = b 
      AND (a,b) IN (SELECT t2.x, COUNT(t2.y) FROM t2 WHERE @a=1 GROUP BY t2.x);";
 
set @a=2; 
execute stmt;
set @a=1;
execute stmt;

MariaDB [test]> prepare stmt from "
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE a = b 
      AND (a,b) IN (SELECT t2.x, COUNT(t2.y) FROM t2 WHERE @a=1 GROUP BY t2.x);";
Query OK, 0 rows affected (0.023 sec)
Statement prepared
 
MariaDB [test]> set @a=2;
Query OK, 0 rows affected (0.001 sec)
 
MariaDB [test]> execute stmt;
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN                                                                                                                                                                                                                                                                                                                       |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| {
  "query_block": {
    "select_id": 1,
    "table": {
      "message": "Impossible WHERE noticed after reading const tables"
    },
    "subqueries": [
      {
        "query_block": {
          "select_id": 2,
          "table": {
            "message": "Impossible WHERE"
          }
        }
      }
    ]
  }
} |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (2.257 sec)
 
MariaDB [test]> set @a=1;
Query OK, 0 rows affected (0.000 sec)
 
MariaDB [test]> execute stmt;
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| {
  "query_block": {
    "select_id": 1,
    "table": {
      "table_name": "t1",
      "access_type": "ALL",
      "rows": 4,
      "filtered": 100,
      "attached_condition": "t1.b = t1.a and t1.a is not null and t1.a is not null"
    },
    "table": {
      "table_name": "<subquery2>",
      "access_type": "eq_ref",
      "possible_keys": ["distinct_key"],
      "key": "distinct_key",
      "key_length": "12",
      "used_key_parts": ["x", "COUNT(t2.y)"],
      "ref": ["test.t1.a", "test.t1.a"],
      "rows": 1,
      "filtered": 100,
      "attached_condition": "t1.a = `<subquery2>`.`COUNT(t2.y)`",
      "materialized": {
        "unique": 1,
        "query_block": {
          "select_id": 2,
          "temporary_table": {
            "table": {
              "table_name": "t2",
              "access_type": "ALL",
              "rows": 6,
              "filtered": 100
            }
          }
        }
      }
    }
  }
} |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.166 sec)

For the second execution pushdown into IN subquery is not made while it should be made. Condition should be pushed down into HAVING.

How it looks if to run stmt with @a=1 in the first execution:

MariaDB [test]> execute stmt;
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| {
  "query_block": {
    "select_id": 1,
    "table": {
      "table_name": "t1",
      "access_type": "ALL",
      "rows": 4,
      "filtered": 100,
      "attached_condition": "t1.b = t1.a and t1.a is not null and t1.a is not null"
    },
    "table": {
      "table_name": "<subquery2>",
      "access_type": "eq_ref",
      "possible_keys": ["distinct_key"],
      "key": "distinct_key",
      "key_length": "12",
      "used_key_parts": ["x", "COUNT(t2.y)"],
      "ref": ["test.t1.a", "test.t1.a"],
      "rows": 1,
      "filtered": 100,
      "attached_condition": "t1.a = `<subquery2>`.`COUNT(t2.y)`",
      "materialized": {
        "unique": 1,
        "query_block": {
          "select_id": 2,
          "having_condition": "`COUNT(t2.y)` = t2.x",
          "temporary_table": {
            "table": {
              "table_name": "t2",
              "access_type": "ALL",
              "rows": 6,
              "filtered": 100
            }
          }
        }
      }
    }
  }
} |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (4.812 sec)



 Comments   
Comment by Galina Shalygina (Inactive) [ 2019-04-17 ]

(Was found during the work on MDEV-17362 and copied from its comments section)

For the first stmt execution pushdown into IN subquery is made. For the second, third, … stmt executions pushdown into IN subquery is not made while it should be made.

It happens because after the first execution of stmt is_jtbm_const_tab field is set for IN subquery.
It remains the same (true) for the second execution while it should be reset.
If is_jtbm_const_tab is set (subquery is degenerated) pushdown into IN subquery is not made. So as is_jtbm_const_tab remains set after the first execution pushdown is not made for the second, ... executions.

Explanation:
What is done on the first execution of stmt statement when @a=2:

1. Pushdown into IN subquery is made. Condition from WHERE clause is saved to be pushed into IN subquery later. It is saved in cond_pushed_into_where field.

2. IN subquery is optimized. cond_pushed_into_where is pushed into IN subqueries WHERE clause.

3. IN subquery is executed. It is said to be degenerated and special flag is_jtbm_const_tab is set.

On the second execution of stmt statement when @a=1:

On the 1 step it is checked if is_jtbm_const_tab is set. And it is set, it remains the same after the first execution while subquery is not degenerated. So pushdown into IN subquery is not made while is should be made.

Comment by Igor Babaev [ 2019-04-20 ]

Ok to push the patch.

Comment by Daniel Black [ 2021-02-11 ]

So there's an unpushed patch somewhere. No MDEV-19269 mentioned in git history.

Comment by Julien Fritsch [ 2023-12-05 ]

Automated message:
----------------------------
Since this issue has not been updated since 6 weeks, it's time to move it back to Stalled.

Comment by JiraAutomate [ 2023-12-05 ]

Automated message:
----------------------------
Since this issue has not been updated since 6 weeks, it's time to move it back to Stalled.

Generated at Thu Feb 08 08:50:20 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.