719 Order *group,
bool distinct,
bool save_sum_fields,
720 uint64_t select_options, ha_rows rows_limit,
721 const char *table_alias)
723 uint i,field_count,null_count,null_pack_length;
724 uint32_t copy_func_count= param->func_count;
725 uint32_t hidden_null_count, hidden_null_pack_length, hidden_field_count;
726 uint32_t blob_count,group_null_items, string_count;
728 ulong reclength, string_total_length;
729 bool using_unique_constraint=
false;
730 bool not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
731 unsigned char *pos, *group_buff;
732 unsigned char *null_flags;
733 Field **reg_field, **from_field, **default_field;
738 MI_COLUMNDEF *recinfo;
739 uint32_t total_uneven_bit_length= 0;
740 bool force_copy_fields= param->force_copy_fields;
741 uint64_t max_rows= 0;
743 session->status_var.created_tmp_tables++;
747 if (! param->quick_group)
751 else for (
Order *tmp=group ; tmp ; tmp=tmp->next)
759 (*tmp->item)->marker= 4;
760 if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
761 using_unique_constraint=
true;
763 if (param->group_length >= MAX_BLOB_WIDTH)
764 using_unique_constraint=
true;
769 field_count=param->field_count+param->func_count+param->sum_func_count;
770 hidden_field_count=param->hidden_field_count;
779 if (param->precomputed_group_by)
781 copy_func_count+= param->sum_func_count;
787 &default_field,
sizeof(
Field*) * (field_count),
788 &from_field,
sizeof(
Field*)*field_count,
789 ©_func,
sizeof(*copy_func)*(copy_func_count+1),
790 ¶m->keyinfo,
sizeof(*param->keyinfo),
791 &key_part_info,
sizeof(*key_part_info)*(param->group_parts+1),
792 ¶m->start_recinfo,
sizeof(*param->recinfo)*(field_count*2+4),
793 &group_buff, (group && ! using_unique_constraint ? param->group_length : 0),
796 param->copy_field= copy=
new (session->mem_root)
CopyField[field_count];
797 param->items_to_copy= copy_func;
800 memset(default_field, 0,
sizeof(
Field*) * (field_count));
801 memset(from_field, 0,
sizeof(
Field*)*field_count);
804 session->mem_root= &table->mem();
806 table->getMutableShare()->setFields(field_count+1);
807 table->setFields(table->getMutableShare()->getFields(
true));
808 reg_field= table->getMutableShare()->getFields(
true);
809 table->setAlias(table_alias);
810 table->reginfo.lock_type=TL_WRITE;
811 table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
813 table->copy_blobs= 1;
815 table->in_use= session;
816 table->quick_keys.reset();
817 table->covering_keys.reset();
818 table->keys_in_use_for_query.reset();
820 table->getMutableShare()->blob_field.resize(field_count+1);
821 uint32_t *blob_field= &table->getMutableShare()->blob_field[0];
822 table->getMutableShare()->db_low_byte_first=1;
823 table->getMutableShare()->table_charset= param->table_charset;
824 table->getMutableShare()->keys_for_keyread.reset();
825 table->getMutableShare()->keys_in_use.reset();
829 reclength= string_total_length= 0;
830 blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
831 param->using_indirect_summary_function= 0;
833 List<Item>::iterator li(fields.begin());
834 Field **tmp_from_field=from_field;
835 while (
Item* item=li++)
837 Item::Type
type=item->type();
840 if (item->with_sum_func &&
type != Item::SUM_FUNC_ITEM)
842 if (item->used_tables() & OUTER_REF_TABLE_BIT)
843 item->update_used_tables();
844 if (
type == Item::SUBSELECT_ITEM ||
845 (item->used_tables() & ~OUTER_REF_TABLE_BIT))
852 param->using_indirect_summary_function=1;
856 if (item->const_item() && (
int) hidden_field_count <= 0)
859 if (
type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
861 ((
Item_sum*) item)->result_field= 0;
862 for (i= 0 ; i < ((
Item_sum*) item)->arg_count ; i++)
870 tmp_from_field, &default_field[fieldnr],
871 group != 0,not_all_columns,
873 param->convert_blob_length);
878 if (new_field->flags & BLOB_FLAG)
880 *blob_field++= fieldnr;
883 *(reg_field++)= new_field;
884 if (new_field->real_type() == DRIZZLE_TYPE_VARCHAR)
889 session->mem_root= mem_root_save;
891 session->mem_root= &table->mem();
892 if (!(new_field->flags & NOT_NULL_FLAG))
899 (*argp)->maybe_null=1;
901 new_field->setPosition(fieldnr++);
919 tmp_from_field, &default_field[fieldnr],
921 !force_copy_fields &&
922 (not_all_columns || group != 0),
924 param->convert_blob_length);
928 if (session->is_fatal_error)
932 if (
type == Item::SUM_FUNC_ITEM)
933 ((
Item_sum *) item)->result_field= new_field;
936 if (!(new_field->flags & NOT_NULL_FLAG))
938 if (new_field->flags & BLOB_FLAG)
940 *blob_field++= fieldnr;
943 if (item->marker == 4 && item->maybe_null)
946 new_field->flags|= GROUP_FLAG;
948 new_field->setPosition(fieldnr++);
949 *(reg_field++)= new_field;
951 if (!--hidden_field_count)
957 hidden_null_count=null_count;
962 param->hidden_field_count= fieldnr;
966 assert(fieldnr == (uint32_t) (reg_field - table->getFields()));
967 assert(field_count >= (uint32_t) (reg_field - table->getFields()));
968 field_count= fieldnr;
971 table->getMutableShare()->setFieldSize(field_count);
975 if (blob_count || using_unique_constraint ||
976 (session->lex().select_lex.options & SELECT_BIG_RESULT) ||
977 (session->lex().current_select->olap == ROLLUP_TYPE) ||
978 (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) == OPTION_BIG_TABLES)
980 table->getMutableShare()->storage_engine= myisam_engine;
981 table->cursor= table->getMutableShare()->db_type()->getCursor(*table);
983 (param->group_parts > table->cursor->getEngine()->max_key_parts() ||
984 param->group_length > table->cursor->getEngine()->max_key_length()))
986 using_unique_constraint=
true;
991 table->getMutableShare()->storage_engine= heap_engine;
992 table->cursor= table->getMutableShare()->db_type()->getCursor(*table);
998 if (! using_unique_constraint)
999 reclength+= group_null_items;
1001 table->getMutableShare()->blob_fields= blob_count;
1002 if (blob_count == 0)
1005 if (param->hidden_field_count)
1006 hidden_null_count++;
1010 hidden_null_pack_length=(hidden_null_count+7)/8;
1011 null_pack_length= (hidden_null_pack_length +
1012 (null_count + total_uneven_bit_length + 7) / 8);
1013 reclength+=null_pack_length;
1017 table->getMutableShare()->setRecordLength(reclength);
1019 uint32_t alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
1020 table->getMutableShare()->rec_buff_length= alloc_length;
1021 table->record[0]= table->alloc(alloc_length*2);
1022 table->record[1]= table->getInsertRecord()+alloc_length;
1023 table->getMutableShare()->resizeDefaultValues(alloc_length);
1026 param->func_count= copy_func - param->items_to_copy;
1028 table->setup_tmp_table_column_bitmaps();
1030 recinfo=param->start_recinfo;
1031 null_flags= table->getInsertRecord();
1032 pos=table->getInsertRecord()+ null_pack_length;
1033 if (null_pack_length)
1035 memset(recinfo, 0,
sizeof(*recinfo));
1036 recinfo->type=FIELD_NORMAL;
1037 recinfo->length=null_pack_length;
1039 memset(null_flags, 255, null_pack_length);
1041 table->null_flags= table->getInsertRecord();
1042 table->getMutableShare()->null_fields= null_count+ hidden_null_count;
1043 table->getMutableShare()->null_bytes= null_pack_length;
1045 null_count= (blob_count == 0) ? 1 : 0;
1046 hidden_field_count=param->hidden_field_count;
1047 for (i= 0,reg_field= table->getFields(); i < field_count; i++,reg_field++,recinfo++)
1049 Field *field= *reg_field;
1051 memset(recinfo, 0,
sizeof(*recinfo));
1053 if (!(field->flags & NOT_NULL_FLAG))
1055 if (field->flags & GROUP_FLAG && !using_unique_constraint)
1063 recinfo->type=FIELD_NORMAL;
1065 memset(recinfo, 0,
sizeof(*recinfo));
1069 recinfo->null_bit= 1 << (null_count & 7);
1070 recinfo->null_pos= null_count/8;
1072 field->move_field(pos,null_flags+null_count/8,
1073 1 << (null_count & 7));
1077 field->move_field(pos,(
unsigned char*) 0,0);
1084 if (default_field[i] && default_field[i]->ptr)
1092 Field *orig_field= default_field[i];
1094 diff= (ptrdiff_t) (orig_field->getTable()->getDefaultValues() - orig_field->getTable()->getInsertRecord());
1095 orig_field->move_field_offset(diff);
1096 if (orig_field->is_real_null())
1100 field->set_notnull();
1101 memcpy(field->ptr, orig_field->
ptr, field->pack_length());
1103 orig_field->move_field_offset(-diff);
1108 copy->set(field,from_field[i],save_sum_fields);
1111 length=field->pack_length();
1115 recinfo->length=length;
1116 if (field->flags & BLOB_FLAG)
1117 recinfo->type= (int) FIELD_BLOB;
1119 recinfo->type=FIELD_NORMAL;
1120 if (!--hidden_field_count)
1121 null_count=(null_count+7) & ~7;
1124 param->copy_field_end=copy;
1125 param->recinfo=recinfo;
1126 table->storeRecordAsDefault();
1128 if (session->variables.tmp_table_size == ~ (uint64_t) 0)
1130 max_rows= ~(uint64_t) 0;
1134 max_rows= (uint64_t) (((table->getMutableShare()->db_type() == heap_engine) ?
1135 min(session->variables.tmp_table_size,
1136 session->variables.max_heap_table_size) :
1137 session->variables.tmp_table_size) /
1138 table->getMutableShare()->getRecordLength());
1141 set_if_bigger(max_rows, (uint64_t)1);
1146 set_if_smaller(max_rows, rows_limit);
1148 table->getMutableShare()->setMaxRows(max_rows);
1150 param->end_write_records= rows_limit;
1152 keyinfo= param->keyinfo;
1157 param->group_buff=group_buff;
1158 table->getMutableShare()->keys=1;
1159 table->getMutableShare()->uniques= test(using_unique_constraint);
1160 table->key_info=keyinfo;
1161 keyinfo->key_part=key_part_info;
1162 keyinfo->flags=HA_NOSAME;
1163 keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
1164 keyinfo->key_length= 0;
1165 keyinfo->rec_per_key= 0;
1166 keyinfo->algorithm= message::Table::Index::UNKNOWN_INDEX;
1167 keyinfo->name= (
char*)
"group_key";
1168 Order *cur_group= group;
1169 for (; cur_group ; cur_group= cur_group->next, key_part_info++)
1171 Field *field=(*cur_group->item)->get_tmp_table_field();
1172 bool maybe_null=(*cur_group->item)->maybe_null;
1173 key_part_info->null_bit= 0;
1174 key_part_info->field= field;
1175 key_part_info->offset= field->offset(table->getInsertRecord());
1176 key_part_info->length= (uint16_t) field->key_length();
1177 key_part_info->type= (uint8_t) field->key_type();
1178 key_part_info->key_type=
1179 ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
1180 (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
1181 (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
1183 if (!using_unique_constraint)
1185 cur_group->buff=(
char*) group_buff;
1186 if (!(cur_group->field= field->new_key_field(session->mem_root,table,
1200 keyinfo->flags|= HA_NULL_ARE_EQUAL;
1201 key_part_info->null_bit=field->null_bit;
1202 key_part_info->null_offset= (uint32_t) (field->null_ptr -
1203 (
unsigned char*) table->getInsertRecord());
1208 key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
1211 keyinfo->key_length+= key_part_info->length;
1215 if (distinct && field_count != param->hidden_field_count)
1230 table->getMutableShare()->uniques= 1;
1232 null_pack_length-=hidden_null_pack_length;
1233 keyinfo->key_parts= ((field_count-param->hidden_field_count)+
1234 (table->getMutableShare()->uniques ? test(null_pack_length) : 0));
1236 table->getMutableShare()->keys= 1;
1237 key_part_info=
new (table->mem())
KeyPartInfo[keyinfo->key_parts];
1238 memset(key_part_info, 0, keyinfo->key_parts *
sizeof(
KeyPartInfo));
1239 table->key_info=keyinfo;
1240 keyinfo->key_part=key_part_info;
1241 keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
1242 keyinfo->key_length=(uint16_t) reclength;
1243 keyinfo->name= (
char*)
"distinct_key";
1244 keyinfo->algorithm= message::Table::Index::UNKNOWN_INDEX;
1245 keyinfo->rec_per_key= 0;
1252 if (null_pack_length && table->getMutableShare()->uniques)
1254 key_part_info->null_bit= 0;
1255 key_part_info->offset=hidden_null_pack_length;
1256 key_part_info->length=null_pack_length;
1257 table->setVariableWidth();
1259 (uint32_t) key_part_info->length,
1265 if (!key_part_info->field)
1267 key_part_info->field->init(table);
1268 key_part_info->key_type= 1;
1269 key_part_info->type= HA_KEYTYPE_BINARY;
1273 for (i=param->hidden_field_count, reg_field=table->getFields() + i ;
1275 i++, reg_field++, key_part_info++)
1277 key_part_info->null_bit= 0;
1278 key_part_info->field= *reg_field;
1279 key_part_info->offset= (*reg_field)->offset(table->getInsertRecord());
1280 key_part_info->length= (uint16_t) (*reg_field)->pack_length();
1288 key_part_info->store_length= key_part_info->length;
1290 if ((*reg_field)->real_maybe_null())
1291 key_part_info->store_length+= HA_KEY_NULL_LENGTH;
1292 if ((*reg_field)->type() == DRIZZLE_TYPE_BLOB ||
1293 (*reg_field)->real_type() == DRIZZLE_TYPE_VARCHAR)
1294 key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
1296 key_part_info->type= (uint8_t) (*reg_field)->key_type();
1297 key_part_info->key_type =
1298 ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
1299 (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
1300 (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
1305 if (session->is_fatal_error)
1307 table->getMutableShare()->db_record_offset= 1;
1308 if (table->getShare()->db_type() == myisam_engine)
1310 if (table->create_myisam_tmp_table(param->keyinfo, param->start_recinfo,
1311 ¶m->recinfo, select_options))
1314 assert(table->in_use);
1315 if (table->open_tmp_table())
1318 session->mem_root= mem_root_save;
1323 session->mem_root= mem_root_save;