mariadb/sql/item_func.h

1526 lines
42 KiB
C
Raw Normal View History

/* Copyright (C) 2000-2006 MySQL AB
2000-07-31 21:29:14 +02:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
2000-07-31 21:29:14 +02:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2000-07-31 21:29:14 +02:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/* Function items used by mysql */
#ifdef USE_PRAGMA_INTERFACE
2000-07-31 21:29:14 +02:00
#pragma interface /* gcc class implementation */
#endif
#ifdef HAVE_IEEEFP_H
extern "C" /* Bug in BSDI include file */
{
#include <ieeefp.h>
}
#endif
class Item_func :public Item_result_field
{
protected:
Item **args, *tmp_arg[2];
/*
Allowed numbers of columns in result (usually 1, which means scalar value)
0 means get this number from first argument
*/
2002-11-15 19:32:09 +01:00
uint allowed_arg_cols;
2000-07-31 21:29:14 +02:00
public:
uint arg_count;
table_map used_tables_cache, not_null_tables_cache;
2000-07-31 21:29:14 +02:00
bool const_item_cache;
enum Functype { UNKNOWN_FUNC,EQ_FUNC,EQUAL_FUNC,NE_FUNC,LT_FUNC,LE_FUNC,
GE_FUNC,GT_FUNC,FT_FUNC,
LIKE_FUNC,ISNULL_FUNC,ISNOTNULL_FUNC,
COND_AND_FUNC, COND_OR_FUNC, COND_XOR_FUNC,
BETWEEN, IN_FUNC, MULT_EQUAL_FUNC,
INTERVAL_FUNC, ISNOTNULLTEST_FUNC,
2002-02-22 12:24:42 +01:00
SP_EQUALS_FUNC, SP_DISJOINT_FUNC,SP_INTERSECTS_FUNC,
SP_TOUCHES_FUNC,SP_CROSSES_FUNC,SP_WITHIN_FUNC,
SP_CONTAINS_FUNC,SP_OVERLAPS_FUNC,
SP_STARTPOINT,SP_ENDPOINT,SP_EXTERIORRING,
SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN,
2004-06-25 15:48:10 +02:00
NOT_FUNC, NOT_ALL_FUNC,
NOW_FUNC, TRIG_COND_FUNC,
SUSERVAR_FUNC, GUSERVAR_FUNC, COLLATE_FUNC,
EXTRACT_FUNC, CHAR_TYPECAST_FUNC, FUNC_SP, UDF_FUNC };
enum optimize_type { OPTIMIZE_NONE,OPTIMIZE_KEY,OPTIMIZE_OP, OPTIMIZE_NULL,
OPTIMIZE_EQUAL };
2000-07-31 21:29:14 +02:00
enum Type type() const { return FUNC_ITEM; }
virtual enum Functype functype() const { return UNKNOWN_FUNC; }
2002-11-15 19:32:09 +01:00
Item_func(void):
allowed_arg_cols(1), arg_count(0)
2000-07-31 21:29:14 +02:00
{
with_sum_func= 0;
2000-07-31 21:29:14 +02:00
}
2002-11-15 19:32:09 +01:00
Item_func(Item *a):
allowed_arg_cols(1), arg_count(1)
2000-07-31 21:29:14 +02:00
{
args= tmp_arg;
args[0]= a;
with_sum_func= a->with_sum_func;
2000-07-31 21:29:14 +02:00
}
2002-11-15 19:32:09 +01:00
Item_func(Item *a,Item *b):
allowed_arg_cols(1), arg_count(2)
2000-07-31 21:29:14 +02:00
{
args= tmp_arg;
args[0]= a; args[1]= b;
with_sum_func= a->with_sum_func || b->with_sum_func;
2000-07-31 21:29:14 +02:00
}
2002-11-15 19:32:09 +01:00
Item_func(Item *a,Item *b,Item *c):
allowed_arg_cols(1)
2000-07-31 21:29:14 +02:00
{
arg_count= 0;
if ((args= (Item**) sql_alloc(sizeof(Item*)*3)))
2000-07-31 21:29:14 +02:00
{
arg_count= 3;
args[0]= a; args[1]= b; args[2]= c;
with_sum_func= a->with_sum_func || b->with_sum_func || c->with_sum_func;
2000-07-31 21:29:14 +02:00
}
}
2002-11-15 19:32:09 +01:00
Item_func(Item *a,Item *b,Item *c,Item *d):
allowed_arg_cols(1)
2000-07-31 21:29:14 +02:00
{
arg_count= 0;
if ((args= (Item**) sql_alloc(sizeof(Item*)*4)))
2000-07-31 21:29:14 +02:00
{
arg_count= 4;
args[0]= a; args[1]= b; args[2]= c; args[3]= d;
with_sum_func= a->with_sum_func || b->with_sum_func ||
c->with_sum_func || d->with_sum_func;
2000-07-31 21:29:14 +02:00
}
}
2002-11-15 19:32:09 +01:00
Item_func(Item *a,Item *b,Item *c,Item *d,Item* e):
allowed_arg_cols(1)
2000-07-31 21:29:14 +02:00
{
arg_count= 5;
if ((args= (Item**) sql_alloc(sizeof(Item*)*5)))
2000-07-31 21:29:14 +02:00
{
args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
with_sum_func= a->with_sum_func || b->with_sum_func ||
c->with_sum_func || d->with_sum_func || e->with_sum_func ;
2000-07-31 21:29:14 +02:00
}
}
Item_func(List<Item> &list);
// Constructor used for Item_cond_and/or (see Item comment)
Item_func(THD *thd, Item_func *item);
bool fix_fields(THD *, Item **ref);
2000-07-31 21:29:14 +02:00
table_map used_tables() const;
table_map not_null_tables() const;
2000-07-31 21:29:14 +02:00
void update_used_tables();
bool eq(const Item *item, bool binary_cmp) const;
2000-07-31 21:29:14 +02:00
virtual optimize_type select_optimize() const { return OPTIMIZE_NONE; }
virtual bool have_rev_func() const { return 0; }
virtual Item *key_item() const { return args[0]; }
/*
This method is used for debug purposes to print the name of an
item to the debug log. The second use of this method is as
a helper function of print(), where it is applicable.
To suit both goals it should return a meaningful,
distinguishable and sintactically correct string. This method
should not be used for runtime type identification, use enum
{Sum}Functype and Item_func::functype()/Item_sum::sum_func()
instead.
*/
virtual const char *func_name() const= 0;
2000-07-31 21:29:14 +02:00
virtual bool const_item() const { return const_item_cache; }
inline Item **arguments() const { return args; }
void set_arguments(List<Item> &list);
2000-07-31 21:29:14 +02:00
inline uint argument_count() const { return arg_count; }
inline void remove_arguments() { arg_count=0; }
void split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields);
2000-07-31 21:29:14 +02:00
void print(String *str);
void print_op(String *str);
2003-11-03 11:28:36 +01:00
void print_args(String *str, uint from);
2005-02-08 23:50:45 +01:00
virtual void fix_num_length_and_dec();
void count_only_length();
void count_real_length();
void count_decimal_length();
inline bool get_arg0_date(TIME *ltime, uint fuzzy_date)
2000-07-31 21:29:14 +02:00
{
return (null_value=args[0]->get_date(ltime, fuzzy_date));
2000-07-31 21:29:14 +02:00
}
inline bool get_arg0_time(TIME *ltime)
{
return (null_value=args[0]->get_time(ltime));
}
bool is_null() {
update_null_value();
return null_value;
}
void signal_divide_by_null();
2000-07-31 21:29:14 +02:00
friend class udf_handler;
Field *tmp_table_field() { return result_field; }
Field *tmp_table_field(TABLE *t_arg);
Item *get_tmp_table_item(THD *thd);
2005-02-08 23:50:45 +01:00
my_decimal *val_decimal(my_decimal *);
bool agg_arg_collations(DTCollation &c, Item **items, uint nitems,
uint flags)
{
return agg_item_collations(c, func_name(), items, nitems, flags, 1);
}
bool agg_arg_collations_for_comparison(DTCollation &c,
Item **items, uint nitems,
uint flags)
{
return agg_item_collations_for_comparison(c, func_name(),
items, nitems, flags);
}
bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems,
uint flags, int item_sep)
{
return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep);
}
This changeset is largely a handler cleanup changeset (WL#3281), but includes fixes and cleanups that was found necessary while testing the handler changes Changes that requires code changes in other code of other storage engines. (Note that all changes are very straightforward and one should find all issues by compiling a --debug build and fixing all compiler errors and all asserts in field.cc while running the test suite), - New optional handler function introduced: reset() This is called after every DML statement to make it easy for a handler to statement specific cleanups. (The only case it's not called is if force the file to be closed) - handler::extra(HA_EXTRA_RESET) is removed. Code that was there before should be moved to handler::reset() - table->read_set contains a bitmap over all columns that are needed in the query. read_row() and similar functions only needs to read these columns - table->write_set contains a bitmap over all columns that will be updated in the query. write_row() and update_row() only needs to update these columns. The above bitmaps should now be up to date in all context (including ALTER TABLE, filesort()). The handler is informed of any changes to the bitmap after fix_fields() by calling the virtual function handler::column_bitmaps_signal(). If the handler does caching of these bitmaps (instead of using table->read_set, table->write_set), it should redo the caching in this code. as the signal() may be sent several times, it's probably best to set a variable in the signal and redo the caching on read_row() / write_row() if the variable was set. - Removed the read_set and write_set bitmap objects from the handler class - Removed all column bit handling functions from the handler class. (Now one instead uses the normal bitmap functions in my_bitmap.c instead of handler dedicated bitmap functions) - field->query_id is removed. One should instead instead check table->read_set and table->write_set if a field is used in the query. - handler::extra(HA_EXTRA_RETRIVE_ALL_COLS) and handler::extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY) are removed. One should now instead use table->read_set to check for which columns to retrieve. - If a handler needs to call Field->val() or Field->store() on columns that are not used in the query, one should install a temporary all-columns-used map while doing so. For this, we provide the following functions: my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set); field->val(); dbug_tmp_restore_column_map(table->read_set, old_map); and similar for the write map: my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set); field->val(); dbug_tmp_restore_column_map(table->write_set, old_map); If this is not done, you will sooner or later hit a DBUG_ASSERT in the field store() / val() functions. (For not DBUG binaries, the dbug_tmp_restore_column_map() and dbug_tmp_restore_column_map() are inline dummy functions and should be optimized away be the compiler). - If one needs to temporary set the column map for all binaries (and not just to avoid the DBUG_ASSERT() in the Field::store() / Field::val() methods) one should use the functions tmp_use_all_columns() and tmp_restore_column_map() instead of the above dbug_ variants. - All 'status' fields in the handler base class (like records, data_file_length etc) are now stored in a 'stats' struct. This makes it easier to know what status variables are provided by the base handler. This requires some trivial variable names in the extra() function. - New virtual function handler::records(). This is called to optimize COUNT(*) if (handler::table_flags() & HA_HAS_RECORDS()) is true. (stats.records is not supposed to be an exact value. It's only has to be 'reasonable enough' for the optimizer to be able to choose a good optimization path). - Non virtual handler::init() function added for caching of virtual constants from engine. - Removed has_transactions() virtual method. Now one should instead return HA_NO_TRANSACTIONS in table_flags() if the table handler DOES NOT support transactions. - The 'xxxx_create_handler()' function now has a MEM_ROOT_root argument that is to be used with 'new handler_name()' to allocate the handler in the right area. The xxxx_create_handler() function is also responsible for any initialization of the object before returning. For example, one should change: static handler *myisam_create_handler(TABLE_SHARE *table) { return new ha_myisam(table); } -> static handler *myisam_create_handler(TABLE_SHARE *table, MEM_ROOT *mem_root) { return new (mem_root) ha_myisam(table); } - New optional virtual function: use_hidden_primary_key(). This is called in case of an update/delete when (table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined but we don't have a primary key. This allows the handler to take precisions in remembering any hidden primary key to able to update/delete any found row. The default handler marks all columns to be read. - handler::table_flags() now returns a ulonglong (to allow for more flags). - New/changed table_flags() - HA_HAS_RECORDS Set if ::records() is supported - HA_NO_TRANSACTIONS Set if engine doesn't support transactions - HA_PRIMARY_KEY_REQUIRED_FOR_DELETE Set if we should mark all primary key columns for read when reading rows as part of a DELETE statement. If there is no primary key, all columns are marked for read. - HA_PARTIAL_COLUMN_READ Set if engine will not read all columns in some cases (based on table->read_set) - HA_PRIMARY_KEY_ALLOW_RANDOM_ACCESS Renamed to HA_PRIMARY_KEY_REQUIRED_FOR_POSITION. - HA_DUPP_POS Renamed to HA_DUPLICATE_POS - HA_REQUIRES_KEY_COLUMNS_FOR_DELETE Set this if we should mark ALL key columns for read when when reading rows as part of a DELETE statement. In case of an update we will mark all keys for read for which key part changed value. - HA_STATS_RECORDS_IS_EXACT Set this if stats.records is exact. (This saves us some extra records() calls when optimizing COUNT(*)) - Removed table_flags() - HA_NOT_EXACT_COUNT Now one should instead use HA_HAS_RECORDS if handler::records() gives an exact count() and HA_STATS_RECORDS_IS_EXACT if stats.records is exact. - HA_READ_RND_SAME Removed (no one supported this one) - Removed not needed functions ha_retrieve_all_cols() and ha_retrieve_all_pk() - Renamed handler::dupp_pos to handler::dup_pos - Removed not used variable handler::sortkey Upper level handler changes: - ha_reset() now does some overall checks and calls ::reset() - ha_table_flags() added. This is a cached version of table_flags(). The cache is updated on engine creation time and updated on open. MySQL level changes (not obvious from the above): - DBUG_ASSERT() added to check that column usage matches what is set in the column usage bit maps. (This found a LOT of bugs in current column marking code). - In 5.1 before, all used columns was marked in read_set and only updated columns was marked in write_set. Now we only mark columns for which we need a value in read_set. - Column bitmaps are created in open_binary_frm() and open_table_from_share(). (Before this was in table.cc) - handler::table_flags() calls are replaced with handler::ha_table_flags() - For calling field->val() you must have the corresponding bit set in table->read_set. For calling field->store() you must have the corresponding bit set in table->write_set. (There are asserts in all store()/val() functions to catch wrong usage) - thd->set_query_id is renamed to thd->mark_used_columns and instead of setting this to an integer value, this has now the values: MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE Changed also all variables named 'set_query_id' to mark_used_columns. - In filesort() we now inform the handler of exactly which columns are needed doing the sort and choosing the rows. - The TABLE_SHARE object has a 'all_set' column bitmap one can use when one needs a column bitmap with all columns set. (This is used for table->use_all_columns() and other places) - The TABLE object has 3 column bitmaps: - def_read_set Default bitmap for columns to be read - def_write_set Default bitmap for columns to be written - tmp_set Can be used as a temporary bitmap when needed. The table object has also two pointer to bitmaps read_set and write_set that the handler should use to find out which columns are used in which way. - count() optimization now calls handler::records() instead of using handler->stats.records (if (table_flags() & HA_HAS_RECORDS) is true). - Added extra argument to Item::walk() to indicate if we should also traverse sub queries. - Added TABLE parameter to cp_buffer_from_ref() - Don't close tables created with CREATE ... SELECT but keep them in the table cache. (Faster usage of newly created tables). New interfaces: - table->clear_column_bitmaps() to initialize the bitmaps for tables at start of new statements. - table->column_bitmaps_set() to set up new column bitmaps and signal the handler about this. - table->column_bitmaps_set_no_signal() for some few cases where we need to setup new column bitmaps but don't signal the handler (as the handler has already been signaled about these before). Used for the momement only in opt_range.cc when doing ROR scans. - table->use_all_columns() to install a bitmap where all columns are marked as use in the read and the write set. - table->default_column_bitmaps() to install the normal read and write column bitmaps, but not signaling the handler about this. This is mainly used when creating TABLE instances. - table->mark_columns_needed_for_delete(), table->mark_columns_needed_for_delete() and table->mark_columns_needed_for_insert() to allow us to put additional columns in column usage maps if handler so requires. (The handler indicates what it neads in handler->table_flags()) - table->prepare_for_position() to allow us to tell handler that it needs to read primary key parts to be able to store them in future table->position() calls. (This replaces the table->file->ha_retrieve_all_pk function) - table->mark_auto_increment_column() to tell handler are going to update columns part of any auto_increment key. - table->mark_columns_used_by_index() to mark all columns that is part of an index. It will also send extra(HA_EXTRA_KEYREAD) to handler to allow it to quickly know that it only needs to read colums that are part of the key. (The handler can also use the column map for detecting this, but simpler/faster handler can just monitor the extra() call). - table->mark_columns_used_by_index_no_reset() to in addition to other columns, also mark all columns that is used by the given key. - table->restore_column_maps_after_mark_index() to restore to default column maps after a call to table->mark_columns_used_by_index(). - New item function register_field_in_read_map(), for marking used columns in table->read_map. Used by filesort() to mark all used columns - Maintain in TABLE->merge_keys set of all keys that are used in query. (Simplices some optimization loops) - Maintain Field->part_of_key_not_clustered which is like Field->part_of_key but the field in the clustered key is not assumed to be part of all index. (used in opt_range.cc for faster loops) - dbug_tmp_use_all_columns(), dbug_tmp_restore_column_map() tmp_use_all_columns() and tmp_restore_column_map() functions to temporally mark all columns as usable. The 'dbug_' version is primarily intended inside a handler when it wants to just call Field:store() & Field::val() functions, but don't need the column maps set for any other usage. (ie:: bitmap_is_set() is never called) - We can't use compare_records() to skip updates for handlers that returns a partial column set and the read_set doesn't cover all columns in the write set. The reason for this is that if we have a column marked only for write we can't in the MySQL level know if the value changed or not. The reason this worked before was that MySQL marked all to be written columns as also to be read. The new 'optimal' bitmaps exposed this 'hidden bug'. - open_table_from_share() does not anymore setup temporary MEM_ROOT object as a thread specific variable for the handler. Instead we send the to-be-used MEMROOT to get_new_handler(). (Simpler, faster code) Bugs fixed: - Column marking was not done correctly in a lot of cases. (ALTER TABLE, when using triggers, auto_increment fields etc) (Could potentially result in wrong values inserted in table handlers relying on that the old column maps or field->set_query_id was correct) Especially when it comes to triggers, there may be cases where the old code would cause lost/wrong values for NDB and/or InnoDB tables. - Split thd->options flag OPTION_STATUS_NO_TRANS_UPDATE to two flags: OPTION_STATUS_NO_TRANS_UPDATE and OPTION_KEEP_LOG. This allowed me to remove some wrong warnings about: "Some non-transactional changed tables couldn't be rolled back" - Fixed handling of INSERT .. SELECT and CREATE ... SELECT that wrongly reset (thd->options & OPTION_STATUS_NO_TRANS_UPDATE) which caused us to loose some warnings about "Some non-transactional changed tables couldn't be rolled back") - Fixed use of uninitialized memory in ha_ndbcluster.cc::delete_table() which could cause delete_table to report random failures. - Fixed core dumps for some tests when running with --debug - Added missing FN_LIBCHAR in mysql_rm_tmp_tables() (This has probably caused us to not properly remove temporary files after crash) - slow_logs was not properly initialized, which could maybe cause extra/lost entries in slow log. - If we get an duplicate row on insert, change column map to read and write all columns while retrying the operation. This is required by the definition of REPLACE and also ensures that fields that are only part of UPDATE are properly handled. This fixed a bug in NDB and REPLACE where REPLACE wrongly copied some column values from the replaced row. - For table handler that doesn't support NULL in keys, we would give an error when creating a primary key with NULL fields, even after the fields has been automaticly converted to NOT NULL. - Creating a primary key on a SPATIAL key, would fail if field was not declared as NOT NULL. Cleanups: - Removed not used condition argument to setup_tables - Removed not needed item function reset_query_id_processor(). - Field->add_index is removed. Now this is instead maintained in (field->flags & FIELD_IN_ADD_INDEX) - Field->fieldnr is removed (use field->field_index instead) - New argument to filesort() to indicate that it should return a set of row pointers (not used columns). This allowed me to remove some references to sql_command in filesort and should also enable us to return column results in some cases where we couldn't before. - Changed column bitmap handling in opt_range.cc to be aligned with TABLE bitmap, which allowed me to use bitmap functions instead of looping over all fields to create some needed bitmaps. (Faster and smaller code) - Broke up found too long lines - Moved some variable declaration at start of function for better code readability. - Removed some not used arguments from functions. (setup_fields(), mysql_prepare_insert_check_table()) - setup_fields() now takes an enum instead of an int for marking columns usage. - For internal temporary tables, use handler::write_row(), handler::delete_row() and handler::update_row() instead of handler::ha_xxxx() for faster execution. - Changed some constants to enum's and define's. - Using separate column read and write sets allows for easier checking of timestamp field was set by statement. - Remove calls to free_io_cache() as this is now done automaticly in ha_reset() - Don't build table->normalized_path as this is now identical to table->path (after bar's fixes to convert filenames) - Fixed some missed DBUG_PRINT(.."%lx") to use "0x%lx" to make it easier to do comparision with the 'convert-dbug-for-diff' tool. Things left to do in 5.1: - We wrongly log failed CREATE TABLE ... SELECT in some cases when using row based logging (as shown by testcase binlog_row_mix_innodb_myisam.result) Mats has promised to look into this. - Test that my fix for CREATE TABLE ... SELECT is indeed correct. (I added several test cases for this, but in this case it's better that someone else also tests this throughly). Lars has promosed to do this.
2006-06-04 17:52:22 +02:00
bool walk(Item_processor processor, bool walk_subquery, byte *arg);
Item *transform(Item_transformer transformer, byte *arg);
Item* compile(Item_analyzer analyzer, byte **arg_p,
Item_transformer transformer, byte *arg_t);
void traverse_cond(Cond_traverser traverser,
void * arg, traverse_order order);
bool is_expensive_processor(byte *arg);
virtual bool is_expensive() { return 0; }
2000-07-31 21:29:14 +02:00
};
class Item_real_func :public Item_func
{
public:
Item_real_func() :Item_func() {}
Item_real_func(Item *a) :Item_func(a) {}
Item_real_func(Item *a,Item *b) :Item_func(a,b) {}
Item_real_func(List<Item> &list) :Item_func(list) {}
String *val_str(String*str);
my_decimal *val_decimal(my_decimal *decimal_value);
2004-11-11 19:39:35 +01:00
longlong val_int()
{ DBUG_ASSERT(fixed == 1); return (longlong) rint(val_real()); }
2000-07-31 21:29:14 +02:00
enum Item_result result_type () const { return REAL_RESULT; }
2005-02-08 23:50:45 +01:00
void fix_length_and_dec()
{ decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
2000-07-31 21:29:14 +02:00
};
2005-02-08 23:50:45 +01:00
class Item_func_numhybrid: public Item_func
2000-07-31 21:29:14 +02:00
{
2005-02-08 23:50:45 +01:00
protected:
2000-07-31 21:29:14 +02:00
Item_result hybrid_type;
public:
Item_func_numhybrid(Item *a) :Item_func(a), hybrid_type(REAL_RESULT)
2005-02-08 23:50:45 +01:00
{}
Item_func_numhybrid(Item *a,Item *b)
:Item_func(a,b), hybrid_type(REAL_RESULT)
2005-02-08 23:50:45 +01:00
{}
Item_func_numhybrid(List<Item> &list)
:Item_func(list), hybrid_type(REAL_RESULT)
{}
2005-02-08 23:50:45 +01:00
2000-07-31 21:29:14 +02:00
enum Item_result result_type () const { return hybrid_type; }
2005-02-08 23:50:45 +01:00
void fix_length_and_dec();
void fix_num_length_and_dec();
virtual void find_num_type()= 0; /* To be called from fix_length_and_dec */
double val_real();
longlong val_int();
my_decimal *val_decimal(my_decimal *);
String *val_str(String*str);
virtual longlong int_op()= 0;
virtual double real_op()= 0;
virtual my_decimal *decimal_op(my_decimal *)= 0;
virtual String *str_op(String *)= 0;
bool is_null() { update_null_value(); return null_value; }
2000-07-31 21:29:14 +02:00
};
2005-02-08 23:50:45 +01:00
/* function where type of result detected by first argument */
class Item_func_num1: public Item_func_numhybrid
{
public:
Item_func_num1(Item *a) :Item_func_numhybrid(a) {}
Item_func_num1(Item *a, Item *b) :Item_func_numhybrid(a, b) {}
2000-07-31 21:29:14 +02:00
2005-02-08 23:50:45 +01:00
void fix_num_length_and_dec();
void find_num_type();
String *str_op(String *str) { DBUG_ASSERT(0); return 0; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2005-02-08 23:50:45 +01:00
};
2000-07-31 21:29:14 +02:00
2005-02-08 23:50:45 +01:00
/* Base class for operations like '+', '-', '*' */
class Item_num_op :public Item_func_numhybrid
2000-07-31 21:29:14 +02:00
{
public:
2005-02-08 23:50:45 +01:00
Item_num_op(Item *a,Item *b) :Item_func_numhybrid(a, b) {}
virtual void result_precision()= 0;
2000-07-31 21:29:14 +02:00
void print(String *str) { print_op(str); }
2005-02-08 23:50:45 +01:00
void find_num_type();
String *str_op(String *str) { DBUG_ASSERT(0); return 0; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_int_func :public Item_func
{
public:
2005-02-08 23:50:45 +01:00
Item_int_func() :Item_func() { max_length= 21; }
Item_int_func(Item *a) :Item_func(a) { max_length= 21; }
Item_int_func(Item *a,Item *b) :Item_func(a,b) { max_length= 21; }
Item_int_func(Item *a,Item *b,Item *c) :Item_func(a,b,c)
{ max_length= 21; }
Item_int_func(List<Item> &list) :Item_func(list) { max_length= 21; }
Item_int_func(THD *thd, Item_int_func *item) :Item_func(thd, item) {}
2004-11-11 19:39:35 +01:00
double val_real() { DBUG_ASSERT(fixed == 1); return (double) val_int(); }
2000-07-31 21:29:14 +02:00
String *val_str(String*str);
enum Item_result result_type () const { return INT_RESULT; }
void fix_length_and_dec() {}
2000-07-31 21:29:14 +02:00
};
class Item_func_connection_id :public Item_int_func
{
longlong value;
public:
Item_func_connection_id() {}
const char *func_name() const { return "connection_id"; }
void fix_length_and_dec();
bool fix_fields(THD *thd, Item **ref);
longlong val_int() { DBUG_ASSERT(fixed == 1); return value; }
};
class Item_func_signed :public Item_int_func
{
public:
Item_func_signed(Item *a) :Item_int_func(a) {}
const char *func_name() const { return "cast_as_signed"; }
2004-11-11 19:39:35 +01:00
double val_real()
{
2004-11-11 19:39:35 +01:00
double tmp= args[0]->val_real();
null_value= args[0]->null_value;
return tmp;
}
longlong val_int();
longlong val_int_from_str(int *error);
void fix_length_and_dec()
{ max_length=args[0]->max_length; unsigned_flag=0; }
void print(String *str);
uint decimal_precision() const { return args[0]->decimal_precision(); }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
};
class Item_func_unsigned :public Item_func_signed
{
public:
Item_func_unsigned(Item *a) :Item_func_signed(a) {}
const char *func_name() const { return "cast_as_unsigned"; }
void fix_length_and_dec()
{ max_length=args[0]->max_length; unsigned_flag=1; }
longlong val_int();
void print(String *str);
};
2005-02-08 23:50:45 +01:00
class Item_decimal_typecast :public Item_func
2000-07-31 21:29:14 +02:00
{
2005-02-08 23:50:45 +01:00
my_decimal decimal_value;
2000-07-31 21:29:14 +02:00
public:
2005-02-08 23:50:45 +01:00
Item_decimal_typecast(Item *a, int len, int dec) :Item_func(a)
{
max_length= len + 2;
decimals= dec;
}
String *val_str(String *str);
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
longlong val_int();
2005-02-08 23:50:45 +01:00
my_decimal *val_decimal(my_decimal*);
enum Item_result result_type () const { return DECIMAL_RESULT; }
enum_field_types field_type() const { return MYSQL_TYPE_NEWDECIMAL; }
2005-02-08 23:50:45 +01:00
void fix_length_and_dec() {};
const char *func_name() const { return "decimal_typecast"; }
void print(String *);
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2005-02-08 23:50:45 +01:00
};
class Item_func_additive_op :public Item_num_op
{
public:
Item_func_additive_op(Item *a,Item *b) :Item_num_op(a,b) {}
void result_precision();
};
class Item_func_plus :public Item_func_additive_op
{
public:
Item_func_plus(Item *a,Item *b) :Item_func_additive_op(a,b) {}
const char *func_name() const { return "+"; }
longlong int_op();
double real_op();
my_decimal *decimal_op(my_decimal *);
2000-07-31 21:29:14 +02:00
};
2005-02-08 23:50:45 +01:00
class Item_func_minus :public Item_func_additive_op
2000-07-31 21:29:14 +02:00
{
public:
2005-02-08 23:50:45 +01:00
Item_func_minus(Item *a,Item *b) :Item_func_additive_op(a,b) {}
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "-"; }
2005-02-08 23:50:45 +01:00
longlong int_op();
double real_op();
my_decimal *decimal_op(my_decimal *);
void fix_length_and_dec();
2000-07-31 21:29:14 +02:00
};
2000-07-31 21:29:14 +02:00
class Item_func_mul :public Item_num_op
{
public:
Item_func_mul(Item *a,Item *b) :Item_num_op(a,b) {}
const char *func_name() const { return "*"; }
2005-02-08 23:50:45 +01:00
longlong int_op();
double real_op();
my_decimal *decimal_op(my_decimal *);
void result_precision();
2000-07-31 21:29:14 +02:00
};
class Item_func_div :public Item_num_op
{
public:
uint prec_increment;
2000-07-31 21:29:14 +02:00
Item_func_div(Item *a,Item *b) :Item_num_op(a,b) {}
longlong int_op() { DBUG_ASSERT(0); return 0; }
2005-02-08 23:50:45 +01:00
double real_op();
my_decimal *decimal_op(my_decimal *);
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "/"; }
void fix_length_and_dec();
2005-02-08 23:50:45 +01:00
void result_precision();
2000-07-31 21:29:14 +02:00
};
class Item_func_int_div :public Item_int_func
{
public:
Item_func_int_div(Item *a,Item *b) :Item_int_func(a,b)
2005-02-08 23:50:45 +01:00
{}
longlong val_int();
const char *func_name() const { return "DIV"; }
void fix_length_and_dec();
2005-02-08 23:50:45 +01:00
void print(String *str) { print_op(str); }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
};
2000-07-31 21:29:14 +02:00
class Item_func_mod :public Item_num_op
{
public:
Item_func_mod(Item *a,Item *b) :Item_num_op(a,b) {}
2005-02-08 23:50:45 +01:00
longlong int_op();
double real_op();
my_decimal *decimal_op(my_decimal *);
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "%"; }
2005-02-08 23:50:45 +01:00
void result_precision();
2006-11-07 02:41:18 +01:00
void fix_length_and_dec();
2005-02-08 23:50:45 +01:00
};
class Item_func_neg :public Item_func_num1
2000-07-31 21:29:14 +02:00
{
public:
Item_func_neg(Item *a) :Item_func_num1(a) {}
2005-02-08 23:50:45 +01:00
double real_op();
longlong int_op();
my_decimal *decimal_op(my_decimal *);
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "-"; }
void fix_length_and_dec();
2005-02-08 23:50:45 +01:00
void fix_num_length_and_dec();
uint decimal_precision() const { return args[0]->decimal_precision(); }
2000-07-31 21:29:14 +02:00
};
2005-02-08 23:50:45 +01:00
class Item_func_abs :public Item_func_num1
2000-07-31 21:29:14 +02:00
{
public:
2005-02-08 23:50:45 +01:00
Item_func_abs(Item *a) :Item_func_num1(a) {}
double real_op();
longlong int_op();
my_decimal *decimal_op(my_decimal *);
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "abs"; }
void fix_length_and_dec();
};
2005-02-08 23:50:45 +01:00
// A class to handle logarithmic and trigonometric functions
2000-07-31 21:29:14 +02:00
class Item_dec_func :public Item_real_func
{
public:
Item_dec_func(Item *a) :Item_real_func(a) {}
Item_dec_func(Item *a,Item *b) :Item_real_func(a,b) {}
void fix_length_and_dec()
{
decimals=NOT_FIXED_DEC; max_length=float_length(decimals);
2000-07-31 21:29:14 +02:00
maybe_null=1;
}
inline double fix_result(double value)
{
#ifndef HAVE_FINITE
return value;
#else
/* The following should be safe, even if we compare doubles */
2000-07-31 21:29:14 +02:00
if (finite(value) && value != POSTFIX_ERROR)
return value;
null_value=1;
return 0.0;
#endif
}
};
class Item_func_exp :public Item_dec_func
{
public:
Item_func_exp(Item *a) :Item_dec_func(a) {}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "exp"; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_ln :public Item_dec_func
{
public:
Item_func_ln(Item *a) :Item_dec_func(a) {}
2004-11-11 19:39:35 +01:00
double val_real();
const char *func_name() const { return "ln"; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
};
2000-07-31 21:29:14 +02:00
class Item_func_log :public Item_dec_func
{
public:
Item_func_log(Item *a) :Item_dec_func(a) {}
Item_func_log(Item *a,Item *b) :Item_dec_func(a,b) {}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "log"; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_log2 :public Item_dec_func
{
public:
Item_func_log2(Item *a) :Item_dec_func(a) {}
2004-11-11 19:39:35 +01:00
double val_real();
const char *func_name() const { return "log2"; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
};
2000-07-31 21:29:14 +02:00
class Item_func_log10 :public Item_dec_func
{
public:
Item_func_log10(Item *a) :Item_dec_func(a) {}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "log10"; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_sqrt :public Item_dec_func
{
public:
Item_func_sqrt(Item *a) :Item_dec_func(a) {}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "sqrt"; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_pow :public Item_dec_func
{
public:
Item_func_pow(Item *a,Item *b) :Item_dec_func(a,b) {}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "pow"; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_acos :public Item_dec_func
{
public:
2000-07-31 21:29:14 +02:00
Item_func_acos(Item *a) :Item_dec_func(a) {}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "acos"; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_asin :public Item_dec_func
{
public:
2000-07-31 21:29:14 +02:00
Item_func_asin(Item *a) :Item_dec_func(a) {}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "asin"; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_atan :public Item_dec_func
{
public:
2000-07-31 21:29:14 +02:00
Item_func_atan(Item *a) :Item_dec_func(a) {}
Item_func_atan(Item *a,Item *b) :Item_dec_func(a,b) {}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "atan"; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_cos :public Item_dec_func
{
public:
2000-07-31 21:29:14 +02:00
Item_func_cos(Item *a) :Item_dec_func(a) {}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "cos"; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_sin :public Item_dec_func
{
public:
2000-07-31 21:29:14 +02:00
Item_func_sin(Item *a) :Item_dec_func(a) {}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "sin"; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_tan :public Item_dec_func
{
public:
2000-07-31 21:29:14 +02:00
Item_func_tan(Item *a) :Item_dec_func(a) {}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "tan"; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_integer :public Item_int_func
{
public:
inline Item_func_integer(Item *a) :Item_int_func(a) {}
void fix_length_and_dec();
};
2005-02-08 23:50:45 +01:00
class Item_func_int_val :public Item_func_num1
{
public:
Item_func_int_val(Item *a) :Item_func_num1(a) {}
void fix_num_length_and_dec();
void find_num_type();
};
class Item_func_ceiling :public Item_func_int_val
2000-07-31 21:29:14 +02:00
{
public:
2005-02-08 23:50:45 +01:00
Item_func_ceiling(Item *a) :Item_func_int_val(a) {}
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "ceiling"; }
2005-02-08 23:50:45 +01:00
longlong int_op();
double real_op();
my_decimal *decimal_op(my_decimal *);
2000-07-31 21:29:14 +02:00
};
2005-02-08 23:50:45 +01:00
class Item_func_floor :public Item_func_int_val
2000-07-31 21:29:14 +02:00
{
public:
2005-02-08 23:50:45 +01:00
Item_func_floor(Item *a) :Item_func_int_val(a) {}
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "floor"; }
2005-02-08 23:50:45 +01:00
longlong int_op();
double real_op();
my_decimal *decimal_op(my_decimal *);
2000-07-31 21:29:14 +02:00
};
/* This handles round and truncate */
2005-02-08 23:50:45 +01:00
class Item_func_round :public Item_func_num1
2000-07-31 21:29:14 +02:00
{
bool truncate;
public:
2005-02-08 23:50:45 +01:00
Item_func_round(Item *a, Item *b, bool trunc_arg)
:Item_func_num1(a,b), truncate(trunc_arg) {}
2000-07-31 21:29:14 +02:00
const char *func_name() const { return truncate ? "truncate" : "round"; }
2005-02-08 23:50:45 +01:00
double real_op();
longlong int_op();
my_decimal *decimal_op(my_decimal *);
void fix_length_and_dec();
2000-07-31 21:29:14 +02:00
};
class Item_func_rand :public Item_real_func
{
struct rand_struct *rand;
2000-07-31 21:29:14 +02:00
public:
Item_func_rand(Item *a) :Item_real_func(a), rand(0) {}
Item_func_rand() :Item_real_func() {}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "rand"; }
bool const_item() const { return 0; }
2004-03-16 19:19:36 +01:00
void update_used_tables();
bool fix_fields(THD *thd, Item **ref);
private:
void seed_random (Item * val);
2000-07-31 21:29:14 +02:00
};
class Item_func_sign :public Item_int_func
{
public:
Item_func_sign(Item *a) :Item_int_func(a) {}
const char *func_name() const { return "sign"; }
longlong val_int();
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_units :public Item_real_func
{
char *name;
double mul,add;
public:
2000-07-31 21:29:14 +02:00
Item_func_units(char *name_arg,Item *a,double mul_arg,double add_arg)
:Item_real_func(a),name(name_arg),mul(mul_arg),add(add_arg) {}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
const char *func_name() const { return name; }
2005-02-08 23:50:45 +01:00
void fix_length_and_dec()
{ decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_min_max :public Item_func
{
Item_result cmp_type;
String tmp_value;
int cmp_sign;
public:
Item_func_min_max(List<Item> &list,int cmp_sign_arg) :Item_func(list),
cmp_type(INT_RESULT), cmp_sign(cmp_sign_arg) {}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
longlong val_int();
String *val_str(String *);
2005-02-08 23:50:45 +01:00
my_decimal *val_decimal(my_decimal *);
2000-07-31 21:29:14 +02:00
void fix_length_and_dec();
enum Item_result result_type () const { return cmp_type; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_min :public Item_func_min_max
{
public:
Item_func_min(List<Item> &list) :Item_func_min_max(list,1) {}
const char *func_name() const { return "least"; }
};
class Item_func_max :public Item_func_min_max
{
public:
Item_func_max(List<Item> &list) :Item_func_min_max(list,-1) {}
const char *func_name() const { return "greatest"; }
};
2000-07-31 21:29:14 +02:00
class Item_func_length :public Item_int_func
{
String value;
public:
Item_func_length(Item *a) :Item_int_func(a) {}
longlong val_int();
const char *func_name() const { return "length"; }
void fix_length_and_dec() { max_length=10; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
2002-01-03 20:47:14 +01:00
class Item_func_bit_length :public Item_func_length
{
public:
Item_func_bit_length(Item *a) :Item_func_length(a) {}
longlong val_int()
{ DBUG_ASSERT(fixed == 1); return Item_func_length::val_int()*8; }
2002-01-03 20:47:14 +01:00
const char *func_name() const { return "bit_length"; }
};
2000-07-31 21:29:14 +02:00
class Item_func_char_length :public Item_int_func
{
String value;
public:
Item_func_char_length(Item *a) :Item_int_func(a) {}
longlong val_int();
const char *func_name() const { return "char_length"; }
void fix_length_and_dec() { max_length=10; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_coercibility :public Item_int_func
{
public:
Item_func_coercibility(Item *a) :Item_int_func(a) {}
longlong val_int();
const char *func_name() const { return "coercibility"; }
void fix_length_and_dec() { max_length=10; maybe_null= 0; }
table_map not_null_tables() const { return 0; }
};
2000-07-31 21:29:14 +02:00
class Item_func_locate :public Item_int_func
{
String value1,value2;
DTCollation cmp_collation;
2000-07-31 21:29:14 +02:00
public:
Item_func_locate(Item *a,Item *b) :Item_int_func(a,b) {}
Item_func_locate(Item *a,Item *b,Item *c) :Item_int_func(a,b,c) {}
const char *func_name() const { return "locate"; }
longlong val_int();
void fix_length_and_dec();
void print(String *str);
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_field :public Item_int_func
{
String value,tmp;
Item_result cmp_type;
DTCollation cmp_collation;
2000-07-31 21:29:14 +02:00
public:
Item_func_field(List<Item> &list) :Item_int_func(list) {}
2000-07-31 21:29:14 +02:00
longlong val_int();
const char *func_name() const { return "field"; }
void fix_length_and_dec();
2000-07-31 21:29:14 +02:00
};
class Item_func_ascii :public Item_int_func
{
String value;
public:
Item_func_ascii(Item *a) :Item_int_func(a) {}
longlong val_int();
const char *func_name() const { return "ascii"; }
void fix_length_and_dec() { max_length=3; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_ord :public Item_int_func
{
String value;
public:
Item_func_ord(Item *a) :Item_int_func(a) {}
longlong val_int();
const char *func_name() const { return "ord"; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_find_in_set :public Item_int_func
{
String value,value2;
uint enum_value;
ulonglong enum_bit;
DTCollation cmp_collation;
2000-07-31 21:29:14 +02:00
public:
Item_func_find_in_set(Item *a,Item *b) :Item_int_func(a,b),enum_value(0) {}
longlong val_int();
const char *func_name() const { return "find_in_set"; }
void fix_length_and_dec();
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
/* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
2000-07-31 21:29:14 +02:00
class Item_func_bit: public Item_int_func
2000-07-31 21:29:14 +02:00
{
public:
Item_func_bit(Item *a, Item *b) :Item_int_func(a, b) {}
Item_func_bit(Item *a) :Item_int_func(a) {}
void fix_length_and_dec() { unsigned_flag= 1; }
2003-12-17 16:35:34 +01:00
void print(String *str) { print_op(str); }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
};
class Item_func_bit_or :public Item_func_bit
{
public:
Item_func_bit_or(Item *a, Item *b) :Item_func_bit(a, b) {}
2000-07-31 21:29:14 +02:00
longlong val_int();
const char *func_name() const { return "|"; }
};
class Item_func_bit_and :public Item_func_bit
2000-07-31 21:29:14 +02:00
{
public:
Item_func_bit_and(Item *a, Item *b) :Item_func_bit(a, b) {}
2000-07-31 21:29:14 +02:00
longlong val_int();
const char *func_name() const { return "&"; }
};
class Item_func_bit_count :public Item_int_func
{
public:
Item_func_bit_count(Item *a) :Item_int_func(a) {}
longlong val_int();
const char *func_name() const { return "bit_count"; }
void fix_length_and_dec() { max_length=2; }
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
class Item_func_shift_left :public Item_func_bit
2000-07-31 21:29:14 +02:00
{
public:
Item_func_shift_left(Item *a, Item *b) :Item_func_bit(a, b) {}
2000-07-31 21:29:14 +02:00
longlong val_int();
const char *func_name() const { return "<<"; }
};
class Item_func_shift_right :public Item_func_bit
2000-07-31 21:29:14 +02:00
{
public:
Item_func_shift_right(Item *a, Item *b) :Item_func_bit(a, b) {}
2000-07-31 21:29:14 +02:00
longlong val_int();
const char *func_name() const { return ">>"; }
};
class Item_func_bit_neg :public Item_func_bit
2000-07-31 21:29:14 +02:00
{
public:
Item_func_bit_neg(Item *a) :Item_func_bit(a) {}
2000-07-31 21:29:14 +02:00
longlong val_int();
const char *func_name() const { return "~"; }
void print(String *str) { Item_func::print(str); }
2000-07-31 21:29:14 +02:00
};
class Item_func_last_insert_id :public Item_int_func
2000-07-31 21:29:14 +02:00
{
public:
Item_func_last_insert_id() :Item_int_func() {}
Item_func_last_insert_id(Item *a) :Item_int_func(a) {}
2000-07-31 21:29:14 +02:00
longlong val_int();
const char *func_name() const { return "last_insert_id"; }
2005-02-08 23:50:45 +01:00
void fix_length_and_dec()
{
if (arg_count)
max_length= args[0]->max_length;
}
2000-07-31 21:29:14 +02:00
};
2000-07-31 21:29:14 +02:00
class Item_func_benchmark :public Item_int_func
{
public:
Item_func_benchmark(Item *count_expr, Item *expr)
:Item_int_func(count_expr, expr)
2000-07-31 21:29:14 +02:00
{}
longlong val_int();
const char *func_name() const { return "benchmark"; }
void fix_length_and_dec() { max_length=1; maybe_null=0; }
void print(String *str);
2000-07-31 21:29:14 +02:00
};
class Item_func_sleep :public Item_int_func
{
public:
Item_func_sleep(Item *a) :Item_int_func(a) {}
bool const_item() const { return 0; }
const char *func_name() const { return "sleep"; }
void update_used_tables()
{
Item_int_func::update_used_tables();
used_tables_cache|= RAND_TABLE_BIT;
}
longlong val_int();
};
2000-07-31 21:29:14 +02:00
#ifdef HAVE_DLOPEN
class Item_udf_func :public Item_func
{
protected:
2000-07-31 21:29:14 +02:00
udf_handler udf;
public:
Item_udf_func(udf_func *udf_arg)
:Item_func(), udf(udf_arg) {}
2000-07-31 21:29:14 +02:00
Item_udf_func(udf_func *udf_arg, List<Item> &list)
:Item_func(list), udf(udf_arg) {}
const char *func_name() const { return udf.name(); }
enum Functype functype() const { return UDF_FUNC; }
bool fix_fields(THD *thd, Item **ref)
2000-07-31 21:29:14 +02:00
{
DBUG_ASSERT(fixed == 0);
bool res= udf.fix_fields(thd, this, arg_count, args);
used_tables_cache= udf.used_tables_cache;
const_item_cache= udf.const_item_cache;
fixed= 1;
2000-07-31 21:29:14 +02:00
return res;
}
void cleanup();
2000-07-31 21:29:14 +02:00
Item_result result_type () const { return udf.result_type(); }
table_map not_null_tables() const { return 0; }
bool is_expensive() { return 1; }
void print(String *str);
2000-07-31 21:29:14 +02:00
};
class Item_func_udf_float :public Item_udf_func
{
public:
Item_func_udf_float(udf_func *udf_arg)
:Item_udf_func(udf_arg) {}
Item_func_udf_float(udf_func *udf_arg,
List<Item> &list)
:Item_udf_func(udf_arg, list) {}
longlong val_int()
2004-11-11 19:39:35 +01:00
{
DBUG_ASSERT(fixed == 1);
return (longlong) rint(Item_func_udf_float::val_real());
2004-11-11 19:39:35 +01:00
}
2005-02-08 23:50:45 +01:00
my_decimal *val_decimal(my_decimal *dec_buf)
{
double res=val_real();
if (null_value)
return NULL;
double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
return dec_buf;
}
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
String *val_str(String *str);
void fix_length_and_dec() { fix_num_length_and_dec(); }
};
class Item_func_udf_int :public Item_udf_func
{
public:
Item_func_udf_int(udf_func *udf_arg)
:Item_udf_func(udf_arg) {}
Item_func_udf_int(udf_func *udf_arg,
List<Item> &list)
:Item_udf_func(udf_arg, list) {}
2000-07-31 21:29:14 +02:00
longlong val_int();
2004-11-11 19:39:35 +01:00
double val_real() { return (double) Item_func_udf_int::val_int(); }
2000-07-31 21:29:14 +02:00
String *val_str(String *str);
enum Item_result result_type () const { return INT_RESULT; }
2005-02-08 23:50:45 +01:00
void fix_length_and_dec() { decimals= 0; max_length= 21; }
};
class Item_func_udf_decimal :public Item_udf_func
{
public:
Item_func_udf_decimal(udf_func *udf_arg)
:Item_udf_func(udf_arg) {}
2005-02-08 23:50:45 +01:00
Item_func_udf_decimal(udf_func *udf_arg, List<Item> &list)
:Item_udf_func(udf_arg, list) {}
2005-02-08 23:50:45 +01:00
longlong val_int();
double val_real();
my_decimal *val_decimal(my_decimal *);
String *val_str(String *str);
enum Item_result result_type () const { return DECIMAL_RESULT; }
void fix_length_and_dec();
2000-07-31 21:29:14 +02:00
};
class Item_func_udf_str :public Item_udf_func
{
public:
Item_func_udf_str(udf_func *udf_arg)
:Item_udf_func(udf_arg) {}
2000-07-31 21:29:14 +02:00
Item_func_udf_str(udf_func *udf_arg, List<Item> &list)
:Item_udf_func(udf_arg, list) {}
2000-07-31 21:29:14 +02:00
String *val_str(String *);
2004-11-11 19:39:35 +01:00
double val_real()
2000-07-31 21:29:14 +02:00
{
int err_not_used;
char *end_not_used;
String *res;
res= val_str(&str_value);
return res ? my_strntod(res->charset(),(char*) res->ptr(),
res->length(), &end_not_used, &err_not_used) : 0.0;
2000-07-31 21:29:14 +02:00
}
longlong val_int()
{
int err_not_used;
2000-07-31 21:29:14 +02:00
String *res; res=val_str(&str_value);
return res ? my_strntoll(res->charset(),res->ptr(),res->length(),10,
(char**) 0, &err_not_used) : (longlong) 0;
2000-07-31 21:29:14 +02:00
}
2005-02-08 23:50:45 +01:00
my_decimal *val_decimal(my_decimal *dec_buf)
{
String *res=val_str(&str_value);
if (!res)
return NULL;
string2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
return dec_buf;
}
2000-07-31 21:29:14 +02:00
enum Item_result result_type () const { return STRING_RESULT; }
void fix_length_and_dec();
};
#else /* Dummy functions to get sql_yacc.cc compiled */
class Item_func_udf_float :public Item_real_func
{
public:
Item_func_udf_float(udf_func *udf_arg)
:Item_real_func() {}
Item_func_udf_float(udf_func *udf_arg, List<Item> &list)
:Item_real_func(list) {}
2004-11-11 19:39:35 +01:00
double val_real() { DBUG_ASSERT(fixed == 1); return 0.0; }
2000-07-31 21:29:14 +02:00
};
class Item_func_udf_int :public Item_int_func
{
public:
Item_func_udf_int(udf_func *udf_arg)
:Item_int_func() {}
Item_func_udf_int(udf_func *udf_arg, List<Item> &list)
:Item_int_func(list) {}
longlong val_int() { DBUG_ASSERT(fixed == 1); return 0; }
2000-07-31 21:29:14 +02:00
};
2005-02-08 23:50:45 +01:00
class Item_func_udf_decimal :public Item_int_func
{
public:
Item_func_udf_decimal(udf_func *udf_arg)
:Item_int_func() {}
Item_func_udf_decimal(udf_func *udf_arg, List<Item> &list)
:Item_int_func(list) {}
2005-02-08 23:50:45 +01:00
my_decimal *val_decimal(my_decimal *) { DBUG_ASSERT(fixed == 1); return 0; }
};
2000-07-31 21:29:14 +02:00
class Item_func_udf_str :public Item_func
{
public:
Item_func_udf_str(udf_func *udf_arg)
:Item_func() {}
Item_func_udf_str(udf_func *udf_arg, List<Item> &list)
:Item_func(list) {}
String *val_str(String *)
{ DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
2004-11-11 19:39:35 +01:00
double val_real() { DBUG_ASSERT(fixed == 1); null_value= 1; return 0.0; }
longlong val_int() { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
2000-07-31 21:29:14 +02:00
enum Item_result result_type () const { return STRING_RESULT; }
void fix_length_and_dec() { maybe_null=1; max_length=0; }
};
#endif /* HAVE_DLOPEN */
/*
** User level locks
*/
class User_level_lock;
2000-07-31 21:29:14 +02:00
void item_user_lock_init(void);
void item_user_lock_release(User_level_lock *ull);
2000-07-31 21:29:14 +02:00
void item_user_lock_free(void);
class Item_func_get_lock :public Item_int_func
{
String value;
public:
Item_func_get_lock(Item *a,Item *b) :Item_int_func(a,b) {}
longlong val_int();
const char *func_name() const { return "get_lock"; }
void fix_length_and_dec() { max_length=1; maybe_null=1;}
2000-07-31 21:29:14 +02:00
};
class Item_func_release_lock :public Item_int_func
{
String value;
public:
2000-07-31 21:29:14 +02:00
Item_func_release_lock(Item *a) :Item_int_func(a) {}
longlong val_int();
const char *func_name() const { return "release_lock"; }
void fix_length_and_dec() { max_length=1; maybe_null=1;}
2000-07-31 21:29:14 +02:00
};
/* replication functions */
class Item_master_pos_wait :public Item_int_func
{
String value;
public:
Item_master_pos_wait(Item *a,Item *b) :Item_int_func(a,b) {}
Item_master_pos_wait(Item *a,Item *b,Item *c) :Item_int_func(a,b,c) {}
longlong val_int();
const char *func_name() const { return "master_pos_wait"; }
void fix_length_and_dec() { max_length=21; maybe_null=1;}
};
2000-07-31 21:29:14 +02:00
2005-02-08 23:50:45 +01:00
/* Handling of user definable variables */
2000-07-31 21:29:14 +02:00
class user_var_entry;
class Item_func_set_user_var :public Item_func
{
enum Item_result cached_result_type;
user_var_entry *entry;
char buffer[MAX_FIELD_WIDTH];
String value;
2005-02-08 23:50:45 +01:00
my_decimal decimal_buff;
bool null_item;
union
{
longlong vint;
double vreal;
String *vstr;
2005-02-08 23:50:45 +01:00
my_decimal *vdec;
} save_result;
2000-07-31 21:29:14 +02:00
public:
LEX_STRING name; // keep it public
Item_func_set_user_var(LEX_STRING a,Item *b)
:Item_func(b), cached_result_type(INT_RESULT), name(a)
{}
enum Functype functype() const { return SUSERVAR_FUNC; }
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
longlong val_int();
String *val_str(String *str);
2005-02-08 23:50:45 +01:00
my_decimal *val_decimal(my_decimal *);
bool update_hash(void *ptr, uint length, enum Item_result type,
2006-09-12 14:25:40 +02:00
CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
bool send(Protocol *protocol, String *str_arg);
void make_field(Send_field *tmp_field);
bool check(bool use_result_field);
2000-07-31 21:29:14 +02:00
bool update();
enum Item_result result_type () const { return cached_result_type; }
bool fix_fields(THD *thd, Item **ref);
2000-07-31 21:29:14 +02:00
void fix_length_and_dec();
2001-12-27 01:04:27 +01:00
void print(String *str);
void print_as_stmt(String *str);
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "set_user_var"; }
int save_in_field(Field *field, bool no_conversions);
2000-07-31 21:29:14 +02:00
};
class Item_func_get_user_var :public Item_func,
private Settable_routine_parameter
2000-07-31 21:29:14 +02:00
{
user_var_entry *var_entry;
2000-07-31 21:29:14 +02:00
public:
LEX_STRING name; // keep it public
Item_func_get_user_var(LEX_STRING a):
Item_func(), name(a) {}
enum Functype functype() const { return GUSERVAR_FUNC; }
LEX_STRING get_name() { return name; }
2004-11-11 19:39:35 +01:00
double val_real();
2000-07-31 21:29:14 +02:00
longlong val_int();
2005-02-08 23:50:45 +01:00
my_decimal *val_decimal(my_decimal*);
2000-07-31 21:29:14 +02:00
String *val_str(String* str);
void fix_length_and_dec();
2001-12-27 01:04:27 +01:00
void print(String *str);
2000-07-31 21:29:14 +02:00
enum Item_result result_type() const;
/*
We must always return variables as strings to guard against selects of type
select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
*/
enum_field_types field_type() const { return MYSQL_TYPE_VARCHAR; }
2000-07-31 21:29:14 +02:00
const char *func_name() const { return "get_user_var"; }
bool const_item() const;
table_map used_tables() const
{ return const_item() ? 0 : RAND_TABLE_BIT; }
bool eq(const Item *item, bool binary_cmp) const;
private:
bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
public:
Settable_routine_parameter *get_settable_routine_parameter()
{
return this;
}
};
2000-07-31 21:29:14 +02:00
2001-12-27 01:04:27 +01:00
/*
This item represents user variable used as out parameter (e.g in LOAD DATA),
and it is supposed to be used only for this purprose. So it is simplified
a lot. Actually you should never obtain its value.
The only two reasons for this thing being an Item is possibility to store it
in List<Item> and desire to place this code somewhere near other functions
working with user variables.
*/
class Item_user_var_as_out_param :public Item
{
LEX_STRING name;
user_var_entry *entry;
public:
Item_user_var_as_out_param(LEX_STRING a) : name(a) {}
/* We should return something different from FIELD_ITEM here */
enum Type type() const { return STRING_ITEM;}
double val_real();
longlong val_int();
String *val_str(String *str);
my_decimal *val_decimal(my_decimal *decimal_buffer);
/* fix_fields() binds variable name with its entry structure */
bool fix_fields(THD *thd, Item **ref);
void print(String *str);
void set_null_value(CHARSET_INFO* cs);
void set_value(const char *str, uint length, CHARSET_INFO* cs);
};
/* A system variable */
class Item_func_get_system_var :public Item_func
{
sys_var *var;
enum_var_type var_type;
LEX_STRING component;
public:
Item_func_get_system_var(sys_var *var_arg, enum_var_type var_type_arg,
LEX_STRING *component_arg, const char *name_arg,
size_t name_len_arg);
2005-07-16 14:10:42 +02:00
bool fix_fields(THD *thd, Item **ref);
/*
Stubs for pure virtual methods. Should never be called: this
item is always substituted with a constant in fix_fields().
*/
2005-07-16 14:10:42 +02:00
double val_real() { DBUG_ASSERT(0); return 0.0; }
longlong val_int() { DBUG_ASSERT(0); return 0; }
String* val_str(String*) { DBUG_ASSERT(0); return 0; }
void fix_length_and_dec() { DBUG_ASSERT(0); }
2005-07-16 14:10:42 +02:00
/* TODO: fix to support views */
const char *func_name() const { return "get_system_var"; }
};
2000-07-31 21:29:14 +02:00
class Item_func_inet_aton : public Item_int_func
{
public:
Item_func_inet_aton(Item *a) :Item_int_func(a) {}
longlong val_int();
const char *func_name() const { return "inet_aton"; }
void fix_length_and_dec() { decimals= 0; max_length= 21; maybe_null= 1; unsigned_flag= 1;}
2006-09-26 08:25:23 +02:00
bool check_partition_func_processor(byte *int_arg) {return FALSE;}
2000-07-31 21:29:14 +02:00
};
/* for fulltext search */
#include <ft_global.h>
2000-07-31 21:29:14 +02:00
class Item_func_match :public Item_real_func
{
public:
uint key, flags;
2001-12-02 13:34:01 +01:00
bool join_key;
DTCollation cmp_collation;
FT_INFO *ft_handler;
TABLE *table;
Item_func_match *master; // for master-slave optimization
Item *concat; // Item_func_concat_ws
String value; // value of concat
String search_value; // key_item()'s value converted to cmp_collation
2000-07-31 21:29:14 +02:00
2003-10-30 19:17:57 +01:00
Item_func_match(List<Item> &a, uint b): Item_real_func(a), key(0), flags(b),
join_key(0), ft_handler(0), table(0), master(0), concat(0) { }
void cleanup()
{
DBUG_ENTER("Item_func_match");
Item_real_func::cleanup();
if (!master && ft_handler)
2001-10-09 14:53:54 +02:00
ft_handler->please->close_search(ft_handler);
ft_handler= 0;
concat= 0;
DBUG_VOID_RETURN;
}
2000-07-31 21:29:14 +02:00
enum Functype functype() const { return FT_FUNC; }
const char *func_name() const { return "match"; }
2000-07-31 21:29:14 +02:00
void update_used_tables() {}
table_map not_null_tables() const { return 0; }
bool fix_fields(THD *thd, Item **ref);
bool eq(const Item *, bool binary_cmp) const;
/* The following should be safe, even if we compare doubles */
2004-11-11 19:39:35 +01:00
longlong val_int() { DBUG_ASSERT(fixed == 1); return val_real() != 0.0; }
double val_real();
void print(String *str);
bool fix_index();
2000-11-04 15:48:06 +01:00
void init_search(bool no_order);
};
2001-10-09 14:53:54 +02:00
class Item_func_bit_xor : public Item_func_bit
{
public:
Item_func_bit_xor(Item *a, Item *b) :Item_func_bit(a, b) {}
longlong val_int();
const char *func_name() const { return "^"; }
};
class Item_func_is_free_lock :public Item_int_func
{
String value;
public:
Item_func_is_free_lock(Item *a) :Item_int_func(a) {}
longlong val_int();
const char *func_name() const { return "is_free_lock"; }
void fix_length_and_dec() { decimals=0; max_length=1; maybe_null=1;}
};
class Item_func_is_used_lock :public Item_int_func
{
String value;
public:
Item_func_is_used_lock(Item *a) :Item_int_func(a) {}
longlong val_int();
const char *func_name() const { return "is_used_lock"; }
void fix_length_and_dec() { decimals=0; max_length=10; maybe_null=1;}
};
/* For type casts */
enum Cast_target
{
ITEM_CAST_BINARY, ITEM_CAST_SIGNED_INT, ITEM_CAST_UNSIGNED_INT,
2005-02-08 23:50:45 +01:00
ITEM_CAST_DATE, ITEM_CAST_TIME, ITEM_CAST_DATETIME, ITEM_CAST_CHAR,
ITEM_CAST_DECIMAL
};
class Item_func_row_count :public Item_int_func
{
public:
Item_func_row_count() :Item_int_func() {}
longlong val_int();
const char *func_name() const { return "row_count"; }
void fix_length_and_dec() { decimals= 0; maybe_null=0; }
};
/*
*
* Stored FUNCTIONs
*
*/
class sp_head;
class sp_name;
struct st_sp_security_context;
class Item_func_sp :public Item_func
{
private:
Name_resolution_context *context;
sp_name *m_name;
mutable sp_head *m_sp;
2005-03-09 22:53:04 +01:00
TABLE *dummy_table;
Field *result_field;
char result_buf[64];
bool execute(Field **flp);
bool execute_impl(THD *thd, Field *return_value_fld);
Field *sp_result_field(void) const;
public:
Item_func_sp(Name_resolution_context *context_arg, sp_name *name);
Item_func_sp(Name_resolution_context *context_arg,
sp_name *name, List<Item> &list);
virtual ~Item_func_sp()
{}
void cleanup();
const char *func_name() const;
enum enum_field_types field_type() const;
Field *tmp_table_field(TABLE *t_arg);
void make_field(Send_field *tmp_field);
Item_result result_type() const;
longlong val_int()
{
if (execute(&result_field))
2005-04-27 13:54:42 +02:00
return (longlong) 0;
return result_field->val_int();
}
2004-11-11 19:39:35 +01:00
double val_real()
{
if (execute(&result_field))
return 0.0;
return result_field->val_real();
}
2005-02-08 23:50:45 +01:00
my_decimal *val_decimal(my_decimal *dec_buf)
{
if (execute(&result_field))
2005-02-08 23:50:45 +01:00
return NULL;
return result_field->val_decimal(dec_buf);
2005-02-08 23:50:45 +01:00
}
String *val_str(String *str)
{
String buf;
char buff[20];
buf.set(buff, 20, str->charset());
buf.length(0);
if (execute(&result_field))
return NULL;
/*
result_field will set buf pointing to internal buffer
of the resul_field. Due to this it will change any time
when SP is executed. In order to prevent occasional
corruption of returned value, we make here a copy.
*/
result_field->val_str(&buf);
str->copy(buf);
return str;
}
virtual bool change_context_processor(byte *cntx)
{ context= (Name_resolution_context *)cntx; return FALSE; }
void fix_length_and_dec();
bool find_and_check_access(THD * thd);
virtual enum Functype functype() const { return FUNC_SP; }
bool fix_fields(THD *thd, Item **ref);
bool is_expensive() { return 1; }
};
2004-07-07 10:29:39 +02:00
class Item_func_found_rows :public Item_int_func
{
public:
Item_func_found_rows() :Item_int_func() {}
longlong val_int();
const char *func_name() const { return "found_rows"; }
void fix_length_and_dec() { decimals= 0; maybe_null=0; }
};