diff --git a/mysql-test/r/cache_temporal_4265.result b/mysql-test/r/cache_temporal_4265.result index 980bb957e19..7f215de43fb 100644 --- a/mysql-test/r/cache_temporal_4265.result +++ b/mysql-test/r/cache_temporal_4265.result @@ -7,7 +7,6 @@ a 2002-03-04 Warnings: Note 1003 2000-01-01 -Note 1003 2000-01-06 set debug_dbug=''; drop table t1; create table t1 (id int not null, ut timestamp(6) not null); diff --git a/sql/item.cc b/sql/item.cc index 999b64fcc1e..ea3123168b2 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -109,6 +109,19 @@ void Item::push_note_converted_to_positive_complement(THD *thd) } +longlong Item::val_datetime_packed_result() +{ + MYSQL_TIME ltime, tmp; + if (get_date_result(<ime, TIME_FUZZY_DATES | TIME_INVALID_DATES)) + return 0; + if (ltime.time_type != MYSQL_TIMESTAMP_TIME) + return pack_time(<ime); + if ((null_value= time_to_datetime_with_warn(current_thd, <ime, &tmp, 0))) + return 0; + return pack_time(&tmp); +} + + /** Get date/time/datetime. Optionally extend TIME result to DATETIME. @@ -9676,14 +9689,19 @@ bool Item_cache_temporal::cache_value() { if (!example) return false; - value_cached= true; - - MYSQL_TIME ltime; - if (example->get_date_result(<ime, 0)) - value=0; - else - value= pack_time(<ime); + value= example->val_datetime_packed_result(); + null_value= example->null_value; + return true; +} + + +bool Item_cache_time::cache_value() +{ + if (!example) + return false; + value_cached= true; + value= example->val_time_packed_result(); null_value= example->null_value; return true; } @@ -9734,8 +9752,8 @@ void Item_cache_temporal::store_packed(longlong val_arg, Item *example_arg) Item *Item_cache_temporal::clone_item(THD *thd) { - Item_cache_temporal *item= new (thd->mem_root) - Item_cache_temporal(thd, Item_cache_temporal::type_handler()); + Item_cache *tmp= type_handler()->Item_get_cache(thd, this); + Item_cache_temporal *item= static_cast(tmp); item->store_packed(value, example); return item; } diff --git a/sql/item.h b/sql/item.h index d3636ccc263..b9f8004278d 100644 --- a/sql/item.h +++ b/sql/item.h @@ -1397,6 +1397,14 @@ public: ulonglong fuzzydate= TIME_FUZZY_DATES | TIME_INVALID_DATES | TIME_TIME_ONLY; return get_date(<ime, fuzzydate) ? 0 : pack_time(<ime); } + longlong val_datetime_packed_result(); + longlong val_time_packed_result() + { + MYSQL_TIME ltime; + ulonglong fuzzydate= TIME_FUZZY_DATES | TIME_INVALID_DATES | TIME_TIME_ONLY; + return get_date_result(<ime, fuzzydate) ? 0 : pack_time(<ime); + } + // Get a temporal value in packed DATE/DATETIME or TIME format longlong val_temporal_packed(enum_field_types f_type) { @@ -5760,8 +5768,9 @@ public: class Item_cache_temporal: public Item_cache_int { -public: +protected: Item_cache_temporal(THD *thd, const Type_handler *handler); +public: String* val_str(String *str); my_decimal *val_decimal(my_decimal *); longlong val_int(); @@ -5779,8 +5788,37 @@ public: */ Item *clone_item(THD *thd); Item *convert_to_basic_const_item(THD *thd); +}; + + +class Item_cache_time: public Item_cache_temporal +{ +public: + Item_cache_time(THD *thd) + :Item_cache_temporal(thd, &type_handler_time2) { } + bool cache_value(); Item *get_copy(THD *thd, MEM_ROOT *mem_root) - { return get_item_copy(thd, mem_root, this); } + { return get_item_copy(thd, mem_root, this); } +}; + + +class Item_cache_datetime: public Item_cache_temporal +{ +public: + Item_cache_datetime(THD *thd) + :Item_cache_temporal(thd, &type_handler_datetime2) { } + Item *get_copy(THD *thd, MEM_ROOT *mem_root) + { return get_item_copy(thd, mem_root, this); } +}; + + +class Item_cache_date: public Item_cache_temporal +{ +public: + Item_cache_date(THD *thd) + :Item_cache_temporal(thd, &type_handler_newdate) { } + Item *get_copy(THD *thd, MEM_ROOT *mem_root) + { return get_item_copy(thd, mem_root, this); } }; diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index 9954c66ebc2..90b150a8b4a 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -569,18 +569,24 @@ bool Arg_comparator::set_cmp_func_string() bool Arg_comparator::set_cmp_func_time() { + THD *thd= current_thd; m_compare_collation= &my_charset_numeric; func= is_owner_equal_func() ? &Arg_comparator::compare_e_time : &Arg_comparator::compare_time; + a= cache_converted_constant(thd, a, &a_cache, compare_type_handler()); + b= cache_converted_constant(thd, b, &b_cache, compare_type_handler()); return false; } bool Arg_comparator::set_cmp_func_datetime() { + THD *thd= current_thd; m_compare_collation= &my_charset_numeric; func= is_owner_equal_func() ? &Arg_comparator::compare_e_datetime : &Arg_comparator::compare_datetime; + a= cache_converted_constant(thd, a, &a_cache, compare_type_handler()); + b= cache_converted_constant(thd, b, &b_cache, compare_type_handler()); return false; } @@ -682,18 +688,13 @@ Item** Arg_comparator::cache_converted_constant(THD *thd_arg, Item **value, Item **cache_item, const Type_handler *handler) { - /* - get_datetime_value creates Item_cache internally when comparing - values for the first row. - Arg_comparator::cache_converted_constant() is never called for TIME_RESULT. - */ - DBUG_ASSERT(handler->cmp_type() != TIME_RESULT); /* Don't need cache if doing context analysis only. */ if (!thd_arg->lex->is_ps_or_view_context_analysis() && (*value)->const_item() && - handler->cmp_type() != (*value)->cmp_type()) + handler->type_handler_for_comparison() != + (*value)->type_handler_for_comparison()) { Item_cache *cache= handler->Item_get_cache(thd_arg, *value); cache->setup(thd_arg, *value); @@ -750,7 +751,8 @@ get_datetime_value(THD *thd, Item ***item_arg, Item **cache_arg, if (!thd) thd= current_thd; const Type_handler *h= Type_handler::get_handler_by_field_type(f_type); - Item_cache_temporal *cache= new (thd->mem_root) Item_cache_temporal(thd, h); + Item_cache *tmp_cache= h->Item_get_cache(thd, item); + Item_cache_temporal *cache= static_cast(tmp_cache); cache->store_packed(value, item); *cache_arg= cache; *item_arg= cache_arg; @@ -759,63 +761,57 @@ get_datetime_value(THD *thd, Item ***item_arg, Item **cache_arg, } -/* - Compare items values as dates. - - SYNOPSIS - Arg_comparator::compare_datetime() - - DESCRIPTION - Compare items values as DATE/DATETIME for both EQUAL_FUNC and from other - comparison functions. The correct DATETIME values are obtained - with help of the get_datetime_value() function. - - RETURN - -1 a < b or at least one item is null - 0 a == b - 1 a > b -*/ - -int Arg_comparator::compare_temporal(enum_field_types type) +int Arg_comparator::compare_time() { - bool a_is_null, b_is_null; - longlong a_value, b_value; - + longlong val1= (*a)->val_time_packed(); + if (!(*a)->null_value) + { + longlong val2= (*b)->val_time_packed(); + if (!(*b)->null_value) + return compare_not_null_values(val1, val2); + } if (set_null) - owner->null_value= 1; - - /* Get DATE/DATETIME/TIME value of the 'a' item. */ - a_value= get_datetime_value(0, &a, &a_cache, type, &a_is_null); - if (a_is_null) - return -1; - - /* Get DATE/DATETIME/TIME value of the 'b' item. */ - b_value= get_datetime_value(0, &b, &b_cache, type, &b_is_null); - if (b_is_null) - return -1; - - /* Here we have two not-NULL values. */ - if (set_null) - owner->null_value= 0; - - /* Compare values. */ - return a_value < b_value ? -1 : a_value > b_value ? 1 : 0; + owner->null_value= true; + return -1; } -int Arg_comparator::compare_e_temporal(enum_field_types type) + +int Arg_comparator::compare_e_time() { - bool a_is_null, b_is_null; - longlong a_value, b_value; - - /* Get DATE/DATETIME/TIME value of the 'a' item. */ - a_value= get_datetime_value(0, &a, &a_cache, type, &a_is_null); - - /* Get DATE/DATETIME/TIME value of the 'b' item. */ - b_value= get_datetime_value(0, &b, &b_cache, type, &b_is_null); - return a_is_null || b_is_null ? a_is_null == b_is_null - : a_value == b_value; + longlong val1= (*a)->val_time_packed(); + longlong val2= (*b)->val_time_packed(); + if ((*a)->null_value || (*b)->null_value) + return MY_TEST((*a)->null_value && (*b)->null_value); + return MY_TEST(val1 == val2); } + + +int Arg_comparator::compare_datetime() +{ + longlong val1= (*a)->val_datetime_packed(); + if (!(*a)->null_value) + { + longlong val2= (*b)->val_datetime_packed(); + if (!(*b)->null_value) + return compare_not_null_values(val1, val2); + } + if (set_null) + owner->null_value= true; + return -1; +} + + +int Arg_comparator::compare_e_datetime() +{ + longlong val1= (*a)->val_datetime_packed(); + longlong val2= (*b)->val_datetime_packed(); + if ((*a)->null_value || (*b)->null_value) + return MY_TEST((*a)->null_value && (*b)->null_value); + return MY_TEST(val1 == val2); +} + + int Arg_comparator::compare_string() { String *res1,*res2; @@ -962,13 +958,7 @@ int Arg_comparator::compare_int_signed() { longlong val2= (*b)->val_int(); if (!(*b)->null_value) - { - if (set_null) - owner->null_value= 0; - if (val1 < val2) return -1; - if (val1 == val2) return 0; - return 1; - } + return compare_not_null_values(val1, val2); } if (set_null) owner->null_value= 1; @@ -2112,38 +2102,40 @@ bool Item_func_between::fix_length_and_dec_numeric(THD *thd) } +bool Item_func_between::fix_length_and_dec_temporal(THD *thd) +{ + if (!thd->lex->is_ps_or_view_context_analysis()) + { + for (uint i= 0; i < 3; i ++) + { + if (args[i]->const_item() && + args[i]->type_handler_for_comparison() != m_comparator.type_handler()) + { + Item_cache *cache= m_comparator.type_handler()->Item_get_cache(thd, args[i]); + if (!cache || cache->setup(thd, args[i])) + return true; + thd->change_item_tree(&args[i], cache); + } + } + } + return false; +} + + longlong Item_func_between::val_int_cmp_temporal() { - THD *thd= current_thd; - longlong value, a, b; - Item *cache, **ptr; - bool value_is_null, a_is_null, b_is_null; - - ptr= &args[0]; enum_field_types f_type= m_comparator.type_handler()->field_type(); - value= get_datetime_value(thd, &ptr, &cache, f_type, &value_is_null); - if (ptr != &args[0]) - thd->change_item_tree(&args[0], *ptr); - - if ((null_value= value_is_null)) + longlong value= args[0]->val_temporal_packed(f_type), a, b; + if ((null_value= args[0]->null_value)) return 0; - - ptr= &args[1]; - a= get_datetime_value(thd, &ptr, &cache, f_type, &a_is_null); - if (ptr != &args[1]) - thd->change_item_tree(&args[1], *ptr); - - ptr= &args[2]; - b= get_datetime_value(thd, &ptr, &cache, f_type, &b_is_null); - if (ptr != &args[2]) - thd->change_item_tree(&args[2], *ptr); - - if (!a_is_null && !b_is_null) + a= args[1]->val_temporal_packed(f_type); + b= args[2]->val_temporal_packed(f_type); + if (!args[1]->null_value && !args[2]->null_value) return (longlong) ((value >= a && value <= b) != negated); - if (a_is_null && b_is_null) + if (args[1]->null_value && args[2]->null_value) null_value= true; - else if (a_is_null) - null_value= value <= b; // not null if false range. + else if (args[1]->null_value) + null_value= value <= b; // not null if false range. else null_value= value >= a; return (longlong) (!null_value && negated); diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h index f0862608463..62066ef61e5 100644 --- a/sql/item_cmpfunc.h +++ b/sql/item_cmpfunc.h @@ -60,9 +60,14 @@ class Arg_comparator: public Sql_alloc int set_cmp_func(Item_func_or_sum *owner_arg, Item **a1, Item **a2); - int compare_temporal(enum_field_types type); - int compare_e_temporal(enum_field_types type); - + int compare_not_null_values(longlong val1, longlong val2) + { + if (set_null) + owner->null_value= false; + if (val1 < val2) return -1; + if (val1 == val2) return 0; + return 1; + } public: /* Allow owner function to use string buffers. */ String value1, value2; @@ -112,10 +117,10 @@ public: int compare_e_row(); // compare args[0] & args[1] int compare_real_fixed(); int compare_e_real_fixed(); - int compare_datetime() { return compare_temporal(MYSQL_TYPE_DATETIME); } - int compare_e_datetime() { return compare_e_temporal(MYSQL_TYPE_DATETIME); } - int compare_time() { return compare_temporal(MYSQL_TYPE_TIME); } - int compare_e_time() { return compare_e_temporal(MYSQL_TYPE_TIME); } + int compare_datetime(); + int compare_e_datetime(); + int compare_time(); + int compare_e_time(); int compare_json_str_basic(Item *j, Item *s); int compare_json_str(); int compare_str_json(); @@ -899,6 +904,7 @@ public: { return agg_arg_charsets_for_comparison(cmp_collation, args, 3); } + bool fix_length_and_dec_temporal(THD *); bool fix_length_and_dec_numeric(THD *); virtual void print(String *str, enum_query_type query_type); bool eval_not_null_tables(void *opt_arg); diff --git a/sql/sql_type.cc b/sql/sql_type.cc index d6b861eaaf2..5d16a49c696 100644 --- a/sql/sql_type.cc +++ b/sql/sql_type.cc @@ -2661,9 +2661,27 @@ Type_handler_string_result::Item_get_cache(THD *thd, const Item *item) const } Item_cache * -Type_handler_temporal_result::Item_get_cache(THD *thd, const Item *item) const +Type_handler_timestamp_common::Item_get_cache(THD *thd, const Item *item) const { - return new (thd->mem_root) Item_cache_temporal(thd, item->type_handler()); + return new (thd->mem_root) Item_cache_datetime(thd); +} + +Item_cache * +Type_handler_datetime_common::Item_get_cache(THD *thd, const Item *item) const +{ + return new (thd->mem_root) Item_cache_datetime(thd); +} + +Item_cache * +Type_handler_time_common::Item_get_cache(THD *thd, const Item *item) const +{ + return new (thd->mem_root) Item_cache_time(thd); +} + +Item_cache * +Type_handler_date_common::Item_get_cache(THD *thd, const Item *item) const +{ + return new (thd->mem_root) Item_cache_date(thd); } /*************************************************************************/ @@ -3505,7 +3523,7 @@ bool Type_handler_numeric:: bool Type_handler_temporal_result:: Item_func_between_fix_length_and_dec(Item_func_between *func) const { - return func->fix_length_and_dec_numeric(current_thd); + return func->fix_length_and_dec_temporal(current_thd); } bool Type_handler_string_result:: @@ -5233,7 +5251,7 @@ Item *Type_handler_time_common:: longlong value= item->val_time_packed(); if (item->null_value) return new (thd->mem_root) Item_null(thd, item->name.str); - cache= new (thd->mem_root) Item_cache_temporal(thd, this); + cache= new (thd->mem_root) Item_cache_time(thd); if (cache) cache->store_packed(value, item); return cache; @@ -5247,7 +5265,7 @@ Item *Type_handler_temporal_with_date:: longlong value= item->val_datetime_packed(); if (item->null_value) return new (thd->mem_root) Item_null(thd, item->name.str); - cache= new (thd->mem_root) Item_cache_temporal(thd, this); + cache= new (thd->mem_root) Item_cache_datetime(thd); if (cache) cache->store_packed(value, item); return cache; diff --git a/sql/sql_type.h b/sql/sql_type.h index c2e2127994a..ec1b5c54c2b 100644 --- a/sql/sql_type.h +++ b/sql/sql_type.h @@ -1578,7 +1578,6 @@ public: Item *source_expr, Item *source_const) const; bool subquery_type_allows_materialization(const Item *inner, const Item *outer) const; - Item_cache *Item_get_cache(THD *thd, const Item *item) const; bool Item_sum_hybrid_fix_length_and_dec(Item_sum_hybrid *func) const; bool Item_sum_sum_fix_length_and_dec(Item_sum_sum *) const; bool Item_sum_avg_fix_length_and_dec(Item_sum_avg *) const; @@ -2060,6 +2059,7 @@ public: } int Item_save_in_field(Item *item, Field *field, bool no_conversions) const; String *print_item_value(THD *thd, Item *item, String *str) const; + Item_cache *Item_get_cache(THD *thd, const Item *item) const; bool Item_hybrid_func_fix_attributes(THD *thd, const char *name, Type_handler_hybrid_field_type *, @@ -2146,6 +2146,7 @@ public: bool Column_definition_fix_attributes(Column_definition *c) const; uint Item_decimal_precision(const Item *item) const; String *print_item_value(THD *thd, Item *item, String *str) const; + Item_cache *Item_get_cache(THD *thd, const Item *item) const; bool Item_hybrid_func_fix_attributes(THD *thd, const char *name, Type_handler_hybrid_field_type *, @@ -2219,6 +2220,7 @@ public: return Item_send_datetime(item, protocol, buf); } String *print_item_value(THD *thd, Item *item, String *str) const; + Item_cache *Item_get_cache(THD *thd, const Item *item) const; bool Item_hybrid_func_fix_attributes(THD *thd, const char *name, Type_handler_hybrid_field_type *, @@ -2298,6 +2300,7 @@ public: return Item_send_datetime(item, protocol, buf); } String *print_item_value(THD *thd, Item *item, String *str) const; + Item_cache *Item_get_cache(THD *thd, const Item *item) const; bool Item_hybrid_func_fix_attributes(THD *thd, const char *name, Type_handler_hybrid_field_type *,