32 #include <drizzled/sql_select.h>
34 #include <drizzled/error.h>
35 #include <drizzled/gettext.h>
36 #include <drizzled/util/test.h>
37 #include <drizzled/name_resolution_context_state.h>
38 #include <drizzled/nested_join.h>
39 #include <drizzled/probes.h>
41 #include <drizzled/item/cache.h>
42 #include <drizzled/item/cmpfunc.h>
43 #include <drizzled/item/copy_string.h>
44 #include <drizzled/item/uint.h>
45 #include <drizzled/cached_item.h>
46 #include <drizzled/sql_base.h>
47 #include <drizzled/field/blob.h>
48 #include <drizzled/check_stack_overrun.h>
49 #include <drizzled/lock.h>
50 #include <drizzled/item/outer_ref.h>
52 #include <drizzled/records.h>
53 #include <drizzled/internal/iocache.h>
54 #include <drizzled/drizzled.h>
55 #include <drizzled/plugin/storage_engine.h>
56 #include <drizzled/sql_union.h>
57 #include <drizzled/optimizer/key_field.h>
58 #include <drizzled/optimizer/position.h>
59 #include <drizzled/optimizer/sargable_param.h>
60 #include <drizzled/optimizer/key_use.h>
61 #include <drizzled/optimizer/range.h>
62 #include <drizzled/optimizer/quick_range_select.h>
63 #include <drizzled/optimizer/quick_ror_intersect_select.h>
64 #include <drizzled/filesort.h>
65 #include <drizzled/sql_lex.h>
66 #include <drizzled/session.h>
67 #include <drizzled/sort_field.h>
68 #include <drizzled/select_result.h>
69 #include <drizzled/key.h>
70 #include <drizzled/my_hash.h>
76 static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
78 COND_EQUAL *inherited,
79 List<TableList> *join_list,
80 COND_EQUAL **cond_equal_ref);
82 static Item* part_of_refkey(Table *form,Field *field);
83 static bool cmp_buffer_with_ref(JoinTable *tab);
84 static void change_cond_ref_to_const(Session *session,
85 list<COND_CMP>& save_list,
90 static void copy_blobs(Field **ptr);
92 static bool eval_const_cond(COND *cond)
94 return ((Item_func*) cond)->val_int() ?
true :
false;
102 const char *subq_sj_cond_name=
"0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
104 static void copy_blobs(Field **ptr)
108 if ((*ptr)->flags & BLOB_FLAG)
110 ((Field_blob *) (*ptr))->copy();
119 uint64_t setup_tables_done_option)
122 Select_Lex *select_lex= &lex->select_lex;
123 DRIZZLE_SELECT_START(session->getQueryString()->c_str());
125 if (select_lex->master_unit()->is_union() or
126 select_lex->master_unit()->fake_select_lex)
128 res= drizzle_union(session, lex, result, &lex->unit,
129 setup_tables_done_option);
133 Select_Lex_Unit *unit= &lex->unit;
134 unit->set_limit(unit->global_parameters);
143 &select_lex->ref_pointer_array,
144 (
TableList*) select_lex->table_list.first,
145 select_lex->with_wild,
146 select_lex->item_list,
148 select_lex->order_list.size() +
149 select_lex->group_list.size(),
150 (
Order*) select_lex->order_list.first,
151 (
Order*) select_lex->group_list.first,
153 select_lex->options | session->
options |
154 setup_tables_done_option,
155 result, unit, select_lex);
208 bool fix_inner_refs(Session *session,
209 List<Item> &all_fields,
211 Item **ref_pointer_array)
215 bool direct_ref=
false;
217 List<Item_outer_ref>::iterator ref_it(select->inner_refs_list.begin());
218 while ((ref= ref_it++))
220 Item *item= ref->outer_ref;
221 Item **item_ref= ref->ref;
230 if (ref_pointer_array && !ref->found_in_select_list)
232 int el= all_fields.size();
233 ref_pointer_array[el]= item;
235 all_fields.push_front(item);
240 item_ref= ref_pointer_array + el;
243 if (ref->in_sum_func)
246 if (ref->in_sum_func->nest_level > select->nest_level)
252 for (sum_func= ref->in_sum_func; sum_func &&
253 sum_func->aggr_level >= select->nest_level;
254 sum_func= sum_func->in_sum_func)
256 if (sum_func->aggr_level == select->nest_level)
265 new_ref= direct_ref ?
266 new Item_direct_ref(ref->context, item_ref, ref->table_name,
267 ref->field_name, ref->alias_name_used) :
268 new Item_ref(ref->context, item_ref, ref->table_name,
269 ref->field_name, ref->alias_name_used);
271 ref->outer_ref= new_ref;
272 ref->ref= &ref->outer_ref;
274 if (!ref->fixed && ref->fix_fields(session, 0))
278 session->used_tables|= item->used_tables();
306 void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
308 join_tab->packed_info= TAB_INFO_HAVE_VALUE;
310 if (join_tab->table->covering_keys.test(join_tab->ref.key))
311 join_tab->packed_info |= TAB_INFO_USING_INDEX;
314 join_tab->packed_info |= TAB_INFO_USING_WHERE;
316 for (uint32_t i = 0; i < join_tab->ref.key_parts; i++)
318 if (join_tab->ref.cond_guards[i])
320 join_tab->packed_info |= TAB_INFO_FULL_SCAN_ON_NULL;
369 Item ***rref_pointer_array,
378 uint64_t select_options,
380 Select_Lex_Unit *unit,
381 Select_Lex *select_lex)
386 select_lex->context.resolve_in_select_list=
true;
388 if (select_lex->join != 0)
390 join= select_lex->join;
395 if (select_lex->linkage != DERIVED_TABLE_TYPE ||
396 (select_options & SELECT_DESCRIBE))
398 if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
408 if ((err= join->
prepare(rref_pointer_array, tables, wild_num,
409 conds, og_num, order, group, having, select_lex, unit)))
416 join->select_options= select_options;
420 join=
new Join(session, fields, select_options, result);
423 if ((err= join->
prepare(rref_pointer_array, tables, wild_num, conds, og_num, order, group, having, select_lex, unit)))
435 if (session->lex().describe & DESCRIBE_EXTENDED)
448 if (session->lex().describe & DESCRIBE_EXTENDED)
458 err|= select_lex->cleanup();
464 inline Item *and_items(Item* cond, Item *item)
466 return (cond? (
new Item_cond_and(cond, item)) : item);
473 ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table,
const key_map *keys,ha_rows limit)
484 table->reginfo.impossible_range=0;
485 if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
486 limit, 0,
false)) == 1)
488 return(select->quick->records);
493 table->reginfo.impossible_range=1;
498 return(HA_POS_ERROR);
518 for (found=0; bits & 1 ; found++,bits>>=1) ;
523 static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
526 if (a->getTable()->tablenr != b->getTable()->tablenr)
527 return static_cast<int>((a->getTable()->tablenr - b->getTable()->tablenr));
529 if (a->getKey() != b->getKey())
530 return static_cast<int>((a->getKey() - b->getKey()));
532 if (a->getKeypart() != b->getKeypart())
533 return static_cast<int>((a->getKeypart() - b->getKeypart()));
536 if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
537 test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
541 return static_cast<int>(((a->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) -
542 (b->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)));
572 table_map normal_tables,
573 Select_Lex *select_lex,
574 vector<optimizer::SargableParam> &sargables)
576 uint32_t m= max(select_lex->max_equal_elems,(uint32_t)1);
598 field= end= key_fields;
603 add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
605 for (; field != end; field++)
607 add_key_part(keyuse, field);
609 if (field->getValue()->type() == Item::NULL_ITEM &&
610 ! field->getField()->real_maybe_null())
612 field->getField()->getTable()->reginfo.not_exists_optimize= 1;
616 for (uint32_t i= 0; i < tables; i++)
627 if (*join_tab[i].on_expr_ref)
628 add_key_fields(join_tab->join, &end, &and_level,
630 join_tab[i].table->
map, sargables);
637 while ((table= li++))
639 if (table->getNestedJoin())
640 add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
646 for ( ; field != end ; field++)
647 add_key_part(keyuse,field);
665 memset(&key_end, 0,
sizeof(key_end));
666 keyuse->push_back(&key_end);
670 uint found_eq_constant= 0;
672 for (uint32_t i= 0; i < keyuse->size()-1; i++, use++)
674 if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
675 use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
677 if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
679 if (prev->getKeypart() + 1 < use->getKeypart() ||
680 ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
685 else if (use->getKeypart() != 0)
696 found_eq_constant= ! use->getUsedTables();
698 if (! use->getTable()->reginfo.join_tab->
keyuse)
700 use->getTable()->reginfo.join_tab->
keyuse= save_pos;
702 use->getTable()->reginfo.join_tab->
checked_keys.set(use->getKey());
719 for (
optimizer::KeyUse* end= keyuse+ keyuse_array->size() ; keyuse < end ; keyuse++)
730 keyuse->setTableRows(~(ha_rows) 0);
732 if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
735 for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
738 Table *tmp_table=join->all_tables[tablenr];
739 keyuse->setTableRows(max(tmp_table->
cursor->stats.records, (ha_rows)100));
747 if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
749 keyuse->setTableRows(1);
778 key_map possible_keys(0);
782 for (cur_group= join->
group_list; cur_group; cur_group= cur_group->next)
784 (*cur_group->item)->walk(&Item::collect_item_field_processor, 0, (
unsigned char*) &indexed_fields);
792 while ((item= select_items_it++))
794 item->walk(&Item::collect_item_field_processor, 0, (
unsigned char*) &indexed_fields);
802 if (indexed_fields.size() == 0)
808 cur_item= indexed_fields_it++;
809 possible_keys|= cur_item->field->part_of_key;
810 while ((cur_item= indexed_fields_it++))
812 possible_keys&= cur_item->field->part_of_key;
815 if (possible_keys.any())
848 if (jt1->dependent & jt2->table->
map)
851 if (jt2->dependent & jt1->table->
map)
860 return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
871 if (jt1->dependent & jt2->table->
map)
874 if (jt2->dependent & jt1->table->
map)
877 return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
885 uint32_t null_fields,blobs,fields,rec_length;
886 Field **f_ptr,*field;
888 null_fields= blobs= fields= rec_length=0;
889 for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
891 if (field->isReadSet())
893 uint32_t flags=field->flags;
897 if (flags & BLOB_FLAG)
902 if (!(flags & NOT_NULL_FLAG))
911 rec_length+=(join_tab->table->getNullFields() + 7)/8;
914 if (join_tab->table->maybe_null)
916 rec_length+=
sizeof(bool);
921 uint32_t blob_length=(uint32_t) (join_tab->table->
cursor->stats.mean_rec_length-
922 (join_tab->table->getRecordLength()- rec_length));
923 rec_length+= max((uint32_t)4,blob_length);
930 StoredKey *get_store_key(Session *session,
931 optimizer::KeyUse *keyuse,
932 table_map used_tables,
933 KeyPartInfo *key_part,
934 unsigned char *key_buff,
937 Item_ref *key_use_val=
static_cast<Item_ref *
>(keyuse->getVal());
938 if (! ((~used_tables) & keyuse->getUsedTables()))
940 return new store_key_const_item(session,
942 key_buff + maybe_null,
943 maybe_null ? key_buff : 0,
947 else if (key_use_val->type() == Item::FIELD_ITEM ||
948 (key_use_val->type() == Item::REF_ITEM &&
949 key_use_val->ref_type() == Item_ref::OUTER_REF &&
950 (*(Item_ref**)((Item_ref*)key_use_val)->ref)->ref_type() == Item_ref::DIRECT_REF &&
951 key_use_val->real_item()->type() == Item::FIELD_ITEM))
953 return new store_key_field(session,
955 key_buff + maybe_null,
956 maybe_null ? key_buff : 0,
958 ((Item_field*) key_use_val->real_item())->field,
959 key_use_val->full_name());
961 return new store_key_item(session,
963 key_buff + maybe_null,
964 maybe_null ? key_buff : 0,
978 Table *table= field->getTable();
987 enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
988 session->count_cuted_fields= check_flag;
989 error= item->save_in_field(field, 1);
990 session->count_cuted_fields= old_count_cuted_fields;
994 inline void add_cond_and_fix(Item **e1, Item *e2)
998 Item* res=
new Item_cond_and(*e1, e2);
1000 res->quick_fix_field();
1008 bool create_ref_for_key(Join *join,
1010 optimizer::KeyUse *org_keyuse,
1011 table_map used_tables)
1013 optimizer::KeyUse *keyuse= org_keyuse;
1014 Session *session= join->session;
1019 KeyInfo *keyinfo= NULL;
1023 key= keyuse->getKey();
1024 keyinfo= table->key_info + key;
1027 keyparts= length= 0;
1028 uint32_t found_part_ref_or_null= 0;
1036 if (! (~used_tables & keyuse->getUsedTables()))
1038 if (keyparts == keyuse->getKeypart() &&
1039 ! (found_part_ref_or_null & keyuse->getOptimizeFlags()))
1042 length+= keyinfo->key_part[keyuse->getKeypart()].store_length;
1043 found_part_ref_or_null|= keyuse->getOptimizeFlags();
1047 }
while (keyuse->getTable() == table && keyuse->getKey() == key);
1051 keyinfo=table->key_info+key;
1052 j->ref.key_parts=keyparts;
1053 j->ref.key_length=length;
1054 j->ref.key=(int) key;
1055 j->ref.key_buff= (
unsigned char*) session->mem.calloc(ALIGN_SIZE(length)*2);
1056 j->ref.key_copy=
new (session->mem) StoredKey*[keyparts + 1];
1057 j->ref.items=
new (session->mem) Item*[keyparts];
1058 j->ref.cond_guards=
new (session->mem)
bool*[keyparts];
1059 j->ref.key_buff2=j->ref.key_buff+ALIGN_SIZE(length);
1061 j->ref.null_rejecting= 0;
1062 j->ref.disable_cache=
false;
1065 StoredKey **ref_key= j->ref.key_copy;
1066 unsigned char *key_buff= j->ref.key_buff, *null_ref_key= 0;
1067 bool keyuse_uses_no_tables=
true;
1069 for (uint32_t i= 0; i < keyparts; keyuse++, i++)
1071 while (keyuse->getKeypart() != i or ((~used_tables) & keyuse->getUsedTables()))
1076 uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
1077 j->ref.items[i]= keyuse->getVal();
1078 j->ref.cond_guards[i]= keyuse->getConditionalGuard();
1079 if (keyuse->isNullRejected())
1081 j->ref.null_rejecting |= 1 << i;
1084 keyuse_uses_no_tables= keyuse_uses_no_tables && ! keyuse->getUsedTables();
1085 if (! keyuse->getUsedTables() && !(join->select_options & SELECT_DESCRIBE))
1087 store_key_item tmp(session, keyinfo->key_part[i].field,
1088 key_buff + maybe_null,
1089 maybe_null ? key_buff : 0,
1090 keyinfo->key_part[i].length, keyuse->getVal());
1091 if (session->is_fatal_error)
1099 *ref_key++= get_store_key(session,
1100 keyuse,join->const_table_map,
1101 &keyinfo->key_part[i],
1102 key_buff, maybe_null);
1110 if ((keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
1111 null_ref_key= key_buff;
1112 key_buff+=keyinfo->key_part[i].store_length;
1116 if (j->type == AM_CONST)
1118 j->table->const_table= 1;
1120 else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) || keyparts != keyinfo->key_parts || null_ref_key)
1123 j->type= null_ref_key ? AM_REF_OR_NULL : AM_REF;
1124 j->ref.null_ref_key= null_ref_key;
1126 else if (keyuse_uses_no_tables)
1195 void add_not_null_conds(Join *join)
1197 for (uint32_t i= join->const_tables; i < join->tables; i++)
1199 JoinTable *tab=join->join_tab+i;
1200 if ((tab->type == AM_REF || tab->type == AM_EQ_REF ||
1201 tab->type == AM_REF_OR_NULL) &&
1202 !tab->table->maybe_null)
1204 for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
1206 if (tab->ref.null_rejecting & (1 << keypart))
1208 Item *item= tab->ref.items[keypart];
1210 assert(item->type() == Item::FIELD_ITEM);
1211 Item_field *not_null_item= (Item_field*)item;
1212 JoinTable *referred_tab= not_null_item->field->getTable()->reginfo.join_tab;
1218 if (!referred_tab || referred_tab->join != join)
1222 notnull=
new Item_func_isnotnull(not_null_item);
1230 if (notnull->fix_fields(join->session, ¬null))
1235 add_cond_and_fix(&referred_tab->select_cond, notnull);
1257 COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
1261 if (tab == root_tab)
1263 if ((tmp= add_found_match_trig_cond(tab->first_upper, cond, root_tab)))
1264 tmp=
new Item_func_trig_cond(tmp, &tab->found);
1267 tmp->quick_fix_field();
1268 tmp->update_used_tables();
1276 void JoinTable::cleanup()
1278 safe_delete(select);
1283 size_t size= cache.end - cache.buff;
1284 global_join_buffer.sub(size);
1291 if (table->key_read)
1294 table->cursor->extra(HA_EXTRA_NO_KEYREAD);
1296 table->cursor->ha_index_or_rnd_end();
1301 table->reginfo.join_tab= 0;
1303 read_record.end_read_record();
1306 bool only_eq_ref_tables(Join *join,Order *order,table_map tables)
1308 for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
1339 if (tab->cached_eq_ref_table)
1341 return tab->eq_ref_table;
1344 tab->cached_eq_ref_table=1;
1349 return (tab->eq_ref_table=1);
1352 if (tab->
type != AM_EQ_REF || tab->table->maybe_null)
1354 return (tab->eq_ref_table=0);
1360 table_map map=tab->table->
map;
1362 for (; ref_item != end ; ref_item++)
1364 if (! (*ref_item)->const_item())
1367 for (order=start_order ; order ; order=order->next)
1369 if ((*ref_item)->eq(order->item[0],0))
1376 assert(!(order->used & map));
1381 if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
1383 return (tab->eq_ref_table= 0);
1388 for (; found && start_order ; start_order=start_order->next)
1390 if (start_order->used & map)
1395 if (start_order->depend_map & map)
1396 return (tab->eq_ref_table= 0);
1398 return tab->eq_ref_table= 1;
1422 bool in_upper_level=
false;
1426 while ((item= li++))
1433 in_upper_level=
true;
1434 cond_equal= cond_equal->upper_levels;
1436 in_upper_level=
false;
1439 *inherited_fl= in_upper_level;
1529 if (left_item->type() == Item::FIELD_ITEM &&
1530 right_item->type() == Item::FIELD_ITEM &&
1539 if (!left_field->
eq_def(right_field))
1543 bool left_copyfl, right_copyfl;
1550 if (left_field->eq(right_field))
1551 return (!(left_field->maybe_null() && !left_item_equal));
1553 if (left_item_equal && left_item_equal == right_item_equal)
1563 bool copy_item_name= test(item && item->
name >= subq_sj_cond_name &&
1564 item->
name < subq_sj_cond_name + 64);
1569 left_item_equal=
new Item_equal(left_item_equal);
1570 cond_equal->current_level.push_back(left_item_equal);
1573 left_item_equal->
name = item->
name;
1579 right_item_equal=
new Item_equal(right_item_equal);
1580 cond_equal->current_level.push_back(right_item_equal);
1583 right_item_equal->
name = item->
name;
1587 if (left_item_equal)
1590 if (! right_item_equal)
1591 left_item_equal->add((
Item_field *) right_item);
1595 left_item_equal->
merge(right_item_equal);
1598 while ((li++) != right_item_equal) {};
1605 if (right_item_equal)
1607 right_item_equal->add((
Item_field *) left_item);
1610 right_item_equal->
name = item->
name;
1618 cond_equal->current_level.push_back(item_equal);
1630 Item *const_item= 0;
1632 if (left_item->type() == Item::FIELD_ITEM &&
1634 right_item->const_item())
1637 const_item= right_item;
1639 else if (right_item->type() == Item::FIELD_ITEM &&
1644 const_item= left_item;
1648 field_item->result_type() == const_item->result_type())
1652 if (field_item->result_type() == STRING_RESULT)
1659 eq_item->set_cmp_func();
1664 if ((cs != ((
Item_func *) item)->compare_collation()) || !cs->coll->propagate())
1671 field_item->field, ©fl);
1675 cond_equal->current_level.push_back(item_equal);
1684 item_equal->add(const_item);
1688 item_equal=
new Item_equal(const_item, field_item);
1689 cond_equal->current_level.push_back(item_equal);
1729 uint32_t n= left_row->cols();
1730 for (uint32_t i= 0 ; i < n; i++)
1733 Item *left_item= left_row->element_index(i);
1734 Item *right_item= right_row->element_index(i);
1735 if (left_item->type() == Item::ROW_ITEM &&
1736 right_item->type() == Item::ROW_ITEM)
1741 cond_equal, eq_list);
1744 session->lex().current_select->cond_count++;
1750 session->lex().current_select->cond_count++;
1757 eq_item->set_cmp_func();
1759 eq_list->push_back(eq_item);
1796 if (item->type() == Item::FUNC_ITEM &&
1797 ((
Item_func*) item)->functype() == Item_func::EQ_FUNC)
1802 if (left_item->type() == Item::ROW_ITEM &&
1803 right_item->type() == Item::ROW_ITEM)
1805 session->lex().current_select->cond_count--;
1809 cond_equal, eq_list);
1886 cond_equal.upper_levels= inherited;
1888 if (cond->type() == Item::COND_ITEM)
1891 bool and_level= ((
Item_cond*) cond)->functype() ==
1892 Item_func::COND_AND_FUNC;
1906 while ((item= li++))
1920 while ((item_equal= it++))
1922 item_equal->fix_length_and_dec();
1923 item_equal->update_used_tables();
1924 set_if_bigger(session->lex().current_select->max_equal_elems,
1925 item_equal->members());
1936 while ((item= li++))
1947 li.replace(new_item);
1952 args->concat(&eq_list);
1953 args->concat((
List<Item> *)&cond_equal.current_level);
1956 else if (cond->type() == Item::FUNC_ITEM)
1971 int n= cond_equal.current_level.size() + eq_list.size();
1975 return new Item_int((int64_t) 1,1);
1979 if ((item_equal= cond_equal.current_level.pop()))
1981 item_equal->fix_length_and_dec();
1982 item_equal->update_used_tables();
1988 set_if_bigger(session->lex().current_select->max_equal_elems,
1989 item_equal->members());
2002 while ((item_equal= it++))
2004 item_equal->fix_length_and_dec();
2005 item_equal->update_used_tables();
2006 set_if_bigger(session->lex().current_select->max_equal_elems,
2007 item_equal->members());
2009 and_cond->cond_equal= cond_equal;
2010 args->concat((
List<Item> *)&cond_equal.current_level);
2021 unsigned char *is_subst_valid= (
unsigned char *) 1;
2022 cond= cond->
compile(&Item::subst_argument_checker,
2024 &Item::equal_fields_propagator,
2025 (
unsigned char *) inherited);
2026 cond->update_used_tables();
2106 cond->update_used_tables();
2108 if (cond->type() == Item::COND_ITEM && ((
Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
2112 else if (cond->type() == Item::FUNC_ITEM &&
2113 ((
Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2116 cond_equal->current_level.push_back((
Item_equal *) cond);
2121 cond_equal->upper_levels= inherited;
2122 inherited= cond_equal;
2124 *cond_equal_ref= cond_equal;
2131 while ((table= li++))
2136 &table->getNestedJoin()->join_list : NULL;
2172 void *table_join_idx)
2194 cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
2196 return cmp < 0 ? -1 : (cmp ? 1 : 0);
2242 if (((
Item *) item_equal)->const_item() && !item_equal->
val_int())
2243 return new Item_int((int64_t) 0,1);
2244 Item *item_const= item_equal->get_const();
2245 Item_equal_iterator it(item_equal->begin());
2253 head= item_equal->get_first();
2257 while ((item_field= it++))
2263 if (item_const && upper->get_const())
2269 Item_equal_iterator li(item_equal->begin());
2270 while ((item= li++) != item_field)
2279 if (item == item_field)
2283 eq_list.push_back(eq_item);
2292 eq_item->set_cmp_func();
2297 if (!cond && !&eq_list.front())
2301 return new Item_int((int64_t) 1,1);
2308 eq_list.push_back(eq_item);
2317 assert(cond->type() == Item::COND_ITEM);
2318 ((
Item_cond *) cond)->add_at_head(&eq_list);
2322 cond->update_used_tables();
2358 if (cond->type() == Item::COND_ITEM)
2362 bool and_level= ((
Item_cond*) cond)->functype() ==
2363 Item_func::COND_AND_FUNC;
2367 cond_list->disjoin((
List<Item> *) &cond_equal->current_level);
2370 while ((item_equal= it++))
2378 while ((item= li++))
2386 if (new_item != item)
2387 li.replace(new_item);
2393 while ((item_equal= it++))
2399 if (cond->type() != Item::COND_ITEM)
2404 if (cond->type() == Item::COND_ITEM && !((
Item_cond*)cond)->argument_list()->size())
2410 else if (cond->type() == Item::FUNC_ITEM &&
2411 ((
Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2415 if (cond_equal && &cond_equal->current_level.front() == item_equal)
2424 cond->transform(&Item::replace_equal_field, 0);
2447 if (cond->type() == Item::COND_ITEM)
2452 while ((item= li++))
2455 else if (cond->type() == Item::FUNC_ITEM &&
2456 ((
Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2459 bool contained_const= item_equal->get_const() != NULL;
2461 if (!contained_const && item_equal->get_const())
2464 Item_equal_iterator it(item_equal->begin());
2466 while ((item_field= it++))
2468 Field *field= item_field->field;
2469 JoinTable *stat= field->getTable()->reginfo.join_tab;
2470 key_map possible_keys= field->
key_start;
2471 possible_keys&= field->getTable()->keys_in_use_for_query;
2479 if (possible_keys.any())
2481 Table *field_tab= field->getTable();
2483 for (use= stat->
keyuse; use && use->getTable() == field_tab; use++)
2484 if (possible_keys.test(use->getKey()) &&
2485 field_tab->
key_info[use->getKey()].key_part[use->getKeypart()].field ==
2487 field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
2498 static void change_cond_ref_to_const(Session *session,
2499 list<COND_CMP>& save_list,
2505 if (cond->type() == Item::COND_ITEM)
2507 bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2508 List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
2511 change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
2516 if (cond->eq_cmp_result() == Item::COND_OK)
2521 Item_bool_func2 *func= (Item_bool_func2*) cond;
2522 Item **args= func->arguments();
2523 Item *left_item= args[0];
2524 Item *right_item= args[1];
2525 Item_func::Functype functype= func->functype();
2527 if (right_item->eq(field,0) && left_item != value &&
2528 right_item->cmp_context == field->cmp_context &&
2529 (left_item->result_type() != STRING_RESULT ||
2530 value->result_type() != STRING_RESULT ||
2531 left_item->collation.collation == value->collation.collation))
2533 Item *tmp=value->clone_item();
2536 tmp->collation.set(right_item->collation);
2538 func->update_used_tables();
2539 if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2540 and_father != cond &&
2541 ! left_item->const_item())
2544 save_list.push_back( COND_CMP(and_father, func) );
2546 func->set_cmp_func();
2549 else if (left_item->eq(field,0) && right_item != value &&
2550 left_item->cmp_context == field->cmp_context &&
2551 (right_item->result_type() != STRING_RESULT ||
2552 value->result_type() != STRING_RESULT ||
2553 right_item->collation.collation == value->collation.collation))
2555 Item *tmp= value->clone_item();
2558 tmp->collation.set(left_item->collation);
2561 func->update_used_tables();
2562 if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2563 and_father != cond &&
2564 ! right_item->const_item())
2569 save_list.push_back( COND_CMP(and_father, func) );
2571 func->set_cmp_func();
2591 if (conds->type() == Item::COND_ITEM)
2596 while ((item= li++))
2601 if (cnd->argument_list()->size() == 1)
2603 return &cnd->argument_list()->front();
2613 static void propagate_cond_constants(Session *session,
2614 list<COND_CMP>& save_list,
2618 if (cond->type() == Item::COND_ITEM)
2620 bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2621 List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
2623 list<COND_CMP> save;
2626 propagate_cond_constants(session, save, and_level ? cond : item, item);
2631 for (list<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
2633 Item **args= iter->second->arguments();
2634 if (not args[0]->const_item())
2636 change_cond_ref_to_const(session, save, iter->first,
2637 iter->first, args[0], args[1] );
2642 else if (and_father != cond && !cond->marker)
2644 if (cond->type() == Item::FUNC_ITEM &&
2645 (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
2646 ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
2648 Item_func_eq *func=(Item_func_eq*) cond;
2649 Item **args= func->arguments();
2650 bool left_const= args[0]->const_item();
2651 bool right_const= args[1]->const_item();
2652 if (!(left_const && right_const) && args[0]->result_type() == args[1]->result_type())
2656 resolve_const_item(session, &args[1], args[0]);
2657 func->update_used_tables();
2658 change_cond_ref_to_const(session, save_list, and_father, and_father,
2661 else if (left_const)
2663 resolve_const_item(session, &args[0], args[1]);
2664 func->update_used_tables();
2665 change_cond_ref_to_const(session, save_list, and_father, and_father,
2765 TableList *next_emb= next_tab->table->pos_in_table_list->getEmbedding();
2766 Join *join= next_tab->join;
2781 for (;next_emb; next_emb= next_emb->getEmbedding())
2783 next_emb->getNestedJoin()->counter_++;
2784 if (next_emb->getNestedJoin()->counter_ == 1)
2794 if (next_emb->getNestedJoin()->join_list.size() != next_emb->getNestedJoin()->counter_)
2808 COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
2810 Session *session= join->session;
2814 *cond_value= Item::COND_TRUE;
2830 list<COND_CMP> temp;
2831 propagate_cond_constants(session, temp, conds, conds);
2853 if (cond->type() == Item::COND_ITEM)
2855 bool and_level= (((
Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
2858 Item::cond_result tmp_cond_value;
2859 bool should_fix_fields=
false;
2861 *cond_value= Item::COND_UNDEF;
2863 while ((item= li++))
2870 else if (item != new_item)
2872 li.replace(new_item);
2873 should_fix_fields=
true;
2876 if (*cond_value == Item::COND_UNDEF)
2878 *cond_value= tmp_cond_value;
2881 switch (tmp_cond_value)
2884 if (and_level || (*cond_value == Item::COND_FALSE))
2885 *cond_value= tmp_cond_value;
2887 case Item::COND_FALSE:
2890 *cond_value= tmp_cond_value;
2891 return (
COND *) NULL;
2894 case Item::COND_TRUE:
2897 *cond_value= tmp_cond_value;
2898 return (
COND *) NULL;
2901 case Item::COND_UNDEF:
2906 if (should_fix_fields)
2908 cond->update_used_tables();
2911 if (! ((
Item_cond*) cond)->argument_list()->size() || *cond_value != Item::COND_OK)
2913 return (
COND*) NULL;
2916 if (((
Item_cond*) cond)->argument_list()->size() == 1)
2919 item= &((
Item_cond*) cond)->argument_list()->front();
2920 ((
Item_cond*) cond)->argument_list()->clear();
2925 else if (cond->type() == Item::FUNC_ITEM && ((
Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
2938 Item **args= func->arguments();
2939 if (args[0]->type() == Item::FIELD_ITEM)
2942 if (field->flags & AUTO_INCREMENT_FLAG
2943 && ! field->getTable()->maybe_null
2944 && session->
options & OPTION_AUTO_IS_NULL
2952 session->read_first_successful_insert_id_in_prev_stmt(), MY_INT64_NUM_DECIMAL_DIGITS));
2959 cond->fix_fields(session, &cond);
2969 ((field->type() == DRIZZLE_TYPE_DATE) || (field->type() == DRIZZLE_TYPE_DATETIME))
2970 && (field->flags & NOT_NULL_FLAG)
2971 && ! field->
table->maybe_null)
2980 cond->fix_fields(session, &cond);
2984 if (cond->const_item())
2986 *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
2987 return (
COND *) NULL;
2990 else if (cond->const_item() && !cond->is_expensive())
3002 *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
3003 return (
COND *) NULL;
3005 else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
3010 if (left_item->
eq(right_item,1))
3014 return (
COND*) NULL;
3018 *cond_value= Item::COND_OK;
3045 static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
3047 return r->const_item() &&
3049 (Arg_comparator::can_compare_as_dates(l, r, 0) ||
3051 (r->result_type() == l->result_type() &&
3053 (l->result_type() != STRING_RESULT ||
3054 l->collation.collation == r->collation.collation)));
3062 if (cond->type() == Item::COND_ITEM)
3064 bool and_level= (((
Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
3078 else if (and_level ==
false)
3083 return and_level ?
false :
true;
3085 else if (cond->eq_cmp_result() != Item::COND_OK)
3088 if (func->functype() != Item_func::EQUAL_FUNC &&
3089 func->functype() != Item_func::EQ_FUNC)
3097 if (left_item->
eq(comp_item,1))
3099 if (test_if_equality_guarantees_uniqueness (left_item, right_item))
3103 return right_item->
eq(*const_item, 1);
3105 *const_item=right_item;
3109 else if (right_item->
eq(comp_item,1))
3111 if (test_if_equality_guarantees_uniqueness (right_item, left_item))
3115 return left_item->
eq(*const_item, 1);
3117 *const_item=left_item;
3139 Table *table= join->tmp_table;
3141 Next_select_func end_select;
3146 if (table->group && tmp_tbl->sum_func_count &&
3147 !tmp_tbl->precomputed_group_by)
3149 if (table->getShare()->sizeKeys())
3158 else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
3160 end_select= end_write_group;
3164 end_select= end_write;
3165 if (tmp_tbl->precomputed_group_by)
3174 memcpy(tmp_tbl->items_to_copy + tmp_tbl->func_count,
3176 sizeof(
Item*)*tmp_tbl->sum_func_count);
3177 tmp_tbl->items_to_copy[tmp_tbl->func_count+tmp_tbl->sum_func_count]= 0;
3183 if ((join->sort_and_group) && !tmp_tbl->precomputed_group_by)
3185 end_select= end_send_group;
3189 end_select= end_send;
3212 join->tmp_table= table;
3213 join->fields= fields;
3217 table->
cursor->extra(HA_EXTRA_WRITE_CACHE);
3218 table->emptyRecord();
3219 if (table->group && join->tmp_table_param.sum_func_count &&
3220 table->getShare()->sizeKeys() && !table->
cursor->inited)
3223 tmp_error= table->
cursor->startIndexScan(0, 0);
3226 table->print_error(tmp_error, MYF(0));
3236 join->join_tab[join->
tables-1].next_select= end_select;
3238 join_tab=join->join_tab+join->const_tables;
3241 join->send_records=0;
3242 if (join->
tables == join->const_tables)
3248 if (!join->conds || join->conds->
val_int())
3250 error= (*end_select)(join, 0, 0);
3251 if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
3252 error= (*end_select)(join, 0, 1);
3259 join->examined_rows++;
3260 join->session->row_count++;
3261 assert(join->examined_rows <= 1);
3263 else if (join->send_row_on_empty_set())
3266 rc= join->result->send_data(*columns_list);
3273 if (error == NESTED_LOOP_OK || error == NESTED_LOOP_NO_MORE_ROWS)
3278 if (error == NESTED_LOOP_QUERY_LIMIT)
3280 error= NESTED_LOOP_OK;
3284 if (error == NESTED_LOOP_NO_MORE_ROWS)
3286 error= NESTED_LOOP_OK;
3289 if (error == NESTED_LOOP_OK)
3302 if (join->result->send_eof())
3315 int tmp, new_errno= 0;
3316 if ((tmp=table->
cursor->extra(HA_EXTRA_NO_CACHE)))
3321 if ((tmp=table->
cursor->ha_index_or_rnd_end()))
3328 table->print_error(new_errno,MYF(0));
3331 return(join->session->
is_error() ? -1 : rc);
3334 enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab,
bool end_of_records)
3336 enum_nested_loop_state rc;
3340 rc= flush_cached_records(join,join_tab,
false);
3341 if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS)
3342 rc=
sub_select(join,join_tab,end_of_records);
3346 if (join->session->getKilled())
3348 join->session->send_kill_message();
3349 return NESTED_LOOP_KILLED;
3352 if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
3354 if (! join_tab->cache.store_record_in_cache())
3355 return NESTED_LOOP_OK;
3356 return flush_cached_records(join,join_tab,
false);
3358 rc= flush_cached_records(join, join_tab,
true);
3359 if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS)
3361 rc=
sub_select(join, join_tab, end_of_records);
3488 join_tab->table->null_row=0;
3491 return (*join_tab->next_select)(join,join_tab+1,end_of_records);
3501 if (join_tab < join->join_tab + join->
tables - 1)
3503 rc= (*join_tab->next_select)(join, join_tab + 1, 0);
3528 error= (*join_tab->read_first_record)(join_tab);
3536 while (rc == NESTED_LOOP_OK && join->
return_tab >= join_tab)
3538 error= info->read_record(info);
3542 if (rc == NESTED_LOOP_NO_MORE_ROWS and join_tab->
last_inner && !join_tab->
found)
3547 if (rc == NESTED_LOOP_NO_MORE_ROWS)
3555 int safe_index_read(JoinTable *tab)
3558 Table *table= tab->table;
3559 if ((error=table->cursor->index_read_map(table->getInsertRecord(),
3561 make_prev_keypart_map(tab->ref.key_parts),
3562 HA_READ_KEY_EXACT)))
3563 return table->report_error(error);
3582 Table *table= tab->table;
3583 if (table->status & STATUS_GARBAGE)
3586 if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3588 error= HA_ERR_KEY_NOT_FOUND;
3593 (
unsigned char*) tab->ref.
key_buff,
3594 make_prev_keypart_map(tab->ref.
key_parts),
3599 table->status= STATUS_NOT_FOUND;
3600 tab->table->mark_as_null_row();
3601 table->emptyRecord();
3602 if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3608 table->storeRecord();
3610 else if (!(table->status & ~STATUS_NULL_ROW))
3613 table->restoreRecord();
3616 return table->status ? -1 : 0;
3635 int join_read_key(JoinTable *tab)
3638 Table *table= tab->table;
3640 if (!table->cursor->inited)
3642 error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3645 table->print_error(error, MYF(0));
3650 if (cmp_buffer_with_ref(tab) ||
3651 (table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
3653 if (tab->ref.key_err)
3655 table->status=STATUS_NOT_FOUND;
3658 error=table->cursor->index_read_map(table->getInsertRecord(),
3660 make_prev_keypart_map(tab->ref.key_parts),
3662 if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3664 return table->report_error(error);
3668 return table->status ? -1 : 0;
3689 int join_read_always_key(JoinTable *tab)
3692 Table *table= tab->table;
3695 if (!table->cursor->inited)
3697 error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3700 return table->report_error(error);
3705 for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
3707 if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
3713 if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3718 if ((error=table->cursor->index_read_map(table->getInsertRecord(),
3720 make_prev_keypart_map(tab->ref.key_parts),
3721 HA_READ_KEY_EXACT)))
3723 if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3725 return table->report_error(error);
3740 Table *table= tab->table;
3742 if (!table->
cursor->inited)
3749 if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3756 make_prev_keypart_map(tab->ref.
key_parts))))
3758 if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3768 int join_no_more_records(ReadRecord *)
3773 int join_read_next_same_diff(ReadRecord *info)
3775 Table *table= info->table;
3776 JoinTable *tab=table->reginfo.join_tab;
3777 if (tab->insideout_match_tab->found_match)
3779 KeyInfo *key= tab->table->key_info + tab->index;
3784 key_copy(tab->insideout_buf, info->record, key, 0);
3786 if ((error=table->cursor->index_next_same(table->getInsertRecord(),
3788 tab->ref.key_length)))
3790 if (error != HA_ERR_END_OF_FILE)
3791 return table->report_error(error);
3792 table->status= STATUS_GARBAGE;
3795 }
while (!
key_cmp(tab->table->key_info[tab->index].key_part,
3796 tab->insideout_buf, key->key_length));
3797 tab->insideout_match_tab->found_match= 0;
3802 return join_read_next_same(info);
3806 int join_read_next_same(ReadRecord *info)
3809 Table *table= info->table;
3810 JoinTable *tab=table->reginfo.join_tab;
3812 if ((error=table->cursor->index_next_same(table->getInsertRecord(),
3814 tab->ref.key_length)))
3816 if (error != HA_ERR_END_OF_FILE)
3817 return table->report_error(error);
3818 table->status= STATUS_GARBAGE;
3825 int join_read_prev_same(ReadRecord *info)
3828 Table *table= info->table;
3829 JoinTable *tab=table->reginfo.join_tab;
3831 if ((error=table->cursor->index_prev(table->getInsertRecord())))
3833 return table->report_error(error);
3837 tab->ref.key_length))
3839 table->status=STATUS_NOT_FOUND;
3846 int join_init_quick_read_record(JoinTable *tab)
3848 if (test_if_quick_select(tab) == -1)
3853 return join_init_read_record(tab);
3856 int init_read_record_seq(JoinTable *tab)
3858 tab->read_record.init_reard_record_sequential();
3860 if (tab->read_record.cursor->startTableScan(1))
3864 return (*tab->read_record.read_record)(&tab->read_record);
3867 int test_if_quick_select(JoinTable *tab)
3869 safe_delete(tab->select->quick);
3871 return tab->select->test_quick_select(tab->join->session, tab->keys,
3872 (table_map) 0, HA_POS_ERROR, 0,
false);
3875 int join_init_read_record(JoinTable *tab)
3877 if (tab->select && tab->select->quick && tab->select->quick->reset())
3882 if (tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1,
true))
3887 return (*tab->read_record.read_record)(&tab->read_record);
3890 int join_read_first(JoinTable *tab)
3893 Table *table=tab->table;
3894 if (!table->key_read && table->covering_keys.test(tab->index) &&
3898 table->cursor->extra(HA_EXTRA_KEYREAD);
3900 tab->table->status= 0;
3901 tab->read_record.table=table;
3902 tab->read_record.cursor=table->cursor;
3903 tab->read_record.index=tab->index;
3904 tab->read_record.record=table->getInsertRecord();
3905 if (tab->insideout_match_tab)
3907 tab->read_record.do_insideout_scan= tab;
3908 tab->read_record.read_record=join_read_next_different;
3909 tab->insideout_match_tab->found_match= 0;
3913 tab->read_record.read_record=join_read_next;
3914 tab->read_record.do_insideout_scan= 0;
3917 if (!table->cursor->inited)
3919 error= table->cursor->startIndexScan(tab->index, tab->sorted);
3922 table->report_error(error);
3926 if ((error=tab->table->cursor->index_first(tab->table->getInsertRecord())))
3928 if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3929 table->report_error(error);
3936 int join_read_next_different(ReadRecord *info)
3938 JoinTable *tab= info->do_insideout_scan;
3939 if (tab->insideout_match_tab->found_match)
3941 KeyInfo *key= tab->table->key_info + tab->index;
3946 key_copy(tab->insideout_buf, info->record, key, 0);
3948 if ((error=info->cursor->index_next(info->record)))
3950 return info->table->report_error(error);
3952 }
while (!
key_cmp(tab->table->key_info[tab->index].key_part,
3953 tab->insideout_buf, key->key_length));
3954 tab->insideout_match_tab->found_match= 0;
3959 return join_read_next(info);
3963 int join_read_next(ReadRecord *info)
3966 if ((error=info->cursor->index_next(info->record)))
3968 return info->table->report_error(error);
3973 int join_read_last(JoinTable *tab)
3975 Table *table=tab->table;
3977 if (!table->key_read && table->covering_keys.test(tab->index) &&
3981 table->cursor->extra(HA_EXTRA_KEYREAD);
3983 tab->table->status=0;
3984 tab->read_record.read_record=join_read_prev;
3985 tab->read_record.table=table;
3986 tab->read_record.cursor=table->cursor;
3987 tab->read_record.index=tab->index;
3988 tab->read_record.record=table->getInsertRecord();
3990 if (!table->cursor->inited)
3992 error= table->cursor->startIndexScan(tab->index, 1);
3995 return table->report_error(error);
3999 if ((error= tab->table->cursor->index_last(tab->table->getInsertRecord())))
4001 return table->report_error(error);
4007 int join_read_prev(ReadRecord *info)
4010 if ((error= info->cursor->index_prev(info->record)))
4012 return info->table->report_error(error);
4027 if ((res= join_read_always_key(tab)) >= 0)
4034 return safe_index_read(tab);
4037 int join_read_next_same_or_null(ReadRecord *info)
4040 if ((error= join_read_next_same(info)) >= 0)
4044 JoinTable *tab= info->table->reginfo.join_tab;
4047 if (*tab->ref.null_ref_key)
4052 *tab->ref.null_ref_key= 1;
4054 return safe_index_read(tab);
4062 if (!join->first_record or end_of_records or (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
4064 if (join->first_record or
4065 (end_of_records && !join->group && !join->group_optimized_away))
4067 if (idx < (
int) join->send_group_parts)
4071 if (!join->first_record)
4073 List<Item>::iterator it(join->fields->begin());
4078 while ((item= it++))
4080 item->no_rows_in_result();
4083 if (join->having && join->having->val_int() == 0)
4089 if (join->do_send_rows)
4090 error=join->result->send_data(*join->fields) ? 1 : 0;
4091 join->send_records++;
4093 if (join->rollup.getState() != Rollup::STATE_NONE && error <= 0)
4095 if (join->rollup_send_data((uint32_t) (idx+1)))
4102 return(NESTED_LOOP_ERROR);
4107 return(NESTED_LOOP_OK);
4110 if (join->send_records >= join->unit->select_limit_cnt &&
4113 if (!(join->select_options & OPTION_FOUND_ROWS))
4115 return(NESTED_LOOP_QUERY_LIMIT);
4117 join->do_send_rows=0;
4118 join->unit->select_limit_cnt = HA_POS_ERROR;
4120 else if (join->send_records >= join->fetch_limit)
4130 ok_code= NESTED_LOOP_CURSOR_LIMIT;
4138 return(NESTED_LOOP_OK);
4140 join->first_record=1;
4141 test_if_item_cache_changed(join->group_fields);
4143 if (idx < (
int) join->send_group_parts)
4150 if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
4152 return(NESTED_LOOP_ERROR);
4157 if (update_sum_func(join->sum_funcs))
4158 return(NESTED_LOOP_ERROR);
4159 return(NESTED_LOOP_OK);
4164 Table *table=join->tmp_table;
4167 if (join->session->getKilled())
4169 join->session->send_kill_message();
4170 return NESTED_LOOP_KILLED;
4173 if (!join->first_record or end_of_records or (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
4175 if (join->first_record or (end_of_records && !join->group))
4177 int send_group_parts= join->send_group_parts;
4178 if (idx < send_group_parts)
4180 if (!join->first_record)
4186 copy_sum_funcs(join->sum_funcs, join->sum_funcs_end[send_group_parts]);
4188 if (!join->having || join->having->val_int())
4190 int error= table->cursor->insertRecord(table->getInsertRecord());
4194 my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
4195 return NESTED_LOOP_ERROR;
4199 if (join->rollup.getState() != Rollup::STATE_NONE)
4201 if (join->rollup_write_data((uint32_t) (idx+1), table))
4203 return NESTED_LOOP_ERROR;
4209 return NESTED_LOOP_OK;
4217 return NESTED_LOOP_OK;
4219 join->first_record=1;
4220 test_if_item_cache_changed(join->group_fields);
4222 if (idx < (
int) join->send_group_parts)
4225 if (
copy_funcs(join->tmp_table_param.items_to_copy, join->session))
4227 return NESTED_LOOP_ERROR;
4230 if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
4232 return NESTED_LOOP_ERROR;
4235 return NESTED_LOOP_OK;
4239 if (update_sum_func(join->sum_funcs))
4241 return NESTED_LOOP_ERROR;
4244 return NESTED_LOOP_OK;
4257 bool test_if_ref(Item_field *left_item,Item *right_item)
4259 Field *field=left_item->field;
4261 if (not field->getTable()->const_table && !field->getTable()->maybe_null)
4263 Item *ref_item=part_of_refkey(field->getTable(),field);
4264 if (ref_item && ref_item->eq(right_item,1))
4266 right_item= right_item->real_item();
4267 if (right_item->type() == Item::FIELD_ITEM)
4268 return (field->eq_def(((Item_field *) right_item)->field));
4270 else if (right_item->type() == Item::CACHE_ITEM)
4271 return ((Item_cache *)right_item)->eq_def (field);
4272 if (right_item->const_item() && !(right_item->is_null()))
4287 if (field->binary() &&
4288 field->real_type() != DRIZZLE_TYPE_VARCHAR &&
4289 field->decimals() == 0)
4330 COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table,
bool exclude_expensive_cond)
4332 if (used_table && !(cond->used_tables() & used_table) &&
4339 !((used_table & 1) && cond->is_expensive()))
4344 if (cond->type() == Item::COND_ITEM)
4346 if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
4349 Item_cond_and *new_cond=
new Item_cond_and;
4350 if (new_cond == NULL)
4354 List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
4358 Item *fix= make_cond_for_table(item,tables,used_table,
4359 exclude_expensive_cond);
4362 new_cond->argument_list()->push_back(fix);
4365 switch (new_cond->argument_list()->size())
4371 return &new_cond->argument_list()->front();
4378 new_cond->quick_fix_field();
4379 new_cond->used_tables_cache= ((Item_cond_and*) cond)->used_tables_cache & tables;
4385 Item_cond_or *new_cond=
new Item_cond_or;
4386 if (new_cond == NULL)
4390 List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
4394 Item *fix= make_cond_for_table(item,tables,0L, exclude_expensive_cond);
4399 new_cond->argument_list()->push_back(fix);
4405 new_cond->quick_fix_field();
4406 new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
4407 new_cond->top_level_item();
4419 if (cond->marker == 3 || (cond->used_tables() & ~tables) ||
4424 (!used_table && exclude_expensive_cond && cond->is_expensive()))
4429 if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
4438 if (((Item_func*) cond)->functype() == Item_func::EQ_FUNC)
4440 Item *left_item= ((Item_func*) cond)->arguments()[0];
4441 Item *right_item= ((Item_func*) cond)->arguments()[1];
4442 if (left_item->type() == Item::FIELD_ITEM && test_if_ref((Item_field*) left_item,right_item))
4447 if (right_item->type() == Item::FIELD_ITEM && test_if_ref((Item_field*) right_item,left_item))
4457 static Item *part_of_refkey(Table *table,Field *field)
4459 if (!table->reginfo.join_tab)
4464 uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
4467 KeyPartInfo *key_part=
4468 table->key_info[table->reginfo.join_tab->ref.key].key_part;
4471 for (part=0 ; part < ref_parts ; part++)
4473 if (table->reginfo.join_tab->ref.cond_guards[part])
4479 for (part=0 ; part < ref_parts ; part++,key_part++)
4481 if (field->eq(key_part->field) &&
4482 !(key_part->key_part_flag & HA_PART_KEY_SEG) &&
4485 !(field->real_maybe_null()))
4487 return table->reginfo.join_tab->ref.items[part];
4525 key_part= table->
key_info[idx].key_part;
4526 key_part_end= key_part + table->
key_info[idx].key_parts;
4527 key_part_map const_key_parts=table->const_key_parts[idx];
4529 bool on_primary_key=
false;
4531 for (; order ; order=order->next, const_key_parts>>=1)
4540 for (; const_key_parts & 1 ; const_key_parts>>= 1)
4545 if (key_part == key_part_end)
4552 if (!on_primary_key &&
4553 (table->
cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
4554 table->getShare()->hasPrimaryKey())
4556 on_primary_key=
true;
4557 key_part= table->
key_info[table->getShare()->getPrimaryKey()].key_part;
4558 key_part_end=key_part+table->
key_info[table->getShare()->getPrimaryKey()].key_parts;
4559 const_key_parts=table->const_key_parts[table->getShare()->getPrimaryKey()];
4561 for (; const_key_parts & 1 ; const_key_parts>>= 1)
4570 if (key_part == key_part_end && reverse == 0)
4581 if (key_part->field != field)
4587 flag= ((order->asc == !(key_part->key_part_flag & HA_REVERSE_SORT)) ?
4589 if (reverse && flag != reverse)
4598 uint32_t used_key_parts_secondary= table->
key_info[idx].key_parts;
4599 uint32_t used_key_parts_pk= (uint32_t) (key_part - table->
key_info[table->getShare()->getPrimaryKey()].key_part);
4600 *used_key_parts= used_key_parts_pk + used_key_parts_secondary;
4602 if (reverse == -1 &&
4603 (!(table->index_flags(idx) &
4605 !(table->index_flags(table->getShare()->getPrimaryKey()) &
4611 *used_key_parts= (uint32_t) (key_part - table->
key_info[idx].key_part);
4612 if (reverse == -1 &&
4613 !(table->index_flags(idx) & HA_READ_PREV))
4638 for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
4640 if (! key_part->field->eq(ref_key_part->field))
4663 uint32_t ref_key_parts,
4664 const key_map *usable_keys)
4667 uint32_t min_length= UINT32_MAX;
4668 uint32_t best= MAX_KEY;
4671 KeyPartInfo *ref_key_part_end= ref_key_part + ref_key_parts;
4673 for (nr= 0 ; nr < table->getShare()->sizeKeys() ; nr++)
4675 if (usable_keys->test(nr) &&
4676 table->
key_info[nr].key_length < min_length &&
4677 table->
key_info[nr].key_parts >= ref_key_parts &&
4679 ref_key_part_end) &&
4682 min_length= table->
key_info[nr].key_length;
4722 for (uint32_t keynr= 0; keynr < table->getShare()->sizeKeys(); keynr++)
4724 if (keynr == table->getShare()->getPrimaryKey() ||
4725 (table->
key_info[keynr].flags & HA_NOSAME))
4731 for (key_part=keyinfo->key_part,
4732 key_part_end=key_part+ keyinfo->key_parts;
4733 key_part < key_part_end;
4736 if (key_part->field->maybe_null() || ! find_func(key_part->field, data))
4742 if (key_part == key_part_end)
4768 for (
Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4770 Item *item= (*tmp_group->item)->real_item();
4771 if (item->type() == Item::FIELD_ITEM &&
4801 while ((item= li++))
4803 if (item->type() == Item::FIELD_ITEM &&
4841 uint32_t ref_key_parts;
4842 int order_direction;
4843 uint32_t used_key_parts;
4844 Table *table=tab->table;
4846 key_map usable_keys;
4855 for (
Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4857 Item *item= (*tmp_order->item)->real_item();
4858 if (item->type() != Item::FIELD_ITEM)
4860 usable_keys.reset();
4863 usable_keys&= ((
Item_field*) item)->field->part_of_sortkey;
4864 if (usable_keys.none())
4874 ref_key= tab->ref.
key;
4876 if (tab->
type == AM_REF_OR_NULL)
4881 else if (select && select->
quick)
4884 save_quick= select->
quick;
4891 if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
4892 quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
4893 quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT)
4906 if (! usable_keys.test(ref_key))
4911 uint32_t new_ref_key;
4916 if (table->covering_keys.test(ref_key))
4917 usable_keys&= table->covering_keys;
4922 if ((new_ref_key=
test_if_subkey(order, table, ref_key, ref_key_parts,
4923 &usable_keys)) < MAX_KEY)
4926 if (tab->ref.
key >= 0)
4937 while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
4940 if (create_ref_for_key(tab->join, tab, keyuse, tab->join->const_table_map))
4955 key_map new_ref_key_map;
4956 new_ref_key_map.reset();
4957 new_ref_key_map.set(new_ref_key);
4959 if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
4960 (tab->join->select_options &
4961 OPTION_FOUND_ROWS) ?
4963 tab->join->
unit->select_limit_cnt,0,
4970 ref_key= new_ref_key;
4974 if (usable_keys.test(ref_key) &&
4977 goto check_reverse_order;
4988 uint32_t best_key_parts= 0;
4989 int best_key_direction= 0;
4990 ha_rows best_records= 0;
4993 bool is_best_covering=
false;
4995 Join *join= tab->join;
4996 uint32_t tablenr= tab - join->join_tab;
4997 ha_rows table_records= table->
cursor->stats.records;
4998 bool group= join->group && order == join->
group_list;
5006 if (select_limit >= table_records)
5012 if (tab->
type == AM_ALL && tab->join->
tables > tab->join->const_tables + 1)
5016 keys= *table->
cursor->keys_to_use_for_scanning();
5017 keys|= table->covering_keys;
5024 if (table->force_index)
5026 keys|= (group ? table->keys_in_use_for_group_by :
5027 table->keys_in_use_for_order_by);
5037 read_time= cur_pos.getCost();
5039 for (uint32_t i= tablenr+1; i < join->
tables; i++)
5042 fanout*= cur_pos.getFanout();
5045 for (nr=0; nr < table->getShare()->sizeKeys() ; nr++)
5048 if (keys.test(nr) &&
5051 bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->getPrimaryKey() && table->
cursor->primary_key_is_clustered());
5063 select_limit != HA_POS_ERROR ||
5064 (ref_key < 0 && (group || table->force_index)))
5067 double index_scan_time;
5069 if (select_limit == HA_POS_ERROR)
5071 select_limit= table_records;
5076 rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
5077 set_if_bigger(rec_per_key, 1.0);
5083 if (select_limit > table_records/rec_per_key)
5085 select_limit= table_records;
5089 select_limit= (ha_rows) (select_limit*rec_per_key);
5102 select_limit= (ha_rows) (select_limit < fanout ?
5103 1 : select_limit/fanout);
5116 select_limit= table_records;
5120 select_limit= (ha_rows) (select_limit *
5121 (
double) table_records /
5124 rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
5125 set_if_bigger(rec_per_key, 1.0);
5137 index_scan_time= select_limit/rec_per_key * min(rec_per_key, table->
cursor->scan_time());
5139 if (is_covering || (ref_key < 0 && (group || table->force_index)) || index_scan_time < read_time)
5141 ha_rows quick_records= table_records;
5142 if (is_best_covering && !is_covering)
5147 if (table->quick_keys.test(nr))
5149 quick_records= table->quick_rows[nr];
5153 (select_limit <= min(quick_records,best_records) ?
5154 keyinfo->key_parts < best_key_parts :
5155 quick_records < best_records))
5158 best_key_parts= keyinfo->key_parts;
5159 best_records= quick_records;
5160 is_best_covering= is_covering;
5161 best_key_direction= direction;
5170 bool quick_created=
false;
5171 if (table->quick_keys.test(best_key) && best_key != ref_key)
5175 test_map.set(best_key);
5177 select->test_quick_select(join->session, test_map, 0,
5178 join->select_options & OPTION_FOUND_ROWS ?
5180 join->
unit->select_limit_cnt,
5183 if (no_changes ==
false)
5187 tab->index= best_key;
5188 tab->read_first_record= best_key_direction > 0 ?
5189 join_read_first : join_read_last;
5191 if (select && select->
quick)
5193 safe_delete(select->
quick);
5195 if (table->covering_keys.test(best_key))
5198 table->
cursor->extra(HA_EXTRA_KEYREAD);
5200 table->
cursor->ha_index_or_rnd_end();
5201 if (join->select_options & SELECT_DESCRIBE)
5205 if (select_limit < table_records)
5206 tab->
limit= select_limit;
5209 else if (tab->
type != AM_ALL)
5216 assert(tab->select->
quick);
5221 tab->read_first_record= join_init_read_record;
5224 used_key_parts= best_key_parts;
5225 order_direction= best_key_direction;
5233 check_reverse_order:
5234 if (order_direction == -1)
5236 if (select && select->
quick)
5242 if (! select->
quick->reverse_sorted())
5247 if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
5248 quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT ||
5249 quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
5250 quick_type == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX)
5253 select->
quick= save_quick;
5264 select->
quick= save_quick;
5271 else if (tab->
type != AM_NEXT &&
5272 tab->ref.
key >= 0 && tab->ref.
key_parts <= used_key_parts)
5281 tab->read_record.read_record= join_read_prev_same;
5284 else if (select && select->
quick)
5286 select->
quick->sorted= 1;
5320 int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit,
bool is_order_by)
5323 ha_rows examined_rows;
5325 optimizer::SqlSelect *select= NULL;
5328 if (join->tables == join->const_tables)
5330 tab= join->join_tab + join->const_tables;
5332 select= tab->select;
5340 if ((order != join->group_list ||
5341 !(join->select_options & SELECT_BIG_RESULT) ||
5342 (select && select->quick && (select->quick->get_type() == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX))) &&
5344 is_order_by ? &table->keys_in_use_for_order_by :
5345 &table->keys_in_use_for_group_by))
5350 for (Order *ord= join->order; ord; ord= ord->next)
5355 join->sortorder= make_unireg_sortorder(order, &length, join->sortorder);
5356 table->sort.io_cache=
new internal::io_cache_st;
5360 if (select && !select->quick && tab->ref.key >= 0)
5364 select->quick=tab->quick;
5370 if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
5373 table->cursor->extra(HA_EXTRA_NO_KEYREAD);
5384 if (! (select->quick= (optimizer::get_quick_select_for_ref(session,
5387 tab->found_records))))
5394 if (table->getShare()->getType())
5395 table->cursor->info(HA_STATUS_VARIABLE);
5397 FileSort filesort(*session);
5398 table->sort.found_records=filesort.run(table,join->sortorder, length,
5399 select, filesort_limit, 0,
5401 tab->records= table->sort.found_records;
5409 tab->first_unmatched= 0;
5411 tab->read_first_record= join_init_read_record;
5412 tab->join->examined_rows+=examined_rows;
5413 if (table->key_read)
5416 table->cursor->extra(HA_EXTRA_NO_KEYREAD);
5419 return(table->sort.found_records == HA_POS_ERROR);
5422 int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having)
5424 Cursor *cursor=table->cursor;
5425 char *org_record,*new_record;
5426 unsigned char *record;
5428 uint32_t reclength= table->getShare()->getRecordLength() - offset;
5430 org_record=(
char*) (record=table->getInsertRecord())+offset;
5431 new_record=(
char*) table->getUpdateRecord()+offset;
5433 if ((error= cursor->startTableScan(1)))
5436 error=cursor->rnd_next(record);
5439 if (session->getKilled())
5441 session->send_kill_message();
5447 if (error == HA_ERR_RECORD_DELETED)
5451 if (error == HA_ERR_END_OF_FILE)
5457 if (having && !having->val_int())
5459 if ((error=cursor->deleteRecord(record)))
5463 error=cursor->rnd_next(record);
5466 copy_blobs(first_field);
5467 memcpy(new_record,org_record,reclength);
5473 if ((error=cursor->rnd_next(record)))
5475 if (error == HA_ERR_RECORD_DELETED)
5479 if (error == HA_ERR_END_OF_FILE)
5486 if (table->compare_record(first_field) == 0)
5488 if ((error=cursor->deleteRecord(record)))
5496 cursor->position(record);
5504 error= cursor->rnd_pos(record, cursor->ref);
5507 cursor->extra(HA_EXTRA_NO_CACHE);
5510 cursor->extra(HA_EXTRA_NO_CACHE);
5513 table->print_error(error,MYF(0));
5526 uint32_t field_count,
5527 Field **first_field,
5528 uint32_t key_length,
5531 unsigned char *key_pos, *record=table->getInsertRecord();
5534 uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5535 uint32_t *field_length;
5537 std::vector<unsigned char> key_buffer((key_length + extra_length) * (
long) cursor.stats.records);
5538 std::vector<uint32_t> field_lengths(field_count);
5542 uint32_t total_length= 0;
5544 for (ptr= first_field, field_length= &field_lengths[0] ; *ptr ; ptr++)
5546 uint32_t length= (*ptr)->sort_length();
5547 (*field_length++)= length;
5548 total_length+= length;
5550 assert(total_length <= key_length);
5551 key_length= total_length;
5552 extra_length= ALIGN_SIZE(key_length)-key_length;
5555 hash_init(&hash, &my_charset_bin, (uint32_t) cursor.stats.records, 0, key_length, (hash_get_key) 0, 0, 0);
5557 if ((error= cursor.startTableScan(1)))
5562 key_pos= &key_buffer[0];
5565 if (session->getKilled())
5567 session->send_kill_message();
5571 if ((error=cursor.rnd_next(record)))
5573 if (error == HA_ERR_RECORD_DELETED)
5578 if (error == HA_ERR_END_OF_FILE)
5585 if (having && !having->
val_int())
5587 if ((error=cursor.deleteRecord(record)))
5595 unsigned char* org_key_pos= key_pos;
5596 field_length= &field_lengths[0];
5597 for (
Field **ptr= first_field ; *ptr ; ptr++)
5599 (*ptr)->sort_string(key_pos,*field_length);
5600 key_pos+= *field_length++;
5603 if (hash_search(&hash, org_key_pos, key_length))
5606 if ((error=cursor.deleteRecord(record)))
5611 (void) my_hash_insert(&hash, org_key_pos);
5613 key_pos+=extra_length;
5616 cursor.extra(HA_EXTRA_NO_CACHE);
5617 (void) cursor.endTableScan();
5622 cursor.extra(HA_EXTRA_NO_CACHE);
5623 (void) cursor.endTableScan();
5626 table->print_error(error,MYF(0));
5632 SortField* make_unireg_sortorder(Order* order, uint32_t* length, SortField* sortorder)
5634 SortField *sort,*pos;
5637 for (Order *tmp = order; tmp; tmp=tmp->next)
5640 sortorder= (SortField*) memory::sql_alloc(
sizeof(SortField) * (max(count, *length) + 1));
5641 pos= sort= sortorder;
5643 for (; order; order= order->next,pos++)
5645 Item *item= order->
item[0]->real_item();
5646 pos->field= 0; pos->item= 0;
5648 if (item->type() == Item::FIELD_ITEM)
5650 pos->field= ((Item_field*) item)->field;
5652 else if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item())
5654 pos->field= ((Item_sum*) item)->get_tmp_table_field();
5656 else if (item->type() == Item::COPY_STR_ITEM)
5658 pos->item= ((Item_copy_string*) item)->item;
5662 pos->item= *order->item;
5665 pos->reverse=! order->asc;
5689 static bool cmp_buffer_with_ref(JoinTable *tab)
5692 if (!tab->ref.disable_cache)
5694 if (!(no_prev_key= tab->ref.key_err))
5697 memcpy(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length);
5705 if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) || no_prev_key)
5709 return (memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length) != 0);
5712 bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
5714 enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
5715 session->count_cuted_fields= CHECK_FIELD_IGNORE;
5718 for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
5720 if ((*copy)->copy() & 1)
5726 session->count_cuted_fields= save_count_cuted_fields;
5767 Item **ref_pointer_array,
5772 bool is_group_field)
5774 Item *order_item= *order->item;
5775 Item::Type order_item_type;
5785 if (order_item->type() == Item::INT_ITEM && order_item->
basic_const_item())
5787 uint32_t count= (uint32_t) order_item->
val_int();
5788 if (!count || count > fields.size())
5790 my_error(ER_BAD_FIELD_ERROR, MYF(0), order_item->full_name(), session->where());
5793 order->item= ref_pointer_array + count - 1;
5794 order->in_field_list= 1;
5795 order->counter= count;
5796 order->counter_used= 1;
5800 select_item= find_item_in_list(session, order_item, fields, &counter, REPORT_EXCEPT_NOT_FOUND, &resolution);
5808 if (select_item != not_found_item)
5810 Item *view_ref= NULL;
5816 if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->
fixed && order_item->fix_fields(session, order->item))
5822 order_item_type= order_item->type();
5823 from_field= (
Field*) not_found_field;
5824 if ((is_group_field && order_item_type == Item::FIELD_ITEM) ||
5825 order_item_type == Item::REF_ITEM)
5827 from_field= find_field_in_tables(session, (
Item_ident*) order_item, tables,
5828 NULL, &view_ref, IGNORE_ERRORS,
false);
5831 from_field= (
Field*) not_found_field;
5835 if (from_field == not_found_field ||
5836 (from_field != view_ref_found ?
5838 ((*select_item)->type() == Item::FIELD_ITEM &&
5839 ((
Item_field*) (*select_item))->field->eq(from_field)) :
5844 ((*select_item)->type() == Item::REF_ITEM &&
5845 view_ref->type() == Item::REF_ITEM &&
5846 ((
Item_ref *) (*select_item))->ref ==
5856 order->item= ref_pointer_array + counter;
5857 order->in_field_list=1;
5868 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5869 ER(ER_NON_UNIQ_ERROR),
5875 order->in_field_list=0;
5887 if (!order_item->
fixed &&
5888 (order_item->fix_fields(session, order->item) ||
5889 (order_item= *order->item)->check_cols(1) ||
5895 uint32_t el= all_fields.size();
5896 all_fields.push_front(order_item);
5897 ref_pointer_array[el]= order_item;
5898 order->item= ref_pointer_array + el;
5909 Item **ref_pointer_array,
5915 session->setWhere(
"order clause");
5916 for (; order; order= order->next)
5918 if (
find_order_in_list(session, ref_pointer_array, tables, order, fields, all_fields,
false))
5952 Item **ref_pointer_array,
5957 bool *hidden_group_fields)
5959 *hidden_group_fields=0;
5966 uint32_t org_fields=all_fields.size();
5968 session->setWhere(
"group statement");
5969 for (
Order *ord= order; ord; ord= ord->next)
5976 (*ord->item)->marker= UNDEF_POS;
5977 if ((*ord->item)->with_sum_func)
5979 my_error(ER_WRONG_GROUP_FIELD, MYF(0), (*ord->item)->full_name());
6003 int cur_pos_in_select_list= 0;
6008 while (field && (item=li++))
6010 if (item->type() != Item::SUM_FUNC_ITEM && item->marker >= 0 &&
6012 !(item->real_item()->type() == Item::FIELD_ITEM &&
6018 if (field->marker < cur_pos_in_select_list)
6024 if (field->marker > cur_pos_in_select_list)
6033 for (
Order *ord= order; ord; ord= ord->next)
6035 if ((*ord->item)->eq((
Item*)field, 0))
6044 my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name());
6050 cur_pos_in_select_list++;
6054 if (org_fields != all_fields.size())
6056 *hidden_group_fields=1;
6069 Item **ref_pointer_array,
6073 bool *all_order_by_fields_used)
6076 Order *order,*group,**prev;
6078 *all_order_by_fields_used= 1;
6079 while (
Item* item=li++)
6084 prev= &group; group=0;
6085 for (order=order_list ; order; order=order->next)
6087 if (order->in_field_list)
6092 (*ord->item)->marker=1;
6096 *all_order_by_fields_used= 0;
6101 while (
Item* item=li++)
6103 if (!item->const_item() && !item->with_sum_func && !item->marker)
6110 for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
6112 if ((*ord_iter->item)->eq(item, 1))
6125 ord->item= ref_pointer_array;
6131 ref_pointer_array++;
6145 param->field_count=param->sum_func_count=param->func_count= param->hidden_field_count=0;
6146 param->quick_group=1;
6148 while ((field=li++))
6150 Item::Type real_type= field->real_item()->type();
6151 if (real_type == Item::FIELD_ITEM)
6153 param->field_count++;
6155 else if (real_type == Item::SUM_FUNC_ITEM)
6157 if (! field->const_item())
6160 if (!sum_item->depended_from() ||
6161 sum_item->depended_from() == select_lex)
6163 if (!sum_item->quick_group)
6165 param->quick_group=0;
6167 param->sum_func_count++;
6169 for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
6171 if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM)
6172 param->field_count++;
6174 param->func_count++;
6177 param->func_count++;
6182 param->func_count++;
6183 if (reset_with_sum_func)
6185 field->with_sum_func= 0;
6203 int test_if_item_cache_changed(List<Cached_item> &list)
6205 List<Cached_item>::iterator li(list.begin());
6209 for (
int i=(
int) list.size()-1 ; (buff=li++) ; i--)
6247 Item **ref_pointer_array,
6256 res_selected_fields.clear();
6257 res_all_fields.clear();
6260 uint32_t border= all_fields.size() - elements;
6262 if (param->field_count &&
6263 !(copy= param->copy_field=
new CopyField[param->field_count]))
6268 param->copy_funcs.clear();
6269 for (uint32_t i= 0; (pos= li++); i++)
6273 Item *real_pos= pos->real_item();
6274 if (real_pos->type() == Item::FIELD_ITEM)
6277 if (pos->type() == Item::REF_ITEM)
6281 item->db_name= ref->db_name;
6282 item->table_name= ref->table_name;
6287 if (item->field->flags & BLOB_FLAG)
6299 param->copy_funcs.push_front(pos);
6308 item->result_field=field->new_field(session->
mem_root,field->getTable(), 1);
6314 if (!(tmp= (
unsigned char*) memory::sql_alloc(field->
pack_length()+2)))
6321 copy->set(tmp, item->result_field);
6322 item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
6323 #ifdef HAVE_VALGRIND
6324 copy->to_ptr[copy->from_length]= 0;
6330 else if ((real_pos->type() == Item::FUNC_ITEM ||
6331 real_pos->type() == Item::SUBSELECT_ITEM ||
6332 real_pos->type() == Item::CACHE_ITEM ||
6333 real_pos->type() == Item::COND_ITEM) &&
6334 !real_pos->with_sum_func)
6346 extra_funcs.push_back(pos);
6350 param->copy_funcs.push_back(pos);
6353 res_all_fields.push_back(pos);
6354 ref_pointer_array[((i < border)? all_fields.size()-i-1 : i-border)]=
6357 param->copy_field_end= copy;
6359 for (uint32_t i= 0; i < border; i++)
6363 itr.sublist(res_selected_fields, elements);
6368 param->copy_funcs.concat(&extra_funcs);
6375 delete[] param->copy_field;
6377 param->copy_field=0;
6389 for (
CopyField *ptr= param->copy_field; ptr != param->copy_field_end; ptr++)
6391 (*ptr->do_copy)(ptr);
6419 Item **ref_pointer_array,
6426 Item *item_field,*item;
6428 res_selected_fields.clear();
6429 res_all_fields.clear();
6431 uint32_t i, border= all_fields.size() - elements;
6432 for (i= 0; (item= it++); i++)
6436 if ((item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM) or
6437 (item->type() == Item::FUNC_ITEM and ((
Item_func*)item)->functype() == Item_func::SUSERVAR_FUNC))
6443 if (item->type() == Item::FIELD_ITEM)
6445 item_field= item->get_tmp_table_item(session);
6447 else if ((field= item->get_tmp_table_field()))
6449 if (item->type() == Item::SUM_FUNC_ITEM && field->getTable()->group)
6451 item_field= ((
Item_sum*) item)->result_item(field);
6458 if (item_field == NULL)
6463 if (item->real_item()->type() != Item::FIELD_ITEM)
6468 item_field->name= item->name;
6470 if (item->type() == Item::REF_ITEM)
6474 ifield->table_name= iref->table_name;
6475 ifield->db_name= iref->db_name;
6483 res_all_fields.push_back(item_field);
6484 ref_pointer_array[((i < border)? all_fields.size()-i-1 : i-border)]= item_field;
6488 for (i= 0; i < border; i++)
6490 itr.sublist(res_selected_fields, elements);
6511 Item **ref_pointer_array,
6518 Item *item, *new_item;
6519 res_selected_fields.clear();
6520 res_all_fields.clear();
6522 uint32_t i, border= all_fields.size() - elements;
6523 for (i= 0; (item= it++); i++)
6525 res_all_fields.push_back(new_item= item->get_tmp_table_item(session));
6526 ref_pointer_array[((i < border)? all_fields.size()-i-1 : i-border)]=
6531 for (i= 0; i < border; i++)
6535 itr.sublist(res_selected_fields, elements);
6558 while ((func= *(func_ptr++)))
6560 if (func->setup(session))
6568 void init_tmptable_sum_functions(Item_sum **func_ptr)
6571 while ((func= *(func_ptr++)))
6573 func->reset_field();
6581 while ((func= *(func_ptr++)))
6583 func->update_field();
6590 for (; func_ptr != end_ptr ; func_ptr++)
6592 (void) (*func_ptr)->save_in_result_field(1);
6596 bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
6598 for (; func_ptr != end_ptr ;func_ptr++)
6600 if ((*func_ptr)->reset())
6605 for ( ; *func_ptr ; func_ptr++)
6607 if ((*func_ptr)->add())
6615 bool update_sum_func(Item_sum **func_ptr)
6618 for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
6632 for (; (func = *func_ptr) ; func_ptr++)
6634 func->save_in_result_field(1);
6657 for (Select_Lex_Unit *unit= select->first_inner_unit();
6659 unit= unit->next_unit())
6708 if (expr->arg_count)
6711 Item **arg,**arg_end;
6712 bool arg_changed=
false;
6713 for (arg= expr->arguments(),
6714 arg_end= expr->arguments()+expr->arg_count;
6715 arg != arg_end; arg++)
6718 if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6721 for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6723 if (item->
eq(*group_tmp->item,0))
6731 else if (item->type() == Item::FUNC_ITEM)
6750 static void print_table_array(Session *session, String *str, TableList **table,
6753 (*table)->print(session, str);
6755 for (TableList **tbl= table + 1; tbl < end; tbl++)
6757 TableList *curr= *tbl;
6758 if (curr->outer_join)
6761 str->append(STRING_WITH_LEN(
" left join "));
6763 else if (curr->straight)
6765 str->append(STRING_WITH_LEN(
" straight_join "));
6769 str->append(STRING_WITH_LEN(
" join "));
6771 curr->print(session, str);
6774 str->append(STRING_WITH_LEN(
" on("));
6775 curr->on_expr->print(str);
6794 for (
TableList **t= table + (tables->size() - 1); t >= table; t--)
6798 assert(tables->size() >= 1);
6799 print_table_array(session, str, table, table + tables->size());
6802 void Select_Lex::print(Session *session, String *str)
6807 session= current_session;
6811 str->append(STRING_WITH_LEN(
"select "));
6814 if (options & SELECT_STRAIGHT_JOIN)
6815 str->append(STRING_WITH_LEN(
"straight_join "));
6817 if (options & SELECT_DISTINCT)
6818 str->append(STRING_WITH_LEN(
"distinct "));
6820 if (options & SELECT_SMALL_RESULT)
6821 str->append(STRING_WITH_LEN(
"sql_small_result "));
6823 if (options & SELECT_BIG_RESULT)
6824 str->append(STRING_WITH_LEN(
"sql_big_result "));
6826 if (options & OPTION_BUFFER_RESULT)
6827 str->append(STRING_WITH_LEN(
"sql_buffer_result "));
6829 if (options & OPTION_FOUND_ROWS)
6830 str->append(STRING_WITH_LEN(
"sql_calc_found_rows "));
6834 List<Item>::iterator it(item_list.begin());
6836 while ((item= it++))
6846 item->print_item_w_name(str);
6853 if (table_list.size())
6855 str->append(STRING_WITH_LEN(
" from "));
6865 str->append(STRING_WITH_LEN(
" from DUAL "));
6869 Item *cur_where= where;
6871 cur_where= join->conds;
6872 if (cur_where || cond_value != Item::COND_UNDEF)
6874 str->append(STRING_WITH_LEN(
" where "));
6877 cur_where->print(str);
6881 if (cond_value != Item::COND_FALSE)
6883 str->append(STRING_WITH_LEN(
"1"));
6887 str->append(STRING_WITH_LEN(
"0"));
6893 if (group_list.size())
6895 str->append(STRING_WITH_LEN(
" group by "));
6896 print_order(str, (Order *) group_list.first);
6900 str->append(STRING_WITH_LEN(
" with cube"));
6903 str->append(STRING_WITH_LEN(
" with rollup"));
6911 Item *cur_having= having;
6913 cur_having= join->having;
6915 if (cur_having || having_value != Item::COND_UNDEF)
6917 str->append(STRING_WITH_LEN(
" having "));
6920 cur_having->print(str);
6924 if (having_value != Item::COND_FALSE)
6926 str->append(STRING_WITH_LEN(
"1"));
6930 str->append(STRING_WITH_LEN(
"0"));
6935 if (order_list.size())
6937 str->append(STRING_WITH_LEN(
" order by "));
6938 print_order(str, (Order *) order_list.first);
6942 print_limit(session, str);
JoinTable * first_unmatched
int join_tab_cmp_straight(const void *ptr1, const void *ptr2)
virtual uint32_t pack_length() const
virtual bool const_item() const
virtual int index_read_idx_map(unsigned char *buf, uint32_t index, const unsigned char *key, key_part_map keypart_map, enum ha_rkey_function find_flag)
Positions an index cursor to the index specified in the handle. Fetches the row if available...
bool check_interleaving_with_nj(JoinTable *next_tab)
static COND * build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
enum_nested_loop_state evaluate_join_record(Join *join, JoinTable *join_tab, int error)
Process one record of the nested loop join.
table_map map
ID bit of table (1,2,4,8,16...)
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
void * memdup(const void *, size_t)
Duplicate the provided block into memory allocated from within the specified Root.
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
bool substitute_null_with_insert_id
virtual bool basic_const_item() const
void set_proc_info(const char *info)
bool is_subkey(KeyPartInfo *key_part, KeyPartInfo *ref_key_part, KeyPartInfo *ref_key_part_end)
Item_equal * find_item_equal(COND_EQUAL *cond_equal)
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
void merge(Item_equal *item)
void calc_used_field_length(Session *, JoinTable *join_tab)
virtual int64_t val_int()=0
void copy_fields(Tmp_Table_Param *param)
int join_read_always_key_or_null(JoinTable *tab)
bool change_refs_to_tmp_fields(Session *session, Item **ref_pointer_array, List< Item > &res_selected_fields, List< Item > &res_all_fields, uint32_t elements, List< Item > &all_fields)
uint32_t max_part_bit(key_part_map bits)
bool contains(Field *field)
const char * in_additional_cond
COND_EQUAL * cond_equal
Used with outer join.
TODO: Rename this file - func.h is stupid.
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
bool list_contains_unique_index(Table *table, bool(*find_func)(Field *, void *), void *data)
enum_nested_loop_state end_update(Join *join, JoinTable *, bool end_of_records)
table_map used_tables() const
COND * remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List< Item > &fields, bool reset_with_sum_func)
int do_select(Join *join, List< Item > *fields, Table *table)
virtual bool eq_def(Field *field)
QuickSelectInterface * quick
bool change_result(select_result *result)
int prepare(Item ***rref_pointer_array, TableList *tables, uint32_t wind_num, COND *conds, uint32_t og_num, Order *order, Order *group, Item *having, Select_Lex *select, Select_Lex_Unit *unit)
int join_read_const(JoinTable *tab)
int report_error(int error)
static uint32_t test_if_subkey(Order *order, Table *table, uint32_t ref, uint32_t ref_key_parts, const key_map *usable_keys)
Order * create_distinct_group(Session *session, Item **ref_pointer_array, Order *order_list, List< Item > &fields, List< Item > &, bool *all_order_by_fields_used)
virtual bool eq(const Item *, bool binary_cmp) const
#define STACK_MIN_SIZE
Abort if less stack during eval.
bool check_stack_overrun(Session *session, long margin, void *)
COND * substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
uint64_t first_successful_insert_id_in_prev_stmt
static int compare_fields_by_table_order(Item_field *field1, Item_field *field2, void *table_join_idx)
int join_read_last_key(JoinTable *tab)
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
static bool check_row_equality(Session *session, Item *left_row, Item_row *right_row, COND_EQUAL *cond_equal, List< Item > *eq_list)
static Item_equal * find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
bool select_query(Session *session, Item ***rref_pointer_array, TableList *tables, uint32_t wild_num, List< Item > &fields, COND *conds, uint32_t og_num, Order *order, Order *group, Item *having, uint64_t select_options, select_result *result, Select_Lex_Unit *unit, Select_Lex *select_lex)
uint64_t limit_found_rows
int remove_dup_with_hash_index(Session *session, Table *table, uint32_t field_count, Field **first_field, uint32_t key_length, Item *having)
int key_cmp(KeyPartInfo *key_part, const unsigned char *key, uint32_t key_length)
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
enum_nested_loop_state evaluate_null_complemented_join_record(Join *join, JoinTable *join_tab)
int join_tab_cmp(const void *ptr1, const void *ptr2)
void sort(Item_field_cmpfunc cmp, void *arg)
void update_ref_and_keys(Session *session, DYNAMIC_ARRAY *keyuse, JoinTable *join_tab, uint32_t tables, COND *cond, COND_EQUAL *, table_map normal_tables, Select_Lex *select_lex, vector< optimizer::SargableParam > &sargables)
bool handle_select(Session *session, LEX *lex, select_result *result, uint64_t setup_tables_done_option)
List< TableList > * join_list
bool find_field_in_item_list(Field *field, void *data)
int setup_group(Session *session, Item **ref_pointer_array, TableList *tables, List< Item > &fields, List< Item > &all_fields, Order *order, bool *hidden_group_fields)
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
bool change_to_use_tmp_fields(Session *session, Item **ref_pointer_array, List< Item > &res_selected_fields, List< Item > &res_all_fields, uint32_t elements, List< Item > &all_fields)
int(* qsort_cmp)(const void *, const void *)
bool find_field_in_order_list(Field *field, void *data)
enum_nested_loop_state end_unique_update(Join *join, JoinTable *, bool end_of_records)
void print_join(Session *session, String *str, List< TableList > *tables)
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
void update_const_equal_items(COND *cond, JoinTable *tab)
static bool find_order_in_list(Session *session, Item **ref_pointer_array, TableList *tables, Order *order, List< Item > &fields, List< Item > &all_fields, bool is_group_field)
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables, List< Item > &fields, List< Item > &all_fields, Order *order)
std::bitset< 64 > cur_embedding_map
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
uint32_t used_fieldlength
bool setup_copy_fields(Session *session, Tmp_Table_Param *param, Item **ref_pointer_array, List< Item > &res_selected_fields, List< Item > &res_all_fields, uint32_t elements, List< Item > &all_fields)
void free_underlaid_joins(Session *session, Select_Lex *select)
Next_select_func setup_end_select_func(Join *join)
std::bitset< 64 > embedding_map
Item * pre_idx_push_select_cond
virtual int get_type() const =0
unsigned char * null_ref_key
List< Item > & fields_list
ha_rows quick_condition_rows
virtual table_map used_tables() const
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
bool copy_funcs(Item **func_ptr, const Session *session)
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
static COND * build_equal_items(Session *session, COND *cond, COND_EQUAL *inherited, List< TableList > *join_list, COND_EQUAL **cond_equal_ref)
virtual int index_read_last_map(unsigned char *buf, const unsigned char *key, key_part_map keypart_map)
The following functions works like index_read, but it find the last row with the current key value or...
Item * remove_additional_cond(Item *conds)
optimizer::Position & getPosFromOptimalPlan(uint32_t index)
optimizer::KeyUse * keyuse
static bool check_simple_equality(Item *left_item, Item *right_item, Item *item, COND_EQUAL *cond_equal)
bool key_cmp_if_same(Table *table, const unsigned char *key, uint32_t idx, uint32_t key_length)
Item * on_expr
Used with outer join.
static Item * eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List< Item > *eq_list)
virtual Item * compile(Item_analyzer analyzer, unsigned char **arg_p, Item_transformer transformer, unsigned char *arg_t)