summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVarun Gupta <varun.gupta@mariadb.com>2021-01-31 19:55:07 +0530
committerVarun Gupta <varun.gupta@mariadb.com>2021-01-31 19:55:07 +0530
commit26f5033555a9bb33d3d8b68e2d766e274bec28ba (patch)
tree63e7728f4303df90bdd1d3853edd535fbc3a4390
parent072b39da66d9c1693ca832eb97f2d63b238cc412 (diff)
downloadmariadb-git-26f5033555a9bb33d3d8b68e2d766e274bec28ba.tar.gz
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.
-rw-r--r--mysql-test/r/subselect4.result8
-rw-r--r--mysql-test/t/subselect4.test11
-rw-r--r--sql/sql_select.cc33
3 files changed, 36 insertions, 16 deletions
diff --git a/mysql-test/r/subselect4.result b/mysql-test/r/subselect4.result
index c0df4f626b1..25937373e5e 100644
--- a/mysql-test/r/subselect4.result
+++ b/mysql-test/r/subselect4.result
@@ -2686,4 +2686,12 @@ SELECT * FROM t2;
f
bar
DROP TABLE t1, t2;
+#
+# MDEV-23449: alias do not exist and a query do not report an error
+#
+CREATE TABLE t1(a INT, b INT);
+INSERT INTO t1 VALUES (1,1), (2,2), (3,3), (4,4), (5,5);
+SELECT a, b FROM t1 WHERE a IN (SELECT A.a FROM t1 A GROUP BY s.id);
+ERROR 42S22: Unknown column 's.id' in 'group statement'
+DROP TABLE t1;
# End of 10.2 tests
diff --git a/mysql-test/t/subselect4.test b/mysql-test/t/subselect4.test
index 6eada9b27d9..fd36023435f 100644
--- a/mysql-test/t/subselect4.test
+++ b/mysql-test/t/subselect4.test
@@ -2201,4 +2201,15 @@ SELECT * FROM t2;
DROP TABLE t1, t2;
+--echo #
+--echo # MDEV-23449: alias do not exist and a query do not report an error
+--echo #
+
+CREATE TABLE t1(a INT, b INT);
+INSERT INTO t1 VALUES (1,1), (2,2), (3,3), (4,4), (5,5);
+
+--error ER_BAD_FIELD_ERROR
+SELECT a, b FROM t1 WHERE a IN (SELECT A.a FROM t1 A GROUP BY s.id);
+DROP TABLE t1;
+
--echo # End of 10.2 tests
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 5a2474cabee..aa3474dff5f 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -729,22 +729,6 @@ JOIN::prepare(TABLE_LIST *tables_init,
tables_list, select_lex->leaf_tables,
FALSE, SELECT_ACL, SELECT_ACL, FALSE))
DBUG_RETURN(-1);
-
- /*
- Permanently remove redundant parts from the query if
- 1) This is a subquery
- 2) This is the first time this query is optimized (since the
- transformation is permanent
- 3) Not normalizing a view. Removal should take place when a
- query involving a view is optimized, not when the view
- is created
- */
- if (select_lex->master_unit()->item && // 1)
- select_lex->first_cond_optimization && // 2)
- !thd->lex->is_view_context_analysis()) // 3)
- {
- remove_redundant_subquery_clauses(select_lex);
- }
/*
TRUE if the SELECT list mixes elements with and without grouping,
@@ -824,6 +808,23 @@ JOIN::prepare(TABLE_LIST *tables_init,
&hidden_group_fields,
&select_lex->select_n_reserved))
DBUG_RETURN(-1);
+
+ /*
+ Permanently remove redundant parts from the query if
+ 1) This is a subquery
+ 2) This is the first time this query is optimized (since the
+ transformation is permanent
+ 3) Not normalizing a view. Removal should take place when a
+ query involving a view is optimized, not when the view
+ is created
+ */
+ if (select_lex->master_unit()->item && // 1)
+ select_lex->first_cond_optimization && // 2)
+ !thd->lex->is_view_context_analysis()) // 3)
+ {
+ remove_redundant_subquery_clauses(select_lex);
+ }
+
/* Resolve the ORDER BY that was skipped, then remove it. */
if (skip_order_by && select_lex !=
select_lex->master_unit()->global_parameters())