Skip to content

Commit 0b4c3ad

Browse files
author
Alexander Barkov
committed
Moving Used_tables_and_const_chache from Item_func to Item_func_or_sum
and thus reusing Used_tables_and_const_cache for Item_sum instead of declaring the same members inside Item_sum.
1 parent affff1a commit 0b4c3ad

File tree

5 files changed

+32
-44
lines changed

5 files changed

+32
-44
lines changed

sql/item.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3663,7 +3663,9 @@ class Used_tables_and_const_cache
36633663
An abstract class representing common features of
36643664
regular functions and aggregate functions.
36653665
*/
3666-
class Item_func_or_sum: public Item_result_field, public Item_args
3666+
class Item_func_or_sum: public Item_result_field,
3667+
public Item_args,
3668+
public Used_tables_and_const_cache
36673669
{
36683670
bool agg_item_collations(DTCollation &c, const char *name,
36693671
Item **items, uint nitems,
@@ -3794,7 +3796,8 @@ class Item_func_or_sum: public Item_result_field, public Item_args
37943796
Item_func_or_sum(THD *thd, Item *a, Item *b, Item *c, Item *d, Item *e):
37953797
Item_result_field(thd), Item_args(a, b, c, d, e) { }
37963798
Item_func_or_sum(THD *thd, Item_func_or_sum *item):
3797-
Item_result_field(thd, item), Item_args(thd, item) { }
3799+
Item_result_field(thd, item), Item_args(thd, item),
3800+
Used_tables_and_const_cache(item) { }
37983801
Item_func_or_sum(THD *thd, List<Item> &list):
37993802
Item_result_field(thd), Item_args(thd, list) { }
38003803
bool walk(Item_processor processor, bool walk_subquery, uchar *arg)
@@ -3821,6 +3824,8 @@ class Item_func_or_sum: public Item_result_field, public Item_args
38213824
*/
38223825
virtual const char *func_name() const= 0;
38233826
virtual void fix_length_and_dec()= 0;
3827+
bool const_item() const { return const_item_cache; }
3828+
table_map used_tables() const { return used_tables_cache; }
38243829
};
38253830

38263831

sql/item_func.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -441,12 +441,6 @@ void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array,
441441
}
442442

443443

444-
table_map Item_func::used_tables() const
445-
{
446-
return used_tables_cache;
447-
}
448-
449-
450444
table_map Item_func::not_null_tables() const
451445
{
452446
return not_null_tables_cache;

sql/item_func.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extern "C" /* Bug in BSDI include file */
3131
#endif
3232

3333

34-
class Item_func :public Item_func_or_sum, public Used_tables_and_const_cache
34+
class Item_func :public Item_func_or_sum
3535
{
3636
void sync_with_sum_func_and_with_field(List<Item> &list);
3737
protected:
@@ -106,8 +106,8 @@ class Item_func :public Item_func_or_sum, public Used_tables_and_const_cache
106106
set_arguments(thd, list);
107107
}
108108
// Constructor used for Item_cond_and/or (see Item comment)
109-
Item_func(THD *thd, Item_func *item)
110-
:Item_func_or_sum(thd, item), Used_tables_and_const_cache(item),
109+
Item_func(THD *thd, Item_func *item):
110+
Item_func_or_sum(thd, item),
111111
allowed_arg_cols(item->allowed_arg_cols),
112112
not_null_tables_cache(item->not_null_tables_cache)
113113
{
@@ -120,7 +120,6 @@ class Item_func :public Item_func_or_sum, public Used_tables_and_const_cache
120120
}
121121
void fix_after_pullout(st_select_lex *new_parent, Item **ref);
122122
void quick_fix_field();
123-
table_map used_tables() const;
124123
table_map not_null_tables() const;
125124
void update_used_tables()
126125
{
@@ -137,7 +136,6 @@ class Item_func :public Item_func_or_sum, public Used_tables_and_const_cache
137136
}
138137
bool eq(const Item *item, bool binary_cmp) const;
139138
virtual Item *key_item() const { return args[0]; }
140-
virtual bool const_item() const { return const_item_cache; }
141139
void set_arguments(THD *thd, List<Item> &list)
142140
{
143141
allowed_arg_cols= 1;

sql/item_sum.cc

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,7 @@ bool Item_sum::collect_outer_ref_processor(uchar *param)
401401
}
402402

403403

404-
Item_sum::Item_sum(THD *thd, List<Item> &list): Item_func_or_sum(thd, list),
405-
forced_const(FALSE)
404+
Item_sum::Item_sum(THD *thd, List<Item> &list): Item_func_or_sum(thd, list)
406405
{
407406
if (!(orig_args= (Item **) thd->alloc(sizeof(Item *) * arg_count)))
408407
{
@@ -423,9 +422,7 @@ Item_sum::Item_sum(THD *thd, Item_sum *item):
423422
aggr_sel(item->aggr_sel),
424423
nest_level(item->nest_level), aggr_level(item->aggr_level),
425424
quick_group(item->quick_group),
426-
orig_args(NULL),
427-
used_tables_cache(item->used_tables_cache),
428-
forced_const(item->forced_const)
425+
orig_args(NULL)
429426
{
430427
if (arg_count <= 2)
431428
{
@@ -449,6 +446,7 @@ void Item_sum::mark_as_sum_func()
449446
SELECT_LEX *cur_select= current_thd->lex->current_select;
450447
cur_select->n_sum_items++;
451448
cur_select->with_sum_func= 1;
449+
const_item_cache= false;
452450
with_sum_func= 1;
453451
with_field= 0;
454452
}
@@ -538,7 +536,7 @@ Field *Item_sum::create_tmp_field(bool group, TABLE *table,
538536

539537
void Item_sum::update_used_tables ()
540538
{
541-
if (!forced_const)
539+
if (!Item_sum::const_item())
542540
{
543541
used_tables_cache= 0;
544542
for (uint i=0 ; i < arg_count ; i++)
@@ -610,7 +608,7 @@ void Item_sum::cleanup()
610608
aggr= NULL;
611609
}
612610
Item_result_field::cleanup();
613-
forced_const= FALSE;
611+
const_item_cache= false;
614612
}
615613

616614
Item *Item_sum::result_item(THD *thd, Field *field)
@@ -2073,7 +2071,6 @@ void Item_sum_hybrid::cleanup()
20732071
{
20742072
DBUG_ENTER("Item_sum_hybrid::cleanup");
20752073
Item_sum::cleanup();
2076-
forced_const= FALSE;
20772074
if (cmp)
20782075
delete cmp;
20792076
cmp= 0;

sql/item_sum.h

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,13 @@ class st_select_lex;
302302
We assume that the nesting level of subquries does not exceed 127.
303303
TODO: to catch queries where the limit is exceeded to make the
304304
code clean here.
305-
305+
306+
@note
307+
The implementation takes into account the used strategy:
308+
- Items resolved at optimization phase return 0 from Item_sum::used_tables().
309+
- Items that depend on the number of join output records, but not columns of
310+
any particular table (like COUNT(*)), returm 0 from Item_sum::used_tables(),
311+
but still return false from Item_sum::const_item().
306312
*/
307313

308314
class Item_sum :public Item_func_or_sum
@@ -368,32 +374,25 @@ class Item_sum :public Item_func_or_sum
368374
the current argument list can be altered by usage of temporary tables.
369375
*/
370376
Item **orig_args, *tmp_orig_args[2];
371-
table_map used_tables_cache;
372377

373-
/*
374-
TRUE <=> We've managed to calculate the value of this Item in
375-
opt_sum_query(), hence it can be considered constant at all subsequent
376-
steps.
377-
*/
378-
bool forced_const;
379378
static ulonglong ram_limitation(THD *thd);
380379

381380
public:
382381

383382
void mark_as_sum_func();
384-
Item_sum(THD *thd): Item_func_or_sum(thd), quick_group(1), forced_const(FALSE)
383+
Item_sum(THD *thd): Item_func_or_sum(thd), quick_group(1)
385384
{
386385
mark_as_sum_func();
387386
init_aggregator();
388387
}
389388
Item_sum(THD *thd, Item *a): Item_func_or_sum(thd, a), quick_group(1),
390-
orig_args(tmp_orig_args), forced_const(FALSE)
389+
orig_args(tmp_orig_args)
391390
{
392391
mark_as_sum_func();
393392
init_aggregator();
394393
}
395394
Item_sum(THD *thd, Item *a, Item *b): Item_func_or_sum(thd, a, b),
396-
quick_group(1), orig_args(tmp_orig_args), forced_const(FALSE)
395+
quick_group(1), orig_args(tmp_orig_args)
397396
{
398397
mark_as_sum_func();
399398
init_aggregator();
@@ -433,16 +432,6 @@ class Item_sum :public Item_func_or_sum
433432
virtual void fix_length_and_dec() { maybe_null=1; null_value=1; }
434433
virtual Item *result_item(THD *thd, Field *field);
435434

436-
/*
437-
Return bitmap of tables that are needed to evaluate the item.
438-
439-
The implementation takes into account the used strategy: items resolved
440-
at optimization phase will report 0.
441-
Items that depend on the number of join output records, but not columns
442-
of any particular table (like COUNT(*)) will report 0 from used_tables(),
443-
but will still return false from const_item().
444-
*/
445-
table_map used_tables() const { return used_tables_cache; }
446435
void update_used_tables ();
447436
COND *build_equal_items(THD *thd, COND_EQUAL *inherited,
448437
bool link_item_fields,
@@ -458,12 +447,17 @@ class Item_sum :public Item_func_or_sum
458447
cond_equal_ref);
459448
}
460449
bool is_null() { return null_value; }
450+
/**
451+
make_const()
452+
Called if we've managed to calculate the value of this Item in
453+
opt_sum_query(), hence it can be considered constant at all subsequent
454+
steps.
455+
*/
461456
void make_const ()
462457
{
463458
used_tables_cache= 0;
464-
forced_const= TRUE;
459+
const_item_cache= true;
465460
}
466-
virtual bool const_item() const { return forced_const; }
467461
virtual bool const_during_execution() const { return false; }
468462
virtual void print(String *str, enum_query_type query_type);
469463
void fix_num_length_and_dec();

0 commit comments

Comments
 (0)