Commit ec226e55 authored by Igor Babaev's avatar Igor Babaev

Fixed bug mdev-5078.

For aggregated fields from views/derived tables the possible adjustment
of thd->lex->in_sum_func->max_arg_level in the function Item_field::fix_fields
must be done before we leave the function.
parent 00816a96
...@@ -2205,6 +2205,24 @@ DROP VIEW v1; ...@@ -2205,6 +2205,24 @@ DROP VIEW v1;
DROP TABLE t1,t2; DROP TABLE t1,t2;
set optimizer_switch=@save_optimizer_switch; set optimizer_switch=@save_optimizer_switch;
# #
# mdev-5078: sum over a view/derived table
#
CREATE TABLE t1 (a int);
INSERT INTO t1 (a) VALUES (1), (2);
CREATE TABLE t2 (b int(11));
INSERT INTO t2 (b) VALUES (1), (2);
CREATE VIEW v AS SELECT b as c FROM t2;
SELECT a, (SELECT SUM(a + c) FROM v) FROM t1;
a (SELECT SUM(a + c) FROM v)
1 5
2 7
SELECT a, (SELECT SUM(a + c) FROM (SELECT b as c FROM t2) AS v1) FROM t1;
a (SELECT SUM(a + c) FROM (SELECT b as c FROM t2) AS v1)
1 5
2 7
DROP VIEW v;
DROP TABLE t1,t2;
#
# end of 5.3 tests # end of 5.3 tests
# #
set optimizer_switch=@exit_optimizer_switch; set optimizer_switch=@exit_optimizer_switch;
......
...@@ -1545,6 +1545,25 @@ DROP TABLE t1,t2; ...@@ -1545,6 +1545,25 @@ DROP TABLE t1,t2;
set optimizer_switch=@save_optimizer_switch; set optimizer_switch=@save_optimizer_switch;
--echo #
--echo # mdev-5078: sum over a view/derived table
--echo #
CREATE TABLE t1 (a int);
INSERT INTO t1 (a) VALUES (1), (2);
CREATE TABLE t2 (b int(11));
INSERT INTO t2 (b) VALUES (1), (2);
CREATE VIEW v AS SELECT b as c FROM t2;
SELECT a, (SELECT SUM(a + c) FROM v) FROM t1;
SELECT a, (SELECT SUM(a + c) FROM (SELECT b as c FROM t2) AS v1) FROM t1;
DROP VIEW v;
DROP TABLE t1,t2;
--echo # --echo #
--echo # end of 5.3 tests --echo # end of 5.3 tests
--echo # --echo #
......
...@@ -4746,6 +4746,11 @@ bool Item_field::fix_fields(THD *thd, Item **reference) ...@@ -4746,6 +4746,11 @@ bool Item_field::fix_fields(THD *thd, Item **reference)
goto mark_non_agg_field; goto mark_non_agg_field;
} }
if (thd->lex->in_sum_func &&
thd->lex->in_sum_func->nest_level ==
thd->lex->current_select->nest_level)
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
thd->lex->current_select->nest_level);
/* /*
if it is not expression from merged VIEW we will set this field. if it is not expression from merged VIEW we will set this field.
...@@ -4762,11 +4767,6 @@ bool Item_field::fix_fields(THD *thd, Item **reference) ...@@ -4762,11 +4767,6 @@ bool Item_field::fix_fields(THD *thd, Item **reference)
return FALSE; return FALSE;
set_field(from_field); set_field(from_field);
if (thd->lex->in_sum_func &&
thd->lex->in_sum_func->nest_level ==
thd->lex->current_select->nest_level)
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
thd->lex->current_select->nest_level);
} }
else if (thd->mark_used_columns != MARK_COLUMNS_NONE) else if (thd->mark_used_columns != MARK_COLUMNS_NONE)
{ {
......
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