#include "procedure.h"#include <myisam.h>Include dependency graph for sql_select.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.
| typedef struct st_cache_field CACHE_FIELD |
| typedef struct st_join_cache JOIN_CACHE |
| typedef struct st_join_table JOIN_TAB |
| typedef enum_nested_loop_state(*) Next_select_func(JOIN *, struct st_join_table *, bool) |
Definition at line 102 of file sql_select.h.
| typedef struct st_position POSITION |
| typedef int(*) Read_record_func(struct st_join_table *tab) |
Definition at line 103 of file sql_select.h.
| typedef struct st_select_check SELECT_CHECK |
| typedef struct st_table_ref TABLE_REF |
| NESTED_LOOP_KILLED | |
| NESTED_LOOP_ERROR | |
| NESTED_LOOP_OK | |
| NESTED_LOOP_NO_MORE_ROWS | |
| NESTED_LOOP_QUERY_LIMIT | |
| NESTED_LOOP_CURSOR_LIMIT |
Definition at line 94 of file sql_select.h.
00095 { 00096 NESTED_LOOP_KILLED= -2, NESTED_LOOP_ERROR= -1, 00097 NESTED_LOOP_OK= 0, NESTED_LOOP_NO_MORE_ROWS= 1, 00098 NESTED_LOOP_QUERY_LIMIT= 3, NESTED_LOOP_CURSOR_LIMIT= 4 00099 };
| enum join_type |
| JT_UNKNOWN | |
| JT_SYSTEM | |
| JT_CONST | |
| JT_EQ_REF | |
| JT_REF | |
| JT_MAYBE_REF | |
| JT_ALL | |
| JT_RANGE | |
| JT_NEXT | |
| JT_FT | |
| JT_REF_OR_NULL | |
| JT_UNIQUE_SUBQUERY | |
| JT_INDEX_SUBQUERY | |
| JT_INDEX_MERGE |
Definition at line 88 of file sql_select.h.
00088 { JT_UNKNOWN,JT_SYSTEM,JT_CONST,JT_EQ_REF,JT_REF,JT_MAYBE_REF, 00089 JT_ALL, JT_RANGE, JT_NEXT, JT_FT, JT_REF_OR_NULL, 00090 JT_UNIQUE_SUBQUERY, JT_INDEX_SUBQUERY, JT_INDEX_MERGE};
| void copy_fields | ( | TMP_TABLE_PARAM * | param | ) |
Definition at line 13352 of file sql_select.cc.
References Copy_field::do_copy.
Referenced by Item_sum_count_distinct::add(), Item_func_group_concat::add(), JOIN::clear(), end_send_group(), end_unique_update(), end_update(), end_write(), end_write_group(), and QUICK_GROUP_MIN_MAX_SELECT::get_next().
13353 { 13354 Copy_field *ptr=param->copy_field; 13355 Copy_field *end=param->copy_field_end; 13356 13357 for (; ptr != end; ptr++) 13358 (*ptr->do_copy)(ptr); 13359 13360 List_iterator_fast<Item> it(param->copy_funcs); 13361 Item_copy_string *item; 13362 while ((item = (Item_copy_string*) it++)) 13363 item->copy(); 13364 }
Here is the caller graph for this function:

| void copy_funcs | ( | Item ** | func_ptr | ) |
Definition at line 13676 of file sql_select.cc.
References func.
Referenced by Item_sum_count_distinct::add(), Item_func_group_concat::add(), end_unique_update(), end_update(), end_write(), and end_write_group().
13677 { 13678 Item *func; 13679 for (; (func = *func_ptr) ; func_ptr++) 13680 func->save_in_result_field(1); 13681 }
Here is the caller graph for this function:

Definition at line 12974 of file sql_select.cc.
References Item::const_item(), Item::FIELD_ITEM, Item::real_item(), Item::SUM_FUNC_ITEM, Item::type(), and Item::with_sum_func.
Referenced by JOIN::exec(), JOIN::prepare(), Item_sum_count_distinct::setup(), Item_func_group_concat::setup(), and Item_in_subselect::single_value_transformer().
12976 { 12977 List_iterator<Item> li(fields); 12978 Item *field; 12979 12980 param->field_count=param->sum_func_count=param->func_count= 12981 param->hidden_field_count=0; 12982 param->quick_group=1; 12983 while ((field=li++)) 12984 { 12985 Item::Type type=field->real_item()->type(); 12986 if (type == Item::FIELD_ITEM) 12987 param->field_count++; 12988 else if (type == Item::SUM_FUNC_ITEM) 12989 { 12990 if (! field->const_item()) 12991 { 12992 Item_sum *sum_item=(Item_sum*) field->real_item(); 12993 if (!sum_item->quick_group) 12994 param->quick_group=0; // UDF SUM function 12995 param->sum_func_count++; 12996 12997 for (uint i=0 ; i < sum_item->arg_count ; i++) 12998 { 12999 if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM) 13000 param->field_count++; 13001 else 13002 param->func_count++; 13003 } 13004 } 13005 } 13006 else 13007 { 13008 param->func_count++; 13009 if (reset_with_sum_func) 13010 field->with_sum_func=0; 13011 } 13012 } 13013 }
Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 12575 of file sql_select.cc.
References CHECK_FIELD_IGNORE, mySTL::copy(), dbug_tmp_restore_column_map(), dbug_tmp_use_all_columns(), st_table_ref::key_copy, and st_table::write_set.
Referenced by cmp_buffer_with_ref(), get_quick_select_for_ref(), join_ft_read_first(), join_read_always_key(), join_read_const(), and join_read_last_key().
12576 { 12577 enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields; 12578 thd->count_cuted_fields= CHECK_FIELD_IGNORE; 12579 my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set); 12580 bool result= 0; 12581 12582 for (store_key **copy=ref->key_copy ; *copy ; copy++) 12583 { 12584 if ((*copy)->copy() & 1) 12585 { 12586 result= 1; 12587 break; 12588 } 12589 } 12590 thd->count_cuted_fields= save_count_cuted_fields; 12591 dbug_tmp_restore_column_map(table->write_set, old_map); 12592 return result; 12593 }
Here is the call graph for this function:

Here is the caller graph for this function:

| bool create_myisam_from_heap | ( | THD * | thd, | |
| TABLE * | table, | |||
| TMP_TABLE_PARAM * | param, | |||
| int | error, | |||
| bool | ignore_last_dupp_error | |||
| ) |
Definition at line 9437 of file sql_select.cc.
References handler::change_table_ptr(), handler::close(), create_myisam_tmp_table(), st_table_share::db_type, DBUG_ENTER, DBUG_PRINT, DBUG_RETURN, handler::delete_table(), handler::disable_indexes(), err, handler::extra(), st_table::file, get_new_handler(), HA_CHECK_DUP, HA_ERR_RECORD_FILE_FULL, HA_EXTRA_NO_ROWS, HA_EXTRA_WRITE_CACHE, handler::ha_index_or_rnd_end(), HA_KEY_SWITCH_ALL, handler::ha_rnd_end(), handler::ha_rnd_init(), handler::ha_start_bulk_insert(), HA_STATUS_VARIABLE, heap_hton, handler::indexes_are_disabled(), handler::info(), handler::is_fatal_error(), st_table::mem_root, MYF, myisam_hton, st_table::no_rows, open_tmp_table(), handler::print_error(), st_table::record, ha_statistics::records, handler::rnd_next(), st_table::s, handler::stats, LEX_STRING::str, strcmp(), st_table_share::table_name, st_table::use_all_columns(), and handler::write_row().
Referenced by end_update(), end_write(), end_write_group(), JOIN::rollup_write_data(), and schema_table_store_record().
09439 { 09440 TABLE new_table; 09441 TABLE_SHARE share; 09442 const char *save_proc_info; 09443 int write_err; 09444 DBUG_ENTER("create_myisam_from_heap"); 09445 09446 if (table->s->db_type != &heap_hton || 09447 error != HA_ERR_RECORD_FILE_FULL) 09448 { 09449 table->file->print_error(error,MYF(0)); 09450 DBUG_RETURN(1); 09451 } 09452 new_table= *table; 09453 share= *table->s; 09454 new_table.s= &share; 09455 new_table.s->db_type= &myisam_hton; 09456 if (!(new_table.file= get_new_handler(&share, &new_table.mem_root, 09457 &myisam_hton))) 09458 DBUG_RETURN(1); // End of memory 09459 09460 save_proc_info=thd->proc_info; 09461 thd->proc_info="converting HEAP to MyISAM"; 09462 09463 if (create_myisam_tmp_table(&new_table, param, 09464 thd->lex->select_lex.options | thd->options)) 09465 goto err2; 09466 if (open_tmp_table(&new_table)) 09467 goto err1; 09468 if (table->file->indexes_are_disabled()) 09469 new_table.file->disable_indexes(HA_KEY_SWITCH_ALL); 09470 table->file->ha_index_or_rnd_end(); 09471 table->file->ha_rnd_init(1); 09472 if (table->no_rows) 09473 { 09474 new_table.file->extra(HA_EXTRA_NO_ROWS); 09475 new_table.no_rows=1; 09476 } 09477 09478 #ifdef TO_BE_DONE_LATER_IN_4_1 09479 /* 09480 To use start_bulk_insert() (which is new in 4.1) we need to find 09481 all places where a corresponding end_bulk_insert() should be put. 09482 */ 09483 table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */ 09484 new_table.file->ha_start_bulk_insert(table->file->stats.records); 09485 #else 09486 /* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */ 09487 new_table.file->extra(HA_EXTRA_WRITE_CACHE); 09488 #endif 09489 09490 /* 09491 copy all old rows from heap table to MyISAM table 09492 This is the only code that uses record[1] to read/write but this 09493 is safe as this is a temporary MyISAM table without timestamp/autoincrement 09494 or partitioning. 09495 */ 09496 while (!table->file->rnd_next(new_table.record[1])) 09497 { 09498 if ((write_err= new_table.file->write_row(new_table.record[1]))) 09499 goto err; 09500 } 09501 /* copy row that filled HEAP table */ 09502 if ((write_err=new_table.file->write_row(table->record[0]))) 09503 { 09504 if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) || 09505 !ignore_last_dupp_key_error) 09506 goto err; 09507 } 09508 09509 /* remove heap table and change to use myisam table */ 09510 (void) table->file->ha_rnd_end(); 09511 (void) table->file->close(); 09512 (void) table->file->delete_table(table->s->table_name.str); 09513 delete table->file; 09514 table->file=0; 09515 new_table.s= table->s; // Keep old share 09516 *table= new_table; 09517 *table->s= share; 09518 table->file->change_table_ptr(table, table->s); 09519 table->use_all_columns(); 09520 if (save_proc_info) 09521 thd->proc_info= (!strcmp(save_proc_info,"Copying to tmp table") ? 09522 "Copying to tmp table on disk" : save_proc_info); 09523 DBUG_RETURN(0); 09524 09525 err: 09526 DBUG_PRINT("error",("Got error: %d",write_err)); 09527 table->file->print_error(error,MYF(0)); // Give table is full error 09528 (void) table->file->ha_rnd_end(); 09529 (void) new_table.file->close(); 09530 err1: 09531 new_table.file->delete_table(new_table.s->table_name.str); 09532 err2: 09533 delete new_table.file; 09534 thd->proc_info=save_proc_info; 09535 DBUG_RETURN(1); 09536 }
Here is the call graph for this function:

Here is the caller graph for this function:

| Field* create_tmp_field_from_field | ( | THD * | thd, | |
| Field * | org_field, | |||
| const char * | name, | |||
| TABLE * | table, | |||
| Item_field * | item, | |||
| uint | convert_blob_length | |||
| ) |
Definition at line 8139 of file sql_select.cc.
References BLOB_FLAG, Field::charset(), Field::field_name, Field::flags, HA_OPTION_PACK_RECORD, Field::init(), Item::maybe_null, Field::maybe_null(), MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VARCHAR, Field::new_field(), NO_DEFAULT_VALUE_FLAG, NOT_NULL_FLAG, Field::orig_table, Item_field::result_field, st_table::s, Field::table, Field::type(), and UINT_MAX16.
Referenced by Item_sum_hybrid::create_tmp_field(), and create_tmp_field().
08142 { 08143 Field *new_field; 08144 08145 /* 08146 Make sure that the blob fits into a Field_varstring which has 08147 2-byte lenght. 08148 */ 08149 if (convert_blob_length && convert_blob_length < UINT_MAX16 && 08150 (org_field->flags & BLOB_FLAG)) 08151 new_field= new Field_varstring(convert_blob_length, 08152 org_field->maybe_null(), 08153 org_field->field_name, table->s, 08154 org_field->charset()); 08155 else 08156 new_field= org_field->new_field(thd->mem_root, table, 08157 table == org_field->table); 08158 if (new_field) 08159 { 08160 new_field->init(table); 08161 new_field->orig_table= org_field->orig_table; 08162 if (item) 08163 item->result_field= new_field; 08164 else 08165 new_field->field_name= name; 08166 new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG); 08167 if (org_field->maybe_null() || (item && item->maybe_null)) 08168 new_field->flags&= ~NOT_NULL_FLAG; // Because of outer join 08169 if (org_field->type() == MYSQL_TYPE_VAR_STRING || 08170 org_field->type() == MYSQL_TYPE_VARCHAR) 08171 table->s->db_create_options|= HA_OPTION_PACK_RECORD; 08172 } 08173 return new_field; 08174 }
Here is the call graph for this function:

Here is the caller graph for this function:

| TABLE* create_tmp_table | ( | THD * | thd, | |
| TMP_TABLE_PARAM * | param, | |||
| List< Item > & | fields, | |||
| ORDER * | group, | |||
| bool | distinct, | |||
| bool | save_sum_fields, | |||
| ulonglong | select_options, | |||
| ha_rows | rows_limit, | |||
| char * | alias | |||
| ) |
Definition at line 8496 of file sql_select.cc.
References ALIGN_SIZE, alloc_root(), args, AVG_STRING_LENGTH_TO_PACK_ROWS, bfill, bitmap_buffer_size, bitmap_lock_clear_bit(), bitmap_lock_set_next(), BLOB_FLAG, st_order::buff, bzero, Item::const_item(), CONVERT_IF_BIGGER_TO_BLOB, mySTL::copy(), create_myisam_tmp_table(), create_tmp_field(), create_tmp_field_for_schema(), current_pid, DBUG_ASSERT, DBUG_ENTER, DBUG_PRINT, DBUG_RETURN, st_table_share::default_values, err, st_order::field, FIELD_BLOB, Field::field_index, Field::field_length, FIELD_NORMAL, FIELD_SKIP_ENDSPACE, FIELD_TYPE_BIT, FIELDFLAG_BINARY, Field::flags, fn_format(), FN_REFLEN, free_root(), free_tmp_table(), get_new_handler(), GROUP_FLAG, HA_END_SPACE_ARE_EQUAL, HA_KEY_ALG_UNDEF, HA_KEYTYPE_BINARY, HA_KEYTYPE_TEXT, HA_KEYTYPE_VARTEXT1, HA_KEYTYPE_VARTEXT2, HA_NOSAME, HA_NULL_ARE_EQUAL, HA_OPEN_KEYFILE, HA_OPEN_RNDFILE, heap_hton, init_sql_alloc(), init_tmp_table_share(), int(), Field::is_real_null(), st_order::item, keyinfo, LOCK_status, Item::marker, MAX_BLOB_WIDTH, MAX_KEY, memcpy, mi_portable_sizeof_char_ptr, MI_UNIQUE_HASH_LENGTH, min, MIN_STRING_LENGTH_TO_PACK_ROWS, Field::move_field(), Field::move_field_offset(), multi_alloc_root(), MY_BIT_NONE, my_charset_bin, MY_REPLACE_EXT, MY_UNPACK_FILENAME, MYF, myisam_hton, MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR, new(), Field::new_key_field(), st_order::next, Item::next, Field::NONE, NOT_NULL_FLAG, NULL, NullS, open_tmp_table(), OPTION_BIG_TABLES, Field::pack_length(), path, pos(), Field::ptr, RATIO_TO_PACK_ROWS, Field::real_type(), recinfo, reclength, st_table::record, Field::reset(), st_table::s, SELECT_SMALL_RESULT, set_if_bigger, set_if_smaller, Field::set_notnull(), Field::set_null(), setup_tmp_table_column_bitmaps(), statistic_increment, store_record, STRING_TOTAL_LENGTH_TO_PACK_ROWS, strlen(), strmov(), Item::SUM_FUNC_ITEM, Field::table, TABLE_ALLOC_BLOCK_SIZE, Field::table_name, temp_pool, test, test_flags, TEST_KEEP_TMP_TABLES, TL_WRITE, tmp_file_prefix, TMP_TABLE_ALL_COLUMNS, TMP_TABLE_FORCE_MYISAM, Field::type(), Item::type(), use_temp_pool, and Item::with_sum_func.
Referenced by JOIN::exec(), Item_sum_count_distinct::setup(), and Item_func_group_concat::setup().
08500 { 08501 MEM_ROOT *mem_root_save, own_root; 08502 TABLE *table; 08503 TABLE_SHARE *share; 08504 uint i,field_count,null_count,null_pack_length; 08505 uint copy_func_count= param->func_count; 08506 uint hidden_null_count, hidden_null_pack_length, hidden_field_count; 08507 uint blob_count,group_null_items, string_count; 08508 uint temp_pool_slot=MY_BIT_NONE; 08509 ulong reclength, string_total_length, fieldnr= 0; 08510 bool using_unique_constraint= 0; 08511 bool use_packed_rows= 0; 08512 bool not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS); 08513 char *tmpname,path[FN_REFLEN]; 08514 byte *pos, *group_buff, *bitmaps; 08515 uchar *null_flags; 08516 Field **reg_field, **from_field, **default_field; 08517 uint *blob_field; 08518 Copy_field *copy=0; 08519 KEY *keyinfo; 08520 KEY_PART_INFO *key_part_info; 08521 Item **copy_func; 08522 MI_COLUMNDEF *recinfo; 08523 uint total_uneven_bit_length= 0; 08524 bool force_copy_fields= param->force_copy_fields; 08525 DBUG_ENTER("create_tmp_table"); 08526 DBUG_PRINT("enter", 08527 ("distinct: %d save_sum_fields: %d rows_limit: %lu group: %d", 08528 (int) distinct, (int) save_sum_fields, 08529 (ulong) rows_limit,test(group))); 08530 08531 statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status); 08532 08533 if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES)) 08534 temp_pool_slot = bitmap_lock_set_next(&temp_pool); 08535 08536 if (temp_pool_slot != MY_BIT_NONE) // we got a slot 08537 sprintf(path, "%s_%lx_%i", tmp_file_prefix, 08538 current_pid, temp_pool_slot); 08539 else 08540 { 08541 /* if we run out of slots or we are not using tempool */ 08542 sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid, 08543 thd->thread_id, thd->tmp_table++); 08544 } 08545 08546 /* 08547 No need to change table name to lower case as we are only creating 08548 MyISAM or HEAP tables here 08549 */ 08550 fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME); 08551 08552 if (group) 08553 { 08554 if (!param->quick_group) 08555 group=0; // Can't use group key 08556 else for (ORDER *tmp=group ; tmp ; tmp=tmp->next) 08557 { 08558 (*tmp->item)->marker=4; // Store null in key 08559 if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB) 08560 using_unique_constraint=1; 08561 } 08562 if (param->group_length >= MAX_BLOB_WIDTH) 08563 using_unique_constraint=1; 08564 if (group) 08565 distinct=0; // Can't use distinct 08566 } 08567 08568 field_count=param->field_count+param->func_count+param->sum_func_count; 08569 hidden_field_count=param->hidden_field_count; 08570 08571 /* 08572 When loose index scan is employed as access method, it already 08573 computes all groups and the result of all aggregate functions. We 08574 make space for the items of the aggregate function in the list of 08575 functions TMP_TABLE_PARAM::items_to_copy, so that the values of 08576 these items are stored in the temporary table. 08577 */ 08578 if (param->precomputed_group_by) 08579 copy_func_count+= param->sum_func_count; 08580 08581 init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0); 08582 08583 if (!multi_alloc_root(&own_root, 08584 &table, sizeof(*table), 08585 &share, sizeof(*share), 08586 ®_field, sizeof(Field*) * (field_count+1), 08587 &default_field, sizeof(Field*) * (field_count), 08588 &blob_field, sizeof(uint)*(field_count+1), 08589 &from_field, sizeof(Field*)*field_count, 08590 ©_func, sizeof(*copy_func)*(copy_func_count+1), 08591 ¶m->keyinfo, sizeof(*param->keyinfo), 08592 &key_part_info, 08593 sizeof(*key_part_info)*(param->group_parts+1), 08594 ¶m->start_recinfo, 08595 sizeof(*param->recinfo)*(field_count*2+4), 08596 &tmpname, (uint) strlen(path)+1, 08597 &group_buff, (group && ! using_unique_constraint ? 08598 param->group_length : 0), 08599 &bitmaps, bitmap_buffer_size(field_count)*2, 08600 NullS)) 08601 { 08602 if (temp_pool_slot != MY_BIT_NONE) 08603 bitmap_lock_clear_bit(&temp_pool, temp_pool_slot); 08604 DBUG_RETURN(NULL); /* purecov: inspected */ 08605 } 08606 /* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */ 08607 if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count])) 08608 { 08609 if (temp_pool_slot != MY_BIT_NONE) 08610 bitmap_lock_clear_bit(&temp_pool, temp_pool_slot); 08611 free_root(&own_root, MYF(0)); /* purecov: inspected */ 08612 DBUG_RETURN(NULL); /* purecov: inspected */ 08613 } 08614 param->items_to_copy= copy_func; 08615 strmov(tmpname,path); 08616 /* make table according to fields */ 08617 08618 bzero((char*) table,sizeof(*table)); 08619 bzero((char*) reg_field,sizeof(Field*)*(field_count+1)); 08620 bzero((char*) default_field, sizeof(Field*) * (field_count)); 08621 bzero((char*) from_field,sizeof(Field*)*field_count); 08622 08623 table->mem_root= own_root; 08624 mem_root_save= thd->mem_root; 08625 thd->mem_root= &table->mem_root; 08626 08627 table->field=reg_field; 08628 table->alias= table_alias; 08629 table->reginfo.lock_type=TL_WRITE; /* Will be updated */ 08630 table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE; 08631 table->map=1; 08632 table->temp_pool_slot = temp_pool_slot; 08633 table->copy_blobs= 1; 08634 table->in_use= thd; 08635 table->quick_keys.init(); 08636 table->used_keys.init(); 08637 table->keys_in_use_for_query.init(); 08638 08639 table->s= share; 08640 init_tmp_table_share(share, "", 0, tmpname, tmpname); 08641 share->blob_field= blob_field; 08642 share->blob_ptr_size= mi_portable_sizeof_char_ptr; 08643 share->db_low_byte_first=1; // True for HEAP and MyISAM 08644 share->table_charset= param->table_charset; 08645 share->primary_key= MAX_KEY; // Indicate no primary key 08646 share->keys_for_keyread.init(); 08647 share->keys_in_use.init(); 08648 08649 /* Calculate which type of fields we will store in the temporary table */ 08650 08651 reclength= string_total_length= 0; 08652 blob_count= string_count= null_count= hidden_null_count= group_null_items= 0; 08653 param->using_indirect_summary_function=0; 08654 08655 List_iterator_fast<Item> li(fields); 08656 Item *item; 08657 Field **tmp_from_field=from_field; 08658 while ((item=li++)) 08659 { 08660 Item::Type type=item->type(); 08661 if (not_all_columns) 08662 { 08663 if (item->with_sum_func && type != Item::SUM_FUNC_ITEM) 08664 { 08665 /* 08666 Mark that the we have ignored an item that refers to a summary 08667 function. We need to know this if someone is going to use 08668 DISTINCT on the result. 08669 */ 08670 param->using_indirect_summary_function=1; 08671 continue; 08672 } 08673 if (item->const_item() && (int) hidden_field_count <= 0) 08674 continue; // We don't have to store this 08675 } 08676 if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields) 08677 { /* Can't calc group yet */ 08678 ((Item_sum*) item)->result_field=0; 08679 for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++) 08680 { 08681 Item **argp= ((Item_sum*) item)->args + i; 08682 Item *arg= *argp; 08683 if (!arg->const_item()) 08684 { 08685 Field *new_field= 08686 create_tmp_field(thd, table, arg, arg->type(), ©_func, 08687 tmp_from_field, &default_field[fieldnr], 08688 group != 0,not_all_columns, 08689 distinct, 0, 08690 param->convert_blob_length); 08691 if (!new_field) 08692 goto err; // Should be OOM 08693 tmp_from_field++; 08694 reclength+=new_field->pack_length(); 08695 if (new_field->flags & BLOB_FLAG) 08696 { 08697 *blob_field++= fieldnr; 08698 blob_count++; 08699 } 08700 if (new_field->type() == FIELD_TYPE_BIT) 08701 total_uneven_bit_length+= new_field->field_length & 7; 08702 *(reg_field++)= new_field; 08703 if (new_field->real_type() == MYSQL_TYPE_STRING || 08704 new_field->real_type() == MYSQL_TYPE_VARCHAR) 08705 { 08706 string_count++; 08707 string_total_length+= new_field->pack_length(); 08708 } 08709 thd->mem_root= mem_root_save; 08710 thd->change_item_tree(argp, new Item_field(new_field)); 08711 thd->mem_root= &table->mem_root; 08712 if (!(new_field->flags & NOT_NULL_FLAG)) 08713 { 08714 null_count++; 08715 /* 08716 new_field->maybe_null() is still false, it will be 08717 changed below. But we have to setup Item_field correctly 08718 */ 08719 (*argp)->maybe_null=1; 08720 } 08721 new_field->field_index= fieldnr++; 08722 } 08723 } 08724 } 08725 else 08726 { 08727 /* 08728 The last parameter to create_tmp_field() is a bit tricky: 08729 08730 We need to set it to 0 in union, to get fill_record() to modify the 08731 temporary table. 08732 We need to set it to 1 on multi-table-update and in select to 08733 write rows to the temporary table. 08734 We here distinguish between UNION and multi-table-updates by the fact 08735 that in the later case group is set to the row pointer. 08736 08737 The test for item->marker == 4 is ensure we don't create a group-by 08738 key over a bit field as heap tables can't handle that. 08739 */ 08740 Field *new_field= (param->schema_table) ? 08741 create_tmp_field_for_schema(thd, item, table) : 08742 create_tmp_field(thd, table, item, type, ©_func, 08743 tmp_from_field, &default_field[fieldnr], 08744 group != 0, 08745 !force_copy_fields && 08746 (not_all_columns || group !=0), 08747 item->marker == 4, force_copy_fields, 08748 param->convert_blob_length); 08749 08750 if (!new_field) 08751 { 08752 if (thd->is_fatal_error) 08753 goto err; // Got OOM 08754 continue; // Some kindf of const item 08755 } 08756 if (type == Item::SUM_FUNC_ITEM) 08757 ((Item_sum *) item)->result_field= new_field; 08758 tmp_from_field++; 08759 reclength+=new_field->pack_length(); 08760 if (!(new_field->flags & NOT_NULL_FLAG)) 08761 null_count++; 08762 if (new_field->type() == FIELD_TYPE_BIT) 08763 total_uneven_bit_length+= new_field->field_length & 7; 08764 if (new_field->flags & BLOB_FLAG) 08765 { 08766 *blob_field++= fieldnr; 08767 blob_count++; 08768 } 08769 if (item->marker == 4 && item->maybe_null) 08770 { 08771 group_null_items++; 08772 new_field->flags|= GROUP_FLAG; 08773 } 08774 new_field->field_index= fieldnr++; 08775 *(reg_field++)= new_field; 08776 } 08777 if (!--hidden_field_count) 08778 { 08779 /* 08780 This was the last hidden field; Remember how many hidden fields could 08781 have null 08782 */ 08783 hidden_null_count=null_count; 08784 /* 08785 We need to update hidden_field_count as we may have stored group 08786 functions with constant arguments 08787 */ 08788 param->hidden_field_count= fieldnr; 08789 null_count= 0; 08790 } 08791 } 08792 DBUG_ASSERT(fieldnr == (uint) (reg_field - table->field)); 08793 DBUG_ASSERT(field_count >= (uint) (reg_field - table->field)); 08794 field_count= fieldnr; 08795 *reg_field= 0; 08796 *blob_field= 0; // End marker 08797 share->fields= field_count; 08798 08799 /* If result table is small; use a heap */ 08800 if (blob_count || using_unique_constraint || 08801 (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) == 08802 OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM)) 08803 { 08804 table->file= get_new_handler(share, &table->mem_root, 08805 share->db_type= &myisam_hton); 08806 if (group && 08807 (param->group_parts > table->file->max_key_parts() || 08808 param->group_length > table->file->max_key_length())) 08809 using_unique_constraint=1; 08810 } 08811 else 08812 { 08813 table->file= get_new_handler(share, &table->mem_root, 08814 share->db_type= &heap_hton); 08815 } 08816 if (!table->file) 08817 goto err; 08818 08819 08820 if (!using_unique_constraint) 08821 reclength+= group_null_items; // null flag is stored separately 08822 08823 share->blob_fields= blob_count; 08824 if (blob_count == 0) 08825 { 08826 /* We need to ensure that first byte is not 0 for the delete link */ 08827 if (param->hidden_field_count) 08828 hidden_null_count++; 08829 else 08830 null_count++; 08831 } 08832 hidden_null_pack_length=(hidden_null_count+7)/8; 08833 null_pack_length= (hidden_null_pack_length + 08834 (null_count + total_uneven_bit_length + 7) / 8); 08835 reclength+=null_pack_length; 08836 if (!reclength) 08837 reclength=1; // Dummy select 08838 /* Use packed rows if there is blobs or a lot of space to gain */ 08839 if (blob_count || 08840 string_total_length >= STRING_TOTAL_LENGTH_TO_PACK_ROWS && 08841 (reclength / string_total_length <= RATIO_TO_PACK_ROWS || 08842 string_total_length / string_count >= AVG_STRING_LENGTH_TO_PACK_ROWS)) 08843 use_packed_rows= 1; 08844 08845 share->reclength= reclength; 08846 { 08847 uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1); 08848 share->rec_buff_length= alloc_length; 08849 if (!(table->record[0]= (byte*) 08850 alloc_root(&table->mem_root, alloc_length*3))) 08851 goto err; 08852 table->record[1]= table->record[0]+alloc_length; 08853 share->default_values= table->record[1]+alloc_length; 08854 } 08855 copy_func[0]=0; // End marker 08856 08857 setup_tmp_table_column_bitmaps(table, bitmaps); 08858 08859 recinfo=param->start_recinfo; 08860 null_flags=(uchar*) table->record[0]; 08861 pos=table->record[0]+ null_pack_length; 08862 if (null_pack_length) 08863 { 08864 bzero((byte*) recinfo,sizeof(*recinfo)); 08865 recinfo->type=FIELD_NORMAL; 08866 recinfo->length=null_pack_length; 08867 recinfo++; 08868 bfill(null_flags,null_pack_length,255); // Set null fields 08869 08870 table->null_flags= (uchar*) table->record[0]; 08871 share->null_fields= null_count+ hidden_null_count; 08872 share->null_bytes= null_pack_length; 08873 } 08874 null_count= (blob_count == 0) ? 1 : 0; 08875 hidden_field_count=param->hidden_field_count; 08876 for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++) 08877 { 08878 Field *field= *reg_field; 08879 uint length; 08880 bzero((byte*) recinfo,sizeof(*recinfo)); 08881 08882 if (!(field->flags & NOT_NULL_FLAG)) 08883 { 08884 if (field->flags & GROUP_FLAG && !using_unique_constraint) 08885 { 08886 /* 08887 We have to reserve one byte here for NULL bits, 08888 as this is updated by 'end_update()' 08889 */ 08890 *pos++=0; // Null is stored here 08891 recinfo->length=1; 08892 recinfo->type=FIELD_NORMAL; 08893 recinfo++; 08894 bzero((byte*) recinfo,sizeof(*recinfo)); 08895 } 08896 else 08897 { 08898 recinfo->null_bit= 1 << (null_count & 7); 08899 recinfo->null_pos= null_count/8; 08900 } 08901 field->move_field((char*) pos,null_flags+null_count/8, 08902 1 << (null_count & 7)); 08903 null_count++; 08904 } 08905 else 08906 field->move_field((char*) pos,(uchar*) 0,0); 08907 if (field->type() == FIELD_TYPE_BIT) 08908 { 08909 /* We have to reserve place for extra bits among null bits */ 08910 ((Field_bit*) field)->set_bit_ptr(null_flags + null_count / 8, 08911 null_count & 7); 08912 null_count+= (field->field_length & 7); 08913 } 08914 field->reset(); 08915 08916 /* 08917 Test if there is a default field value. The test for ->ptr is to skip 08918 'offset' fields generated by initalize_tables 08919 */ 08920 if (default_field[i] && default_field[i]->ptr) 08921 { 08922 /* 08923 default_field[i] is set only in the cases when 'field' can 08924 inherit the default value that is defined for the field referred 08925 by the Item_field object from which 'field' has been created. 08926 */ 08927 my_ptrdiff_t diff; 08928 Field *orig_field= default_field[i]; 08929 /* Get the value from default_values */ 08930 diff= (my_ptrdiff_t) (orig_field->table->s->default_values- 08931 orig_field->table->record[0]); 08932 orig_field->move_field_offset(diff); // Points now at default_values 08933 if (orig_field->is_real_null()) 08934 field->set_null(); 08935 else 08936 { 08937 field->set_notnull(); 08938 memcpy(field->ptr, orig_field->ptr, field->pack_length()); 08939 } 08940 orig_field->move_field_offset(-diff); // Back to record[0] 08941 } 08942 08943 if (from_field[i]) 08944 { /* Not a table Item */ 08945 copy->set(field,from_field[i],save_sum_fields); 08946 copy++; 08947 } 08948 length=field->pack_length(); 08949 pos+= length; 08950 08951 /* Make entry for create table */ 08952 recinfo->length=length; 08953 if (field->flags & BLOB_FLAG) 08954 recinfo->type= (int) FIELD_BLOB; 08955 else if (use_packed_rows && 08956 field->real_type() == MYSQL_TYPE_STRING && 08957 length >= MIN_STRING_LENGTH_TO_PACK_ROWS) 08958 recinfo->type=FIELD_SKIP_ENDSPACE; 08959 else 08960 recinfo->type=FIELD_NORMAL; 08961 if (!--hidden_field_count) 08962 null_count=(null_count+7) & ~7; // move to next byte 08963 08964 // fix table name in field entry 08965 field->table_name= &table->alias; 08966 } 08967 08968 param->copy_field_end=copy; 08969 param->recinfo=recinfo; 08970 store_record(table,s->default_values); // Make empty default record 08971 08972 if (thd->variables.tmp_table_size == ~(ulong) 0) // No limit 08973 share->max_rows= ~(ha_rows) 0; 08974 else 08975 share->max_rows= (((share->db_type == &heap_hton) ? 08976 min(thd->variables.tmp_table_size, 08977 thd->variables.max_heap_table_size) : 08978 thd->variables.tmp_table_size)/ share->reclength); 08979 set_if_bigger(share->max_rows,1); // For dummy start options 08980 keyinfo= param->keyinfo; 08981 08982 if (group) 08983 { 08984 DBUG_PRINT("info",("Creating group key in temporary table")); 08985 table->group=group; /* Table is grouped by key */ 08986 param->group_buff=group_buff; 08987 share->keys=1; 08988 share->uniques= test(using_unique_constraint); 08989 table->key_info=keyinfo; 08990 keyinfo->key_part=key_part_info; 08991 keyinfo->flags=HA_NOSAME; 08992 keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts; 08993 keyinfo->key_length=0; 08994 keyinfo->rec_per_key=0; 08995 keyinfo->algorithm= HA_KEY_ALG_UNDEF; 08996 keyinfo->name= (char*) "group_key"; 08997 for (; group ; group=group->next,key_part_info++) 08998 { 08999 Field *field=(*group->item)->get_tmp_table_field(); 09000 bool maybe_null=(*group->item)->maybe_null; 09001 key_part_info->null_bit=0; 09002 key_part_info->field= field; 09003 key_part_info->offset= field->offset(); 09004 key_part_info->length= (uint16) field->key_length(); 09005 key_part_info->type= (uint8) field->key_type(); 09006 key_part_info->key_type = 09007 ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT || 09008 (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 || 09009 (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ? 09010 0 : FIELDFLAG_BINARY; 09011 if (!using_unique_constraint) 09012 { 09013 group->buff=(char*) group_buff; 09014 if (!(group->field= field->new_key_field(thd->mem_root,table, 09015 (char*) group_buff + 09016 test(maybe_null), 09017 field->null_ptr, 09018 field->null_bit))) 09019 goto err; /* purecov: inspected */ 09020 if (maybe_null) 09021 { 09022 /* 09023 To be able to group on NULL, we reserved place in group_buff 09024 for the NULL flag just before the column. (see above). 09025 The field data is after this flag. 09026 The NULL flag is updated in 'end_update()' and 'end_write()' 09027 */ 09028 keyinfo->flags|= HA_NULL_ARE_EQUAL; // def. that NULL == NULL 09029 key_part_info->null_bit=field->null_bit; 09030 key_part_info->null_offset= (uint) (field->null_ptr - 09031 (uchar*) table->record[0]); 09032 group->buff++; // Pointer to field data 09033 group_buff++; // Skipp null flag 09034 } 09035 /* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */ 09036 key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; 09037 group_buff+= group->field->pack_length(); 09038 } 09039 keyinfo->key_length+= key_part_info->length; 09040 } 09041 } 09042 else 09043 { 09044 set_if_smaller(table->s->max_rows, rows_limit); 09045 param->end_write_records= rows_limit; 09046 } 09047 09048 if (distinct && field_count != param->hidden_field_count) 09049 { 09050 /* 09051 Create an unique key or an unique constraint over all columns 09052 that should be in the result. In the temporary table, there are 09053 'param->hidden_field_count' extra columns, whose null bits are stored 09054 in the first 'hidden_null_pack_length' bytes of the row. 09055 */ 09056 DBUG_PRINT("info",("hidden_field_count: %d", param->hidden_field_count)); 09057 09058 null_pack_length-=hidden_null_pack_length; 09059 keyinfo->key_parts= ((field_count-param->hidden_field_count)+ 09060 test(null_pack_length)); 09061 set_if_smaller(share->max_rows, rows_limit); 09062 param->end_write_records= rows_limit; 09063 table->distinct= 1; 09064 share->keys= 1; 09065 if (blob_count) 09066 { 09067 using_unique_constraint=1; 09068 share->uniques= 1; 09069 } 09070 if (!(key_part_info= (KEY_PART_INFO*) 09071 alloc_root(&table->mem_root, 09072 keyinfo->key_parts * sizeof(KEY_PART_INFO)))) 09073 goto err; 09074 bzero((void*) key_part_info, keyinfo->key_parts * sizeof(KEY_PART_INFO)); 09075 table->key_info=keyinfo; 09076 keyinfo->key_part=key_part_info; 09077 keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL; 09078 keyinfo->key_length=(uint16) reclength; 09079 keyinfo->name= (char*) "distinct_key"; 09080 keyinfo->algorithm= HA_KEY_ALG_UNDEF; 09081 keyinfo->rec_per_key=0; 09082 if (null_pack_length) 09083 { 09084 key_part_info->null_bit=0; 09085 key_part_info->offset=hidden_null_pack_length; 09086 key_part_info->length=null_pack_length; 09087 key_part_info->field= new Field_string((char*) table->record[0], 09088 (uint32) key_part_info->length, 09089 (uchar*) 0, 09090 (uint) 0, 09091 Field::NONE, 09092 NullS, &my_charset_bin); 09093 if (!key_part_info->field) 09094 goto err; 09095 key_part_info->field->init(table); 09096 key_part_info->key_type=FIELDFLAG_BINARY; 09097 key_part_info->type= HA_KEYTYPE_BINARY; 09098 key_part_info++; 09099 } 09100 /* Create a distinct key over the columns we are going to return */ 09101 for (i=param->hidden_field_count, reg_field=table->field + i ; 09102 i < field_count; 09103 i++, reg_field++, key_part_info++) 09104 { 09105 key_part_info->null_bit=0; 09106 key_part_info->field= *reg_field; 09107 key_part_info->offset= (*reg_field)->offset(); 09108 key_part_info->length= (uint16) (*reg_field)->pack_length(); 09109 key_part_info->type= (uint8) (*reg_field)->key_type(); 09110 key_part_info->key_type = 09111 ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT || 09112 (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 || 09113 (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ? 09114 0 : FIELDFLAG_BINARY; 09115 } 09116 } 09117 if (thd->is_fatal_error) // If end of memory 09118 goto err; /* purecov: inspected */ 09119 share->db_record_offset= 1; 09120 if (share->db_type == &myisam_hton) 09121 { 09122 if (create_myisam_tmp_table(table,param,select_options)) 09123 goto err; 09124 } 09125 if (open_tmp_table(table)) 09126 goto err; 09127 09128 thd->mem_root= mem_root_save; 09129 09130 DBUG_RETURN(table); 09131 09132 err: 09133 thd->mem_root= mem_root_save; 09134 free_tmp_table(thd,table); /* purecov: inspected */ 09135 if (temp_pool_slot != MY_BIT_NONE) 09136 bitmap_lock_clear_bit(&temp_pool, temp_pool_slot); 09137 DBUG_RETURN(NULL); /* purecov: inspected */ 09138 }
Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 5788 of file sql_select.cc.
References ER, ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE, JOIN::join_tab, JT_ALL, my_message(), MYF, SQL_SELECT::quick, st_join_table::select, JOIN::tables, and st_join_table::type.
05789 { 05790 for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables; 05791 tab < end; 05792 tab++) 05793 { 05794 if (tab->type == JT_ALL && (!tab->select || !tab->select->quick)) 05795 { 05796 my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE, 05797 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0)); 05798 return(1); 05799 } 05800 } 05801 return(0); 05802 }
Here is the call graph for this function:

| void free_tmp_table | ( | THD * | thd, | |
| TABLE * | entry | |||
| ) |
Definition at line 9400 of file sql_select.cc.
References bitmap_lock_clear_bit(), DBUG_ENTER, DBUG_PRINT, DBUG_VOID_RETURN, free_io_cache(), free_root(), MY_BIT_NONE, MYF, _entry::str, and temp_pool.
Referenced by Item_sum_count_distinct::cleanup(), Item_func_group_concat::cleanup(), Materialized_cursor::close(), close_thread_tables(), create_tmp_table(), JOIN::destroy(), mysql_derived_prepare(), and mysql_open_cursor().
09401 { 09402 MEM_ROOT own_root= entry->mem_root; 09403 const char *save_proc_info; 09404 DBUG_ENTER("free_tmp_table"); 09405 DBUG_PRINT("enter",("table: %s",entry->alias)); 09406 09407 save_proc_info=thd->proc_info; 09408 thd->proc_info="removing tmp table"; 09409 09410 if (entry->file) 09411 { 09412 if (entry->db_stat) 09413 entry->file->drop_table(entry->s->table_name.str); 09414 else 09415 entry->file->delete_table(entry->s->table_name.str); 09416 delete entry->file; 09417 } 09418 09419 /* free blobs */ 09420 for (Field **ptr=entry->field ; *ptr ; ptr++) 09421 (*ptr)->free(); 09422 free_io_cache(entry); 09423 09424 if (entry->temp_pool_slot != MY_BIT_NONE) 09425 bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot); 09426 09427 free_root(&own_root, MYF(0)); /* the table is allocated in its own root */ 09428 thd->proc_info=save_proc_info; 09429 09430 DBUG_VOID_RETURN; 09431 }
Here is the call graph for this function:

Here is the caller graph for this function:

| int opt_sum_query | ( | TABLE_LIST * | tables, | |
| List< Item > & | all_fields, | |||
| COND * | conds | |||
| ) |
Definition at line 108 of file opt_sum.cc.
References args, Item::const_item(), count, Item_sum::COUNT_FUNC, error, handler::extra(), FALSE, Item_field::field, Item::FIELD_ITEM, st_table::file, find_key_for_maxmin(), get_exact_record_count(), HA_ERR_END_OF_FILE, HA_ERR_KEY_NOT_FOUND, HA_EXTRA_NO_KEYREAD, HA_HAS_RECORDS, handler::ha_index_end(), handler::ha_index_init(), HA_READ_AFTER_KEY, HA_READ_BEFORE_KEY, HA_READ_KEY_OR_NEXT, HA_READ_PREFIX_LAST_OR_PREV, HA_STATS_RECORDS_IS_EXACT, HA_STATUS_NO_LOCK, HA_STATUS_VARIABLE, handler::index_first(), handler::index_last(), handler::index_read(), st_table_ref::key, st_table_ref::key_buff, st_table_ref::key_length, st_table::key_read, st_table::map, Item_sum::MAX_FUNC, MAX_KEY_LENGTH, Item_sum::MIN_FUNC, MYF, NEAR_MAX, NEAR_MIN, st_table_list::next_leaf, handler::print_error(), Item::real_item(), reckey_in_range(), st_table::record, Item::SUM_FUNC_ITEM, Field::table, test, TRUE, Item::type(), ULONGLONG_MAX, Item::update_used_tables(), and Item::used_tables().
Referenced by JOIN::optimize().
00109 { 00110 List_iterator_fast<Item> it(all_fields); 00111 int const_result= 1; 00112 bool recalc_const_item= 0; 00113 ulonglong count= 1; 00114 bool is_exact_count= TRUE, maybe_exact_count= TRUE; 00115 table_map removed_tables= 0, outer_tables= 0, used_tables= 0; 00116 table_map where_tables= 0; 00117 Item *item; 00118 int error; 00119 00120 if (conds) 00121 where_tables= conds->used_tables(); 00122 00123 /* 00124 Analyze outer join dependencies, and, if possible, compute the number 00125 of returned rows. 00126 */ 00127 for (TABLE_LIST *tl= tables; tl; tl= tl->next_leaf) 00128 { 00129 TABLE_LIST *embedded; 00130 for (embedded= tl ; embedded; embedded= embedded->embedding) 00131 { 00132 if (embedded->on_expr) 00133 break; 00134 } 00135 if (embedded) 00136 /* Don't replace expression on a table that is part of an outer join */ 00137 { 00138 outer_tables|= tl->table->map; 00139 00140 /* 00141 We can't optimise LEFT JOIN in cases where the WHERE condition 00142 restricts the table that is used, like in: 00143 SELECT MAX(t1.a) FROM t1 LEFT JOIN t2 join-condition 00144 WHERE t2.field IS NULL; 00145 */ 00146 if (tl->table->map & where_tables) 00147 return 0; 00148 } 00149 else 00150 used_tables|= tl->table->map; 00151 00152 /* 00153 If the storage manager of 'tl' gives exact row count as part of 00154 statistics (cheap), compute the total number of rows. If there are 00155 no outer table dependencies, this count may be used as the real count. 00156 Schema tables are filled after this function is invoked, so we can't 00157 get row count 00158 */ 00159 if (!(tl->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) || 00160 tl->schema_table) 00161 { 00162 maybe_exact_count&= test(!tl->schema_table && 00163 (tl->table->file->ha_table_flags() & 00164 HA_HAS_RECORDS)); 00165 is_exact_count= FALSE; 00166 count= 1; // ensure count != 0 00167 } 00168 else 00169 { 00170 tl->table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK); 00171 count*= tl->table->file->stats.records; 00172 } 00173 } 00174 00175 /* 00176 Iterate through all items in the SELECT clause and replace 00177 COUNT(), MIN() and MAX() with constants (if possible). 00178 */ 00179 00180 while ((item= it++)) 00181 { 00182 if (item->type() == Item::SUM_FUNC_ITEM) 00183 { 00184 Item_sum *item_sum= (((Item_sum*) item)); 00185 switch (item_sum->sum_func()) { 00186 case Item_sum::COUNT_FUNC: 00187 /* 00188 If the expr in COUNT(expr) can never be null we can change this 00189 to the number of rows in the tables if this number is exact and 00190 there are no outer joins. 00191 */ 00192 if (!conds && !((Item_sum_count*) item)->args[0]->maybe_null && 00193 !outer_tables && maybe_exact_count) 00194 { 00195 if (!is_exact_count) 00196 { 00197 if ((count= get_exact_record_count(tables)) == ULONGLONG_MAX) 00198 { 00199 /* Error from handler in counting rows. Don't optimize count() */ 00200 const_result= 0; 00201 continue; 00202 } 00203 is_exact_count= 1; // count is now exact 00204 } 00205 ((Item_sum_count*) item)->make_const((longlong) count); 00206 recalc_const_item= 1; 00207 } 00208 else 00209 const_result= 0; 00210 break; 00211 case Item_sum::MIN_FUNC: 00212 { 00213 /* 00214 If MIN(expr) is the first part of a key or if all previous 00215 parts of the key is found in the COND, then we can use 00216 indexes to find the key. 00217 */ 00218 Item *expr=item_sum->args[0]; 00219 if (expr->real_item()->type() == Item::FIELD_ITEM) 00220 { 00221 byte key_buff[MAX_KEY_LENGTH]; 00222 TABLE_REF ref; 00223 uint range_fl, prefix_len; 00224 00225 ref.key_buff= key_buff; 00226 Item_field *item_field= (Item_field*) (expr->real_item()); 00227 TABLE *table= item_field->field->table; 00228 00229 /* 00230 Look for a partial key that can be used for optimization. 00231 If we succeed, ref.key_length will contain the length of 00232 this key, while prefix_len will contain the length of 00233 the beginning of this key without field used in MIN(). 00234 Type of range for the key part for this field will be 00235 returned in range_fl. 00236 */ 00237 if ((outer_tables & table->map) || 00238 !find_key_for_maxmin(0, &ref, item_field->field, conds, 00239 &range_fl, &prefix_len)) 00240 { 00241 const_result= 0; 00242 break; 00243 } 00244 error= table->file->ha_index_init((uint) ref.key, 1); 00245 00246 if (!ref.key_length) 00247 error= table->file->index_first(table->record[0]); 00248 else 00249 error= table->file->index_read(table->record[0],key_buff, 00250 ref.key_length, 00251 range_fl & NEAR_MIN ? 00252 HA_READ_AFTER_KEY : 00253 HA_READ_KEY_OR_NEXT); 00254 if (!error && reckey_in_range(0, &ref, item_field->field, 00255 conds, range_fl, prefix_len)) 00256 error= HA_ERR_KEY_NOT_FOUND; 00257 if (table->key_read) 00258 { 00259 table->key_read= 0; 00260 table->file->extra(HA_EXTRA_NO_KEYREAD); 00261 } 00262 table->file->ha_index_end(); 00263 if (error) 00264 { 00265 if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE) 00266 return -1; // No rows matching WHERE 00267 /* HA_ERR_LOCK_DEADLOCK or some other error */ 00268 table->file->print_error(error, MYF(0)); 00269 return(error); 00270 } 00271 removed_tables|= table->map; 00272 } 00273 else if (!expr->const_item() || !is_exact_count) 00274 { 00275 /* 00276 The optimization is not applicable in both cases: 00277 (a) 'expr' is a non-constant expression. Then we can't 00278 replace 'expr' by a constant. 00279 (b) 'expr' is a costant. According to ANSI, MIN/MAX must return 00280 NULL if the query does not return any rows. Thus, if we are not 00281 able to determine if the query returns any rows, we can't apply 00282 the optimization and replace MIN/MAX with a constant. 00283 */ 00284 const_result= 0; 00285 break; 00286 } 00287 if (!count) 00288 { 00289 /* If count == 0, then we know that is_exact_count == TRUE. */ 00290 ((Item_sum_min*) item_sum)->clear(); /* Set to NULL. */ 00291 } 00292 else 00293 ((Item_sum_min*) item_sum)->reset(); /* Set to the constant value. */ 00294 ((Item_sum_min*) item_sum)->make_const(); 00295 recalc_const_item= 1; 00296 break; 00297 } 00298 case Item_sum::MAX_FUNC: 00299 { 00300 /* 00301 If MAX(expr) is the first part of a key or if all previous 00302 parts of the key is found in the COND, then we can use 00303 indexes to find the key. 00304 */ 00305 Item *expr=item_sum->args[0]; 00306 if (expr->real_item()->type() == Item::FIELD_ITEM) 00307 { 00308 byte key_buff[MAX_KEY_LENGTH]; 00309 TABLE_REF ref; 00310 uint range_fl, prefix_len; 00311 00312 ref.key_buff= key_buff; 00313 Item_field *item_field= (Item_field*) (expr->real_item()); 00314 TABLE *table= item_field->field->table; 00315 00316 /* 00317 Look for a partial key that can be used for optimization. 00318 If we succeed, ref.key_length will contain the length of 00319 this key, while prefix_len will contain the length of 00320 the beginning of this key without field used in MAX(). 00321 Type of range for the key part for this field will be 00322 returned in range_fl. 00323 */ 00324 if ((outer_tables & table->map) || 00325 !find_key_for_maxmin(1, &ref, item_field->field, conds, 00326 &range_fl, &prefix_len)) 00327 { 00328 const_result= 0; 00329 break; 00330 } 00331 error= table->file->ha_index_init((uint) ref.key, 1); 00332 00333 if (!ref.key_length) 00334 error= table->file->index_last(table->record[0]); 00335 else 00336 error= table->file->index_read(table->record[0], key_buff, 00337 ref.key_length, 00338 range_fl & NEAR_MAX ? 00339 HA_READ_BEFORE_KEY : 00340 HA_READ_PREFIX_LAST_OR_PREV); 00341 if (!error && reckey_in_range(1, &ref, item_field->field, 00342 conds, range_fl, prefix_len)) 00343 error= HA_ERR_KEY_NOT_FOUND; 00344 if (table->key_read) 00345 { 00346 table->key_read=0; 00347 table->file->extra(HA_EXTRA_NO_KEYREAD); 00348 } 00349 table->file->ha_index_end(); 00350 if (error) 00351 { 00352 if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE) 00353 return -1; // No rows matching WHERE 00354 /* HA_ERR_LOCK_DEADLOCK or some other error */ 00355 table->file->print_error(error, MYF(0)); 00356 return(error); 00357 } 00358 removed_tables|= table->map; 00359 } 00360 else if (!expr->const_item() || !is_exact_count) 00361 { 00362 /* 00363 The optimization is not applicable in both cases: 00364 (a) 'expr' is a non-constant expression. Then we can't 00365 replace 'expr' by a constant. 00366 (b) 'expr' is a costant. According to ANSI, MIN/MAX must return 00367 NULL if the query does not return any rows. Thus, if we are not 00368 able to determine if the query returns any rows, we can't apply 00369 the optimization and replace MIN/MAX with a constant. 00370 */ 00371 const_result= 0; 00372 break; 00373 } 00374 if (!count) 00375 { 00376 /* If count != 1, then we know that is_exact_count == TRUE. */ 00377 ((Item_sum_max*) item_sum)->clear(); /* Set to NULL. */ 00378 } 00379 else 00380 ((Item_sum_max*) item_sum)->reset(); /* Set to the constant value. */ 00381 ((Item_sum_max*) item_sum)->make_const(); 00382 recalc_const_item= 1; 00383 break; 00384 } 00385 default: 00386 const_result= 0; 00387 break; 00388 } 00389 } 00390 else if (const_result) 00391 { 00392 if (recalc_const_item) 00393 item->update_used_tables(); 00394 if (!item->const_item()) 00395 const_result= 0; 00396 } 00397 } 00398 /* 00399 If we have a where clause, we can only ignore searching in the 00400 tables if MIN/MAX optimisation replaced all used tables 00401 We do not use replaced values in case of: 00402 SELECT MIN(key) FROM table_1, empty_table 00403 removed_tables is != 0 if we have used MIN() or MAX(). 00404 */ 00405 if (removed_tables && used_tables != removed_tables) 00406 const_result= 0; // We didn't remove all tables 00407 return const_result; 00408 }
Here is the call graph for this function:

Here is the caller graph for this function:

| int refpos_order_cmp | ( | void * | arg, | |
| const void * | a, | |||
| const void * | b | |||
| ) |
Definition at line 417 of file sql_delete.cc.
References handler::cmp_ref().
Referenced by QUICK_INDEX_MERGE_SELECT::read_keys_and_merge().
00418 { 00419 handler *file= (handler*)arg; 00420 return file->cmp_ref((const byte*)a, (const byte*)b); 00421 }
Here is the call graph for this function:

Here is the caller graph for this function:

| COND* remove_eq_conds | ( | THD * | thd, | |
| COND * | cond, | |||
| Item::cond_result * | cond_value | |||
| ) |
Definition at line 7903 of file sql_select.cc.
References args, AUTO_INCREMENT_FLAG, cond, Item_func::COND_AND_FUNC, Item::COND_FALSE, Item::COND_ITEM, Item::COND_OK, Item::COND_TRUE, Item::COND_UNDEF, Item::eq(), Item_func::EQUAL_FUNC, eval_const_cond(), FALSE, Item::FIELD_ITEM, FIELD_TYPE_DATE, FIELD_TYPE_DATETIME, Field::flags, func, Item::FUNC_ITEM, Item_func::ISNULL_FUNC, Item::maybe_null, st_table::maybe_null, NOT_NULL_FLAG, OPTION_AUTO_IS_NULL, query_cache_abort(), List_iterator< T >::remove(), remove_eq_conds(), List_iterator< T >::replace(), Field::table, Field::type(), and VOID.
Referenced by mysql_delete(), optimize_cond(), and remove_eq_conds().
07904 { 07905 if (cond->type() == Item::COND_ITEM) 07906 { 07907 bool and_level= ((Item_cond*) cond)->functype() 07908 == Item_func::COND_AND_FUNC; 07909 List_iterator<Item> li(*((Item_cond*) cond)->argument_list()); 07910 Item::cond_result tmp_cond_value; 07911 bool should_fix_fields=0; 07912 07913 *cond_value=Item::COND_UNDEF; 07914 Item *item; 07915 while ((item=li++)) 07916 { 07917 Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value); 07918 if (!new_item) 07919 li.remove(); 07920 else if (item != new_item) 07921 { 07922 VOID(li.replace(new_item)); 07923 should_fix_fields=1; 07924 } 07925 if (*cond_value == Item::COND_UNDEF) 07926 *cond_value=tmp_cond_value; 07927 switch (tmp_cond_value) { 07928 case Item::COND_OK: // Not TRUE or FALSE 07929 if (and_level || *cond_value == Item::COND_FALSE) 07930 *cond_value=tmp_cond_value; 07931 break; 07932 case Item::COND_FALSE: 07933 if (and_level) 07934 { 07935 *cond_value=tmp_cond_value; 07936 return (COND*) 0; // Always false 07937 } 07938 break; 07939 case Item::COND_TRUE: 07940 if (!and_level) 07941 { 07942 *cond_value= tmp_cond_value; 07943 return (COND*) 0; // Always true 07944 } 07945 break; 07946 case Item::COND_UNDEF: // Impossible 07947 break; /* purecov: deadcode */ 07948 } 07949 } 07950 if (should_fix_fields) 07951 cond->update_used_tables(); 07952 07953 if (!((Item_cond*) cond)->argument_list()->elements || 07954 *cond_value != Item::COND_OK) 07955 return (COND*) 0; 07956 if (((Item_cond*) cond)->argument_list()->elements == 1) 07957 { // Remove list 07958 item= ((Item_cond*) cond)->argument_list()->head(); 07959 ((Item_cond*) cond)->argument_list()->empty(); 07960 return item; 07961 } 07962 } 07963 else if (cond->type() == Item::FUNC_ITEM && 07964 ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC) 07965 { 07966 /* 07967 Handles this special case for some ODBC applications: 07968 The are requesting the row that was just updated with a auto_increment 07969 value with this construct: 07970 07971 SELECT * from table_name where auto_increment_column IS NULL 07972 This will be changed to: 07973 SELECT * from table_name where auto_increment_column = LAST_INSERT_ID 07974 */ 07975 07976 Item_func_isnull *func=(Item_func_isnull*) cond; 07977 Item **args= func->arguments(); 07978 if (args[0]->type() == Item::FIELD_ITEM) 07979 { 07980 Field *field=((Item_field*) args[0])->field; 07981 if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null && 07982 (thd->options & OPTION_AUTO_IS_NULL) && 07983 (thd->first_successful_insert_id_in_prev_stmt > 0 && 07984 thd->substitute_null_with_insert_id)) 07985 { 07986 #ifdef HAVE_QUERY_CACHE 07987 query_cache_abort(&thd->net); 07988 #endif 07989 COND *new_cond; 07990 if ((new_cond= new Item_func_eq(args[0], 07991 new Item_int("last_insert_id()", 07992 thd->read_first_successful_insert_id_in_prev_stmt(), 07993 21)))) 07994 { 07995 cond=new_cond; 07996 /* 07997 Item_func_eq can't be fixed after creation so we do not check 07998 cond->fixed, also it do not need tables so we use 0 as second 07999 argument. 08000 */ 08001 cond->fix_fields(thd, &cond); 08002 } 08003 /* 08004 IS NULL should be mapped to LAST_INSERT_ID only for first row, so 08005 clear for next row 08006 */ 08007 thd->substitute_null_with_insert_id= FALSE; 08008 } 08009 /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */ 08010 else if (((field->type() == FIELD_TYPE_DATE) || 08011 (field->type() == FIELD_TYPE_DATETIME)) && 08012 (field->flags & NOT_NULL_FLAG) && 08013 !field->table->maybe_null) 08014 { 08015 COND *new_cond; 08016 if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2)))) 08017 { 08018 cond=new_cond; 08019 /* 08020 Item_func_eq can't be fixed after creation so we do not check 08021 cond->fixed, also it do not need tables so we use 0 as second 08022 argument. 08023 */ 08024 cond->fix_fields(thd, &cond); 08025 } 08026 } 08027 } 08028 if (cond->const_item()) 08029 { 08030 *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE; 08031 return (COND*) 0; 08032 } 08033 } 08034 else if (cond->const_item()) 08035 { 08036 *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE; 08037 return (COND*) 0; 08038 } 08039 else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK) 08040 { // boolan compare function 08041 Item *left_item= ((Item_func*) cond)->arguments()[0]; 08042 Item *right_item= ((Item_func*) cond)->arguments()[1]; 08043 if (left_item->eq(right_item,1)) 08044 { 08045 if (!left_item->maybe_null || 08046 ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC) 08047 return (COND*) 0; // Compare of identical items 08048 } 08049 } 08050 *cond_value=Item::COND_OK; 08051 return cond; // Point at next and level 08052 }
Here is the call graph for this function:

Here is the caller graph for this function:

| int report_error | ( | TABLE * | table, | |
| int | error | |||
| ) |
Definition at line 10195 of file sql_select.cc.
References st_table::file, HA_ERR_END_OF_FILE, HA_ERR_KEY_NOT_FOUND, HA_ERR_LOCK_DEADLOCK, HA_ERR_LOCK_WAIT_TIMEOUT, MYF, st_table_share::path, handler::print_error(), st_table::s, sql_print_error(), st_table::status, and STATUS_GARBAGE.
Referenced by subselect_uniquesubquery_engine::exec(), subselect_indexsubquery_engine::exec(), join_ft_read_first(), join_ft_read_next(), join_read_always_key(), join_read_const(), join_read_first(), join_read_key(), join_read_last(), join_read_last_key(), join_read_next(), join_read_next_same(), join_read_prev(), join_read_prev_same(), join_read_system(), safe_index_read(), and sub_select().
10196 { 10197 if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND) 10198 { 10199 table->status= STATUS_GARBAGE; 10200 return -1; // key not found; ok 10201 } 10202 /* 10203 Locking reads can legally return also these errors, do not 10204 print them to the .err log 10205 */ 10206 if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT) 10207 sql_print_error("Got error %d when reading table '%s'", 10208 error, table->s->path); 10209 table->file->print_error(error,MYF(0)); 10210 return 1; 10211 }
Here is the call graph for this function:

Here is the caller graph for this function:

| int safe_index_read | ( | JOIN_TAB * | tab | ) |
Definition at line 10214 of file sql_select.cc.
References error, st_table::file, HA_READ_KEY_EXACT, handler::index_read(), st_table_ref::key_buff, st_table_ref::key_length, st_table::record, st_join_table::ref, report_error(), and st_join_table::table.
Referenced by subselect_indexsubquery_engine::exec(), join_read_always_key_or_null(), and join_read_next_same_or_null().
10215 { 10216 int error; 10217 TABLE *table= tab->table; 10218 if ((error=table->file->index_read(table->record[0], 10219 tab->ref.key_buff, 10220 tab->ref.key_length, HA_READ_KEY_EXACT))) 10221 return report_error(table, error); 10222 return 0; 10223 }
Here is the call graph for this function:

Here is the caller graph for this function:

| bool setup_copy_fields | ( | THD * | thd, | |
| TMP_TABLE_PARAM * | param, | |||
| Item ** | ref_pointer_array, | |||
| List< Item > & | new_list1, | |||
| List< Item > & | new_list2, | |||
| uint | elements, | |||
| List< Item > & | fields | |||
| ) |
Definition at line 13230 of file sql_select.cc.
References BLOB_FLAG, Item::CACHE_ITEM, Item::COND_ITEM, mySTL::copy(), DBUG_ENTER, DBUG_RETURN, base_list::elements, base_list::empty(), err, Item::FIELD_ITEM, Item::FUNC_ITEM, Field::new_field(), NULL, Field::pack_length(), pos(), List< T >::push_back(), sql_alloc(), List_iterator_fast< T >::sublist(), Item::SUBSELECT_ITEM, Field::table, and TRUE.
Referenced by JOIN::exec().
13234 { 13235 Item *pos; 13236 List_iterator_fast<Item> li(all_fields); 13237 Copy_field *copy= NULL; 13238 res_selected_fields.empty(); 13239 res_all_fields.empty(); 13240 List_iterator_fast<Item> itr(res_all_fields); 13241 List<Item> extra_funcs; 13242 uint i, border= all_fields.elements - elements; 13243 DBUG_ENTER("setup_copy_fields"); 13244 13245 if (param->field_count && 13246 !(copy=param->copy_field= new Copy_field[param->field_count])) 13247 goto err2; 13248 13249 param->copy_funcs.empty(); 13250 for (i= 0; (pos= li++); i++) 13251 { 13252 Item *real_pos= pos->real_item(); 13253 if (real_pos->type() == Item::FIELD_ITEM) 13254 { 13255 Item_field *item; 13256 pos= real_pos; 13257 if (!(item= new Item_field(thd, ((Item_field*) pos)))) 13258 goto err; 13259 pos= item; 13260 if (item->field->flags & BLOB_FLAG) 13261 { 13262 if (!(pos= new Item_copy_string(pos))) 13263 goto err; 13264 /* 13265 Item_copy_string::copy for function can call 13266 Item_copy_string::val_int for blob via Item_ref. 13267 But if Item_copy_string::copy for blob isn't called before, 13268 it's value will be wrong 13269 so let's insert Item_copy_string for blobs in the beginning of 13270 copy_funcs 13271 (to see full test case look at having.test, BUG #4358) 13272 */ 13273 if (param->copy_funcs.push_front(pos)) 13274 goto err; 13275 } 13276 else 13277 { 13278 /* 13279 set up save buffer and change result_field to point at 13280 saved value 13281 */ 13282 Field *field= item->field; 13283 item->result_field=field->new_field(thd->mem_root,field->table, 1); 13284 char *tmp=(char*) sql_alloc(field->pack_length()+1); 13285 if (!tmp) 13286 goto err; 13287 if (copy) 13288 { 13289 copy->set(tmp, item->result_field); 13290 item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1); 13291 copy++; 13292 } 13293 } 13294 } 13295 else if ((real_pos->type() == Item::FUNC_ITEM || 13296 real_pos->type() == Item::SUBSELECT_ITEM || 13297 real_pos->type() == Item::CACHE_ITEM || 13298 real_pos->type() == Item::COND_ITEM) && 13299 !real_pos->with_sum_func) 13300 { // Save for send fields 13301 pos= real_pos; 13302 /* TODO: 13303 In most cases this result will be sent to the user. 13304 This should be changed to use copy_int or copy_real depending 13305 on how the value is to be used: In some cases this may be an 13306 argument in a group function, like: IF(ISNULL(col),0,COUNT(*)) 13307 */ 13308 if (!(pos=new Item_copy_string(pos))) 13309 goto err; 13310 if (i < border) // HAVING, ORDER and GROUP BY 13311 { 13312 if (extra_funcs.push_back(pos)) 13313 goto err; 13314 } 13315 else if (param->copy_funcs.push_back(pos)) 13316 goto err; 13317 } 13318 res_all_fields.push_back(pos); 13319 ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]= 13320 pos; 13321 } 13322 param->copy_field_end= copy; 13323 13324 for (i= 0; i < border; i++) 13325 itr++; 13326 itr.sublist(res_selected_fields, elements); 13327 /* 13328 Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any 13329 reference used in these will resolve to a item that is already calculated 13330 */ 13331 param->copy_funcs.concat(&extra_funcs); 13332 13333 DBUG_RETURN(0); 13334 13335 err: 13336 if (copy) 13337 delete [] param->copy_field; // This is never 0 13338 param->copy_field=0; 13339 err2: 13340 DBUG_RETURN(TRUE); 13341 }
Here is the call graph for this function:

Here is the caller graph for this function:

| Next_select_func setup_end_select_func | ( | JOIN * | join | ) |
Definition at line 9553 of file sql_select.cc.
References DBUG_PRINT, end_send(), end_send_group(), end_unique_update(), end_update(), end_write(), end_write_group(), Procedure::flags, st_table::group, st_table_share::keys, memcpy, PROC_GROUP, JOIN::procedure, st_table::s, JOIN::sort_and_group, JOIN::sum_funcs, JOIN::tmp_table, and JOIN::tmp_table_param.
Referenced by do_select(), and Sensitive_cursor::open().
09554 { 09555 TABLE *table= join->tmp_table; 09556 TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param; 09557 Next_select_func end_select; 09558 09559 /* Set up select_end */ 09560 if (table) 09561 { 09562 if (table->group && tmp_tbl->sum_func_count) 09563 { 09564 if (table->s->keys) 09565 { 09566 DBUG_PRINT("info",("Using end_update")); 09567 end_select=end_update; 09568 } 09569 else 09570 { 09571 DBUG_PRINT("info",("Using end_unique_update")); 09572 end_select=end_unique_update; 09573 } 09574 } 09575 else if (join->sort_and_group && !tmp_tbl->precomputed_group_by) 09576 { 09577 DBUG_PRINT("info",("Using end_write_group")); 09578 end_select=end_write_group; 09579 } 09580 else 09581 { 09582 DBUG_PRINT("info",("Using end_write")); 09583 end_select=end_write; 09584 if (tmp_tbl->precomputed_group_by) 09585 { 09586 /* 09587 A preceding call to create_tmp_table in the case when loose 09588 index scan is used guarantees that 09589 TMP_TABLE_PARAM::items_to_copy has enough space for the group 09590 by functions. It is OK here to use memcpy since we copy 09591 Item_sum pointers into an array of Item pointers. 09592 */ 09593 memcpy(tmp_tbl->items_to_copy + tmp_tbl->func_count, 09594 join->sum_funcs, 09595 sizeof(Item*)*tmp_tbl->sum_func_count); 09596 tmp_tbl->items_to_copy[tmp_tbl->func_count+tmp_tbl->sum_func_count]= 0; 09597 } 09598 } 09599 } 09600 else 09601 { 09602 if ((join->sort_and_group || 09603 (join->procedure && join->procedure->flags & PROC_GROUP)) && 09604 !tmp_tbl->precomputed_group_by) 09605 end_select= end_send_group; 09606 else 09607 end_select= end_send; 09608 } 09609 return end_select; 09610 }
Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 427 of file opt_sum.cc.
References args, Item_func::argument_count(), Item_func::arguments(), Item::const_item(), Item::FIELD_ITEM, and Item::type().
Referenced by check_group_min_max_predicates(), and matching_cond().
00428 { 00429 Item *item; 00430 *inv_order= 0; 00431 switch (func_item->argument_count()) { 00432 case 0: 00433 /* MULT_EQUAL_FUNC */ 00434 { 00435 Item_equal *item_equal= (Item_equal *) func_item; 00436 Item_equal_iterator it(*item_equal); 00437 args[0]= it++; 00438 if (it++) 00439 return 0; 00440 if (!(args[1]= item_equal->get_const())) 00441 return 0; 00442 } 00443 break; 00444 case 1: 00445 /* field IS NULL */ 00446 item= func_item->arguments()[0]; 00447 if (item->type() != Item::FIELD_ITEM) 00448 return 0; 00449 args[0]= item; 00450 break; 00451 case 2: 00452 /* 'field op const' or 'const op field' */ 00453 item= func_item->arguments()[0]; 00454 if (item->type() == Item::FIELD_ITEM) 00455 { 00456 args[0]= item; 00457 item= func_item->arguments()[1]; 00458 if (!item->const_item()) 00459 return 0; 00460 args[1]= item; 00461 } 00462 else if (item->const_item()) 00463 { 00464 args[1]= item; 00465 item= func_item->arguments()[1]; 00466 if (item->type() != Item::FIELD_ITEM) 00467 return 0; 00468 args[0]= item; 00469 *inv_order= 1; 00470 } 00471 else 00472 return 0; 00473 break; 00474 case 3: 00475 /* field BETWEEN const AND const */ 00476 item= func_item->arguments()[0]; 00477 if (item->type() == Item::FIELD_ITEM) 00478 { 00479 args[0]= item; 00480 for (int i= 1 ; i <= 2; i++) 00481 { 00482 item= func_item->arguments()[i]; 00483 if (!item->const_item()) 00484 return 0; 00485 args[i]= item; 00486 } 00487 } 00488 else 00489 return 0; 00490 } 00491 return 1; 00492 }
Here is the call graph for this function:

Here is the caller graph for this function:

| bool store_val_in_field | ( | Field * | field, | |
| Item * | val, | |||
| enum_check_fields | check_flag | |||
| ) |
Definition at line 4921 of file sql_select.cc.
References dbug_tmp_restore_column_map(), dbug_tmp_use_all_columns(), error, st_table::in_use, Item::save_in_field(), Field::table, and st_table::write_set.
Referenced by matching_cond(), and test_if_ref().
04922 { 04923 bool error; 04924 TABLE *table= field->table; 04925 THD *thd= table->in_use; 04926 ha_rows cuted_fields=thd->cuted_fields; 04927 my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, 04928 table->write_set); 04929 04930 /* 04931 we should restore old value of count_cuted_fields because 04932 store_val_in_field can be called from mysql_insert 04933 with select_insert, which make count_cuted_fields= 1 04934 */ 04935 enum_check_fields old_count_cuted_fields= thd->count_cuted_fields; 04936 thd->count_cuted_fields= check_flag; 04937 error= item->save_in_field(field, 1); 04938 thd->count_cuted_fields= old_count_cuted_fields; 04939 dbug_tmp_restore_column_map(table->write_set, old_map); 04940 return error || cuted_fields != thd->cuted_fields; 04941 }
Here is the call graph for this function:

Here is the caller graph for this function:

| enum_nested_loop_state sub_select | ( | JOIN * | join, | |
| JOIN_TAB * | join_tab, | |||
| bool | end_of_records | |||
| ) |
Definition at line 9864 of file sql_select.cc.
References error, evaluate_join_record(), evaluate_null_complemented_join_record(), FALSE, st_join_table::found, info, NESTED_LOOP_NO_MORE_ROWS, NESTED_LOOP_OK, st_join_table::next_select, st_table::null_row, st_read_record::read_record, report_error(), JOIN::resume_nested_loop, JOIN::return_tab, st_join_table::table, JOIN::tables, and JOIN::thd.
Referenced by do_select(), Sensitive_cursor::fetch(), make_join_readinfo(), Sensitive_cursor::open(), and sub_select_cache().
09865 { 09866 join_tab->table->null_row=0; 09867 if (end_of_records) 09868 return (*join_tab->next_select)(join,join_tab+1,end_of_records); 09869 09870 int error; 09871 enum_nested_loop_state rc; 09872 my_bool *report_error= &(join->thd->net.report_error); 09873 READ_RECORD *info= &join_tab->read_record; 09874 09875 if (join->resume_nested_loop) 09876 { 09877 /* If not the last table, plunge down the nested loop */ 09878 if (join_tab < join->join_tab + join->tables - 1) 09879 rc= (*join_tab->next_select)(join, join_tab + 1, 0); 09880 else 09881 { 09882 join->resume_nested_loop= FALSE; 09883 rc= NESTED_LOOP_OK; 09884 } 09885 } 09886 else 09887 { 09888 join->return_tab= join_tab; 09889 09890 if (join_tab->last_inner) 09891 { 09892 /* join_tab is the first inner table for an outer join operation. */ 09893 09894 /* Set initial state of guard variables for this table.*/ 09895 join_tab->found=0; 09896 join_tab->not_null_compl= 1; 09897 09898 /* Set first_unmatched for the last inner table of this group */ 09899 join_tab->last_inner->first_unmatched= join_tab; 09900 } 09901 join->thd->row_count= 0; 09902 09903 error= (*join_tab->read_first_record)(join_tab); 09904 rc= evaluate_join_record(join, join_tab, error, report_error); 09905 } 09906 09907 while (rc == NESTED_LOOP_OK) 09908 { 09909 error= info->read_record(info); 09910 rc= evaluate_join_record(join, join_tab, error, report_error); 09911 } 09912 09913 if (rc == NESTED_LOOP_NO_MORE_ROWS && 09914 join_tab->last_inner && !join_tab->found) 09915 rc= evaluate_null_complemented_join_record(join, join_tab); 09916 09917 if (rc == NESTED_LOOP_NO_MORE_ROWS) 09918 rc= NESTED_LOOP_OK; 09919 return rc; 09920 }
Here is the call graph for this function:

Here is the caller graph for this function:

| enum_nested_loop_state sub_select_cache | ( | JOIN * | join, | |
| JOIN_TAB * | join_tab, | |||
| bool | end_of_records | |||
| ) |
Definition at line 9723 of file sql_select.cc.
References st_join_table::cache, FALSE, flush_cached_records(), NESTED_LOOP_KILLED, NESTED_LOOP_NO_MORE_ROWS, NESTED_LOOP_OK, store_record_in_cache(), sub_select(), test_if_quick_select(), JOIN::thd, TRUE, and st_join_table::use_quick.
Referenced by make_join_readinfo(), and Sensitive_cursor::open().
09724 { 09725 enum_nested_loop_state rc; 09726 09727 if (end_of_records) 09728 { 09729 rc= flush_cached_records(join,join_tab,FALSE); 09730 if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS) 09731 rc= sub_select(join,join_tab,end_of_records); 09732 return rc; 09733 } 09734 if (join->thd->killed) // If aborted by user 09735 { 09736 join->thd->send_kill_message(); 09737 return NESTED_LOOP_KILLED; /* purecov: inspected */ 09738 } 09739 if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0) 09740 { 09741 if (!store_record_in_cache(&join_tab->cache)) 09742 return NESTED_LOOP_OK; // There is more room in cache 09743 return flush_cached_records(join,join_tab,FALSE); 09744 } 09745 rc= flush_cached_records(join, join_tab, TRUE); 09746 if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS) 09747 rc= sub_select(join, join_tab, end_of_records); 09748 return rc; 09749 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void TEST_join | ( | JOIN * | join | ) |
Definition at line 157 of file sql_test.cc.
References buf, DBUG_ENTER, DBUG_FILE, DBUG_LOCK_FILE, DBUG_UNLOCK_FILE, DBUG_VOID_RETURN, FALSE, JOIN::join_tab, join_type_str, MAX_KEY, st_join_table::table, JOIN::tables, and VOID.
00158 { 00159 uint i,ref; 00160 DBUG_ENTER("TEST_join"); 00161 00162 DBUG_LOCK_FILE; 00163 VOID(fputs("\nInfo about JOIN\n",DBUG_FILE)); 00164 for (i=0 ; i < join->tables ; i++) 00165 { 00166 JOIN_TAB *tab=join->join_tab+i; 00167 TABLE *form=tab->table; 00168 char key_map_buff[128]; 00169 fprintf(DBUG_FILE,"%-16.16s type: %-7s q_keys: %s refs: %d key: %d len: %d\n", 00170 form->alias, 00171 join_type_str[tab->type], 00172 tab->keys.print(key_map_buff), 00173 tab->ref.key_parts, 00174 tab->ref.key, 00175 tab->ref.key_length); 00176 if (tab->select) 00177 { 00178 char buf[MAX_KEY/8+1]; 00179 if (tab->use_quick == 2) 00180 fprintf(DBUG_FILE, 00181 " quick select checked for each record (keys: %s)\n", 00182 tab->select->quick_keys.print(buf)); 00183 else if (tab->select->quick) 00184 { 00185 fprintf(DBUG_FILE, " quick select used:\n"); 00186 tab->select->quick->dbug_dump(18, FALSE); 00187 } 00188 else 00189 VOID(fputs(" select used\n",DBUG_FILE)); 00190 } 00191 if (tab->ref.key_parts) 00192 { 00193 VOID(fputs(" refs: ",DBUG_FILE)); 00194 for (ref=0 ; ref < tab->ref.key_parts ; ref++) 00195 { 00196 Item *item=tab->ref.items[ref]; 00197 fprintf(DBUG_FILE,"%s ", item->full_name()); 00198 } 00199 VOID(fputc('\n',DBUG_FILE)); 00200 } 00201 } 00202 DBUG_UNLOCK_FILE; 00203 DBUG_VOID_RETURN; 00204 }
| const char* join_type_str[] |
1.4.7

