~ubuntu-branches/ubuntu/oneiric/mysql-5.1/oneiric-updates

« back to all changes in this revision

Viewing changes to sql/opt_sum.cc

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2011-07-17 17:26:27 UTC
  • mfrom: (1.3.8 upstream)
  • mto: This revision was merged to the branch mainline in revision 27.
  • Revision ID: james.westby@ubuntu.com-20110717172627-6ml4e6l16adhecmy
Tags: 5.1.58-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
211
211
/**
212
212
  Substitutes constants for some COUNT(), MIN() and MAX() functions.
213
213
 
 
214
  @param thd                   thread handler
214
215
  @param tables                list of leaves of join table tree
215
216
  @param all_fields            All fields to be returned
216
217
  @param conds                 WHERE clause
228
229
    HA_ERR_KEY_NOT_FOUND on impossible conditions
229
230
  @retval
230
231
    HA_ERR_... if a deadlock or a lock wait timeout happens, for example
 
232
  @retval
 
233
    ER_...     e.g. ER_SUBQUERY_NO_1_ROW
231
234
*/
232
235
 
233
 
int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds)
 
236
int opt_sum_query(THD *thd,
 
237
                  TABLE_LIST *tables, List<Item> &all_fields, COND *conds)
234
238
{
235
239
  List_iterator_fast<Item> it(all_fields);
236
240
  int const_result= 1;
242
246
  Item *item;
243
247
  int error;
244
248
 
 
249
  DBUG_ENTER("opt_sum_query");
 
250
 
245
251
  if (conds)
246
252
    where_tables= conds->used_tables();
247
253
 
269
275
          WHERE t2.field IS NULL;
270
276
      */
271
277
      if (tl->table->map & where_tables)
272
 
        return 0;
 
278
        DBUG_RETURN(0);
273
279
    }
274
280
    else
275
281
      used_tables|= tl->table->map;
297
303
      {
298
304
        tl->table->file->print_error(error, MYF(0));
299
305
        tl->table->in_use->fatal_error();
300
 
        return error;
 
306
        DBUG_RETURN(error);
301
307
      }
302
308
      count*= tl->table->file->stats.records;
303
309
    }
390
396
          if (error)
391
397
          {
392
398
            if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
393
 
              return HA_ERR_KEY_NOT_FOUND;            // No rows matching WHERE
 
399
              DBUG_RETURN(HA_ERR_KEY_NOT_FOUND); // No rows matching WHERE
394
400
            /* HA_ERR_LOCK_DEADLOCK or some other error */
395
401
            table->file->print_error(error, MYF(0));
396
 
            return(error);
 
402
            DBUG_RETURN(error);
397
403
          }
398
404
          removed_tables|= table->map;
399
405
        }
437
443
        const_result= 0;
438
444
    }
439
445
  }
 
446
 
 
447
  if (thd->is_error())
 
448
    DBUG_RETURN(thd->main_da.sql_errno());
 
449
 
440
450
  /*
441
451
    If we have a where clause, we can only ignore searching in the
442
452
    tables if MIN/MAX optimisation replaced all used tables
446
456
  */
447
457
  if (removed_tables && used_tables != removed_tables)
448
458
    const_result= 0;                            // We didn't remove all tables
449
 
  return const_result;
 
459
  DBUG_RETURN(const_result);
450
460
}
451
461
 
452
462
 
732
742
 
733
743
    if (is_null || (is_null_safe_eq && args[1]->is_null()))
734
744
    {
 
745
      /*
 
746
        If we have a non-nullable index, we cannot use it,
 
747
        since set_null will be ignored, and we will compare uninitialized data.
 
748
      */
 
749
      if (!part->field->real_maybe_null())
 
750
        DBUG_RETURN(false);
735
751
      part->field->set_null();
736
752
      *key_ptr= (uchar) 1;
737
753
    }
802
818
  @param[out]    prefix_len  Length of prefix for the search range
803
819
 
804
820
  @note
805
 
    This function may set table->key_read to 1, which must be reset after
806
 
    index is used! (This can only happen when function returns 1)
 
821
    This function may set field->table->key_read to true,
 
822
    which must be reset after index is used!
 
823
    (This can only happen when function returns 1)
807
824
 
808
825
  @retval
809
826
    0   Index can not be used to optimize MIN(field)/MAX(field)
818
835
                                uint *range_fl, uint *prefix_len)
819
836
{
820
837
  if (!(field->flags & PART_KEY_FLAG))
821
 
    return 0;                                        // Not key field
 
838
    return false;                               // Not key field
 
839
 
 
840
  DBUG_ENTER("find_key_for_maxmin");
822
841
 
823
842
  TABLE *table= field->table;
824
843
  uint idx= 0;
843
862
         part++, jdx++, key_part_to_use= (key_part_to_use << 1) | 1)
844
863
    {
845
864
      if (!(table->file->index_flags(idx, jdx, 0) & HA_READ_ORDER))
846
 
        return 0;
 
865
        DBUG_RETURN(false);
847
866
 
848
867
      /* Check whether the index component is partial */
849
868
      Field *part_field= table->field[part->fieldnr-1];
892
911
          */
893
912
          if (field->part_of_key.is_set(idx))
894
913
            table->set_keyread(TRUE);
895
 
          return 1;
 
914
          DBUG_RETURN(true);
896
915
        }
897
916
      }
898
917
    }
899
918
  }
900
 
  return 0;
 
919
  DBUG_RETURN(false);
901
920
}
902
921
 
903
922