From 5af5bb2da606df8d4f6af7ae8021f3a3ed98ca66 Mon Sep 17 00:00:00 2001 From: "oystein.grovlen@sun.com" <> Date: Tue, 13 Apr 2010 11:38:28 +0200 Subject: [PATCH] Bug#51980 mysqld service crashes with a simple COUNT(DISTINCT) query over a view Problem: Segmentation fault in add_group_and_distinct_keys() when accessing field of what is assumed to be an Item_field object. Cause: In case of views, the item added to list by is_indexed_agg_distinct() was not of type Item_field, but Item_ref. Resolution: Add the real Item_field object, the one referred to by Item_ref object, to the list, instead. --- mysql-test/r/count_distinct.result | 8 ++++++++ mysql-test/t/count_distinct.test | 11 +++++++++++ sql/sql_select.cc | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/count_distinct.result b/mysql-test/r/count_distinct.result index 804bc1f4788..3b65dd0e608 100644 --- a/mysql-test/r/count_distinct.result +++ b/mysql-test/r/count_distinct.result @@ -86,3 +86,11 @@ select count(distinct if(f1,3,f2)) from t1; count(distinct if(f1,3,f2)) 2 drop table t1; +create table t1 (i int); +insert into t1 values (0), (1); +create view v1 as select * from t1; +select count(distinct i) from v1; +count(distinct i) +2 +drop table t1; +drop view v1; diff --git a/mysql-test/t/count_distinct.test b/mysql-test/t/count_distinct.test index d0996689aeb..10b4ac6f0e7 100644 --- a/mysql-test/t/count_distinct.test +++ b/mysql-test/t/count_distinct.test @@ -96,3 +96,14 @@ insert into t1 values (0,1),(1,2); select count(distinct if(f1,3,f2)) from t1; drop table t1; +# +# Bug #51980 "mysqld service crashes with a simple COUNT(DISTINCT) query +# over a view" +# + +create table t1 (i int); +insert into t1 values (0), (1); +create view v1 as select * from t1; +select count(distinct i) from v1; +drop table t1; +drop view v1; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 7d379ab34de..62a51a32ca2 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -4157,7 +4157,7 @@ is_indexed_agg_distinct(JOIN *join, List *out_args) optimization is applicable */ if (out_args) - out_args->push_back((Item_field *) expr); + out_args->push_back((Item_field *) expr->real_item()); result= true; } }