1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1
/* Copyright (C) 2000-2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21
17
/* compare and test functions */
19
#ifdef USE_PRAGMA_INTERFACE
20
#pragma interface /* gcc class implementation */
24
23
extern Item_result item_cmp_type(Item_result a,Item_result b);
25
24
class Item_bool_func2;
109
108
Item_bool_func(THD *thd, Item_bool_func *item) :Item_int_func(thd, item) {}
110
109
bool is_bool_func() { return 1; }
111
110
void fix_length_and_dec() { decimals=0; max_length=1; }
112
uint32_t decimal_precision() const { return 1; }
111
uint decimal_precision() const { return 1; }
228
227
FALSE - result is FALSE
229
228
TRUE - result is NULL
231
bool result_for_null_param;
230
my_bool result_for_null_param;
233
232
Item_in_optimizer(Item *a, Item_in_subselect *b):
234
Item_bool_func(a, reinterpret_cast<Item *>(b)), cache(0),
233
Item_bool_func(a, my_reinterpret_cast(Item *)(b)), cache(0),
235
234
save_cache(0), result_for_null_param(UNKNOWN)
236
235
{ with_subselect= true; }
237
236
bool fix_fields(THD *, Item **);
242
241
const char *func_name() const { return "<in_optimizer>"; }
243
242
Item_cache **get_cache() { return &cache; }
244
243
void keep_top_level_cache();
245
Item *transform(Item_transformer transformer, unsigned char *arg);
244
Item *transform(Item_transformer transformer, uchar *arg);
248
247
class Comp_creator
349
348
bool is_null() { return test(args[0]->is_null() || args[1]->is_null()); }
350
349
bool is_bool_func() { return 1; }
351
const CHARSET_INFO *compare_collation() { return cmp.cmp_collation.collation; }
352
uint32_t decimal_precision() const { return 1; }
350
CHARSET_INFO *compare_collation() { return cmp.cmp_collation.collation; }
351
uint decimal_precision() const { return 1; }
353
352
void top_level_item() { abort_on_null= true; }
355
354
friend class Arg_comparator;
365
364
Item *neg_transformer(THD *thd);
366
365
virtual Item *negated_item();
367
bool subst_argument_checker(unsigned char **arg __attribute__((unused)))
366
bool subst_argument_checker(uchar **arg __attribute__((__unused__)))
485
484
enum Functype rev_functype() const { return EQUAL_FUNC; }
486
485
cond_result eq_cmp_result() const { return COND_TRUE; }
487
486
const char *func_name() const { return "<=>"; }
488
Item *neg_transformer(THD *thd __attribute__((unused))) { return 0; }
487
Item *neg_transformer(THD *thd __attribute__((__unused__))) { return 0; }
577
576
inline void negate() { negated= !negated; }
578
577
inline void top_level_item() { pred_level= 1; }
579
Item *neg_transformer(THD *thd __attribute__((unused)))
578
Item *neg_transformer(THD *thd __attribute__((__unused__)))
581
580
negated= !negated;
584
583
bool eq(const Item *item, bool binary_cmp) const;
585
bool subst_argument_checker(unsigned char **arg __attribute__((unused)))
584
bool subst_argument_checker(uchar **arg __attribute__((__unused__)))
607
606
void fix_length_and_dec();
608
607
virtual void print(String *str, enum_query_type query_type);
609
608
bool is_bool_func() { return 1; }
610
const CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
611
uint32_t decimal_precision() const { return 1; }
609
CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
610
uint decimal_precision() const { return 1; }
637
636
class Item_func_interval :public Item_int_func
640
bool use_decimal_comparison;
639
my_bool use_decimal_comparison;
641
640
interval_range *intervals;
643
642
Item_func_interval(Item_row *a)
685
684
enum_field_types field_type() const;
686
685
void fix_length_and_dec();
687
686
const char *func_name() const { return "ifnull"; }
688
Field *tmp_table_field(Table *table);
689
uint32_t decimal_precision() const;
687
Field *tmp_table_field(TABLE *table);
688
uint decimal_precision() const;
706
705
enum_field_types field_type() const { return cached_field_type; }
707
706
bool fix_fields(THD *, Item **);
708
707
void fix_length_and_dec();
709
uint32_t decimal_precision() const;
708
uint decimal_precision() const;
710
709
const char *func_name() const { return "if"; }
724
723
my_decimal *val_decimal(my_decimal *);
725
724
enum Item_result result_type () const { return cached_result_type; }
726
725
void fix_length_and_dec();
727
uint32_t decimal_precision() const { return args[0]->decimal_precision(); }
726
uint decimal_precision() const { return args[0]->decimal_precision(); }
728
727
const char *func_name() const { return "nullif"; }
730
729
virtual inline void print(String *str, enum_query_type query_type)
750
749
qsort2_cmp compare;
751
const CHARSET_INFO *collation;
750
CHARSET_INFO *collation;
755
in_vector(uint32_t elements,uint32_t element_length,qsort2_cmp cmp_func,
756
const CHARSET_INFO * const cmp_coll)
754
in_vector(uint elements,uint element_length,qsort2_cmp cmp_func,
755
CHARSET_INFO *cmp_coll)
757
756
:base((char*) sql_calloc(elements*element_length)),
758
757
size(element_length), compare(cmp_func), collation(cmp_coll),
759
758
count(elements), used_count(elements) {}
760
759
virtual ~in_vector() {}
761
virtual void set(uint32_t pos,Item *item)=0;
762
virtual unsigned char *get_value(Item *item)=0;
760
virtual void set(uint pos,Item *item)=0;
761
virtual uchar *get_value(Item *item)=0;
765
my_qsort2(base,used_count,size,compare, (void *) collation);
764
my_qsort2(base,used_count,size,compare,collation);
767
766
int find(Item *item);
784
783
item Constant item to store value into. The item must be of the same
785
784
type that create_item() returns.
787
virtual void value_to_item(uint32_t pos __attribute__((unused)),
788
Item *item __attribute__((unused))) { }
786
virtual void value_to_item(uint pos __attribute__((__unused__)),
787
Item *item __attribute__((__unused__))) { }
790
789
/* Compare values number pos1 and pos2 for equality */
791
bool compare_elems(uint32_t pos1, uint32_t pos2)
790
bool compare_elems(uint pos1, uint pos2)
793
792
return test(compare(collation, base + pos1*size, base + pos2*size));
800
799
char buff[STRING_BUFFER_USUAL_SIZE];
803
in_string(uint32_t elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs);
802
in_string(uint elements,qsort2_cmp cmp_func, CHARSET_INFO *cs);
805
void set(uint32_t pos,Item *item);
806
unsigned char *get_value(Item *item);
804
void set(uint pos,Item *item);
805
uchar *get_value(Item *item);
807
806
Item* create_item()
809
808
return new Item_string(collation);
811
void value_to_item(uint32_t pos, Item *item)
810
void value_to_item(uint pos, Item *item)
813
812
String *str=((String*) base)+pos;
814
813
Item_string *to= (Item_string*)item;
831
830
int64_t unsigned_flag; // Use int64_t, not bool, to preserve alignment
834
in_int64_t(uint32_t elements);
835
void set(uint32_t pos,Item *item);
836
unsigned char *get_value(Item *item);
833
in_int64_t(uint elements);
834
void set(uint pos,Item *item);
835
uchar *get_value(Item *item);
838
837
Item* create_item()
844
843
return new Item_int((int64_t)0);
846
void value_to_item(uint32_t pos, Item *item)
845
void value_to_item(uint pos, Item *item)
848
847
((Item_int*) item)->value= ((packed_int64_t*) base)[pos].val;
849
((Item_int*) item)->unsigned_flag= (bool)
848
((Item_int*) item)->unsigned_flag= (my_bool)
850
849
((packed_int64_t*) base)[pos].unsigned_flag;
852
851
Item_result result_type() { return INT_RESULT; }
870
869
/* Cache for the left item. */
871
870
Item *lval_cache;
873
in_datetime(Item *warn_item_arg, uint32_t elements)
872
in_datetime(Item *warn_item_arg, uint elements)
874
873
:in_int64_t(elements), thd(current_thd), warn_item(warn_item_arg),
875
874
lval_cache(0) {};
876
void set(uint32_t pos,Item *item);
877
unsigned char *get_value(Item *item);
875
void set(uint pos,Item *item);
876
uchar *get_value(Item *item);
878
877
friend int cmp_int64_t(void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
886
in_double(uint32_t elements);
887
void set(uint32_t pos,Item *item);
888
unsigned char *get_value(Item *item);
885
in_double(uint elements);
886
void set(uint pos,Item *item);
887
uchar *get_value(Item *item);
889
888
Item *create_item()
891
890
return new Item_float(0.0, 0);
893
void value_to_item(uint32_t pos, Item *item)
892
void value_to_item(uint pos, Item *item)
895
894
((Item_float*)item)->value= ((double*) base)[pos];
905
in_decimal(uint32_t elements);
906
void set(uint32_t pos, Item *item);
907
unsigned char *get_value(Item *item);
904
in_decimal(uint elements);
905
void set(uint pos, Item *item);
906
uchar *get_value(Item *item);
908
907
Item *create_item()
910
909
return new Item_decimal(0, false);
912
void value_to_item(uint32_t pos, Item *item)
911
void value_to_item(uint pos, Item *item)
914
913
my_decimal *dec= ((my_decimal *)base) + pos;
915
914
Item_decimal *item_dec= (Item_decimal*)item;
927
926
class cmp_item :public Sql_alloc
930
const CHARSET_INFO *cmp_charset;
929
CHARSET_INFO *cmp_charset;
931
930
cmp_item() { cmp_charset= &my_charset_bin; }
932
931
virtual ~cmp_item() {}
933
932
virtual void store_value(Item *item)= 0;
934
933
virtual int cmp(Item *item)= 0;
935
934
// for optimized IN with row
936
935
virtual int compare(cmp_item *item)= 0;
937
static cmp_item* get_comparator(Item_result type, const CHARSET_INFO * const cs);
936
static cmp_item* get_comparator(Item_result type, CHARSET_INFO *cs);
938
937
virtual cmp_item *make_same()= 0;
939
virtual void store_value_by_template(cmp_item *tmpl __attribute__((unused)),
938
virtual void store_value_by_template(cmp_item *tmpl __attribute__((__unused__)),
942
941
store_value(item);
949
948
String *value_res;
951
950
cmp_item_string () {}
952
cmp_item_string (const CHARSET_INFO * const cs) { cmp_charset= cs; }
953
void set_charset(const CHARSET_INFO * const cs) { cmp_charset= cs; }
951
cmp_item_string (CHARSET_INFO *cs) { cmp_charset= cs; }
952
void set_charset(CHARSET_INFO *cs) { cmp_charset= cs; }
954
953
friend class cmp_item_sort_string;
955
954
friend class cmp_item_sort_string_in_static;
964
963
cmp_item_sort_string():
965
964
cmp_item_string() {}
966
cmp_item_sort_string(const CHARSET_INFO * const cs):
965
cmp_item_sort_string(CHARSET_INFO *cs):
967
966
cmp_item_string(cs),
968
967
value(value_buff, sizeof(value_buff), cs) {}
969
968
void store_value(Item *item)
984
983
return sortcmp(value_res, l_cmp->value_res, cmp_charset);
986
985
cmp_item *make_same();
987
void set_charset(const CHARSET_INFO * const cs)
986
void set_charset(CHARSET_INFO *cs)
990
989
value.set_quick(value_buff, sizeof(value_buff), cs);
1083
cmp_item_sort_string_in_static(const CHARSET_INFO * const cs):
1082
cmp_item_sort_string_in_static(CHARSET_INFO *cs):
1084
1083
cmp_item_string(cs) {}
1085
1084
void store_value(Item *item)
1087
1086
value_res= item->val_str(&value);
1089
int cmp(Item *item __attribute__((unused)))
1088
int cmp(Item *item __attribute__((__unused__)))
1091
1090
// Should never be called
1127
1126
int first_expr_num, else_expr_num;
1128
1127
enum Item_result cached_result_type, left_result_type;
1129
1128
String tmp_value;
1131
1130
Item_result cmp_type;
1132
1131
DTCollation cmp_collation;
1133
1132
enum_field_types cached_field_type;
1150
1149
list.push_back(else_expr_arg);
1152
1151
set_arguments(list);
1153
memset(&cmp_items, 0, sizeof(cmp_items));
1152
bzero(&cmp_items, sizeof(cmp_items));
1155
1154
double val_real();
1156
1155
int64_t val_int();
1158
1157
my_decimal *val_decimal(my_decimal *);
1159
1158
bool fix_fields(THD *thd, Item **ref);
1160
1159
void fix_length_and_dec();
1161
uint32_t decimal_precision() const;
1160
uint decimal_precision() const;
1162
1161
table_map not_null_tables() const { return 0; }
1163
1162
enum Item_result result_type () const { return cached_result_type; }
1164
1163
enum_field_types field_type() const { return cached_field_type; }
1165
1164
const char *func_name() const { return "case"; }
1166
1165
virtual void print(String *str, enum_query_type query_type);
1167
1166
Item *find_item(String *str);
1168
const CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
1167
CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
1169
1168
void cleanup();
1170
1169
void agg_str_lengths(Item *arg);
1171
1170
void agg_num_lengths(Item *arg);
1207
1206
:Item_func_opt_neg(list), array(0), have_null(0),
1208
1207
arg_types_compatible(false)
1210
memset(&cmp_items, 0, sizeof(cmp_items));
1209
bzero(&cmp_items, sizeof(cmp_items));
1211
1210
allowed_arg_cols= 0; // Fetch this value from first argument
1213
1212
int64_t val_int();
1214
1213
bool fix_fields(THD *, Item **);
1215
1214
void fix_length_and_dec();
1216
uint32_t decimal_precision() const { return 1; }
1215
uint decimal_precision() const { return 1; }
1220
1219
Item_int_func::cleanup();
1234
1233
const char *func_name() const { return " IN "; }
1235
1234
bool nulls_in_row();
1236
1235
bool is_bool_func() { return 1; }
1237
const CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
1236
CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
1240
1239
class cmp_item_row :public cmp_item
1242
1241
cmp_item **comparators;
1245
1244
cmp_item_row(): comparators(0), n(0) {}
1246
1245
~cmp_item_row();
1259
1258
cmp_item_row tmp;
1261
in_row(uint32_t elements, Item *);
1260
in_row(uint elements, Item *);
1263
void set(uint32_t pos,Item *item);
1264
unsigned char *get_value(Item *item);
1262
void set(uint pos,Item *item);
1263
uchar *get_value(Item *item);
1265
1264
friend void Item_func_in::fix_length_and_dec();
1266
1265
Item_result result_type() { return ROW_RESULT; }
1305
1304
table_map not_null_tables() const { return 0; }
1306
1305
optimize_type select_optimize() const { return OPTIMIZE_NULL; }
1307
1306
Item *neg_transformer(THD *thd);
1308
const CHARSET_INFO *compare_collation() { return args[0]->collation.collation; }
1307
CHARSET_INFO *compare_collation() { return args[0]->collation.collation; }
1311
1310
/* Functions used by HAVING for rewriting IN subquery */
1352
1351
{ return abort_on_null ? not_null_tables_cache : 0; }
1353
1352
Item *neg_transformer(THD *thd);
1354
1353
virtual void print(String *str, enum_query_type query_type);
1355
const CHARSET_INFO *compare_collation() { return args[0]->collation.collation; }
1354
CHARSET_INFO *compare_collation() { return args[0]->collation.collation; }
1356
1355
void top_level_item() { abort_on_null=1; }
1429
1428
void update_used_tables();
1430
1429
virtual void print(String *str, enum_query_type query_type);
1431
1430
void split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields);
1432
friend int setup_conds(THD *thd, TableList *tables, TableList *leaves,
1431
friend int setup_conds(THD *thd, TABLE_LIST *tables, TABLE_LIST *leaves,
1434
1433
void top_level_item() { abort_on_null=1; }
1435
1434
void copy_andor_arguments(THD *thd, Item_cond *item);
1436
bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1437
Item *transform(Item_transformer transformer, unsigned char *arg);
1435
bool walk(Item_processor processor, bool walk_subquery, uchar *arg);
1436
Item *transform(Item_transformer transformer, uchar *arg);
1438
1437
void traverse_cond(Cond_traverser, void *arg, traverse_order order);
1439
1438
void neg_arguments(THD *thd);
1440
enum_field_types field_type() const { return DRIZZLE_TYPE_LONGLONG; }
1441
bool subst_argument_checker(unsigned char **arg __attribute__((unused)))
1439
enum_field_types field_type() const { return MYSQL_TYPE_LONGLONG; }
1440
bool subst_argument_checker(uchar **arg __attribute__((__unused__)))
1442
1441
{ return true; }
1443
Item *compile(Item_analyzer analyzer, unsigned char **arg_p,
1444
Item_transformer transformer, unsigned char *arg_t);
1442
Item *compile(Item_analyzer analyzer, uchar **arg_p,
1443
Item_transformer transformer, uchar *arg_t);
1550
1549
void fix_length_and_dec();
1551
1550
bool fix_fields(THD *thd, Item **ref);
1552
1551
void update_used_tables();
1553
bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1554
Item *transform(Item_transformer transformer, unsigned char *arg);
1552
bool walk(Item_processor processor, bool walk_subquery, uchar *arg);
1553
Item *transform(Item_transformer transformer, uchar *arg);
1555
1554
virtual void print(String *str, enum_query_type query_type);
1556
const CHARSET_INFO *compare_collation()
1555
CHARSET_INFO *compare_collation()
1557
1556
{ return fields.head()->collation.collation; }
1560
1559
class COND_EQUAL: public Sql_alloc
1563
uint32_t max_members; /* max number of members the current level
1562
uint max_members; /* max number of members the current level
1564
1563
list and all lower level lists */
1565
1564
COND_EQUAL *upper_levels; /* multiple equalities of upper and levels */
1566
1565
List<Item_equal> current_level; /* list of multiple equalities of