From a578be339d93a3d1d2866bd407c2c733d1ef5ee4 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 12 Jul 2005 23:22:08 +0400 Subject: [PATCH 1/2] Fix bug#11709 View was ordered by wrong column. When searching column to sort on, item was compared to field under view column, but not the column itself. Because names of view column and underlaid field may differ, it leads to possibly choosing wrong column for sorting on. This patch makes Item_direct_view_ref::eq(Item *item,...) compare item's name with it's own name proir to comparing to *ref item. sql/item.cc: Fix bug #11709 View was ordered by wrong column sql/item.h: Fix bug #11709 View was ordered by wrong column mysql-test/t/view.test: Test case for bug #11709 View was ordered by wrong column. mysql-test/r/view.result: Test case for bug #11709 View was ordered by wrong column. --- mysql-test/r/view.result | 9 +++++++++ mysql-test/t/view.test | 9 +++++++++ sql/item.cc | 29 +++++++++++++++++++++++++++++ sql/item.h | 1 + 4 files changed, 48 insertions(+) diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index 7bcfaf8bdc3..bd4e94bd1b3 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -1940,3 +1940,12 @@ s1 s2 DROP PROCEDURE p1; DROP VIEW v1; DROP TABLE t1; +create table t1 (f1 int, f2 int); +create view v1 as select f1 as f3, f2 as f1 from t1; +insert into t1 values (1,3),(2,1),(3,2); +select * from v1 order by f1; +f3 f1 +2 1 +3 2 +1 3 +drop table t1; diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index 6b6b3d8a00f..6b4113c0b05 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -1778,3 +1778,12 @@ CALL p1(); DROP PROCEDURE p1; DROP VIEW v1; DROP TABLE t1; + +# +# Test for bug #11709 View was ordered by wrong column +# +create table t1 (f1 int, f2 int); +create view v1 as select f1 as f3, f2 as f1 from t1; +insert into t1 values (1,3),(2,1),(3,2); +select * from v1 order by f1; +drop table t1; diff --git a/sql/item.cc b/sql/item.cc index e53c1ebf734..08395df4b73 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -4499,6 +4499,35 @@ bool Item_direct_view_ref::fix_fields(THD *thd, Item **reference) return Item_direct_ref::fix_fields(thd, reference); } +/* + Compare view field's name with item's name before call to referenced + item's eq() + + SYNOPSIS + Item_direct_view_ref::eq() + item item to compare with + binary_cmp make binary comparison + + DESCRIPTION + Consider queries: + create view v1 as select t1.f1 as f2, t1.f2 as f1 from t1; + select * from v1 order by f1; + In order to choose right field for sorting we need to compare + given item's name (f1) to view field's name prior to calling + referenced item's eq(). + + RETURN + TRUE Referenced item is equal to given item + FALSE otherwise +*/ + + +bool Item_direct_view_ref::eq(const Item *item, bool binary_cmp) const +{ + Item *it= ((Item *) item)->real_item(); + return (!it->name || !my_strcasecmp(system_charset_info, it->name, + field_name)) && ref && (*ref)->real_item()->eq(it, binary_cmp); +} void Item_null_helper::print(String *str) { diff --git a/sql/item.h b/sql/item.h index 560f1124fb4..f748286b2f7 100644 --- a/sql/item.h +++ b/sql/item.h @@ -1516,6 +1516,7 @@ public: :Item_direct_ref(thd, item) {} bool fix_fields(THD *, Item **); + bool eq(const Item *item, bool binary_cmp) const; }; From 8c182f9734e335dc20f596e0305df599b13d533d Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 13 Jul 2005 04:10:19 +0400 Subject: [PATCH 2/2] view.result, view.test: Fix for test case for bug#11709 mysql-test/t/view.test: Fix for test case for bug#11709 mysql-test/r/view.result: Fix for test case for bug#11709 --- mysql-test/r/view.result | 1 + mysql-test/t/view.test | 1 + 2 files changed, 2 insertions(+) diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index f9843d40f07..3d4c8e1ae6f 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -1956,6 +1956,7 @@ f3 f1 2 1 3 2 1 3 +drop view v1; drop table t1; CREATE TABLE t1 (f1 char) ENGINE = innodb; INSERT INTO t1 VALUES ('A'); diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index 6b10edc7abd..956c705de74 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -1796,6 +1796,7 @@ create table t1 (f1 int, f2 int); create view v1 as select f1 as f3, f2 as f1 from t1; insert into t1 values (1,3),(2,1),(3,2); select * from v1 order by f1; +drop view v1; drop table t1; #