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) ||
596
593
if (const_tables && !(select_options & SELECT_NO_UNLOCK))
597
session->unlockSomeTables(table, const_tables);
594
mysql_unlock_some_tables(session, table, const_tables);
598
595
if (!conds && outer_join)
600
597
/* Handle the case where we have an OUTER JOIN without a WHERE */
657
654
/* Optimize distinct away if possible */
659
Order *org_order= order;
656
order_st *org_order= order;
660
657
order= remove_constants(this, order,conds,1, &simple_order);
661
658
if (session->is_error())
793
Order *old_group_list;
790
order_st *old_group_list;
794
791
group_list= remove_constants(this, (old_group_list= group_list), conds,
795
792
rollup.state == ROLLUP::STATE_NONE,
940
937
Force using of tmp table if sorting by a SP or UDF function due to
941
938
their expensive and probably non-deterministic nature.
943
for (Order *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
940
for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
945
942
Item *item= *tmp_order->item;
946
943
if (item->is_expensive())
985
982
tmp_table_param.hidden_field_count= (all_fields.elements -
986
983
fields_list.elements);
987
Order *tmp_group= ((!simple_group &&
984
order_st *tmp_group= ((!simple_group &&
988
985
! (test_flags.test(TEST_NO_KEY_GROUP))) ? group_list :
991
988
Pushing LIMIT to the temporary table creation is not applicable
992
989
when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1433
1430
exec_tmp_table2= create_tmp_table(session,
1434
1431
&curr_join->tmp_table_param,
1435
1432
*curr_all_fields,
1437
1434
curr_join->select_distinct &&
1438
1435
!curr_join->group_list,
1439
1436
1, curr_join->select_options,
1791
1788
is called after all rows are sent, but before EOF packet is sent.
1793
1790
For a simple SELECT with no subqueries this function performs a full
1794
cleanup of the Join and calls unlockReadTables to free used base
1791
cleanup of the Join and calls mysql_unlock_read_tables to free used base
1797
1794
If a Join is executed for a subquery or if it has a subquery, we can't
2133
2130
new_item->fix_fields(session, (Item **) 0);
2134
2131
session->change_item_tree(it.ref(), new_item);
2135
for (Order *tmp= group_tmp; tmp; tmp= tmp->next)
2132
for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
2137
2134
if (*tmp->item == item)
2138
2135
session->change_item_tree(tmp->item, new_item);
2465
2462
return NESTED_LOOP_ERROR;
2467
2464
return NESTED_LOOP_NO_MORE_ROWS;
2468
if (join->session->getKilled()) // Aborted by user
2465
if (join->session->killed) // Aborted by user
2470
2467
join->session->send_kill_message();
2471
2468
return NESTED_LOOP_KILLED;
2677
2674
info= &join_tab->read_record;
2680
if (join->session->getKilled())
2677
if (join->session->killed)
2682
2679
join->session->send_kill_message();
2683
2680
return NESTED_LOOP_KILLED;
2810
2807
Table *table= join->tmp_table;
2812
if (join->session->getKilled()) // Aborted by user
2809
if (join->session->killed) // Aborted by user
2814
2811
join->session->send_kill_message();
2815
2812
return NESTED_LOOP_KILLED;
2817
2814
if (!end_of_records)
2819
2816
copy_fields(&join->tmp_table_param);
2820
if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
2821
return NESTED_LOOP_ERROR;
2817
copy_funcs(join->tmp_table_param.items_to_copy);
2822
2818
if (!join->having || join->having->val_int())
2851
2847
enum_nested_loop_state end_update(Join *join, JoinTable *, bool end_of_records)
2853
2849
Table *table= join->tmp_table;
2857
2853
if (end_of_records)
2858
2854
return NESTED_LOOP_OK;
2859
if (join->session->getKilled()) // Aborted by user
2855
if (join->session->killed) // Aborted by user
2861
2857
join->session->send_kill_message();
2862
2858
return NESTED_LOOP_KILLED;
2903
2899
memcpy(table->getInsertRecord()+key_part->offset, group->buff, 1);
2905
2901
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;
2902
copy_funcs(join->tmp_table_param.items_to_copy);
2908
2903
if ((error=table->cursor->insertRecord(table->getInsertRecord())))
2910
2905
my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
2931
2926
init_tmptable_sum_functions(join->sum_funcs);
2932
2927
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;
2928
copy_funcs(join->tmp_table_param.items_to_copy);
2936
2930
if (!(error= table->cursor->insertRecord(table->getInsertRecord())))
2937
2931
join->send_records++; // New group
2991
2985
calc how big buffer we need for comparing group entries.
2993
static void calc_group_buffer(Join *join, Order *group)
2987
static void calc_group_buffer(Join *join,order_st *group)
2995
2989
uint32_t key_length=0, parts=0, null_parts=0;
3016
3010
case REAL_RESULT:
3017
3011
key_length+= sizeof(double);
3020
3013
case INT_RESULT:
3021
3014
key_length+= sizeof(int64_t);
3024
3016
case DECIMAL_RESULT:
3025
3017
key_length+= my_decimal_get_binary_size(group_item->max_length -
3026
3018
(group_item->decimals ? 1 : 0),
3027
3019
group_item->decimals);
3030
3021
case STRING_RESULT:
3032
enum enum_field_types type= group_item->field_type();
3023
enum enum_field_types type= group_item->field_type();
3025
As items represented as DATE/TIME fields in the group buffer
3026
have STRING_RESULT result type, we increase the length
3027
by 8 as maximum pack length of such fields.
3029
if (type == DRIZZLE_TYPE_DATE ||
3030
type == DRIZZLE_TYPE_DATETIME ||
3031
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.
3038
Group strings are taken as varstrings and require an length field.
3039
A field is not yet created by create_tmp_field()
3040
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;
3042
key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
3057
3047
/* This case should never be choosen */
3059
3049
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
3065
3053
if (group_item->maybe_null)
3069
3056
join->tmp_table_param.group_length=key_length+null_parts;
3070
3057
join->tmp_table_param.group_parts=parts;
3071
3058
join->tmp_table_param.group_null_parts=null_parts;
4959
4946
/** Update the dependency map for the sort order. */
4960
static void update_depend_map(Join *join, Order *order)
4947
static void update_depend_map(Join *join, order_st *order)
4962
4949
for (; order ; order=order->next)
4996
4983
Returns new sort order
4998
static Order *remove_constants(Join *join,Order *first_order, COND *cond, bool change_list, bool *simple_order)
4985
static order_st *remove_constants(Join *join,order_st *first_order, COND *cond, bool change_list, bool *simple_order)
5000
4987
if (join->tables == join->const_tables)
5001
4988
return change_list ? 0 : first_order; // No need to sort
5003
Order *order,**prev_ptr;
4990
order_st *order,**prev_ptr;
5004
4991
table_map first_table= join->join_tab[join->const_tables].table->map;
5005
4992
table_map not_const_tables= ~join->const_table_map;
5881
5868
if (join->const_tables != join->tables)
5883
5870
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);
5871
DRIZZLE_QUERY_OPT_CHOOSE_PLAN_START(join->session->query.c_str(), join->session->thread_id);
5887
5872
bool res= choose_plan(join, all_table_map & ~join->const_table_map);
5888
5873
DRIZZLE_QUERY_OPT_CHOOSE_PLAN_DONE(res ? 1 : 0);
5955
5940
Return table number if there is only one table in sort order
5956
5941
and group and order is compatible, else return 0.
5958
static Table *get_sort_by_table(Order *a, Order *b,TableList *tables)
5943
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables)
5960
5945
table_map map= (table_map) 0;
6010
5995
If first parts has different direction, change it to second part
6011
5996
(group is sorted like order)
6013
static bool test_if_subpart(Order *a, Order *b)
5998
static bool test_if_subpart(order_st *a,order_st *b)
6015
6000
for (; a && b; a=a->next,b=b->next)