~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2003 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
17
/**
18
  @file
19
20
  Optimising of MIN(), MAX() and COUNT(*) queries without 'group by' clause
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
21
  by replacing the aggregate expression with a constant.
1 by brian
clean slate
22
23
  Given a table with a compound key on columns (a,b,c), the following
24
  types of queries are optimised (assuming the table handler supports
25
  the required methods)
26
27
  @verbatim
28
  SELECT COUNT(*) FROM t1[,t2,t3,...]
29
  SELECT MIN(b) FROM t1 WHERE a=const
30
  SELECT MAX(c) FROM t1 WHERE a=const AND b=const
31
  SELECT MAX(b) FROM t1 WHERE a=const AND b<const
32
  SELECT MIN(b) FROM t1 WHERE a=const AND b>const
33
  SELECT MIN(b) FROM t1 WHERE a=const AND b BETWEEN const AND const
34
  SELECT MAX(b) FROM t1 WHERE a=const AND b BETWEEN const AND const
35
  @endverbatim
36
37
  Instead of '<' one can use '<=', '>', '>=' and '=' as well.
38
  Instead of 'a=const' the condition 'a IS NULL' can be used.
39
40
  If all selected fields are replaced then we will also remove all
41
  involved tables and return the answer without any join. Thus, the
42
  following query will be replaced with a row of two constants:
43
  @verbatim
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
44
  SELECT MAX(b), MIN(d) FROM t1,t2
1 by brian
clean slate
45
    WHERE a=const AND b<const AND d>const
46
  @endverbatim
47
  (assuming a index for column d of table t2 is defined)
48
*/
49
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
50
#include <drizzled/server_includes.h>
51
#include <drizzled/sql_select.h>
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
52
#include <drizzled/item/sum.h>
53
#include <drizzled/item/cmpfunc.h>
1 by brian
clean slate
54
1089.1.14 by Brian Aker
Fix TABLE_REF structure
55
static bool find_key_for_maxmin(bool max_fl, table_reference_st *ref, Field* field,
482 by Brian Aker
Remove uint.
56
                                COND *cond, uint32_t *range_fl,
57
                                uint32_t *key_prefix_length);
1089.1.14 by Brian Aker
Fix TABLE_REF structure
58
static int reckey_in_range(bool max_fl, table_reference_st *ref, Field* field,
482 by Brian Aker
Remove uint.
59
                            COND *cond, uint32_t range_fl, uint32_t prefix_len);
1 by brian
clean slate
60
static int maxmin_in_range(bool max_fl, Field* field, COND *cond);
61
62
63
/*
64
  Get exact count of rows in all tables
65
66
  SYNOPSIS
67
    get_exact_records()
68
    tables		List of tables
69
70
  NOTES
71
    When this is called, we know all table handlers supports HA_HAS_RECORDS
72
    or HA_STATS_RECORDS_IS_EXACT
73
74
  RETURN
163 by Brian Aker
Merge Monty's code.
75
    UINT64_MAX	Error: Could not calculate number of rows
1 by brian
clean slate
76
    #			Multiplication of number of rows in all tables
77
*/
78
327.2.4 by Brian Aker
Refactoring table.h
79
static uint64_t get_exact_record_count(TableList *tables)
1 by brian
clean slate
80
{
151 by Brian Aker
Ulonglong to uint64_t
81
  uint64_t count= 1;
327.2.4 by Brian Aker
Refactoring table.h
82
  for (TableList *tl= tables; tl; tl= tl->next_leaf)
1 by brian
clean slate
83
  {
84
    ha_rows tmp= tl->table->file->records();
85
    if ((tmp == HA_POS_ERROR))
163 by Brian Aker
Merge Monty's code.
86
      return UINT64_MAX;
1 by brian
clean slate
87
    count*= tmp;
88
  }
89
  return count;
90
}
91
92
93
/**
94
  Substitutes constants for some COUNT(), MIN() and MAX() functions.
95
96
  @param tables                list of leaves of join table tree
97
  @param all_fields            All fields to be returned
98
  @param conds                 WHERE clause
99
100
  @note
101
    This function is only called for queries with sum functions and no
102
    GROUP BY part.
103
104
  @retval
105
    0                    no errors
106
  @retval
107
    1                    if all items were resolved
108
  @retval
109
    HA_ERR_KEY_NOT_FOUND on impossible conditions
110
  @retval
111
    HA_ERR_... if a deadlock or a lock wait timeout happens, for example
112
*/
113
327.2.4 by Brian Aker
Refactoring table.h
114
int opt_sum_query(TableList *tables, List<Item> &all_fields,COND *conds)
1 by brian
clean slate
115
{
116
  List_iterator_fast<Item> it(all_fields);
117
  int const_result= 1;
118
  bool recalc_const_item= 0;
151 by Brian Aker
Ulonglong to uint64_t
119
  uint64_t count= 1;
163 by Brian Aker
Merge Monty's code.
120
  bool is_exact_count= true, maybe_exact_count= true;
1 by brian
clean slate
121
  table_map removed_tables= 0, outer_tables= 0, used_tables= 0;
122
  table_map where_tables= 0;
123
  Item *item;
124
  int error;
125
126
  if (conds)
127
    where_tables= conds->used_tables();
128
129
  /*
130
    Analyze outer join dependencies, and, if possible, compute the number
131
    of returned rows.
132
  */
327.2.4 by Brian Aker
Refactoring table.h
133
  for (TableList *tl= tables; tl; tl= tl->next_leaf)
1 by brian
clean slate
134
  {
327.2.4 by Brian Aker
Refactoring table.h
135
    TableList *embedded;
1 by brian
clean slate
136
    for (embedded= tl ; embedded; embedded= embedded->embedding)
137
    {
138
      if (embedded->on_expr)
139
        break;
140
    }
141
    if (embedded)
142
    /* Don't replace expression on a table that is part of an outer join */
143
    {
144
      outer_tables|= tl->table->map;
145
146
      /*
147
        We can't optimise LEFT JOIN in cases where the WHERE condition
148
        restricts the table that is used, like in:
149
          SELECT MAX(t1.a) FROM t1 LEFT JOIN t2 join-condition
150
          WHERE t2.field IS NULL;
151
      */
152
      if (tl->table->map & where_tables)
153
        return 0;
154
    }
155
    else
156
      used_tables|= tl->table->map;
157
158
    /*
159
      If the storage manager of 'tl' gives exact row count as part of
160
      statistics (cheap), compute the total number of rows. If there are
161
      no outer table dependencies, this count may be used as the real count.
162
      Schema tables are filled after this function is invoked, so we can't
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
163
      get row count
1 by brian
clean slate
164
    */
165
    if (!(tl->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) ||
166
        tl->schema_table)
167
    {
168
      maybe_exact_count&= test(!tl->schema_table &&
169
                               (tl->table->file->ha_table_flags() &
170
                                HA_HAS_RECORDS));
51.1.35 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
171
      is_exact_count= false;
1 by brian
clean slate
172
      count= 1;                                 // ensure count != 0
173
    }
174
    else
175
    {
176
      error= tl->table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
177
      if(error)
178
      {
179
        tl->table->file->print_error(error, MYF(ME_FATALERROR));
180
        return error;
181
      }
182
      count*= tl->table->file->stats.records;
183
    }
184
  }
185
186
  /*
187
    Iterate through all items in the SELECT clause and replace
188
    COUNT(), MIN() and MAX() with constants (if possible).
189
  */
190
191
  while ((item= it++))
192
  {
193
    if (item->type() == Item::SUM_FUNC_ITEM)
194
    {
195
      Item_sum *item_sum= (((Item_sum*) item));
196
      switch (item_sum->sum_func()) {
197
      case Item_sum::COUNT_FUNC:
198
        /*
199
          If the expr in COUNT(expr) can never be null we can change this
200
          to the number of rows in the tables if this number is exact and
201
          there are no outer joins.
202
        */
203
        if (!conds && !((Item_sum_count*) item)->args[0]->maybe_null &&
204
            !outer_tables && maybe_exact_count)
205
        {
206
          if (!is_exact_count)
207
          {
163 by Brian Aker
Merge Monty's code.
208
            if ((count= get_exact_record_count(tables)) == UINT64_MAX)
1 by brian
clean slate
209
            {
210
              /* Error from handler in counting rows. Don't optimize count() */
211
              const_result= 0;
212
              continue;
213
            }
214
            is_exact_count= 1;                  // count is now exact
215
          }
779.3.16 by Monty Taylor
Some Sun warning fixes.
216
          ((Item_sum_count*) item)->make_const_count((int64_t) count);
1 by brian
clean slate
217
          recalc_const_item= 1;
218
        }
219
        else
220
          const_result= 0;
221
        break;
222
      case Item_sum::MIN_FUNC:
223
      {
224
        /*
225
          If MIN(expr) is the first part of a key or if all previous
226
          parts of the key is found in the COND, then we can use
227
          indexes to find the key.
228
        */
229
        Item *expr=item_sum->args[0];
230
        if (expr->real_item()->type() == Item::FIELD_ITEM)
231
        {
481 by Brian Aker
Remove all of uchar.
232
          unsigned char key_buff[MAX_KEY_LENGTH];
1089.1.14 by Brian Aker
Fix TABLE_REF structure
233
          table_reference_st ref;
482 by Brian Aker
Remove uint.
234
          uint32_t range_fl, prefix_len;
1 by brian
clean slate
235
236
          ref.key_buff= key_buff;
237
          Item_field *item_field= (Item_field*) (expr->real_item());
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
238
          Table *table= item_field->field->table;
1 by brian
clean slate
239
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
240
          /*
1 by brian
clean slate
241
            Look for a partial key that can be used for optimization.
242
            If we succeed, ref.key_length will contain the length of
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
243
            this key, while prefix_len will contain the length of
244
            the beginning of this key without field used in MIN().
1 by brian
clean slate
245
            Type of range for the key part for this field will be
246
            returned in range_fl.
247
          */
248
          if (table->file->inited || (outer_tables & table->map) ||
249
              !find_key_for_maxmin(0, &ref, item_field->field, conds,
250
                                   &range_fl, &prefix_len))
251
          {
252
            const_result= 0;
253
            break;
254
          }
895 by Brian Aker
Completion (?) of uint conversion.
255
          error= table->file->ha_index_init((uint32_t) ref.key, 1);
1 by brian
clean slate
256
257
          if (!ref.key_length)
258
            error= table->file->index_first(table->record[0]);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
259
          else
1 by brian
clean slate
260
          {
261
            /*
262
              Use index to replace MIN/MAX functions with their values
263
              according to the following rules:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
264
1 by brian
clean slate
265
              1) Insert the minimum non-null values where the WHERE clause still
266
                 matches, or
267
              2) a NULL value if there are only NULL values for key_part_k.
268
              3) Fail, producing a row of nulls
269
270
              Implementation: Read the smallest value using the search key. If
271
              the interval is open, read the next value after the search
272
              key. If read fails, and we're looking for a MIN() value for a
273
              nullable column, test if there is an exact match for the key.
274
            */
275
            if (!(range_fl & NEAR_MIN))
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
276
              /*
1 by brian
clean slate
277
                 Closed interval: Either The MIN argument is non-nullable, or
278
                 we have a >= predicate for the MIN argument.
279
              */
280
              error= table->file->index_read_map(table->record[0],
281
                                                 ref.key_buff,
282
                                                 make_prev_keypart_map(ref.key_parts),
283
                                                 HA_READ_KEY_OR_NEXT);
284
            else
285
            {
286
              /*
287
                Open interval: There are two cases:
288
                1) We have only MIN() and the argument column is nullable, or
289
                2) there is a > predicate on it, nullability is irrelevant.
290
                We need to scan the next bigger record first.
291
              */
292
              error= table->file->index_read_map(table->record[0],
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
293
                                                 ref.key_buff,
1 by brian
clean slate
294
                                                 make_prev_keypart_map(ref.key_parts),
295
                                                 HA_READ_AFTER_KEY);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
296
              /*
1 by brian
clean slate
297
                 If the found record is outside the group formed by the search
298
                 prefix, or there is no such record at all, check if all
299
                 records in that group have NULL in the MIN argument
300
                 column. If that is the case return that NULL.
301
302
                 Check if case 1 from above holds. If it does, we should read
303
                 the skipped tuple.
304
              */
305
              if (item_field->field->real_maybe_null() &&
306
                  ref.key_buff[prefix_len] == 1 &&
307
                  /*
308
                     Last keypart (i.e. the argument to MIN) is set to NULL by
309
                     find_key_for_maxmin only if all other keyparts are bound
310
                     to constants in a conjunction of equalities. Hence, we
311
                     can detect this by checking only if the last keypart is
312
                     NULL.
313
                  */
314
                  (error == HA_ERR_KEY_NOT_FOUND ||
315
                   key_cmp_if_same(table, ref.key_buff, ref.key, prefix_len)))
316
              {
51.1.35 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
317
                assert(item_field->field->real_maybe_null());
1 by brian
clean slate
318
                error= table->file->index_read_map(table->record[0],
319
                                                   ref.key_buff,
320
                                                   make_prev_keypart_map(ref.key_parts),
321
                                                   HA_READ_KEY_EXACT);
322
              }
323
            }
324
          }
325
          /* Verify that the read tuple indeed matches the search key */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
326
	  if (!error && reckey_in_range(0, &ref, item_field->field,
1 by brian
clean slate
327
			                conds, range_fl, prefix_len))
328
	    error= HA_ERR_KEY_NOT_FOUND;
329
          if (table->key_read)
330
          {
331
            table->key_read= 0;
332
            table->file->extra(HA_EXTRA_NO_KEYREAD);
333
          }
334
          table->file->ha_index_end();
335
          if (error)
336
	  {
337
	    if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
338
	      return HA_ERR_KEY_NOT_FOUND;	      // No rows matching WHERE
339
	    /* HA_ERR_LOCK_DEADLOCK or some other error */
340
 	    table->file->print_error(error, MYF(0));
341
            return(error);
342
	  }
343
          removed_tables|= table->map;
344
        }
345
        else if (!expr->const_item() || !is_exact_count)
346
        {
347
          /*
348
            The optimization is not applicable in both cases:
349
            (a) 'expr' is a non-constant expression. Then we can't
350
            replace 'expr' by a constant.
351
            (b) 'expr' is a costant. According to ANSI, MIN/MAX must return
352
            NULL if the query does not return any rows. Thus, if we are not
353
            able to determine if the query returns any rows, we can't apply
354
            the optimization and replace MIN/MAX with a constant.
355
          */
356
          const_result= 0;
357
          break;
358
        }
359
        if (!count)
360
        {
163 by Brian Aker
Merge Monty's code.
361
          /* If count == 0, then we know that is_exact_count == true. */
1 by brian
clean slate
362
          ((Item_sum_min*) item_sum)->clear(); /* Set to NULL. */
363
        }
364
        else
365
          ((Item_sum_min*) item_sum)->reset(); /* Set to the constant value. */
366
        ((Item_sum_min*) item_sum)->make_const();
367
        recalc_const_item= 1;
368
        break;
369
      }
370
      case Item_sum::MAX_FUNC:
371
      {
372
        /*
373
          If MAX(expr) is the first part of a key or if all previous
374
          parts of the key is found in the COND, then we can use
375
          indexes to find the key.
376
        */
377
        Item *expr=item_sum->args[0];
378
        if (expr->real_item()->type() == Item::FIELD_ITEM)
379
        {
481 by Brian Aker
Remove all of uchar.
380
          unsigned char key_buff[MAX_KEY_LENGTH];
1089.1.14 by Brian Aker
Fix TABLE_REF structure
381
          table_reference_st ref;
482 by Brian Aker
Remove uint.
382
          uint32_t range_fl, prefix_len;
1 by brian
clean slate
383
384
          ref.key_buff= key_buff;
385
          Item_field *item_field= (Item_field*) (expr->real_item());
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
386
          Table *table= item_field->field->table;
1 by brian
clean slate
387
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
388
          /*
1 by brian
clean slate
389
            Look for a partial key that can be used for optimization.
390
            If we succeed, ref.key_length will contain the length of
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
391
            this key, while prefix_len will contain the length of
1 by brian
clean slate
392
            the beginning of this key without field used in MAX().
393
            Type of range for the key part for this field will be
394
            returned in range_fl.
395
          */
396
          if (table->file->inited || (outer_tables & table->map) ||
397
	          !find_key_for_maxmin(1, &ref, item_field->field, conds,
398
				                   &range_fl, &prefix_len))
399
          {
400
            const_result= 0;
401
            break;
402
          }
895 by Brian Aker
Completion (?) of uint conversion.
403
          error= table->file->ha_index_init((uint32_t) ref.key, 1);
1 by brian
clean slate
404
405
          if (!ref.key_length)
406
            error= table->file->index_last(table->record[0]);
407
          else
408
	    error= table->file->index_read_map(table->record[0], key_buff,
409
                                               make_prev_keypart_map(ref.key_parts),
410
                                               range_fl & NEAR_MAX ?
411
                                               HA_READ_BEFORE_KEY :
412
                                               HA_READ_PREFIX_LAST_OR_PREV);
413
	  if (!error && reckey_in_range(1, &ref, item_field->field,
414
			                conds, range_fl, prefix_len))
415
	    error= HA_ERR_KEY_NOT_FOUND;
416
          if (table->key_read)
417
          {
418
            table->key_read=0;
419
            table->file->extra(HA_EXTRA_NO_KEYREAD);
420
          }
421
          table->file->ha_index_end();
422
          if (error)
423
          {
424
	    if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
425
	      return HA_ERR_KEY_NOT_FOUND;	     // No rows matching WHERE
426
	    /* HA_ERR_LOCK_DEADLOCK or some other error */
427
            table->file->print_error(error, MYF(ME_FATALERROR));
428
            return(error);
429
	  }
430
          removed_tables|= table->map;
431
        }
432
        else if (!expr->const_item() || !is_exact_count)
433
        {
434
          /*
435
            The optimization is not applicable in both cases:
436
            (a) 'expr' is a non-constant expression. Then we can't
437
            replace 'expr' by a constant.
438
            (b) 'expr' is a costant. According to ANSI, MIN/MAX must return
439
            NULL if the query does not return any rows. Thus, if we are not
440
            able to determine if the query returns any rows, we can't apply
441
            the optimization and replace MIN/MAX with a constant.
442
          */
443
          const_result= 0;
444
          break;
445
        }
446
        if (!count)
447
        {
163 by Brian Aker
Merge Monty's code.
448
          /* If count != 1, then we know that is_exact_count == true. */
1 by brian
clean slate
449
          ((Item_sum_max*) item_sum)->clear(); /* Set to NULL. */
450
        }
451
        else
452
          ((Item_sum_max*) item_sum)->reset(); /* Set to the constant value. */
453
        ((Item_sum_max*) item_sum)->make_const();
454
        recalc_const_item= 1;
455
        break;
456
      }
457
      default:
458
        const_result= 0;
459
        break;
460
      }
461
    }
462
    else if (const_result)
463
    {
464
      if (recalc_const_item)
465
        item->update_used_tables();
466
      if (!item->const_item())
467
        const_result= 0;
468
    }
469
  }
470
  /*
471
    If we have a where clause, we can only ignore searching in the
472
    tables if MIN/MAX optimisation replaced all used tables
473
    We do not use replaced values in case of:
474
    SELECT MIN(key) FROM table_1, empty_table
475
    removed_tables is != 0 if we have used MIN() or MAX().
476
  */
477
  if (removed_tables && used_tables != removed_tables)
478
    const_result= 0;                            // We didn't remove all tables
479
  return const_result;
480
}
481
482
483
/**
484
  Test if the predicate compares a field with constants.
485
486
  @param func_item        Predicate item
487
  @param[out] args        Here we store the field followed by constants
488
  @param[out] inv_order   Is set to 1 if the predicate is of the form
489
                          'const op field'
490
491
  @retval
492
    0        func_item is a simple predicate: a field is compared with
493
    constants
494
  @retval
495
    1        Otherwise
496
*/
497
498
bool simple_pred(Item_func *func_item, Item **args, bool *inv_order)
499
{
500
  Item *item;
501
  *inv_order= 0;
502
  switch (func_item->argument_count()) {
503
  case 0:
504
    /* MULT_EQUAL_FUNC */
505
    {
506
      Item_equal *item_equal= (Item_equal *) func_item;
507
      Item_equal_iterator it(*item_equal);
508
      args[0]= it++;
509
      if (it++)
510
        return 0;
511
      if (!(args[1]= item_equal->get_const()))
512
        return 0;
513
    }
514
    break;
515
  case 1:
516
    /* field IS NULL */
517
    item= func_item->arguments()[0];
518
    if (item->type() != Item::FIELD_ITEM)
519
      return 0;
520
    args[0]= item;
521
    break;
522
  case 2:
523
    /* 'field op const' or 'const op field' */
524
    item= func_item->arguments()[0];
525
    if (item->type() == Item::FIELD_ITEM)
526
    {
527
      args[0]= item;
528
      item= func_item->arguments()[1];
529
      if (!item->const_item())
530
        return 0;
531
      args[1]= item;
532
    }
533
    else if (item->const_item())
534
    {
535
      args[1]= item;
536
      item= func_item->arguments()[1];
537
      if (item->type() != Item::FIELD_ITEM)
538
        return 0;
539
      args[0]= item;
540
      *inv_order= 1;
541
    }
542
    else
543
      return 0;
544
    break;
545
  case 3:
546
    /* field BETWEEN const AND const */
547
    item= func_item->arguments()[0];
548
    if (item->type() == Item::FIELD_ITEM)
549
    {
550
      args[0]= item;
551
      for (int i= 1 ; i <= 2; i++)
552
      {
553
        item= func_item->arguments()[i];
554
        if (!item->const_item())
555
          return 0;
556
        args[i]= item;
557
      }
558
    }
559
    else
560
      return 0;
561
  }
562
  return 1;
563
}
564
565
566
/**
567
  Check whether a condition matches a key to get {MAX|MIN}(field):.
568
569
     For the index specified by the keyinfo parameter, index that
570
     contains field as its component (field_part), the function
571
     checks whether the condition cond is a conjunction and all its
572
     conjuncts referring to the columns of the same table as column
573
     field are one of the following forms:
574
     - f_i= const_i or const_i= f_i or f_i is null,
575
     where f_i is part of the index
576
     - field {<|<=|>=|>|=} const or const {<|<=|>=|>|=} field
577
     - field between const1 and const2
578
579
  @param[in]     max_fl         Set to 1 if we are optimising MAX()
580
  @param[in,out] ref            Reference to the structure we store the key
581
    value
582
  @param[in]     keyinfo        Reference to the key info
583
  @param[in]     field_part     Pointer to the key part for the field
584
  @param[in]     cond           WHERE condition
585
  @param[in,out] key_part_used  Map of matchings parts
586
  @param[in,out] range_fl       Says whether including key will be used
587
  @param[out]    prefix_len     Length of common key part for the range
588
    where MAX/MIN is searched for
589
590
  @retval
591
    0        Index can't be used.
592
  @retval
593
    1        We can use index to get MIN/MAX value
594
*/
595
1089.1.14 by Brian Aker
Fix TABLE_REF structure
596
static bool matching_cond(bool max_fl, table_reference_st *ref, KEY *keyinfo,
1 by brian
clean slate
597
                          KEY_PART_INFO *field_part, COND *cond,
482 by Brian Aker
Remove uint.
598
                          key_part_map *key_part_used, uint32_t *range_fl,
599
                          uint32_t *prefix_len)
1 by brian
clean slate
600
{
601
  if (!cond)
602
    return 1;
603
  Field *field= field_part->field;
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
604
605
  field->setWriteSet();
606
1 by brian
clean slate
607
  if (!(cond->used_tables() & field->table->map))
608
  {
609
    /* Condition doesn't restrict the used table */
610
    return 1;
611
  }
612
  if (cond->type() == Item::COND_ITEM)
613
  {
614
    if (((Item_cond*) cond)->functype() == Item_func::COND_OR_FUNC)
615
      return 0;
616
617
    /* AND */
618
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
619
    Item *item;
620
    while ((item= li++))
621
    {
622
      if (!matching_cond(max_fl, ref, keyinfo, field_part, item,
623
                         key_part_used, range_fl, prefix_len))
624
        return 0;
625
    }
626
    return 1;
627
  }
628
629
  if (cond->type() != Item::FUNC_ITEM)
630
    return 0;                                 // Not operator, can't optimize
631
632
  bool eq_type= 0;                            // =, <=> or IS NULL
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
633
  bool noeq_type= 0;                          // < or >
634
  bool less_fl= 0;                            // < or <=
1 by brian
clean slate
635
  bool is_null= 0;
636
  bool between= 0;
637
638
  switch (((Item_func*) cond)->functype()) {
639
  case Item_func::ISNULL_FUNC:
640
    is_null= 1;     /* fall through */
641
  case Item_func::EQ_FUNC:
642
  case Item_func::EQUAL_FUNC:
643
    eq_type= 1;
644
    break;
645
  case Item_func::LT_FUNC:
646
    noeq_type= 1;   /* fall through */
647
  case Item_func::LE_FUNC:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
648
    less_fl= 1;
1 by brian
clean slate
649
    break;
650
  case Item_func::GT_FUNC:
651
    noeq_type= 1;   /* fall through */
652
  case Item_func::GE_FUNC:
653
    break;
654
  case Item_func::BETWEEN:
655
    between= 1;
656
    break;
657
  case Item_func::MULT_EQUAL_FUNC:
658
    eq_type= 1;
659
    break;
660
  default:
661
    return 0;                                        // Can't optimize function
662
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
663
1 by brian
clean slate
664
  Item *args[3];
665
  bool inv;
666
667
  /* Test if this is a comparison of a field and constant */
668
  if (!simple_pred((Item_func*) cond, args, &inv))
669
    return 0;
670
671
  if (inv && !eq_type)
672
    less_fl= 1-less_fl;                         // Convert '<' -> '>' (etc)
673
674
  /* Check if field is part of the tested partial key */
481 by Brian Aker
Remove all of uchar.
675
  unsigned char *key_ptr= ref->key_buff;
1 by brian
clean slate
676
  KEY_PART_INFO *part;
677
  for (part= keyinfo->key_part; ; key_ptr+= part++->store_length)
678
679
  {
680
    if (part > field_part)
681
      return 0;                     // Field is beyond the tested parts
682
    if (part->field->eq(((Item_field*) args[0])->field))
683
      break;                        // Found a part of the key for the field
684
  }
685
686
  bool is_field_part= part == field_part;
687
  if (!(is_field_part || eq_type))
688
    return 0;
689
690
  key_part_map org_key_part_used= *key_part_used;
691
  if (eq_type || between || max_fl == less_fl)
692
  {
482 by Brian Aker
Remove uint.
693
    uint32_t length= (key_ptr-ref->key_buff)+part->store_length;
1 by brian
clean slate
694
    if (ref->key_length < length)
695
    {
696
    /* Ultimately ref->key_length will contain the length of the search key */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
697
      ref->key_length= length;
1 by brian
clean slate
698
      ref->key_parts= (part - keyinfo->key_part) + 1;
699
    }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
700
    if (!*prefix_len && part+1 == field_part)
1 by brian
clean slate
701
      *prefix_len= length;
702
    if (is_field_part && eq_type)
703
      *prefix_len= ref->key_length;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
704
1 by brian
clean slate
705
    *key_part_used|= (key_part_map) 1 << (part - keyinfo->key_part);
706
  }
707
708
  if (org_key_part_used != *key_part_used ||
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
709
      (is_field_part &&
1 by brian
clean slate
710
       (between || eq_type || max_fl == less_fl) && !cond->val_int()))
711
  {
712
    /*
713
      It's the first predicate for this part or a predicate of the
714
      following form  that moves upper/lower bounds for max/min values:
715
      - field BETWEEN const AND const
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
716
      - field = const
1 by brian
clean slate
717
      - field {<|<=} const, when searching for MAX
718
      - field {>|>=} const, when searching for MIN
719
    */
720
721
    if (is_null)
722
    {
723
      part->field->set_null();
481 by Brian Aker
Remove all of uchar.
724
      *key_ptr= (unsigned char) 1;
1 by brian
clean slate
725
    }
726
    else
727
    {
728
      store_val_in_field(part->field, args[between && max_fl ? 2 : 1],
729
                         CHECK_FIELD_IGNORE);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
730
      if (part->null_bit)
481 by Brian Aker
Remove all of uchar.
731
        *key_ptr++= (unsigned char) test(part->field->is_null());
1055.2.5 by Jay Pipes
Removal of dead Field::image_type and st_key_part::image_type member variables. Legacy from geometry MyISAM types...
732
      part->field->get_key_image(key_ptr, part->length);
1 by brian
clean slate
733
    }
734
    if (is_field_part)
735
    {
736
      if (between || eq_type)
737
        *range_fl&= ~(NO_MAX_RANGE | NO_MIN_RANGE);
738
      else
739
      {
740
        *range_fl&= ~(max_fl ? NO_MAX_RANGE : NO_MIN_RANGE);
741
        if (noeq_type)
742
          *range_fl|=  (max_fl ? NEAR_MAX : NEAR_MIN);
743
        else
744
          *range_fl&= ~(max_fl ? NEAR_MAX : NEAR_MIN);
745
      }
746
    }
747
  }
748
  else if (eq_type)
749
  {
750
    if ((!is_null && !cond->val_int()) ||
751
        (is_null && !test(part->field->is_null())))
752
     return 0;                       // Impossible test
753
  }
754
  else if (is_field_part)
755
    *range_fl&= ~(max_fl ? NO_MIN_RANGE : NO_MAX_RANGE);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
756
  return 1;
1 by brian
clean slate
757
}
758
759
760
/**
761
  Check whether we can get value for {max|min}(field) by using a key.
762
763
     If where-condition is not a conjunction of 0 or more conjuct the
764
     function returns false, otherwise it checks whether there is an
765
     index including field as its k-th component/part such that:
766
767
     -# for each previous component f_i there is one and only one conjunct
768
        of the form: f_i= const_i or const_i= f_i or f_i is null
769
     -# references to field occur only in conjucts of the form:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
770
        field {<|<=|>=|>|=} const or const {<|<=|>=|>|=} field or
1 by brian
clean slate
771
        field BETWEEN const1 AND const2
772
     -# all references to the columns from the same table as column field
773
        occur only in conjucts mentioned above.
774
     -# each of k first components the index is not partial, i.e. is not
775
        defined on a fixed length proper prefix of the field.
776
777
     If such an index exists the function through the ref parameter
778
     returns the key value to find max/min for the field using the index,
779
     the length of first (k-1) components of the key and flags saying
780
     how to apply the key for the search max/min value.
781
     (if we have a condition field = const, prefix_len contains the length
782
     of the whole search key)
783
784
  @param[in]     max_fl      0 for MIN(field) / 1 for MAX(field)
785
  @param[in,out] ref         Reference to the structure we store the key value
786
  @param[in]     field       Field used inside MIN() / MAX()
787
  @param[in]     cond        WHERE condition
788
  @param[out]    range_fl    Bit flags for how to search if key is ok
789
  @param[out]    prefix_len  Length of prefix for the search range
790
791
  @note
792
    This function may set table->key_read to 1, which must be reset after
793
    index is used! (This can only happen when function returns 1)
794
795
  @retval
796
    0   Index can not be used to optimize MIN(field)/MAX(field)
797
  @retval
798
    1   Can use key to optimize MIN()/MAX().
799
    In this case ref, range_fl and prefix_len are updated
800
*/
801
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
802
1089.1.14 by Brian Aker
Fix TABLE_REF structure
803
static bool find_key_for_maxmin(bool max_fl, table_reference_st *ref,
1 by brian
clean slate
804
                                Field* field, COND *cond,
482 by Brian Aker
Remove uint.
805
                                uint32_t *range_fl, uint32_t *prefix_len)
1 by brian
clean slate
806
{
807
  if (!(field->flags & PART_KEY_FLAG))
808
    return 0;                                        // Not key field
809
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
810
  Table *table= field->table;
482 by Brian Aker
Remove uint.
811
  uint32_t idx= 0;
1 by brian
clean slate
812
813
  KEY *keyinfo,*keyinfo_end;
814
  for (keyinfo= table->key_info, keyinfo_end= keyinfo+table->s->keys ;
815
       keyinfo != keyinfo_end;
816
       keyinfo++,idx++)
817
  {
818
    KEY_PART_INFO *part,*part_end;
819
    key_part_map key_part_to_use= 0;
820
    /*
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
821
      Perform a check if index is not disabled by ALTER Table
1 by brian
clean slate
822
      or IGNORE INDEX.
823
    */
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
824
    if (!table->keys_in_use_for_query.test(idx))
1 by brian
clean slate
825
      continue;
482 by Brian Aker
Remove uint.
826
    uint32_t jdx= 0;
1 by brian
clean slate
827
    *prefix_len= 0;
828
    for (part= keyinfo->key_part, part_end= part+keyinfo->key_parts ;
829
         part != part_end ;
830
         part++, jdx++, key_part_to_use= (key_part_to_use << 1) | 1)
831
    {
832
      if (!(table->file->index_flags(idx, jdx, 0) & HA_READ_ORDER))
833
        return 0;
834
835
      /* Check whether the index component is partial */
836
      Field *part_field= table->field[part->fieldnr-1];
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
837
      part_field->setWriteSet();
838
1 by brian
clean slate
839
      if ((part_field->flags & BLOB_FLAG) ||
840
          part->length < part_field->key_length())
841
        break;
842
843
      if (field->eq(part->field))
844
      {
845
        ref->key= idx;
846
        ref->key_length= 0;
847
        ref->key_parts= 0;
848
        key_part_map key_part_used= 0;
849
        *range_fl= NO_MIN_RANGE | NO_MAX_RANGE;
850
        if (matching_cond(max_fl, ref, keyinfo, part, cond,
851
                          &key_part_used, range_fl, prefix_len) &&
852
            !(key_part_to_use & ~key_part_used))
853
        {
854
          if (!max_fl && key_part_used == key_part_to_use && part->null_bit)
855
          {
856
            /*
857
              The query is on this form:
858
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
859
              SELECT MIN(key_part_k)
860
              FROM t1
1 by brian
clean slate
861
              WHERE key_part_1 = const and ... and key_part_k-1 = const
862
863
              If key_part_k is nullable, we want to find the first matching row
864
              where key_part_k is not null. The key buffer is now {const, ...,
865
              NULL}. This will be passed to the handler along with a flag
866
              indicating open interval. If a tuple is read that does not match
867
              these search criteria, an attempt will be made to read an exact
868
              match for the key buffer.
869
            */
870
            /* Set the first byte of key_part_k to 1, that means NULL */
871
            ref->key_buff[ref->key_length]= 1;
872
            ref->key_length+= part->store_length;
873
            ref->key_parts++;
51.1.35 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
874
            assert(ref->key_parts == jdx+1);
1 by brian
clean slate
875
            *range_fl&= ~NO_MIN_RANGE;
876
            *range_fl|= NEAR_MIN; // Open interval
877
          }
878
          /*
879
            The following test is false when the key in the key tree is
880
            converted (for example to upper case)
881
          */
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
882
          if (field->part_of_key.test(idx))
1 by brian
clean slate
883
          {
884
            table->key_read= 1;
885
            table->file->extra(HA_EXTRA_KEYREAD);
886
          }
887
          return 1;
888
        }
889
      }
890
    }
891
  }
892
  return 0;
893
}
894
895
896
/**
897
  Check whether found key is in range specified by conditions.
898
899
  @param[in] max_fl         0 for MIN(field) / 1 for MAX(field)
900
  @param[in] ref            Reference to the key value and info
901
  @param[in] field          Field used the MIN/MAX expression
902
  @param[in] cond           WHERE condition
903
  @param[in] range_fl       Says whether there is a condition to to be checked
904
  @param[in] prefix_len     Length of the constant part of the key
905
906
  @retval
907
    0        ok
908
  @retval
909
    1        WHERE was not true for the found row
910
*/
911
1089.1.14 by Brian Aker
Fix TABLE_REF structure
912
static int reckey_in_range(bool max_fl, table_reference_st *ref, Field* field,
482 by Brian Aker
Remove uint.
913
                            COND *cond, uint32_t range_fl, uint32_t prefix_len)
1 by brian
clean slate
914
{
915
  if (key_cmp_if_same(field->table, ref->key_buff, ref->key, prefix_len))
916
    return 1;
917
  if (!cond || (range_fl & (max_fl ? NO_MIN_RANGE : NO_MAX_RANGE)))
918
    return 0;
919
  return maxmin_in_range(max_fl, field, cond);
920
}
921
922
923
/**
924
  Check whether {MAX|MIN}(field) is in range specified by conditions.
925
926
  @param[in] max_fl          0 for MIN(field) / 1 for MAX(field)
927
  @param[in] field           Field used the MIN/MAX expression
928
  @param[in] cond            WHERE condition
929
930
  @retval
931
    0        ok
932
  @retval
933
    1        WHERE was not true for the found row
934
*/
935
936
static int maxmin_in_range(bool max_fl, Field* field, COND *cond)
937
{
938
  /* If AND/OR condition */
939
  if (cond->type() == Item::COND_ITEM)
940
  {
941
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
942
    Item *item;
943
    while ((item= li++))
944
    {
945
      if (maxmin_in_range(max_fl, field, item))
946
        return 1;
947
    }
948
    return 0;
949
  }
950
951
  if (cond->used_tables() != field->table->map)
952
    return 0;
953
  bool less_fl= 0;
954
  switch (((Item_func*) cond)->functype()) {
955
  case Item_func::BETWEEN:
956
    return cond->val_int() == 0;                // Return 1 if WHERE is false
957
  case Item_func::LT_FUNC:
958
  case Item_func::LE_FUNC:
959
    less_fl= 1;
960
  case Item_func::GT_FUNC:
961
  case Item_func::GE_FUNC:
962
  {
963
    Item *item= ((Item_func*) cond)->arguments()[1];
964
    /* In case of 'const op item' we have to swap the operator */
965
    if (!item->const_item())
966
      less_fl= 1-less_fl;
967
    /*
968
      We only have to check the expression if we are using an expression like
969
      SELECT MAX(b) FROM t1 WHERE a=const AND b>const
970
      not for
971
      SELECT MAX(b) FROM t1 WHERE a=const AND b<const
972
    */
973
    if (max_fl != less_fl)
974
      return cond->val_int() == 0;                // Return 1 if WHERE is false
975
    return 0;
976
  }
977
  case Item_func::EQ_FUNC:
978
  case Item_func::EQUAL_FUNC:
979
    break;
980
  default:                                        // Keep compiler happy
51.1.35 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
981
    assert(1);                               // Impossible
1 by brian
clean slate
982
    break;
983
  }
984
  return 0;
985
}
986