72
72
/** Declarations of static functions used in this source file. */
73
73
static bool make_group_fields(Join *main_join, Join *curr_join);
74
static void calc_group_buffer(Join *join, Order *group);
75
static bool alloc_group_fields(Join *join, Order *group);
74
static void calc_group_buffer(Join *join,order_st *group);
75
static bool alloc_group_fields(Join *join,order_st *group);
76
76
static uint32_t cache_record_length(Join *join, uint32_t index);
77
77
static double prev_record_reads(Join *join, uint32_t idx, table_map found_ref);
78
78
static bool get_best_combination(Join *join);
102
102
static bool make_join_select(Join *join, optimizer::SqlSelect *select,COND *item);
103
103
static bool make_join_readinfo(Join *join);
104
104
static void update_depend_map(Join *join);
105
static void update_depend_map(Join *join, Order *order);
106
static Order *remove_constants(Join *join,Order *first_order,COND *cond, bool change_list, bool *simple_order);
105
static void update_depend_map(Join *join, order_st *order);
106
static order_st *remove_constants(Join *join,order_st *first_order,COND *cond, bool change_list, bool *simple_order);
107
107
static int return_zero_rows(Join *join,
108
108
select_result *res,
109
109
TableList *tables,
121
121
List<Item> &fields,
122
122
List<Item> &all_fields,
126
126
bool *hidden_group_fields);
127
127
static bool make_join_statistics(Join *join, TableList *leaves, COND *conds, DYNAMIC_ARRAY *keyuse);
128
128
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, uint32_t first_unused);
129
static Table *get_sort_by_table(Order *a, Order *b,TableList *tables);
129
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
130
130
static void reset_nj_counters(List<TableList> *join_list);
131
static bool test_if_subpart(Order *a,Order *b);
131
static bool test_if_subpart(order_st *a,order_st *b);
132
132
static void restore_prev_nj_state(JoinTable *last);
133
133
static bool add_ref_to_table_cond(Session *session, JoinTable *join_tab);
134
134
static void free_blobs(Field **ptr); /* Rename this method...conflicts with another in global namespace... */
192
192
for (table_ptr= select_lex->leaf_tables;
194
194
table_ptr= table_ptr->next_leaf)
200
197
if (setup_wild(session, fields_list, &all_fields, wild_num) ||
201
198
select_lex->setup_ref_array(session, og_num) ||
337
334
/* Caclulate the number of groups */
338
335
send_group_parts= 0;
339
for (Order *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
336
for (order_st *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
340
337
send_group_parts++;
347
344
* The below will create the new table for
350
347
* @see create_table_from_items() in drizzled/sql_insert.cc
352
349
if (result && result->prepare(fields_list, unit_arg))
355
352
/* Init join struct */
356
353
count_field_types(select_lex, &tmp_table_param, all_fields, 0);
596
595
if (const_tables && !(select_options & SELECT_NO_UNLOCK))
597
session->unlockSomeTables(table, const_tables);
596
mysql_unlock_some_tables(session, table, const_tables);
598
597
if (!conds && outer_join)
600
599
/* Handle the case where we have an OUTER JOIN without a WHERE */
657
656
/* Optimize distinct away if possible */
659
Order *org_order= order;
658
order_st *org_order= order;
660
659
order= remove_constants(this, order,conds,1, &simple_order);
661
660
if (session->is_error())
793
Order *old_group_list;
792
order_st *old_group_list;
794
793
group_list= remove_constants(this, (old_group_list= group_list), conds,
795
794
rollup.state == ROLLUP::STATE_NONE,
868
867
save_index_subquery_explain_info(join_tab, where);
869
868
join_tab[0].type= AM_UNIQUE_SUBQUERY;
871
return(unit->item->change_engine(new subselect_uniquesubquery_engine(session, join_tab, unit->item, where)));
872
subselect_uniquesubquery_engine(session,
873
877
else if (join_tab[0].type == AM_REF &&
874
878
join_tab[0].ref.items[0]->name == in_left_expr_name)
877
881
save_index_subquery_explain_info(join_tab, where);
878
882
join_tab[0].type= AM_INDEX_SUBQUERY;
880
return(unit->item->change_engine(new subselect_indexsubquery_engine(session, join_tab, unit->item, where, NULL, 0)));
886
subselect_indexsubquery_engine(session,
883
894
else if (join_tab[0].type == AM_REF_OR_NULL &&
889
900
conds= remove_additional_cond(conds);
890
901
save_index_subquery_explain_info(join_tab, conds);
891
return(unit->item->change_engine(new subselect_indexsubquery_engine(session, join_tab, unit->item, conds, having, 1)));
903
change_engine(new subselect_indexsubquery_engine(session,
940
957
Force using of tmp table if sorting by a SP or UDF function due to
941
958
their expensive and probably non-deterministic nature.
943
for (Order *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
960
for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
945
962
Item *item= *tmp_order->item;
946
963
if (item->is_expensive())
985
1002
tmp_table_param.hidden_field_count= (all_fields.elements -
986
1003
fields_list.elements);
987
Order *tmp_group= ((!simple_group &&
1004
order_st *tmp_group= ((!simple_group &&
988
1005
! (test_flags.test(TEST_NO_KEY_GROUP))) ? group_list :
991
1008
Pushing LIMIT to the temporary table creation is not applicable
992
1009
when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1089
1106
If this join belongs to an uncacheable subquery save
1090
1107
the original join
1092
if (select_lex->uncacheable.any() &&
1093
! is_top_level_join() &&
1109
if (select_lex->uncacheable && !is_top_level_join() &&
1094
1110
init_save_join_tab())
1103
1117
setup_subq_exit:
1104
1118
/* Even with zero matching rows, subqueries in the HAVING clause
1175
1189
if (!(tmp_join= (Join*)session->alloc(sizeof(Join))))
1178
1191
error= 0; // Ensure that tmp_join.error= 0
1184
1196
bool Join::save_join_tab()
1186
if (! join_tab_save && select_lex->master_unit()->uncacheable.any())
1198
if (!join_tab_save && select_lex->master_unit()->uncacheable)
1188
1200
if (!(join_tab_save= (JoinTable*)session->memdup((unsigned char*) join_tab,
1189
1201
sizeof(JoinTable) * tables)))
1433
1445
exec_tmp_table2= create_tmp_table(session,
1434
1446
&curr_join->tmp_table_param,
1435
1447
*curr_all_fields,
1437
1449
curr_join->select_distinct &&
1438
1450
!curr_join->group_list,
1439
1451
1, curr_join->select_options,
1791
1803
is called after all rows are sent, but before EOF packet is sent.
1793
1805
For a simple SELECT with no subqueries this function performs a full
1794
cleanup of the Join and calls unlockReadTables to free used base
1806
cleanup of the Join and calls mysql_unlock_read_tables to free used base
1797
1809
If a Join is executed for a subquery or if it has a subquery, we can't
2133
2145
new_item->fix_fields(session, (Item **) 0);
2134
2146
session->change_item_tree(it.ref(), new_item);
2135
for (Order *tmp= group_tmp; tmp; tmp= tmp->next)
2147
for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
2137
2149
if (*tmp->item == item)
2138
2150
session->change_item_tree(tmp->item, new_item);
2465
2477
return NESTED_LOOP_ERROR;
2467
2479
return NESTED_LOOP_NO_MORE_ROWS;
2468
if (join->session->getKilled()) // Aborted by user
2480
if (join->session->killed) // Aborted by user
2470
2482
join->session->send_kill_message();
2471
2483
return NESTED_LOOP_KILLED;
2677
2689
info= &join_tab->read_record;
2680
if (join->session->getKilled())
2692
if (join->session->killed)
2682
2694
join->session->send_kill_message();
2683
2695
return NESTED_LOOP_KILLED;
2810
2822
Table *table= join->tmp_table;
2812
if (join->session->getKilled()) // Aborted by user
2824
if (join->session->killed) // Aborted by user
2814
2826
join->session->send_kill_message();
2815
2827
return NESTED_LOOP_KILLED;
2817
2829
if (!end_of_records)
2819
2831
copy_fields(&join->tmp_table_param);
2820
if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
2821
return NESTED_LOOP_ERROR;
2832
copy_funcs(join->tmp_table_param.items_to_copy);
2822
2833
if (!join->having || join->having->val_int())
2826
2837
if ((error=table->cursor->insertRecord(table->getInsertRecord())))
2828
2839
if (!table->cursor->is_fatal_error(error, HA_CHECK_DUP))
2830
return NESTED_LOOP_OK;
2833
2842
my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
2834
2843
return NESTED_LOOP_ERROR; // Table is_full error
2851
2860
enum_nested_loop_state end_update(Join *join, JoinTable *, bool end_of_records)
2853
2862
Table *table= join->tmp_table;
2857
2866
if (end_of_records)
2858
2867
return NESTED_LOOP_OK;
2859
if (join->session->getKilled()) // Aborted by user
2868
if (join->session->killed) // Aborted by user
2861
2870
join->session->send_kill_message();
2862
2871
return NESTED_LOOP_KILLED;
2903
2912
memcpy(table->getInsertRecord()+key_part->offset, group->buff, 1);
2905
2914
init_tmptable_sum_functions(join->sum_funcs);
2906
if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
2907
return NESTED_LOOP_ERROR;
2915
copy_funcs(join->tmp_table_param.items_to_copy);
2908
2916
if ((error=table->cursor->insertRecord(table->getInsertRecord())))
2910
2918
my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
2931
2939
init_tmptable_sum_functions(join->sum_funcs);
2932
2940
copy_fields(&join->tmp_table_param); // Groups are copied twice.
2933
if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
2934
return NESTED_LOOP_ERROR;
2941
copy_funcs(join->tmp_table_param.items_to_copy);
2936
2943
if (!(error= table->cursor->insertRecord(table->getInsertRecord())))
2937
2944
join->send_records++; // New group
2991
2998
calc how big buffer we need for comparing group entries.
2993
static void calc_group_buffer(Join *join, Order *group)
3000
static void calc_group_buffer(Join *join,order_st *group)
2995
3002
uint32_t key_length=0, parts=0, null_parts=0;
3016
3023
case REAL_RESULT:
3017
3024
key_length+= sizeof(double);
3020
3026
case INT_RESULT:
3021
3027
key_length+= sizeof(int64_t);
3024
3029
case DECIMAL_RESULT:
3025
3030
key_length+= my_decimal_get_binary_size(group_item->max_length -
3026
3031
(group_item->decimals ? 1 : 0),
3027
3032
group_item->decimals);
3030
3034
case STRING_RESULT:
3032
enum enum_field_types type= group_item->field_type();
3036
enum enum_field_types type= group_item->field_type();
3038
As items represented as DATE/TIME fields in the group buffer
3039
have STRING_RESULT result type, we increase the length
3040
by 8 as maximum pack length of such fields.
3042
if (type == DRIZZLE_TYPE_DATE ||
3043
type == DRIZZLE_TYPE_DATETIME ||
3044
type == DRIZZLE_TYPE_TIMESTAMP)
3034
As items represented as DATE/TIME fields in the group buffer
3035
have STRING_RESULT result type, we increase the length
3036
by 8 as maximum pack length of such fields.
3051
Group strings are taken as varstrings and require an length field.
3052
A field is not yet created by create_tmp_field()
3053
and the sizes should match up.
3038
if (type == DRIZZLE_TYPE_DATE ||
3039
type == DRIZZLE_TYPE_DATETIME ||
3040
type == DRIZZLE_TYPE_TIMESTAMP)
3047
Group strings are taken as varstrings and require an length field.
3048
A field is not yet created by create_tmp_field()
3049
and the sizes should match up.
3051
key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
3055
key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
3057
3060
/* This case should never be choosen */
3059
3062
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
3065
3066
if (group_item->maybe_null)
3069
3069
join->tmp_table_param.group_length=key_length+null_parts;
3070
3070
join->tmp_table_param.group_parts=parts;
3071
3071
join->tmp_table_param.group_null_parts=null_parts;
4959
4959
/** Update the dependency map for the sort order. */
4960
static void update_depend_map(Join *join, Order *order)
4960
static void update_depend_map(Join *join, order_st *order)
4962
4962
for (; order ; order=order->next)
4996
4996
Returns new sort order
4998
static Order *remove_constants(Join *join,Order *first_order, COND *cond, bool change_list, bool *simple_order)
4998
static order_st *remove_constants(Join *join,order_st *first_order, COND *cond, bool change_list, bool *simple_order)
5000
5000
if (join->tables == join->const_tables)
5001
5001
return change_list ? 0 : first_order; // No need to sort
5003
Order *order,**prev_ptr;
5003
order_st *order,**prev_ptr;
5004
5004
table_map first_table= join->join_tab[join->const_tables].table->map;
5005
5005
table_map not_const_tables= ~join->const_table_map;
5881
5881
if (join->const_tables != join->tables)
5883
5883
optimize_keyuse(join, keyuse_array);
5884
// @note c_str() is not likely to be valid here if dtrace expects it to
5885
// exist for any period of time.
5886
DRIZZLE_QUERY_OPT_CHOOSE_PLAN_START(join->session->getQueryString()->c_str(), join->session->thread_id);
5884
DRIZZLE_QUERY_OPT_CHOOSE_PLAN_START(join->session->query.c_str(), join->session->thread_id);
5887
5885
bool res= choose_plan(join, all_table_map & ~join->const_table_map);
5888
5886
DRIZZLE_QUERY_OPT_CHOOSE_PLAN_DONE(res ? 1 : 0);
5955
5953
Return table number if there is only one table in sort order
5956
5954
and group and order is compatible, else return 0.
5958
static Table *get_sort_by_table(Order *a, Order *b,TableList *tables)
5956
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables)
5960
5958
table_map map= (table_map) 0;
6010
6008
If first parts has different direction, change it to second part
6011
6009
(group is sorted like order)
6013
static bool test_if_subpart(Order *a, Order *b)
6011
static bool test_if_subpart(order_st *a,order_st *b)
6015
6013
for (; a && b; a=a->next,b=b->next)