00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00024 #include <config.h>
00025
00026 #include <drizzled/cached_item.h>
00027 #include <drizzled/check_stack_overrun.h>
00028 #include <drizzled/current_session.h>
00029 #include <drizzled/error.h>
00030 #include <drizzled/internal/my_sys.h>
00031 #include <drizzled/item/cache_int.h>
00032 #include <drizzled/item/cmpfunc.h>
00033 #include <drizzled/item/int_with_ref.h>
00034 #include <drizzled/item/subselect.h>
00035 #include <drizzled/session.h>
00036 #include <drizzled/sql_lex.h>
00037 #include <drizzled/sql_select.h>
00038 #include <drizzled/system_variables.h>
00039 #include <drizzled/temporal.h>
00040 #include <drizzled/time_functions.h>
00041
00042 #include <math.h>
00043 #include <algorithm>
00044
00045 using namespace std;
00046
00047 namespace drizzled {
00048
00049 extern const double log_10[309];
00050
00051 static Eq_creator eq_creator;
00052 static Ne_creator ne_creator;
00053 static Gt_creator gt_creator;
00054 static Lt_creator lt_creator;
00055 static Ge_creator ge_creator;
00056 static Le_creator le_creator;
00057
00058 static bool convert_constant_item(Session *, Item_field *, Item **);
00059
00060 static Item_result item_store_type(Item_result a, Item *item,
00061 bool unsigned_flag)
00062 {
00063 Item_result b= item->result_type();
00064
00065 if (a == STRING_RESULT || b == STRING_RESULT)
00066 return STRING_RESULT;
00067 else if (a == REAL_RESULT || b == REAL_RESULT)
00068 return REAL_RESULT;
00069 else if (a == DECIMAL_RESULT || b == DECIMAL_RESULT ||
00070 unsigned_flag != item->unsigned_flag)
00071 return DECIMAL_RESULT;
00072 else
00073 return INT_RESULT;
00074 }
00075
00076 static void agg_result_type(Item_result *type, Item **items, uint32_t nitems)
00077 {
00078 Item **item, **item_end;
00079 bool unsigned_flag= 0;
00080
00081 *type= STRING_RESULT;
00082
00083 for (item= items, item_end= item + nitems; item < item_end; item++)
00084 {
00085 if ((*item)->type() != Item::NULL_ITEM)
00086 {
00087 *type= (*item)->result_type();
00088 unsigned_flag= (*item)->unsigned_flag;
00089 item++;
00090 break;
00091 }
00092 }
00093
00094 for (; item < item_end; item++)
00095 {
00096 if ((*item)->type() != Item::NULL_ITEM)
00097 {
00098 *type= item_store_type(*type, *item, unsigned_flag);
00099 }
00100 }
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 static int cmp_row_type(Item* item1, Item* item2)
00125 {
00126 uint32_t n= item1->cols();
00127 if (item2->check_cols(n))
00128 return 1;
00129 for (uint32_t i=0; i<n; i++)
00130 {
00131 if (item2->element_index(i)->check_cols(item1->element_index(i)->cols()) ||
00132 (item1->element_index(i)->result_type() == ROW_RESULT &&
00133 cmp_row_type(item1->element_index(i), item2->element_index(i))))
00134 return 1;
00135 }
00136 return 0;
00137 }
00138
00139
00163 static int agg_cmp_type(Item_result *type, Item **items, uint32_t nitems)
00164 {
00165 uint32_t i;
00166 type[0]= items[0]->result_type();
00167 for (i= 1 ; i < nitems ; i++)
00168 {
00169 type[0]= item_cmp_type(type[0], items[i]->result_type());
00170
00171
00172
00173
00174
00175
00176
00177 if (type[0] == ROW_RESULT && cmp_row_type(items[0], items[i]))
00178 return 1;
00179 }
00180 return 0;
00181 }
00182
00183
00202 enum_field_types agg_field_type(Item **items, uint32_t nitems)
00203 {
00204 uint32_t i;
00205 if (!nitems || items[0]->result_type() == ROW_RESULT )
00206 return (enum_field_types)-1;
00207 enum_field_types res= items[0]->field_type();
00208 for (i= 1 ; i < nitems ; i++)
00209 res= Field::field_type_merge(res, items[i]->field_type());
00210 return res;
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 static uint32_t collect_cmp_types(Item **items, uint32_t nitems, bool skip_nulls= false)
00232 {
00233 uint32_t i;
00234 uint32_t found_types;
00235 Item_result left_result= items[0]->result_type();
00236 assert(nitems > 1);
00237 found_types= 0;
00238 for (i= 1; i < nitems ; i++)
00239 {
00240 if (skip_nulls && items[i]->type() == Item::NULL_ITEM)
00241 continue;
00242 if ((left_result == ROW_RESULT ||
00243 items[i]->result_type() == ROW_RESULT) &&
00244 cmp_row_type(items[0], items[i]))
00245 return 0;
00246 found_types|= 1<< (uint32_t)item_cmp_type(left_result,
00247 items[i]->result_type());
00248 }
00249
00250
00251
00252
00253 if (skip_nulls && !found_types)
00254 found_types= 1 << (uint)left_result;
00255 return found_types;
00256 }
00257
00258
00259 Item_bool_func2* Eq_creator::create(Item *a, Item *b) const
00260 {
00261 return new Item_func_eq(a, b);
00262 }
00263
00264
00265 const Eq_creator* Eq_creator::instance()
00266 {
00267 return &eq_creator;
00268 }
00269
00270
00271 Item_bool_func2* Ne_creator::create(Item *a, Item *b) const
00272 {
00273 return new Item_func_ne(a, b);
00274 }
00275
00276
00277 const Ne_creator* Ne_creator::instance()
00278 {
00279 return &ne_creator;
00280 }
00281
00282
00283 Item_bool_func2* Gt_creator::create(Item *a, Item *b) const
00284 {
00285 return new Item_func_gt(a, b);
00286 }
00287
00288
00289 const Gt_creator* Gt_creator::instance()
00290 {
00291 return >_creator;
00292 }
00293
00294
00295 Item_bool_func2* Lt_creator::create(Item *a, Item *b) const
00296 {
00297 return new Item_func_lt(a, b);
00298 }
00299
00300
00301 const Lt_creator* Lt_creator::instance()
00302 {
00303 return <_creator;
00304 }
00305
00306
00307 Item_bool_func2* Ge_creator::create(Item *a, Item *b) const
00308 {
00309 return new Item_func_ge(a, b);
00310 }
00311
00312
00313 const Ge_creator* Ge_creator::instance()
00314 {
00315 return &ge_creator;
00316 }
00317
00318
00319 Item_bool_func2* Le_creator::create(Item *a, Item *b) const
00320 {
00321 return new Item_func_le(a, b);
00322 }
00323
00324 const Le_creator* Le_creator::instance()
00325 {
00326 return &le_creator;
00327 }
00328
00329
00330
00331
00332
00333
00334
00335
00336 int64_t Item_func_not::val_int()
00337 {
00338 assert(fixed == 1);
00339 bool value= args[0]->val_bool();
00340 null_value=args[0]->null_value;
00341 return ((!null_value && value == 0) ? 1 : 0);
00342 }
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353 void Item_func_not::print(String *str)
00354 {
00355 str->append('(');
00356 Item_func::print(str);
00357 str->append(')');
00358 }
00359
00365 int64_t Item_func_not_all::val_int()
00366 {
00367 assert(fixed == 1);
00368 bool value= args[0]->val_bool();
00369
00370
00371
00372
00373
00374 if (empty_underlying_subquery())
00375 return 1;
00376
00377 null_value= args[0]->null_value;
00378 return ((!null_value && value == 0) ? 1 : 0);
00379 }
00380
00381
00382 bool Item_func_not_all::empty_underlying_subquery()
00383 {
00384 return ((test_sum_item && !test_sum_item->any_value()) ||
00385 (test_sub_item && !test_sub_item->any_value()));
00386 }
00387
00388 void Item_func_not_all::print(String *str)
00389 {
00390 if (show)
00391 Item_func::print(str);
00392 else
00393 args[0]->print(str);
00394 }
00395
00396
00406 int64_t Item_func_nop_all::val_int()
00407 {
00408 assert(fixed == 1);
00409 int64_t value= args[0]->val_int();
00410
00411
00412
00413
00414
00415 if (empty_underlying_subquery())
00416 return 0;
00417
00418 null_value= args[0]->null_value;
00419 return (null_value || value == 0) ? 0 : 1;
00420 }
00421
00422
00450 static bool convert_constant_item(Session *session, Item_field *field_item,
00451 Item **item)
00452 {
00453 Field *field= field_item->field;
00454 int result= 0;
00455
00456 field->setWriteSet();
00457
00458 if (!(*item)->with_subselect && (*item)->const_item())
00459 {
00460 ulong orig_sql_mode= session->variables.sql_mode;
00461 enum_check_fields orig_count_cuted_fields= session->count_cuted_fields;
00462 uint64_t orig_field_val= 0;
00463
00464
00465 session->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
00466 MODE_INVALID_DATES;
00467 session->count_cuted_fields= CHECK_FIELD_IGNORE;
00468
00469
00470
00471
00472
00473 if (field_item->depended_from)
00474 {
00475 orig_field_val= field->val_int();
00476 }
00477
00478 if (!(*item)->is_null() && !(*item)->save_in_field(field, 1))
00479 {
00480 Item *tmp= new Item_int_with_ref(field->val_int(), *item,
00481 test(field->flags & UNSIGNED_FLAG));
00482 if (tmp)
00483 *item= tmp;
00484 result= 1;
00485 }
00486
00487
00488 if (field_item->depended_from)
00489 {
00490 result= field->store(orig_field_val, field->isUnsigned());
00491
00492 assert(!result);
00493 }
00494 session->variables.sql_mode= orig_sql_mode;
00495 session->count_cuted_fields= orig_count_cuted_fields;
00496 }
00497 return result;
00498 }
00499
00500
00501 void Item_bool_func2::fix_length_and_dec()
00502 {
00503 max_length= 1;
00504
00505
00506
00507
00508
00509 if (!args[0] || !args[1])
00510 return;
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525 DTCollation coll;
00526 if (args[0]->result_type() == STRING_RESULT &&
00527 args[1]->result_type() == STRING_RESULT &&
00528 agg_arg_charsets(coll, args, 2, MY_COLL_CMP_CONV, 1))
00529 return;
00530
00531 args[0]->cmp_context= args[1]->cmp_context=
00532 item_cmp_type(args[0]->result_type(), args[1]->result_type());
00533
00534
00535 if (functype() == LIKE_FUNC)
00536 {
00537 set_cmp_func();
00538 return;
00539 }
00540
00541 Item_field *field_item= NULL;
00542
00543 if (args[0]->real_item()->type() == FIELD_ITEM)
00544 {
00545 field_item= static_cast<Item_field*>(args[0]->real_item());
00546 if (field_item->field->can_be_compared_as_int64_t() &&
00547 !(field_item->is_datetime() && args[1]->result_type() == STRING_RESULT))
00548 {
00549 if (convert_constant_item(&getSession(), field_item, &args[1]))
00550 {
00551 cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
00552 INT_RESULT);
00553 args[0]->cmp_context= args[1]->cmp_context= INT_RESULT;
00554 return;
00555 }
00556 }
00557
00558 if (args[1]->real_item()->type() == FIELD_ITEM)
00559 {
00560 field_item= static_cast<Item_field*>(args[1]->real_item());
00561 if (field_item->field->can_be_compared_as_int64_t() &&
00562 !(field_item->is_datetime() &&
00563 args[0]->result_type() == STRING_RESULT))
00564 {
00565 if (convert_constant_item(&getSession(), field_item, &args[0]))
00566 {
00567 cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
00568 INT_RESULT);
00569 args[0]->cmp_context= args[1]->cmp_context= INT_RESULT;
00570 return;
00571 }
00572 }
00573 }
00574 }
00575 set_cmp_func();
00576 }
00577
00578 Arg_comparator::Arg_comparator():
00579 session(current_session),
00580 a_cache(0),
00581 b_cache(0)
00582 {}
00583
00584 Arg_comparator::Arg_comparator(Item **a1, Item **a2):
00585 a(a1),
00586 b(a2),
00587 session(current_session),
00588 a_cache(0),
00589 b_cache(0)
00590 {}
00591
00592 int Arg_comparator::set_compare_func(Item_bool_func2 *item, Item_result type)
00593 {
00594 owner= item;
00595 func= comparator_matrix[type]
00596 [test(owner->functype() == Item_func::EQUAL_FUNC)];
00597
00598 switch (type) {
00599 case ROW_RESULT:
00600 {
00601 uint32_t n= (*a)->cols();
00602 if (n != (*b)->cols())
00603 {
00604 my_error(ER_OPERAND_COLUMNS, MYF(0), n);
00605 comparators= 0;
00606 return 1;
00607 }
00608 comparators= new Arg_comparator[n];
00609 for (uint32_t i=0; i < n; i++)
00610 {
00611 if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
00612 {
00613 my_error(ER_OPERAND_COLUMNS, MYF(0), (*a)->element_index(i)->cols());
00614 return 1;
00615 }
00616 comparators[i].set_cmp_func(owner, (*a)->addr(i), (*b)->addr(i));
00617 }
00618 break;
00619 }
00620
00621 case STRING_RESULT:
00622 {
00623
00624
00625
00626
00627 if (cmp_collation.set((*a)->collation, (*b)->collation) ||
00628 cmp_collation.derivation == DERIVATION_NONE)
00629 {
00630 my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
00631 return 1;
00632 }
00633 if (cmp_collation.collation == &my_charset_bin)
00634 {
00635
00636
00637
00638
00639 if (func == &Arg_comparator::compare_string)
00640 func= &Arg_comparator::compare_binary_string;
00641 else if (func == &Arg_comparator::compare_e_string)
00642 func= &Arg_comparator::compare_e_binary_string;
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652 (*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
00653 (*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
00654 }
00655 break;
00656 }
00657 case INT_RESULT:
00658 {
00659 if (func == &Arg_comparator::compare_int_signed)
00660 {
00661 if ((*a)->unsigned_flag)
00662 func= (((*b)->unsigned_flag)?
00663 &Arg_comparator::compare_int_unsigned :
00664 &Arg_comparator::compare_int_unsigned_signed);
00665 else if ((*b)->unsigned_flag)
00666 func= &Arg_comparator::compare_int_signed_unsigned;
00667 }
00668 else if (func== &Arg_comparator::compare_e_int)
00669 {
00670 if ((*a)->unsigned_flag ^ (*b)->unsigned_flag)
00671 func= &Arg_comparator::compare_e_int_diff_signedness;
00672 }
00673 break;
00674 }
00675 case DECIMAL_RESULT:
00676 break;
00677 case REAL_RESULT:
00678 {
00679 if ((*a)->decimals < NOT_FIXED_DEC && (*b)->decimals < NOT_FIXED_DEC)
00680 {
00681 precision= 5 / log_10[max((*a)->decimals, (*b)->decimals) + 1];
00682 if (func == &Arg_comparator::compare_real)
00683 func= &Arg_comparator::compare_real_fixed;
00684 else if (func == &Arg_comparator::compare_e_real)
00685 func= &Arg_comparator::compare_e_real_fixed;
00686 }
00687 break;
00688 }
00689 }
00690
00691 return 0;
00692 }
00693
00694
00717 static int64_t
00718 get_date_from_str(Session *session, String *str, type::timestamp_t warn_type, const char *warn_name, bool *error_arg)
00719 {
00720 int64_t value= 0;
00721 type::cut_t error= type::VALID;
00722 type::Time l_time;
00723 type::timestamp_t ret;
00724
00725 ret= l_time.store(str->ptr(), str->length(),
00726 (TIME_FUZZY_DATE | MODE_INVALID_DATES | (session->variables.sql_mode & MODE_NO_ZERO_DATE)),
00727 error);
00728
00729 if (ret == type::DRIZZLE_TIMESTAMP_DATETIME || ret == type::DRIZZLE_TIMESTAMP_DATE)
00730 {
00731
00732
00733
00734
00735 *error_arg= false;
00736 l_time.convert(value);
00737 }
00738 else
00739 {
00740 *error_arg= true;
00741 error= type::CUT;
00742 }
00743
00744 if (error != type::VALID)
00745 {
00746 make_truncated_value_warning(*session, DRIZZLE_ERROR::WARN_LEVEL_WARN, *str, warn_type, warn_name);
00747 }
00748
00749 return value;
00750 }
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785 enum Arg_comparator::enum_date_cmp_type
00786 Arg_comparator::can_compare_as_dates(Item *in_a, Item *in_b,
00787 int64_t *const_value)
00788 {
00789 enum enum_date_cmp_type cmp_type= CMP_DATE_DFLT;
00790 Item *str_arg= 0;
00791
00792 if (in_a->type() == Item::ROW_ITEM || in_b->type() == Item::ROW_ITEM)
00793 return CMP_DATE_DFLT;
00794
00795 if (in_a->is_datetime())
00796 {
00797 if (in_b->is_datetime())
00798 {
00799 cmp_type= CMP_DATE_WITH_DATE;
00800 }
00801 else if (in_b->result_type() == STRING_RESULT)
00802 {
00803 cmp_type= CMP_DATE_WITH_STR;
00804 str_arg= in_b;
00805 }
00806 }
00807 else if (in_b->is_datetime() && in_a->result_type() == STRING_RESULT)
00808 {
00809 cmp_type= CMP_STR_WITH_DATE;
00810 str_arg= in_a;
00811 }
00812
00813 if (cmp_type != CMP_DATE_DFLT)
00814 {
00815
00816
00817
00818
00819 if (cmp_type != CMP_DATE_WITH_DATE && str_arg->const_item() &&
00820 (str_arg->type() != Item::FUNC_ITEM ||
00821 ((Item_func*)str_arg)->functype() != Item_func::GUSERVAR_FUNC))
00822 {
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840 int64_t value;
00841 String *str_val;
00842 String tmp;
00843
00844 DateTime temporal;
00845
00846 str_val= str_arg->val_str(&tmp);
00847 if (! str_val)
00848 {
00849
00850
00851
00852
00853
00854
00855
00856 return CMP_DATE_DFLT;
00857 }
00858 if (temporal.from_string(str_val->c_ptr(), str_val->length()))
00859 {
00860
00861 temporal.to_int64_t(&value);
00862 }
00863 else
00864 {
00865
00866 Time timevalue;
00867 if (timevalue.from_string(str_val->c_ptr(), str_val->length()))
00868 {
00869 uint64_t timeint;
00870 timevalue.to_uint64_t(timeint);
00871 value= static_cast<int64_t>(timeint);
00872 }
00873 else
00874 {
00875
00876 my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), str_val->c_ptr());
00877 return CMP_DATE_DFLT;
00878 }
00879 }
00880
00881 if (const_value)
00882 *const_value= value;
00883 }
00884 }
00885 return cmp_type;
00886 }
00887
00888
00889 int Arg_comparator::set_cmp_func(Item_bool_func2 *owner_arg,
00890 Item **a1, Item **a2,
00891 Item_result type)
00892 {
00893 enum_date_cmp_type cmp_type;
00894 int64_t const_value= -1;
00895 a= a1;
00896 b= a2;
00897
00898 if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
00899 {
00900 owner= owner_arg;
00901 a_type= (*a)->field_type();
00902 b_type= (*b)->field_type();
00903 a_cache= 0;
00904 b_cache= 0;
00905
00906 if (const_value != -1)
00907 {
00908 Item_cache_int *cache= new Item_cache_int();
00909
00910 cache->set_used_tables(1);
00911 if (!(*a)->is_datetime())
00912 {
00913 cache->store((*a), const_value);
00914 a_cache= cache;
00915 a= (Item **)&a_cache;
00916 }
00917 else
00918 {
00919 cache->store((*b), const_value);
00920 b_cache= cache;
00921 b= (Item **)&b_cache;
00922 }
00923 }
00924 is_nulls_eq= test(owner && owner->functype() == Item_func::EQUAL_FUNC);
00925 func= &Arg_comparator::compare_datetime;
00926 get_value_func= &get_datetime_value;
00927
00928 return 0;
00929 }
00930
00931 return set_compare_func(owner_arg, type);
00932 }
00933
00934
00935 void Arg_comparator::set_datetime_cmp_func(Item **a1, Item **b1)
00936 {
00937
00938 owner= NULL;
00939 a= a1;
00940 b= b1;
00941 a_type= (*a)->field_type();
00942 b_type= (*b)->field_type();
00943 a_cache= 0;
00944 b_cache= 0;
00945 is_nulls_eq= false;
00946 func= &Arg_comparator::compare_datetime;
00947 get_value_func= &get_datetime_value;
00948 }
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980 int64_t
00981 get_datetime_value(Session *session, Item ***item_arg, Item **cache_arg,
00982 Item *warn_item, bool *is_null)
00983 {
00984 int64_t value= 0;
00985 String buf, *str= 0;
00986 Item *item= **item_arg;
00987
00988 if (item->result_as_int64_t())
00989 {
00990 value= item->val_int();
00991 *is_null= item->null_value;
00992 enum_field_types f_type= item->field_type();
00993
00994
00995
00996
00997
00998
00999 if (f_type == DRIZZLE_TYPE_DATE ||
01000 (f_type != DRIZZLE_TYPE_DATETIME && value < 100000000L))
01001 value*= 1000000L;
01002 }
01003 else
01004 {
01005 str= item->val_str(&buf);
01006 *is_null= item->null_value;
01007 }
01008
01009 if (*is_null)
01010 return ~(uint64_t) 0;
01011
01012
01013
01014
01015
01016
01017
01018 if (str)
01019 {
01020 bool error;
01021 enum_field_types f_type= warn_item->field_type();
01022 type::timestamp_t t_type= f_type == DRIZZLE_TYPE_DATE ? type::DRIZZLE_TIMESTAMP_DATE : type::DRIZZLE_TIMESTAMP_DATETIME;
01023 value= get_date_from_str(session, str, t_type, warn_item->name, &error);
01024
01025
01026
01027
01028
01029
01030 }
01031
01032
01033
01034
01035 if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
01036 ((Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
01037 {
01038 Item_cache_int *cache= new Item_cache_int(DRIZZLE_TYPE_DATETIME);
01039
01040 cache->set_used_tables(1);
01041 cache->store(item, value);
01042 *cache_arg= cache;
01043 *item_arg= cache_arg;
01044 }
01045
01046 return value;
01047 }
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070 int Arg_comparator::compare_datetime()
01071 {
01072 bool is_null= false;
01073 uint64_t a_value, b_value;
01074
01075
01076 a_value= (*get_value_func)(session, &a, &a_cache, *b, &is_null);
01077 if (!is_nulls_eq && is_null)
01078 {
01079 if (owner)
01080 owner->null_value= 1;
01081 return -1;
01082 }
01083
01084
01085 b_value= (*get_value_func)(session, &b, &b_cache, *a, &is_null);
01086 if (is_null)
01087 {
01088 if (owner)
01089 owner->null_value= is_nulls_eq ? 0 : 1;
01090 return is_nulls_eq ? 1 : -1;
01091 }
01092
01093 if (owner)
01094 owner->null_value= 0;
01095
01096
01097 if (is_nulls_eq)
01098 return (a_value == b_value);
01099 return (a_value < b_value) ? -1 : ((a_value > b_value) ? 1 : 0);
01100 }
01101
01102
01103 int Arg_comparator::compare_string()
01104 {
01105 String *res1,*res2;
01106 if ((res1= (*a)->val_str(&owner->tmp_value1)))
01107 {
01108 if ((res2= (*b)->val_str(&owner->tmp_value2)))
01109 {
01110 owner->null_value= 0;
01111 return sortcmp(res1,res2,cmp_collation.collation);
01112 }
01113 }
01114 owner->null_value= 1;
01115 return -1;
01116 }
01117
01118
01130 int Arg_comparator::compare_binary_string()
01131 {
01132 String *res1,*res2;
01133 if ((res1= (*a)->val_str(&owner->tmp_value1)))
01134 {
01135 if ((res2= (*b)->val_str(&owner->tmp_value2)))
01136 {
01137 owner->null_value= 0;
01138 uint32_t res1_length= res1->length();
01139 uint32_t res2_length= res2->length();
01140 int cmp= memcmp(res1->ptr(), res2->ptr(), min(res1_length,res2_length));
01141 return cmp ? cmp : (int) (res1_length - res2_length);
01142 }
01143 }
01144 owner->null_value= 1;
01145 return -1;
01146 }
01147
01148
01154 int Arg_comparator::compare_e_string()
01155 {
01156 String *res1,*res2;
01157 res1= (*a)->val_str(&owner->tmp_value1);
01158 res2= (*b)->val_str(&owner->tmp_value2);
01159 if (!res1 || !res2)
01160 return test(res1 == res2);
01161 return test(sortcmp(res1, res2, cmp_collation.collation) == 0);
01162 }
01163
01164
01165 int Arg_comparator::compare_e_binary_string()
01166 {
01167 String *res1,*res2;
01168 res1= (*a)->val_str(&owner->tmp_value1);
01169 res2= (*b)->val_str(&owner->tmp_value2);
01170 if (!res1 || !res2)
01171 return test(res1 == res2);
01172 return test(stringcmp(res1, res2) == 0);
01173 }
01174
01175
01176 int Arg_comparator::compare_real()
01177 {
01178
01179
01180
01181
01182
01183 volatile double val1, val2;
01184 val1= (*a)->val_real();
01185 if (!(*a)->null_value)
01186 {
01187 val2= (*b)->val_real();
01188 if (!(*b)->null_value)
01189 {
01190 owner->null_value= 0;
01191 if (val1 < val2) return -1;
01192 if (val1 == val2) return 0;
01193 return 1;
01194 }
01195 }
01196 owner->null_value= 1;
01197 return -1;
01198 }
01199
01200 int Arg_comparator::compare_decimal()
01201 {
01202 type::Decimal value1;
01203 type::Decimal *val1= (*a)->val_decimal(&value1);
01204 if (!(*a)->null_value)
01205 {
01206 type::Decimal value2;
01207 type::Decimal *val2= (*b)->val_decimal(&value2);
01208 if (!(*b)->null_value)
01209 {
01210 owner->null_value= 0;
01211 return class_decimal_cmp(val1, val2);
01212 }
01213 }
01214 owner->null_value= 1;
01215 return -1;
01216 }
01217
01218 int Arg_comparator::compare_e_real()
01219 {
01220 double val1= (*a)->val_real();
01221 double val2= (*b)->val_real();
01222 if ((*a)->null_value || (*b)->null_value)
01223 return test((*a)->null_value && (*b)->null_value);
01224 return test(val1 == val2);
01225 }
01226
01227 int Arg_comparator::compare_e_decimal()
01228 {
01229 type::Decimal value1, value2;
01230 type::Decimal *val1= (*a)->val_decimal(&value1);
01231 type::Decimal *val2= (*b)->val_decimal(&value2);
01232 if ((*a)->null_value || (*b)->null_value)
01233 return test((*a)->null_value && (*b)->null_value);
01234 return test(class_decimal_cmp(val1, val2) == 0);
01235 }
01236
01237
01238 int Arg_comparator::compare_real_fixed()
01239 {
01240
01241
01242
01243
01244
01245 volatile double val1, val2;
01246 val1= (*a)->val_real();
01247 if (!(*a)->null_value)
01248 {
01249 val2= (*b)->val_real();
01250 if (!(*b)->null_value)
01251 {
01252 owner->null_value= 0;
01253 if (val1 == val2 || fabs(val1 - val2) < precision)
01254 return 0;
01255 if (val1 < val2)
01256 return -1;
01257 return 1;
01258 }
01259 }
01260 owner->null_value= 1;
01261 return -1;
01262 }
01263
01264
01265 int Arg_comparator::compare_e_real_fixed()
01266 {
01267 double val1= (*a)->val_real();
01268 double val2= (*b)->val_real();
01269 if ((*a)->null_value || (*b)->null_value)
01270 return test((*a)->null_value && (*b)->null_value);
01271 return test(val1 == val2 || fabs(val1 - val2) < precision);
01272 }
01273
01274
01275 int Arg_comparator::compare_int_signed()
01276 {
01277 int64_t val1= (*a)->val_int();
01278 if (!(*a)->null_value)
01279 {
01280 int64_t val2= (*b)->val_int();
01281 if (!(*b)->null_value)
01282 {
01283 owner->null_value= 0;
01284 if (val1 < val2) return -1;
01285 if (val1 == val2) return 0;
01286 return 1;
01287 }
01288 }
01289 owner->null_value= 1;
01290 return -1;
01291 }
01292
01293
01298 int Arg_comparator::compare_int_unsigned()
01299 {
01300 uint64_t val1= (*a)->val_int();
01301 if (!(*a)->null_value)
01302 {
01303 uint64_t val2= (*b)->val_int();
01304 if (!(*b)->null_value)
01305 {
01306 owner->null_value= 0;
01307 if (val1 < val2) return -1;
01308 if (val1 == val2) return 0;
01309 return 1;
01310 }
01311 }
01312 owner->null_value= 1;
01313 return -1;
01314 }
01315
01316
01321 int Arg_comparator::compare_int_signed_unsigned()
01322 {
01323 int64_t sval1= (*a)->val_int();
01324 if (!(*a)->null_value)
01325 {
01326 uint64_t uval2= (uint64_t)(*b)->val_int();
01327 if (!(*b)->null_value)
01328 {
01329 owner->null_value= 0;
01330 if (sval1 < 0 || (uint64_t)sval1 < uval2)
01331 return -1;
01332 if ((uint64_t)sval1 == uval2)
01333 return 0;
01334 return 1;
01335 }
01336 }
01337 owner->null_value= 1;
01338 return -1;
01339 }
01340
01341
01346 int Arg_comparator::compare_int_unsigned_signed()
01347 {
01348 uint64_t uval1= (uint64_t)(*a)->val_int();
01349 if (!(*a)->null_value)
01350 {
01351 int64_t sval2= (*b)->val_int();
01352 if (!(*b)->null_value)
01353 {
01354 owner->null_value= 0;
01355 if (sval2 < 0)
01356 return 1;
01357 if (uval1 < (uint64_t)sval2)
01358 return -1;
01359 if (uval1 == (uint64_t)sval2)
01360 return 0;
01361 return 1;
01362 }
01363 }
01364 owner->null_value= 1;
01365 return -1;
01366 }
01367
01368
01369 int Arg_comparator::compare_e_int()
01370 {
01371 int64_t val1= (*a)->val_int();
01372 int64_t val2= (*b)->val_int();
01373 if ((*a)->null_value || (*b)->null_value)
01374 return test((*a)->null_value && (*b)->null_value);
01375 return test(val1 == val2);
01376 }
01377
01381 int Arg_comparator::compare_e_int_diff_signedness()
01382 {
01383 int64_t val1= (*a)->val_int();
01384 int64_t val2= (*b)->val_int();
01385 if ((*a)->null_value || (*b)->null_value)
01386 return test((*a)->null_value && (*b)->null_value);
01387 return (val1 >= 0) && test(val1 == val2);
01388 }
01389
01390 int Arg_comparator::compare_row()
01391 {
01392 int res= 0;
01393 bool was_null= 0;
01394 (*a)->bring_value();
01395 (*b)->bring_value();
01396 uint32_t n= (*a)->cols();
01397 for (uint32_t i= 0; i<n; i++)
01398 {
01399 res= comparators[i].compare();
01400 if (owner->null_value)
01401 {
01402
01403 switch (owner->functype()) {
01404 case Item_func::NE_FUNC:
01405 break;
01406 case Item_func::LT_FUNC:
01407 case Item_func::LE_FUNC:
01408 case Item_func::GT_FUNC:
01409 case Item_func::GE_FUNC:
01410 return -1;
01411 default:
01412 if (owner->abort_on_null)
01413 return -1;
01414 }
01415 was_null= 1;
01416 owner->null_value= 0;
01417 res= 0;
01418 }
01419 else if (res)
01420 return res;
01421 }
01422 if (was_null)
01423 {
01424
01425
01426
01427
01428 owner->null_value= 1;
01429 return -1;
01430 }
01431 return 0;
01432 }
01433
01434
01435 int Arg_comparator::compare_e_row()
01436 {
01437 (*a)->bring_value();
01438 (*b)->bring_value();
01439 uint32_t n= (*a)->cols();
01440 for (uint32_t i= 0; i<n; i++)
01441 {
01442 if (!comparators[i].compare())
01443 return 0;
01444 }
01445 return 1;
01446 }
01447
01448
01449 void Item_func_truth::fix_length_and_dec()
01450 {
01451 maybe_null= 0;
01452 null_value= 0;
01453 decimals= 0;
01454 max_length= 1;
01455 }
01456
01457
01458 void Item_func_truth::print(String *str)
01459 {
01460 str->append('(');
01461 args[0]->print(str);
01462 str->append(STRING_WITH_LEN(" is "));
01463 if (! affirmative)
01464 str->append(STRING_WITH_LEN("not "));
01465 if (value)
01466 str->append(STRING_WITH_LEN("true"));
01467 else
01468 str->append(STRING_WITH_LEN("false"));
01469 str->append(')');
01470 }
01471
01472
01473 bool Item_func_truth::val_bool()
01474 {
01475 bool val= args[0]->val_bool();
01476 if (args[0]->null_value)
01477 {
01478
01479
01480
01481
01482 return (! affirmative);
01483 }
01484
01485 if (affirmative)
01486 {
01487
01488 return (val == value);
01489 }
01490
01491
01492 return (val != value);
01493 }
01494
01495
01496 int64_t Item_func_truth::val_int()
01497 {
01498 return (val_bool() ? 1 : 0);
01499 }
01500
01501
01502 bool Item_in_optimizer::fix_left(Session *session, Item **)
01503 {
01504 if ((!args[0]->fixed && args[0]->fix_fields(session, args)) ||
01505 (!cache && !(cache= Item_cache::get_cache(args[0]))))
01506 return 1;
01507
01508 cache->setup(args[0]);
01509 if (cache->cols() == 1)
01510 {
01511 if ((used_tables_cache= args[0]->used_tables()))
01512 cache->set_used_tables(OUTER_REF_TABLE_BIT);
01513 else
01514 cache->set_used_tables(0);
01515 }
01516 else
01517 {
01518 uint32_t n= cache->cols();
01519 for (uint32_t i= 0; i < n; i++)
01520 {
01521 if (args[0]->element_index(i)->used_tables())
01522 ((Item_cache *)cache->element_index(i))->set_used_tables(OUTER_REF_TABLE_BIT);
01523 else
01524 ((Item_cache *)cache->element_index(i))->set_used_tables(0);
01525 }
01526 used_tables_cache= args[0]->used_tables();
01527 }
01528 not_null_tables_cache= args[0]->not_null_tables();
01529 with_sum_func= args[0]->with_sum_func;
01530 if ((const_item_cache= args[0]->const_item()))
01531 cache->store(args[0]);
01532 return 0;
01533 }
01534
01535
01536 bool Item_in_optimizer::fix_fields(Session *session, Item **ref)
01537 {
01538 assert(fixed == 0);
01539 if (fix_left(session, ref))
01540 return true;
01541 if (args[0]->maybe_null)
01542 maybe_null=1;
01543
01544 if (!args[1]->fixed && args[1]->fix_fields(session, args+1))
01545 return true;
01546 Item_in_subselect * sub= (Item_in_subselect *)args[1];
01547 if (args[0]->cols() != sub->engine->cols())
01548 {
01549 my_error(ER_OPERAND_COLUMNS, MYF(0), args[0]->cols());
01550 return true;
01551 }
01552 if (args[1]->maybe_null)
01553 maybe_null=1;
01554 with_sum_func= with_sum_func || args[1]->with_sum_func;
01555 used_tables_cache|= args[1]->used_tables();
01556 not_null_tables_cache|= args[1]->not_null_tables();
01557 const_item_cache&= args[1]->const_item();
01558 fixed= 1;
01559 return false;
01560 }
01561
01562
01563 int64_t Item_in_optimizer::val_int()
01564 {
01565 bool tmp;
01566 assert(fixed == 1);
01567 cache->store(args[0]);
01568
01569 if (cache->null_value)
01570 {
01571 if (((Item_in_subselect*)args[1])->is_top_level_item())
01572 {
01573
01574
01575
01576
01577 null_value= 1;
01578 }
01579 else
01580 {
01581 if (!((Item_in_subselect*)args[1])->is_correlated &&
01582 result_for_null_param != UNKNOWN)
01583 {
01584
01585 null_value= result_for_null_param;
01586 }
01587 else
01588 {
01589
01590
01591
01592
01593
01594
01595
01596 Item_in_subselect *item_subs=(Item_in_subselect*)args[1];
01597 if (cache->cols() == 1)
01598 {
01599 item_subs->set_cond_guard_var(0, false);
01600 (void) args[1]->val_bool_result();
01601 result_for_null_param= null_value= !item_subs->engine->no_rows();
01602 item_subs->set_cond_guard_var(0, true);
01603 }
01604 else
01605 {
01606 uint32_t i;
01607 uint32_t ncols= cache->cols();
01608
01609
01610
01611
01612 for (i= 0; i < ncols; i++)
01613 {
01614 if (cache->element_index(i)->null_value)
01615 item_subs->set_cond_guard_var(i, false);
01616 }
01617
01618 (void) args[1]->val_bool_result();
01619 result_for_null_param= null_value= !item_subs->engine->no_rows();
01620
01621
01622 for (i= 0; i < ncols; i++)
01623 item_subs->set_cond_guard_var(i, true);
01624 }
01625 }
01626 }
01627 return 0;
01628 }
01629 tmp= args[1]->val_bool_result();
01630 null_value= args[1]->null_value;
01631 return tmp;
01632 }
01633
01634
01635 void Item_in_optimizer::keep_top_level_cache()
01636 {
01637 cache->keep_array();
01638 save_cache= 1;
01639 }
01640
01641
01642 void Item_in_optimizer::cleanup()
01643 {
01644 item::function::Boolean::cleanup();
01645 if (!save_cache)
01646 cache= 0;
01647 return;
01648 }
01649
01650
01651 bool Item_in_optimizer::is_null()
01652 {
01653 cache->store(args[0]);
01654 return (null_value= (cache->null_value || args[1]->is_null()));
01655 }
01656
01657
01680 Item *Item_in_optimizer::transform(Item_transformer transformer, unsigned char *argument)
01681 {
01682 Item *new_item;
01683
01684 assert(arg_count == 2);
01685
01686
01687 new_item= (*args)->transform(transformer, argument);
01688 if (!new_item)
01689 return 0;
01690 *args= new_item;
01691
01692
01693
01694
01695
01696
01697
01698
01699 assert((args[1])->type() == Item::SUBSELECT_ITEM &&
01700 (((Item_subselect*)(args[1]))->substype() ==
01701 Item_subselect::IN_SUBS ||
01702 ((Item_subselect*)(args[1]))->substype() ==
01703 Item_subselect::ALL_SUBS ||
01704 ((Item_subselect*)(args[1]))->substype() ==
01705 Item_subselect::ANY_SUBS));
01706
01707 Item_in_subselect *in_arg= (Item_in_subselect*)args[1];
01708 in_arg->left_expr= args[0];
01709
01710 return (this->*transformer)(argument);
01711 }
01712
01713
01714
01715 int64_t Item_func_eq::val_int()
01716 {
01717 assert(fixed == 1);
01718 int value= cmp.compare();
01719 return value == 0 ? 1 : 0;
01720 }
01721
01722
01725 void Item_func_equal::fix_length_and_dec()
01726 {
01727 Item_bool_func2::fix_length_and_dec();
01728 maybe_null=null_value=0;
01729 }
01730
01731 int64_t Item_func_equal::val_int()
01732 {
01733 assert(fixed == 1);
01734 return cmp.compare();
01735 }
01736
01737 int64_t Item_func_ne::val_int()
01738 {
01739 assert(fixed == 1);
01740 int value= cmp.compare();
01741 return value != 0 && !null_value ? 1 : 0;
01742 }
01743
01744
01745 int64_t Item_func_ge::val_int()
01746 {
01747 assert(fixed == 1);
01748 int value= cmp.compare();
01749 return value >= 0 ? 1 : 0;
01750 }
01751
01752
01753 int64_t Item_func_gt::val_int()
01754 {
01755 assert(fixed == 1);
01756 int value= cmp.compare();
01757 return value > 0 ? 1 : 0;
01758 }
01759
01760 int64_t Item_func_le::val_int()
01761 {
01762 assert(fixed == 1);
01763 int value= cmp.compare();
01764 return value <= 0 && !null_value ? 1 : 0;
01765 }
01766
01767
01768 int64_t Item_func_lt::val_int()
01769 {
01770 assert(fixed == 1);
01771 int value= cmp.compare();
01772 return value < 0 && !null_value ? 1 : 0;
01773 }
01774
01775
01776 int64_t Item_func_strcmp::val_int()
01777 {
01778 assert(fixed == 1);
01779 String *a=args[0]->val_str(&tmp_value1);
01780 String *b=args[1]->val_str(&tmp_value2);
01781 if (!a || !b)
01782 {
01783 null_value=1;
01784 return 0;
01785 }
01786 int value= sortcmp(a,b,cmp.cmp_collation.collation);
01787 null_value=0;
01788 return !value ? 0 : (value < 0 ? (int64_t) -1 : (int64_t) 1);
01789 }
01790
01791
01792 bool Item_func_opt_neg::eq(const Item *item, bool binary_cmp) const
01793 {
01794
01795 if (this == item)
01796 return 1;
01797 if (item->type() != FUNC_ITEM)
01798 return 0;
01799 Item_func *item_func=(Item_func*) item;
01800 if (arg_count != item_func->arg_count ||
01801 functype() != item_func->functype())
01802 return 0;
01803 if (negated != ((Item_func_opt_neg *) item_func)->negated)
01804 return 0;
01805 for (uint32_t i=0; i < arg_count ; i++)
01806 if (!args[i]->eq(item_func->arguments()[i], binary_cmp))
01807 return 0;
01808 return 1;
01809 }
01810
01811
01812 void Item_func_interval::fix_length_and_dec()
01813 {
01814 uint32_t rows= row->cols();
01815
01816 use_decimal_comparison= ((row->element_index(0)->result_type() ==
01817 DECIMAL_RESULT) ||
01818 (row->element_index(0)->result_type() ==
01819 INT_RESULT));
01820 if (rows > 8)
01821 {
01822 bool not_null_consts= true;
01823
01824 for (uint32_t i= 1; not_null_consts && i < rows; i++)
01825 {
01826 Item *el= row->element_index(i);
01827 not_null_consts&= el->const_item() & !el->is_null();
01828 }
01829
01830 if (not_null_consts &&
01831 (intervals=
01832 (interval_range*) memory::sql_alloc(sizeof(interval_range) * (rows - 1))))
01833 {
01834 if (use_decimal_comparison)
01835 {
01836 for (uint32_t i= 1; i < rows; i++)
01837 {
01838 Item *el= row->element_index(i);
01839 interval_range *range= intervals + (i-1);
01840 if ((el->result_type() == DECIMAL_RESULT) ||
01841 (el->result_type() == INT_RESULT))
01842 {
01843 range->type= DECIMAL_RESULT;
01844 range->dec.init();
01845 type::Decimal *dec= el->val_decimal(&range->dec);
01846 if (dec != &range->dec)
01847 {
01848 range->dec= *dec;
01849 range->dec.fix_buffer_pointer();
01850 }
01851 }
01852 else
01853 {
01854 range->type= REAL_RESULT;
01855 range->dbl= el->val_real();
01856 }
01857 }
01858 }
01859 else
01860 {
01861 for (uint32_t i= 1; i < rows; i++)
01862 {
01863 intervals[i-1].dbl= row->element_index(i)->val_real();
01864 }
01865 }
01866 }
01867 }
01868 maybe_null= 0;
01869 max_length= 2;
01870 used_tables_cache|= row->used_tables();
01871 not_null_tables_cache= row->not_null_tables();
01872 with_sum_func= with_sum_func || row->with_sum_func;
01873 const_item_cache&= row->const_item();
01874 }
01875
01876
01891 int64_t Item_func_interval::val_int()
01892 {
01893 assert(fixed == 1);
01894 double value;
01895 type::Decimal dec_buf, *dec= NULL;
01896 uint32_t i;
01897
01898 if (use_decimal_comparison)
01899 {
01900 dec= row->element_index(0)->val_decimal(&dec_buf);
01901 if (row->element_index(0)->null_value)
01902 return -1;
01903 class_decimal2double(E_DEC_FATAL_ERROR, dec, &value);
01904 }
01905 else
01906 {
01907 value= row->element_index(0)->val_real();
01908 if (row->element_index(0)->null_value)
01909 return -1;
01910 }
01911
01912 if (intervals)
01913 {
01914 uint32_t start,end;
01915 start= 0;
01916 end= row->cols()-2;
01917 while (start != end)
01918 {
01919 uint32_t mid= (start + end + 1) / 2;
01920 interval_range *range= intervals + mid;
01921 bool cmp_result;
01922
01923
01924
01925
01926
01927 if (dec && range->type == DECIMAL_RESULT)
01928 cmp_result= class_decimal_cmp(&range->dec, dec) <= 0;
01929 else
01930 cmp_result= (range->dbl <= value);
01931 if (cmp_result)
01932 start= mid;
01933 else
01934 end= mid - 1;
01935 }
01936 interval_range *range= intervals+start;
01937 return ((dec && range->type == DECIMAL_RESULT) ?
01938 class_decimal_cmp(dec, &range->dec) < 0 :
01939 value < range->dbl) ? 0 : start + 1;
01940 }
01941
01942 for (i=1 ; i < row->cols() ; i++)
01943 {
01944 Item *el= row->element_index(i);
01945 if (use_decimal_comparison &&
01946 ((el->result_type() == DECIMAL_RESULT) ||
01947 (el->result_type() == INT_RESULT)))
01948 {
01949 type::Decimal e_dec_buf, *e_dec= el->val_decimal(&e_dec_buf);
01950
01951 if (el->null_value)
01952 continue;
01953 if (class_decimal_cmp(e_dec, dec) > 0)
01954 return i - 1;
01955 }
01956 else
01957 {
01958 double val= el->val_real();
01959
01960 if (el->null_value)
01961 continue;
01962 if (val > value)
01963 return i - 1;
01964 }
01965 }
01966 return i-1;
01967 }
01968
01969
01998 bool Item_func_between::fix_fields(Session *session, Item **ref)
01999 {
02000 if (Item_func_opt_neg::fix_fields(session, ref))
02001 return 1;
02002
02003 session->lex().current_select->between_count++;
02004
02005
02006 if (pred_level && !negated)
02007 return 0;
02008
02009
02010 not_null_tables_cache= (args[0]->not_null_tables() |
02011 (args[1]->not_null_tables() &
02012 args[2]->not_null_tables()));
02013
02014 return 0;
02015 }
02016
02017
02018 void Item_func_between::fix_length_and_dec()
02019 {
02020 max_length= 1;
02021 int i;
02022 bool datetime_found= false;
02023 compare_as_dates= true;
02024
02025
02026
02027
02028
02029 if (!args[0] || !args[1] || !args[2])
02030 return;
02031 if ( agg_cmp_type(&cmp_type, args, 3))
02032 return;
02033 if (cmp_type == STRING_RESULT &&
02034 agg_arg_charsets(cmp_collation, args, 3, MY_COLL_CMP_CONV, 1))
02035 return;
02036
02037
02038
02039
02040
02041
02042 if (cmp_type == STRING_RESULT)
02043 {
02044 for (i= 0; i < 3; i++)
02045 {
02046 if (args[i]->is_datetime())
02047 {
02048 datetime_found= true;
02049 continue;
02050 }
02051 }
02052 }
02053 if (!datetime_found)
02054 compare_as_dates= false;
02055
02056 if (compare_as_dates)
02057 {
02058 ge_cmp.set_datetime_cmp_func(args, args + 1);
02059 le_cmp.set_datetime_cmp_func(args, args + 2);
02060 }
02061 else if (args[0]->real_item()->type() == FIELD_ITEM)
02062 {
02063 Item_field *field_item= (Item_field*) (args[0]->real_item());
02064 if (field_item->field->can_be_compared_as_int64_t())
02065 {
02066
02067
02068
02069
02070 if (convert_constant_item(&getSession(), field_item, &args[1]))
02071 cmp_type=INT_RESULT;
02072 if (convert_constant_item(&getSession(), field_item, &args[2]))
02073 cmp_type=INT_RESULT;
02074 }
02075 }
02076 }
02077
02078
02079 int64_t Item_func_between::val_int()
02080 {
02081 assert(fixed == 1);
02082 if (compare_as_dates)
02083 {
02084 int ge_res, le_res;
02085
02086 ge_res= ge_cmp.compare();
02087 if ((null_value= args[0]->null_value))
02088 return 0;
02089 le_res= le_cmp.compare();
02090
02091 if (!args[1]->null_value && !args[2]->null_value)
02092 return (int64_t) ((ge_res >= 0 && le_res <=0) != negated);
02093 else if (args[1]->null_value)
02094 {
02095 null_value= le_res > 0;
02096 }
02097 else
02098 {
02099 null_value= ge_res < 0;
02100 }
02101 }
02102 else if (cmp_type == STRING_RESULT)
02103 {
02104 String *value,*a,*b;
02105 value=args[0]->val_str(&value0);
02106 if ((null_value=args[0]->null_value))
02107 return 0;
02108 a=args[1]->val_str(&value1);
02109 b=args[2]->val_str(&value2);
02110 if (!args[1]->null_value && !args[2]->null_value)
02111 return (int64_t) ((sortcmp(value,a,cmp_collation.collation) >= 0 &&
02112 sortcmp(value,b,cmp_collation.collation) <= 0) !=
02113 negated);
02114 if (args[1]->null_value && args[2]->null_value)
02115 null_value=1;
02116 else if (args[1]->null_value)
02117 {
02118
02119 null_value= sortcmp(value,b,cmp_collation.collation) <= 0;
02120 }
02121 else
02122 {
02123
02124 null_value= sortcmp(value,a,cmp_collation.collation) >= 0;
02125 }
02126 }
02127 else if (cmp_type == INT_RESULT)
02128 {
02129 int64_t value=args[0]->val_int(), a, b;
02130 if ((null_value=args[0]->null_value))
02131 return 0;
02132 a=args[1]->val_int();
02133 b=args[2]->val_int();
02134 if (!args[1]->null_value && !args[2]->null_value)
02135 return (int64_t) ((value >= a && value <= b) != negated);
02136 if (args[1]->null_value && args[2]->null_value)
02137 null_value=1;
02138 else if (args[1]->null_value)
02139 {
02140 null_value= value <= b;
02141 }
02142 else
02143 {
02144 null_value= value >= a;
02145 }
02146 }
02147 else if (cmp_type == DECIMAL_RESULT)
02148 {
02149 type::Decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
02150 a_buf, *a_dec, b_buf, *b_dec;
02151 if ((null_value=args[0]->null_value))
02152 return 0;
02153 a_dec= args[1]->val_decimal(&a_buf);
02154 b_dec= args[2]->val_decimal(&b_buf);
02155 if (!args[1]->null_value && !args[2]->null_value)
02156 return (int64_t) ((class_decimal_cmp(dec, a_dec) >= 0 &&
02157 class_decimal_cmp(dec, b_dec) <= 0) != negated);
02158 if (args[1]->null_value && args[2]->null_value)
02159 null_value=1;
02160 else if (args[1]->null_value)
02161 null_value= (class_decimal_cmp(dec, b_dec) <= 0);
02162 else
02163 null_value= (class_decimal_cmp(dec, a_dec) >= 0);
02164 }
02165 else
02166 {
02167 double value= args[0]->val_real(),a,b;
02168 if ((null_value=args[0]->null_value))
02169 return 0;
02170 a= args[1]->val_real();
02171 b= args[2]->val_real();
02172 if (!args[1]->null_value && !args[2]->null_value)
02173 return (int64_t) ((value >= a && value <= b) != negated);
02174 if (args[1]->null_value && args[2]->null_value)
02175 null_value=1;
02176 else if (args[1]->null_value)
02177 {
02178 null_value= value <= b;
02179 }
02180 else
02181 {
02182 null_value= value >= a;
02183 }
02184 }
02185 return (int64_t) (!null_value && negated);
02186 }
02187
02188
02189 void Item_func_between::print(String *str)
02190 {
02191 str->append('(');
02192 args[0]->print(str);
02193 if (negated)
02194 str->append(STRING_WITH_LEN(" not"));
02195 str->append(STRING_WITH_LEN(" between "));
02196 args[1]->print(str);
02197 str->append(STRING_WITH_LEN(" and "));
02198 args[2]->print(str);
02199 str->append(')');
02200 }
02201
02202 void
02203 Item_func_ifnull::fix_length_and_dec()
02204 {
02205 agg_result_type(&hybrid_type, args, 2);
02206 maybe_null= args[1]->maybe_null;
02207 decimals= max(args[0]->decimals, args[1]->decimals);
02208 unsigned_flag= args[0]->unsigned_flag && args[1]->unsigned_flag;
02209
02210 if (hybrid_type == DECIMAL_RESULT || hybrid_type == INT_RESULT)
02211 {
02212 int len0= args[0]->max_length - args[0]->decimals
02213 - (args[0]->unsigned_flag ? 0 : 1);
02214
02215 int len1= args[1]->max_length - args[1]->decimals
02216 - (args[1]->unsigned_flag ? 0 : 1);
02217
02218 max_length= max(len0, len1) + decimals + (unsigned_flag ? 0 : 1);
02219 }
02220 else
02221 {
02222 max_length= max(args[0]->max_length, args[1]->max_length);
02223 }
02224
02225 switch (hybrid_type)
02226 {
02227 case STRING_RESULT:
02228 agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
02229 break;
02230
02231 case DECIMAL_RESULT:
02232 case REAL_RESULT:
02233 break;
02234
02235 case INT_RESULT:
02236 decimals= 0;
02237 break;
02238
02239 case ROW_RESULT:
02240 assert(0);
02241 }
02242
02243 cached_field_type= agg_field_type(args, 2);
02244 }
02245
02246
02247 uint32_t Item_func_ifnull::decimal_precision() const
02248 {
02249 int max_int_part= max(args[0]->decimal_int_part(),args[1]->decimal_int_part());
02250 return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
02251 }
02252
02253
02254 enum_field_types Item_func_ifnull::field_type() const
02255 {
02256 return cached_field_type;
02257 }
02258
02259 Field *Item_func_ifnull::tmp_table_field(Table *table)
02260 {
02261 return tmp_table_field_from_field_type(table, 0);
02262 }
02263
02264 double
02265 Item_func_ifnull::real_op()
02266 {
02267 assert(fixed == 1);
02268 double value= args[0]->val_real();
02269 if (!args[0]->null_value)
02270 {
02271 null_value=0;
02272 return value;
02273 }
02274 value= args[1]->val_real();
02275 if ((null_value=args[1]->null_value))
02276 return 0.0;
02277 return value;
02278 }
02279
02280 int64_t
02281 Item_func_ifnull::int_op()
02282 {
02283 assert(fixed == 1);
02284 int64_t value=args[0]->val_int();
02285 if (!args[0]->null_value)
02286 {
02287 null_value=0;
02288 return value;
02289 }
02290 value=args[1]->val_int();
02291 if ((null_value=args[1]->null_value))
02292 return 0;
02293 return value;
02294 }
02295
02296
02297 type::Decimal *Item_func_ifnull::decimal_op(type::Decimal *decimal_value)
02298 {
02299 assert(fixed == 1);
02300 type::Decimal *value= args[0]->val_decimal(decimal_value);
02301 if (!args[0]->null_value)
02302 {
02303 null_value= 0;
02304 return value;
02305 }
02306 value= args[1]->val_decimal(decimal_value);
02307 if ((null_value= args[1]->null_value))
02308 return 0;
02309 return value;
02310 }
02311
02312
02313 String *
02314 Item_func_ifnull::str_op(String *str)
02315 {
02316 assert(fixed == 1);
02317 String *res =args[0]->val_str(str);
02318 if (!args[0]->null_value)
02319 {
02320 null_value=0;
02321 res->set_charset(collation.collation);
02322 return res;
02323 }
02324 res=args[1]->val_str(str);
02325 if ((null_value=args[1]->null_value))
02326 return 0;
02327 res->set_charset(collation.collation);
02328 return res;
02329 }
02330
02331
02358 bool
02359 Item_func_if::fix_fields(Session *session, Item **ref)
02360 {
02361 assert(fixed == 0);
02362 args[0]->top_level_item();
02363
02364 if (Item_func::fix_fields(session, ref))
02365 return 1;
02366
02367 not_null_tables_cache= (args[1]->not_null_tables() &
02368 args[2]->not_null_tables());
02369
02370 return 0;
02371 }
02372
02373
02374 void Item_func_if::fix_length_and_dec()
02375 {
02376 maybe_null= args[1]->maybe_null || args[2]->maybe_null;
02377 decimals= max(args[1]->decimals, args[2]->decimals);
02378 unsigned_flag= args[1]->unsigned_flag && args[2]->unsigned_flag;
02379
02380 enum Item_result arg1_type= args[1]->result_type();
02381 enum Item_result arg2_type= args[2]->result_type();
02382 bool null1= args[1]->const_item() && args[1]->null_value;
02383 bool null2= args[2]->const_item() && args[2]->null_value;
02384
02385 if (null1)
02386 {
02387 cached_result_type= arg2_type;
02388 collation.set(args[2]->collation.collation);
02389 cached_field_type= args[2]->field_type();
02390 }
02391 else if (null2)
02392 {
02393 cached_result_type= arg1_type;
02394 collation.set(args[1]->collation.collation);
02395 cached_field_type= args[1]->field_type();
02396 }
02397 else
02398 {
02399 agg_result_type(&cached_result_type, args +1, 2);
02400 if (cached_result_type == STRING_RESULT)
02401 {
02402 if (agg_arg_charsets(collation, args +1, 2, MY_COLL_ALLOW_CONV, 1))
02403 return;
02404 }
02405 else
02406 {
02407 collation.set(&my_charset_bin);
02408 }
02409 cached_field_type= agg_field_type(args +1, 2);
02410 }
02411
02412 switch (cached_result_type)
02413 {
02414 case DECIMAL_RESULT:
02415 case INT_RESULT:
02416 {
02417 int len1= args[1]->max_length -args[1]->decimals -(args[1]->unsigned_flag ? 0 : 1);
02418 int len2= args[2]->max_length -args[2]->decimals -(args[2]->unsigned_flag ? 0 : 1);
02419 max_length= max(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
02420 }
02421 break;
02422 case REAL_RESULT:
02423 case STRING_RESULT:
02424 max_length= max(args[1]->max_length, args[2]->max_length);
02425 break;
02426
02427 case ROW_RESULT:
02428 assert(0);
02429 break;
02430 }
02431 }
02432
02433
02434 uint32_t Item_func_if::decimal_precision() const
02435 {
02436 int precision= (max(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
02437 decimals);
02438 return min(precision, DECIMAL_MAX_PRECISION);
02439 }
02440
02441
02442 double
02443 Item_func_if::val_real()
02444 {
02445 assert(fixed == 1);
02446 Item *arg= args[0]->val_bool() ? args[1] : args[2];
02447 double value= arg->val_real();
02448 null_value=arg->null_value;
02449 return value;
02450 }
02451
02452 int64_t
02453 Item_func_if::val_int()
02454 {
02455 assert(fixed == 1);
02456 Item *arg= args[0]->val_bool() ? args[1] : args[2];
02457 int64_t value= arg->val_int();
02458 null_value= arg->null_value;
02459 return value;
02460 }
02461
02462 String *
02463 Item_func_if::val_str(String *str)
02464 {
02465 assert(fixed == 1);
02466 Item *arg= args[0]->val_bool() ? args[1] : args[2];
02467 String *res= arg->val_str(str);
02468 if (res)
02469 {
02470 res->set_charset(collation.collation);
02471 }
02472 null_value= arg->null_value;
02473 return res;
02474 }
02475
02476
02477 type::Decimal *
02478 Item_func_if::val_decimal(type::Decimal *decimal_value)
02479 {
02480 assert(fixed == 1);
02481 Item *arg= args[0]->val_bool() ? args[1] : args[2];
02482 type::Decimal *value= arg->val_decimal(decimal_value);
02483 null_value= arg->null_value;
02484 return value;
02485 }
02486
02487
02488 void
02489 Item_func_nullif::fix_length_and_dec()
02490 {
02491 Item_bool_func2::fix_length_and_dec();
02492 maybe_null=1;
02493 if (args[0])
02494 {
02495 max_length=args[0]->max_length;
02496 decimals=args[0]->decimals;
02497 unsigned_flag= args[0]->unsigned_flag;
02498 cached_result_type= args[0]->result_type();
02499 if (cached_result_type == STRING_RESULT &&
02500 agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1))
02501 return;
02502 }
02503 }
02504
02505
02516 double
02517 Item_func_nullif::val_real()
02518 {
02519 assert(fixed == 1);
02520 double value;
02521 if (!cmp.compare())
02522 {
02523 null_value=1;
02524 return 0.0;
02525 }
02526 value= args[0]->val_real();
02527 null_value=args[0]->null_value;
02528 return value;
02529 }
02530
02531 int64_t
02532 Item_func_nullif::val_int()
02533 {
02534 assert(fixed == 1);
02535 int64_t value;
02536 if (!cmp.compare())
02537 {
02538 null_value=1;
02539 return 0;
02540 }
02541 value=args[0]->val_int();
02542 null_value=args[0]->null_value;
02543 return value;
02544 }
02545
02546 String *
02547 Item_func_nullif::val_str(String *str)
02548 {
02549 assert(fixed == 1);
02550 String *res;
02551 if (!cmp.compare())
02552 {
02553 null_value=1;
02554 return 0;
02555 }
02556 res=args[0]->val_str(str);
02557 null_value=args[0]->null_value;
02558 return res;
02559 }
02560
02561
02562 type::Decimal *
02563 Item_func_nullif::val_decimal(type::Decimal * decimal_value)
02564 {
02565 assert(fixed == 1);
02566 type::Decimal *res;
02567 if (!cmp.compare())
02568 {
02569 null_value=1;
02570 return 0;
02571 }
02572 res= args[0]->val_decimal(decimal_value);
02573 null_value= args[0]->null_value;
02574 return res;
02575 }
02576
02577
02578 bool
02579 Item_func_nullif::is_null()
02580 {
02581 return (null_value= (!cmp.compare() ? 1 : args[0]->null_value));
02582 }
02583
02584
02606 Item *Item_func_case::find_item(String *)
02607 {
02608 uint32_t value_added_map= 0;
02609
02610 if (first_expr_num == -1)
02611 {
02612 for (uint32_t i=0 ; i < ncases ; i+=2)
02613 {
02614
02615 if (args[i]->val_bool())
02616 return args[i+1];
02617 continue;
02618 }
02619 }
02620 else
02621 {
02622
02623 for (uint32_t i=0 ; i < ncases ; i+=2)
02624 {
02625 cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
02626 assert(cmp_type != ROW_RESULT);
02627 assert(cmp_items[(uint32_t)cmp_type]);
02628 if (!(value_added_map & (1<<(uint32_t)cmp_type)))
02629 {
02630 cmp_items[(uint32_t)cmp_type]->store_value(args[first_expr_num]);
02631 if ((null_value=args[first_expr_num]->null_value))
02632 return else_expr_num != -1 ? args[else_expr_num] : 0;
02633 value_added_map|= 1<<(uint32_t)cmp_type;
02634 }
02635 if (!cmp_items[(uint32_t)cmp_type]->cmp(args[i]) && !args[i]->null_value)
02636 return args[i + 1];
02637 }
02638 }
02639
02640 return else_expr_num != -1 ? args[else_expr_num] : 0;
02641 }
02642
02643
02644 String *Item_func_case::val_str(String *str)
02645 {
02646 assert(fixed == 1);
02647 String *res;
02648 Item *item=find_item(str);
02649
02650 if (!item)
02651 {
02652 null_value=1;
02653 return 0;
02654 }
02655 null_value= 0;
02656 if (!(res=item->val_str(str)))
02657 null_value= 1;
02658 return res;
02659 }
02660
02661
02662 int64_t Item_func_case::val_int()
02663 {
02664 assert(fixed == 1);
02665 char buff[MAX_FIELD_WIDTH];
02666 String dummy_str(buff,sizeof(buff),default_charset());
02667 Item *item=find_item(&dummy_str);
02668 int64_t res;
02669
02670 if (!item)
02671 {
02672 null_value=1;
02673 return 0;
02674 }
02675 res=item->val_int();
02676 null_value=item->null_value;
02677 return res;
02678 }
02679
02680 double Item_func_case::val_real()
02681 {
02682 assert(fixed == 1);
02683 char buff[MAX_FIELD_WIDTH];
02684 String dummy_str(buff,sizeof(buff),default_charset());
02685 Item *item=find_item(&dummy_str);
02686 double res;
02687
02688 if (!item)
02689 {
02690 null_value=1;
02691 return 0;
02692 }
02693 res= item->val_real();
02694 null_value=item->null_value;
02695 return res;
02696 }
02697
02698
02699 type::Decimal *Item_func_case::val_decimal(type::Decimal *decimal_value)
02700 {
02701 assert(fixed == 1);
02702 char buff[MAX_FIELD_WIDTH];
02703 String dummy_str(buff, sizeof(buff), default_charset());
02704 Item *item= find_item(&dummy_str);
02705 type::Decimal *res;
02706
02707 if (!item)
02708 {
02709 null_value=1;
02710 return 0;
02711 }
02712
02713 res= item->val_decimal(decimal_value);
02714 null_value= item->null_value;
02715 return res;
02716 }
02717
02718
02719 bool Item_func_case::fix_fields(Session *session, Item **ref)
02720 {
02721
02722
02723
02724
02725 unsigned char buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2
02726 +sizeof(double)*2+sizeof(int64_t)*2];
02727 bool res= Item_func::fix_fields(session, ref);
02728
02729
02730
02731
02732 if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
02733 return true;
02734 return res;
02735 }
02736
02737
02738 void Item_func_case::agg_str_lengths(Item* arg)
02739 {
02740 set_if_bigger(max_length, arg->max_length);
02741 set_if_bigger(decimals, arg->decimals);
02742 unsigned_flag= unsigned_flag && arg->unsigned_flag;
02743 }
02744
02745
02746 void Item_func_case::agg_num_lengths(Item *arg)
02747 {
02748 uint32_t len= class_decimal_length_to_precision(arg->max_length, arg->decimals,
02749 arg->unsigned_flag) - arg->decimals;
02750 set_if_bigger(max_length, len);
02751 set_if_bigger(decimals, arg->decimals);
02752 unsigned_flag= unsigned_flag && arg->unsigned_flag;
02753 }
02754
02755
02756 void Item_func_case::fix_length_and_dec()
02757 {
02758 Item **agg;
02759 uint32_t nagg;
02760 uint32_t found_types= 0;
02761 if (!(agg= (Item**) memory::sql_alloc(sizeof(Item*)*(ncases+1))))
02762 return;
02763
02764
02765
02766
02767
02768
02769 for (nagg= 0 ; nagg < ncases/2 ; nagg++)
02770 agg[nagg]= args[nagg*2+1];
02771
02772 if (else_expr_num != -1)
02773 agg[nagg++]= args[else_expr_num];
02774
02775 agg_result_type(&cached_result_type, agg, nagg);
02776 if ((cached_result_type == STRING_RESULT) &&
02777 agg_arg_charsets(collation, agg, nagg, MY_COLL_ALLOW_CONV, 1))
02778 return;
02779
02780 cached_field_type= agg_field_type(agg, nagg);
02781
02782
02783
02784
02785 if (first_expr_num != -1)
02786 {
02787 agg[0]= args[first_expr_num];
02788 left_result_type= agg[0]->result_type();
02789
02790 for (nagg= 0; nagg < ncases/2 ; nagg++)
02791 agg[nagg+1]= args[nagg*2];
02792 nagg++;
02793 if (!(found_types= collect_cmp_types(agg, nagg)))
02794 return;
02795
02796 for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
02797 {
02798 if (found_types & (1 << i) && !cmp_items[i])
02799 {
02800 assert((Item_result)i != ROW_RESULT);
02801 if ((Item_result)i == STRING_RESULT &&
02802 agg_arg_charsets(cmp_collation, agg, nagg, MY_COLL_CMP_CONV, 1))
02803 return;
02804 if (!(cmp_items[i]=
02805 cmp_item::get_comparator((Item_result)i,
02806 cmp_collation.collation)))
02807 return;
02808 }
02809 }
02810 }
02811
02812 if (else_expr_num == -1 || args[else_expr_num]->maybe_null)
02813 maybe_null=1;
02814
02815 max_length=0;
02816 decimals=0;
02817 unsigned_flag= true;
02818 if (cached_result_type == STRING_RESULT)
02819 {
02820 for (uint32_t i= 0; i < ncases; i+= 2)
02821 agg_str_lengths(args[i + 1]);
02822 if (else_expr_num != -1)
02823 agg_str_lengths(args[else_expr_num]);
02824 }
02825 else
02826 {
02827 for (uint32_t i= 0; i < ncases; i+= 2)
02828 agg_num_lengths(args[i + 1]);
02829 if (else_expr_num != -1)
02830 agg_num_lengths(args[else_expr_num]);
02831 max_length= class_decimal_precision_to_length(max_length + decimals, decimals,
02832 unsigned_flag);
02833 }
02834 }
02835
02836
02837 uint32_t Item_func_case::decimal_precision() const
02838 {
02839 int max_int_part=0;
02840 for (uint32_t i=0 ; i < ncases ; i+=2)
02841 set_if_bigger(max_int_part, args[i+1]->decimal_int_part());
02842
02843 if (else_expr_num != -1)
02844 set_if_bigger(max_int_part, args[else_expr_num]->decimal_int_part());
02845 return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
02846 }
02847
02848
02854 void Item_func_case::print(String *str)
02855 {
02856 str->append(STRING_WITH_LEN("(case "));
02857 if (first_expr_num != -1)
02858 {
02859 args[first_expr_num]->print(str);
02860 str->append(' ');
02861 }
02862 for (uint32_t i=0 ; i < ncases ; i+=2)
02863 {
02864 str->append(STRING_WITH_LEN("when "));
02865 args[i]->print(str);
02866 str->append(STRING_WITH_LEN(" then "));
02867 args[i+1]->print(str);
02868 str->append(' ');
02869 }
02870 if (else_expr_num != -1)
02871 {
02872 str->append(STRING_WITH_LEN("else "));
02873 args[else_expr_num]->print(str);
02874 str->append(' ');
02875 }
02876 str->append(STRING_WITH_LEN("end)"));
02877 }
02878
02879
02880 void Item_func_case::cleanup()
02881 {
02882 Item_func::cleanup();
02883 for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
02884 {
02885 delete cmp_items[i];
02886 cmp_items[i]= 0;
02887 }
02888 }
02889
02890
02895 String *Item_func_coalesce::str_op(String *str)
02896 {
02897 assert(fixed == 1);
02898 null_value=0;
02899 for (uint32_t i=0 ; i < arg_count ; i++)
02900 {
02901 String *res;
02902 if ((res=args[i]->val_str(str)))
02903 return res;
02904 }
02905 null_value=1;
02906 return 0;
02907 }
02908
02909 int64_t Item_func_coalesce::int_op()
02910 {
02911 assert(fixed == 1);
02912 null_value=0;
02913 for (uint32_t i=0 ; i < arg_count ; i++)
02914 {
02915 int64_t res=args[i]->val_int();
02916 if (!args[i]->null_value)
02917 return res;
02918 }
02919 null_value=1;
02920 return 0;
02921 }
02922
02923 double Item_func_coalesce::real_op()
02924 {
02925 assert(fixed == 1);
02926 null_value=0;
02927 for (uint32_t i=0 ; i < arg_count ; i++)
02928 {
02929 double res= args[i]->val_real();
02930 if (!args[i]->null_value)
02931 return res;
02932 }
02933 null_value=1;
02934 return 0;
02935 }
02936
02937
02938 type::Decimal *Item_func_coalesce::decimal_op(type::Decimal *decimal_value)
02939 {
02940 assert(fixed == 1);
02941 null_value= 0;
02942 for (uint32_t i= 0; i < arg_count; i++)
02943 {
02944 type::Decimal *res= args[i]->val_decimal(decimal_value);
02945 if (!args[i]->null_value)
02946 return res;
02947 }
02948 null_value=1;
02949 return 0;
02950 }
02951
02952
02953 void Item_func_coalesce::fix_length_and_dec()
02954 {
02955 cached_field_type= agg_field_type(args, arg_count);
02956 agg_result_type(&hybrid_type, args, arg_count);
02957
02958 switch (hybrid_type) {
02959 case STRING_RESULT:
02960 count_only_length();
02961 decimals= NOT_FIXED_DEC;
02962 agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1);
02963 break;
02964
02965 case DECIMAL_RESULT:
02966 count_decimal_length();
02967 break;
02968
02969 case REAL_RESULT:
02970 count_real_length();
02971 break;
02972
02973 case INT_RESULT:
02974 count_only_length();
02975 decimals= 0;
02976 break;
02977
02978 case ROW_RESULT:
02979 assert(0);
02980 }
02981 }
02982
02983
02984
02985
02986
02987
02988
02989
02990
02991
02992
02993
02994
02995
02996
02997
02998
02999
03000
03001
03002
03003
03004
03005 static inline int cmp_longs (int64_t a_val, int64_t b_val)
03006 {
03007 return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
03008 }
03009
03010
03011
03012
03013
03014
03015
03016
03017
03018
03019
03020
03021
03022
03023
03024
03025
03026
03027
03028
03029 static inline int cmp_ulongs (uint64_t a_val, uint64_t b_val)
03030 {
03031 return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
03032 }
03033
03034
03035
03036
03037
03038
03039
03040
03041
03042
03043
03044
03045
03046
03047
03048
03049
03050
03051
03052
03053
03054
03055
03056
03057
03058 int cmp_int64_t(void *, in_int64_t::packed_int64_t *a,
03059 in_int64_t::packed_int64_t *b)
03060 {
03061 if (a->unsigned_flag != b->unsigned_flag)
03062 {
03063
03064
03065
03066
03067 if ((a->unsigned_flag && ((uint64_t) a->val) > (uint64_t) INT64_MAX) ||
03068 (b->unsigned_flag && ((uint64_t) b->val) > (uint64_t) INT64_MAX))
03069 return a->unsigned_flag ? 1 : -1;
03070
03071
03072
03073
03074 return cmp_longs (a->val, b->val);
03075 }
03076 if (a->unsigned_flag)
03077 return cmp_ulongs ((uint64_t) a->val, (uint64_t) b->val);
03078 else
03079 return cmp_longs (a->val, b->val);
03080 }
03081
03082 static int cmp_double(void *, double *a, double *b)
03083 {
03084 return *a < *b ? -1 : *a == *b ? 0 : 1;
03085 }
03086
03087 static int cmp_row(void *, cmp_item_row *a, cmp_item_row *b)
03088 {
03089 return a->compare(b);
03090 }
03091
03092
03093 static int cmp_decimal(void *, type::Decimal *a, type::Decimal *b)
03094 {
03095
03096
03097
03098
03099 a->fix_buffer_pointer();
03100 b->fix_buffer_pointer();
03101 return class_decimal_cmp(a, b);
03102 }
03103
03104
03105 void in_vector::sort()
03106 {
03107 internal::my_qsort2(base,used_count,size,compare, (void *) collation);
03108 }
03109
03110
03111 int in_vector::find(Item *item)
03112 {
03113 unsigned char *result=get_value(item);
03114 if (!result || !used_count)
03115 return 0;
03116
03117 uint32_t start,end;
03118 start=0; end=used_count-1;
03119 while (start != end)
03120 {
03121 uint32_t mid=(start+end+1)/2;
03122 int res;
03123 if ((res=(*compare)(collation, base+mid*size, result)) == 0)
03124 return 1;
03125 if (res < 0)
03126 start=mid;
03127 else
03128 end=mid-1;
03129 }
03130 return (int) ((*compare)(collation, base+start*size, result) == 0);
03131 }
03132
03133 in_string::in_string(uint32_t elements,qsort2_cmp cmp_func, const charset_info_st * const cs)
03134 :in_vector(elements, sizeof(String), cmp_func, cs),
03135 tmp(buff, sizeof(buff), &my_charset_bin)
03136 {}
03137
03138 in_string::~in_string()
03139 {
03140 if (base)
03141 {
03142
03143 for (uint32_t i=0 ; i < count ; i++)
03144 ((String*) base)[i].free();
03145 }
03146 }
03147
03148 void in_string::set(uint32_t pos,Item *item)
03149 {
03150 String *str=((String*) base)+pos;
03151 String *res=item->val_str(str);
03152 if (res && res != str)
03153 {
03154 if (res->uses_buffer_owned_by(str))
03155 res->copy();
03156 if (item->type() == Item::FUNC_ITEM)
03157 str->copy(*res);
03158 else
03159 *str= *res;
03160 }
03161 if (!str->charset())
03162 {
03163 const charset_info_st *cs;
03164 if (!(cs= item->collation.collation))
03165 cs= &my_charset_bin;
03166 str->set_charset(cs);
03167 }
03168 }
03169
03170
03171 unsigned char *in_string::get_value(Item *item)
03172 {
03173 return (unsigned char*) item->val_str(&tmp);
03174 }
03175
03176 in_row::in_row(uint32_t elements, Item *)
03177 {
03178 base= (char*) new cmp_item_row[count= elements];
03179 size= sizeof(cmp_item_row);
03180 compare= (qsort2_cmp) cmp_row;
03181
03182
03183
03184
03185 used_count= elements;
03186 collation= 0;
03187 }
03188
03189 in_row::~in_row()
03190 {
03191 delete[] (cmp_item_row*) base;
03192 }
03193
03194 unsigned char *in_row::get_value(Item *item)
03195 {
03196 tmp.store_value(item);
03197 if (item->is_null())
03198 return 0;
03199 return (unsigned char *)&tmp;
03200 }
03201
03202 void in_row::set(uint32_t pos, Item *item)
03203 {
03204 ((cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
03205 return;
03206 }
03207
03208 in_int64_t::in_int64_t(uint32_t elements) :
03209 in_vector(elements, sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
03210 {}
03211
03212 void in_int64_t::set(uint32_t pos,Item *item)
03213 {
03214 struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
03215
03216 buff->val= item->val_int();
03217 buff->unsigned_flag= item->unsigned_flag;
03218 }
03219
03220 unsigned char *in_int64_t::get_value(Item *item)
03221 {
03222 tmp.val= item->val_int();
03223 if (item->null_value)
03224 return 0;
03225 tmp.unsigned_flag= item->unsigned_flag;
03226 return (unsigned char*) &tmp;
03227 }
03228
03229 in_datetime::in_datetime(Item *warn_item_arg, uint32_t elements) :
03230 in_int64_t(elements),
03231 session(current_session),
03232 warn_item(warn_item_arg),
03233 lval_cache(0)
03234 {}
03235
03236 void in_datetime::set(uint32_t pos, Item *item)
03237 {
03238 Item **tmp_item= &item;
03239 bool is_null;
03240 struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
03241
03242 buff->val= get_datetime_value(session, &tmp_item, 0, warn_item, &is_null);
03243 buff->unsigned_flag= 1L;
03244 }
03245
03246 unsigned char *in_datetime::get_value(Item *item)
03247 {
03248 bool is_null;
03249 Item **tmp_item= lval_cache ? &lval_cache : &item;
03250 tmp.val= get_datetime_value(session, &tmp_item, &lval_cache, warn_item, &is_null);
03251 if (item->null_value)
03252 return 0;
03253 tmp.unsigned_flag= 1L;
03254 return (unsigned char*) &tmp;
03255 }
03256
03257 in_double::in_double(uint32_t elements)
03258 :in_vector(elements,sizeof(double),(qsort2_cmp) cmp_double, 0)
03259 {}
03260
03261 void in_double::set(uint32_t pos,Item *item)
03262 {
03263 ((double*) base)[pos]= item->val_real();
03264 }
03265
03266 unsigned char *in_double::get_value(Item *item)
03267 {
03268 tmp= item->val_real();
03269 if (item->null_value)
03270 return 0;
03271 return (unsigned char*) &tmp;
03272 }
03273
03274
03275 in_decimal::in_decimal(uint32_t elements)
03276 :in_vector(elements, sizeof(type::Decimal),(qsort2_cmp) cmp_decimal, 0)
03277 {}
03278
03279
03280 void in_decimal::set(uint32_t pos, Item *item)
03281 {
03282
03283 type::Decimal *dec= ((type::Decimal *)base) + pos;
03284 dec->len= DECIMAL_BUFF_LENGTH;
03285 dec->fix_buffer_pointer();
03286 type::Decimal *res= item->val_decimal(dec);
03287
03288 if (!item->null_value && res != dec)
03289 class_decimal2decimal(res, dec);
03290 }
03291
03292
03293 unsigned char *in_decimal::get_value(Item *item)
03294 {
03295 type::Decimal *result= item->val_decimal(&val);
03296 if (item->null_value)
03297 return 0;
03298 return (unsigned char *)result;
03299 }
03300
03301
03302 cmp_item* cmp_item::get_comparator(Item_result type,
03303 const charset_info_st * const cs)
03304 {
03305 switch (type) {
03306 case STRING_RESULT:
03307 return new cmp_item_sort_string(cs);
03308
03309 case INT_RESULT:
03310 return new cmp_item_int;
03311
03312 case REAL_RESULT:
03313 return new cmp_item_real;
03314
03315 case ROW_RESULT:
03316 return new cmp_item_row;
03317
03318 case DECIMAL_RESULT:
03319 return new cmp_item_decimal;
03320 }
03321
03322 return 0;
03323 }
03324
03325
03326 cmp_item* cmp_item_sort_string::make_same()
03327 {
03328 return new cmp_item_sort_string_in_static(cmp_charset);
03329 }
03330
03331 cmp_item* cmp_item_int::make_same()
03332 {
03333 return new cmp_item_int();
03334 }
03335
03336 cmp_item* cmp_item_real::make_same()
03337 {
03338 return new cmp_item_real();
03339 }
03340
03341 cmp_item* cmp_item_row::make_same()
03342 {
03343 return new cmp_item_row();
03344 }
03345
03346
03347 cmp_item_row::~cmp_item_row()
03348 {
03349 if (comparators)
03350 {
03351 for (uint32_t i= 0; i < n; i++)
03352 {
03353 if (comparators[i])
03354 delete comparators[i];
03355 }
03356 }
03357 return;
03358 }
03359
03360
03361 void cmp_item_row::alloc_comparators()
03362 {
03363 if (!comparators)
03364 comparators= (cmp_item **) current_session->mem.calloc(sizeof(cmp_item *)*n);
03365 }
03366
03367
03368 void cmp_item_row::store_value(Item *item)
03369 {
03370 n= item->cols();
03371 alloc_comparators();
03372 if (comparators)
03373 {
03374 item->bring_value();
03375 item->null_value= 0;
03376 for (uint32_t i=0; i < n; i++)
03377 {
03378 if (!comparators[i])
03379 if (!(comparators[i]=
03380 cmp_item::get_comparator(item->element_index(i)->result_type(),
03381 item->element_index(i)->collation.collation)))
03382 break;
03383 comparators[i]->store_value(item->element_index(i));
03384 item->null_value|= item->element_index(i)->null_value;
03385 }
03386 }
03387 return;
03388 }
03389
03390
03391 void cmp_item_row::store_value_by_template(cmp_item *t, Item *item)
03392 {
03393 cmp_item_row *tmpl= (cmp_item_row*) t;
03394 if (tmpl->n != item->cols())
03395 {
03396 my_error(ER_OPERAND_COLUMNS, MYF(0), tmpl->n);
03397 return;
03398 }
03399 n= tmpl->n;
03400 if ((comparators= (cmp_item **) memory::sql_alloc(sizeof(cmp_item *)*n)))
03401 {
03402 item->bring_value();
03403 item->null_value= 0;
03404 for (uint32_t i=0; i < n; i++)
03405 {
03406 if (!(comparators[i]= tmpl->comparators[i]->make_same()))
03407 break;
03408 comparators[i]->store_value_by_template(tmpl->comparators[i],
03409 item->element_index(i));
03410 item->null_value|= item->element_index(i)->null_value;
03411 }
03412 }
03413 }
03414
03415
03416 int cmp_item_row::cmp(Item *arg)
03417 {
03418 arg->null_value= 0;
03419 if (arg->cols() != n)
03420 {
03421 my_error(ER_OPERAND_COLUMNS, MYF(0), n);
03422 return 1;
03423 }
03424 bool was_null= 0;
03425 arg->bring_value();
03426 for (uint32_t i=0; i < n; i++)
03427 {
03428 if (comparators[i]->cmp(arg->element_index(i)))
03429 {
03430 if (!arg->element_index(i)->null_value)
03431 return 1;
03432 was_null= 1;
03433 }
03434 }
03435 return (arg->null_value= was_null);
03436 }
03437
03438
03439 int cmp_item_row::compare(cmp_item *c)
03440 {
03441 cmp_item_row *l_cmp= (cmp_item_row *) c;
03442 for (uint32_t i=0; i < n; i++)
03443 {
03444 int res;
03445 if ((res= comparators[i]->compare(l_cmp->comparators[i])))
03446 return res;
03447 }
03448 return 0;
03449 }
03450
03451
03452 void cmp_item_decimal::store_value(Item *item)
03453 {
03454 type::Decimal *val= item->val_decimal(&value);
03455
03456 if (val && val != &value)
03457 class_decimal2decimal(val, &value);
03458 }
03459
03460
03461 int cmp_item_decimal::cmp(Item *arg)
03462 {
03463 type::Decimal tmp_buf, *tmp= arg->val_decimal(&tmp_buf);
03464 if (arg->null_value)
03465 return 1;
03466 return class_decimal_cmp(&value, tmp);
03467 }
03468
03469
03470 int cmp_item_decimal::compare(cmp_item *arg)
03471 {
03472 cmp_item_decimal *l_cmp= (cmp_item_decimal*) arg;
03473 return class_decimal_cmp(&value, &l_cmp->value);
03474 }
03475
03476
03477 cmp_item* cmp_item_decimal::make_same()
03478 {
03479 return new cmp_item_decimal();
03480 }
03481
03482
03483 void cmp_item_datetime::store_value(Item *item)
03484 {
03485 bool is_null;
03486 Item **tmp_item= lval_cache ? &lval_cache : &item;
03487 value= get_datetime_value(session, &tmp_item, &lval_cache, warn_item, &is_null);
03488 }
03489
03490
03491 int cmp_item_datetime::cmp(Item *arg)
03492 {
03493 bool is_null;
03494 Item **tmp_item= &arg;
03495 return value !=
03496 get_datetime_value(session, &tmp_item, 0, warn_item, &is_null);
03497 }
03498
03499
03500 int cmp_item_datetime::compare(cmp_item *ci)
03501 {
03502 cmp_item_datetime *l_cmp= (cmp_item_datetime *)ci;
03503 return (value < l_cmp->value) ? -1 : ((value == l_cmp->value) ? 0 : 1);
03504 }
03505
03506
03507 cmp_item *cmp_item_datetime::make_same()
03508 {
03509 return new cmp_item_datetime(warn_item);
03510 }
03511
03512
03513 bool Item_func_in::nulls_in_row()
03514 {
03515 Item **arg,**arg_end;
03516 for (arg= args+1, arg_end= args+arg_count; arg != arg_end ; arg++)
03517 {
03518 if ((*arg)->null_inside())
03519 return 1;
03520 }
03521 return 0;
03522 }
03523
03524
03553 bool
03554 Item_func_in::fix_fields(Session *session, Item **ref)
03555 {
03556 Item **arg, **arg_end;
03557
03558 if (Item_func_opt_neg::fix_fields(session, ref))
03559 return 1;
03560
03561
03562 if (pred_level && negated)
03563 return 0;
03564
03565
03566 not_null_tables_cache= ~(table_map) 0;
03567 for (arg= args + 1, arg_end= args + arg_count; arg != arg_end; arg++)
03568 not_null_tables_cache&= (*arg)->not_null_tables();
03569 not_null_tables_cache|= (*args)->not_null_tables();
03570 return 0;
03571 }
03572
03573
03574 static int srtcmp_in(const charset_info_st * const cs, const String *x,const String *y)
03575 {
03576 return cs->coll->strnncollsp(cs,
03577 (unsigned char *) x->ptr(),x->length(),
03578 (unsigned char *) y->ptr(),y->length(), 0);
03579 }
03580
03581
03582 void Item_func_in::fix_length_and_dec()
03583 {
03584 Item **arg, **arg_end;
03585 bool const_itm= 1;
03586 bool datetime_found= false;
03587
03588 bool compare_as_datetime= false;
03589 Item *date_arg= 0;
03590 uint32_t found_types= 0;
03591 uint32_t type_cnt= 0;
03592 Item_result cmp_type= STRING_RESULT;
03593 left_result_type= args[0]->result_type();
03594 if (!(found_types= collect_cmp_types(args, arg_count, true)))
03595 return;
03596
03597 for (arg= args + 1, arg_end= args + arg_count; arg != arg_end ; arg++)
03598 {
03599 if (!arg[0]->const_item())
03600 {
03601 const_itm= 0;
03602 break;
03603 }
03604 }
03605 for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
03606 {
03607 if (found_types & 1 << i)
03608 {
03609 (type_cnt)++;
03610 cmp_type= (Item_result) i;
03611 }
03612 }
03613
03614 if (type_cnt == 1)
03615 {
03616 if (cmp_type == STRING_RESULT &&
03617 agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1))
03618 return;
03619 arg_types_compatible= true;
03620 }
03621 if (type_cnt == 1)
03622 {
03623
03624
03625
03626
03627 if (cmp_type == ROW_RESULT)
03628 {
03629 cmp_item_row *cmp= 0;
03630 if (const_itm && !nulls_in_row())
03631 {
03632 array= new in_row(arg_count-1, 0);
03633 cmp= &((in_row*)array)->tmp;
03634 }
03635 else
03636 {
03637 cmp= new cmp_item_row;
03638 cmp_items[ROW_RESULT]= cmp;
03639 }
03640 cmp->n= args[0]->cols();
03641 cmp->alloc_comparators();
03642 }
03643
03644 if (cmp_type == STRING_RESULT || cmp_type == ROW_RESULT)
03645 {
03646 uint32_t col, num_cols= args[0]->cols();
03647
03648 for (col= 0; col < num_cols; col++)
03649 {
03650 bool skip_column= false;
03651
03652
03653
03654
03655 for (arg= args, arg_end= args + arg_count; arg != arg_end ; arg++)
03656 {
03657 Item *itm= ((cmp_type == STRING_RESULT) ? arg[0] :
03658 arg[0]->element_index(col));
03659 if (itm->result_type() != STRING_RESULT)
03660 {
03661 skip_column= true;
03662 break;
03663 }
03664 else if (itm->is_datetime())
03665 {
03666 datetime_found= true;
03667
03668
03669
03670
03671 if (!date_arg)
03672 date_arg= itm;
03673 else if (itm->field_type() == DRIZZLE_TYPE_DATETIME)
03674 {
03675 date_arg= itm;
03676
03677 if (cmp_type == STRING_RESULT)
03678 break;
03679 }
03680 }
03681 }
03682 if (skip_column)
03683 continue;
03684 if (datetime_found)
03685 {
03686 if (cmp_type == ROW_RESULT)
03687 {
03688 cmp_item **cmp= 0;
03689 if (array)
03690 cmp= ((in_row*)array)->tmp.comparators + col;
03691 else
03692 cmp= ((cmp_item_row*)cmp_items[ROW_RESULT])->comparators + col;
03693 *cmp= new cmp_item_datetime(date_arg);
03694
03695 date_arg= 0;
03696 datetime_found= false;
03697 }
03698 else
03699 compare_as_datetime= true;
03700 }
03701 }
03702 }
03703 }
03704
03705
03706
03707
03708 if (type_cnt == 1 && const_itm && !nulls_in_row())
03709 {
03710 if (compare_as_datetime)
03711 {
03712 array= new in_datetime(date_arg, arg_count - 1);
03713 }
03714 else
03715 {
03716
03717
03718
03719
03720
03721
03722
03723 if (args[0]->real_item()->type() == FIELD_ITEM &&
03724 cmp_type != INT_RESULT)
03725 {
03726 Item_field *field_item= (Item_field*) (args[0]->real_item());
03727 if (field_item->field->can_be_compared_as_int64_t())
03728 {
03729 bool all_converted= true;
03730 for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
03731 {
03732 if (!convert_constant_item (&getSession(), field_item, &arg[0]))
03733 all_converted= false;
03734 }
03735 if (all_converted)
03736 cmp_type= INT_RESULT;
03737 }
03738 }
03739
03740 switch (cmp_type) {
03741 case STRING_RESULT:
03742 array=new in_string(arg_count-1,(qsort2_cmp) srtcmp_in,
03743 cmp_collation.collation);
03744 break;
03745
03746 case INT_RESULT:
03747 array= new in_int64_t(arg_count-1);
03748 break;
03749
03750 case REAL_RESULT:
03751 array= new in_double(arg_count-1);
03752 break;
03753
03754 case ROW_RESULT:
03755
03756
03757
03758
03759
03760 ((in_row*)array)->tmp.store_value(args[0]);
03761 break;
03762
03763 case DECIMAL_RESULT:
03764 array= new in_decimal(arg_count - 1);
03765 break;
03766 }
03767 }
03768
03769 if (array && !(getSession().is_fatal_error))
03770 {
03771 uint32_t j=0;
03772 for (uint32_t arg_num=1 ; arg_num < arg_count ; arg_num++)
03773 {
03774 if (!args[arg_num]->null_value)
03775 {
03776 array->set(j,args[arg_num]);
03777 j++;
03778 }
03779 else
03780 have_null= 1;
03781 }
03782 if ((array->used_count= j))
03783 array->sort();
03784 }
03785 }
03786 else
03787 {
03788 if (compare_as_datetime)
03789 cmp_items[STRING_RESULT]= new cmp_item_datetime(date_arg);
03790 else
03791 {
03792 for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
03793 {
03794 if (found_types & (1 << i) && !cmp_items[i])
03795 {
03796 if ((Item_result)i == STRING_RESULT &&
03797 agg_arg_charsets(cmp_collation, args, arg_count,
03798 MY_COLL_CMP_CONV, 1))
03799 return;
03800 if (!cmp_items[i] && !(cmp_items[i]=
03801 cmp_item::get_comparator((Item_result)i,
03802 cmp_collation.collation)))
03803 return;
03804 }
03805 }
03806 }
03807 }
03808 max_length= 1;
03809 }
03810
03811
03812 void Item_func_in::print(String *str)
03813 {
03814 str->append('(');
03815 args[0]->print(str);
03816 if (negated)
03817 str->append(STRING_WITH_LEN(" not"));
03818 str->append(STRING_WITH_LEN(" in ("));
03819 print_args(str, 1);
03820 str->append(STRING_WITH_LEN("))"));
03821 }
03822
03823
03824
03825
03826
03827
03828
03829
03830
03831
03832
03833
03834
03835
03836
03837
03838
03839
03840
03841
03842
03843
03844
03845
03846
03847
03848
03849 int64_t Item_func_in::val_int()
03850 {
03851 cmp_item *in_item;
03852 assert(fixed == 1);
03853 uint32_t value_added_map= 0;
03854 if (array)
03855 {
03856 int tmp=array->find(args[0]);
03857 null_value=args[0]->null_value || (!tmp && have_null);
03858 return (int64_t) (!null_value && tmp != negated);
03859 }
03860
03861 for (uint32_t i= 1 ; i < arg_count ; i++)
03862 {
03863 Item_result cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
03864 in_item= cmp_items[(uint32_t)cmp_type];
03865 assert(in_item);
03866 if (!(value_added_map & (1 << (uint32_t)cmp_type)))
03867 {
03868 in_item->store_value(args[0]);
03869 if ((null_value=args[0]->null_value))
03870 return 0;
03871 have_null= 0;
03872 value_added_map|= 1 << (uint32_t)cmp_type;
03873 }
03874 if (!in_item->cmp(args[i]) && !args[i]->null_value)
03875 return (int64_t) (!negated);
03876 have_null|= args[i]->null_value;
03877 }
03878
03879 null_value= have_null;
03880 return (int64_t) (!null_value && negated);
03881 }
03882
03883
03884 Item_cond::Item_cond(Session *session, Item_cond *item)
03885 :item::function::Boolean(session, item),
03886 abort_on_null(item->abort_on_null),
03887 and_tables_cache(item->and_tables_cache)
03888 {
03889
03890
03891
03892 }
03893
03894
03895 void Item_cond::copy_andor_arguments(Session *session, Item_cond *item)
03896 {
03897 List<Item>::iterator li(item->list.begin());
03898 while (Item *it= li++)
03899 list.push_back(it->copy_andor_structure(session));
03900 }
03901
03902
03903 bool
03904 Item_cond::fix_fields(Session *session, Item **)
03905 {
03906 assert(fixed == 0);
03907 List<Item>::iterator li(list.begin());
03908 Item *item;
03909 void *orig_session_marker= session->session_marker;
03910 unsigned char buff[sizeof(char*)];
03911 not_null_tables_cache= used_tables_cache= 0;
03912 const_item_cache= true;
03913
03914 if (functype() == COND_OR_FUNC)
03915 session->session_marker= 0;
03916
03917
03918
03919
03920 and_tables_cache= ~(table_map) 0;
03921
03922 if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
03923 return true;
03924
03925
03926
03927
03928
03929
03930
03931
03932
03933
03934
03935
03936
03937
03938
03939 while ((item=li++))
03940 {
03941 table_map tmp_table_map;
03942 while (item->type() == Item::COND_ITEM &&
03943 ((Item_cond*) item)->functype() == functype() &&
03944 !((Item_cond*) item)->list.is_empty())
03945 {
03946 li.replace(((Item_cond*) item)->list);
03947 ((Item_cond*) item)->list.clear();
03948 item= &*li;
03949 }
03950 if (abort_on_null)
03951 item->top_level_item();
03952
03953
03954 if ((!item->fixed &&
03955 item->fix_fields(session, li.ref())) ||
03956 (item= &*li)->check_cols(1))
03957 return true;
03958 used_tables_cache|= item->used_tables();
03959 if (item->const_item())
03960 and_tables_cache= (table_map) 0;
03961 else
03962 {
03963 tmp_table_map= item->not_null_tables();
03964 not_null_tables_cache|= tmp_table_map;
03965 and_tables_cache&= tmp_table_map;
03966 const_item_cache= false;
03967 }
03968 with_sum_func= with_sum_func || item->with_sum_func;
03969 with_subselect|= item->with_subselect;
03970 if (item->maybe_null)
03971 maybe_null=1;
03972 }
03973 session->lex().current_select->cond_count+= list.size();
03974 session->session_marker= orig_session_marker;
03975 fix_length_and_dec();
03976 fixed= 1;
03977 return false;
03978 }
03979
03980
03981 void Item_cond::fix_after_pullout(Select_Lex *new_parent, Item **)
03982 {
03983 List<Item>::iterator li(list.begin());
03984 Item *item;
03985
03986 used_tables_cache=0;
03987 const_item_cache= true;
03988
03989 and_tables_cache= ~(table_map) 0;
03990 not_null_tables_cache= 0;
03991
03992 while ((item=li++))
03993 {
03994 table_map tmp_table_map;
03995 item->fix_after_pullout(new_parent, li.ref());
03996 item= &*li;
03997 used_tables_cache|= item->used_tables();
03998 const_item_cache&= item->const_item();
03999
04000 if (item->const_item())
04001 and_tables_cache= (table_map) 0;
04002 else
04003 {
04004 tmp_table_map= item->not_null_tables();
04005 not_null_tables_cache|= tmp_table_map;
04006 and_tables_cache&= tmp_table_map;
04007 const_item_cache= false;
04008 }
04009 }
04010 }
04011
04012
04013 bool Item_cond::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
04014 {
04015 List<Item>::iterator li(list.begin());
04016 Item *item;
04017 while ((item= li++))
04018 if (item->walk(processor, walk_subquery, arg))
04019 return 1;
04020 return Item_func::walk(processor, walk_subquery, arg);
04021 }
04022
04023
04042 Item *Item_cond::transform(Item_transformer transformer, unsigned char *arg)
04043 {
04044 List<Item>::iterator li(list.begin());
04045 Item *item;
04046 while ((item= li++))
04047 {
04048 Item *new_item= item->transform(transformer, arg);
04049 if (!new_item)
04050 return 0;
04051 *li.ref()= new_item;
04052 }
04053 return Item_func::transform(transformer, arg);
04054 }
04055
04056
04081 Item *Item_cond::compile(Item_analyzer analyzer, unsigned char **arg_p,
04082 Item_transformer transformer, unsigned char *arg_t)
04083 {
04084 if (!(this->*analyzer)(arg_p))
04085 return 0;
04086
04087 List<Item>::iterator li(list.begin());
04088 Item *item;
04089 while ((item= li++))
04090 {
04091
04092
04093
04094
04095 unsigned char *arg_v= *arg_p;
04096 Item *new_item= item->compile(analyzer, &arg_v, transformer, arg_t);
04097 if (new_item && new_item != item)
04098 li.replace(new_item);
04099 }
04100 return Item_func::transform(transformer, arg_t);
04101 }
04102
04103 void Item_cond::traverse_cond(Cond_traverser traverser,
04104 void *arg, traverse_order order)
04105 {
04106 List<Item>::iterator li(list.begin());
04107 Item *item;
04108
04109 switch (order) {
04110 case (T_PREFIX):
04111 (*traverser)(this, arg);
04112 while ((item= li++))
04113 {
04114 item->traverse_cond(traverser, arg, order);
04115 }
04116 (*traverser)(NULL, arg);
04117 break;
04118 case (T_POSTFIX):
04119 while ((item= li++))
04120 {
04121 item->traverse_cond(traverser, arg, order);
04122 }
04123 (*traverser)(this, arg);
04124 }
04125 }
04126
04144 void Item_cond::split_sum_func(Session *session, Item **ref_pointer_array, List<Item> &fields)
04145 {
04146 List<Item>::iterator li(list.begin());
04147 Item *item;
04148 while ((item= li++))
04149 item->split_sum_func(session, ref_pointer_array, fields, li.ref(), true);
04150 }
04151
04152
04153 table_map
04154 Item_cond::used_tables() const
04155 {
04156 return used_tables_cache;
04157 }
04158
04159
04160 void Item_cond::update_used_tables()
04161 {
04162 List<Item>::iterator li(list.begin());
04163 Item *item;
04164
04165 used_tables_cache=0;
04166 const_item_cache= true;
04167 while ((item=li++))
04168 {
04169 item->update_used_tables();
04170 used_tables_cache|= item->used_tables();
04171 const_item_cache&= item->const_item();
04172 }
04173 }
04174
04175
04176 void Item_cond::print(String *str)
04177 {
04178 str->append('(');
04179 List<Item>::iterator li(list.begin());
04180 Item *item;
04181 if ((item=li++))
04182 item->print(str);
04183 while ((item=li++))
04184 {
04185 str->append(' ');
04186 str->append(func_name());
04187 str->append(' ');
04188 item->print(str);
04189 }
04190 str->append(')');
04191 }
04192
04193
04194 void Item_cond::neg_arguments(Session *session)
04195 {
04196 List<Item>::iterator li(list.begin());
04197 Item *item;
04198 while ((item= li++))
04199 {
04200 Item *new_item= item->neg_transformer(session);
04201 if (!new_item)
04202 new_item= new Item_func_not(item);
04203 li.replace(new_item);
04204 }
04205 }
04206
04207
04228 int64_t Item_cond_and::val_int()
04229 {
04230 assert(fixed == 1);
04231 List<Item>::iterator li(list.begin());
04232 Item *item;
04233 null_value= 0;
04234 while ((item=li++))
04235 {
04236 if (!item->val_bool())
04237 {
04238 if (abort_on_null || !(null_value= item->null_value))
04239 return 0;
04240 }
04241 }
04242 return null_value ? 0 : 1;
04243 }
04244
04245
04246 int64_t Item_cond_or::val_int()
04247 {
04248 assert(fixed == 1);
04249 List<Item>::iterator li(list.begin());
04250 Item *item;
04251 null_value=0;
04252 while ((item=li++))
04253 {
04254 if (item->val_bool())
04255 {
04256 null_value=0;
04257 return 1;
04258 }
04259 if (item->null_value)
04260 null_value=1;
04261 }
04262 return 0;
04263 }
04264
04285 Item *and_expressions(Item *a, Item *b, Item **org_item)
04286 {
04287 if (!a)
04288 return (*org_item= (Item*) b);
04289 if (a == *org_item)
04290 {
04291 Item_cond *res;
04292 res= new Item_cond_and(a, (Item*) b);
04293 res->used_tables_cache= a->used_tables() | b->used_tables();
04294 res->not_null_tables_cache= a->not_null_tables() | b->not_null_tables();
04295 return res;
04296 }
04297 ((Item_cond_and*) a)->add((Item*) b);
04298 ((Item_cond_and*) a)->used_tables_cache|= b->used_tables();
04299 ((Item_cond_and*) a)->not_null_tables_cache|= b->not_null_tables();
04300 return a;
04301 }
04302
04303
04304 int64_t Item_func_isnull::val_int()
04305 {
04306 assert(fixed == 1);
04307
04308
04309
04310
04311 if (!used_tables_cache && !with_subselect)
04312 return cached_value;
04313 return args[0]->is_null() ? 1: 0;
04314 }
04315
04316 int64_t Item_is_not_null_test::val_int()
04317 {
04318 assert(fixed == 1);
04319 if (!used_tables_cache && !with_subselect)
04320 {
04321 owner->was_null|= (!cached_value);
04322 return(cached_value);
04323 }
04324 if (args[0]->is_null())
04325 {
04326 owner->was_null|= 1;
04327 return 0;
04328 }
04329 else
04330 return 1;
04331 }
04332
04336 void Item_is_not_null_test::update_used_tables()
04337 {
04338 if (!args[0]->maybe_null)
04339 {
04340 used_tables_cache= 0;
04341 cached_value= (int64_t) 1;
04342 }
04343 else
04344 {
04345 args[0]->update_used_tables();
04346 if (!(used_tables_cache=args[0]->used_tables()) && !with_subselect)
04347 {
04348
04349 cached_value= (int64_t) !args[0]->is_null();
04350 }
04351 }
04352 }
04353
04354
04355 int64_t Item_func_isnotnull::val_int()
04356 {
04357 assert(fixed == 1);
04358 return args[0]->is_null() ? 0 : 1;
04359 }
04360
04361
04362 void Item_func_isnotnull::print(String *str)
04363 {
04364 str->append('(');
04365 args[0]->print(str);
04366 str->append(STRING_WITH_LEN(" is not null)"));
04367 }
04368
04369
04370 int64_t Item_func_like::val_int()
04371 {
04372 assert(fixed == 1);
04373 String* res = args[0]->val_str(&tmp_value1);
04374 if (args[0]->null_value)
04375 {
04376 null_value=1;
04377 return 0;
04378 }
04379 String* res2 = args[1]->val_str(&tmp_value2);
04380 if (args[1]->null_value)
04381 {
04382 null_value=1;
04383 return 0;
04384 }
04385 null_value=0;
04386 if (canDoTurboBM)
04387 return turboBM_matches(res->ptr(), res->length()) ? 1 : 0;
04388 return my_wildcmp(cmp.cmp_collation.collation,
04389 res->ptr(),res->ptr()+res->length(),
04390 res2->ptr(),res2->ptr()+res2->length(),
04391 make_escape_code(cmp.cmp_collation.collation, escape), internal::wild_one,internal::wild_many) ? 0 : 1;
04392 }
04393
04394
04399 Item_func::optimize_type Item_func_like::select_optimize() const
04400 {
04401 if (args[1]->const_item())
04402 {
04403 String* res2= args[1]->val_str((String *)&tmp_value2);
04404
04405 if (!res2)
04406 return OPTIMIZE_NONE;
04407
04408 if (*res2->ptr() != internal::wild_many)
04409 {
04410 if (args[0]->result_type() != STRING_RESULT || *res2->ptr() != internal::wild_one)
04411 return OPTIMIZE_OP;
04412 }
04413 }
04414 return OPTIMIZE_NONE;
04415 }
04416
04417
04418 bool Item_func_like::fix_fields(Session *session, Item **ref)
04419 {
04420 assert(fixed == 0);
04421 if (Item_bool_func2::fix_fields(session, ref) ||
04422 escape_item->fix_fields(session, &escape_item))
04423 return true;
04424
04425 if (!escape_item->const_during_execution())
04426 {
04427 my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
04428 return true;
04429 }
04430
04431 if (escape_item->const_item())
04432 {
04433
04434
04435 String *escape_str= escape_item->val_str(&tmp_value1);
04436 if (escape_str)
04437 {
04438 escape= (char *)memory::sql_alloc(escape_str->length());
04439 strcpy(escape, escape_str->ptr());
04440 }
04441 else
04442 {
04443 escape= (char *)memory::sql_alloc(1);
04444 strcpy(escape, "\\");
04445 }
04446
04447
04448
04449
04450
04451 if (args[1]->const_item() && not collation.collation->use_strnxfrm())
04452 {
04453 String* res2 = args[1]->val_str(&tmp_value2);
04454 if (!res2)
04455 return false;
04456
04457 const size_t len = res2->length();
04458 const char* first = res2->ptr();
04459 const char* last = first + len - 1;
04460
04461
04462
04463
04464
04465 if (len > MIN_TURBOBM_PATTERN_LEN + 2 &&
04466 *first == internal::wild_many &&
04467 *last == internal::wild_many)
04468 {
04469 const char* tmp = first + 1;
04470 for (; *tmp != internal::wild_many && *tmp != internal::wild_one; tmp++)
04471 {
04472 if (escape == tmp)
04473 break;
04474 }
04475
04476 canDoTurboBM = (tmp == last) && !use_mb(args[0]->collation.collation);
04477 }
04478 if (canDoTurboBM)
04479 {
04480 pattern = first + 1;
04481 pattern_len = (int) len - 2;
04482 int *suff = (int*) session->mem.alloc(sizeof(int) * ((pattern_len + 1)*2+ alphabet_size));
04483 bmGs = suff + pattern_len + 1;
04484 bmBc = bmGs + pattern_len + 1;
04485 turboBM_compute_good_suffix_shifts(suff);
04486 turboBM_compute_bad_character_shifts();
04487 }
04488 }
04489 }
04490 return false;
04491 }
04492
04493 void Item_func_like::cleanup()
04494 {
04495 canDoTurboBM= false;
04496 Item_bool_func2::cleanup();
04497 }
04498
04499 static unsigned char likeconv(const charset_info_st *cs, unsigned char a)
04500 {
04501 #ifdef LIKE_CMP_TOUPPER
04502 return cs->toupper(a);
04503 #else
04504 return cs->sort_order[a];
04505 #endif
04506 }
04507
04512 void Item_func_like::turboBM_compute_suffixes(int *suff)
04513 {
04514 const int plm1 = pattern_len - 1;
04515 int f = 0;
04516 int g = plm1;
04517 int *const splm1 = suff + plm1;
04518 const charset_info_st * const cs= cmp.cmp_collation.collation;
04519
04520 *splm1 = pattern_len;
04521
04522 if (!cs->sort_order)
04523 {
04524 for (int i = pattern_len - 2; i >= 0; i--)
04525 {
04526 int tmp = *(splm1 + i - f);
04527 if (g < i && tmp < i - g)
04528 suff[i] = tmp;
04529 else
04530 {
04531 if (i < g)
04532 g = i;
04533 f = i;
04534 while (g >= 0 && pattern[g] == pattern[g + plm1 - f])
04535 g--;
04536 suff[i] = f - g;
04537 }
04538 }
04539 }
04540 else
04541 {
04542 for (int i = pattern_len - 2; 0 <= i; --i)
04543 {
04544 int tmp = *(splm1 + i - f);
04545 if (g < i && tmp < i - g)
04546 suff[i] = tmp;
04547 else
04548 {
04549 if (i < g)
04550 g = i;
04551 f = i;
04552 while (g >= 0 &&
04553 likeconv(cs, pattern[g]) == likeconv(cs, pattern[g + plm1 - f]))
04554 g--;
04555 suff[i] = f - g;
04556 }
04557 }
04558 }
04559 }
04560
04561
04566 void Item_func_like::turboBM_compute_good_suffix_shifts(int *suff)
04567 {
04568 turboBM_compute_suffixes(suff);
04569
04570 int *end = bmGs + pattern_len;
04571 int *k;
04572 for (k = bmGs; k < end; k++)
04573 *k = pattern_len;
04574
04575 int tmp;
04576 int i;
04577 int j = 0;
04578 const int plm1 = pattern_len - 1;
04579 for (i = plm1; i > -1; i--)
04580 {
04581 if (suff[i] == i + 1)
04582 {
04583 for (tmp = plm1 - i; j < tmp; j++)
04584 {
04585 int *tmp2 = bmGs + j;
04586 if (*tmp2 == pattern_len)
04587 *tmp2 = tmp;
04588 }
04589 }
04590 }
04591
04592 int *tmp2;
04593 for (tmp = plm1 - i; j < tmp; j++)
04594 {
04595 tmp2 = bmGs + j;
04596 if (*tmp2 == pattern_len)
04597 *tmp2 = tmp;
04598 }
04599
04600 tmp2 = bmGs + plm1;
04601 for (i = 0; i <= pattern_len - 2; i++)
04602 *(tmp2 - suff[i]) = plm1 - i;
04603 }
04604
04605
04610 void Item_func_like::turboBM_compute_bad_character_shifts()
04611 {
04612 int *i;
04613 int *end = bmBc + alphabet_size;
04614 int j;
04615 const int plm1 = pattern_len - 1;
04616 const charset_info_st *const cs= cmp.cmp_collation.collation;
04617
04618 for (i = bmBc; i < end; i++)
04619 *i = pattern_len;
04620
04621 if (!cs->sort_order)
04622 {
04623 for (j = 0; j < plm1; j++)
04624 bmBc[(uint32_t) (unsigned char) pattern[j]] = plm1 - j;
04625 }
04626 else
04627 {
04628 for (j = 0; j < plm1; j++)
04629 bmBc[(uint32_t) likeconv(cs,pattern[j])] = plm1 - j;
04630 }
04631 }
04632
04633
04641 bool Item_func_like::turboBM_matches(const char* text, int text_len) const
04642 {
04643 int bcShift;
04644 int turboShift;
04645 int shift = pattern_len;
04646 int j = 0;
04647 int u = 0;
04648 const charset_info_st * const cs= cmp.cmp_collation.collation;
04649
04650 const int plm1= pattern_len - 1;
04651 const int tlmpl= text_len - pattern_len;
04652
04653
04654 if (!cs->sort_order)
04655 {
04656 while (j <= tlmpl)
04657 {
04658 int i= plm1;
04659 while (i >= 0 && pattern[i] == text[i + j])
04660 {
04661 i--;
04662 if (i == plm1 - shift)
04663 i-= u;
04664 }
04665 if (i < 0)
04666 return 1;
04667
04668 const int v = plm1 - i;
04669 turboShift = u - v;
04670 bcShift = bmBc[(uint32_t) (unsigned char) text[i + j]] - plm1 + i;
04671 shift = (turboShift > bcShift) ? turboShift : bcShift;
04672 shift = (shift > bmGs[i]) ? shift : bmGs[i];
04673 if (shift == bmGs[i])
04674 u = (pattern_len - shift < v) ? pattern_len - shift : v;
04675 else
04676 {
04677 if (turboShift < bcShift)
04678 shift= max(shift, u + 1);
04679 u = 0;
04680 }
04681 j+= shift;
04682 }
04683 return 0;
04684 }
04685 else
04686 {
04687 while (j <= tlmpl)
04688 {
04689 int i = plm1;
04690 while (i >= 0 && likeconv(cs,pattern[i]) == likeconv(cs,text[i + j]))
04691 {
04692 i--;
04693 if (i == plm1 - shift)
04694 i-= u;
04695 }
04696
04697 if (i < 0)
04698 return 1;
04699
04700 const int v= plm1 - i;
04701 turboShift= u - v;
04702 bcShift= bmBc[(uint32_t) likeconv(cs, text[i + j])] - plm1 + i;
04703 shift= (turboShift > bcShift) ? turboShift : bcShift;
04704 shift= max(shift, bmGs[i]);
04705
04706 if (shift == bmGs[i])
04707 u= (pattern_len - shift < v) ? pattern_len - shift : v;
04708 else
04709 {
04710 if (turboShift < bcShift)
04711 shift= max(shift, u + 1);
04712 u = 0;
04713 }
04714
04715 j+= shift;
04716 }
04717 return 0;
04718 }
04719 }
04720
04721
04738 int64_t Item_cond_xor::val_int()
04739 {
04740 assert(fixed == 1);
04741 List<Item>::iterator li(list.begin());
04742 Item *item;
04743 int result=0;
04744 null_value=0;
04745 while ((item=li++))
04746 {
04747 result^= (item->val_int() != 0);
04748 if (item->null_value)
04749 {
04750 null_value=1;
04751 return 0;
04752 }
04753 }
04754 return (int64_t) result;
04755 }
04756
04783 Item *Item_func_not::neg_transformer(Session *)
04784 {
04785 return args[0];
04786 }
04787
04788
04789 Item *Item_bool_rowready_func2::neg_transformer(Session *)
04790 {
04791 Item *item= negated_item();
04792 return item;
04793 }
04794
04795
04799 Item *Item_func_isnull::neg_transformer(Session *)
04800 {
04801 Item *item= new Item_func_isnotnull(args[0]);
04802 return item;
04803 }
04804
04805
04809 Item *Item_func_isnotnull::neg_transformer(Session *)
04810 {
04811 Item *item= new Item_func_isnull(args[0]);
04812 return item;
04813 }
04814
04815
04816 Item *Item_cond_and::neg_transformer(Session *session)
04817
04818 {
04819 neg_arguments(session);
04820 Item *item= new Item_cond_or(list);
04821 return item;
04822 }
04823
04824
04825 Item *Item_cond_or::neg_transformer(Session *session)
04826
04827 {
04828 neg_arguments(session);
04829 Item *item= new Item_cond_and(list);
04830 return item;
04831 }
04832
04833
04834 Item *Item_func_nop_all::neg_transformer(Session *)
04835 {
04836
04837 Item_func_not_all *new_item= new Item_func_not_all(args[0]);
04838 Item_allany_subselect *allany= (Item_allany_subselect*)args[0];
04839 allany->func= allany->func_creator(false);
04840 allany->all= !allany->all;
04841 allany->upper_item= new_item;
04842 return new_item;
04843 }
04844
04845 Item *Item_func_not_all::neg_transformer(Session *)
04846 {
04847
04848 Item_func_nop_all *new_item= new Item_func_nop_all(args[0]);
04849 Item_allany_subselect *allany= (Item_allany_subselect*)args[0];
04850 allany->all= !allany->all;
04851 allany->func= allany->func_creator(true);
04852 allany->upper_item= new_item;
04853 return new_item;
04854 }
04855
04856 Item *Item_func_eq::negated_item()
04857 {
04858 return new Item_func_ne(args[0], args[1]);
04859 }
04860
04861
04862 Item *Item_func_ne::negated_item()
04863 {
04864 return new Item_func_eq(args[0], args[1]);
04865 }
04866
04867
04868 Item *Item_func_lt::negated_item()
04869 {
04870 return new Item_func_ge(args[0], args[1]);
04871 }
04872
04873
04874 Item *Item_func_ge::negated_item()
04875 {
04876 return new Item_func_lt(args[0], args[1]);
04877 }
04878
04879
04880 Item *Item_func_gt::negated_item()
04881 {
04882 return new Item_func_le(args[0], args[1]);
04883 }
04884
04885
04886 Item *Item_func_le::negated_item()
04887 {
04888 return new Item_func_gt(args[0], args[1]);
04889 }
04890
04894 Item *Item_bool_rowready_func2::negated_item()
04895 {
04896 assert(0);
04897 return 0;
04898 }
04899
04900 Item_equal::Item_equal(Item_field *f1, Item_field *f2)
04901 : item::function::Boolean(), const_item(0), eval_item(0), cond_false(0)
04902 {
04903 const_item_cache= false;
04904 fields.push_back(f1);
04905 fields.push_back(f2);
04906 }
04907
04908 Item_equal::Item_equal(Item *c, Item_field *f)
04909 : item::function::Boolean(), eval_item(0), cond_false(0)
04910 {
04911 const_item_cache= false;
04912 fields.push_back(f);
04913 const_item= c;
04914 }
04915
04916
04917 Item_equal::Item_equal(Item_equal *item_equal)
04918 : item::function::Boolean(), eval_item(0), cond_false(0)
04919 {
04920 const_item_cache= false;
04921 List<Item_field>::iterator li(item_equal->fields.begin());
04922 Item_field *item;
04923 while ((item= li++))
04924 {
04925 fields.push_back(item);
04926 }
04927 const_item= item_equal->const_item;
04928 cond_false= item_equal->cond_false;
04929 }
04930
04931 void Item_equal::add(Item *c)
04932 {
04933 if (cond_false)
04934 return;
04935 if (!const_item)
04936 {
04937 const_item= c;
04938 return;
04939 }
04940 Item_func_eq *func= new Item_func_eq(c, const_item);
04941 func->set_cmp_func();
04942 func->quick_fix_field();
04943 if ((cond_false= !func->val_int()))
04944 const_item_cache= true;
04945 }
04946
04947 void Item_equal::add(Item_field *f)
04948 {
04949 fields.push_back(f);
04950 }
04951
04952 uint32_t Item_equal::members()
04953 {
04954 return fields.size();
04955 }
04956
04957
04971 bool Item_equal::contains(Field *field)
04972 {
04973 List<Item_field>::iterator it(fields.begin());
04974 Item_field *item;
04975 while ((item= it++))
04976 {
04977 if (field->eq(item->field))
04978 return 1;
04979 }
04980 return 0;
04981 }
04982
04983
04995 void Item_equal::merge(Item_equal *item)
04996 {
04997 fields.concat(&item->fields);
04998 Item *c= item->const_item;
04999 if (c)
05000 {
05001
05002
05003
05004
05005
05006 add(c);
05007 }
05008 cond_false|= item->cond_false;
05009 }
05010
05011
05029 void Item_equal::sort(Item_field_cmpfunc cmp, void *arg)
05030 {
05031 bool swap;
05032 List<Item_field>::iterator it(fields.begin());
05033 do
05034 {
05035 Item_field *item1= it++;
05036 Item_field **ref1= it.ref();
05037 Item_field *item2;
05038
05039 swap= false;
05040 while ((item2= it++))
05041 {
05042 Item_field **ref2= it.ref();
05043 if (cmp(item1, item2, arg) < 0)
05044 {
05045 Item_field *item= *ref1;
05046 *ref1= *ref2;
05047 *ref2= item;
05048 swap= true;
05049 }
05050 else
05051 {
05052 item1= item2;
05053 ref1= ref2;
05054 }
05055 }
05056 it= fields.begin();
05057 } while (swap);
05058 }
05059
05060
05071 void Item_equal::update_const()
05072 {
05073 List<Item_field>::iterator it(fields.begin());
05074 Item *item;
05075 while ((item= it++))
05076 {
05077 if (item->const_item())
05078 {
05079 it.remove();
05080 add(item);
05081 }
05082 }
05083 }
05084
05085 bool Item_equal::fix_fields(Session *, Item **)
05086 {
05087 List<Item_field>::iterator li(fields.begin());
05088 Item *item;
05089 not_null_tables_cache= used_tables_cache= 0;
05090 const_item_cache= false;
05091 while ((item= li++))
05092 {
05093 table_map tmp_table_map;
05094 used_tables_cache|= item->used_tables();
05095 tmp_table_map= item->not_null_tables();
05096 not_null_tables_cache|= tmp_table_map;
05097 if (item->maybe_null)
05098 maybe_null=1;
05099 }
05100 fix_length_and_dec();
05101 fixed= 1;
05102 return 0;
05103 }
05104
05105 void Item_equal::update_used_tables()
05106 {
05107 List<Item_field>::iterator li(fields.begin());
05108 Item *item;
05109 not_null_tables_cache= used_tables_cache= 0;
05110 if ((const_item_cache= cond_false))
05111 return;
05112 while ((item=li++))
05113 {
05114 item->update_used_tables();
05115 used_tables_cache|= item->used_tables();
05116 const_item_cache&= item->const_item();
05117 }
05118 }
05119
05120 int64_t Item_equal::val_int()
05121 {
05122 Item_field *item_field;
05123 if (cond_false)
05124 return 0;
05125 List<Item_field>::iterator it(fields.begin());
05126 Item *item= const_item ? const_item : it++;
05127 eval_item->store_value(item);
05128 if ((null_value= item->null_value))
05129 return 0;
05130 while ((item_field= it++))
05131 {
05132
05133 if (item_field->field->getTable()->const_table)
05134 {
05135 if (eval_item->cmp(item_field) || (null_value= item_field->null_value))
05136 return 0;
05137 }
05138 }
05139 return 1;
05140 }
05141
05142 void Item_equal::fix_length_and_dec()
05143 {
05144 Item *item= get_first();
05145 eval_item= cmp_item::get_comparator(item->result_type(),
05146 item->collation.collation);
05147 }
05148
05149 bool Item_equal::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
05150 {
05151 List<Item_field>::iterator it(fields.begin());
05152 Item *item;
05153 while ((item= it++))
05154 {
05155 if (item->walk(processor, walk_subquery, arg))
05156 return 1;
05157 }
05158 return Item_func::walk(processor, walk_subquery, arg);
05159 }
05160
05161 Item *Item_equal::transform(Item_transformer transformer, unsigned char *arg)
05162 {
05163 List<Item_field>::iterator it(fields.begin());
05164 Item *item;
05165 while ((item= it++))
05166 {
05167 Item *new_item= item->transform(transformer, arg);
05168 if (!new_item)
05169 return 0;
05170 *(Item **)it.ref()= new_item;
05171 }
05172 return Item_func::transform(transformer, arg);
05173 }
05174
05175 void Item_equal::print(String *str)
05176 {
05177 str->append(func_name());
05178 str->append('(');
05179 List<Item_field>::iterator it(fields.begin());
05180 Item *item;
05181 if (const_item)
05182 const_item->print(str);
05183 else
05184 {
05185 item= it++;
05186 item->print(str);
05187 }
05188 while ((item= it++))
05189 {
05190 str->append(',');
05191 str->append(' ');
05192 item->print(str);
05193 }
05194 str->append(')');
05195 }
05196
05197 cmp_item_datetime::cmp_item_datetime(Item *warn_item_arg) :
05198 session(current_session),
05199 warn_item(warn_item_arg),
05200 lval_cache(0)
05201 {}
05202
05203 }