From 5097c6825e0412fd023c59766ed31812ac5a5189 Mon Sep 17 00:00:00 2001 From: "evgen@moonbone.local" <> Date: Wed, 22 Jun 2005 04:45:10 +0400 Subject: [PATCH] Fix bug#11298 insert into select from VIEW produces incorrect result when using ORDER BY Insert were inserting data from last record fetched instead of inserting from temporary table. Make Item_ref to save data from result_field if it's available rather then from *ref on save_in_field() call. --- mysql-test/r/view.result | 14 ++++++++++++++ mysql-test/t/view.test | 13 +++++++++++++ sql/item.cc | 22 ++++++++++++++++++++++ sql/item.h | 3 +-- 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index d7f3b043439..b69787dbc87 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -1757,3 +1757,17 @@ select * from v1; cast(1 as decimal) 1.00 drop view v1; +create table t1(f1 int); +create table t2(f2 int); +insert into t1 values(1),(2),(3); +insert into t2 values(1),(2),(3); +create view v1 as select * from t1,t2 where f1=f2; +create table t3 (f1 int, f2 int); +insert into t3 select * from v1 order by 1; +select * from t3; +f1 f2 +1 1 +2 2 +3 3 +drop view v1; +drop table t1,t2,t3; diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index 29284540435..27cd9404cbe 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -1599,3 +1599,16 @@ drop table t1; create view v1 as select cast(1 as decimal); select * from v1; drop view v1; +# +# Bug#11298 insert into select from VIEW produces incorrect result when +# using ORDER BY +create table t1(f1 int); +create table t2(f2 int); +insert into t1 values(1),(2),(3); +insert into t2 values(1),(2),(3); +create view v1 as select * from t1,t2 where f1=f2; +create table t3 (f1 int, f2 int); +insert into t3 select * from v1 order by 1; +select * from t3; +drop view v1; +drop table t1,t2,t3; diff --git a/sql/item.cc b/sql/item.cc index 8a7d00e2841..20e411afceb 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -4360,6 +4360,28 @@ my_decimal *Item_ref::val_decimal(my_decimal *decimal_value) return val; } +int Item_ref::save_in_field(Field *to, bool no_conversions) +{ + int res; + if(result_field){ + if (result_field->is_null()) + { + null_value= 1; + return set_field_to_null_with_conversions(to, no_conversions); + } + else + { + to->set_notnull(); + field_conv(to, result_field); + null_value= 0; + } + return 0; + } + res= (*ref)->save_in_field(to, no_conversions); + null_value= (*ref)->null_value; + return res; +} + void Item_ref_null_helper::print(String *str) { diff --git a/sql/item.h b/sql/item.h index de292279e06..856434ab626 100644 --- a/sql/item.h +++ b/sql/item.h @@ -1343,8 +1343,7 @@ public: bool send(Protocol *prot, String *tmp); void make_field(Send_field *field) { (*ref)->make_field(field); } bool fix_fields(THD *, struct st_table_list *, Item **); - int save_in_field(Field *field, bool no_conversions) - { return (*ref)->save_in_field(field, no_conversions); } + int save_in_field(Field *field, bool no_conversions); void save_org_in_field(Field *field) { (*ref)->save_org_in_field(field); } enum Item_result result_type () const { return (*ref)->result_type(); } enum_field_types field_type() const { return (*ref)->field_type(); }