From 4202454baa1f41bcd371fb6747157de98b44e072 Mon Sep 17 00:00:00 2001 From: "gkodinov/kgeorge@magare.gmz" <> Date: Wed, 11 Apr 2007 13:58:16 +0300 Subject: [PATCH 1/6] Bug #27530: The function CRC32() returns unsigned integer. But the metadata (the unsigned flag) for the function was set incorrectly. As a result type arithmetics based on the function's metadata (like finding the concise type of an temporary table column to hold the result) returned incorrect results. Fixed by returning correct type information. This fix is based on code contributed by Martin Friebe (martin@hybyte.com) on 2007-03-30. --- mysql-test/r/bdb_notembedded.result | 35 +++++++++++++++ mysql-test/r/func_str.result | 69 +++++++++++++++++++++++++++++ mysql-test/t/bdb_notembedded.test | 38 ++++++++++++++++ mysql-test/t/func_str.test | 18 ++++++++ sql/item_strfunc.h | 2 +- 5 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 mysql-test/r/bdb_notembedded.result create mode 100644 mysql-test/t/bdb_notembedded.test diff --git a/mysql-test/r/bdb_notembedded.result b/mysql-test/r/bdb_notembedded.result new file mode 100644 index 00000000000..14cb5fad915 --- /dev/null +++ b/mysql-test/r/bdb_notembedded.result @@ -0,0 +1,35 @@ +set autocommit=1; +reset master; +create table bug16206 (a int); +insert into bug16206 values(1); +start transaction; +insert into bug16206 values(2); +commit; +show binlog events; +Log_name Pos Event_type Server_id End_log_pos Info +f n Format_desc 1 n Server ver: VERSION, Binlog ver: 4 +f n Query 1 n use `test`; create table bug16206 (a int) +f n Query 1 n use `test`; insert into bug16206 values(1) +f n Query 1 n use `test`; insert into bug16206 values(2) +drop table bug16206; +reset master; +create table bug16206 (a int) engine= bdb; +insert into bug16206 values(0); +insert into bug16206 values(1); +start transaction; +insert into bug16206 values(2); +commit; +insert into bug16206 values(3); +show binlog events; +Log_name Pos Event_type Server_id End_log_pos Info +f n Format_desc 1 n Server ver: VERSION, Binlog ver: 4 +f n Query 1 n use `test`; create table bug16206 (a int) engine= bdb +f n Query 1 n use `test`; insert into bug16206 values(0) +f n Query 1 n use `test`; insert into bug16206 values(1) +f n Query 1 n use `test`; BEGIN +f n Query 1 n use `test`; insert into bug16206 values(2) +f n Query 1 n use `test`; COMMIT +f n Query 1 n use `test`; insert into bug16206 values(3) +drop table bug16206; +set autocommit=0; +End of 5.0 tests diff --git a/mysql-test/r/func_str.result b/mysql-test/r/func_str.result index 92265c77984..d8afbe13c76 100644 --- a/mysql-test/r/func_str.result +++ b/mysql-test/r/func_str.result @@ -1992,4 +1992,73 @@ abc SELECT INSERT('abc', 6, 3, '1234'); INSERT('abc', 6, 3, '1234') abc +CREATE TABLE t1 (a INT); +CREATE VIEW v1 AS SELECT CRC32(a) AS C FROM t1; +INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10); +SELECT CRC32(a), COUNT(*) FROM t1 GROUP BY 1; +CRC32(a) COUNT(*) +450215437 1 +498629140 1 +1790921346 1 +1842515611 1 +2212294583 1 +2226203566 1 +2366072709 1 +2707236321 1 +4088798008 1 +4194326291 1 +SELECT CRC32(a), COUNT(*) FROM t1 GROUP BY 1 ORDER BY 1; +CRC32(a) COUNT(*) +450215437 1 +498629140 1 +1790921346 1 +1842515611 1 +2212294583 1 +2226203566 1 +2366072709 1 +2707236321 1 +4088798008 1 +4194326291 1 +SELECT * FROM (SELECT CRC32(a) FROM t1) t2; +CRC32(a) +2212294583 +450215437 +1842515611 +4088798008 +2226203566 +498629140 +1790921346 +4194326291 +2366072709 +2707236321 +CREATE TABLE t2 SELECT CRC32(a) FROM t1; +desc t2; +Field Type Null Key Default Extra +CRC32(a) int(10) unsigned YES NULL +SELECT * FROM v1; +C +2212294583 +450215437 +1842515611 +4088798008 +2226203566 +498629140 +1790921346 +4194326291 +2366072709 +2707236321 +SELECT * FROM (SELECT * FROM v1) x; +C +2212294583 +450215437 +1842515611 +4088798008 +2226203566 +498629140 +1790921346 +4194326291 +2366072709 +2707236321 +DROP TABLE t1, t2; +DROP VIEW v1; End of 5.0 tests diff --git a/mysql-test/t/bdb_notembedded.test b/mysql-test/t/bdb_notembedded.test new file mode 100644 index 00000000000..24e64ebbfb2 --- /dev/null +++ b/mysql-test/t/bdb_notembedded.test @@ -0,0 +1,38 @@ +-- source include/not_embedded.inc +-- source include/have_bdb.inc + +# +# Bug #16206: Superfluous COMMIT event in binlog when updating BDB in autocommit mode +# +set autocommit=1; + +let $VERSION=`select version()`; + +reset master; +create table bug16206 (a int); +insert into bug16206 values(1); +start transaction; +insert into bug16206 values(2); +commit; +--replace_result $VERSION VERSION +--replace_column 1 f 2 n 5 n +show binlog events; +drop table bug16206; + +reset master; +create table bug16206 (a int) engine= bdb; +insert into bug16206 values(0); +insert into bug16206 values(1); +start transaction; +insert into bug16206 values(2); +commit; +insert into bug16206 values(3); +--replace_result $VERSION VERSION +--replace_column 1 f 2 n 5 n +show binlog events; +drop table bug16206; + +set autocommit=0; + + +--echo End of 5.0 tests diff --git a/mysql-test/t/func_str.test b/mysql-test/t/func_str.test index 0e4b404fe3a..bca977e6df3 100644 --- a/mysql-test/t/func_str.test +++ b/mysql-test/t/func_str.test @@ -1058,4 +1058,22 @@ SELECT INSERT('abc', 4, 3, '1234'); SELECT INSERT('abc', 5, 3, '1234'); SELECT INSERT('abc', 6, 3, '1234'); +# +# Bug #27530: Grouping on crc32, or create table select crc32 +# +CREATE TABLE t1 (a INT); +CREATE VIEW v1 AS SELECT CRC32(a) AS C FROM t1; + +INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10); +SELECT CRC32(a), COUNT(*) FROM t1 GROUP BY 1; +SELECT CRC32(a), COUNT(*) FROM t1 GROUP BY 1 ORDER BY 1; +SELECT * FROM (SELECT CRC32(a) FROM t1) t2; +CREATE TABLE t2 SELECT CRC32(a) FROM t1; +desc t2; +SELECT * FROM v1; +SELECT * FROM (SELECT * FROM v1) x; + +DROP TABLE t1, t2; +DROP VIEW v1; + --echo End of 5.0 tests diff --git a/sql/item_strfunc.h b/sql/item_strfunc.h index 778ea6e9496..d7c4a3eddef 100644 --- a/sql/item_strfunc.h +++ b/sql/item_strfunc.h @@ -790,7 +790,7 @@ class Item_func_crc32 :public Item_int_func { String value; public: - Item_func_crc32(Item *a) :Item_int_func(a) {} + Item_func_crc32(Item *a) :Item_int_func(a) { unsigned_flag= 1; } const char *func_name() const { return "crc32"; } void fix_length_and_dec() { max_length=10; } longlong val_int(); From 6ad81b4e1325e727e9d218b1783f5891f7ff0405 Mon Sep 17 00:00:00 2001 From: "igor@olga.mysql.com" <> Date: Tue, 17 Apr 2007 17:35:29 -0700 Subject: [PATCH 2/6] Fixed bug #27870. The bug that causes crashes manifests itself at some conditions when executing an equijoin query with WHERE condition containing a subquery predicate of the form join_attr NOT IN (SELECT ...). To resolve a problem of the correct evaluation of the expression attr NOT IN (SELECT ...) an array of guards is created to make it possible to filter out some predicates of the EXISTS subquery into which the original subquery predicate is transformed, in the cases when a takes the NULL value. If attr is defined as a field that cannot be NULL than such an array is not needed and is not created. However if the field a occurred also an an equijoin predicate t2.a=t1.b and table t1 is accessed before table t2 then it may happen that the the EXISTS subquery is pushed down to the condition evaluated just after table t1 has been accessed. In this case any occurrence of t2.a is substituted for t1.b. When t1.b takes the value of NULL an attempt is made to turn on the corresponding guard. This action caused a crash as no guard array had been created. Now the code of Item_in_subselect::set_cond_guard_var checks that the guard array has been created before setting a guard variable on. Otherwise the method does nothing. It cannot results in returning a row that could be rejected as the condition t2.a=t1.b will be checked later anyway. --- mysql-test/r/bdb_notembedded.result | 35 ++++++++++++++++++++++++++ mysql-test/r/subselect3.result | 19 +++++++++++++++ mysql-test/t/bdb_notembedded.test | 38 +++++++++++++++++++++++++++++ mysql-test/t/subselect3.test | 18 ++++++++++++++ sql/item_subselect.h | 6 ++++- 5 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 mysql-test/r/bdb_notembedded.result create mode 100644 mysql-test/t/bdb_notembedded.test diff --git a/mysql-test/r/bdb_notembedded.result b/mysql-test/r/bdb_notembedded.result new file mode 100644 index 00000000000..14cb5fad915 --- /dev/null +++ b/mysql-test/r/bdb_notembedded.result @@ -0,0 +1,35 @@ +set autocommit=1; +reset master; +create table bug16206 (a int); +insert into bug16206 values(1); +start transaction; +insert into bug16206 values(2); +commit; +show binlog events; +Log_name Pos Event_type Server_id End_log_pos Info +f n Format_desc 1 n Server ver: VERSION, Binlog ver: 4 +f n Query 1 n use `test`; create table bug16206 (a int) +f n Query 1 n use `test`; insert into bug16206 values(1) +f n Query 1 n use `test`; insert into bug16206 values(2) +drop table bug16206; +reset master; +create table bug16206 (a int) engine= bdb; +insert into bug16206 values(0); +insert into bug16206 values(1); +start transaction; +insert into bug16206 values(2); +commit; +insert into bug16206 values(3); +show binlog events; +Log_name Pos Event_type Server_id End_log_pos Info +f n Format_desc 1 n Server ver: VERSION, Binlog ver: 4 +f n Query 1 n use `test`; create table bug16206 (a int) engine= bdb +f n Query 1 n use `test`; insert into bug16206 values(0) +f n Query 1 n use `test`; insert into bug16206 values(1) +f n Query 1 n use `test`; BEGIN +f n Query 1 n use `test`; insert into bug16206 values(2) +f n Query 1 n use `test`; COMMIT +f n Query 1 n use `test`; insert into bug16206 values(3) +drop table bug16206; +set autocommit=0; +End of 5.0 tests diff --git a/mysql-test/r/subselect3.result b/mysql-test/r/subselect3.result index 33e7fc54ed2..f52249db8a1 100644 --- a/mysql-test/r/subselect3.result +++ b/mysql-test/r/subselect3.result @@ -692,3 +692,22 @@ a MAX(b) test 2 3 h 3 4 i DROP TABLE t1, t2; +CREATE TABLE t1 (a int); +CREATE TABLE t2 (b int, PRIMARY KEY(b)); +INSERT INTO t1 VALUES (1), (NULL), (4); +INSERT INTO t2 VALUES (3), (1),(2), (5), (4), (7), (6); +EXPLAIN EXTENDED +SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1)); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where +1 PRIMARY t2 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using index +2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 3 Using where +Warnings: +Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` join `test`.`t2` where ((`test`.`t2`.`b` = `test`.`t1`.`a`) and (not((`test`.`t1`.`a`,(select 1 AS `Not_used` from `test`.`t1` where (((`test`.`t2`.`b`) = `test`.`t1`.`a`) or isnull(`test`.`t1`.`a`)) having (`test`.`t1`.`a`)))))) +SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1)); +a +SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1 WHERE a > 4)); +a +1 +4 +DROP TABLE t1,t2; diff --git a/mysql-test/t/bdb_notembedded.test b/mysql-test/t/bdb_notembedded.test new file mode 100644 index 00000000000..24e64ebbfb2 --- /dev/null +++ b/mysql-test/t/bdb_notembedded.test @@ -0,0 +1,38 @@ +-- source include/not_embedded.inc +-- source include/have_bdb.inc + +# +# Bug #16206: Superfluous COMMIT event in binlog when updating BDB in autocommit mode +# +set autocommit=1; + +let $VERSION=`select version()`; + +reset master; +create table bug16206 (a int); +insert into bug16206 values(1); +start transaction; +insert into bug16206 values(2); +commit; +--replace_result $VERSION VERSION +--replace_column 1 f 2 n 5 n +show binlog events; +drop table bug16206; + +reset master; +create table bug16206 (a int) engine= bdb; +insert into bug16206 values(0); +insert into bug16206 values(1); +start transaction; +insert into bug16206 values(2); +commit; +insert into bug16206 values(3); +--replace_result $VERSION VERSION +--replace_column 1 f 2 n 5 n +show binlog events; +drop table bug16206; + +set autocommit=0; + + +--echo End of 5.0 tests diff --git a/mysql-test/t/subselect3.test b/mysql-test/t/subselect3.test index 11468cd6759..dfe09968fa2 100644 --- a/mysql-test/t/subselect3.test +++ b/mysql-test/t/subselect3.test @@ -528,3 +528,21 @@ SELECT a, MAX(b), DROP TABLE t1, t2; + + +# +# Bug #27870: crash of an equijoin query with WHERE condition containing +# a subquery predicate of the form NOT IN (SELECT ...) +# + +CREATE TABLE t1 (a int); +CREATE TABLE t2 (b int, PRIMARY KEY(b)); +INSERT INTO t1 VALUES (1), (NULL), (4); +INSERT INTO t2 VALUES (3), (1),(2), (5), (4), (7), (6); + +EXPLAIN EXTENDED +SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1)); +SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1)); +SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1 WHERE a > 4)); + +DROP TABLE t1,t2; diff --git a/sql/item_subselect.h b/sql/item_subselect.h index 6b605e96432..118609671b8 100644 --- a/sql/item_subselect.h +++ b/sql/item_subselect.h @@ -276,7 +276,11 @@ public: { return pushed_cond_guards ? pushed_cond_guards + i : NULL; } - void set_cond_guard_var(int i, bool v) { pushed_cond_guards[i]= v; } + void set_cond_guard_var(int i, bool v) + { + if ( pushed_cond_guards) + pushed_cond_guards[i]= v; + } bool have_guarded_conds() { return test(pushed_cond_guards); } Item_func_not_all *upper_item; // point on NOT/NOP before ALL/SOME subquery From 16223f0d75e715b49f02df9c220d0c8fa0077b20 Mon Sep 17 00:00:00 2001 From: "gluh@mysql.com/eagle.(none)" <> Date: Thu, 19 Apr 2007 16:49:21 +0500 Subject: [PATCH 3/6] Bug#27499 DROP TABLE race with SHOW TABLE STATUS They can drop table after table names list creation and before table opening. We open non existing table and get ER_NO_SUCH_TABLE error. In this case we do not store the record into I_S table and clear error. --- sql/sql_show.cc | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 0c9ea7b0bbf..ff3f76f446c 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -2314,18 +2314,32 @@ int get_all_tables(THD *thd, TABLE_LIST *tables, COND *cond) res= open_normal_and_derived_tables(thd, show_table_list, MYSQL_LOCK_IGNORE_FLUSH); lex->sql_command= save_sql_command; - /* - We should use show_table_list->alias instead of - show_table_list->table_name because table_name - could be changed during opening of I_S tables. It's safe - to use alias because alias contains original table name - in this case. + /* + They can drop table after table names list creation and + before table opening. We open non existing table and + get ER_NO_SUCH_TABLE error. In this case we do not store + the record into I_S table and clear error. */ - res= schema_table->process_table(thd, show_table_list, table, - res, orig_base_name, - show_table_list->alias); - close_tables_for_reopen(thd, &show_table_list); - DBUG_ASSERT(!lex->query_tables_own_last); + if (thd->net.last_errno == ER_NO_SUCH_TABLE) + { + res= 0; + thd->clear_error(); + } + else + { + /* + We should use show_table_list->alias instead of + show_table_list->table_name because table_name + could be changed during opening of I_S tables. It's safe + to use alias because alias contains original table name + in this case. + */ + res= schema_table->process_table(thd, show_table_list, table, + res, orig_base_name, + show_table_list->alias); + close_tables_for_reopen(thd, &show_table_list); + DBUG_ASSERT(!lex->query_tables_own_last); + } if (res) goto err; } From 4c89a5960fbd73c684b9f8d3a07ee8d78436b732 Mon Sep 17 00:00:00 2001 From: "gkodinov/kgeorge@magare.gmz" <> Date: Fri, 20 Apr 2007 10:49:45 +0300 Subject: [PATCH 4/6] Bug #27786: When merging views into the enclosing statement the ORDER BY clause of the view is merged to the parent's ORDER BY clause. However when the VIEW is merged into an UNION branch the ORDER BY should be ignored. Use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces unordered set of rows. Fixed by ignoring the ORDER BY clause from the merge view when expanded in an UNION branch. --- mysql-test/r/view.result | 35 +++++++++++++++++++++++++++++++++++ mysql-test/t/view.test | 16 ++++++++++++++++ sql/sql_lex.h | 8 ++++++++ sql/sql_view.cc | 9 +++++++-- 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index 71d743f37b9..70dd6b2550f 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -3319,4 +3319,39 @@ lgid clid 2 YES DROP VIEW v1; DROP table t1,t2; +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(2),(3); +CREATE VIEW v1 AS SELECT a FROM t1 ORDER BY a; +SELECT * FROM t1 UNION SELECT * FROM v1; +a +1 +2 +3 +EXPLAIN SELECT * FROM t1 UNION SELECT * FROM v1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 +2 UNION t1 ALL NULL NULL NULL NULL 3 +NULL UNION RESULT ALL NULL NULL NULL NULL NULL +SELECT * FROM v1 UNION SELECT * FROM t1; +a +1 +2 +3 +EXPLAIN SELECT * FROM v1 UNION SELECT * FROM t1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 +2 UNION t1 ALL NULL NULL NULL NULL 3 +NULL UNION RESULT ALL NULL NULL NULL NULL NULL +SELECT * FROM t1 UNION SELECT * FROM v1 ORDER BY a; +a +1 +2 +3 +EXPLAIN SELECT * FROM t1 UNION SELECT * FROM v1 ORDER BY a; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 +2 UNION t1 ALL NULL NULL NULL NULL 3 +NULL UNION RESULT ALL NULL NULL NULL NULL NULL Using filesort +DROP VIEW v1; +DROP TABLE t1; End of 5.0 tests. diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index 920fcee3b24..e5bf9de13eb 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -3205,4 +3205,20 @@ SELECT * FROM v1; DROP VIEW v1; DROP table t1,t2; +# +# Bug#27786: Inconsistent Operation Performing UNION On View With ORDER BY +# +CREATE TABLE t1 (a INT); INSERT INTO t1 VALUES (1),(2),(3); +CREATE VIEW v1 AS SELECT a FROM t1 ORDER BY a; + +SELECT * FROM t1 UNION SELECT * FROM v1; +EXPLAIN SELECT * FROM t1 UNION SELECT * FROM v1; +SELECT * FROM v1 UNION SELECT * FROM t1; +EXPLAIN SELECT * FROM v1 UNION SELECT * FROM t1; +SELECT * FROM t1 UNION SELECT * FROM v1 ORDER BY a; +EXPLAIN SELECT * FROM t1 UNION SELECT * FROM v1 ORDER BY a; + +DROP VIEW v1; +DROP TABLE t1; + --echo End of 5.0 tests. diff --git a/sql/sql_lex.h b/sql/sql_lex.h index de7de0d46e9..2ff29d684d1 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -468,6 +468,7 @@ public: bool change_result(select_subselect *result, select_subselect *old_result); void set_limit(st_select_lex *values); void set_thd(THD *thd_arg) { thd= thd_arg; } + inline bool is_union (); friend void lex_start(THD *thd, uchar *buf, uint length); friend int subselect_union_engine::exec(); @@ -700,6 +701,13 @@ public: }; typedef class st_select_lex SELECT_LEX; + +inline bool st_select_lex_unit::is_union () +{ + return first_select()->next_select() && + first_select()->next_select()->linkage == UNION_TYPE; +} + #define ALTER_ADD_COLUMN 1 #define ALTER_DROP_COLUMN 2 #define ALTER_CHANGE_COLUMN 4 diff --git a/sql/sql_view.cc b/sql/sql_view.cc index 83beec3d1be..95dec40450f 100644 --- a/sql/sql_view.cc +++ b/sql/sql_view.cc @@ -1263,13 +1263,18 @@ bool mysql_make_view(THD *thd, File_parser *parser, TABLE_LIST *table, unit->slave= save_slave; // fix include_down initialisation } + /* + We can safely ignore the VIEW's ORDER BY if we merge into union + branch, as order is not important there. + */ + if (!table->select_lex->master_unit()->is_union()) + table->select_lex->order_list.push_back(&lex->select_lex.order_list); /* This SELECT_LEX will be linked in global SELECT_LEX list to make it processed by mysql_handle_derived(), but it will not be included to SELECT_LEX tree, because it will not be executed - */ - table->select_lex->order_list.push_back(&lex->select_lex.order_list); + */ goto ok; } From f0fc4b88e0a6eb6249106306f411ecab3e413a6c Mon Sep 17 00:00:00 2001 From: "mhansson@dl145s.mysql.com" <> Date: Fri, 20 Apr 2007 11:01:53 +0200 Subject: [PATCH 5/6] Bug #24778: Innodb: No result when using ORDER BY This bug was intruduced by the fix for bug#17212 (in 4.1). It is not ok to call test_if_skip_sort_order since this function will alter the execution plan. By contract it is not ok to call test_if_skip_sort_order in this context. This bug appears only in the case when the optimizer has chosen an index for accessing a particular table but finds a covering index that enables it to skip ORDER BY. This happens in test_if_skip_sort_order. --- mysql-test/r/key.result | 41 +++++++++++++++++++++++++++++++++++ mysql-test/t/key.test | 48 +++++++++++++++++++++++++++++++++++++++++ sql/sql_select.cc | 5 +---- 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/mysql-test/r/key.result b/mysql-test/r/key.result index eea884e4294..ec15eaa97f5 100644 --- a/mysql-test/r/key.result +++ b/mysql-test/r/key.result @@ -414,3 +414,44 @@ a int(11) NO PRI b varchar(20) NO MUL c varchar(20) NO drop table t1; +CREATE TABLE t1 ( +a INTEGER auto_increment PRIMARY KEY, +b INTEGER NOT NULL, +c INTEGER NOT NULL, +d CHAR(64) +); +CREATE TABLE t2 ( +a INTEGER auto_increment PRIMARY KEY, +b INTEGER NOT NULL, +c SMALLINT NOT NULL, +d DATETIME NOT NULL, +e SMALLINT NOT NULL, +f INTEGER NOT NULL, +g INTEGER NOT NULL, +h SMALLINT NOT NULL, +i INTEGER NOT NULL, +j INTEGER NOT NULL, +UNIQUE INDEX (b), +INDEX (b, d, e, f, g, h, i, j, c), +INDEX (c) +); +INSERT INTO t2 VALUES +(NULL, 1, 254, '1000-01-01 00:00:00', 257, 0, 0, 0, 0, 0), +(NULL, 2, 1, '2004-11-30 12:00:00', 1, 0, 0, 0, 0, 0), +(NULL, 3, 1, '2004-11-30 12:00:00', 1, 0, 0, 2, -21600, 0), +(NULL, 4, 1, '2004-11-30 12:00:00', 1, 0, 0, 2, -10800, 0), +(NULL, 5, 1, '2004-11-30 12:00:00', 1, 0, 0, 5, -10800, 0), +(NULL, 6, 1, '2004-11-30 12:00:00', 102, 0, 0, 0, 0, 0), +(NULL, 7, 1, '2004-11-30 12:00:00', 105, 2, 0, 0, 0, 0), +(NULL, 8, 1, '2004-11-30 12:00:00', 105, 10, 0, 0, 0, 0); +INSERT INTO t1 (b, c, d) VALUES +(3388000, -553000, NULL), +(3388000, -553000, NULL); +SELECT * +FROM t2 c JOIN t1 pa ON c.b = pa.a +WHERE c.c = 1 +ORDER BY c.b, c.d +; +a b c d e f g h i j a b c d +2 2 1 2004-11-30 12:00:00 1 0 0 0 0 0 2 3388000 -553000 NULL +DROP TABLE t1, t2; diff --git a/mysql-test/t/key.test b/mysql-test/t/key.test index 3767f5f885e..e84b2071ab1 100644 --- a/mysql-test/t/key.test +++ b/mysql-test/t/key.test @@ -384,3 +384,51 @@ desc t1; drop table t1; # End of 4.1 tests + +# +# Bug #24778: Innodb: No result when using ORDER BY +# +CREATE TABLE t1 ( + a INTEGER auto_increment PRIMARY KEY, + b INTEGER NOT NULL, + c INTEGER NOT NULL, + d CHAR(64) +); + +CREATE TABLE t2 ( + a INTEGER auto_increment PRIMARY KEY, + b INTEGER NOT NULL, + c SMALLINT NOT NULL, + d DATETIME NOT NULL, + e SMALLINT NOT NULL, + f INTEGER NOT NULL, + g INTEGER NOT NULL, + h SMALLINT NOT NULL, + i INTEGER NOT NULL, + j INTEGER NOT NULL, + UNIQUE INDEX (b), + INDEX (b, d, e, f, g, h, i, j, c), + INDEX (c) +); + +INSERT INTO t2 VALUES + (NULL, 1, 254, '1000-01-01 00:00:00', 257, 0, 0, 0, 0, 0), + (NULL, 2, 1, '2004-11-30 12:00:00', 1, 0, 0, 0, 0, 0), + (NULL, 3, 1, '2004-11-30 12:00:00', 1, 0, 0, 2, -21600, 0), + (NULL, 4, 1, '2004-11-30 12:00:00', 1, 0, 0, 2, -10800, 0), + (NULL, 5, 1, '2004-11-30 12:00:00', 1, 0, 0, 5, -10800, 0), + (NULL, 6, 1, '2004-11-30 12:00:00', 102, 0, 0, 0, 0, 0), + (NULL, 7, 1, '2004-11-30 12:00:00', 105, 2, 0, 0, 0, 0), + (NULL, 8, 1, '2004-11-30 12:00:00', 105, 10, 0, 0, 0, 0); + +INSERT INTO t1 (b, c, d) VALUES + (3388000, -553000, NULL), + (3388000, -553000, NULL); + +SELECT * +FROM t2 c JOIN t1 pa ON c.b = pa.a +WHERE c.c = 1 +ORDER BY c.b, c.d +; + +DROP TABLE t1, t2; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index dc018dd43bb..77565ca8f7b 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -5982,10 +5982,7 @@ make_join_readinfo(JOIN *join, ulonglong options) */ if (!ordered_set && (table == join->sort_by_table && - (!join->order || join->skip_sort_order || - test_if_skip_sort_order(tab, join->order, join->select_limit, - 1)) - ) || + (!join->order || join->skip_sort_order)) || (join->sort_by_table == (TABLE *) 1 && i != join->const_tables)) ordered_set= 1; From f5a229b3d1dee09db8c8bd1999c6cc6c066123d5 Mon Sep 17 00:00:00 2001 From: "gshchepa/uchum@gshchepa.loc" <> Date: Fri, 20 Apr 2007 15:14:09 +0500 Subject: [PATCH 6/6] Bug#27704: incorrect comparison of rows with NULL components Support for NULL components was incomplete for row comparison, fixed. Added support for abort_on_null at compare_row() like in 5.x --- mysql-test/r/row.result | 99 ++++++++++++++++++++++++++++++++++- mysql-test/r/subselect.result | 4 +- mysql-test/t/row.test | 45 ++++++++++++++++ sql/item_cmpfunc.cc | 36 +++++++++++-- sql/item_cmpfunc.h | 4 +- 5 files changed, 180 insertions(+), 8 deletions(-) diff --git a/mysql-test/r/row.result b/mysql-test/r/row.result index 1c1c4809f36..6169619712b 100644 --- a/mysql-test/r/row.result +++ b/mysql-test/r/row.result @@ -53,7 +53,7 @@ SELECT (1,2,3)=(1,NULL,3); NULL SELECT (1,2,3)=(1,NULL,0); (1,2,3)=(1,NULL,0) -NULL +0 SELECT ROW(1,2,3)=ROW(1,2,3); ROW(1,2,3)=ROW(1,2,3) 1 @@ -188,3 +188,100 @@ SELECT ROW(2,1) IN (ROW(21,2),ROW(ROW(1,1,3),0)); ERROR 21000: Operand should contain 1 column(s) SELECT ROW(2,1) IN (ROW(ROW(1,1,3),0),ROW(21,2)); ERROR 21000: Operand should contain 1 column(s) +CREATE TABLE t1(a int, b int, c int); +INSERT INTO t1 VALUES (1, 2, 3), +(NULL, 2, 3 ), (1, NULL, 3 ), (1, 2, NULL), +(NULL, 2, 3+1), (1, NULL, 3+1), (1, 2+1, NULL), +(NULL, 2, 3-1), (1, NULL, 3-1), (1, 2-1, NULL); +SELECT (1,2,3) = (1, NULL, 3); +(1,2,3) = (1, NULL, 3) +NULL +SELECT (1,2,3) = (1+1, NULL, 3); +(1,2,3) = (1+1, NULL, 3) +0 +SELECT (1,2,3) = (1, NULL, 3+1); +(1,2,3) = (1, NULL, 3+1) +0 +SELECT * FROM t1 WHERE (a,b,c) = (1,2,3); +a b c +1 2 3 +SELECT (1,2,3) <> (1, NULL, 3); +(1,2,3) <> (1, NULL, 3) +NULL +SELECT (1,2,3) <> (1+1, NULL, 3); +(1,2,3) <> (1+1, NULL, 3) +1 +SELECT (1,2,3) <> (1, NULL, 3+1); +(1,2,3) <> (1, NULL, 3+1) +1 +SELECT * FROM t1 WHERE (a,b,c) <> (1,2,3); +a b c +NULL 2 4 +1 NULL 4 +1 3 NULL +NULL 2 2 +1 NULL 2 +1 1 NULL +SELECT (1,2,3) < (NULL, 2, 3); +(1,2,3) < (NULL, 2, 3) +NULL +SELECT (1,2,3) < (1, NULL, 3); +(1,2,3) < (1, NULL, 3) +NULL +SELECT (1,2,3) < (1-1, NULL, 3); +(1,2,3) < (1-1, NULL, 3) +0 +SELECT (1,2,3) < (1+1, NULL, 3); +(1,2,3) < (1+1, NULL, 3) +1 +SELECT * FROM t1 WHERE (a,b,c) < (1,2,3); +a b c +1 1 NULL +SELECT (1,2,3) <= (NULL, 2, 3); +(1,2,3) <= (NULL, 2, 3) +NULL +SELECT (1,2,3) <= (1, NULL, 3); +(1,2,3) <= (1, NULL, 3) +NULL +SELECT (1,2,3) <= (1-1, NULL, 3); +(1,2,3) <= (1-1, NULL, 3) +0 +SELECT (1,2,3) <= (1+1, NULL, 3); +(1,2,3) <= (1+1, NULL, 3) +1 +SELECT * FROM t1 WHERE (a,b,c) <= (1,2,3); +a b c +1 2 3 +1 1 NULL +SELECT (1,2,3) > (NULL, 2, 3); +(1,2,3) > (NULL, 2, 3) +NULL +SELECT (1,2,3) > (1, NULL, 3); +(1,2,3) > (1, NULL, 3) +NULL +SELECT (1,2,3) > (1-1, NULL, 3); +(1,2,3) > (1-1, NULL, 3) +1 +SELECT (1,2,3) > (1+1, NULL, 3); +(1,2,3) > (1+1, NULL, 3) +0 +SELECT * FROM t1 WHERE (a,b,c) > (1,2,3); +a b c +1 3 NULL +SELECT (1,2,3) >= (NULL, 2, 3); +(1,2,3) >= (NULL, 2, 3) +NULL +SELECT (1,2,3) >= (1, NULL, 3); +(1,2,3) >= (1, NULL, 3) +NULL +SELECT (1,2,3) >= (1-1, NULL, 3); +(1,2,3) >= (1-1, NULL, 3) +1 +SELECT (1,2,3) >= (1+1, NULL, 3); +(1,2,3) >= (1+1, NULL, 3) +0 +SELECT * FROM t1 WHERE (a,b,c) >= (1,2,3); +a b c +1 2 3 +1 3 NULL +DROP TABLE t1; diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index a339a139687..5bb79a53771 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -913,7 +913,7 @@ select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),(select c from t a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a') (select c from t1 where a=t2.a) 1 1 a 2 0 b -NULL NULL NULL +NULL 0 NULL select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b'),(select c from t1 where a=t2.a) from t2; a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b') (select c from t1 where a=t2.a) 1 0 a @@ -923,7 +923,7 @@ select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c'),(select c from t a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c') (select c from t1 where a=t2.a) 1 0 a 2 0 b -NULL NULL NULL +NULL 0 NULL drop table t1,t2; create table t1 (a int, b real, c varchar(10)); insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b'); diff --git a/mysql-test/t/row.test b/mysql-test/t/row.test index 6c66d45b942..6f845607d8c 100644 --- a/mysql-test/t/row.test +++ b/mysql-test/t/row.test @@ -108,4 +108,49 @@ SELECT ROW(2,1) IN (ROW(21,2),ROW(ROW(1,1,3),0)); --error 1241 SELECT ROW(2,1) IN (ROW(ROW(1,1,3),0),ROW(21,2)); +# +# Bug#27704: erroneous comparison of rows with NULL components +# +CREATE TABLE t1(a int, b int, c int); +INSERT INTO t1 VALUES (1, 2, 3), + (NULL, 2, 3 ), (1, NULL, 3 ), (1, 2, NULL), + (NULL, 2, 3+1), (1, NULL, 3+1), (1, 2+1, NULL), + (NULL, 2, 3-1), (1, NULL, 3-1), (1, 2-1, NULL); + +SELECT (1,2,3) = (1, NULL, 3); +SELECT (1,2,3) = (1+1, NULL, 3); +SELECT (1,2,3) = (1, NULL, 3+1); +SELECT * FROM t1 WHERE (a,b,c) = (1,2,3); + +SELECT (1,2,3) <> (1, NULL, 3); +SELECT (1,2,3) <> (1+1, NULL, 3); +SELECT (1,2,3) <> (1, NULL, 3+1); +SELECT * FROM t1 WHERE (a,b,c) <> (1,2,3); + +SELECT (1,2,3) < (NULL, 2, 3); +SELECT (1,2,3) < (1, NULL, 3); +SELECT (1,2,3) < (1-1, NULL, 3); +SELECT (1,2,3) < (1+1, NULL, 3); +SELECT * FROM t1 WHERE (a,b,c) < (1,2,3); + +SELECT (1,2,3) <= (NULL, 2, 3); +SELECT (1,2,3) <= (1, NULL, 3); +SELECT (1,2,3) <= (1-1, NULL, 3); +SELECT (1,2,3) <= (1+1, NULL, 3); +SELECT * FROM t1 WHERE (a,b,c) <= (1,2,3); + +SELECT (1,2,3) > (NULL, 2, 3); +SELECT (1,2,3) > (1, NULL, 3); +SELECT (1,2,3) > (1-1, NULL, 3); +SELECT (1,2,3) > (1+1, NULL, 3); +SELECT * FROM t1 WHERE (a,b,c) > (1,2,3); + +SELECT (1,2,3) >= (NULL, 2, 3); +SELECT (1,2,3) >= (1, NULL, 3); +SELECT (1,2,3) >= (1-1, NULL, 3); +SELECT (1,2,3) >= (1+1, NULL, 3); +SELECT * FROM t1 WHERE (a,b,c) >= (1,2,3); + +DROP TABLE t1; + # End of 4.1 tests diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index ce3096da778..be44d490415 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -680,17 +680,45 @@ int Arg_comparator::compare_e_int_diff_signedness() int Arg_comparator::compare_row() { int res= 0; + bool was_null= 0; (*a)->bring_value(); (*b)->bring_value(); uint n= (*a)->cols(); for (uint i= 0; inull_value) - return -1; + { + // NULL was compared + switch (owner->functype()) { + case Item_func::NE_FUNC: + break; // NE never aborts on NULL even if abort_on_null is set + case Item_func::LT_FUNC: + case Item_func::LE_FUNC: + case Item_func::GT_FUNC: + case Item_func::GE_FUNC: + return -1; // <, <=, > and >= always fail on NULL + default: // EQ_FUNC + if (owner->abort_on_null) + return -1; // We do not need correct NULL returning + } + was_null= 1; + owner->null_value= 0; + res= 0; // continue comparison (maybe we will meet explicit difference) + } + else if (res) + return res; } - return res; + if (was_null) + { + /* + There was NULL(s) in comparison in some parts, but there was no + explicit difference in other parts, so we have to return NULL. + */ + owner->null_value= 1; + return -1; + } + return 0; } int Arg_comparator::compare_e_row() diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h index 132e019b4a3..7dbbdc7a63b 100644 --- a/sql/item_cmpfunc.h +++ b/sql/item_cmpfunc.h @@ -206,10 +206,11 @@ class Item_bool_func2 :public Item_int_func protected: Arg_comparator cmp; String tmp_value1,tmp_value2; + bool abort_on_null; public: Item_bool_func2(Item *a,Item *b) - :Item_int_func(a,b), cmp(tmp_arg, tmp_arg+1) {} + :Item_int_func(a,b), cmp(tmp_arg, tmp_arg+1), abort_on_null(FALSE) {} void fix_length_and_dec(); void set_cmp_func() { @@ -222,6 +223,7 @@ public: bool is_null() { return test(args[0]->is_null() || args[1]->is_null()); } bool is_bool_func() { return 1; } CHARSET_INFO *compare_collation() { return cmp.cmp_collation.collation; } + void top_level_item() { abort_on_null= TRUE; } friend class Arg_comparator; };