-
Sreeharsha Ramanavarapu authored
AND ORDER BY IN A SUBQUERY Issue: ------ prepare xx from "SELECT 1 AS countSelected FROM (SELECT 2, ........ FROM ....... ORDER BY (SELECT 3 .........)) AS t2 ORDER BY countSelected"; In conventional execution: 1) "SELECT 3" is removed by Item_singlerow_subselect::select_transformer(). This subquery is replaced with an Item_int. 2) "SELECT 2 ..." is invalidated as part of the derived_merge strategy by SELECT_LEX::merge_derived. During Prepare: 1) "SELECT 3" can't be removed by Item_singlerow_subselect::select_transformer because of the special condition "thd->stmt_arena->is_stmt_prepare_or_first_sp_execute()". So this subquery is not removed.. 2) "SELECT 2 ..." is invalidated as part of the derived_merge strategy by SELECT_LEX::merge_derived. This makes "SELECT 3" an orphan and causes incorrect behavior during EXECUTE. Solution: --------- When a derived table is merged, the ORDER BY clause in the derived table should either be moved into the outer query block or removed. In this case, it can't moved into the outer query since there is already an ORDER BY clause in the outer query. So the derived table's ORDER BY should be eliminated. This part has now been implemented.
Sreeharsha Ramanavarapu authoredAND ORDER BY IN A SUBQUERY Issue: ------ prepare xx from "SELECT 1 AS countSelected FROM (SELECT 2, ........ FROM ....... ORDER BY (SELECT 3 .........)) AS t2 ORDER BY countSelected"; In conventional execution: 1) "SELECT 3" is removed by Item_singlerow_subselect::select_transformer(). This subquery is replaced with an Item_int. 2) "SELECT 2 ..." is invalidated as part of the derived_merge strategy by SELECT_LEX::merge_derived. During Prepare: 1) "SELECT 3" can't be removed by Item_singlerow_subselect::select_transformer because of the special condition "thd->stmt_arena->is_stmt_prepare_or_first_sp_execute()". So this subquery is not removed.. 2) "SELECT 2 ..." is invalidated as part of the derived_merge strategy by SELECT_LEX::merge_derived. This makes "SELECT 3" an orphan and causes incorrect behavior during EXECUTE. Solution: --------- When a derived table is merged, the ORDER BY clause in the derived table should either be moved into the outer query block or removed. In this case, it can't moved into the outer query since there is already an ORDER BY clause in the outer query. So the derived table's ORDER BY should be eliminated. This part has now been implemented.
Loading