From 2893d11c5e502bb303bea1cd47445d96a48c7413 Mon Sep 17 00:00:00 2001 From: "igor@rurik.mysql.com" <> Date: Tue, 20 Jun 2006 19:57:21 -0700 Subject: [PATCH 1/8] Fixed bug #16674. The length of the prefix of the pattern string in the LIKE predicate that determined the index range to be scanned was calculated incorrectly for multi-byte character sets. As a result of this in 4. 1 the the scanned range was wider then necessary if the prefix contained not only one-byte characters. In 5.0 additionally it caused missing some rows from the result set. --- mysql-test/r/ctype_utf8.result | 67 ++++++++++++++++++++++++++++++++++ mysql-test/t/ctype_utf8.test | 55 ++++++++++++++++++++++++++++ strings/ctype-mb.c | 25 ++++++++----- 3 files changed, 138 insertions(+), 9 deletions(-) diff --git a/mysql-test/r/ctype_utf8.result b/mysql-test/r/ctype_utf8.result index 69d7577ee77..dff4476993c 100644 --- a/mysql-test/r/ctype_utf8.result +++ b/mysql-test/r/ctype_utf8.result @@ -1124,3 +1124,70 @@ check table t1; Table Op Msg_type Msg_text test.t1 check status OK drop table t1; +SET NAMES utf8; +CREATE TABLE t1 ( +a CHAR(13) DEFAULT '', +INDEX(a) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; +INSERT INTO t1 VALUES +('Käli Käli 2-4'), ('Käli Käli 2-4'), +('Käli Käli 2+4'), ('Käli Käli 2+4'), +('Käli Käli 2-6'), ('Käli Käli 2-6'); +CREATE TABLE t2 ( +a CHAR(13) DEFAULT '', +INDEX(a) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; +INSERT INTO t2 VALUES +('Kali Kali 2-4'), ('Kali Kali 2-4'), +('Kali Kali 2+4'), ('Kali Kali 2+4'), +('Kali Kali 2-6'), ('Kali Kali 2-6'); +SELECT a FROM t1 WHERE a LIKE 'Käli Käli 2+4'; +a +Käli Käli 2+4 +Käli Käli 2+4 +SELECT a FROM t2 WHERE a LIKE 'Kali Kali 2+4'; +a +Kali Kali 2+4 +Kali Kali 2+4 +EXPLAIN SELECT a FROM t1 WHERE a LIKE 'Käli Käli 2+4'; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 range a a 40 NULL 2 Using where; Using index +EXPLAIN SELECT a FROM t1 WHERE a = 'Käli Käli 2+4'; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ref a a 40 const 2 Using where; Using index +EXPLAIN SELECT a FROM t2 WHERE a LIKE 'Kali Kali 2+4'; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 range a a 14 NULL 2 Using where; Using index +EXPLAIN SELECT a FROM t2 WHERE a = 'Kali Kali 2+4'; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 ref a a 14 const 2 Using where; Using index +DROP TABLE t1,t2; +CREATE TABLE t1 ( +a char(255) DEFAULT '', +KEY(a(10)) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; +INSERT INTO t1 VALUES ('Käli Käli 2-4'); +SELECT * FROM t1 WHERE a LIKE 'Käli Käli 2%'; +a +Käli Käli 2-4 +INSERT INTO t1 VALUES ('Käli Käli 2-4'); +SELECT * FROM t1 WHERE a LIKE 'Käli Käli 2%'; +a +Käli Käli 2-4 +Käli Käli 2-4 +DROP TABLE t1; +CREATE TABLE t1 ( +a char(255) DEFAULT '' +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; +INSERT INTO t1 VALUES ('Käli Käli 2-4'); +INSERT INTO t1 VALUES ('Käli Käli 2-4'); +SELECT * FROM t1 WHERE a LIKE 'Käli Käli 2%'; +a +Käli Käli 2-4 +Käli Käli 2-4 +ALTER TABLE t1 ADD KEY (a(10)); +SELECT * FROM t1 WHERE a LIKE 'Käli Käli 2%'; +a +Käli Käli 2-4 +Käli Käli 2-4 +DROP TABLE t1; diff --git a/mysql-test/t/ctype_utf8.test b/mysql-test/t/ctype_utf8.test index 5044f7979f1..47436956354 100644 --- a/mysql-test/t/ctype_utf8.test +++ b/mysql-test/t/ctype_utf8.test @@ -926,4 +926,59 @@ INSERT INTO t1 VALUES('uUABCDEFGHIGKLMNOPRSTUVWXYZ̈bbbbbbbbbbbbbbbbbbbbbbbbbbbb check table t1; drop table t1; +# +# Bug#16674: LIKE predicate for a utf8 character set column +# + +SET NAMES utf8; + +CREATE TABLE t1 ( + a CHAR(13) DEFAULT '', + INDEX(a) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; +INSERT INTO t1 VALUES + ('Käli Käli 2-4'), ('Käli Käli 2-4'), + ('Käli Käli 2+4'), ('Käli Käli 2+4'), + ('Käli Käli 2-6'), ('Käli Käli 2-6'); + +CREATE TABLE t2 ( + a CHAR(13) DEFAULT '', + INDEX(a) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; + +INSERT INTO t2 VALUES + ('Kali Kali 2-4'), ('Kali Kali 2-4'), + ('Kali Kali 2+4'), ('Kali Kali 2+4'), + ('Kali Kali 2-6'), ('Kali Kali 2-6'); + +SELECT a FROM t1 WHERE a LIKE 'Käli Käli 2+4'; +SELECT a FROM t2 WHERE a LIKE 'Kali Kali 2+4'; + +EXPLAIN SELECT a FROM t1 WHERE a LIKE 'Käli Käli 2+4'; +EXPLAIN SELECT a FROM t1 WHERE a = 'Käli Käli 2+4'; +EXPLAIN SELECT a FROM t2 WHERE a LIKE 'Kali Kali 2+4'; +EXPLAIN SELECT a FROM t2 WHERE a = 'Kali Kali 2+4'; + +DROP TABLE t1,t2; + +CREATE TABLE t1 ( + a char(255) DEFAULT '', + KEY(a(10)) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; +INSERT INTO t1 VALUES ('Käli Käli 2-4'); +SELECT * FROM t1 WHERE a LIKE 'Käli Käli 2%'; +INSERT INTO t1 VALUES ('Käli Käli 2-4'); +SELECT * FROM t1 WHERE a LIKE 'Käli Käli 2%'; +DROP TABLE t1; + +CREATE TABLE t1 ( + a char(255) DEFAULT '' +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; +INSERT INTO t1 VALUES ('Käli Käli 2-4'); +INSERT INTO t1 VALUES ('Käli Käli 2-4'); +SELECT * FROM t1 WHERE a LIKE 'Käli Käli 2%'; +ALTER TABLE t1 ADD KEY (a(10)); +SELECT * FROM t1 WHERE a LIKE 'Käli Käli 2%'; +DROP TABLE t1; + # End of 4.1 tests diff --git a/strings/ctype-mb.c b/strings/ctype-mb.c index eb032759d25..4f57f7c78e4 100644 --- a/strings/ctype-mb.c +++ b/strings/ctype-mb.c @@ -502,21 +502,19 @@ my_bool my_like_range_mb(CHARSET_INFO *cs, char *min_str,char *max_str, uint *min_length,uint *max_length) { + uint mblen; const char *end= ptr + ptr_length; char *min_org= min_str; char *min_end= min_str + res_length; char *max_end= max_str + res_length; - uint charlen= res_length / cs->mbmaxlen; + uint maxcharlen= res_length / cs->mbmaxlen; - for (; ptr != end && min_str != min_end && charlen > 0 ; ptr++, charlen--) + for (; ptr != end && min_str != min_end && maxcharlen ; maxcharlen--) { + /* We assume here that escape, w_any, w_namy are one-byte characters */ if (*ptr == escape && ptr+1 != end) - { - ptr++; /* Skip escape */ - *min_str++= *max_str++ = *ptr; - continue; - } - if (*ptr == w_one || *ptr == w_many) /* '_' and '%' in SQL */ + ptr++; /* Skip escape */ + else if (*ptr == w_one || *ptr == w_many) /* '_' and '%' in SQL */ { /* Write min key */ *min_length= (uint) (min_str - min_org); @@ -534,7 +532,16 @@ my_bool my_like_range_mb(CHARSET_INFO *cs, pad_max_char(cs, max_str, max_end); return 0; } - *min_str++= *max_str++ = *ptr; + if ((mblen= my_ismbchar(cs, ptr, end)) > 1) + { + if (ptr+mblen > end || min_str+mblen > min_end) + break; + while (mblen--) + *min_str++= *max_str++= *ptr++; + } + else + *min_str++= *max_str++= *ptr++; + } *min_length= *max_length = (uint) (min_str - min_org); From e307b5b2976ad8d19694819a07ef128a50824c98 Mon Sep 17 00:00:00 2001 From: "igor@rurik.mysql.com" <> Date: Wed, 21 Jun 2006 22:39:48 -0700 Subject: [PATCH 2/8] Modified the test case for bug 16674 to have the same execution plans in 4.1 and 5.0. --- mysql-test/r/ctype_utf8.result | 14 ++++++++++---- mysql-test/t/ctype_utf8.test | 2 ++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/mysql-test/r/ctype_utf8.result b/mysql-test/r/ctype_utf8.result index c2f4dce0175..fc5645ae10b 100644 --- a/mysql-test/r/ctype_utf8.result +++ b/mysql-test/r/ctype_utf8.result @@ -1173,6 +1173,7 @@ INSERT INTO t1 VALUES ('Käli Käli 2-4'), ('Käli Käli 2-4'), ('Käli Käli 2+4'), ('Käli Käli 2+4'), ('Käli Käli 2-6'), ('Käli Käli 2-6'); +INSERT INTO t1 SELECT * FROM t1; CREATE TABLE t2 ( a CHAR(13) DEFAULT '', INDEX(a) @@ -1181,26 +1182,31 @@ INSERT INTO t2 VALUES ('Kali Kali 2-4'), ('Kali Kali 2-4'), ('Kali Kali 2+4'), ('Kali Kali 2+4'), ('Kali Kali 2-6'), ('Kali Kali 2-6'); +INSERT INTO t2 SELECT * FROM t2; SELECT a FROM t1 WHERE a LIKE 'Käli Käli 2+4'; a Käli Käli 2+4 Käli Käli 2+4 +Käli Käli 2+4 +Käli Käli 2+4 SELECT a FROM t2 WHERE a LIKE 'Kali Kali 2+4'; a Kali Kali 2+4 Kali Kali 2+4 +Kali Kali 2+4 +Kali Kali 2+4 EXPLAIN SELECT a FROM t1 WHERE a LIKE 'Käli Käli 2+4'; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t1 range a a 40 NULL 2 Using where; Using index +1 SIMPLE t1 range a a 40 NULL 4 Using where; Using index EXPLAIN SELECT a FROM t1 WHERE a = 'Käli Käli 2+4'; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t1 ref a a 40 const 2 Using where; Using index +1 SIMPLE t1 ref a a 40 const 4 Using where; Using index EXPLAIN SELECT a FROM t2 WHERE a LIKE 'Kali Kali 2+4'; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t2 range a a 14 NULL 2 Using where; Using index +1 SIMPLE t2 range a a 14 NULL 4 Using where; Using index EXPLAIN SELECT a FROM t2 WHERE a = 'Kali Kali 2+4'; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t2 ref a a 14 const 2 Using where; Using index +1 SIMPLE t2 ref a a 14 const 4 Using where; Using index DROP TABLE t1,t2; CREATE TABLE t1 ( a char(255) DEFAULT '', diff --git a/mysql-test/t/ctype_utf8.test b/mysql-test/t/ctype_utf8.test index f646142de67..de9bb4687b1 100644 --- a/mysql-test/t/ctype_utf8.test +++ b/mysql-test/t/ctype_utf8.test @@ -966,6 +966,7 @@ INSERT INTO t1 VALUES ('Käli Käli 2-4'), ('Käli Käli 2-4'), ('Käli Käli 2+4'), ('Käli Käli 2+4'), ('Käli Käli 2-6'), ('Käli Käli 2-6'); +INSERT INTO t1 SELECT * FROM t1; CREATE TABLE t2 ( a CHAR(13) DEFAULT '', @@ -976,6 +977,7 @@ INSERT INTO t2 VALUES ('Kali Kali 2-4'), ('Kali Kali 2-4'), ('Kali Kali 2+4'), ('Kali Kali 2+4'), ('Kali Kali 2-6'), ('Kali Kali 2-6'); +INSERT INTO t2 SELECT * FROM t2; SELECT a FROM t1 WHERE a LIKE 'Käli Käli 2+4'; SELECT a FROM t2 WHERE a LIKE 'Kali Kali 2+4'; From f1eb33096f528913a3d721988c3645f84606fd74 Mon Sep 17 00:00:00 2001 From: "igor@rurik.mysql.com" <> Date: Thu, 22 Jun 2006 00:08:32 -0700 Subject: [PATCH 3/8] Post-merge fix. --- strings/ctype-mb.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/strings/ctype-mb.c b/strings/ctype-mb.c index 9d7aad0ca05..36b52826486 100644 --- a/strings/ctype-mb.c +++ b/strings/ctype-mb.c @@ -536,12 +536,7 @@ my_bool my_like_range_mb(CHARSET_INFO *cs, if (*ptr == escape && ptr+1 != end) ptr++; /* Skip escape */ else if (*ptr == w_one || *ptr == w_many) /* '_' and '%' in SQL */ - { - charlen= my_charpos(cs, min_org, min_str, res_length/cs->mbmaxlen); - - if (charlen < (uint) (min_str - min_org)) - min_str= min_org + charlen; - + { /* Calculate length of keys: 'a\0\0... is the smallest possible string when we have space expand From 89402314910d95867e42c8bac6aab8c93433338a Mon Sep 17 00:00:00 2001 From: "igor@rurik.mysql.com" <> Date: Thu, 22 Jun 2006 15:50:15 -0700 Subject: [PATCH 4/8] Fixed bug #20076. Server crashed in some cases when a query required a MIN/MAX agrregation for a 'ucs2' field. In these cases the aggregation caused calls of the function update_tmptable_sum_func that indirectly invoked the method Item_sum_hybrid::min_max_update_str_field() containing a call to strip_sp for a ucs2 character set. The latter led directly to the crash as it used my_isspace undefined for the ucs2 character set. Actually the call of strip_sp is not needed at all in this situation and has been removed by the fix. --- mysql-test/r/ctype_ucs.result | 7 +++++++ mysql-test/t/ctype_ucs.test | 12 ++++++++++++ sql/item_sum.cc | 1 - 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/ctype_ucs.result b/mysql-test/r/ctype_ucs.result index 8869604b128..bcf9cc77519 100644 --- a/mysql-test/r/ctype_ucs.result +++ b/mysql-test/r/ctype_ucs.result @@ -715,3 +715,10 @@ lily river drop table t1; deallocate prepare stmt; +CREATE TABLE t1 (id int, s char(5) CHARACTER SET ucs2 COLLATE ucs2_unicode_ci); +INSERT INTO t1 VALUES (1, 'ZZZZZ'), (1, 'ZZZ'), (2, 'ZZZ'), (2, 'ZZZZZ'); +SELECT id, MIN(s) FROM t1 GROUP BY id; +id MIN(s) +1 ZZZ +2 ZZZ +DROP TABLE t1; diff --git a/mysql-test/t/ctype_ucs.test b/mysql-test/t/ctype_ucs.test index 2cbabf88ee0..a4d4d1846a7 100644 --- a/mysql-test/t/ctype_ucs.test +++ b/mysql-test/t/ctype_ucs.test @@ -451,4 +451,16 @@ execute stmt using @param1; select utext from t1 where utext like '%%'; drop table t1; deallocate prepare stmt; + +# +# Bug #20076: server crashes for a query with GROUP BY if MIN/MAX aggregation +# over a 'ucs2' field uses a temporary table +# + +CREATE TABLE t1 (id int, s char(5) CHARACTER SET ucs2 COLLATE ucs2_unicode_ci); +INSERT INTO t1 VALUES (1, 'ZZZZZ'), (1, 'ZZZ'), (2, 'ZZZ'), (2, 'ZZZZZ'); + +SELECT id, MIN(s) FROM t1 GROUP BY id; + +DROP TABLE t1; # End of 4.1 tests diff --git a/sql/item_sum.cc b/sql/item_sum.cc index 4b522cf06fa..0b9b10d05d4 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -930,7 +930,6 @@ Item_sum_hybrid::min_max_update_str_field() if (!args[0]->null_value) { - res_str->strip_sp(); result_field->val_str(&tmp_value); if (result_field->is_null() || From faa48bf1a06f28f13f1360919a549332141bf316 Mon Sep 17 00:00:00 2001 From: "igor@rurik.mysql.com" <> Date: Thu, 22 Jun 2006 20:39:46 -0700 Subject: [PATCH 5/8] Added a test case for bug #18359. This was another manifestation of the problems fixed in the patch for bug 16674. Wrong calculation of length of the search prefix in the pattern string led here to a wrong result set for a query in 4.1. The bug could be demonstrated for any multi-byte character set. --- mysql-test/r/ctype_utf8.result | 56 ++++++++++++++++++++++++++++++++++ mysql-test/t/ctype_utf8.test | 31 +++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/mysql-test/r/ctype_utf8.result b/mysql-test/r/ctype_utf8.result index fc5645ae10b..4ceacaffcbb 100644 --- a/mysql-test/r/ctype_utf8.result +++ b/mysql-test/r/ctype_utf8.result @@ -1237,3 +1237,59 @@ a Käli Käli 2-4 Käli Käli 2-4 DROP TABLE t1; +SET NAMES latin2; +CREATE TABLE t1 ( +id int(11) NOT NULL default '0', +tid int(11) NOT NULL default '0', +val text NOT NULL, +INDEX idx(tid, val(10)) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +INSERT INTO t1 VALUES +(40988,72,'VOLNÝ ADSL'),(41009,72,'VOLNÝ ADSL'), +(41032,72,'VOLNÝ ADSL'),(41038,72,'VOLNÝ ADSL'), +(41063,72,'VOLNÝ ADSL'),(41537,72,'VOLNÝ ADSL Office'), +(42141,72,'VOLNÝ ADSL'),(42565,72,'VOLNÝ ADSL Combi'), +(42749,72,'VOLNÝ ADSL'),(44205,72,'VOLNÝ ADSL'); +SELECT * FROM t1 WHERE tid=72 and val LIKE 'VOLNY ADSL'; +id tid val +40988 72 VOLNÝ ADSL +41009 72 VOLNÝ ADSL +41032 72 VOLNÝ ADSL +41038 72 VOLNÝ ADSL +41063 72 VOLNÝ ADSL +42141 72 VOLNÝ ADSL +42749 72 VOLNÝ ADSL +44205 72 VOLNÝ ADSL +SELECT * FROM t1 WHERE tid=72 and val LIKE 'VOLNÝ ADSL'; +id tid val +40988 72 VOLNÝ ADSL +41009 72 VOLNÝ ADSL +41032 72 VOLNÝ ADSL +41038 72 VOLNÝ ADSL +41063 72 VOLNÝ ADSL +42141 72 VOLNÝ ADSL +42749 72 VOLNÝ ADSL +44205 72 VOLNÝ ADSL +SELECT * FROM t1 WHERE tid=72 and val LIKE '%VOLNÝ ADSL'; +id tid val +40988 72 VOLNÝ ADSL +41009 72 VOLNÝ ADSL +41032 72 VOLNÝ ADSL +41038 72 VOLNÝ ADSL +41063 72 VOLNÝ ADSL +42141 72 VOLNÝ ADSL +42749 72 VOLNÝ ADSL +44205 72 VOLNÝ ADSL +ALTER TABLE t1 DROP KEY idx; +ALTER TABLE t1 ADD KEY idx (tid,val(11)); +SELECT * FROM t1 WHERE tid=72 and val LIKE 'VOLNÝ ADSL'; +id tid val +40988 72 VOLNÝ ADSL +41009 72 VOLNÝ ADSL +41032 72 VOLNÝ ADSL +41038 72 VOLNÝ ADSL +41063 72 VOLNÝ ADSL +42141 72 VOLNÝ ADSL +42749 72 VOLNÝ ADSL +44205 72 VOLNÝ ADSL +DROP TABLE t1; diff --git a/mysql-test/t/ctype_utf8.test b/mysql-test/t/ctype_utf8.test index de9bb4687b1..b1d485ad1ce 100644 --- a/mysql-test/t/ctype_utf8.test +++ b/mysql-test/t/ctype_utf8.test @@ -1009,4 +1009,35 @@ ALTER TABLE t1 ADD KEY (a(10)); SELECT * FROM t1 WHERE a LIKE 'Käli Käli 2%'; DROP TABLE t1; +# +# Bug#18359: LIKE predicate for a 'utf8' text column with a partial index +# (see bug #16674 as well) +# + +SET NAMES latin2; + +CREATE TABLE t1 ( + id int(11) NOT NULL default '0', + tid int(11) NOT NULL default '0', + val text NOT NULL, + INDEX idx(tid, val(10)) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; + +INSERT INTO t1 VALUES + (40988,72,'VOLNÝ ADSL'),(41009,72,'VOLNÝ ADSL'), + (41032,72,'VOLNÝ ADSL'),(41038,72,'VOLNÝ ADSL'), + (41063,72,'VOLNÝ ADSL'),(41537,72,'VOLNÝ ADSL Office'), + (42141,72,'VOLNÝ ADSL'),(42565,72,'VOLNÝ ADSL Combi'), + (42749,72,'VOLNÝ ADSL'),(44205,72,'VOLNÝ ADSL'); + +SELECT * FROM t1 WHERE tid=72 and val LIKE 'VOLNY ADSL'; +SELECT * FROM t1 WHERE tid=72 and val LIKE 'VOLNÝ ADSL'; +SELECT * FROM t1 WHERE tid=72 and val LIKE '%VOLNÝ ADSL'; + +ALTER TABLE t1 DROP KEY idx; +ALTER TABLE t1 ADD KEY idx (tid,val(11)); + +SELECT * FROM t1 WHERE tid=72 and val LIKE 'VOLNÝ ADSL'; + +DROP TABLE t1; # End of 4.1 tests From 9ec681ef351517df7aca783df11838a44238b7d1 Mon Sep 17 00:00:00 2001 From: "gkodinov@mysql.com" <> Date: Tue, 27 Jun 2006 17:40:19 +0300 Subject: [PATCH 6/8] Bug #16458: Simple SELECT FOR UPDATE causes "Result Set not updatable" error 'SELECT DISTINCT a,b FROM t1' should not use temp table if there is unique index (or primary key) on a. There are a number of other similar cases that can be calculated without the use of a temp table : multi-part unique indexes, primary keys or using GROUP BY instead of DISTINCT. When a GROUP BY/DISTINCT clause contains all key parts of a unique index, then it is guaranteed that the fields of the clause will be unique, therefore we can optimize away GROUP BY/DISTINCT altogether. This optimization has two effects: * there is no need to create a temporary table to compute the GROUP/DISTINCT operation (or the temporary table will be smaller if only GROUP is removed and DISTINCT stays or if DISTINCT is removed and GROUP BY stays) * this causes the statement in effect to become updatable in Connector/Java because the result set columns will be direct reference to the primary key of the table (instead to the temporary table that it currently references). Implemented a check that will optimize away GROUP BY/DISTINCT for queries like the above. Currently it will work only for single non-constant table in the FROM clause. --- mysql-test/r/distinct.result | 51 +++++++++++ mysql-test/t/distinct.test | 28 ++++++ sql/sql_select.cc | 168 +++++++++++++++++++++++++++++++++++ 3 files changed, 247 insertions(+) diff --git a/mysql-test/r/distinct.result b/mysql-test/r/distinct.result index 8932285b5d0..c6c614a5646 100644 --- a/mysql-test/r/distinct.result +++ b/mysql-test/r/distinct.result @@ -504,3 +504,54 @@ a 2 b 2 2 4 3 2 5 DROP TABLE t1,t2; +CREATE TABLE t1(a INT PRIMARY KEY, b INT); +INSERT INTO t1 VALUES (1,1), (2,1), (3,1); +EXPLAIN SELECT DISTINCT a FROM t1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 index NULL PRIMARY 4 NULL 3 Using index +EXPLAIN SELECT DISTINCT a,b FROM t1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 +EXPLAIN SELECT DISTINCT t1_1.a, t1_1.b FROM t1 t1_1, t1 t1_2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1_1 ALL NULL NULL NULL NULL 3 Using temporary +1 SIMPLE t1_2 index NULL PRIMARY 4 NULL 3 Using index; Distinct +EXPLAIN SELECT DISTINCT t1_1.a, t1_1.b FROM t1 t1_1, t1 t1_2 +WHERE t1_1.a = t1_2.a; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1_1 ALL PRIMARY NULL NULL NULL 3 Using temporary +1 SIMPLE t1_2 eq_ref PRIMARY PRIMARY 4 test.t1_1.a 1 Using index; Distinct +EXPLAIN SELECT a FROM t1 GROUP BY a; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 index NULL PRIMARY 4 NULL 3 Using index +EXPLAIN SELECT a,b FROM t1 GROUP BY a,b; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 +EXPLAIN SELECT DISTINCT a,b FROM t1 GROUP BY a,b; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 +CREATE TABLE t2(a INT, b INT, c INT, d INT, PRIMARY KEY (a,b)); +INSERT INTO t2 VALUES (1,1,1,50), (1,2,3,40), (2,1,3,4); +EXPLAIN SELECT DISTINCT a FROM t2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 index NULL PRIMARY 8 NULL 3 Using index +EXPLAIN SELECT DISTINCT a,a FROM t2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 index NULL PRIMARY 8 NULL 3 Using index; Using temporary +EXPLAIN SELECT DISTINCT b,a FROM t2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 index NULL PRIMARY 8 NULL 3 Using index +EXPLAIN SELECT DISTINCT a,c FROM t2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 Using temporary +EXPLAIN SELECT DISTINCT c,a,b FROM t2; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 +EXPLAIN SELECT DISTINCT a,b,d FROM t2 GROUP BY c,b,d; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 Using temporary; Using filesort +CREATE UNIQUE INDEX c_b_unq ON t2 (c,b); +EXPLAIN SELECT DISTINCT a,b,d FROM t2 GROUP BY c,b,d; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 +DROP TABLE t1,t2; diff --git a/mysql-test/t/distinct.test b/mysql-test/t/distinct.test index f2fe1ec6372..8ca6f350b8d 100644 --- a/mysql-test/t/distinct.test +++ b/mysql-test/t/distinct.test @@ -348,6 +348,34 @@ SELECT DISTINCT a, b, 2 FROM t2; SELECT DISTINCT 2, a, b FROM t2; SELECT DISTINCT a, 2, b FROM t2; +DROP TABLE t1,t2; +# +# Bug#16458: Simple SELECT FOR UPDATE causes "Result Set not updatable" +# error. +# +CREATE TABLE t1(a INT PRIMARY KEY, b INT); +INSERT INTO t1 VALUES (1,1), (2,1), (3,1); +EXPLAIN SELECT DISTINCT a FROM t1; +EXPLAIN SELECT DISTINCT a,b FROM t1; +EXPLAIN SELECT DISTINCT t1_1.a, t1_1.b FROM t1 t1_1, t1 t1_2; +EXPLAIN SELECT DISTINCT t1_1.a, t1_1.b FROM t1 t1_1, t1 t1_2 + WHERE t1_1.a = t1_2.a; +EXPLAIN SELECT a FROM t1 GROUP BY a; +EXPLAIN SELECT a,b FROM t1 GROUP BY a,b; +EXPLAIN SELECT DISTINCT a,b FROM t1 GROUP BY a,b; + +CREATE TABLE t2(a INT, b INT, c INT, d INT, PRIMARY KEY (a,b)); +INSERT INTO t2 VALUES (1,1,1,50), (1,2,3,40), (2,1,3,4); +EXPLAIN SELECT DISTINCT a FROM t2; +EXPLAIN SELECT DISTINCT a,a FROM t2; +EXPLAIN SELECT DISTINCT b,a FROM t2; +EXPLAIN SELECT DISTINCT a,c FROM t2; +EXPLAIN SELECT DISTINCT c,a,b FROM t2; + +EXPLAIN SELECT DISTINCT a,b,d FROM t2 GROUP BY c,b,d; +CREATE UNIQUE INDEX c_b_unq ON t2 (c,b); +EXPLAIN SELECT DISTINCT a,b,d FROM t2 GROUP BY c,b,d; + DROP TABLE t1,t2; # End of 4.1 tests diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 709ff9726bb..cb23662f42c 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -114,6 +114,10 @@ static Item* part_of_refkey(TABLE *form,Field *field); static uint find_shortest_key(TABLE *table, const key_map *usable_keys); static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order, ha_rows select_limit, bool no_changes); +static bool list_contains_unique_index(TABLE *table, + bool (*find_func) (Field *, void *), void *data); +static bool find_field_in_item_list (Field *field, void *data); +static bool find_field_in_order_list (Field *field, void *data); static int create_sort_index(THD *thd, JOIN *join, ORDER *order, ha_rows filesort_limit, ha_rows select_limit); static int remove_duplicates(JOIN *join,TABLE *entry,List &fields, @@ -695,6 +699,36 @@ JOIN::optimize() if (old_group_list && !group_list) select_distinct= 0; } + /* + Check if we can optimize away GROUP BY/DISTINCT. + We can do that if there are no aggregate functions and the + fields in DISTINCT clause (if present) and/or columns in GROUP BY + (if present) contain direct references to all key parts of + an unique index (in whatever order). + Note that the unique keys for DISTINCT and GROUP BY should not + be the same (as long as they are unique). + + The FROM clause must contain a single non-constant table. + */ + if (tables - const_tables == 1 && (group_list || select_distinct) && + !tmp_table_param.sum_func_count) + { + if (group_list && + list_contains_unique_index(join_tab[const_tables].table, + find_field_in_order_list, + (void *) group_list)) + { + group_list= 0; + group= 0; + } + if (select_distinct && + list_contains_unique_index(join_tab[const_tables].table, + find_field_in_item_list, + (void *) &fields_list)) + { + select_distinct= 0; + } + } if (!group_list && group) { order=0; // The output has only one row @@ -7376,6 +7410,140 @@ test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts, return best; } + +/* + Check if GROUP BY/DISTINCT can be optimized away because the set is + already known to be distinct. + + SYNOPSIS + list_contains_unique_index () + table The table to operate on. + find_func function to iterate over the list and search + for a field + + DESCRIPTION + Used in removing the GROUP BY/DISTINCT of the following types of + statements: + SELECT [DISTINCT] ... FROM + [GROUP BY ,...] + + If (a,b,c is distinct) + then ,{whatever} is also distinct + + This function checks if all the key parts of any of the unique keys + of the table are referenced by a list : either the select list + through find_field_in_item_list or GROUP BY list through + find_field_in_order_list. + If the above holds then we can safely remove the GROUP BY/DISTINCT, + as no result set can be more distinct than an unique key. + + RETURN VALUE + 1 found + 0 not found. +*/ + +static bool +list_contains_unique_index(TABLE *table, + bool (*find_func) (Field *, void *), void *data) +{ + for (uint keynr= 0; keynr < table->keys; keynr++) + { + if (keynr == table->primary_key || + (table->key_info[keynr].flags & HA_NOSAME)) + { + KEY *keyinfo= table->key_info + keynr; + KEY_PART_INFO *key_part, *key_part_end; + + for (key_part=keyinfo->key_part, + key_part_end=key_part+ keyinfo->key_parts; + key_part < key_part_end; + key_part++) + { + if (!find_func(key_part->field, data)) + break; + } + if (key_part == key_part_end) + return 1; + } + } + return 0; +} + + +/* + Helper function for list_contains_unique_index. + Find a field reference in a list of ORDER structures. + + SYNOPSIS + find_field_in_order_list () + field The field to search for. + data ORDER *.The list to search in + + DESCRIPTION + Finds a direct reference of the Field in the list. + + RETURN VALUE + 1 found + 0 not found. +*/ + +static bool +find_field_in_order_list (Field *field, void *data) +{ + ORDER *group= (ORDER *) data; + bool part_found= 0; + for (ORDER *tmp_group= group; tmp_group; tmp_group=tmp_group->next) + { + Item *item= (*tmp_group->item)->real_item(); + if (item->type() == Item::FIELD_ITEM && + ((Item_field*) item)->field->eq(field)) + { + part_found= 1; + break; + } + } + return part_found; +} + + +/* + Helper function for list_contains_unique_index. + Find a field reference in a dynamic list of Items. + + SYNOPSIS + find_field_in_item_list () + field in The field to search for. + data in List *.The list to search in + + DESCRIPTION + Finds a direct reference of the Field in the list. + + RETURN VALUE + 1 found + 0 not found. +*/ + +static bool +find_field_in_item_list (Field *field, void *data) +{ + List *fields= (List *) data; + bool part_found= 0; + List_iterator li(*fields); + Item *item; + + while ((item= li++)) + { + if (item->type() == Item::FIELD_ITEM && + ((Item_field*) item)->field->eq(field)) + { + part_found= 1; + break; + } + } + return part_found; +} + + /* Test if we can skip the ORDER BY by using an index. From 2cda7f5d8055671a36165d3cbc77bc411e48a4b7 Mon Sep 17 00:00:00 2001 From: "gkodinov@mysql.com" <> Date: Wed, 28 Jun 2006 15:53:54 +0300 Subject: [PATCH 7/8] 4.1->5.0 merge for bug #16458 --- mysql-test/r/distinct.result | 2 +- sql/sql_select.cc | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/mysql-test/r/distinct.result b/mysql-test/r/distinct.result index a2ff10594e6..a3d1e8bf3bb 100644 --- a/mysql-test/r/distinct.result +++ b/mysql-test/r/distinct.result @@ -537,7 +537,7 @@ id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t2 index NULL PRIMARY 8 NULL 3 Using index EXPLAIN SELECT DISTINCT a,a FROM t2; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t2 index NULL PRIMARY 8 NULL 3 Using index; Using temporary +1 SIMPLE t2 index NULL PRIMARY 8 NULL 3 Using index EXPLAIN SELECT DISTINCT b,a FROM t2; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t2 index NULL PRIMARY 8 NULL 3 Using index diff --git a/sql/sql_select.cc b/sql/sql_select.cc index eec572a6dfc..3ecac9532ca 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -874,7 +874,11 @@ JOIN::optimize() The FROM clause must contain a single non-constant table. */ if (tables - const_tables == 1 && (group_list || select_distinct) && - !tmp_table_param.sum_func_count) + !tmp_table_param.sum_func_count && + (!join_tab[const_tables].select || + !join_tab[const_tables].select->quick || + join_tab[const_tables].select->quick->get_type() != + QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)) { if (group_list && list_contains_unique_index(join_tab[const_tables].table, @@ -11279,9 +11283,9 @@ static bool list_contains_unique_index(TABLE *table, bool (*find_func) (Field *, void *), void *data) { - for (uint keynr= 0; keynr < table->keys; keynr++) + for (uint keynr= 0; keynr < table->s->keys; keynr++) { - if (keynr == table->primary_key || + if (keynr == table->s->primary_key || (table->key_info[keynr].flags & HA_NOSAME)) { KEY *keyinfo= table->key_info + keynr; From 96ceb6c234c6c1ad06f217f3caef6a4966353552 Mon Sep 17 00:00:00 2001 From: "gkodinov@mysql.com" <> Date: Wed, 28 Jun 2006 16:28:29 +0300 Subject: [PATCH 8/8] gcc 4.1 linux warning fixes backported from 5.0. --- sql/item_cmpfunc.h | 14 ++++++++++++++ sql/opt_range.cc | 4 ++-- sql/spatial.h | 4 ++++ sql/sql_select.h | 2 +- sql/sql_update.cc | 2 +- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h index 68852b5a5f6..73abe208d9e 100644 --- a/sql/item_cmpfunc.h +++ b/sql/item_cmpfunc.h @@ -124,6 +124,8 @@ public: class Comp_creator { public: + Comp_creator() {} /* Remove gcc warning */ + virtual ~Comp_creator() {} /* Remove gcc warning */ virtual Item_bool_func2* create(Item *a, Item *b) const = 0; virtual const char* symbol(bool invert) const = 0; virtual bool eqne_op() const = 0; @@ -133,6 +135,8 @@ public: class Eq_creator :public Comp_creator { public: + Eq_creator() {} /* Remove gcc warning */ + virtual ~Eq_creator() {} /* Remove gcc warning */ virtual Item_bool_func2* create(Item *a, Item *b) const; virtual const char* symbol(bool invert) const { return invert? "<>" : "="; } virtual bool eqne_op() const { return 1; } @@ -142,6 +146,8 @@ public: class Ne_creator :public Comp_creator { public: + Ne_creator() {} /* Remove gcc warning */ + virtual ~Ne_creator() {} /* Remove gcc warning */ virtual Item_bool_func2* create(Item *a, Item *b) const; virtual const char* symbol(bool invert) const { return invert? "=" : "<>"; } virtual bool eqne_op() const { return 1; } @@ -151,6 +157,8 @@ public: class Gt_creator :public Comp_creator { public: + Gt_creator() {} /* Remove gcc warning */ + virtual ~Gt_creator() {} /* Remove gcc warning */ virtual Item_bool_func2* create(Item *a, Item *b) const; virtual const char* symbol(bool invert) const { return invert? "<=" : ">"; } virtual bool eqne_op() const { return 0; } @@ -160,6 +168,8 @@ public: class Lt_creator :public Comp_creator { public: + Lt_creator() {} /* Remove gcc warning */ + virtual ~Lt_creator() {} /* Remove gcc warning */ virtual Item_bool_func2* create(Item *a, Item *b) const; virtual const char* symbol(bool invert) const { return invert? ">=" : "<"; } virtual bool eqne_op() const { return 0; } @@ -169,6 +179,8 @@ public: class Ge_creator :public Comp_creator { public: + Ge_creator() {} /* Remove gcc warning */ + virtual ~Ge_creator() {} /* Remove gcc warning */ virtual Item_bool_func2* create(Item *a, Item *b) const; virtual const char* symbol(bool invert) const { return invert? "<" : ">="; } virtual bool eqne_op() const { return 0; } @@ -178,6 +190,8 @@ public: class Le_creator :public Comp_creator { public: + Le_creator() {} /* Remove gcc warning */ + virtual ~Le_creator() {} /* Remove gcc warning */ virtual Item_bool_func2* create(Item *a, Item *b) const; virtual const char* symbol(bool invert) const { return invert? ">" : "<="; } virtual bool eqne_op() const { return 0; } diff --git a/sql/opt_range.cc b/sql/opt_range.cc index 67141aab6ce..34f11e4968a 100644 --- a/sql/opt_range.cc +++ b/sql/opt_range.cc @@ -63,8 +63,8 @@ public: SEL_ARG(Field *field, uint8 part, char *min_value, char *max_value, uint8 min_flag, uint8 max_flag, uint8 maybe_flag); SEL_ARG(enum Type type_arg) - :elements(1),use_count(1),left(0),next_key_part(0),color(BLACK), - type(type_arg),min_flag(0) + :min_flag(0),elements(1),use_count(1),left(0),next_key_part(0), + color(BLACK), type(type_arg) {} inline bool is_same(SEL_ARG *arg) { diff --git a/sql/spatial.h b/sql/spatial.h index 206958b3eaf..378233a2156 100644 --- a/sql/spatial.h +++ b/sql/spatial.h @@ -165,6 +165,8 @@ struct Geometry_buffer; class Geometry { public: + Geometry() {} /* remove gcc warning */ + virtual ~Geometry() {} /* remove gcc warning */ static void *operator new(size_t size, void *buffer) { return buffer; @@ -173,6 +175,8 @@ public: static void operator delete(void *ptr, void *buffer) {} + static void operator delete(void *buffer) {} /* remove gcc warning */ + enum wkbType { wkb_point= 1, diff --git a/sql/sql_select.h b/sql/sql_select.h index 75cd0b4d797..c61ef4fb92b 100644 --- a/sql/sql_select.h +++ b/sql/sql_select.h @@ -229,7 +229,7 @@ class JOIN :public Sql_alloc } JOIN(JOIN &join) - :fields_list(join.fields_list) + :Sql_alloc(), fields_list(join.fields_list) { init(join.thd, join.fields_list, join.select_options, join.result); diff --git a/sql/sql_update.cc b/sql/sql_update.cc index 16423b39786..089d0bf0660 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -1094,7 +1094,7 @@ bool multi_update::send_data(List ¬_used_values) memcpy((char*) tmp_table->field[0]->ptr, (char*) table->file->ref, table->file->ref_length); /* Write row, ignoring duplicated updates to a row */ - if (error= tmp_table->file->write_row(tmp_table->record[0])) + if ((error= tmp_table->file->write_row(tmp_table->record[0]))) { if (error != HA_ERR_FOUND_DUPP_KEY && error != HA_ERR_FOUND_DUPP_UNIQUE &&