Skip to content

Commit 0fa789a

Browse files
author
Volodymyr Verovkin
committed
Bug#37560280 Incorrect behavior with EXPLAIN and subqueries
EXPLAIN could execute a stored function in a subquery when the subquery created a derived table, e.g.: EXPLAIN SELECT * FROM (SELECT f()) AS a; This issue was caused by Table_ref::materializable_is_const(), which incorrectly treated a function call as a constant expression. Now, we ensure that a derived table subquery containing a stored function call is not considered constant. Change-Id: Id73d567b3617da1523996e8ee0ed73b4879451ad (cherry picked from commit 2830b52f5515af4f7e9330492593446b215b004a)
1 parent c17d86b commit 0fa789a

File tree

12 files changed

+112
-46
lines changed

12 files changed

+112
-46
lines changed

mysql-test/r/partition_locking.result

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5030,9 +5030,9 @@ Handler_read_next 12
50305030
UNLOCK TABLES;
50315031
EXPLAIN SELECT * FROM t2 WHERE a = sf_a_from_t1b_d('1');
50325032
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
5033-
1 SIMPLE t2p1 constPRIMARY PRIMARY 4 const # 100.00NULL
5033+
1 SIMPLE t2p0,p1,p2 eq_refPRIMARY PRIMARY 4 const # 100.00Using where
50345034
Warnings:
5035-
Note 1003 /* select#1 */ select '1' AS `a`,'1' AS `b` from `test`.`t2` where true
5035+
Note 1003 /* select#1 */ select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where (`test`.`t2`.`a` = <cache>(`sf_a_from_t1b_d`('1')))
50365036
FLUSH STATUS;
50375037
START TRANSACTION;
50385038
SELECT * FROM t2 WHERE a = sf_a_from_t1b_d('1');
@@ -5086,9 +5086,9 @@ Handler_read_next 12
50865086
UNLOCK TABLES;
50875087
EXPLAIN SELECT * FROM t2 WHERE a = 7 + sf_a_from_t1b_d('1');
50885088
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
5089-
1 SIMPLE t2 p2constPRIMARY PRIMARY 4 const # 100.00NULL
5089+
1 SIMPLE t2p0,p1,p2eq_refPRIMARY PRIMARY 4 const # 100.00Using where
50905090
Warnings:
5091-
Note 1003 /* select#1 */ select '8' AS `a`,'8' AS `b` from `test`.`t2` where true
5091+
Note 1003 /* select#1 */ select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where (`test`.`t2`.`a` = <cache>((7 + `sf_a_from_t1b_d`('1'))))
50925092
FLUSH STATUS;
50935093
START TRANSACTION;
50945094
SELECT * FROM t2 WHERE a = 7 + sf_a_from_t1b_d('1');
@@ -5138,9 +5138,9 @@ Handler_read_next 7
51385138
UNLOCK TABLES;
51395139
EXPLAIN SELECT * FROM t2 WHERE a = sf_a_from_t1b_d('1') AND a = 2;
51405140
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
5141-
1 SIMPLENULL NULL NULL NULL NULL NULL NULL#NULL Impossible WHERE
5141+
1 SIMPLEt2 p2 const PRIMARY PRIMARY 4 const#100.00 NULL
51425142
Warnings:
5143-
Note 1003 /* select#1 */ select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where false
5143+
Note 1003 /* select#1 */ select '2' AS `a`,'2' AS `b` from `test`.`t2` where ((2 = `sf_a_from_t1b_d`('1')))
51445144
FLUSH STATUS;
51455145
START TRANSACTION;
51465146
SELECT * FROM t2 WHERE a = sf_a_from_t1b_d('1') AND a = 2;
@@ -5322,9 +5322,9 @@ Handler_read_next 12
53225322
UNLOCK TABLES;
53235323
EXPLAIN SELECT sf_add_1(a) - 1, sf_add_hello(b) FROM t2 WHERE a = sf_a_from_t1b_d('1');
53245324
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
5325-
1 SIMPLE t2p1 constPRIMARY PRIMARY 4 const # 100.00NULL
5325+
1 SIMPLE t2p0,p1,p2 eq_refPRIMARY PRIMARY 4 const # 100.00Using where
53265326
Warnings:
5327-
Note 1003 /* select#1 */ select (`sf_add_1`('1') - 1) AS `sf_add_1(a) - 1`,`sf_add_hello`('1') AS `sf_add_hello(b)` from `test`.`t2` where true
5327+
Note 1003 /* select#1 */ select (`sf_add_1`(`test`.`t2`.`a`) - 1) AS `sf_add_1(a) - 1`,`sf_add_hello`(`test`.`t2`.`b`) AS `sf_add_hello(b)` from `test`.`t2` where (`test`.`t2`.`a` = <cache>(`sf_a_from_t1b_d`('1')))
53285328
FLUSH STATUS;
53295329
START TRANSACTION;
53305330
SELECT sf_add_1(a) - 1, sf_add_hello(b) FROM t2 WHERE a = sf_a_from_t1b_d('1');
@@ -5378,9 +5378,9 @@ Handler_read_next 12
53785378
UNLOCK TABLES;
53795379
EXPLAIN SELECT sf_add_1(a) - 1, sf_add_hello(b) FROM t2 WHERE a = 7 + sf_a_from_t1b_d('1');
53805380
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
5381-
1 SIMPLE t2 p2constPRIMARY PRIMARY 4 const # 100.00NULL
5381+
1 SIMPLE t2p0,p1,p2eq_refPRIMARY PRIMARY 4 const # 100.00Using where
53825382
Warnings:
5383-
Note 1003 /* select#1 */ select (`sf_add_1`('8') - 1) AS `sf_add_1(a) - 1`,`sf_add_hello`('8') AS `sf_add_hello(b)` from `test`.`t2` where true
5383+
Note 1003 /* select#1 */ select (`sf_add_1`(`test`.`t2`.`a`) - 1) AS `sf_add_1(a) - 1`,`sf_add_hello`(`test`.`t2`.`b`) AS `sf_add_hello(b)` from `test`.`t2` where (`test`.`t2`.`a` = <cache>((7 + `sf_a_from_t1b_d`('1'))))
53845384
FLUSH STATUS;
53855385
START TRANSACTION;
53865386
SELECT sf_add_1(a) - 1, sf_add_hello(b) FROM t2 WHERE a = 7 + sf_a_from_t1b_d('1');
@@ -5430,9 +5430,9 @@ Handler_read_next 7
54305430
UNLOCK TABLES;
54315431
EXPLAIN SELECT sf_add_1(a) - 1, sf_add_hello(b) FROM t2 WHERE a = sf_a_from_t1b_d('1') AND a = 2;
54325432
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
5433-
1 SIMPLENULL NULL NULL NULL NULL NULL NULL#NULL Impossible WHERE
5433+
1 SIMPLEt2 p2 const PRIMARY PRIMARY 4 const#100.00 NULL
54345434
Warnings:
5435-
Note 1003 /* select#1 */ select (`sf_add_1`(`test`.`t2`.`a`) - 1) AS `sf_add_1(a) - 1`,`sf_add_hello`(`test`.`t2`.`b`) AS `sf_add_hello(b)` from `test`.`t2` where false
5435+
Note 1003 /* select#1 */ select (`sf_add_1`('2') - 1) AS `sf_add_1(a) - 1`,`sf_add_hello`('2') AS `sf_add_hello(b)` from `test`.`t2` where ((2 = `sf_a_from_t1b_d`('1')))
54365436
FLUSH STATUS;
54375437
START TRANSACTION;
54385438
SELECT sf_add_1(a) - 1, sf_add_hello(b) FROM t2 WHERE a = sf_a_from_t1b_d('1') AND a = 2;
@@ -5652,7 +5652,7 @@ ROLLBACK;
56525652
UNLOCK TABLES;
56535653
EXPLAIN UPDATE t2 SET b = CONCAT('+', b) WHERE a = sf_a_from_t1b_d('1');
56545654
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
5655-
1 UPDATE t2p1 range PRIMARYPRIMARY 4const# 100.00 Using where
5655+
1 UPDATE t2p0,p1,p2 index NULLPRIMARY 4NULL# 100.00 Using where
56565656
Warnings:
56575657
Note 1003 update `test`.`t2` set `test`.`t2`.`b` = concat('+',`test`.`t2`.`b`) where (`test`.`t2`.`a` = `sf_a_from_t1b_d`('1'))
56585658
FLUSH STATUS;
@@ -5734,7 +5734,7 @@ ROLLBACK;
57345734
UNLOCK TABLES;
57355735
EXPLAIN UPDATE t2 SET b = CONCAT('+', b) WHERE a = 7 + sf_a_from_t1b_d('1');
57365736
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
5737-
1 UPDATE t2 p2range PRIMARYPRIMARY 4const# 100.00 Using where
5737+
1 UPDATE t2p0,p1,p2index NULLPRIMARY 4NULL# 100.00 Using where
57385738
Warnings:
57395739
Note 1003 update `test`.`t2` set `test`.`t2`.`b` = concat('+',`test`.`t2`.`b`) where (`test`.`t2`.`a` = (7 + `sf_a_from_t1b_d`('1')))
57405740
FLUSH STATUS;
@@ -5808,9 +5808,9 @@ ROLLBACK;
58085808
UNLOCK TABLES;
58095809
EXPLAIN UPDATE t2 SET b = CONCAT('+', b) WHERE a = sf_a_from_t1b_d('1') AND a = 2;
58105810
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
5811-
1 UPDATENULL NULL NULL NULL NULL NULL NULL#NULL Impossible WHERE
5811+
1 UPDATEt2 p2 range PRIMARY PRIMARY 4 const#100.00 Using where
58125812
Warnings:
5813-
Note 1003 update `test`.`t2` set `test`.`t2`.`b` = concat('+',`test`.`t2`.`b`) where ()
5813+
Note 1003 update `test`.`t2` set `test`.`t2`.`b` = concat('+',`test`.`t2`.`b`) where ((`test`.`t2`.`a` = 2) and (2 = `sf_a_from_t1b_d`('1')))
58145814
FLUSH STATUS;
58155815
START TRANSACTION;
58165816
UPDATE t2 SET b = CONCAT('+', b) WHERE a = sf_a_from_t1b_d('1') AND a = 2;
@@ -6064,7 +6064,7 @@ ROLLBACK;
60646064
UNLOCK TABLES;
60656065
EXPLAIN UPDATE t2 SET b = sf_add_hello(b) WHERE a = sf_a_from_t1b_d('1');
60666066
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
6067-
1 UPDATE t2p1 range PRIMARYPRIMARY 4const# 100.00 Using where
6067+
1 UPDATE t2p0,p1,p2 index NULLPRIMARY 4NULL# 100.00 Using where
60686068
Warnings:
60696069
Note 1003 update `test`.`t2` set `test`.`t2`.`b` = `sf_add_hello`(`test`.`t2`.`b`) where (`test`.`t2`.`a` = `sf_a_from_t1b_d`('1'))
60706070
FLUSH STATUS;
@@ -6146,7 +6146,7 @@ ROLLBACK;
61466146
UNLOCK TABLES;
61476147
EXPLAIN UPDATE t2 SET b = sf_add_hello(b) WHERE a = 7 + sf_a_from_t1b_d('1');
61486148
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
6149-
1 UPDATE t2 p2range PRIMARYPRIMARY 4const# 100.00 Using where
6149+
1 UPDATE t2p0,p1,p2index NULLPRIMARY 4NULL# 100.00 Using where
61506150
Warnings:
61516151
Note 1003 update `test`.`t2` set `test`.`t2`.`b` = `sf_add_hello`(`test`.`t2`.`b`) where (`test`.`t2`.`a` = (7 + `sf_a_from_t1b_d`('1')))
61526152
FLUSH STATUS;
@@ -6220,9 +6220,9 @@ ROLLBACK;
62206220
UNLOCK TABLES;
62216221
EXPLAIN UPDATE t2 SET b = sf_add_hello(b) WHERE a = sf_a_from_t1b_d('1') AND a = 2;
62226222
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
6223-
1 UPDATENULL NULL NULL NULL NULL NULL NULL#NULL Impossible WHERE
6223+
1 UPDATEt2 p2 range PRIMARY PRIMARY 4 const#100.00 Using where
62246224
Warnings:
6225-
Note 1003 update `test`.`t2` set `test`.`t2`.`b` = `sf_add_hello`(`test`.`t2`.`b`) where ()
6225+
Note 1003 update `test`.`t2` set `test`.`t2`.`b` = `sf_add_hello`(`test`.`t2`.`b`) where ((`test`.`t2`.`a` = 2) and (2 = `sf_a_from_t1b_d`('1')))
62266226
FLUSH STATUS;
62276227
START TRANSACTION;
62286228
UPDATE t2 SET b = sf_add_hello(b) WHERE a = sf_a_from_t1b_d('1') AND a = 2;
@@ -6482,7 +6482,7 @@ ROLLBACK;
64826482
UNLOCK TABLES;
64836483
EXPLAIN UPDATE t2 SET a = sf_add_1(a) + 4 WHERE a = sf_a_from_t1b_d('1');
64846484
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
6485-
1 UPDATE t2p1 range PRIMARYPRIMARY 4const# 100.00 Using where
6485+
1 UPDATE t2p0,p1,p2 index NULLPRIMARY 4NULL# 100.00 Using where; Using temporary
64866486
Warnings:
64876487
Note 1003 update `test`.`t2` set `test`.`t2`.`a` = (`sf_add_1`(`test`.`t2`.`a`) + 4) where (`test`.`t2`.`a` = `sf_a_from_t1b_d`('1'))
64886488
FLUSH STATUS;
@@ -6568,7 +6568,7 @@ ROLLBACK;
65686568
UNLOCK TABLES;
65696569
EXPLAIN UPDATE t2 SET a = sf_add_1(a) + 4 WHERE a = 7 + sf_a_from_t1b_d('1');
65706570
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
6571-
1 UPDATE t2 p2range PRIMARYPRIMARY 4const# 100.00 Using where
6571+
1 UPDATE t2p0,p1,p2index NULLPRIMARY 4NULL# 100.00 Using where; Using temporary
65726572
Warnings:
65736573
Note 1003 update `test`.`t2` set `test`.`t2`.`a` = (`sf_add_1`(`test`.`t2`.`a`) + 4) where (`test`.`t2`.`a` = (7 + `sf_a_from_t1b_d`('1')))
65746574
FLUSH STATUS;
@@ -6644,9 +6644,9 @@ ROLLBACK;
66446644
UNLOCK TABLES;
66456645
EXPLAIN UPDATE t2 SET a = sf_add_1(a) + 4 WHERE a = sf_a_from_t1b_d('1') AND a = 2;
66466646
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
6647-
1 UPDATENULL NULL NULL NULL NULL NULL NULL#NULL Impossible WHERE
6647+
1 UPDATEt2 p2 range PRIMARY PRIMARY 4 const#100.00 Using where
66486648
Warnings:
6649-
Note 1003 update `test`.`t2` set `test`.`t2`.`a` = (`sf_add_1`(`test`.`t2`.`a`) + 4) where ()
6649+
Note 1003 update `test`.`t2` set `test`.`t2`.`a` = (`sf_add_1`(`test`.`t2`.`a`) + 4) where ((`test`.`t2`.`a` = 2) and (2 = `sf_a_from_t1b_d`('1')))
66506650
FLUSH STATUS;
66516651
START TRANSACTION;
66526652
UPDATE t2 SET a = sf_add_1(a) + 4 WHERE a = sf_a_from_t1b_d('1') AND a = 2;
@@ -6899,7 +6899,7 @@ ROLLBACK;
68996899
UNLOCK TABLES;
69006900
EXPLAIN DELETE FROM t2 WHERE a = sf_a_from_t1b_d('1');
69016901
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
6902-
1 DELETE t2p1 range PRIMARY PRIMARY 4 const# 100.00 Using where
6902+
1 DELETE t2p0,p1,p2 ALL NULL NULL NULL NULL# 100.00 Using where
69036903
Warnings:
69046904
Note 1003 delete from `test`.`t2` where (`test`.`t2`.`a` = `sf_a_from_t1b_d`('1'))
69056905
FLUSH STATUS;
@@ -6977,7 +6977,7 @@ ROLLBACK;
69776977
UNLOCK TABLES;
69786978
EXPLAIN DELETE FROM t2 WHERE a = 7 + sf_a_from_t1b_d('1');
69796979
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
6980-
1 DELETE t2 p2range PRIMARY PRIMARY 4 const# 100.00 Using where
6980+
1 DELETE t2p0,p1,p2ALL NULL NULL NULL NULL# 100.00 Using where
69816981
Warnings:
69826982
Note 1003 delete from `test`.`t2` where (`test`.`t2`.`a` = (7 + `sf_a_from_t1b_d`('1')))
69836983
FLUSH STATUS;
@@ -7049,9 +7049,9 @@ ROLLBACK;
70497049
UNLOCK TABLES;
70507050
EXPLAIN DELETE FROM t2 WHERE a = sf_a_from_t1b_d('1') AND a = 2;
70517051
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
7052-
1 DELETENULL NULL NULL NULL NULL NULL NULL#NULL Impossible WHERE
7052+
1 DELETEt2 p2 range PRIMARY PRIMARY 4 const#100.00 Using where
70537053
Warnings:
7054-
Note 1003 delete from `test`.`t2` where ()
7054+
Note 1003 delete from `test`.`t2` where ((`test`.`t2`.`a` = 2) and (2 = `sf_a_from_t1b_d`('1')))
70557055
FLUSH STATUS;
70567056
START TRANSACTION;
70577057
DELETE FROM t2 WHERE a = sf_a_from_t1b_d('1') AND a = 2;

mysql-test/r/sp.result

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6377,24 +6377,24 @@ Warnings:
63776377
Note 1003 /* select#1 */ select `test`.`t1`.`c1` AS `c1` from `test`.`t1` where (`test`.`t1`.`c1` = 1)
63786378
EXPLAIN SELECT * FROM t1 WHERE c1=f1();
63796379
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
6380-
1 SIMPLE t1 NULL ref c1 c1 5 const 1 100.00 Using index
6380+
1 SIMPLE t1 NULL ref c1 c1 5 const 1 100.00 Using where; Using index
63816381
Warnings:
6382-
Note 1003 /* select#1 */ select `test`.`t1`.`c1` AS `c1` from `test`.`t1` where (`test`.`t1`.`c1` = `f1`())
6382+
Note 1003 /* select#1 */ select `test`.`t1`.`c1` AS `c1` from `test`.`t1` where (`test`.`t1`.`c1` = <cache>(`f1`()))
63836383
EXPLAIN SELECT * FROM v1 WHERE c1=1;
63846384
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
63856385
1 SIMPLE t1 NULL ref c1 c1 5 const 1 100.00 Using index
63866386
Warnings:
63876387
Note 1003 /* select#1 */ select `test`.`t1`.`c1` AS `c1` from `test`.`t1` where (`test`.`t1`.`c1` = 1)
63886388
EXPLAIN SELECT * FROM v1 WHERE c1=f1();
63896389
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
6390-
1 SIMPLE t1 NULL ref c1 c1 5 const 1 100.00 Using index
6390+
1 SIMPLE t1 NULL ref c1 c1 5 const 1 100.00 Using where; Using index
63916391
Warnings:
6392-
Note 1003 /* select#1 */ select `test`.`t1`.`c1` AS `c1` from `test`.`t1` where (`test`.`t1`.`c1` = `f1`())
6392+
Note 1003 /* select#1 */ select `test`.`t1`.`c1` AS `c1` from `test`.`t1` where (`test`.`t1`.`c1` = <cache>(`f1`()))
63936393
EXPLAIN SELECT * FROM t1 WHERE c1=f2(10);
63946394
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
6395-
1 SIMPLE t1 NULL ref c1 c1 5 const 1 100.00 Using index
6395+
1 SIMPLE t1 NULL ref c1 c1 5 const 1 100.00 Using where; Using index
63966396
Warnings:
6397-
Note 1003 /* select#1 */ select `test`.`t1`.`c1` AS `c1` from `test`.`t1` where (`test`.`t1`.`c1` = `f2`(10))
6397+
Note 1003 /* select#1 */ select `test`.`t1`.`c1` AS `c1` from `test`.`t1` where (`test`.`t1`.`c1` = <cache>(`f2`(10)))
63986398
EXPLAIN SELECT * FROM t1 WHERE c1=f2(c1);
63996399
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
64006400
1 SIMPLE t1 NULL index NULL c1 5 NULL 5 20.00 Using where; Using index

mysql-test/t/partition_locking.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2938,6 +2938,11 @@ DROP TABLE t1, t2;
29382938
--echo #
29392939
--echo # Test subqueries/stored functions with UPDATE/DELETE/SELECT
29402940
--echo #
2941+
# Bug#37560280:
2942+
# The EXPLAIN plan for queries with the sf_a_from_t1b_d() function call
2943+
# differs from the actual execution plan because constant folding
2944+
# is performed in the actual plan but not in EXPLAIN.
2945+
29412946
CREATE TABLE tq (id int PRIMARY KEY auto_increment, query varchar(255), not_select tinyint);
29422947
CREATE TABLE tsq (id int PRIMARY KEY auto_increment, subquery varchar(255), can_be_locked tinyint);
29432948
CREATE TABLE t1 (a int, b varchar(255), PRIMARY KEY (a), KEY (b))

sql/item.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3594,6 +3594,15 @@ inline bool WalkItem(Item *item, enum_walk walk, T &&functor) {
35943594
reinterpret_cast<uchar *>(&functor));
35953595
}
35963596

3597+
/**
3598+
Overload for const 'item' and functor taking 'const Item*' argument.
3599+
*/
3600+
template <class T>
3601+
inline bool WalkItem(const Item *item, enum_walk walk, T &&functor) {
3602+
auto to_const = [&](const Item *descendant) { return functor(descendant); };
3603+
return WalkItem(const_cast<Item *>(item), walk, to_const);
3604+
}
3605+
35973606
/**
35983607
Same as WalkItem, but for Item::compile(). Use as e.g.:
35993608

sql/item_func.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8192,6 +8192,8 @@ bool Item_func_sp::val_json(Json_wrapper *result) {
81928192
bool Item_func_sp::execute() {
81938193
THD *thd = current_thd;
81948194

8195+
assert(!thd->lex->is_explain() || thd->lex->is_explain_analyze);
8196+
81958197
Internal_error_handler_holder<View_error_handler, Table_ref> view_handler(
81968198
thd, context->view_error_handler, context->view_error_handler_arg);
81978199

sql/sql_derived.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,7 @@ bool Table_ref::optimize_derived(THD *thd) {
16231623
// at execution time (in fact, it will get confused and crash if it has
16241624
// already been materialized).
16251625
if (!thd->lex->using_hypergraph_optimizer()) {
1626-
if (materializable_is_const() &&
1626+
if (materializable_is_const(thd) &&
16271627
(create_materialized_table(thd) || materialize_derived(thd)))
16281628
return true;
16291629
}

sql/sql_lex.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,8 @@ class Query_expression {
726726
bool prepared; ///< All query blocks in query expression are prepared
727727
bool optimized; ///< All query blocks in query expression are optimized
728728
bool executed; ///< Query expression has been executed
729+
///< Explain mode: query expression refers stored function
730+
bool m_has_stored_program{false};
729731

730732
/// Object to which the result for this query expression is sent.
731733
/// Not used if we materialize directly into a parent query expression's
@@ -770,6 +772,8 @@ class Query_expression {
770772
/// multi-level ORDER, i.e. we have a "simple table".
771773
bool is_simple() const { return m_query_term->term_type() == QT_QUERY_BLOCK; }
772774

775+
bool has_stored_program() const { return m_has_stored_program; }
776+
773777
/// Values for Query_expression::cleaned
774778
enum enum_clean_state {
775779
UC_DIRTY, ///< Unit isn't cleaned
@@ -4825,4 +4829,12 @@ bool accept_for_join(mem_root_deque<Table_ref *> *tables,
48254829
Table_ref *nest_join(THD *thd, Query_block *select, Table_ref *embedding,
48264830
mem_root_deque<Table_ref *> *jlist, size_t table_cnt,
48274831
const char *legend);
4832+
4833+
template <typename T>
4834+
inline bool WalkQueryExpression(Query_expression *query_expr, enum_walk walk,
4835+
T &&functor) {
4836+
return query_expr->walk(&Item::walk_helper_thunk<T>, walk,
4837+
reinterpret_cast<uchar *>(&functor));
4838+
}
4839+
48284840
#endif /* SQL_LEX_INCLUDED */

0 commit comments

Comments
 (0)