Skip to content

Commit 26f5033

Browse files
author
Varun Gupta
committed
MDEV-23449: alias do not exist and a query do not report an error
For an IN/ANY/ALL subquery without an aggregate function and HAVING clause, the GROUP BY clause is removed. Due to the GROUP BY list being removed, the invalid reference in the GROUP BY clause was never resolved. Remove the GROUP BY list only when the all the items in the GROUP BY list are resolved. Also removing the GROUP BY list later would not affect the extension that allows using non-aggregated field in an aggregate function (when ONLY_FULL_GROUP_BY is not set) because the GROUP BY list is removed only when their is NO aggregate function in IN/ALL/ANY subquery.
1 parent 072b39d commit 26f5033

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

mysql-test/r/subselect4.result

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,4 +2686,12 @@ SELECT * FROM t2;
26862686
f
26872687
bar
26882688
DROP TABLE t1, t2;
2689+
#
2690+
# MDEV-23449: alias do not exist and a query do not report an error
2691+
#
2692+
CREATE TABLE t1(a INT, b INT);
2693+
INSERT INTO t1 VALUES (1,1), (2,2), (3,3), (4,4), (5,5);
2694+
SELECT a, b FROM t1 WHERE a IN (SELECT A.a FROM t1 A GROUP BY s.id);
2695+
ERROR 42S22: Unknown column 's.id' in 'group statement'
2696+
DROP TABLE t1;
26892697
# End of 10.2 tests

mysql-test/t/subselect4.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,4 +2201,15 @@ SELECT * FROM t2;
22012201

22022202
DROP TABLE t1, t2;
22032203

2204+
--echo #
2205+
--echo # MDEV-23449: alias do not exist and a query do not report an error
2206+
--echo #
2207+
2208+
CREATE TABLE t1(a INT, b INT);
2209+
INSERT INTO t1 VALUES (1,1), (2,2), (3,3), (4,4), (5,5);
2210+
2211+
--error ER_BAD_FIELD_ERROR
2212+
SELECT a, b FROM t1 WHERE a IN (SELECT A.a FROM t1 A GROUP BY s.id);
2213+
DROP TABLE t1;
2214+
22042215
--echo # End of 10.2 tests

sql/sql_select.cc

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -729,22 +729,6 @@ JOIN::prepare(TABLE_LIST *tables_init,
729729
tables_list, select_lex->leaf_tables,
730730
FALSE, SELECT_ACL, SELECT_ACL, FALSE))
731731
DBUG_RETURN(-1);
732-
733-
/*
734-
Permanently remove redundant parts from the query if
735-
1) This is a subquery
736-
2) This is the first time this query is optimized (since the
737-
transformation is permanent
738-
3) Not normalizing a view. Removal should take place when a
739-
query involving a view is optimized, not when the view
740-
is created
741-
*/
742-
if (select_lex->master_unit()->item && // 1)
743-
select_lex->first_cond_optimization && // 2)
744-
!thd->lex->is_view_context_analysis()) // 3)
745-
{
746-
remove_redundant_subquery_clauses(select_lex);
747-
}
748732

749733
/*
750734
TRUE if the SELECT list mixes elements with and without grouping,
@@ -824,6 +808,23 @@ JOIN::prepare(TABLE_LIST *tables_init,
824808
&hidden_group_fields,
825809
&select_lex->select_n_reserved))
826810
DBUG_RETURN(-1);
811+
812+
/*
813+
Permanently remove redundant parts from the query if
814+
1) This is a subquery
815+
2) This is the first time this query is optimized (since the
816+
transformation is permanent
817+
3) Not normalizing a view. Removal should take place when a
818+
query involving a view is optimized, not when the view
819+
is created
820+
*/
821+
if (select_lex->master_unit()->item && // 1)
822+
select_lex->first_cond_optimization && // 2)
823+
!thd->lex->is_view_context_analysis()) // 3)
824+
{
825+
remove_redundant_subquery_clauses(select_lex);
826+
}
827+
827828
/* Resolve the ORDER BY that was skipped, then remove it. */
828829
if (skip_order_by && select_lex !=
829830
select_lex->master_unit()->global_parameters())

0 commit comments

Comments
 (0)