From 6020281e9511f96cb99cf7d55643d75fa1a4a4a4 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 28 Oct 2005 01:24:11 +0400 Subject: [PATCH 1/4] Fix bug#14186 select datefield is null not updated Date field was declared as not null, thus expression 'datefield is null' was always false. For SELECT special handling of such cases is used. There 'datefield is null' converted to 'datefield eq "0000-00-00"'. In mysql_update() before creation of select added remove_eq_conds() call. It makes some optimization of conds and in particular performs conversion from 'is null' to 'eq'. Also remove_eq_conds() makes some evaluation of conds and if it founds that conds is always false then update statement is not processed further. All this allows to perform some update statements process faster due to optimized conds, and not wasting resources if conds known to be false. sql/sql_select.cc: Fix bug#14186 select datefield is null not updated Remove static from remove_eq_conds() sql/sql_select.h: Fix bug#14186 select datefield is null not updated Added remove_eq_conds() prototype. mysql-test/r/update.result: Test case for bug#14186 select datefield is null not updated mysql-test/t/update.test: Test case for bug#14186 select datefield is null not updated sql/sql_update.cc: Fix bug#14186 select datefield is null not updated To mysql_update() added call to remove_eq_conds() to optimize conds and convert 'datefield is null' to 'datefield eq 0000-00-00' --- mysql-test/r/update.result | 8 ++++++++ mysql-test/t/update.test | 9 +++++++++ sql/sql_select.cc | 4 +--- sql/sql_select.h | 1 + sql/sql_update.cc | 16 ++++++++++++---- 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/mysql-test/r/update.result b/mysql-test/r/update.result index 3408766d603..74c628f96c4 100644 --- a/mysql-test/r/update.result +++ b/mysql-test/r/update.result @@ -337,3 +337,11 @@ a b 22 3 23 3 drop table t1; +create table t1 (f1 date not null); +insert into t1 values('2000-01-01'),('0000-00-00'); +update t1 set f1='2002-02-02' where f1 is null; +select * from t1; +f1 +2000-01-01 +2002-02-02 +drop table t1; diff --git a/mysql-test/t/update.test b/mysql-test/t/update.test index e81415628d0..a21d10b6571 100644 --- a/mysql-test/t/update.test +++ b/mysql-test/t/update.test @@ -260,5 +260,14 @@ update t1 set a=a+11,b=2 order by a limit 3; update t1 set a=a+12,b=3 order by a limit 3; select * from t1 order by a; +drop table t1; + +# +# Bug#14186 select datefield is null not updated +# +create table t1 (f1 date not null); +insert into t1 values('2000-01-01'),('0000-00-00'); +update t1 set f1='2002-02-02' where f1 is null; +select * from t1; drop table t1; # End of 4.1 tests diff --git a/sql/sql_select.cc b/sql/sql_select.cc index c485d6cd04e..fe5b3d453de 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -69,8 +69,6 @@ static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables, SELECT_LEX_UNIT *unit); static COND *optimize_cond(THD *thd, COND *conds, Item::cond_result *cond_value); -static COND *remove_eq_conds(THD *thd, COND *cond, - Item::cond_result *cond_value); static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item); static bool open_tmp_table(TABLE *table); static bool create_myisam_tmp_table(TABLE *table,TMP_TABLE_PARAM *param, @@ -4611,7 +4609,7 @@ optimize_cond(THD *thd, COND *conds, Item::cond_result *cond_value) COND_FALSE always false ( 1 = 2 ) */ -static COND * +COND * remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value) { if (cond->type() == Item::COND_ITEM) diff --git a/sql/sql_select.h b/sql/sql_select.h index c7440fe4c3a..b92576587fe 100644 --- a/sql/sql_select.h +++ b/sql/sql_select.h @@ -456,3 +456,4 @@ bool cp_buffer_from_ref(THD *thd, TABLE_REF *ref); bool error_if_full_join(JOIN *join); int report_error(TABLE *table, int error); int safe_index_read(JOIN_TAB *tab); +COND *remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value); diff --git a/sql/sql_update.cc b/sql/sql_update.cc index b6bce800b0e..cb8064bef87 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -70,7 +70,7 @@ int mysql_update(THD *thd, ha_rows updated, found; key_map old_used_keys; TABLE *table; - SQL_SELECT *select; + SQL_SELECT *select= 0; READ_RECORD info; TABLE_LIST *update_table_list= ((TABLE_LIST*) thd->lex->select_lex.table_list.first); @@ -131,11 +131,19 @@ int mysql_update(THD *thd, DBUG_RETURN(-1); /* purecov: inspected */ } + if (conds) + { + Item::cond_result cond_value; + conds= remove_eq_conds(thd, conds, &cond_value); + if (cond_value == Item::COND_FALSE) + limit= 0; // Impossible WHERE + } // Don't count on usage of 'only index' when calculating which key to use table->used_keys.clear_all(); - select=make_select(table,0,0,conds,&error); - if (error || - (select && select->check_quick(thd, safe_update, limit)) || !limit) + if (limit) + select=make_select(table,0,0,conds,&error); + if (error || !limit || + (select && select->check_quick(thd, safe_update, limit))) { delete select; free_underlaid_joins(thd, &thd->lex->select_lex); From e86e844f3e6ca709c224c0d19b7ad8c0c584c768 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 31 Oct 2005 16:28:45 +0400 Subject: [PATCH 2/4] Check for NULLs only if we don't replace column results, get real results after all checks. (see bug #14254: func_crypt.test fails on FreeBSD with --ps-protocol). client/mysqltest.c: Check for NULLs only if we don't replace column results, get real results after all checks. --- client/mysqltest.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/client/mysqltest.c b/client/mysqltest.c index 35408368a73..d10ad054798 100644 --- a/client/mysqltest.c +++ b/client/mysqltest.c @@ -3272,19 +3272,24 @@ static int run_query_stmt(MYSQL *mysql, struct st_query *q, int flags) /* Read result from each column */ for (col_idx= 0; col_idx < num_fields; col_idx++) { - /* FIXME is string terminated? */ - const char *val= (const char *)bind[col_idx].buffer; - ulonglong len= *bind[col_idx].length; + const char *val; + ulonglong len; if (col_idx < max_replace_column && replace_column[col_idx]) { val= replace_column[col_idx]; len= strlen(val); } - if (*bind[col_idx].is_null) + else if (*bind[col_idx].is_null) { val= "NULL"; len= 4; } + else + { + /* FIXME is string terminated? */ + val= (const char *) bind[col_idx].buffer; + len= *bind[col_idx].length; + } if (!display_result_vertically) { if (col_idx) /* No tab before first col */ From b1a1b4815fa31dc638cb89524f0776f301cdb16f Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 31 Oct 2005 18:35:26 +0100 Subject: [PATCH 3/4] Accept any shared library for "libz", not just the static one. Bug#6584 --- acinclude.m4 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/acinclude.m4 b/acinclude.m4 index 9c7271f7cc9..0a5778285ea 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -310,8 +310,9 @@ case $SYSTEM_TYPE in fi ;; *) - if test -f "$mysql_zlib_dir/lib/libz.a" -a \ - -f "$mysql_zlib_dir/include/zlib.h"; then + if test \( -f "$mysql_zlib_dir/lib/libz.a" -o -f "$mysql_zlib_dir/lib/libz.so" -o \ + -f "$mysql_zlib_dir/lib/libz.sl" -o -f "$mysql_zlib_dir/lib/libz.dylib" \) \ + -a -f "$mysql_zlib_dir/include/zlib.h"; then ZLIB_INCLUDES="-I$mysql_zlib_dir/include" ZLIB_LIBS="-L$mysql_zlib_dir/lib -lz" MYSQL_CHECK_ZLIB_DIR From fffe74170576439325ae7b59bf56e55bbeeee5c4 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 1 Nov 2005 13:00:02 +0200 Subject: [PATCH 4/4] Review of new pushed code Removed wrong fix for bug #14009 (use of abs() on null value causes problems with filesort) Mark that add_time(), time_diff() and str_to_date() can return null values myisam/mi_check.c: keyoffset is not a position (no %lx here) mysql-test/r/func_sapdb.result: Fixed test after marking that timediff() can return NULL sql/item_func.cc: Removed wrong fix for bug #14009 (use of abs() on null value causes problems with filesort) sql/item_timefunc.cc: Mark that add_time and str_to_date() can return null values sql/item_timefunc.h: Mark that time_diff can return 0 sql/spatial.cc: Simple cleanups during review of new code --- myisam/mi_check.c | 2 +- mysql-test/r/func_sapdb.result | 4 ++-- sql/item_func.cc | 1 - sql/item_timefunc.cc | 4 +++- sql/item_timefunc.h | 1 + sql/spatial.cc | 39 +++++++++++++++++++--------------- 6 files changed, 29 insertions(+), 22 deletions(-) diff --git a/myisam/mi_check.c b/myisam/mi_check.c index 8783a5fef54..15d1cceebfe 100644 --- a/myisam/mi_check.c +++ b/myisam/mi_check.c @@ -1839,7 +1839,7 @@ static int sort_one_index(MI_CHECK *param, MI_INFO *info, MI_KEYDEF *keyinfo, if (sort_one_index(param,info,keyinfo,next_page,new_file)) { DBUG_PRINT("error", - ("From page: %ld, keyoffset: 0x%lx used_length: %d", + ("From page: %ld, keyoffset: %lu used_length: %d", (ulong) pagepos, (ulong) (keypos - buff), (int) used_length)); DBUG_DUMP("buff",(byte*) buff,used_length); diff --git a/mysql-test/r/func_sapdb.result b/mysql-test/r/func_sapdb.result index 68c3baa7bde..ea40e1559fd 100644 --- a/mysql-test/r/func_sapdb.result +++ b/mysql-test/r/func_sapdb.result @@ -170,8 +170,8 @@ Field Type Null Key Default Extra f1 date 0000-00-00 f2 datetime YES NULL f3 time YES NULL -f4 time 00:00:00 -f5 time 00:00:00 +f4 time YES NULL +f5 time YES NULL f6 time 00:00:00 f7 datetime YES NULL f8 date YES NULL diff --git a/sql/item_func.cc b/sql/item_func.cc index aff4adb788a..df32672e12b 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -766,7 +766,6 @@ void Item_func_abs::fix_length_and_dec() hybrid_type= REAL_RESULT; if (args[0]->result_type() == INT_RESULT) hybrid_type= INT_RESULT; - maybe_null= 1; } diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc index 7398b1746da..eb58b180ed7 100644 --- a/sql/item_timefunc.cc +++ b/sql/item_timefunc.cc @@ -2381,6 +2381,7 @@ void Item_func_add_time::fix_length_and_dec() enum_field_types arg0_field_type; decimals=0; max_length=MAX_DATETIME_FULL_WIDTH*MY_CHARSET_BIN_MB_MAXLEN; + maybe_null= 1; /* The field type for the result of an Item_func_add_time function is defined @@ -2742,7 +2743,8 @@ Field *Item_func_str_to_date::tmp_table_field(TABLE *t_arg) void Item_func_str_to_date::fix_length_and_dec() { char format_buff[64]; - String format_str(format_buff, sizeof(format_buff), &my_charset_bin), *format; + String format_str(format_buff, sizeof(format_buff), &my_charset_bin); + String *format; maybe_null= 1; decimals=0; cached_field_type= MYSQL_TYPE_STRING; diff --git a/sql/item_timefunc.h b/sql/item_timefunc.h index 0df84d14bea..16c64620369 100644 --- a/sql/item_timefunc.h +++ b/sql/item_timefunc.h @@ -808,6 +808,7 @@ public: { decimals=0; max_length=MAX_TIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN; + maybe_null= 1; } Field *tmp_table_field(TABLE *t_arg) { diff --git a/sql/spatial.cc b/sql/spatial.cc index 9e72dfb9130..684f7e9ecf3 100644 --- a/sql/spatial.cc +++ b/sql/spatial.cc @@ -127,15 +127,14 @@ Geometry *Geometry::construct(Geometry_buffer *buffer, Geometry *result; char byte_order; - if (data_len < SRID_SIZE + 1 + 4) + if (data_len < SRID_SIZE + WKB_HEADER_SIZE) // < 4 + (1 + 4) return NULL; byte_order= data[SRID_SIZE]; geom_type= uint4korr(data + SRID_SIZE + 1); - data+= SRID_SIZE + WKB_HEADER_SIZE; if (!(result= create_by_typeid(buffer, (int) geom_type))) return NULL; - result->m_data= data; - result->m_data_end= data + (data_len - (SRID_SIZE + WKB_HEADER_SIZE)); + result->m_data= data+ SRID_SIZE + WKB_HEADER_SIZE; + result->m_data_end= data + data_len; return result; } @@ -737,7 +736,7 @@ uint Gis_polygon::init_from_wkb(const char *wkb, uint len, wkbByteOrder bo, wkb+= ls_len; } - return wkb - wkb_orig; + return (uint) (wkb - wkb_orig); } @@ -1182,7 +1181,8 @@ uint Gis_multi_line_string::init_from_wkb(const char *wkb, uint len, return 0; res->q_append(n_line_strings); - for (wkb+=4; n_line_strings; n_line_strings--) + wkb+= 4; + while (n_line_strings--) { Gis_line_string ls; int ls_len; @@ -1197,10 +1197,11 @@ uint Gis_multi_line_string::init_from_wkb(const char *wkb, uint len, if (!(ls_len= ls.init_from_wkb(wkb + WKB_HEADER_SIZE, len, (wkbByteOrder) wkb[0], res))) return 0; - wkb+= (ls_len + WKB_HEADER_SIZE); - len-= (ls_len + WKB_HEADER_SIZE); + ls_len+= WKB_HEADER_SIZE;; + wkb+= ls_len; + len-= ls_len; } - return wkb-wkb_orig; + return (uint) (wkb - wkb_orig); } @@ -1434,7 +1435,8 @@ uint Gis_multi_polygon::init_from_wkb(const char *wkb, uint len, return 0; res->q_append(n_poly); - for (wkb+=4; n_poly; n_poly--) + wkb+=4; + while (n_poly--) { Gis_polygon p; int p_len; @@ -1448,10 +1450,11 @@ uint Gis_multi_polygon::init_from_wkb(const char *wkb, uint len, if (!(p_len= p.init_from_wkb(wkb + WKB_HEADER_SIZE, len, (wkbByteOrder) wkb[0], res))) return 0; - wkb+= (p_len + WKB_HEADER_SIZE); - len-= (p_len + WKB_HEADER_SIZE); + p_len+= WKB_HEADER_SIZE; + wkb+= p_len; + len-= p_len; } - return wkb-wkb_orig; + return (uint) (wkb - wkb_orig); } @@ -1731,7 +1734,8 @@ uint Gis_geometry_collection::init_from_wkb(const char *wkb, uint len, return 0; res->q_append(n_geom); - for (wkb+=4; n_geom; n_geom--) + wkb+= 4; + while (n_geom--) { Geometry_buffer buffer; Geometry *geom; @@ -1750,10 +1754,11 @@ uint Gis_geometry_collection::init_from_wkb(const char *wkb, uint len, !(g_len= geom->init_from_wkb(wkb + WKB_HEADER_SIZE, len, (wkbByteOrder) wkb[0], res))) return 0; - wkb+= (g_len + WKB_HEADER_SIZE); - len-= (g_len + WKB_HEADER_SIZE); + g_len+= WKB_HEADER_SIZE; + wkb+= g_len; + len-= g_len; } - return wkb-wkb_orig; + return (uint) (wkb - wkb_orig); }