From e0d8e8fd09f46b9ccda0b39e5483bca4d3d672de Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 2 Feb 2007 15:22:10 -0800 Subject: [PATCH 1/5] Fix bug #24035. This performance degradation for UPDATEs could be observed in the update statements for which the search key cannot be converted to any valid value of the type of the search column, like for a the condition int_fld=99999999999999999999999999, though it can be guaranteed here that there is no row with such a key value. mysql-test/r/update.result: Added a test case for bug #24035. mysql-test/t/update.test: Added a test case for bug #24035. sql/opt_range.cc: Fix bug #24035. This performance degradation for could be observed in the update statements for which the search key cannot be converted to any valid value of the type of the search column, like for a the condition int_fld=99999999999999999999999999, though it can be guaranteed here that there is no row with such a key value. Now the function get_mm_leaf creates trees of the type SEL_ARG::IMPOSSIBLE for such conditions that tells the range scan not to perform any search at all. --- mysql-test/r/update.result | 57 ++++++++++++++++++++++++++++++++++++++ mysql-test/t/update.test | 35 +++++++++++++++++++++++ sql/opt_range.cc | 17 +++++++++++- 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/update.result b/mysql-test/r/update.result index 4b7dbb3dbe9..9e2bc52657a 100644 --- a/mysql-test/r/update.result +++ b/mysql-test/r/update.result @@ -377,3 +377,60 @@ create table t1(f1 int, `*f2` int); insert into t1 values (1,1); update t1 set `*f2`=1; drop table t1; +CREATE TABLE t1 ( +request_id int unsigned NOT NULL auto_increment, +user_id varchar(12) default NULL, +time_stamp datetime NOT NULL default '0000-00-00 00:00:00', +ip_address varchar(15) default NULL, +PRIMARY KEY (request_id), +KEY user_id_2 (user_id,time_stamp) +); +INSERT INTO t1 (user_id) VALUES ('user1'); +INSERT INTO t1(user_id) SELECT user_id FROM t1; +INSERT INTO t1(user_id) SELECT user_id FROM t1; +INSERT INTO t1(user_id) SELECT user_id FROM t1; +INSERT INTO t1(user_id) SELECT user_id FROM t1; +INSERT INTO t1(user_id) SELECT user_id FROM t1; +INSERT INTO t1(user_id) SELECT user_id FROM t1; +INSERT INTO t1(user_id) SELECT user_id FROM t1; +INSERT INTO t1(user_id) SELECT user_id FROM t1; +flush status; +SELECT user_id FROM t1 WHERE request_id=9999999999999; +user_id +show status like '%Handler_read%'; +Variable_name Value +Handler_read_first 0 +Handler_read_key 1 +Handler_read_next 0 +Handler_read_prev 0 +Handler_read_rnd 0 +Handler_read_rnd_next 0 +SELECT user_id FROM t1 WHERE request_id=999999999999999999999999999999; +user_id +show status like '%Handler_read%'; +Variable_name Value +Handler_read_first 0 +Handler_read_key 2 +Handler_read_next 0 +Handler_read_prev 0 +Handler_read_rnd 0 +Handler_read_rnd_next 7 +UPDATE t1 SET user_id=null WHERE request_id=9999999999999; +show status like '%Handler_read%'; +Variable_name Value +Handler_read_first 0 +Handler_read_key 3 +Handler_read_next 0 +Handler_read_prev 0 +Handler_read_rnd 0 +Handler_read_rnd_next 14 +UPDATE t1 SET user_id=null WHERE request_id=999999999999999999999999999999; +show status like '%Handler_read%'; +Variable_name Value +Handler_read_first 0 +Handler_read_key 3 +Handler_read_next 0 +Handler_read_prev 0 +Handler_read_rnd 0 +Handler_read_rnd_next 21 +DROP TABLE t1; diff --git a/mysql-test/t/update.test b/mysql-test/t/update.test index 5a49de248b1..3ce7ef72670 100644 --- a/mysql-test/t/update.test +++ b/mysql-test/t/update.test @@ -307,3 +307,38 @@ insert into t1 values (1,1); update t1 set `*f2`=1; drop table t1; # End of 4.1 tests + +# +# Bug #24035: performance degradation with condition int_field=big_decimal +# + +CREATE TABLE t1 ( + request_id int unsigned NOT NULL auto_increment, + user_id varchar(12) default NULL, + time_stamp datetime NOT NULL default '0000-00-00 00:00:00', + ip_address varchar(15) default NULL, + PRIMARY KEY (request_id), + KEY user_id_2 (user_id,time_stamp) +); + +INSERT INTO t1 (user_id) VALUES ('user1'); +INSERT INTO t1(user_id) SELECT user_id FROM t1; +INSERT INTO t1(user_id) SELECT user_id FROM t1; +INSERT INTO t1(user_id) SELECT user_id FROM t1; +INSERT INTO t1(user_id) SELECT user_id FROM t1; +INSERT INTO t1(user_id) SELECT user_id FROM t1; +INSERT INTO t1(user_id) SELECT user_id FROM t1; +INSERT INTO t1(user_id) SELECT user_id FROM t1; +INSERT INTO t1(user_id) SELECT user_id FROM t1; + +flush status; +SELECT user_id FROM t1 WHERE request_id=9999999999999; +show status like '%Handler_read%'; +SELECT user_id FROM t1 WHERE request_id=999999999999999999999999999999; +show status like '%Handler_read%'; +UPDATE t1 SET user_id=null WHERE request_id=9999999999999; +show status like '%Handler_read%'; +UPDATE t1 SET user_id=null WHERE request_id=999999999999999999999999999999; +show status like '%Handler_read%'; + +DROP TABLE t1; diff --git a/sql/opt_range.cc b/sql/opt_range.cc index f0af4b7db2a..744d222b833 100644 --- a/sql/opt_range.cc +++ b/sql/opt_range.cc @@ -4296,7 +4296,22 @@ get_mm_leaf(PARAM *param, COND *conf_func, Field *field, KEY_PART *key_part, err= value->save_in_field_no_warnings(field, 1); if (err > 0 && field->cmp_type() != value->result_type()) { - tree= 0; + if ((type == Item_func::EQ_FUNC || type == Item_func::EQUAL_FUNC) && + value->result_type() == item_cmp_type(field->result_type(), + value->result_type())) + + { + tree= new (alloc) SEL_ARG(field, 0, 0); + tree->type= SEL_ARG::IMPOSSIBLE; + } + else + { + /* + TODO: We should return trees of the type SEL_ARG::IMPOSSIBLE + for the cases like int_field > 999999999999999999999999 as well. + */ + tree= 0; + } goto end; } if (err < 0) From c35ceeca9e11062516bd93d10d89ac818a6e1f23 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 6 Feb 2007 11:08:57 +0200 Subject: [PATCH 2/5] Bug #22344: InnoDB keys act strange on datetime vs timestamp comparison Ignoring error codes from type conversion allows default (wrong) values to go unnoticed in the formation of index search conditions. Fixed by correctly checking for conversion errors. mysql-test/r/select.result: Bug #22344: InnoDB keys act strange on datetime vs timestamp comparison - test case mysql-test/t/select.test: Bug #22344: InnoDB keys act strange on datetime vs timestamp comparison - test case sql/field.h: Bug #22344: InnoDB keys act strange on datetime vs timestamp comparison - don't ignore coversion errors sql/field_conv.cc: Bug #22344: InnoDB keys act strange on datetime vs timestamp comparison - don't ignore coversion errors sql/item.cc: Bug #22344: InnoDB keys act strange on datetime vs timestamp comparison - don't ignore coversion errors --- mysql-test/r/select.result | 28 ++++++++++++++++++++++++++++ mysql-test/t/select.test | 23 +++++++++++++++++++++++ sql/field.h | 4 ++-- sql/field_conv.cc | 15 +++++++-------- sql/item.cc | 11 ++++++----- 5 files changed, 66 insertions(+), 15 deletions(-) diff --git a/mysql-test/r/select.result b/mysql-test/r/select.result index f3938fd6413..96af58789ec 100644 --- a/mysql-test/r/select.result +++ b/mysql-test/r/select.result @@ -3728,3 +3728,31 @@ WHERE ID_better=1 AND ID1_with_null IS NULL AND id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 ref idx1,idx2 idx2 4 const 1 Using where DROP TABLE t1; +CREATE TABLE t1 (a INT, ts TIMESTAMP, KEY ts(ts)); +INSERT INTO t1 VALUES (30,"2006-01-03 23:00:00"), (31,"2006-01-03 23:00:00"); +ANALYZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +CREATE TABLE t2 (a INT, dt1 DATETIME, dt2 DATETIME, PRIMARY KEY (a)); +INSERT INTO t2 VALUES (30, "2006-01-01 00:00:00", "2999-12-31 00:00:00"); +INSERT INTO t2 SELECT a+1,dt1,dt2 FROM t2; +ANALYZE TABLE t2; +Table Op Msg_type Msg_text +test.t2 analyze status OK +EXPLAIN +SELECT * FROM t1 LEFT JOIN t2 ON (t1.a=t2.a) WHERE t1.a=30 +AND t1.ts BETWEEN t2.dt1 AND t2.dt2 +AND t1.ts BETWEEN "2006-01-01" AND "2006-12-31"; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t2 const PRIMARY PRIMARY 4 const 1 +1 SIMPLE t1 range ts ts 4 NULL 1 Using where +Warnings: +Warning 1292 Incorrect datetime value: '2999-12-31 00:00:00' for column 'ts' at row 1 +SELECT * FROM t1 LEFT JOIN t2 ON (t1.a=t2.a) WHERE t1.a=30 +AND t1.ts BETWEEN t2.dt1 AND t2.dt2 +AND t1.ts BETWEEN "2006-01-01" AND "2006-12-31"; +a ts a dt1 dt2 +30 2006-01-03 23:00:00 30 2006-01-01 00:00:00 2999-12-31 00:00:00 +Warnings: +Warning 1292 Incorrect datetime value: '2999-12-31 00:00:00' for column 'ts' at row 1 +DROP TABLE t1,t2; diff --git a/mysql-test/t/select.test b/mysql-test/t/select.test index 19c7d742f5b..dfa3c1ac2a4 100644 --- a/mysql-test/t/select.test +++ b/mysql-test/t/select.test @@ -3207,3 +3207,26 @@ EXPLAIN SELECT * FROM t1 (ID2_with_null=1 OR ID2_with_null=2); DROP TABLE t1; + +# +# Bug #22344: InnoDB keys act strange on datetime vs timestamp comparison +# +CREATE TABLE t1 (a INT, ts TIMESTAMP, KEY ts(ts)); +INSERT INTO t1 VALUES (30,"2006-01-03 23:00:00"), (31,"2006-01-03 23:00:00"); +ANALYZE TABLE t1; + +CREATE TABLE t2 (a INT, dt1 DATETIME, dt2 DATETIME, PRIMARY KEY (a)); +INSERT INTO t2 VALUES (30, "2006-01-01 00:00:00", "2999-12-31 00:00:00"); +INSERT INTO t2 SELECT a+1,dt1,dt2 FROM t2; +ANALYZE TABLE t2; + +EXPLAIN +SELECT * FROM t1 LEFT JOIN t2 ON (t1.a=t2.a) WHERE t1.a=30 + AND t1.ts BETWEEN t2.dt1 AND t2.dt2 + AND t1.ts BETWEEN "2006-01-01" AND "2006-12-31"; + +SELECT * FROM t1 LEFT JOIN t2 ON (t1.a=t2.a) WHERE t1.a=30 + AND t1.ts BETWEEN t2.dt1 AND t2.dt2 + AND t1.ts BETWEEN "2006-01-01" AND "2006-12-31"; + +DROP TABLE t1,t2; diff --git a/sql/field.h b/sql/field.h index 565342637ba..562991f681e 100644 --- a/sql/field.h +++ b/sql/field.h @@ -29,7 +29,7 @@ class Send_field; class Protocol; struct st_cache_field; -void field_conv(Field *to,Field *from); +int field_conv(Field *to,Field *from); inline uint get_enum_pack_length(int elements) { @@ -1242,7 +1242,7 @@ public: uint max_packed_col_length(uint max_length); void free() { value.free(); } inline void clear_temporary() { bzero((char*) &value,sizeof(value)); } - friend void field_conv(Field *to,Field *from); + friend int field_conv(Field *to,Field *from); uint size_of() const { return sizeof(*this); } bool has_charset(void) const { return charset() == &my_charset_bin ? FALSE : TRUE; } diff --git a/sql/field_conv.cc b/sql/field_conv.cc index e5752708123..dbe58d804ad 100644 --- a/sql/field_conv.cc +++ b/sql/field_conv.cc @@ -679,7 +679,7 @@ void (*Copy_field::get_copy_func(Field *to,Field *from))(Copy_field*) /* Simple quick field convert that is called on insert */ -void field_conv(Field *to,Field *from) +int field_conv(Field *to,Field *from) { if (to->real_type() == from->real_type() && !(to->type() == FIELD_TYPE_BLOB && to->table->copy_blobs)) @@ -707,7 +707,7 @@ void field_conv(Field *to,Field *from) if (to->ptr != from->ptr) #endif memcpy(to->ptr,from->ptr,to->pack_length()); - return; + return 0; } } if (to->type() == FIELD_TYPE_BLOB) @@ -723,8 +723,7 @@ void field_conv(Field *to,Field *from) from->real_type() != MYSQL_TYPE_STRING && from->real_type() != MYSQL_TYPE_VARCHAR)) blob->value.copy(); - blob->store(blob->value.ptr(),blob->value.length(),from->charset()); - return; + return blob->store(blob->value.ptr(),blob->value.length(),from->charset()); } if ((from->result_type() == STRING_RESULT && (to->result_type() == STRING_RESULT || @@ -741,15 +740,15 @@ void field_conv(Field *to,Field *from) end with \0. Can be replaced with .ptr() when we have our own string->double conversion. */ - to->store(result.c_ptr_quick(),result.length(),from->charset()); + return to->store(result.c_ptr_quick(),result.length(),from->charset()); } else if (from->result_type() == REAL_RESULT) - to->store(from->val_real()); + return to->store(from->val_real()); else if (from->result_type() == DECIMAL_RESULT) { my_decimal buff; - to->store_decimal(from->val_decimal(&buff)); + return to->store_decimal(from->val_decimal(&buff)); } else - to->store(from->val_int(), test(from->flags & UNSIGNED_FLAG)); + return to->store(from->val_int(), test(from->flags & UNSIGNED_FLAG)); } diff --git a/sql/item.cc b/sql/item.cc index 9a55eb25e2c..6d0297f909e 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -4245,18 +4245,19 @@ void Item_field::save_org_in_field(Field *to) int Item_field::save_in_field(Field *to, bool no_conversions) { + int res; if (result_field->is_null()) { null_value=1; - return set_field_to_null_with_conversions(to, no_conversions); + res= set_field_to_null_with_conversions(to, no_conversions); } else { to->set_notnull(); - field_conv(to,result_field); + res= field_conv(to,result_field); null_value=0; } - return 0; + return res; } @@ -5284,9 +5285,9 @@ int Item_ref::save_in_field(Field *to, bool no_conversions) return set_field_to_null_with_conversions(to, no_conversions); } to->set_notnull(); - field_conv(to, result_field); + res= field_conv(to, result_field); null_value= 0; - return 0; + return res; } res= (*ref)->save_in_field(to, no_conversions); null_value= (*ref)->null_value; From 976f0a391c9d20982d495cd01ab6e8f701d915fd Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Feb 2007 00:46:03 +0300 Subject: [PATCH 3/5] Bug#19978: INSERT .. ON DUPLICATE erroneously reports some records were updated. INSERT ... ON DUPLICATE KEY UPDATE reports that a record was updated when the duplicate key occurs even if the record wasn't actually changed because the update values are the same as those in the record. Now the compare_record() function is used to check whether the record was changed and the update of a record reported only if the record differs from the original one. sql/sql_update.cc: Bug#19978: INSERT .. ON DUPLICATE erroneously reports some records were updated. The compare_record() function was changed to non-static one. sql/sql_insert.cc: Bug#19978: INSERT .. ON DUPLICATE erroneously reports some records were updated. Now the compare_record() function is used to check whether the record was changed and the update of a record reported only if the record differs from the original one. sql/mysql_priv.h: Bug#19978: INSERT .. ON DUPLICATE erroneously reports some records were updated. Added the prototype of the compare_record() function. mysql-test/t/insert_select.test: Added a test case for bug#19978: INSERT .. ON DUPLICATE erroneously reports some records were updated. mysql-test/r/insert_select.result: Added a test case for bug#19978: INSERT .. ON DUPLICATE erroneously reports some records were updated. --- mysql-test/r/insert_select.result | 12 ++++++++++++ mysql-test/t/insert_select.test | 13 +++++++++++++ sql/mysql_priv.h | 1 + sql/sql_insert.cc | 26 ++++++++++++++++---------- sql/sql_update.cc | 2 +- 5 files changed, 43 insertions(+), 11 deletions(-) diff --git a/mysql-test/r/insert_select.result b/mysql-test/r/insert_select.result index 1453d25ac75..82cc1b036a7 100644 --- a/mysql-test/r/insert_select.result +++ b/mysql-test/r/insert_select.result @@ -705,3 +705,15 @@ use bug21774_1; INSERT INTO bug21774_2.t1 SELECT t1.* FROM t1; DROP DATABASE bug21774_1; DROP DATABASE bug21774_2; +USE test; +create table t1(f1 int primary key, f2 int); +insert into t1 values (1,1); +affected rows: 1 +insert into t1 values (1,1) on duplicate key update f2=1; +affected rows: 0 +insert into t1 values (1,1) on duplicate key update f2=2; +affected rows: 2 +select * from t1; +f1 f2 +1 2 +drop table t1; diff --git a/mysql-test/t/insert_select.test b/mysql-test/t/insert_select.test index 404d67390ab..6302d5f1dae 100644 --- a/mysql-test/t/insert_select.test +++ b/mysql-test/t/insert_select.test @@ -265,4 +265,17 @@ INSERT INTO bug21774_2.t1 SELECT t1.* FROM t1; DROP DATABASE bug21774_1; DROP DATABASE bug21774_2; +USE test; +# +# Bug#19978: INSERT .. ON DUPLICATE erroneously reports some records were +# updated. +# +create table t1(f1 int primary key, f2 int); +--enable_info +insert into t1 values (1,1); +insert into t1 values (1,1) on duplicate key update f2=1; +insert into t1 values (1,1) on duplicate key update f2=2; +--disable_info +select * from t1; +drop table t1; diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index ce5df434295..1ffcad12414 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -701,6 +701,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, char* packet, uint packet_length); void log_slow_statement(THD *thd); bool check_dup(const char *db, const char *name, TABLE_LIST *tables); +bool compare_record(TABLE *table, query_id_t query_id); bool table_cache_init(void); void table_cache_free(void); diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index fb59aeea8e7..1ea01b07166 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -1186,23 +1186,29 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info) goto before_trg_err; if ((error=table->file->update_row(table->record[1],table->record[0]))) - { - if ((error == HA_ERR_FOUND_DUPP_KEY) && info->ignore) + { + if ((error == HA_ERR_FOUND_DUPP_KEY) && info->ignore) { table->file->restore_auto_increment(); goto ok_or_after_trg_err; } goto err; - } - info->updated++; + } + if ((table->file->table_flags() & HA_PARTIAL_COLUMN_READ) || + compare_record(table, query_id)) + { + info->updated++; - if (table->next_number_field) - table->file->adjust_next_insert_id_after_explicit_value(table->next_number_field->val_int()); + if (table->next_number_field) + table->file->adjust_next_insert_id_after_explicit_value( + table->next_number_field->val_int()); - trg_error= (table->triggers && - table->triggers->process_triggers(thd, TRG_EVENT_UPDATE, - TRG_ACTION_AFTER, TRUE)); - info->copied++; + trg_error= (table->triggers && + table->triggers->process_triggers(thd, TRG_EVENT_UPDATE, + TRG_ACTION_AFTER, + TRUE)); + info->copied++; + } goto ok_or_after_trg_err; } else /* DUP_REPLACE */ diff --git a/sql/sql_update.cc b/sql/sql_update.cc index b85c617b12d..04d341b5f67 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -26,7 +26,7 @@ /* Return 0 if row hasn't changed */ -static bool compare_record(TABLE *table, query_id_t query_id) +bool compare_record(TABLE *table, query_id_t query_id) { if (table->s->blob_fields + table->s->varchar_fields == 0) return cmp_record(table,record[1]); From 3a54e5930dc70da39cea42464448cdda14178603 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Feb 2007 00:18:36 -0800 Subject: [PATCH 4/5] Fixed bug #26124: SELECT from a view wrapper over a table with a column of the DATETIME type could return a wrong result set if the WHERE clause included a BETWEEN condition on the column. Fixed the method Item_func_between::fix_length_and_dec where the aggregation type for BETWEEN predicates calculated incorrectly if the first argument was a view column of the DATETIME type. mysql-test/r/view.result: Added a test case for bug #26124. mysql-test/t/view.test: Added a test case for bug #26124. --- mysql-test/r/view.result | 18 ++++++++++++++++++ mysql-test/t/view.test | 16 ++++++++++++++++ sql/item_cmpfunc.cc | 4 ++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index 7185a70689a..7720acd135a 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -3034,4 +3034,22 @@ SHOW CREATE VIEW v1; View Create View v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select _latin1'The\ZEnd' AS `TheEnd` DROP VIEW v1; +CREATE TABLE t1 (mydate DATETIME); +INSERT INTO t1 VALUES +('2007-01-01'), ('2007-01-02'), ('2007-01-30'), ('2007-01-31'); +CREATE VIEW v1 AS SELECT mydate from t1; +SELECT * FROM t1 WHERE mydate BETWEEN '2007-01-01' AND '2007-01-31'; +mydate +2007-01-01 00:00:00 +2007-01-02 00:00:00 +2007-01-30 00:00:00 +2007-01-31 00:00:00 +SELECT * FROM v1 WHERE mydate BETWEEN '2007-01-01' AND '2007-01-31'; +mydate +2007-01-01 00:00:00 +2007-01-02 00:00:00 +2007-01-30 00:00:00 +2007-01-31 00:00:00 +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 47a5a54007b..bd0329ddf4f 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -2986,4 +2986,20 @@ SHOW CREATE VIEW v1; DROP VIEW v1; +# +# Bug #26124: BETWEEN over a view column of the DATETIME type +# + +CREATE TABLE t1 (mydate DATETIME); +INSERT INTO t1 VALUES + ('2007-01-01'), ('2007-01-02'), ('2007-01-30'), ('2007-01-31'); + +CREATE VIEW v1 AS SELECT mydate from t1; + +SELECT * FROM t1 WHERE mydate BETWEEN '2007-01-01' AND '2007-01-31'; +SELECT * FROM v1 WHERE mydate BETWEEN '2007-01-01' AND '2007-01-31'; + +DROP VIEW v1; +DROP TABLE t1; + --echo End of 5.0 tests. diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index e3505d2b4a9..08f9c16384a 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -1185,11 +1185,11 @@ void Item_func_between::fix_length_and_dec() They are compared as integers, so for const item this time-consuming conversion can be done only once, not for every single comparison */ - if (args[0]->type() == FIELD_ITEM && + if (args[0]->real_item()->type() == FIELD_ITEM && thd->lex->sql_command != SQLCOM_CREATE_VIEW && thd->lex->sql_command != SQLCOM_SHOW_CREATE) { - Field *field=((Item_field*) args[0])->field; + Field *field=((Item_field*) (args[0]->real_item()))->field; if (field->can_be_compared_as_longlong()) { /* From 3609c3a4a3aaa42525d22d9de4f92c0ca7ffeeac Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 7 Feb 2007 14:41:57 -0800 Subject: [PATCH 5/5] Fixed bug #25931. View check option clauses were ignored for updates of multi-table views when the updates could not be performed on fly and the rows to update had to be put into temporary tables first. mysql-test/r/view.result: Added a test case for bug #25931. mysql-test/t/view.test: Added a test case for bug #25931. Adjusted another existed test case to have the correct result. sql/sql_update.cc: Fixed bug #25931. View check option clauses were ignored for updates of multi-table views when the updates could not be performed on fly and the rows to update had to be put into temporary tables first. Added the required check to multi_update::do_updates to fix the problem. --- mysql-test/r/view.result | 28 +++++++++++++++++++++++++++- mysql-test/t/view.test | 24 +++++++++++++++++++++++- sql/sql_update.cc | 9 +++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index 7720acd135a..a52882fef2f 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -2543,7 +2543,7 @@ create table t1(f1 int, f2 int); create view v1 as select ta.f1 as a, tb.f1 as b from t1 ta, t1 tb where ta.f1=tb .f1 and ta.f2=tb.f2; insert into t1 values(1,1),(2,2); -create view v2 as select * from v1 where a > 1 with check option; +create view v2 as select * from v1 where a > 1 with local check option; select * from v2; a b 2 2 @@ -3052,4 +3052,30 @@ mydate 2007-01-31 00:00:00 DROP VIEW v1; DROP TABLE t1; +CREATE TABLE t1 (a int); +CREATE TABLE t2 (b int); +INSERT INTO t1 VALUES (1), (2); +INSERT INTO t2 VALUES (1), (2); +CREATE VIEW v1 AS +SELECT t2.b FROM t1,t2 WHERE t1.a = t2.b WITH CHECK OPTION; +SELECT * FROM v1; +b +1 +2 +UPDATE v1 SET b=3; +ERROR HY000: CHECK OPTION failed 'test.v1' +SELECT * FROM v1; +b +1 +2 +SELECT * FROM t1; +a +1 +2 +SELECT * FROM t2; +b +1 +2 +DROP VIEW v1; +DROP TABLE t1,t2; End of 5.0 tests. diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index bd0329ddf4f..0fa5765bb64 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -2385,7 +2385,7 @@ create table t1(f1 int, f2 int); create view v1 as select ta.f1 as a, tb.f1 as b from t1 ta, t1 tb where ta.f1=tb .f1 and ta.f2=tb.f2; insert into t1 values(1,1),(2,2); -create view v2 as select * from v1 where a > 1 with check option; +create view v2 as select * from v1 where a > 1 with local check option; select * from v2; update v2 set b=3 where a=2; select * from v2; @@ -3002,4 +3002,26 @@ SELECT * FROM v1 WHERE mydate BETWEEN '2007-01-01' AND '2007-01-31'; DROP VIEW v1; DROP TABLE t1; +# +# Bug #25931: update of a multi-table view with check option +# + +CREATE TABLE t1 (a int); +CREATE TABLE t2 (b int); +INSERT INTO t1 VALUES (1), (2); +INSERT INTO t2 VALUES (1), (2); + +CREATE VIEW v1 AS + SELECT t2.b FROM t1,t2 WHERE t1.a = t2.b WITH CHECK OPTION; + +SELECT * FROM v1; +--error 1369 +UPDATE v1 SET b=3; +SELECT * FROM v1; +SELECT * FROM t1; +SELECT * FROM t2; + +DROP VIEW v1; +DROP TABLE t1,t2; + --echo End of 5.0 tests. diff --git a/sql/sql_update.cc b/sql/sql_update.cc index 04d341b5f67..1db77f8704c 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -1445,6 +1445,15 @@ int multi_update::do_updates(bool from_send_error) if (!can_compare_record || compare_record(table, thd->query_id)) { + int error; + if ((error= cur_table->view_check_option(thd, ignore)) != + VIEW_CHECK_OK) + { + if (error == VIEW_CHECK_SKIP) + continue; + else if (error == VIEW_CHECK_ERROR) + goto err; + } if ((local_error=table->file->update_row(table->record[1], table->record[0]))) {