From ec226e553aa56718ed9939e333fe36b3499ac9be Mon Sep 17 00:00:00 2001 From: Igor Babaev Date: Fri, 4 Oct 2013 09:51:07 -0700 Subject: 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. --- mysql-test/r/derived_view.result | 18 ++++++++++++++++++ mysql-test/t/derived_view.test | 19 +++++++++++++++++++ sql/item.cc | 10 +++++----- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/mysql-test/r/derived_view.result b/mysql-test/r/derived_view.result index 8853338ca8d..eb2200c7720 100644 --- a/mysql-test/r/derived_view.result +++ b/mysql-test/r/derived_view.result @@ -2205,6 +2205,24 @@ DROP VIEW v1; DROP TABLE t1,t2; 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 # set optimizer_switch=@exit_optimizer_switch; diff --git a/mysql-test/t/derived_view.test b/mysql-test/t/derived_view.test index 4b7e76e11ca..254f97ebe6a 100644 --- a/mysql-test/t/derived_view.test +++ b/mysql-test/t/derived_view.test @@ -1545,6 +1545,25 @@ DROP TABLE t1,t2; 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 # end of 5.3 tests --echo # diff --git a/sql/item.cc b/sql/item.cc index 9738a068d88..803c9fee576 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -4746,6 +4746,11 @@ bool Item_field::fix_fields(THD *thd, Item **reference) 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. @@ -4762,11 +4767,6 @@ bool Item_field::fix_fields(THD *thd, Item **reference) return FALSE; 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) { -- cgit v1.2.1