24 #include <drizzled/common.h>
25 #include <drizzled/comp_creator.h>
26 #include <drizzled/function/math/int.h>
27 #include <drizzled/function/numhybrid.h>
28 #include <drizzled/item/decimal.h>
29 #include <drizzled/item/float.h>
30 #include <drizzled/item/function/boolean.h>
31 #include <drizzled/item/int.h>
32 #include <drizzled/item/row.h>
33 #include <drizzled/item/string.h>
34 #include <drizzled/item/sum.h>
35 #include <drizzled/qsort_cmp.h>
39 extern Item_result item_cmp_type(Item_result a,Item_result b);
41 typedef int (Arg_comparator::*arg_cmp_func)();
43 typedef int (*Item_field_cmpfunc)(Item_field *f1, Item_field *f2,
void *arg);
45 int64_t get_datetime_value(Session *session,
60 enum_field_types a_type, b_type;
61 Item *a_cache, *b_cache;
63 enum enum_date_cmp_type { CMP_DATE_DFLT= 0, CMP_DATE_WITH_DATE,
64 CMP_DATE_WITH_STR, CMP_STR_WITH_DATE };
65 int64_t (*get_value_func)(
Session *session,
Item ***item_arg,
Item **cache_arg,
66 Item *warn_item,
bool *is_null);
77 return set_compare_func(owner_arg, item_cmp_type((*a)->result_type(),
78 (*b)->result_type()));
87 return set_cmp_func(owner_arg, a1, a2,
88 item_cmp_type((*a1)->result_type(),
89 (*a2)->result_type()));
91 inline int compare() {
return (this->*func)(); }
96 int compare_decimal();
97 int compare_int_signed();
103 int compare_e_binary_string();
104 int compare_e_real();
105 int compare_e_decimal();
109 int compare_real_fixed();
110 int compare_e_real_fixed();
111 int compare_datetime();
113 static enum enum_date_cmp_type can_compare_as_dates(
Item *a,
Item *b,
114 int64_t *const_val_arg);
116 void set_datetime_cmp_func(
Item **a1,
Item **b1);
117 static arg_cmp_func comparator_matrix [5][2];
133 virtual void fix_length_and_dec();
162 virtual const char* func_name()
const {
return "istrue"; }
174 virtual const char* func_name()
const {
return "isnottrue"; }
186 virtual const char* func_name()
const {
return "isfalse"; }
198 virtual const char* func_name()
const {
return "isnotfalse"; }
202 #define UNKNOWN ((bool)-1)
231 bool result_for_null_param;
235 save_cache(0), result_for_null_param(UNKNOWN)
242 const char *func_name()
const {
return "<in_optimizer>"; }
244 void keep_top_level_cache();
245 Item *
transform(Item_transformer transformer,
unsigned char *arg);
252 virtual const char* symbol(
bool invert)
const {
return invert?
"<>" :
"="; }
253 virtual bool eqne_op()
const {
return 1; }
254 virtual bool l_op()
const {
return 0; }
262 virtual const char* symbol(
bool invert)
const {
return invert?
"=" :
"<>"; }
263 virtual bool eqne_op()
const {
return 1; }
264 virtual bool l_op()
const {
return 0; }
272 virtual const char* symbol(
bool invert)
const {
return invert?
"<=" :
">"; }
273 virtual bool eqne_op()
const {
return 0; }
274 virtual bool l_op()
const {
return 0; }
282 virtual const char* symbol(
bool invert)
const {
return invert?
">=" :
"<"; }
283 virtual bool eqne_op()
const {
return 0; }
284 virtual bool l_op()
const {
return 1; }
292 virtual const char* symbol(
bool invert)
const {
return invert?
"<" :
">="; }
293 virtual bool eqne_op()
const {
return 0; }
294 virtual bool l_op()
const {
return 0; }
302 virtual const char* symbol(
bool invert)
const {
return invert?
">" :
"<="; }
303 virtual bool eqne_op()
const {
return 0; }
304 virtual bool l_op()
const {
return 1; }
312 String tmp_value1,tmp_value2;
317 :
Item_int_func(a,b), cmp(tmp_arg, tmp_arg+1), abort_on_null(
false) {}
318 void fix_length_and_dec();
321 cmp.set_cmp_func(
this, tmp_arg, tmp_arg+1);
323 optimize_type select_optimize()
const {
return OPTIMIZE_OP; }
324 virtual enum Functype rev_functype()
const {
return UNKNOWN_FUNC; }
325 bool have_rev_func()
const {
return rev_functype() != UNKNOWN_FUNC; }
329 Item_func::print_op(str);
333 bool is_bool_func() {
return 1; }
334 const charset_info_st *compare_collation() {
return cmp.cmp_collation.collation; }
335 uint32_t decimal_precision()
const {
return 1; }
350 bool subst_argument_checker(
unsigned char **)
359 enum Functype functype()
const {
return NOT_FUNC; }
360 const char *func_name()
const {
return "not"; }
400 enum Functype functype()
const {
return TRIG_COND_FUNC; };
401 const char *func_name()
const {
return "trigcond"; };
403 bool *get_trig_var() {
return trig_var; }
419 :
Item_func_not(a), test_sum_item(0), test_sub_item(0), abort_on_null(0),
423 bool top_level() {
return abort_on_null; }
425 enum Functype functype()
const {
return NOT_ALL_FUNC; }
426 const char *func_name()
const {
return "<not>"; }
427 virtual void print(String *str);
428 void set_sum_test(Item_sum_hybrid *item) { test_sum_item= item; };
429 void set_sub_test(Item_maxmin_subselect *item) { test_sub_item= item; };
430 bool empty_underlying_subquery();
441 const char *func_name()
const {
return "<nop>"; }
451 enum Functype functype()
const {
return EQ_FUNC; }
452 enum Functype rev_functype()
const {
return EQ_FUNC; }
453 cond_result eq_cmp_result()
const {
return COND_TRUE; }
454 const char *func_name()
const {
return "="; }
465 enum Functype functype()
const {
return EQUAL_FUNC; }
466 enum Functype rev_functype()
const {
return EQUAL_FUNC; }
467 cond_result eq_cmp_result()
const {
return COND_TRUE; }
468 const char *func_name()
const {
return "<=>"; }
469 Item *neg_transformer(Session *) {
return 0; }
478 enum Functype functype()
const {
return GE_FUNC; }
479 enum Functype rev_functype()
const {
return LE_FUNC; }
480 cond_result eq_cmp_result()
const {
return COND_TRUE; }
481 const char *func_name()
const {
return ">="; }
491 enum Functype functype()
const {
return GT_FUNC; }
492 enum Functype rev_functype()
const {
return LT_FUNC; }
493 cond_result eq_cmp_result()
const {
return COND_FALSE; }
494 const char *func_name()
const {
return ">"; }
504 enum Functype functype()
const {
return LE_FUNC; }
505 enum Functype rev_functype()
const {
return GE_FUNC; }
506 cond_result eq_cmp_result()
const {
return COND_TRUE; }
507 const char *func_name()
const {
return "<="; }
517 enum Functype functype()
const {
return LT_FUNC; }
518 enum Functype rev_functype()
const {
return GT_FUNC; }
519 cond_result eq_cmp_result()
const {
return COND_FALSE; }
520 const char *func_name()
const {
return "<"; }
530 enum Functype functype()
const {
return NE_FUNC; }
531 cond_result eq_cmp_result()
const {
return COND_FALSE; }
532 optimize_type select_optimize()
const {
return OPTIMIZE_KEY; }
533 const char *func_name()
const {
return "<>"; }
558 inline void negate() { negated= !negated; }
565 bool eq(
const Item *item,
bool binary_cmp)
const;
566 bool subst_argument_checker(
unsigned char **)
575 Item_result cmp_type;
576 String value0,value1,value2;
578 bool compare_as_dates;
584 optimize_type select_optimize()
const {
return OPTIMIZE_KEY; }
585 enum Functype functype()
const {
return BETWEEN; }
586 const char *func_name()
const {
return "between"; }
588 void fix_length_and_dec();
590 bool is_bool_func() {
return 1; }
591 const charset_info_st *compare_collation() {
return cmp_collation.collation; }
592 uint32_t decimal_precision()
const {
return 1; }
601 optimize_type select_optimize()
const {
return OPTIMIZE_NONE; }
602 const char *func_name()
const {
return "strcmp"; }
621 bool use_decimal_comparison;
630 void fix_length_and_dec();
631 const char *func_name()
const {
return "interval"; }
632 uint32_t decimal_precision()
const {
return 2; }
639 enum_field_types cached_field_type;
647 void fix_length_and_dec();
648 void find_num_type() {}
649 enum Item_result result_type ()
const {
return hybrid_type; }
650 const char *func_name()
const {
return "coalesce"; }
652 enum_field_types field_type()
const {
return cached_field_type; }
659 bool field_type_defined;
666 enum_field_types field_type()
const;
667 void fix_length_and_dec();
668 const char *func_name()
const {
return "ifnull"; }
669 Field *tmp_table_field()
671 return Item_func::tmp_table_field();
674 uint32_t decimal_precision()
const;
680 Item_result cached_result_type;
681 enum_field_types cached_field_type;
686 cached_result_type(INT_RESULT)
693 Item_result result_type ()
const {
return cached_result_type; }
694 enum_field_types field_type()
const {
return cached_field_type; }
696 void fix_length_and_dec();
697 uint32_t decimal_precision()
const;
698 const char *func_name()
const {
return "if"; }
704 enum Item_result cached_result_type;
713 enum Item_result result_type ()
const {
return cached_result_type; }
714 void fix_length_and_dec();
715 uint32_t decimal_precision()
const {
return args[0]->decimal_precision(); }
716 const char *func_name()
const {
return "nullif"; }
743 in_vector(uint32_t elements,uint32_t element_length,qsort2_cmp cmp_func,
745 :base((
char*) memory::sql_calloc(elements*element_length)),
746 size(element_length), compare(cmp_func), collation(cmp_coll),
747 count(elements), used_count(elements) {}
749 virtual void set(uint32_t pos,
Item *item)=0;
750 virtual unsigned char *get_value(
Item *item)=0;
752 int find(
Item *item);
762 virtual Item* create_item() {
return NULL; }
772 virtual void value_to_item(uint32_t,
Item *) { }
775 bool compare_elems(uint32_t pos1, uint32_t pos2)
777 return test(compare(collation, base + pos1*size, base + pos2*size));
779 virtual Item_result result_type()= 0;
784 char buff[STRING_BUFFER_USUAL_SIZE];
789 void set(uint32_t pos,
Item *item);
790 unsigned char *get_value(
Item *item);
795 void value_to_item(uint32_t pos,
Item *item)
801 Item_result result_type() {
return STRING_RESULT; }
815 int64_t unsigned_flag;
819 void set(uint32_t pos,
Item *item);
820 unsigned char *get_value(
Item *item);
830 void value_to_item(uint32_t pos,
Item *item)
832 ((
Item_int*) item)->value= ((packed_int64_t*) base)[pos].val;
833 ((
Item_int*) item)->unsigned_flag= (bool)
834 ((packed_int64_t*) base)[pos].unsigned_flag;
836 Item_result result_type() {
return INT_RESULT; }
838 friend int cmp_int64_t(
void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
859 void set(uint32_t pos,
Item *item);
860 unsigned char *get_value(
Item *item);
861 friend int cmp_int64_t(
void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
870 void set(uint32_t pos,
Item *item);
871 unsigned char *get_value(
Item *item);
876 void value_to_item(uint32_t pos,
Item *item)
878 ((
Item_float*)item)->value= ((
double*) base)[pos];
880 Item_result result_type() {
return REAL_RESULT; }
889 void set(uint32_t pos,
Item *item);
890 unsigned char *get_value(
Item *item);
895 void value_to_item(uint32_t pos,
Item *item)
899 item_dec->set_decimal_value(dec);
901 Item_result result_type() {
return DECIMAL_RESULT; }
917 cmp_charset= &my_charset_bin;
921 virtual void store_value(
Item *item)= 0;
922 virtual int cmp(
Item *item)= 0;
924 virtual int compare(
cmp_item *item)= 0;
927 virtual void store_value_by_template(
cmp_item *,
Item *item)
940 void set_charset(
const charset_info_st *
const cs) { cmp_charset= cs; }
948 char value_buff[STRING_BUFFER_USUAL_SIZE];
955 value(value_buff,
sizeof(value_buff), cs) {}
956 void store_value(
Item *item)
958 value_res= item->
val_str(&value);
962 char buff[STRING_BUFFER_USUAL_SIZE];
963 String tmp(buff,
sizeof(buff), cmp_charset), *res;
965 return (value_res ? (res ? sortcmp(value_res, res, cmp_charset) : 1) :
971 return sortcmp(value_res, l_cmp->value_res, cmp_charset);
977 value.set_quick(value_buff,
sizeof(value_buff), cs);
985 void store_value(
Item *item)
991 return value != arg->
val_int();
996 return (value < l_cmp->value) ? -1 : ((value == l_cmp->value) ? 0 : 1);
1020 void store_value(
Item *item);
1030 void store_value(
Item *item)
1041 return (value < l_cmp->value)? -1 : ((value == l_cmp->value) ? 0 : 1);
1051 void store_value(
Item *item);
1070 void store_value(
Item *item)
1072 value_res= item->
val_str(&value);
1083 return sortcmp(value_res, l_cmp->value_res, cmp_charset);
1112 int first_expr_num, else_expr_num;
1113 enum Item_result cached_result_type, left_result_type;
1116 Item_result cmp_type;
1118 enum_field_types cached_field_type;
1119 cmp_item *cmp_items[DECIMAL_RESULT+1];
1123 :
Item_func(), first_expr_num(-1), else_expr_num(-1),
1124 cached_result_type(INT_RESULT), left_result_type(INT_RESULT), case_item(0)
1126 ncases= list.size();
1129 first_expr_num= list.size();
1130 list.push_back(first_expr_arg);
1134 else_expr_num= list.size();
1135 list.push_back(else_expr_arg);
1137 set_arguments(list);
1138 memset(&cmp_items, 0,
sizeof(cmp_items));
1145 void fix_length_and_dec();
1146 uint32_t decimal_precision()
const;
1148 enum Item_result result_type ()
const {
return cached_result_type; }
1149 enum_field_types field_type()
const {
return cached_field_type; }
1150 const char *func_name()
const {
return "case"; }
1151 virtual void print(String *str);
1153 const charset_info_st *compare_collation() {
return cmp_collation.collation; }
1155 void agg_str_lengths(
Item *arg);
1156 void agg_num_lengths(
Item *arg);
1186 bool arg_types_compatible;
1187 Item_result left_result_type;
1193 arg_types_compatible(
false)
1195 memset(&cmp_items, 0,
sizeof(cmp_items));
1196 allowed_arg_cols= 0;
1200 void fix_length_and_dec();
1201 uint32_t decimal_precision()
const {
return 1; }
1204 Item_int_func::cleanup();
1207 for (
int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
1209 delete cmp_items[i];
1214 optimize_type select_optimize()
const
1215 {
return OPTIMIZE_KEY; }
1217 enum Functype functype()
const {
return IN_FUNC; }
1218 const char *func_name()
const {
return " IN "; }
1219 bool nulls_in_row();
1220 bool is_bool_func() {
return 1; }
1221 const charset_info_st *compare_collation() {
return cmp_collation.collation; }
1231 void store_value(
Item *item);
1232 inline void alloc_comparators();
1237 friend void Item_func_in::fix_length_and_dec();
1247 void set(uint32_t pos,
Item *item);
1248 unsigned char *get_value(
Item *item);
1249 friend void Item_func_in::fix_length_and_dec();
1250 Item_result result_type() {
return ROW_RESULT; }
1258 int64_t cached_value;
1262 enum Functype functype()
const {
return ISNULL_FUNC; }
1263 void fix_length_and_dec()
1266 update_used_tables();
1268 const char *func_name()
const {
return "isnull"; }
1270 virtual void update_used_tables()
1274 used_tables_cache= 0;
1275 const_item_cache= 1;
1276 cached_value= (int64_t) 0;
1280 args[0]->update_used_tables();
1281 if ((const_item_cache= !(used_tables_cache= args[0]->
used_tables())) &&
1285 cached_value= (int64_t) args[0]->
is_null();
1290 optimize_type select_optimize()
const {
return OPTIMIZE_NULL; }
1292 const charset_info_st *compare_collation() {
return args[0]->collation.collation; }
1308 enum Functype functype()
const {
return ISNOTNULLTEST_FUNC; }
1310 const char *func_name()
const {
return "<is_not_null_test>"; }
1316 {
return used_tables_cache | RAND_TABLE_BIT; }
1326 enum Functype functype()
const {
return ISNOTNULL_FUNC; }
1327 void fix_length_and_dec()
1331 const char *func_name()
const {
return "isnotnull"; }
1332 optimize_type select_optimize()
const {
return OPTIMIZE_NULL; }
1334 {
return abort_on_null ? not_null_tables_cache : 0; }
1337 const charset_info_st *compare_collation() {
return args[0]->collation.collation; }
1346 const char* pattern;
1357 enum { alphabet_size = 256 };
1361 bool escape_used_in_parsing;
1369 :
Item_bool_func2(a,b), canDoTurboBM(
false), pattern(0), pattern_len(0),
1370 bmGs(0), bmBc(0), escape_item(escape_arg),
1371 escape_used_in_parsing(escape_used), escape(NULL) {}
1373 enum Functype functype()
const {
return LIKE_FUNC; }
1375 cond_result eq_cmp_result()
const {
return COND_TRUE; }
1376 const char *func_name()
const {
return "like"; }
1387 table_map and_tables_cache;
1391 using Item::split_sum_func;
1395 { const_item_cache=0; }
1405 void add(
Item *item) { list.push_back(item); }
1406 void add_at_head(
Item *item) { list.push_front(item); }
1407 void add_at_head(
List<Item> *nlist) { list.prepand(nlist); }
1411 enum Type type()
const {
return COND_ITEM; }
1412 List<Item>* argument_list() {
return &list; }
1414 void update_used_tables();
1421 bool walk(Item_processor processor,
bool walk_subquery,
unsigned char *arg);
1422 Item *
transform(Item_transformer transformer,
unsigned char *arg);
1423 void traverse_cond(Cond_traverser,
void *arg, traverse_order order);
1424 void neg_arguments(
Session *session);
1425 enum_field_types field_type()
const {
return DRIZZLE_TYPE_LONGLONG; }
1426 bool subst_argument_checker(
unsigned char **)
1428 Item *
compile(Item_analyzer analyzer,
unsigned char **arg_p,
1429 Item_transformer transformer,
unsigned char *arg_t);
1523 return fields.begin();
1534 Item_field* get_first() {
return &fields.front(); }
1537 enum Functype functype()
const {
return MULT_EQUAL_FUNC; }
1539 const char *func_name()
const {
return "multiple equal"; }
1540 optimize_type select_optimize()
const {
return OPTIMIZE_EQUAL; }
1541 void sort(Item_field_cmpfunc cmp,
void *arg);
1542 void fix_length_and_dec();
1544 void update_used_tables();
1545 bool walk(Item_processor processor,
bool walk_subquery,
unsigned char *arg);
1546 Item *
transform(Item_transformer transformer,
unsigned char *arg);
1549 {
return fields.front().collation.collation; }
1561 uint32_t max_members;
1584 enum Functype functype()
const {
return COND_AND_FUNC; }
1586 const char *func_name()
const {
return "and"; }
1588 {
return abort_on_null ? not_null_tables_cache: and_tables_cache; }
1593 item->copy_andor_arguments(session,
this);
1596 Item *neg_transformer(Session *session);
1599 inline bool is_cond_and(Item *item)
1601 if (item->type() != Item::COND_ITEM)
1604 Item_cond *cond_item= (Item_cond*) item;
1605 return (cond_item->functype() == Item_func::COND_AND_FUNC);
1615 enum Functype functype()
const {
return COND_OR_FUNC; }
1617 const char *func_name()
const {
return "or"; }
1623 item->copy_andor_arguments(session,
this);
1626 Item *neg_transformer(Session *session);
1629 inline bool is_cond_or(Item *item)
1631 if (item->type() != Item::COND_ITEM)
1634 Item_cond *cond_item= (Item_cond*) item;
1635 return (cond_item->functype() == Item_func::COND_OR_FUNC);
1648 enum Functype functype()
const {
return COND_XOR_FUNC; }
1650 enum Type type()
const {
return FUNC_ITEM; }
1652 const char *func_name()
const {
return "xor"; }
1661 inline Item *and_conds(Item *a, Item *b)
1665 return new Item_cond_and(a, b);
int compare_binary_string()
virtual void print(String *str)
virtual void print(String *str)
Item * neg_transformer(Session *session)
virtual void print(String *str)
table_map not_null_tables() const
int64_t int_op()
Performs the operation that this functions implements when the result type is INT.
bool fix_fields(Session *, Item **)
virtual void print(String *str)
Item * find_item(String *str)
bool fix_fields(Session *, Item **)
int compare_int_unsigned()
type::Decimal * decimal_op(type::Decimal *)
Performs the operation that this functions implements when the result type is DECIMAL.
void merge(Item_equal *item)
virtual int64_t val_int()=0
int64_t int_op()
Performs the operation that this functions implements when the result type is INT.
table_map not_null_tables() const
table_map not_null_tables() const
table_map used_tables() const
virtual Item * negated_item()
bool contains(Field *field)
void update_used_tables()
TODO: Rename this file - func.h is stupid.
bool eq(const Item *item, bool binary_cmp) const
bool turboBM_matches(const char *text, int text_len) const
String * str_op(String *)
void fix_length_and_dec()
int compare_int_signed_unsigned()
virtual void print(String *str)
table_map not_null_tables() const
int compare_int_unsigned_signed()
void fix_after_pullout(Select_Lex *new_parent, Item **ref)
String * val_str(String *str)
Item * transform(Item_transformer transformer, unsigned char *arg)
table_map used_tables() const
virtual bool const_item() const
double real_op()
Performs the operation that this functions implements when the result type is REAL.
enum_field_types agg_field_type(Item **items, uint32_t nitems)
Aggregates field types from the array of items.
String * str_op(String *str)
Item * compile(Item_analyzer analyzer, unsigned char **arg_p, Item_transformer transformer, unsigned char *arg_t)
Item * neg_transformer(Session *session)
String * val_str(String *)
void sort(Item_field_cmpfunc cmp, void *arg)
virtual double val_real()=0
virtual void print(String *str)
type::Decimal * val_decimal(type::Decimal *)
virtual void print(String *str)
void turboBM_compute_bad_character_shifts()
table_map not_null_tables() const
String * val_str(String *str)
virtual void print(String *str)
Item * neg_transformer(Session *session)
table_map used_tables() const
table_map not_null_tables() const
virtual void top_level_item()
Item * neg_transformer(Session *session)
virtual String * val_str(String *str)=0
table_map not_null_tables() const
int compare_e_int_diff_signedness()
virtual void print(String *str)
Item * transform(Item_transformer transformer, unsigned char *arg)
virtual table_map used_tables() const
virtual void print(String *str)
void split_sum_func(Session *session, Item **ref_pointer_array, List< Item > &fields)
void turboBM_compute_good_suffix_shifts(int *suff)
bool fix_fields(Session *, Item **)
virtual void print(String *str)
table_map used_tables() const
type::Decimal * val_decimal(type::Decimal *)
Item * and_expressions(Item *a, Item *b, Item **org_item)
void turboBM_compute_suffixes(int *suff)
double real_op()
Performs the operation that this functions implements when the result type is REAL.
Item * neg_transformer(Session *session)
type::Decimal * decimal_op(type::Decimal *)
Performs the operation that this functions implements when the result type is DECIMAL.
virtual void print(String *str)
type::Decimal * val_decimal(type::Decimal *)
optimize_type select_optimize() const
virtual int64_t val_int()
table_map not_null_tables() const
Item * transform(Item_transformer transformer, unsigned char *arg)
virtual void print(String *str)