~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/opt_sum.cc

Merged from jay.

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
#include <drizzled/sql_select.h>
52
52
 
53
53
static bool find_key_for_maxmin(bool max_fl, TABLE_REF *ref, Field* field,
54
 
                                COND *cond, uint32_t *range_fl,
55
 
                                uint32_t *key_prefix_length);
 
54
                                COND *cond, uint *range_fl,
 
55
                                uint *key_prefix_length);
56
56
static int reckey_in_range(bool max_fl, TABLE_REF *ref, Field* field,
57
 
                            COND *cond, uint32_t range_fl, uint32_t prefix_len);
 
57
                            COND *cond, uint range_fl, uint prefix_len);
58
58
static int maxmin_in_range(bool max_fl, Field* field, COND *cond);
59
59
 
60
60
 
74
74
    #                   Multiplication of number of rows in all tables
75
75
*/
76
76
 
77
 
static uint64_t get_exact_record_count(TableList *tables)
 
77
static uint64_t get_exact_record_count(TABLE_LIST *tables)
78
78
{
79
79
  uint64_t count= 1;
80
 
  for (TableList *tl= tables; tl; tl= tl->next_leaf)
 
80
  for (TABLE_LIST *tl= tables; tl; tl= tl->next_leaf)
81
81
  {
82
82
    ha_rows tmp= tl->table->file->records();
83
83
    if ((tmp == HA_POS_ERROR))
109
109
    HA_ERR_... if a deadlock or a lock wait timeout happens, for example
110
110
*/
111
111
 
112
 
int opt_sum_query(TableList *tables, List<Item> &all_fields,COND *conds)
 
112
int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds)
113
113
{
114
114
  List_iterator_fast<Item> it(all_fields);
115
115
  int const_result= 1;
128
128
    Analyze outer join dependencies, and, if possible, compute the number
129
129
    of returned rows.
130
130
  */
131
 
  for (TableList *tl= tables; tl; tl= tl->next_leaf)
 
131
  for (TABLE_LIST *tl= tables; tl; tl= tl->next_leaf)
132
132
  {
133
 
    TableList *embedded;
 
133
    TABLE_LIST *embedded;
134
134
    for (embedded= tl ; embedded; embedded= embedded->embedding)
135
135
    {
136
136
      if (embedded->on_expr)
227
227
        Item *expr=item_sum->args[0];
228
228
        if (expr->real_item()->type() == Item::FIELD_ITEM)
229
229
        {
230
 
          unsigned char key_buff[MAX_KEY_LENGTH];
 
230
          uchar key_buff[MAX_KEY_LENGTH];
231
231
          TABLE_REF ref;
232
 
          uint32_t range_fl, prefix_len;
 
232
          uint range_fl, prefix_len;
233
233
 
234
234
          ref.key_buff= key_buff;
235
235
          Item_field *item_field= (Item_field*) (expr->real_item());
236
 
          Table *table= item_field->field->table;
 
236
          TABLE *table= item_field->field->table;
237
237
 
238
238
          /* 
239
239
            Look for a partial key that can be used for optimization.
375
375
        Item *expr=item_sum->args[0];
376
376
        if (expr->real_item()->type() == Item::FIELD_ITEM)
377
377
        {
378
 
          unsigned char key_buff[MAX_KEY_LENGTH];
 
378
          uchar key_buff[MAX_KEY_LENGTH];
379
379
          TABLE_REF ref;
380
 
          uint32_t range_fl, prefix_len;
 
380
          uint range_fl, prefix_len;
381
381
 
382
382
          ref.key_buff= key_buff;
383
383
          Item_field *item_field= (Item_field*) (expr->real_item());
384
 
          Table *table= item_field->field->table;
 
384
          TABLE *table= item_field->field->table;
385
385
 
386
386
          /* 
387
387
            Look for a partial key that can be used for optimization.
593
593
 
594
594
static bool matching_cond(bool max_fl, TABLE_REF *ref, KEY *keyinfo, 
595
595
                          KEY_PART_INFO *field_part, COND *cond,
596
 
                          key_part_map *key_part_used, uint32_t *range_fl,
597
 
                          uint32_t *prefix_len)
 
596
                          key_part_map *key_part_used, uint *range_fl,
 
597
                          uint *prefix_len)
598
598
{
599
599
  if (!cond)
600
600
    return 1;
667
667
    less_fl= 1-less_fl;                         // Convert '<' -> '>' (etc)
668
668
 
669
669
  /* Check if field is part of the tested partial key */
670
 
  unsigned char *key_ptr= ref->key_buff;
 
670
  uchar *key_ptr= ref->key_buff;
671
671
  KEY_PART_INFO *part;
672
672
  for (part= keyinfo->key_part; ; key_ptr+= part++->store_length)
673
673
 
685
685
  key_part_map org_key_part_used= *key_part_used;
686
686
  if (eq_type || between || max_fl == less_fl)
687
687
  {
688
 
    uint32_t length= (key_ptr-ref->key_buff)+part->store_length;
 
688
    uint length= (key_ptr-ref->key_buff)+part->store_length;
689
689
    if (ref->key_length < length)
690
690
    {
691
691
    /* Ultimately ref->key_length will contain the length of the search key */
716
716
    if (is_null)
717
717
    {
718
718
      part->field->set_null();
719
 
      *key_ptr= (unsigned char) 1;
 
719
      *key_ptr= (uchar) 1;
720
720
    }
721
721
    else
722
722
    {
723
723
      store_val_in_field(part->field, args[between && max_fl ? 2 : 1],
724
724
                         CHECK_FIELD_IGNORE);
725
725
      if (part->null_bit) 
726
 
        *key_ptr++= (unsigned char) test(part->field->is_null());
 
726
        *key_ptr++= (uchar) test(part->field->is_null());
727
727
      part->field->get_key_image(key_ptr, part->length, Field::itRAW);
728
728
    }
729
729
    if (is_field_part)
797
797
      
798
798
static bool find_key_for_maxmin(bool max_fl, TABLE_REF *ref,
799
799
                                Field* field, COND *cond,
800
 
                                uint32_t *range_fl, uint32_t *prefix_len)
 
800
                                uint *range_fl, uint *prefix_len)
801
801
{
802
802
  if (!(field->flags & PART_KEY_FLAG))
803
803
    return 0;                                        // Not key field
804
804
 
805
 
  Table *table= field->table;
806
 
  uint32_t idx= 0;
 
805
  TABLE *table= field->table;
 
806
  uint idx= 0;
807
807
 
808
808
  KEY *keyinfo,*keyinfo_end;
809
809
  for (keyinfo= table->key_info, keyinfo_end= keyinfo+table->s->keys ;
813
813
    KEY_PART_INFO *part,*part_end;
814
814
    key_part_map key_part_to_use= 0;
815
815
    /*
816
 
      Perform a check if index is not disabled by ALTER Table
 
816
      Perform a check if index is not disabled by ALTER TABLE
817
817
      or IGNORE INDEX.
818
818
    */
819
819
    if (!table->keys_in_use_for_query.is_set(idx))
820
820
      continue;
821
 
    uint32_t jdx= 0;
 
821
    uint jdx= 0;
822
822
    *prefix_len= 0;
823
823
    for (part= keyinfo->key_part, part_end= part+keyinfo->key_parts ;
824
824
         part != part_end ;
903
903
*/
904
904
 
905
905
static int reckey_in_range(bool max_fl, TABLE_REF *ref, Field* field,
906
 
                            COND *cond, uint32_t range_fl, uint32_t prefix_len)
 
906
                            COND *cond, uint range_fl, uint prefix_len)
907
907
{
908
908
  if (key_cmp_if_same(field->table, ref->key_buff, ref->key, prefix_len))
909
909
    return 1;