~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Brian Aker
  • Date: 2009-07-09 21:14:05 UTC
  • mto: (1090.1.1 staging)
  • mto: This revision was merged to the branch mainline in revision 1091.
  • Revision ID: brian@gaz-20090709211405-jgl2txbaijtq62a7
Remove of JOIN_TAB to JoinTable

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
                               COND_EQUAL **cond_equal_ref);
69
69
 
70
70
static Item* part_of_refkey(Table *form,Field *field);
71
 
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
 
71
static bool cmp_buffer_with_ref(JoinTable *tab);
72
72
static void change_cond_ref_to_const(Session *session,
73
73
                                     vector<COND_CMP>& save_list,
74
74
                                     Item *and_father,
278
278
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
279
279
    and set appropriate flags in join_tab->packed_info.
280
280
*/
281
 
void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
 
281
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
282
282
{
283
283
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
284
284
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
837
837
}
838
838
 
839
839
/*****************************************************************************
840
 
  Create JOIN_TABS, make a guess about the table types,
 
840
  Create JoinTableS, make a guess about the table types,
841
841
  Approximate how many records will be used in each table
842
842
*****************************************************************************/
843
843
ha_rows get_quick_record_count(Session *session, SQL_SELECT *select, Table *table, const key_map *keys,ha_rows limit)
1057
1057
    }
1058
1058
    else
1059
1059
    {
1060
 
      JOIN_TAB *stat=field->table->reginfo.join_tab;
 
1060
      JoinTable *stat=field->table->reginfo.join_tab;
1061
1061
      key_map possible_keys= field->key_start;
1062
1062
      possible_keys&= field->table->keys_in_use_for_query;
1063
1063
      stat[0].keys|= possible_keys;             // Add possible keys
1581
1581
*/
1582
1582
bool update_ref_and_keys(Session *session,
1583
1583
                         DYNAMIC_ARRAY *keyuse,
1584
 
                         JOIN_TAB *join_tab,
 
1584
                         JoinTable *join_tab,
1585
1585
                         uint32_t tables,
1586
1586
                         COND *cond, 
1587
1587
                         COND_EQUAL *,
1785
1785
  @return
1786
1786
    None
1787
1787
*/
1788
 
void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
 
1788
void add_group_and_distinct_keys(JOIN *join, JoinTable *join_tab)
1789
1789
{
1790
1790
  List<Item_field> indexed_fields;
1791
1791
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
1870
1870
}
1871
1871
 
1872
1872
/**
1873
 
  Compare two JOIN_TAB objects based on the number of accessed records.
 
1873
  Compare two JoinTable objects based on the number of accessed records.
1874
1874
 
1875
 
  @param ptr1 pointer to first JOIN_TAB object
1876
 
  @param ptr2 pointer to second JOIN_TAB object
 
1875
  @param ptr1 pointer to first JoinTable object
 
1876
  @param ptr2 pointer to second JoinTable object
1877
1877
 
1878
1878
  NOTES
1879
1879
    The order relation implemented by join_tab_cmp() is not transitive,
1895
1895
*/
1896
1896
int join_tab_cmp(const void* ptr1, const void* ptr2)
1897
1897
{
1898
 
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
1899
 
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
 
1898
  JoinTable *jt1= *(JoinTable**) ptr1;
 
1899
  JoinTable *jt2= *(JoinTable**) ptr2;
1900
1900
 
1901
1901
  if (jt1->dependent & jt2->table->map)
1902
1902
    return 1;
1914
1914
*/
1915
1915
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
1916
1916
{
1917
 
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
1918
 
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
 
1917
  JoinTable *jt1= *(JoinTable**) ptr1;
 
1918
  JoinTable *jt2= *(JoinTable**) ptr2;
1919
1919
 
1920
1920
  if (jt1->dependent & jt2->table->map)
1921
1921
    return 1;
1927
1927
/**
1928
1928
  Find how much space the prevous read not const tables takes in cache.
1929
1929
*/
1930
 
void calc_used_field_length(Session *, JOIN_TAB *join_tab)
 
1930
void calc_used_field_length(Session *, JoinTable *join_tab)
1931
1931
{
1932
1932
  uint32_t null_fields,blobs,fields,rec_length;
1933
1933
  Field **f_ptr,*field;
2038
2038
    *e1= e2;
2039
2039
}
2040
2040
 
2041
 
bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
2041
bool create_ref_for_key(JOIN *join, JoinTable *j, KEYUSE *org_keyuse,
2042
2042
             table_map used_tables)
2043
2043
{
2044
2044
  KEYUSE *keyuse=org_keyuse;
2211
2211
      1.1 add_key_part saves these to KEYUSE.
2212
2212
      2. create_ref_for_key copies them to TABLE_REF.
2213
2213
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
2214
 
         appropiate JOIN_TAB members.
 
2214
         appropiate JoinTable members.
2215
2215
*/
2216
2216
void add_not_null_conds(JOIN *join)
2217
2217
{
2218
2218
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
2219
2219
  {
2220
 
    JOIN_TAB *tab=join->join_tab+i;
 
2220
    JoinTable *tab=join->join_tab+i;
2221
2221
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF ||
2222
2222
         tab->type == JT_REF_OR_NULL) &&
2223
2223
        !tab->table->maybe_null)
2230
2230
          Item *notnull;
2231
2231
          assert(item->type() == Item::FIELD_ITEM);
2232
2232
          Item_field *not_null_item= (Item_field*)item;
2233
 
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
 
2233
          JoinTable *referred_tab= not_null_item->field->table->reginfo.join_tab;
2234
2234
          /*
2235
2235
            For UPDATE queries such as:
2236
2236
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
2271
2271
    -  pointer to the guarded predicate, if success
2272
2272
    -  0, otherwise
2273
2273
*/
2274
 
COND *add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
 
2274
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
2275
2275
{
2276
2276
  COND *tmp;
2277
2277
  assert(cond != 0);
2536
2536
  DESCRIPTION
2537
2537
    Try to extract and push the index condition down to table handler
2538
2538
*/
2539
 
void push_index_cond(JOIN_TAB *tab, uint32_t keyno, bool other_tbls_ok)
 
2539
void push_index_cond(JoinTable *tab, uint32_t keyno, bool other_tbls_ok)
2540
2540
{
2541
2541
  Item *idx_cond;
2542
2542
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
2587
2587
}
2588
2588
 
2589
2589
/**
2590
 
  cleanup JOIN_TAB.
 
2590
  cleanup JoinTable.
2591
2591
*/
2592
 
void JOIN_TAB::cleanup()
 
2592
void JoinTable::cleanup()
2593
2593
{
2594
2594
  delete select;
2595
2595
  select= 0;
2618
2618
 
2619
2619
bool only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
2620
2620
{
2621
 
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
 
2621
  for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
2622
2622
  {
2623
2623
    if (tables & 1 && !eq_ref_table(join, order, *tab))
2624
2624
      return 0;
2645
2645
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
2646
2646
  @endcode
2647
2647
*/
2648
 
bool eq_ref_table(JOIN *join, order_st *start_order, JOIN_TAB *tab)
 
2648
bool eq_ref_table(JOIN *join, order_st *start_order, JoinTable *tab)
2649
2649
{
2650
2650
  if (tab->cached_eq_ref_table)                 // If cached
2651
2651
    return tab->eq_ref_table;
3457
3457
  }
3458
3458
  if (outer_ref)
3459
3459
    return cmp;
3460
 
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
 
3460
  JoinTable **idx= (JoinTable **) table_join_idx;
3461
3461
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
3462
3462
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
3463
3463
}
3678
3678
  @param cond       condition whose multiple equalities are to be checked
3679
3679
  @param table      constant table that has been read
3680
3680
*/
3681
 
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
 
3681
static void update_const_equal_items(COND *cond, JoinTable *tab)
3682
3682
{
3683
3683
  if (!(cond->used_tables() & tab->table->map))
3684
3684
    return;
3705
3705
      while ((item_field= it++))
3706
3706
      {
3707
3707
        Field *field= item_field->field;
3708
 
        JOIN_TAB *stat= field->table->reginfo.join_tab;
 
3708
        JoinTable *stat= field->table->reginfo.join_tab;
3709
3709
        key_map possible_keys= field->key_start;
3710
3710
        possible_keys&= field->table->keys_in_use_for_query;
3711
3711
        stat[0].const_keys|= possible_keys;
3994
3994
  @retval
3995
3995
    true   Requested join order extension not allowed.
3996
3996
*/
3997
 
bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
 
3997
bool check_interleaving_with_nj(JoinTable *last_tab, JoinTable *next_tab)
3998
3998
{
3999
3999
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
4000
4000
  JOIN *join= last_tab->join;
4038
4038
  return false;
4039
4039
}
4040
4040
 
4041
 
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
 
4041
void advance_sj_state(const table_map remaining_tables, const JoinTable *tab)
4042
4042
{
4043
4043
  TableList *emb_sj_nest;
4044
4044
  if ((emb_sj_nest= tab->emb_sj_nest))
4053
4053
/*
4054
4054
  we assume remaining_tables doesnt contain @tab.
4055
4055
*/
4056
 
void restore_prev_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
 
4056
void restore_prev_sj_state(const table_map remaining_tables, const JoinTable *tab)
4057
4057
{
4058
4058
  TableList *emb_sj_nest;
4059
4059
  if ((emb_sj_nest= tab->emb_sj_nest))
4445
4445
{
4446
4446
  int rc= 0;
4447
4447
  enum_nested_loop_state error= NESTED_LOOP_OK;
4448
 
  JOIN_TAB *join_tab= NULL;
 
4448
  JoinTable *join_tab= NULL;
4449
4449
 
4450
4450
  join->tmp_table= table;                       /* Save for easy recursion */
4451
4451
  join->fields= fields;
4542
4542
  return(join->session->is_error() ? -1 : rc);
4543
4543
}
4544
4544
 
4545
 
enum_nested_loop_state sub_select_cache(JOIN *join, JOIN_TAB *join_tab, bool end_of_records)
 
4545
enum_nested_loop_state sub_select_cache(JOIN *join, JoinTable *join_tab, bool end_of_records)
4546
4546
{
4547
4547
  enum_nested_loop_state rc;
4548
4548
 
4689
4689
  @return
4690
4690
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
4691
4691
*/
4692
 
enum_nested_loop_state sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
 
4692
enum_nested_loop_state sub_select(JOIN *join,JoinTable *join_tab,bool end_of_records)
4693
4693
{
4694
4694
  join_tab->table->null_row=0;
4695
4695
  if (end_of_records)
4765
4765
  return 0;
4766
4766
}
4767
4767
 
4768
 
int safe_index_read(JOIN_TAB *tab)
 
4768
int safe_index_read(JoinTable *tab)
4769
4769
{
4770
4770
  int error;
4771
4771
  Table *table= tab->table;
4777
4777
  return 0;
4778
4778
}
4779
4779
 
4780
 
int join_read_const_table(JOIN_TAB *tab, POSITION *pos)
 
4780
int join_read_const_table(JoinTable *tab, POSITION *pos)
4781
4781
{
4782
4782
  int error;
4783
4783
  Table *table=tab->table;
4855
4855
  return(0);
4856
4856
}
4857
4857
 
4858
 
int join_read_system(JOIN_TAB *tab)
 
4858
int join_read_system(JoinTable *tab)
4859
4859
{
4860
4860
  Table *table= tab->table;
4861
4861
  int error;
4890
4890
  @retval
4891
4891
    1   Got an error (other than row not found) during read
4892
4892
*/
4893
 
int join_read_const(JOIN_TAB *tab)
 
4893
int join_read_const(JoinTable *tab)
4894
4894
{
4895
4895
  int error;
4896
4896
  Table *table= tab->table;
4931
4931
 
4932
4932
  SYNOPSIS
4933
4933
    join_read_key()
4934
 
      tab  JOIN_TAB of the accessed table
 
4934
      tab  JoinTable of the accessed table
4935
4935
 
4936
4936
  DESCRIPTION
4937
4937
    This is "read_fist" function for the "ref" access method. The difference
4942
4942
   -1  - Row not found
4943
4943
    1  - Error
4944
4944
*/
4945
 
int join_read_key(JOIN_TAB *tab)
 
4945
int join_read_key(JoinTable *tab)
4946
4946
{
4947
4947
  int error;
4948
4948
  Table *table= tab->table;
4977
4977
 
4978
4978
  SYNOPSIS
4979
4979
    join_read_always_key()
4980
 
      tab  JOIN_TAB of the accessed table
 
4980
      tab  JoinTable of the accessed table
4981
4981
 
4982
4982
  DESCRIPTION
4983
4983
    This is "read_first" function for the "ref" access method.
4990
4990
   -1  - Row not found
4991
4991
    1  - Error
4992
4992
*/
4993
 
int join_read_always_key(JOIN_TAB *tab)
 
4993
int join_read_always_key(JoinTable *tab)
4994
4994
{
4995
4995
  int error;
4996
4996
  Table *table= tab->table;
5025
5025
  This function is used when optimizing away order_st BY in
5026
5026
  SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC.
5027
5027
*/
5028
 
int join_read_last_key(JOIN_TAB *tab)
 
5028
int join_read_last_key(JoinTable *tab)
5029
5029
{
5030
5030
  int error;
5031
5031
  Table *table= tab->table;
5053
5053
int join_read_next_same_diff(READ_RECORD *info)
5054
5054
{
5055
5055
  Table *table= info->table;
5056
 
  JOIN_TAB *tab=table->reginfo.join_tab;
 
5056
  JoinTable *tab=table->reginfo.join_tab;
5057
5057
  if (tab->insideout_match_tab->found_match)
5058
5058
  {
5059
5059
    KEY *key= tab->table->key_info + tab->index;
5085
5085
{
5086
5086
  int error;
5087
5087
  Table *table= info->table;
5088
 
  JOIN_TAB *tab=table->reginfo.join_tab;
 
5088
  JoinTable *tab=table->reginfo.join_tab;
5089
5089
 
5090
5090
  if ((error=table->file->index_next_same(table->record[0],
5091
5091
                                          tab->ref.key_buff,
5104
5104
{
5105
5105
  int error;
5106
5106
  Table *table= info->table;
5107
 
  JOIN_TAB *tab=table->reginfo.join_tab;
 
5107
  JoinTable *tab=table->reginfo.join_tab;
5108
5108
 
5109
5109
  if ((error=table->file->index_prev(table->record[0])))
5110
5110
    return table->report_error(error);
5117
5117
  return error;
5118
5118
}
5119
5119
 
5120
 
int join_init_quick_read_record(JOIN_TAB *tab)
 
5120
int join_init_quick_read_record(JoinTable *tab)
5121
5121
{
5122
5122
  if (test_if_quick_select(tab) == -1)
5123
5123
    return -1;                                  /* No possible records */
5125
5125
}
5126
5126
 
5127
5127
int rr_sequential(READ_RECORD *info);
5128
 
int init_read_record_seq(JOIN_TAB *tab)
 
5128
int init_read_record_seq(JoinTable *tab)
5129
5129
{
5130
5130
  tab->read_record.read_record= rr_sequential;
5131
5131
  if (tab->read_record.file->ha_rnd_init(1))
5133
5133
  return (*tab->read_record.read_record)(&tab->read_record);
5134
5134
}
5135
5135
 
5136
 
int test_if_quick_select(JOIN_TAB *tab)
 
5136
int test_if_quick_select(JoinTable *tab)
5137
5137
{
5138
5138
  delete tab->select->quick;
5139
5139
  tab->select->quick= 0;
5141
5141
                                        (table_map) 0, HA_POS_ERROR, 0, false);
5142
5142
}
5143
5143
 
5144
 
int join_init_read_record(JOIN_TAB *tab)
 
5144
int join_init_read_record(JoinTable *tab)
5145
5145
{
5146
5146
  if (tab->select && tab->select->quick && tab->select->quick->reset())
5147
5147
    return 1;
5150
5150
  return (*tab->read_record.read_record)(&tab->read_record);
5151
5151
}
5152
5152
 
5153
 
int join_read_first(JOIN_TAB *tab)
 
5153
int join_read_first(JoinTable *tab)
5154
5154
{
5155
5155
  int error;
5156
5156
  Table *table=tab->table;
5191
5191
 
5192
5192
int join_read_next_different(READ_RECORD *info)
5193
5193
{
5194
 
  JOIN_TAB *tab= info->do_insideout_scan;
 
5194
  JoinTable *tab= info->do_insideout_scan;
5195
5195
  if (tab->insideout_match_tab->found_match)
5196
5196
  {
5197
5197
    KEY *key= tab->table->key_info + tab->index;
5220
5220
  return 0;
5221
5221
}
5222
5222
 
5223
 
int join_read_last(JOIN_TAB *tab)
 
5223
int join_read_last(JoinTable *tab)
5224
5224
{
5225
5225
  Table *table=tab->table;
5226
5226
  int error;
5256
5256
/**
5257
5257
  Reading of key with key reference and one part that may be NULL.
5258
5258
*/
5259
 
int join_read_always_key_or_null(JOIN_TAB *tab)
 
5259
int join_read_always_key_or_null(JoinTable *tab)
5260
5260
{
5261
5261
  int res;
5262
5262
 
5275
5275
  int error;
5276
5276
  if ((error= join_read_next_same(info)) >= 0)
5277
5277
    return error;
5278
 
  JOIN_TAB *tab= info->table->reginfo.join_tab;
 
5278
  JoinTable *tab= info->table->reginfo.join_tab;
5279
5279
 
5280
5280
  /* Test if we have already done a read after null key */
5281
5281
  if (*tab->ref.null_ref_key)
5284
5284
  return safe_index_read(tab);                  // then read null keys
5285
5285
}
5286
5286
 
5287
 
enum_nested_loop_state end_send_group(JOIN *join, JOIN_TAB *, bool end_of_records)
 
5287
enum_nested_loop_state end_send_group(JOIN *join, JoinTable *, bool end_of_records)
5288
5288
{
5289
5289
  int idx= -1;
5290
5290
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
5373
5373
  return(NESTED_LOOP_OK);
5374
5374
}
5375
5375
 
5376
 
enum_nested_loop_state end_write_group(JOIN *join, JOIN_TAB *, bool end_of_records)
 
5376
enum_nested_loop_state end_write_group(JOIN *join, JoinTable *, bool end_of_records)
5377
5377
{
5378
5378
  Table *table=join->tmp_table;
5379
5379
  int     idx= -1;
5939
5939
      no_changes
5940
5940
      map
5941
5941
 
5942
 
  If we can use an index, the JOIN_TAB / tab->select struct
 
5942
  If we can use an index, the JoinTable / tab->select struct
5943
5943
  is changed to use the index.
5944
5944
 
5945
5945
  The index must cover all fields in <order>, or it will not be considered.
5953
5953
  @retval
5954
5954
    1    We can use an index.
5955
5955
*/
5956
 
bool test_if_skip_sort_order(JOIN_TAB *tab, order_st *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
5956
bool test_if_skip_sort_order(JoinTable *tab, order_st *order, ha_rows select_limit, bool no_changes, const key_map *map)
5957
5957
{
5958
5958
  int32_t ref_key;
5959
5959
  uint32_t ref_key_parts;
6399
6399
  ha_rows examined_rows;
6400
6400
  Table *table;
6401
6401
  SQL_SELECT *select;
6402
 
  JOIN_TAB *tab;
 
6402
  JoinTable *tab;
6403
6403
 
6404
6404
  if (join->tables == join->const_tables)
6405
6405
    return(0);                          // One row, no need to sort
6722
6722
  return(sort);
6723
6723
}
6724
6724
 
6725
 
void read_cached_record(JOIN_TAB *tab)
 
6725
void read_cached_record(JoinTable *tab)
6726
6726
{
6727
6727
  unsigned char *pos;
6728
6728
  uint32_t length;
6787
6787
    false  The created key is the same as the previous one (and the record
6788
6788
           is already in table->record)
6789
6789
*/
6790
 
static bool cmp_buffer_with_ref(JOIN_TAB *tab)
 
6790
static bool cmp_buffer_with_ref(JoinTable *tab)
6791
6791
{
6792
6792
  bool no_prev_key;
6793
6793
  if (!tab->ref.disable_cache)
7874
7874
    table_map used_tables=0;
7875
7875
    for (uint32_t i=0 ; i < join->tables ; i++)
7876
7876
    {
7877
 
      JOIN_TAB *tab=join->join_tab+i;
 
7877
      JoinTable *tab=join->join_tab+i;
7878
7878
      Table *table=tab->table;
7879
7879
      TableList *table_list= tab->table->pos_in_table_list;
7880
7880
      char buff[512];