28 #include "sql_executor.h"
29 #include "sql_optimizer.h"
30 #include "sql_join_buffer.h"
35 #include "sql_derived.h"
40 #include "opt_explain_format.h"
47 static void save_const_null_info(
JOIN *join, table_map *save_nullinfo);
48 static void restore_const_null_info(
JOIN *join, table_map save_nullinfo);
49 static int do_select(
JOIN *join);
51 static enum_nested_loop_state
53 static enum_nested_loop_state
54 evaluate_null_complemented_join_record(
JOIN *join,
JOIN_TAB *join_tab);
55 static enum_nested_loop_state
56 end_send(
JOIN *join,
JOIN_TAB *join_tab,
bool end_of_records);
57 static enum_nested_loop_state
58 end_write(
JOIN *join,
JOIN_TAB *join_tab,
bool end_of_records);
59 static enum_nested_loop_state
60 end_update(
JOIN *join,
JOIN_TAB *join_tab,
bool end_of_records);
61 static enum_nested_loop_state
62 end_unique_update(
JOIN *join,
JOIN_TAB *join_tab,
bool end_of_records);
65 static int join_read_system(
JOIN_TAB *tab);
66 static int join_read_const(
JOIN_TAB *tab);
67 static int join_read_key(
JOIN_TAB *tab);
68 static int join_read_always_key(
JOIN_TAB *tab);
71 static int test_if_quick_select(
JOIN_TAB *tab);
74 static int join_ft_read_first(
JOIN_TAB *tab);
76 static int join_read_always_key_or_null(
JOIN_TAB *tab);
77 static int join_read_next_same_or_null(
READ_RECORD *info);
78 static int create_sort_index(THD *thd,
JOIN *join,
JOIN_TAB *tab);
79 static bool remove_dup_with_compare(THD *thd,
TABLE *
entry,
Field **field,
81 static bool remove_dup_with_hash_index(THD *thd,
TABLE *
table,
82 uint field_count,
Field **first_field,
83 ulong key_length,
Item *having);
84 static int join_read_linked_first(
JOIN_TAB *tab);
85 static int join_read_linked_next(
READ_RECORD *info);
102 trace_exec.add_select_number(
select_lex->select_number);
105 DBUG_ENTER(
"JOIN::exec");
107 DBUG_ASSERT(!
tables || thd->lex->is_query_tables_locked());
108 DBUG_ASSERT(!(select_options & SELECT_DESCRIBE));
110 THD_STAGE_INFO(thd, stage_executing);
113 if (thd->lex->ignore)
114 thd->lex->current_select->no_error=
true;
130 if (
select_lex->cond_value != Item::COND_FALSE &&
133 if (result->send_result_set_metadata(*columns_list,
134 Protocol::SEND_NUM_ROWS |
146 if (((
select_lex->having_value != Item::COND_FALSE) &&
152 error= (int) result->send_eof();
153 send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
154 thd->get_sent_row_count());
157 thd->limit_found_rows= send_records;
158 thd->set_examined_row_count(0);
162 return_zero_rows(
this, *columns_list);
169 return_zero_rows(
this, *columns_list);
183 error= thd->is_error();
187 THD_STAGE_INFO(thd, stage_sending_data);
188 DBUG_PRINT(
"info", (
"%s", thd->proc_info));
189 result->send_result_set_metadata(*fields,
190 Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
191 error= do_select(
this);
193 thd->inc_examined_row_count(examined_rows);
194 DBUG_PRINT(
"counts", (
"thd->examined_row_count: %lu",
195 (ulong) thd->get_examined_row_count()));
203 ORDER_with_src &tmp_table_group,
204 bool save_sum_fields)
206 DBUG_ENTER(
"JOIN::create_intermediate_table");
207 THD_STAGE_INFO(thd, stage_creating_tmp_table);
218 m_select_limit : HA_POS_ERROR;
220 tab->tmp_table_param=
new TMP_TABLE_PARAM(tmp_table_param);
221 tab->tmp_table_param->skip_create_table=
true;
222 TABLE* table= create_tmp_table(thd, tab->tmp_table_param, *tmp_table_fields,
224 save_sum_fields, select_options, tmp_rows_limit,
228 tmp_table_param.using_outer_summary_function=
229 tab->tmp_table_param->using_outer_summary_function;
231 DBUG_ASSERT(tab > tab->join->join_tab);
237 table->reginfo.join_tab= tab;
248 (select_options & (SELECT_BIG_RESULT | OPTION_BUFFER_RESULT)))
253 if (group_list && simple_group)
255 DBUG_PRINT(
"info",(
"Sorting for group"));
256 THD_STAGE_INFO(thd, stage_sorting_for_group);
258 if (ordered_index_usage != ordered_index_group_by &&
267 if (prepare_sum_aggregators(sum_funcs,
268 !join_tab->is_using_agg_loose_index_scan()))
278 if (prepare_sum_aggregators(sum_funcs,
279 !join_tab->is_using_agg_loose_index_scan()))
284 if (!group_list && !table->distinct &&
order && simple_order)
286 DBUG_PRINT(
"info",(
"Sorting for order"));
287 THD_STAGE_INFO(thd, stage_sorting_for_order);
289 if (ordered_index_usage != ordered_index_order_by &&
299 free_tmp_table(thd, table);
326 for (i= send_group_parts ; i-- > idx ; )
332 if (send_records < unit->select_limit_cnt && do_send_rows &&
333 result->send_data(
rollup.fields[i]))
367 for (i= send_group_parts ; i-- > idx ; )
378 if (item->type() == Item::NULL_ITEM && item->is_result_field())
379 item->save_in_result_field(1);
381 copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
382 if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
384 if (create_myisam_from_heap(thd, table_arg,
385 tmp_table_param.start_recinfo,
386 &tmp_table_param.recinfo,
387 write_error, FALSE, NULL))
399 JOIN::optimize_distinct()
403 if (
select_lex->select_list_tables & last_join_tab->table->map)
405 last_join_tab->not_used_in_distinct=
true;
406 if (last_join_tab == join_tab)
415 DBUG_ASSERT(ordered_index_usage == ordered_index_order_by);
416 if (ordered_index_usage == ordered_index_order_by)
423 bool prepare_sum_aggregators(
Item_sum **func_ptr,
bool need_distinct)
426 DBUG_ENTER(
"prepare_sum_aggregators");
427 while ((func= *(func_ptr++)))
429 if (func->set_aggregator(need_distinct && func->has_with_distinct() ?
430 Aggregator::DISTINCT_AGGREGATOR :
431 Aggregator::SIMPLE_AGGREGATOR))
458 DBUG_ENTER(
"setup_sum_funcs");
459 while ((func= *(func_ptr++)))
469 init_tmptable_sum_functions(
Item_sum **func_ptr)
472 while ((func= *(func_ptr++)))
480 update_tmptable_sum_func(
Item_sum **func_ptr,
481 TABLE *tmp_table __attribute__((unused)))
484 while ((func= *(func_ptr++)))
485 func->update_field();
494 for (; func_ptr != end_ptr ; func_ptr++)
495 (
void) (*func_ptr)->save_in_result_field(1);
503 for (; func_ptr != end_ptr ;func_ptr++)
505 if ((*func_ptr)->reset_and_add())
509 for ( ; *func_ptr ; func_ptr++)
511 if ((*func_ptr)->aggregator_add())
519 update_sum_func(
Item_sum **func_ptr)
522 for (; (func= (
Item_sum*) *func_ptr) ; func_ptr++)
548 for (; (func = *func_ptr) ; func_ptr++)
550 func->save_in_result_field(1);
587 static enum_nested_loop_state
588 end_sj_materialize(
JOIN *join,
JOIN_TAB *join_tab,
bool end_of_records)
593 DBUG_ENTER(
"end_sj_materialize");
603 DBUG_RETURN(NESTED_LOOP_OK);
605 fill_record(thd, table->field, sjm->
sj_nest->nested_join->sj_inner_exprs,
608 DBUG_RETURN(NESTED_LOOP_ERROR);
609 if ((error= table->file->ha_write_row(table->record[0])))
613 create_myisam_from_heap(thd, table,
617 DBUG_RETURN(NESTED_LOOP_ERROR);
620 DBUG_RETURN(NESTED_LOOP_OK);
639 static void update_const_equal_items(
Item *cond,
JOIN_TAB *tab)
641 if (!(cond->used_tables() & tab->table->map))
644 if (cond->type() == Item::COND_ITEM)
650 update_const_equal_items(item, tab);
652 else if (cond->type() == Item::FUNC_ITEM &&
653 ((
Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
656 bool contained_const= item_equal->get_const() != NULL;
658 if (!contained_const && item_equal->get_const())
663 while ((item_field= it++))
665 Field *field= item_field->field;
666 JOIN_TAB *stat= field->table->reginfo.join_tab;
667 key_map possible_keys= field->key_start;
668 possible_keys.intersect(field->table->keys_in_use_for_query);
670 stat[0].
keys.merge(possible_keys);
677 if (!possible_keys.is_clear_all())
679 TABLE *tab= field->table;
681 for (use= stat->
keyuse; use && use->
table == tab; use++)
682 if (possible_keys.is_set(use->
key) &&
683 tab->key_info[use->
key].key_part[use->
keypart].field ==
710 DBUG_ENTER(
"return_zero_rows");
714 if (!(join->result->send_result_set_metadata(fields,
715 Protocol::SEND_NUM_ROWS |
716 Protocol::SEND_EOF)))
718 bool send_error= FALSE;
723 table= table->next_leaf)
724 mark_as_null_row(table->table);
736 item->no_rows_in_result();
739 send_error= join->result->send_data(fields);
742 join->result->send_eof();
745 join->thd->set_examined_row_count(0);
746 join->thd->limit_found_rows= 0;
763 JOIN *join= tab->join;
764 TABLE *table= tab->table;
766 TMP_TABLE_PARAM *tmp_tbl= tab->tmp_table_param;
768 DBUG_ASSERT(table && op);
770 if (table->group && tmp_tbl->sum_func_count &&
771 !tmp_tbl->precomputed_group_by)
777 if (table->s->keys && !table->s->uniques)
779 DBUG_PRINT(
"info",(
"Using end_update"));
784 DBUG_PRINT(
"info",(
"Using end_unique_update"));
790 DBUG_PRINT(
"info",(
"Using end_write_group"));
795 DBUG_PRINT(
"info",(
"Using end_write"));
797 if (tmp_tbl->precomputed_group_by)
806 memcpy(tmp_tbl->items_to_copy + tmp_tbl->func_count,
808 sizeof(
Item*)*tmp_tbl->sum_func_count);
809 tmp_tbl->items_to_copy[tmp_tbl->func_count+tmp_tbl->sum_func_count]= 0;
829 TMP_TABLE_PARAM *tmp_tbl= tab ? tab->tmp_table_param : &join->tmp_table_param;
839 DBUG_PRINT(
"info",(
"Using end_send_group"));
840 return end_send_group;
842 DBUG_PRINT(
"info",(
"Using end_send"));
859 do_select(
JOIN *join)
862 enum_nested_loop_state error= NESTED_LOOP_OK;
863 DBUG_ENTER(
"do_select");
865 join->send_records=0;
878 error= (*end_select)(join, 0, 0);
879 if (error >= NESTED_LOOP_OK)
880 error= (*end_select)(join, 0, 1);
887 join->examined_rows++;
888 DBUG_ASSERT(join->examined_rows <= 1);
890 else if (join->send_row_on_empty_set())
892 table_map save_nullinfo= 0;
900 if (join->select_lex->master_unit()->item && join->const_tables)
901 save_const_null_info(join, &save_nullinfo);
907 item->no_rows_in_result();
912 if (!join->having || join->having->val_int())
913 rc= join->result->send_data(*join->fields);
916 restore_const_null_info(join, save_nullinfo);
923 if (join->thd->is_error())
924 error= NESTED_LOOP_ERROR;
928 JOIN_TAB *join_tab= join->join_tab + join->const_tables;
929 DBUG_ASSERT(join->primary_tables);
930 error= join->first_select(join,join_tab,0);
931 if (error >= NESTED_LOOP_OK)
932 error= join->first_select(join,join_tab,1);
935 join->thd->limit_found_rows= join->send_records;
948 uint const_tables= join->const_tables;
951 if (join->tmp_tables > 0)
952 sort_tab= join_tab + join->primary_tables + join->tmp_tables - 1;
955 DBUG_ASSERT(!join->plan_is_const());
956 sort_tab= join_tab + const_tables;
958 if (sort_tab->filesort &&
959 join->select_options & OPTION_FOUND_ROWS &&
961 sort_tab->filesort->
limit != HA_POS_ERROR)
963 join->thd->limit_found_rows= sort_tab->records;
974 if (error == NESTED_LOOP_OK)
980 if (join->result->send_eof())
982 DBUG_PRINT(
"info",(
"%ld records output", (
long) join->send_records));
989 DBUG_PRINT(
"error",(
"Error: do_select() failed"));
992 rc= join->thd->is_error() ? -1 : rc;
1030 enum_nested_loop_state
1033 enum_nested_loop_state rc;
1037 DBUG_ASSERT(op != NULL);
1039 DBUG_ENTER(
"sub_select_op");
1041 if (join->thd->killed)
1044 join->thd->send_kill_message();
1045 DBUG_RETURN(NESTED_LOOP_KILLED);
1051 if (rc >= NESTED_LOOP_OK)
1052 rc=
sub_select(join, join_tab, end_of_records);
1056 DBUG_RETURN(NESTED_LOOP_ERROR);
1062 DBUG_ASSERT(join_tab->use_quick != QS_DYNAMIC_RANGE);
1197 enum_nested_loop_state
1200 DBUG_ENTER(
"sub_select");
1202 join_tab->table->null_row=0;
1205 enum_nested_loop_state nls=
1206 (*join_tab->next_select)(join,join_tab+1,end_of_records);
1212 DBUG_RETURN(NESTED_LOOP_ERROR);
1216 do_sj_reset(join_tab->flush_weedout_table);
1219 join->return_tab= join_tab;
1221 join_tab->found_match=
false;
1240 join_tab->match_tab->found_match=
false;
1243 join->thd->get_stmt_da()->reset_current_row_for_warning();
1245 enum_nested_loop_state rc= NESTED_LOOP_OK;
1246 bool in_first_read=
true;
1247 while (rc == NESTED_LOOP_OK && join->return_tab >= join_tab)
1252 in_first_read=
false;
1256 error= info->read_record(info);
1258 DBUG_EXECUTE_IF(
"bug13822652_1", join->thd->killed= THD::KILL_QUERY;);
1260 if (error > 0 || (join->thd->is_error()))
1261 rc= NESTED_LOOP_ERROR;
1264 else if (join->thd->killed)
1266 join->thd->send_kill_message();
1267 rc= NESTED_LOOP_KILLED;
1271 if (join_tab->keep_current_rowid)
1272 join_tab->table->file->position(join_tab->table->record[0]);
1273 rc= evaluate_join_record(join, join_tab);
1278 rc= evaluate_null_complemented_join_record(join, join_tab);
1302 if ((*materialize_table)(
this))
1308 if (copy_current_rowid)
1309 copy_current_rowid->bind_buffer(table->file->ref);
1340 DBUG_ENTER(
"do_sj_dups_weedout");
1342 if (sjtbl->is_confluent)
1344 if (sjtbl->have_confluent_row)
1348 sjtbl->have_confluent_row= TRUE;
1353 uchar *ptr= sjtbl->tmp_table->record[0] + 1;
1356 if (((
Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
1358 *ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
1363 int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
1368 uchar *
const nulls_ptr= ptr;
1369 if (sjtbl->null_bytes)
1371 memset(ptr, 0, sjtbl->null_bytes);
1372 ptr += sjtbl->null_bytes;
1376 for (uint
i=0; tab != tab_end; tab++,
i++)
1378 handler *h= tab->join_tab->table->file;
1379 if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
1382 *(nulls_ptr + tab->null_byte) |= tab->null_bit;
1383 memset(ptr + tab->rowid_offset, 0, h->
ref_length);
1388 memcpy(ptr + tab->rowid_offset, h->ref, h->
ref_length);
1392 error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
1396 if (!sjtbl->tmp_table->file->
is_fatal_error(error, HA_CHECK_DUP))
1402 if (create_myisam_from_heap(thd, sjtbl->tmp_table,
1403 sjtbl->start_recinfo, &sjtbl->recinfo,
1404 error, TRUE, &is_duplicate))
1406 DBUG_RETURN(is_duplicate ? 1 : 0);
1418 DBUG_ENTER(
"do_sj_reset");
1419 if (sj_tbl->tmp_table)
1424 sj_tbl->have_confluent_row= FALSE;
1443 static enum_nested_loop_state
1446 bool not_used_in_distinct=join_tab->not_used_in_distinct;
1447 ha_rows found_records=join->found_records;
1448 Item *condition= join_tab->condition();
1450 DBUG_ENTER(
"evaluate_join_record");
1452 (
"join: %p join_tab index: %d table: %s cond: %p",
1453 join, static_cast<int>(join_tab - join_tab->join->join_tab),
1454 join_tab->table->alias, condition));
1458 found=
test(condition->val_int());
1460 if (join->thd->killed)
1462 join->thd->send_kill_message();
1463 DBUG_RETURN(NESTED_LOOP_KILLED);
1467 if (join->thd->is_error())
1468 DBUG_RETURN(NESTED_LOOP_ERROR);
1488 first_unmatched->
found= 1;
1489 for (
JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
1507 DBUG_ASSERT(!(tab->table->reginfo.not_exists_optimize &&
1508 !tab->condition()));
1510 if (tab->condition() && !tab->condition()->val_int())
1514 if (tab->table->reginfo.not_exists_optimize)
1525 join->return_tab= join_tab - 1;
1526 DBUG_RETURN(NESTED_LOOP_OK);
1529 if (tab == join_tab)
1537 join->return_tab= tab;
1538 DBUG_RETURN(NESTED_LOOP_OK);
1546 if ((first_unmatched= first_unmatched->
first_upper) &&
1552 JOIN_TAB *return_tab= join->return_tab;
1558 DBUG_RETURN(NESTED_LOOP_ERROR);
1562 else if (join_tab->
do_loosescan() && join_tab->match_tab->found_match)
1571 if (key_cmp(join_tab->table->key_info[join_tab->
index].key_part,
1572 join_tab->loosescan_buf, join_tab->loosescan_key_len))
1578 join_tab->match_tab->found_match=
false;
1583 join_tab->found_match=
true;
1590 join->examined_rows++;
1591 DBUG_PRINT(
"counts", (
"evaluate_join_record join->examined_rows++: %lu",
1592 (ulong) join->examined_rows));
1596 enum enum_nested_loop_state rc;
1598 rc= (*join_tab->next_select)(join, join_tab+1, 0);
1599 join->thd->get_stmt_da()->inc_current_row_for_warning();
1600 if (rc != NESTED_LOOP_OK)
1603 if (join_tab->
do_loosescan() && join_tab->match_tab->found_match)
1610 KEY *key= join_tab->table->key_info + join_tab->
index;
1611 key_copy(join_tab->loosescan_buf, join_tab->read_record.record, key,
1612 join_tab->loosescan_key_len);
1614 else if (join_tab->
do_firstmatch() && join_tab->match_tab->found_match)
1620 set_if_smaller(return_tab, join_tab->firstmatch_return);
1628 if (not_used_in_distinct && found_records != join->found_records)
1629 set_if_smaller(return_tab, join_tab - 1);
1631 set_if_smaller(join->return_tab, return_tab);
1635 join->thd->get_stmt_da()->inc_current_row_for_warning();
1639 join_tab->read_record.unlock_row(join_tab);
1649 join->examined_rows++;
1650 join->thd->get_stmt_da()->inc_current_row_for_warning();
1652 join_tab->read_record.unlock_row(join_tab);
1654 DBUG_RETURN(NESTED_LOOP_OK);
1666 static enum_nested_loop_state
1667 evaluate_null_complemented_join_record(
JOIN *join,
JOIN_TAB *join_tab)
1673 JOIN_TAB *first_inner_tab= join_tab;
1676 DBUG_ENTER(
"evaluate_null_complemented_join_record");
1678 for ( ; join_tab <= last_inner_tab ; join_tab++)
1681 if (join_tab->copy_current_rowid &&
1682 !join_tab->copy_current_rowid->buffer_is_bound())
1683 join_tab->copy_current_rowid->bind_buffer(join_tab->table->file->ref);
1689 restore_record(join_tab->table,s->default_values);
1690 mark_as_null_row(join_tab->table);
1694 do_sj_reset(join_tab->flush_weedout_table);
1697 if (join_tab->condition() && !join_tab->condition()->val_int())
1698 DBUG_RETURN(NESTED_LOOP_OK);
1700 join_tab= last_inner_tab;
1707 if (first_unmatched != NULL &&
1709 first_unmatched= NULL;
1720 const enum_nested_loop_state rc= evaluate_join_record(join, join_tab);
1734 if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
1736 table->status= STATUS_GARBAGE;
1744 if (error != HA_ERR_LOCK_DEADLOCK &&
1745 error != HA_ERR_LOCK_WAIT_TIMEOUT &&
1746 error != HA_ERR_TABLE_DEF_CHANGED &&
1747 !table->in_use->killed)
1748 sql_print_error(
"Got error %d when reading table '%s'",
1749 error, table->s->path.str);
1758 TABLE *table= tab->table;
1761 make_prev_keypart_map(tab->ref.
key_parts),
1762 HA_READ_KEY_EXACT)))
1769 test_if_quick_select(
JOIN_TAB *tab)
1771 tab->select->set_quick(NULL);
1772 return tab->select->test_quick_select(tab->join->thd,
1777 ORDER::ORDER_NOT_RELEVANT);
1794 DBUG_ENTER(
"join_read_const_table");
1795 TABLE *table=tab->table;
1796 table->const_table=1;
1798 table->status= STATUS_GARBAGE | STATUS_NOT_FOUND;
1800 if (table->reginfo.lock_type >= TL_WRITE_ALLOW_WRITE)
1802 const enum_sql_command sql_command= tab->join->thd->lex->sql_command;
1803 if (sql_command == SQLCOM_UPDATE_MULTI ||
1804 sql_command == SQLCOM_DELETE_MULTI)
1823 bitmap_set_all(table->read_set);
1828 if (tab->type == JT_SYSTEM)
1830 if ((error=join_read_system(tab)))
1832 tab->info= ET_CONST_ROW_NOT_FOUND;
1834 pos->records_read=0.0;
1835 pos->ref_depend_map= 0;
1836 if (!table->pos_in_table_list->outer_join || error > 0)
1842 if (!table->
key_read && table->covering_keys.is_set(tab->ref.
key) &&
1843 !table->no_keyread &&
1844 (int) table->reginfo.lock_type <= (
int) TL_READ_HIGH_PRIORITY)
1846 table->set_keyread(TRUE);
1849 error=join_read_const(tab);
1850 table->set_keyread(FALSE);
1853 tab->info= ET_UNIQUE_ROW_NOT_FOUND;
1855 pos->records_read=0.0;
1856 pos->ref_depend_map= 0;
1857 if (!table->pos_in_table_list->outer_join || error > 0)
1865 DBUG_ASSERT(!(*tab->
on_expr_ref)->is_expensive());
1867 mark_as_null_row(table);
1869 if (!table->null_row)
1870 table->maybe_null=0;
1873 JOIN *join= tab->join;
1875 update_const_equal_items(join->
conds, tab);
1877 for (tbl= join->
select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
1883 embedded= embedding;
1884 if (embedded->join_cond())
1885 update_const_equal_items(embedded->join_cond(), tab);
1886 embedding= embedded->embedding;
1889 embedding->nested_join->join_list.head() == embedded);
1909 TABLE *table= tab->table;
1911 if (table->status & STATUS_GARBAGE)
1914 table->s->primary_key)))
1916 if (error != HA_ERR_END_OF_FILE)
1918 mark_as_null_row(tab->table);
1919 empty_record(table);
1922 store_record(table,
record[1]);
1924 else if (!table->status)
1925 restore_record(table,
record[1]);
1927 return table->status ? -1 : 0;
1946 TABLE *table= tab->table;
1947 DBUG_ENTER(
"join_read_const");
1950 if (table->status & STATUS_GARBAGE)
1953 if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
1954 error=HA_ERR_KEY_NOT_FOUND;
1959 make_prev_keypart_map(tab->ref.
key_parts),
1964 table->status= STATUS_NOT_FOUND;
1965 mark_as_null_row(tab->table);
1966 empty_record(table);
1967 if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
1974 store_record(table,
record[1]);
1976 else if (!(table->status & ~STATUS_NULL_ROW))
1979 restore_record(table,
record[1]);
1982 DBUG_RETURN(table->status ? -1 : 0);
2004 TABLE *
const table= tab->table;
2008 if (!table->file->inited)
2022 if (cmp_buffer_with_ref(tab->join->thd, table, table_ref) ||
2023 (table->status & (STATUS_GARBAGE | STATUS_NULL_ROW)))
2025 if (table_ref->key_err)
2027 table->status=STATUS_NOT_FOUND;
2034 if (table_ref->
has_record && table_ref->use_count == 0)
2036 table->file->unlock_row();
2041 make_prev_keypart_map(table_ref->
key_parts),
2043 if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
2049 table_ref->use_count= 1;
2052 else if (table->status == 0)
2055 table_ref->use_count++;
2058 return table->status ? -1 : 0;
2073 DBUG_ASSERT(tab->ref.use_count);
2074 if (tab->ref.use_count)
2075 tab->ref.use_count--;
2103 join_read_linked_first(
JOIN_TAB *tab)
2106 TABLE *table= tab->table;
2107 DBUG_ENTER(
"join_read_linked_first");
2110 if (!table->file->inited &&
2120 DBUG_PRINT(
"info", (
"join_read_linked_first null_rejected"));
2124 if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
2126 table->status=STATUS_NOT_FOUND;
2132 error=table->file->index_read_pushed(table->record[0],
2134 make_prev_keypart_map(tab->ref.
key_parts));
2135 if (unlikely(error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE))
2139 int rc= table->status ? -1 : 0;
2146 TABLE *table= info->table;
2147 DBUG_ENTER(
"join_read_linked_next");
2149 int error=table->file->index_next_pushed(table->record[0]);
2152 if (unlikely(error != HA_ERR_END_OF_FILE))
2154 table->status= STATUS_GARBAGE;
2180 join_read_always_key(
JOIN_TAB *tab)
2183 TABLE *table= tab->table;
2186 if (!table->file->inited &&
2197 DBUG_PRINT(
"info", (
"join_read_always_key null_rejected"));
2201 if (cp_buffer_from_ref(tab->join->thd, table, ref))
2205 make_prev_keypart_map(tab->ref.
key_parts),
2206 HA_READ_KEY_EXACT)))
2208 if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
2225 TABLE *table= tab->table;
2227 if (!table->file->inited &&
2233 if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
2235 if ((error=table->file->ha_index_read_last_map(table->record[0],
2237 make_prev_keypart_map(tab->ref.
key_parts))))
2239 if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
2249 join_no_more_records(
READ_RECORD *info __attribute__((unused)))
2259 TABLE *table= info->table;
2260 JOIN_TAB *tab=table->reginfo.join_tab;
2266 if (error != HA_ERR_END_OF_FILE)
2268 table->status= STATUS_GARBAGE;
2279 TABLE *table= info->table;
2280 JOIN_TAB *tab=table->reginfo.join_tab;
2290 DBUG_ASSERT(table->file->pushed_idx_cond == NULL);
2294 if (key_cmp_if_same(table, tab->ref.
key_buff, tab->ref.
key,
2297 table->status=STATUS_NOT_FOUND;
2305 join_init_quick_read_record(
JOIN_TAB *tab)
2314 #ifdef OPTIMIZER_TRACE
2316 const bool disable_trace=
2318 !trace->feature_enabled(Opt_trace_context::DYNAMIC_RANGE);
2325 trace_table.add_utf8_table(tab->table);
2336 if ((!tab->select || !tab->select->quick) &&
2337 (tab->table->file->inited != handler::NONE))
2338 tab->table->file->ha_index_or_rnd_end();
2340 if (test_if_quick_select(tab) == -1)
2346 int read_first_record_seq(
JOIN_TAB *tab)
2350 return (*tab->read_record.read_record)(&tab->read_record);
2377 if (tab->
distinct && tab->remove_duplicates())
2379 if (tab->filesort && tab->sort_table())
2382 if (tab->select && tab->select->quick && (error= tab->select->quick->reset()))
2388 if (init_read_record(&tab->read_record, tab->join->thd, tab->table,
2389 tab->select, 1, 1, FALSE))
2392 return (*tab->read_record.read_record)(&tab->read_record);
2401 join_materialize_derived(
JOIN_TAB *tab)
2403 TABLE_LIST *derived= tab->table->pos_in_table_list;
2407 return NESTED_LOOP_OK;
2409 bool res= mysql_handle_single_derived(tab->table->in_use->lex,
2410 derived, &mysql_derived_materialize);
2411 if (!tab->table->in_use->lex->describe)
2412 mysql_handle_single_derived(tab->table->in_use->lex,
2413 derived, &mysql_derived_cleanup);
2414 return res ? NESTED_LOOP_ERROR : NESTED_LOOP_OK;
2428 join_materialize_semijoin(
JOIN_TAB *tab)
2430 DBUG_ENTER(
"join_materialize_semijoin");
2440 last->next_select= end_sj_materialize;
2444 if ((rc=
sub_select(tab->join, first,
false)) < 0)
2446 if ((rc=
sub_select(tab->join, first,
true)) < 0)
2449 last->next_select= NULL;
2452 DBUG_RETURN(NESTED_LOOP_OK);
2468 if (type == JT_EQ_REF ||
2478 join->ordered_index_usage != JOIN::ordered_index_void)
2485 if (
position->sj_strategy == SJ_OPT_LOOSE_SCAN)
2497 JOIN_TAB::sort_table()
2500 DBUG_PRINT(
"info",(
"Sorting for index"));
2501 THD_STAGE_INFO(join->thd, stage_creating_sort_index);
2502 DBUG_ASSERT(join->ordered_index_usage != (filesort->
order == join->
order ?
2503 JOIN::ordered_index_order_by :
2504 JOIN::ordered_index_group_by));
2505 rc= create_sort_index(join->thd, join,
this);
2514 TABLE *table=tab->table;
2515 if (table->covering_keys.is_set(tab->
index) && !table->no_keyread)
2516 table->set_keyread(TRUE);
2517 tab->table->status=0;
2518 tab->read_record.table=
table;
2519 tab->read_record.index=tab->
index;
2520 tab->read_record.record=table->record[0];
2521 tab->read_record.read_record=join_read_next;
2523 if (!table->file->inited &&
2529 if ((error= tab->table->file->
ha_index_first(tab->table->record[0])))
2531 if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
2543 if ((error= info->table->file->
ha_index_next(info->record)))
2552 TABLE *table=tab->table;
2554 if (table->covering_keys.is_set(tab->
index) && !table->no_keyread)
2555 table->set_keyread(TRUE);
2556 tab->table->status=0;
2557 tab->read_record.read_record=join_read_prev;
2558 tab->read_record.table=
table;
2559 tab->read_record.index=tab->
index;
2560 tab->read_record.record=table->record[0];
2561 if (!table->file->inited &&
2567 if ((error= tab->table->file->
ha_index_last(tab->table->record[0])))
2577 if ((error= info->table->file->
ha_index_prev(info->record)))
2587 TABLE *table= tab->table;
2589 if (!table->file->inited &&
2595 table->file->ft_init();
2597 if ((error= table->file->ft_read(table->record[0])))
2606 if ((error= info->table->file->ft_read(info->table->record[0])))
2617 join_read_always_key_or_null(
JOIN_TAB *tab)
2622 *tab->ref.null_ref_key= 0;
2623 if ((res= join_read_always_key(tab)) >= 0)
2627 *tab->ref.null_ref_key= 1;
2628 return safe_index_read(tab);
2636 if ((error= join_read_next_same(info)) >= 0)
2638 JOIN_TAB *tab= info->table->reginfo.join_tab;
2641 if (*tab->ref.null_ref_key)
2643 *tab->ref.null_ref_key= 1;
2644 return safe_index_read(tab);
2666 if (pushed_joins > 0)
2676 DBUG_ASSERT(tab->type != JT_REF_OR_NULL);
2678 tab->read_record.read_record= join_read_linked_next;
2696 tab->read_record.read_record= join_read_next_same;
2700 case JT_REF_OR_NULL:
2702 tab->read_record.read_record= join_read_next_same_or_null;
2708 tab->read_record.read_record= join_no_more_records;
2714 tab->read_record.read_record= join_no_more_records;
2720 tab->read_record.read_record= join_ft_read_next;
2726 tab->read_record.read_record= join_no_more_records;
2748 static enum_nested_loop_state
2749 end_send(
JOIN *join,
JOIN_TAB *join_tab,
bool end_of_records)
2751 DBUG_ENTER(
"end_send");
2757 DBUG_ASSERT(join_tab == NULL || join_tab != join->join_tab);
2759 List<Item> *fields= join_tab ? (join_tab-1)->fields : join->fields;
2761 if (!end_of_records)
2765 join->join_tab->is_using_loose_index_scan())
2772 DBUG_RETURN(NESTED_LOOP_OK);
2774 if (join->do_send_rows)
2775 error=join->result->send_data(*fields);
2777 DBUG_RETURN(NESTED_LOOP_ERROR);
2779 ++join->send_records;
2780 if (join->send_records >= join->
unit->select_limit_cnt &&
2781 !join->do_send_rows)
2790 join->select_options & OPTION_FOUND_ROWS &&
2791 join_tab > join->join_tab &&
2792 (join_tab - 1)->filesort && (join_tab - 1)->filesort->
using_pq)
2794 DBUG_PRINT(
"info", (
"filesort NESTED_LOOP_QUERY_LIMIT"));
2795 DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT);
2798 if (join->send_records >= join->
unit->select_limit_cnt &&
2801 if (join->select_options & OPTION_FOUND_ROWS)
2806 !join->send_group_parts &&
2809 !(jt->select && jt->select->quick) &&
2810 (jt->table->file->
ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
2814 TABLE *table=jt->table;
2816 if (table->sort.record_pointers ||
2817 (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
2820 join->send_records= table->sort.found_records;
2824 table->file->info(HA_STATUS_VARIABLE);
2825 join->send_records= table->file->stats.records;
2830 join->do_send_rows= 0;
2831 if (join->
unit->fake_select_lex)
2832 join->
unit->fake_select_lex->select_limit= 0;
2833 DBUG_RETURN(NESTED_LOOP_OK);
2836 DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT);
2844 DBUG_RETURN(NESTED_LOOP_CURSOR_LIMIT);
2847 DBUG_RETURN(NESTED_LOOP_OK);
2852 enum_nested_loop_state
2853 end_send_group(
JOIN *join,
JOIN_TAB *join_tab __attribute__((unused)),
2854 bool end_of_records)
2857 enum_nested_loop_state ok_code= NESTED_LOOP_OK;
2858 List<Item> *fields= join_tab ? (join_tab-1)->fields : join->fields;
2859 DBUG_ENTER(
"end_send_group");
2868 if (!join->first_record || end_of_records ||
2869 (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
2872 (join->first_record ||
2875 if (idx < (
int) join->send_group_parts)
2879 table_map save_nullinfo= 0;
2880 if (!join->first_record)
2890 save_const_null_info(join, &save_nullinfo);
2896 while ((item= it++))
2897 item->no_rows_in_result();
2906 if (join->do_send_rows)
2907 error=join->result->send_data(*fields) ? 1 : 0;
2908 join->send_records++;
2911 if (join->
rollup.state != ROLLUP::STATE_NONE && error <= 0)
2917 restore_const_null_info(join, save_nullinfo);
2921 DBUG_RETURN(NESTED_LOOP_ERROR);
2923 DBUG_RETURN(NESTED_LOOP_OK);
2924 if (join->send_records >= join->
unit->select_limit_cnt &&
2927 if (!(join->select_options & OPTION_FOUND_ROWS))
2928 DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT);
2929 join->do_send_rows=0;
2930 join->
unit->select_limit_cnt = HA_POS_ERROR;
2942 ok_code= NESTED_LOOP_CURSOR_LIMIT;
2949 DBUG_RETURN(NESTED_LOOP_OK);
2950 join->first_record=1;
2951 (void)(test_if_item_cache_changed(join->group_fields));
2953 if (idx < (
int) join->send_group_parts)
2960 if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
2961 DBUG_RETURN(NESTED_LOOP_ERROR);
2963 DBUG_RETURN(ok_code);
2966 if (update_sum_func(join->sum_funcs))
2967 DBUG_RETURN(NESTED_LOOP_ERROR);
2968 DBUG_RETURN(NESTED_LOOP_OK);
2973 static enum_nested_loop_state
2974 end_write(
JOIN *join,
JOIN_TAB *join_tab,
bool end_of_records)
2976 TABLE *
const table= join_tab->table;
2977 DBUG_ENTER(
"end_write");
2979 if (join->thd->killed)
2981 join->thd->send_kill_message();
2982 DBUG_RETURN(NESTED_LOOP_KILLED);
2984 if (!end_of_records)
2987 if (
copy_funcs(join_tab->tmp_table_param->items_to_copy, join->thd))
2988 DBUG_RETURN(NESTED_LOOP_ERROR);
2993 join->found_records++;
2994 if ((error=table->file->ha_write_row(table->record[0])))
2998 if (create_myisam_from_heap(join->thd, table,
2999 join_tab->tmp_table_param->start_recinfo,
3000 &join_tab->tmp_table_param->recinfo,
3002 DBUG_RETURN(NESTED_LOOP_ERROR);
3003 table->s->uniques=0;
3006 join_tab->tmp_table_param->end_write_records &&
3009 if (!(join->select_options & OPTION_FOUND_ROWS))
3010 DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT);
3011 join->do_send_rows=0;
3012 join->
unit->select_limit_cnt = HA_POS_ERROR;
3013 DBUG_RETURN(NESTED_LOOP_OK);
3018 DBUG_RETURN(NESTED_LOOP_OK);
3024 static enum_nested_loop_state
3025 end_update(
JOIN *join,
JOIN_TAB *join_tab,
bool end_of_records)
3027 TABLE *
const table= join_tab->table;
3030 DBUG_ENTER(
"end_update");
3033 DBUG_RETURN(NESTED_LOOP_OK);
3034 if (join->thd->killed)
3036 join->thd->send_kill_message();
3037 DBUG_RETURN(NESTED_LOOP_KILLED);
3040 join->found_records++;
3043 for (group=table->group ; group ; group=group->next)
3045 Item *item= *group->item;
3046 item->save_org_in_field(group->field);
3048 if (item->maybe_null)
3049 group->buff[-1]= (char) group->field->is_null();
3052 join_tab->tmp_table_param->group_buff,
3056 restore_record(table,
record[1]);
3057 update_tmptable_sum_func(join->sum_funcs,table);
3058 if ((error=table->file->ha_update_row(table->record[1],
3062 DBUG_RETURN(NESTED_LOOP_ERROR);
3064 DBUG_RETURN(NESTED_LOOP_OK);
3073 for (group=table->group,key_part=table->key_info[0].key_part;
3075 group=group->next,key_part++)
3077 if (key_part->null_bit)
3078 memcpy(table->record[0]+key_part->offset, group->buff, 1);
3080 init_tmptable_sum_functions(join->sum_funcs);
3081 if (
copy_funcs(join_tab->tmp_table_param->items_to_copy, join->thd))
3082 DBUG_RETURN(NESTED_LOOP_ERROR);
3083 if ((error=table->file->ha_write_row(table->record[0])))
3085 if (create_myisam_from_heap(join->thd, table,
3086 join_tab->tmp_table_param->start_recinfo,
3087 &join_tab->tmp_table_param->recinfo,
3088 error, FALSE, NULL))
3089 DBUG_RETURN(NESTED_LOOP_ERROR);
3094 DBUG_RETURN(NESTED_LOOP_ERROR);
3096 ((
QEP_tmp_table*)join_tab->op)->set_write_func(end_unique_update);
3099 DBUG_RETURN(NESTED_LOOP_OK);
3105 static enum_nested_loop_state
3106 end_unique_update(
JOIN *join,
JOIN_TAB *join_tab,
bool end_of_records)
3108 TABLE *table= join_tab->table;
3110 DBUG_ENTER(
"end_unique_update");
3113 DBUG_RETURN(NESTED_LOOP_OK);
3114 if (join->thd->killed)
3116 join->thd->send_kill_message();
3117 DBUG_RETURN(NESTED_LOOP_KILLED);
3120 init_tmptable_sum_functions(join->sum_funcs);
3122 if (
copy_funcs(join_tab->tmp_table_param->items_to_copy, join->thd))
3123 DBUG_RETURN(NESTED_LOOP_ERROR);
3125 if (!(error=table->file->ha_write_row(table->record[0])))
3132 DBUG_RETURN(NESTED_LOOP_ERROR);
3134 if (table->file->
ha_rnd_pos(table->record[1], table->file->dup_ref))
3137 DBUG_RETURN(NESTED_LOOP_ERROR);
3139 restore_record(table,
record[1]);
3140 update_tmptable_sum_func(join->sum_funcs,table);
3141 if ((error=table->file->ha_update_row(table->record[1],
3145 DBUG_RETURN(NESTED_LOOP_ERROR);
3148 DBUG_RETURN(NESTED_LOOP_OK);
3153 enum_nested_loop_state
3154 end_write_group(
JOIN *join,
JOIN_TAB *join_tab,
bool end_of_records)
3156 TABLE *table= join_tab->table;
3158 DBUG_ENTER(
"end_write_group");
3160 if (join->thd->killed)
3162 join->thd->send_kill_message();
3163 DBUG_RETURN(NESTED_LOOP_KILLED);
3165 if (!join->first_record || end_of_records ||
3166 (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
3168 if (join->first_record || (end_of_records && !join->
group))
3170 int send_group_parts= join->send_group_parts;
3171 if (idx < send_group_parts)
3173 table_map save_nullinfo= 0;
3174 if (!join->first_record)
3186 save_const_null_info(join, &save_nullinfo);
3191 while ((item= it++))
3192 item->no_rows_in_result();
3197 copy_sum_funcs(join->sum_funcs,
3198 join->sum_funcs_end[send_group_parts]);
3201 int error= table->file->ha_write_row(table->record[0]);
3203 create_myisam_from_heap(join->thd, table,
3204 join_tab->tmp_table_param->start_recinfo,
3205 &join_tab->tmp_table_param->recinfo,
3206 error, FALSE, NULL))
3207 DBUG_RETURN(NESTED_LOOP_ERROR);
3209 if (join->
rollup.state != ROLLUP::STATE_NONE)
3212 DBUG_RETURN(NESTED_LOOP_ERROR);
3215 restore_const_null_info(join, save_nullinfo);
3218 DBUG_RETURN(NESTED_LOOP_OK);
3224 DBUG_RETURN(NESTED_LOOP_OK);
3225 join->first_record=1;
3226 (void)(test_if_item_cache_changed(join->group_fields));
3228 if (idx < (
int) join->send_group_parts)
3231 if (
copy_funcs(join_tab->tmp_table_param->items_to_copy, join->thd))
3232 DBUG_RETURN(NESTED_LOOP_ERROR);
3233 if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3234 DBUG_RETURN(NESTED_LOOP_ERROR);
3235 DBUG_RETURN(NESTED_LOOP_OK);
3238 if (update_sum_func(join->sum_funcs))
3239 DBUG_RETURN(NESTED_LOOP_ERROR);
3240 DBUG_RETURN(NESTED_LOOP_OK);
3272 ha_rows examined_rows;
3274 ha_rows filesort_retval= HA_POS_ERROR;
3278 DBUG_ENTER(
"create_sort_index");
3286 MYF(MY_WME | MY_ZEROFILL));
3290 if (select && tab->ref.
key >= 0)
3296 select->quick= tab->quick;
3302 if (((uint) tab->ref.
key != select->quick->index))
3303 table->set_keyread(FALSE);
3313 if (!(select->quick= (tab->type == JT_FT ?
3314 get_ft_select(thd, table, tab->ref.
key) :
3315 get_quick_select_for_ref(thd, table, &tab->ref,
3316 tab->found_records))))
3323 DBUG_ASSERT(tab->type == JT_REF || tab->type == JT_EQ_REF);
3325 if ((cp_buffer_from_ref(thd, table, &tab->ref) && thd->is_fatal_error))
3331 if ((join->
select_lex->options & OPTION_SCHEMA_TABLE) &&
3332 get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
3335 if (table->s->tmp_table)
3336 table->file->info(HA_STATUS_VARIABLE);
3337 filesort_retval=
filesort(thd, table, fsort, tab->keep_current_rowid,
3338 &examined_rows, &found_rows);
3339 table->sort.found_records= filesort_retval;
3340 tab->records= found_rows;
3341 tab->join->examined_rows+=examined_rows;
3342 table->set_keyread(FALSE);
3343 if (tab->type == JT_FT)
3344 table->file->ft_end();
3346 table->file->ha_index_or_rnd_end();
3347 DBUG_RETURN(filesort_retval == HA_POS_ERROR);
3361 static bool compare_record(
TABLE *table,
Field **ptr)
3363 for (; *ptr ; ptr++)
3365 if ((*ptr)->cmp_offset(table->s->rec_buff_length))
3371 static bool copy_blobs(
Field **ptr)
3373 for (; *ptr ; ptr++)
3375 if ((*ptr)->flags & BLOB_FLAG)
3382 static void free_blobs(
Field **ptr)
3384 for (; *ptr ; ptr++)
3386 if ((*ptr)->flags & BLOB_FLAG)
3393 JOIN_TAB::remove_duplicates()
3399 DBUG_ENTER(
"remove_duplicates");
3401 DBUG_ASSERT(join->
tmp_tables > 0 && table->s->tmp_table != NO_TMP_TABLE);
3402 THD_STAGE_INFO(join->thd, stage_removing_duplicates);
3404 table->reginfo.lock_type=TL_WRITE;
3412 if (item->get_tmp_table_field() && ! item->const_item())
3416 if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !
having)
3418 join->
unit->select_limit_cnt= 1;
3421 Field **first_field= table->field+ table->s->fields - field_count;
3422 offset= (field_count ?
3423 table->field[table->s->fields - field_count]->
3424 offset(table->record[0]) : 0);
3425 reclength= table->s->reclength-
offset;
3427 free_io_cache(table);
3428 table->file->info(HA_STATUS_VARIABLE);
3429 if (table->s->db_type() == heap_hton ||
3430 (!table->s->blob_fields &&
3431 ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * table->file->stats.records <
3432 join->thd->variables.sortbuff_size)))
3433 error=remove_dup_with_hash_index(join->thd, table,
3434 field_count, first_field,
3437 error=remove_dup_with_compare(join->thd, table, first_field, offset,
3440 free_blobs(first_field);
3445 static bool remove_dup_with_compare(THD *thd,
TABLE *table,
Field **first_field,
3446 ulong offset,
Item *having)
3449 char *org_record,*new_record;
3452 ulong reclength= table->s->reclength-
offset;
3453 DBUG_ENTER(
"remove_dup_with_compare");
3455 org_record=(
char*) (record=table->record[0])+
offset;
3456 new_record=(
char*) table->record[1]+offset;
3465 thd->send_kill_message();
3471 if (error == HA_ERR_RECORD_DELETED)
3476 if (error == HA_ERR_END_OF_FILE)
3480 if (having && !having->val_int())
3482 if ((error=file->ha_delete_row(record)))
3487 if (copy_blobs(first_field))
3489 my_message(ER_OUTOFMEMORY, ER(ER_OUTOFMEMORY), MYF(ME_FATALERROR));
3493 memcpy(new_record,org_record,reclength);
3501 if (error == HA_ERR_RECORD_DELETED)
3503 if (error == HA_ERR_END_OF_FILE)
3507 if (compare_record(table, first_field) == 0)
3509 if ((error=file->ha_delete_row(record)))
3515 file->position(record);
3524 file->extra(HA_EXTRA_NO_CACHE);
3527 file->extra(HA_EXTRA_NO_CACHE);
3543 static bool remove_dup_with_hash_index(THD *thd,
TABLE *table,
3545 Field **first_field,
3549 uchar *key_buffer, *key_pos, *record=table->record[0];
3552 ulong extra_length= ALIGN_SIZE(key_length)-key_length;
3553 uint *field_lengths,*field_length;
3555 DBUG_ENTER(
"remove_dup_with_hash_index");
3557 if (!my_multi_malloc(MYF(MY_WME),
3559 (uint) ((key_length + extra_length) *
3560 (
long) file->stats.records),
3562 (uint) (field_count*
sizeof(*field_lengths)),
3568 ulong total_length= 0;
3569 for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
3571 uint length= (*ptr)->sort_length();
3572 (*field_length++)= length;
3573 total_length+= length;
3575 DBUG_PRINT(
"info",(
"field_count: %u key_length: %lu total_length: %lu",
3576 field_count, key_length, total_length));
3577 DBUG_ASSERT(total_length <= key_length);
3578 key_length= total_length;
3579 extra_length= ALIGN_SIZE(key_length)-key_length;
3582 if (my_hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0,
3583 key_length, (my_hash_get_key) 0, 0, 0))
3585 my_free(key_buffer);
3597 thd->send_kill_message();
3603 if (error == HA_ERR_RECORD_DELETED)
3605 if (error == HA_ERR_END_OF_FILE)
3609 if (having && !having->val_int())
3611 if ((error=file->ha_delete_row(record)))
3617 org_key_pos= key_pos;
3618 field_length=field_lengths;
3619 for (
Field **ptr= first_field ; *ptr ; ptr++)
3621 (*ptr)->make_sort_key(key_pos,*field_length);
3622 key_pos+= *field_length++;
3625 if (my_hash_search(&hash, org_key_pos, key_length))
3628 if ((error=file->ha_delete_row(record)))
3633 if (my_hash_insert(&hash, org_key_pos))
3636 key_pos+=extra_length;
3638 my_free(key_buffer);
3639 my_hash_free(&hash);
3640 file->extra(HA_EXTRA_NO_CACHE);
3645 my_free(key_buffer);
3646 my_hash_free(&hash);
3647 file->extra(HA_EXTRA_NO_CACHE);
3685 if (!tab_ref->disable_cache)
3687 if (!(no_prev_key= tab_ref->key_err))
3695 if ((tab_ref->key_err= cp_buffer_from_ref(thd, table, tab_ref)) ||
3706 enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
3707 thd->count_cuted_fields= CHECK_FIELD_IGNORE;
3708 my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
3711 for (uint part_no= 0; part_no < ref->
key_parts; part_no++)
3717 if (s_key->
copy() & 1)
3723 thd->count_cuted_fields= save_count_cuted_fields;
3724 dbug_tmp_restore_column_map(table->write_set, old_map);
3745 if (main_join->group_fields_cache.elements)
3747 curr_join->group_fields= main_join->group_fields_cache;
3754 main_join->group_fields_cache= curr_join->group_fields;
3771 for (;
group ; group=group->next)
3774 if (!tmp || join->group_fields.push_front(tmp))
3798 DBUG_ENTER(
"test_if_item_cache_changed");
3803 for (
i=(
int) list.elements-1 ; (buff=li++) ;
i--)
3808 DBUG_PRINT(
"info", (
"idx: %d", idx));
3851 Copy_field *copy_start __attribute__((unused));
3852 res_selected_fields.empty();
3853 res_all_fields.empty();
3856 uint
i, border= all_fields.elements - elements;
3857 DBUG_ENTER(
"setup_copy_fields");
3859 if (param->field_count &&
3860 !(copy=param->copy_field=
new Copy_field[param->field_count]))
3863 param->copy_funcs.empty();
3865 for (i= 0; (pos= li++); i++)
3869 Item *real_pos= pos->real_item();
3874 if (real_pos->type() == Item::FIELD_ITEM &&
3875 !(real_pos != pos &&
3876 ((
Item_ref *)pos)->ref_type() == Item_ref::AGGREGATE_REF))
3881 if (pos->type() == Item::REF_ITEM)
3885 item->db_name= ref->db_name;
3886 item->table_name= ref->table_name;
3887 item->item_name= ref->item_name;
3890 if (item->field->flags & BLOB_FLAG)
3903 if (param->copy_funcs.push_front(pos))
3913 item->result_field=field->new_field(thd->mem_root,field->table, 1);
3919 if (!(tmp= (uchar*) sql_alloc(field->pack_length()+2)))
3923 DBUG_ASSERT (param->field_count > (uint) (copy - copy_start));
3924 copy->set(tmp, item->result_field);
3925 item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
3933 else if ((real_pos->type() == Item::FUNC_ITEM ||
3934 real_pos->type() == Item::SUBSELECT_ITEM ||
3935 real_pos->type() == Item::CACHE_ITEM ||
3936 real_pos->type() == Item::COND_ITEM) &&
3937 !real_pos->with_sum_func)
3950 if (extra_funcs.push_back(pos))
3953 else if (param->copy_funcs.push_back(pos))
3956 res_all_fields.push_back(pos);
3957 ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
3960 param->copy_field_end= copy;
3962 for (i= 0; i < border; i++)
3964 itr.sublist(res_selected_fields, elements);
3969 param->copy_funcs.concat(&extra_funcs);
3975 delete [] param->copy_field;
3976 param->copy_field=0;
3995 DBUG_ASSERT((ptr != NULL && end >= ptr) || (ptr == NULL && end == NULL));
3997 for (; ptr < end; ptr++)
3998 (*ptr->do_copy)(ptr);
4031 Item *item_field,*item;
4032 DBUG_ENTER(
"change_to_use_tmp_fields");
4034 res_selected_fields.empty();
4035 res_all_fields.empty();
4037 uint border= all_fields.elements - elements;
4038 for (uint
i= 0; (item= it++);
i++)
4041 if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
4043 else if (item->type() == Item::FIELD_ITEM)
4044 item_field= item->get_tmp_table_item(thd);
4045 else if (item->type() == Item::FUNC_ITEM &&
4046 ((
Item_func*)item)->functype() == Item_func::SUSERVAR_FUNC)
4048 field= item->get_tmp_table_field();
4059 if (!suv || !new_field)
4062 list.push_back(new_field);
4063 suv->set_arguments(list);
4069 else if ((field= item->get_tmp_table_field()))
4071 if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
4072 item_field= ((
Item_sum*) item)->result_item(field);
4078 if (item->real_item()->type() != Item::FIELD_ITEM)
4079 field->orig_table= 0;
4080 item_field->item_name= item->item_name;
4081 if (item->type() == Item::REF_ITEM)
4085 ifield->table_name= iref->table_name;
4086 ifield->db_name= iref->db_name;
4089 if (!item_field->item_name.
is_set())
4092 String str(buff,
sizeof(buff),&my_charset_bin);
4094 item->print(&str, QT_ORDINARY);
4095 item_field->item_name.
copy(str.ptr(), str.length());
4102 res_all_fields.push_back(item_field);
4103 ref_pointer_array[((
i < border)? all_fields.elements-
i-1 :
i-border)]=
4108 for (uint
i= 0;
i < border;
i++)
4110 itr.sublist(res_selected_fields, elements);
4139 Item *item, *new_item;
4140 res_selected_fields.empty();
4141 res_all_fields.empty();
4143 uint
i, border= all_fields.elements - elements;
4144 for (i= 0; (item= it++); i++)
4146 res_all_fields.push_back(new_item= item->get_tmp_table_item(thd));
4147 ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
4152 for (i= 0; i < border; i++)
4154 itr.sublist(res_selected_fields, elements);
4156 return thd->is_fatal_error;
4177 static void save_const_null_info(
JOIN *join, table_map *save_nullinfo)
4181 for (uint tableno= 0; tableno < join->
const_tables; tableno++)
4183 TABLE *tbl= (join->join_tab+tableno)->table;
4189 DBUG_ASSERT(tbl->null_row ? (tbl->status & STATUS_NULL_ROW) :
4190 !(tbl->status & STATUS_NULL_ROW));
4193 *save_nullinfo|= tbl->map;
4213 static void restore_const_null_info(
JOIN *join, table_map save_nullinfo)
4217 for (uint tableno= 0; tableno < join->
const_tables; tableno++)
4219 TABLE *tbl= (join->join_tab+tableno)->table;
4220 if ((save_nullinfo & tbl->map))
4226 tbl->null_row=
false;
4227 tbl->status&= ~STATUS_NULL_ROW;
4248 QEP_tmp_table::prepare_tmp_table()
4250 TABLE *table= join_tab->table;
4251 JOIN *join= join_tab->join;
4256 if (instantiate_tmp_table(table, join_tab->tmp_table_param->keyinfo,
4257 join_tab->tmp_table_param->start_recinfo,
4258 &join_tab->tmp_table_param->recinfo,
4259 join->select_options,
4260 join->thd->variables.big_tables,
4261 &join->thd->opt_trace))
4263 (void) table->file->extra(HA_EXTRA_WRITE_CACHE);
4264 empty_record(table);
4267 if (!table->file->inited && table->group &&
4268 join_tab->tmp_table_param->sum_func_count && table->s->keys)
4289 enum_nested_loop_state
4293 if (!join_tab->table->file->inited)
4294 prepare_tmp_table();
4295 enum_nested_loop_state rc= (*write_func)(join_tab->join,
join_tab,
4307 enum_nested_loop_state
4310 enum_nested_loop_state rc= NESTED_LOOP_OK;
4311 TABLE *table= join_tab->table;
4312 JOIN *join= join_tab->join;
4315 int tmp, new_errno= 0;
4320 if ((tmp= table->file->extra(HA_EXTRA_NO_CACHE)))
4322 DBUG_PRINT(
"error",(
"extra(HA_EXTRA_NO_CACHE) failed"));
4325 if ((tmp= table->file->ha_index_or_rnd_end()))
4327 DBUG_PRINT(
"error",(
"ha_index_or_rnd_end() failed"));
4333 return NESTED_LOOP_ERROR;
4337 table->reginfo.lock_type= TL_UNLOCK;
4339 bool in_first_read=
true;
4340 while (rc == NESTED_LOOP_OK)
4345 in_first_read=
false;
4349 error= join_tab->read_record.read_record(&join_tab->read_record);
4351 if (error > 0 || (join->thd->is_error()))
4352 rc= NESTED_LOOP_ERROR;
4355 else if (join->thd->killed)
4357 join->thd->send_kill_message();
4358 rc= NESTED_LOOP_KILLED;
4361 rc= evaluate_join_record(join, join_tab);
4365 if (join_tab->table->file->inited)