~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

modifying folder structure

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
  @file
18
18
 
19
19
  @brief
20
 
  mysql_select and join optimization
 
20
  select_query and join optimization
21
21
 
22
22
  @defgroup Query_Optimizer  Query Optimizer
23
23
  @{
78
78
static Item* part_of_refkey(Table *form,Field *field);
79
79
static bool cmp_buffer_with_ref(JoinTable *tab);
80
80
static void change_cond_ref_to_const(Session *session,
81
 
                                     vector<COND_CMP>& save_list,
 
81
                                     list<COND_CMP>& save_list,
82
82
                                     Item *and_father,
83
83
                                     Item *cond,
84
84
                                     Item *field,
131
131
    unit->set_limit(unit->global_parameters);
132
132
    session->session_marker= 0;
133
133
    /*
134
 
      'options' of mysql_select will be set in JOIN, as far as JOIN for
 
134
      'options' of select_query will be set in JOIN, as far as JOIN for
135
135
      every PS/SP execution new, we will not need reset this flag if
136
136
      setup_tables_done_option changed for next rexecution
137
137
    */
138
 
    res= mysql_select(session,
 
138
    res= select_query(session,
139
139
                      &select_lex->ref_pointer_array,
140
140
                      (TableList*) select_lex->table_list.first,
141
141
                      select_lex->with_wild,
270
270
 
271
271
/*****************************************************************************
272
272
  Check fields, find best join, do the select and output fields.
273
 
  mysql_select assumes that all tables are already opened
 
273
  select_query assumes that all tables are already opened
274
274
*****************************************************************************/
275
275
 
276
276
/*
350
350
  @retval
351
351
    true   an error
352
352
*/
353
 
bool mysql_select(Session *session,
 
353
bool select_query(Session *session,
354
354
                  Item ***rref_pointer_array,
355
355
                  TableList *tables, 
356
356
                  uint32_t wild_num, 
931
931
 
932
932
  /*
933
933
    we should restore old value of count_cuted_fields because
934
 
    store_val_in_field can be called from mysql_insert
 
934
    store_val_in_field can be called from insert_query
935
935
    with select_insert, which make count_cuted_fields= 1
936
936
   */
937
937
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
2364
2364
  and_level
2365
2365
*/
2366
2366
static void change_cond_ref_to_const(Session *session,
2367
 
                                     vector<COND_CMP>& save_list,
 
2367
                                     list<COND_CMP>& save_list,
2368
2368
                                     Item *and_father,
2369
2369
                                     Item *cond,
2370
2370
                                     Item *field,
2470
2470
}
2471
2471
 
2472
2472
static void propagate_cond_constants(Session *session, 
2473
 
                                     vector<COND_CMP>& save_list, 
 
2473
                                     list<COND_CMP>& save_list, 
2474
2474
                                     COND *and_father, 
2475
2475
                                     COND *cond)
2476
2476
{
2479
2479
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2480
2480
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2481
2481
    Item *item;
2482
 
    vector<COND_CMP> save;
 
2482
    list<COND_CMP> save;
2483
2483
    while ((item=li++))
2484
2484
    {
2485
2485
      propagate_cond_constants(session, save, and_level ? cond : item, item);
2487
2487
    if (and_level)
2488
2488
    {
2489
2489
      // Handle other found items
2490
 
      for (vector<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
 
2490
      for (list<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
2491
2491
      {
2492
 
        Item **args= iter->cmp_func->arguments();
 
2492
        Item **args= iter->second->arguments();
2493
2493
        if (not args[0]->const_item())
2494
2494
        {
2495
 
          change_cond_ref_to_const(session, save_list, iter->and_level,
2496
 
                                   iter->and_level, args[0], args[1] );
 
2495
          change_cond_ref_to_const(session, save, iter->first,
 
2496
                                   iter->first, args[0], args[1] );
2497
2497
        }
2498
2498
      }
2499
2499
    }
2684
2684
                             &join->cond_equal);
2685
2685
 
2686
2686
    /* change field = field to field = const for each found field = const */
2687
 
    vector<COND_CMP> temp;
 
2687
    list<COND_CMP> temp;
2688
2688
    propagate_cond_constants(session, temp, conds, conds);
2689
2689
    /*
2690
2690
      Remove all instances of item == item
3053
3053
    table->emptyRecord();
3054
3054
    if (table->group && join->tmp_table_param.sum_func_count &&
3055
3055
        table->getShare()->sizeKeys() && !table->cursor->inited)
3056
 
      table->cursor->startIndexScan(0, 0);
 
3056
    {
 
3057
      int tmp_error;
 
3058
      tmp_error= table->cursor->startIndexScan(0, 0);
 
3059
      if (tmp_error != 0)
 
3060
      {
 
3061
        table->print_error(tmp_error, MYF(0));
 
3062
        return -1;
 
3063
      }
 
3064
    }
3057
3065
  }
3058
3066
  /* Set up select_end */
3059
3067
  Next_select_func end_select= setup_end_select_func(join);
3531
3539
 
3532
3540
  if (!table->cursor->inited)
3533
3541
  {
3534
 
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
 
3542
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
 
3543
    if (error != 0)
 
3544
    {
 
3545
      table->print_error(error, MYF(0));
 
3546
    }
3535
3547
  }
3536
3548
 
3537
3549
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3579
3591
 
3580
3592
  /* Initialize the index first */
3581
3593
  if (!table->cursor->inited)
3582
 
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
 
3594
  {
 
3595
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
 
3596
    if (error != 0)
 
3597
      return table->report_error(error);
 
3598
  }
3583
3599
 
3584
3600
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3585
3601
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
3613
3629
  Table *table= tab->table;
3614
3630
 
3615
3631
  if (!table->cursor->inited)
3616
 
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
 
3632
  {
 
3633
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
 
3634
    if (error != 0)
 
3635
      return table->report_error(error);
 
3636
  }
3617
3637
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3618
3638
    return -1;
3619
3639
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
3728
3748
  if (tab->select && tab->select->quick && tab->select->quick->reset())
3729
3749
    return 1;
3730
3750
 
3731
 
  tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true);
 
3751
  if (tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true))
 
3752
    return 1;
3732
3753
 
3733
3754
  return (*tab->read_record.read_record)(&tab->read_record);
3734
3755
}
3761
3782
  }
3762
3783
 
3763
3784
  if (!table->cursor->inited)
3764
 
    table->cursor->startIndexScan(tab->index, tab->sorted);
 
3785
  {
 
3786
    error= table->cursor->startIndexScan(tab->index, tab->sorted);
 
3787
    if (error != 0)
 
3788
    {
 
3789
      table->report_error(error);
 
3790
      return -1;
 
3791
    }
 
3792
  }
3765
3793
  if ((error=tab->table->cursor->index_first(tab->table->getInsertRecord())))
3766
3794
  {
3767
3795
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3820
3848
  tab->read_record.index=tab->index;
3821
3849
  tab->read_record.record=table->getInsertRecord();
3822
3850
  if (!table->cursor->inited)
3823
 
    table->cursor->startIndexScan(tab->index, 1);
 
3851
  {
 
3852
    error= table->cursor->startIndexScan(tab->index, 1);
 
3853
    if (error != 0)
 
3854
      return table->report_error(error);
 
3855
  }
3824
3856
  if ((error= tab->table->cursor->index_last(tab->table->getInsertRecord())))
3825
3857
    return table->report_error(error);
3826
3858
 
3900
3932
              error=join->result->send_data(*join->fields) ? 1 : 0;
3901
3933
            join->send_records++;
3902
3934
          }
3903
 
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
 
3935
          if (join->rollup.getState() != Rollup::STATE_NONE && error <= 0)
3904
3936
          {
3905
3937
            if (join->rollup_send_data((uint32_t) (idx+1)))
3906
3938
              error= 1;
3990
4022
            return NESTED_LOOP_ERROR;
3991
4023
          }
3992
4024
        }
3993
 
        if (join->rollup.state != ROLLUP::STATE_NONE)
 
4025
        if (join->rollup.getState() != Rollup::STATE_NONE)
3994
4026
        {
3995
4027
          if (join->rollup_write_data((uint32_t) (idx+1), table))
3996
4028
            return NESTED_LOOP_ERROR;
5099
5131
  org_record=(char*) (record=table->getInsertRecord())+offset;
5100
5132
  new_record=(char*) table->getUpdateRecord()+offset;
5101
5133
 
5102
 
  cursor->startTableScan(1);
 
5134
  if ((error= cursor->startTableScan(1)))
 
5135
    goto err;
 
5136
 
5103
5137
  error=cursor->rnd_next(record);
5104
5138
  for (;;)
5105
5139
  {
5216
5250
    return(1);
5217
5251
  }
5218
5252
 
5219
 
  cursor->startTableScan(1);
 
5253
  if ((error= cursor->startTableScan(1)))
 
5254
    goto err;
 
5255
 
5220
5256
  key_pos= &key_buffer[0];
5221
5257
  for (;;)
5222
5258
  {