Commit 1bdf2151 authored by Michael Widenius's avatar Michael Widenius

Fixed MDEV-5617: mysqld crashes when running a query with ONLY_FULL_GROUP_BY

Problem was that we used cache_table in some cases where it was not initialized

mysql-test/r/func_group.result:
  Added test case
mysql-test/t/func_group.test:
  Added test case
sql/item.cc:
  Don't use cached_table if not set
sql/item_sum.cc:
  Don't use cached_table
parent 2d48e9f1
...@@ -1458,6 +1458,8 @@ DROP TABLE derived1; ...@@ -1458,6 +1458,8 @@ DROP TABLE derived1;
DROP TABLE D; DROP TABLE D;
CREATE TABLE t1 (a INT, b INT); CREATE TABLE t1 (a INT, b INT);
INSERT INTO t1 VALUES (1,1), (1,2), (1,3); INSERT INTO t1 VALUES (1,1), (1,2), (1,3);
CREATE TABLE t2 (b INT);
INSERT INTO t2 VALUES (3),(4);
SET SQL_MODE='ONLY_FULL_GROUP_BY'; SET SQL_MODE='ONLY_FULL_GROUP_BY';
SELECT COUNT(*) FROM t1; SELECT COUNT(*) FROM t1;
COUNT(*) COUNT(*)
...@@ -1473,12 +1475,19 @@ COUNT(*) ...@@ -1473,12 +1475,19 @@ COUNT(*)
SELECT COUNT(*), (SELECT count(*) FROM t1 inr WHERE inr.a = outr.a) SELECT COUNT(*), (SELECT count(*) FROM t1 inr WHERE inr.a = outr.a)
FROM t1 outr; FROM t1 outr;
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT COUNT(*) FROM t1 outr, (SELECT b, count(*) FROM t2) as t3;
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT COUNT(*) FROM t1 outr where (1,1) in (SELECT a, count(*) FROM t2);
COUNT(*)
0
SELECT COUNT(*) FROM t1 a JOIN t1 outr SELECT COUNT(*) FROM t1 a JOIN t1 outr
ON a.a= (SELECT count(*) FROM t1 inr WHERE inr.a = outr.a); ON a.a= (SELECT count(*) FROM t1 inr WHERE inr.a = outr.a);
COUNT(*) COUNT(*)
0 0
SELECT * FROM (SELECT a FROM t1 GROUP BY a) sq JOIN t2 ON a = b;
a b
SET SQL_MODE=default; SET SQL_MODE=default;
DROP TABLE t1; DROP TABLE t1,t2;
End of 5.0 tests End of 5.0 tests
# #
# BUG#47280 - strange results from count(*) with order by multiple # BUG#47280 - strange results from count(*) with order by multiple
......
...@@ -981,10 +981,13 @@ DROP TABLE D; ...@@ -981,10 +981,13 @@ DROP TABLE D;
# #
# Bug #39656: Behaviour different for agg functions with & without where - # Bug #39656: Behaviour different for agg functions with & without where -
# ONLY_FULL_GROUP_BY # ONLY_FULL_GROUP_BY
# MDEV-5617 mysqld crashes when running a query with ONLY_FULL_GROUP_BY
# #
CREATE TABLE t1 (a INT, b INT); CREATE TABLE t1 (a INT, b INT);
INSERT INTO t1 VALUES (1,1), (1,2), (1,3); INSERT INTO t1 VALUES (1,1), (1,2), (1,3);
CREATE TABLE t2 (b INT);
INSERT INTO t2 VALUES (3),(4);
SET SQL_MODE='ONLY_FULL_GROUP_BY'; SET SQL_MODE='ONLY_FULL_GROUP_BY';
...@@ -1000,11 +1003,18 @@ SELECT COUNT(*) FROM t1 a JOIN t1 b ON a.a= b.a; ...@@ -1000,11 +1003,18 @@ SELECT COUNT(*) FROM t1 a JOIN t1 b ON a.a= b.a;
SELECT COUNT(*), (SELECT count(*) FROM t1 inr WHERE inr.a = outr.a) SELECT COUNT(*), (SELECT count(*) FROM t1 inr WHERE inr.a = outr.a)
FROM t1 outr; FROM t1 outr;
--error ER_MIX_OF_GROUP_FUNC_AND_FIELDS
SELECT COUNT(*) FROM t1 outr, (SELECT b, count(*) FROM t2) as t3;
SELECT COUNT(*) FROM t1 outr where (1,1) in (SELECT a, count(*) FROM t2);
SELECT COUNT(*) FROM t1 a JOIN t1 outr SELECT COUNT(*) FROM t1 a JOIN t1 outr
ON a.a= (SELECT count(*) FROM t1 inr WHERE inr.a = outr.a); ON a.a= (SELECT count(*) FROM t1 inr WHERE inr.a = outr.a);
SELECT * FROM (SELECT a FROM t1 GROUP BY a) sq JOIN t2 ON a = b;
SET SQL_MODE=default; SET SQL_MODE=default;
DROP TABLE t1; DROP TABLE t1,t2;
### ###
......
...@@ -5230,8 +5230,8 @@ bool Item_field::fix_fields(THD *thd, Item **reference) ...@@ -5230,8 +5230,8 @@ bool Item_field::fix_fields(THD *thd, Item **reference)
if (any_privileges) if (any_privileges)
{ {
char *db, *tab; char *db, *tab;
db= cached_table->get_db_name(); db= field->table->s->db.str;
tab= cached_table->get_table_name(); tab= field->table->s->table_name.str;
if (!(have_privileges= (get_column_grant(thd, &field->table->grant, if (!(have_privileges= (get_column_grant(thd, &field->table->grant,
db, tab, field_name) & db, tab, field_name) &
VIEW_ANY_ACL))) VIEW_ANY_ACL)))
...@@ -5252,7 +5252,12 @@ bool Item_field::fix_fields(THD *thd, Item **reference) ...@@ -5252,7 +5252,12 @@ bool Item_field::fix_fields(THD *thd, Item **reference)
marker= thd->lex->current_select->cur_pos_in_select_list; marker= thd->lex->current_select->cur_pos_in_select_list;
} }
mark_non_agg_field: mark_non_agg_field:
if (fixed && thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY) /*
table->pos_in_table_list can be 0 when fixing partition functions
or virtual fields.
*/
if (fixed && (thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY) &&
field->table->pos_in_table_list)
{ {
/* /*
Mark selects according to presence of non aggregated fields. Mark selects according to presence of non aggregated fields.
...@@ -5265,7 +5270,7 @@ mark_non_agg_field: ...@@ -5265,7 +5270,7 @@ mark_non_agg_field:
(the current level) or a stub added by non-SELECT queries. (the current level) or a stub added by non-SELECT queries.
*/ */
SELECT_LEX *select_lex= cached_table ? SELECT_LEX *select_lex= cached_table ?
cached_table->select_lex : context->select_lex; cached_table->select_lex : field->table->pos_in_table_list->select_lex;
if (!thd->lex->in_sum_func) if (!thd->lex->in_sum_func)
select_lex->set_non_agg_field_used(true); select_lex->set_non_agg_field_used(true);
else else
...@@ -8234,7 +8239,7 @@ int Item_default_value::save_in_field(Field *field_arg, bool no_conversions) ...@@ -8234,7 +8239,7 @@ int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
if (context->error_processor == &view_error_processor) if (context->error_processor == &view_error_processor)
{ {
TABLE_LIST *view= cached_table->top_table(); TABLE_LIST *view= field_arg->table->pos_in_table_list->top_table();
push_warning_printf(field_arg->table->in_use, push_warning_printf(field_arg->table->in_use,
MYSQL_ERROR::WARN_LEVEL_WARN, MYSQL_ERROR::WARN_LEVEL_WARN,
ER_NO_DEFAULT_FOR_VIEW_FIELD, ER_NO_DEFAULT_FOR_VIEW_FIELD,
......
...@@ -261,7 +261,7 @@ bool Item_sum::check_sum_func(THD *thd, Item **ref) ...@@ -261,7 +261,7 @@ bool Item_sum::check_sum_func(THD *thd, Item **ref)
List_iterator<Item_field> of(outer_fields); List_iterator<Item_field> of(outer_fields);
while ((field= of++)) while ((field= of++))
{ {
SELECT_LEX *sel= field->cached_table->select_lex; SELECT_LEX *sel= field->field->table->pos_in_table_list->select_lex;
if (sel->nest_level < aggr_level) if (sel->nest_level < aggr_level)
{ {
if (in_sum_func) if (in_sum_func)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment