Skip to content

Commit 7ca8b4b

Browse files
committed
move internal API out from group_by_handler
into a Pushdown_query object
1 parent 9ca3d9e commit 7ca8b4b

File tree

6 files changed

+65
-67
lines changed

6 files changed

+65
-67
lines changed

sql/group_by_handler.cc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@
3535
-1 if error should be sent
3636
*/
3737

38-
int group_by_handler::execute(JOIN *join)
38+
int Pushdown_query::execute(JOIN *join)
3939
{
4040
int err;
4141
ha_rows max_limit;
4242
ha_rows *reset_limit= 0;
4343
Item **reset_item= 0;
44-
DBUG_ENTER("group_by_handler::execute");
44+
THD *thd= handler->thd;
45+
TABLE *table= handler->table;
46+
DBUG_ENTER("Pushdown_query::execute");
4547

46-
if ((err= init_scan()))
48+
if ((err= handler->init_scan()))
4749
goto error;
4850

4951
if (store_data_in_temp_table)
@@ -58,17 +60,17 @@ int group_by_handler::execute(JOIN *join)
5860
reset_item= &join->unit->fake_select_lex->select_limit;
5961
}
6062

61-
while (!(err= next_row()))
63+
while (!(err= handler->next_row()))
6264
{
6365
if (thd->check_killed())
6466
{
6567
thd->send_kill_message();
66-
(void) end_scan();
68+
handler->end_scan();
6769
DBUG_RETURN(-1);
6870
}
6971

7072
/* Check if we can accept the row */
71-
if (!having || having->val_bool())
73+
if (!handler->having || handler->having->val_bool())
7274
{
7375
if (store_data_in_temp_table)
7476
{
@@ -97,7 +99,7 @@ int group_by_handler::execute(JOIN *join)
9799
/* result < 0 if row was not accepted and should not be counted */
98100
if ((error= join->result->send_data(*join->fields)))
99101
{
100-
(void) end_scan();
102+
handler->end_scan();
101103
DBUG_RETURN(error < 0 ? 0 : -1);
102104
}
103105
}
@@ -119,17 +121,17 @@ int group_by_handler::execute(JOIN *join)
119121
if (err != 0 && err != HA_ERR_END_OF_FILE)
120122
goto error;
121123

122-
if ((err= end_scan()))
124+
if ((err= handler->end_scan()))
123125
goto error_2;
124126
if (!store_data_in_temp_table && join->result->send_eof())
125127
DBUG_RETURN(1); // Don't send error to client
126128

127129
DBUG_RETURN(0);
128130

129131
error:
130-
(void) end_scan();
132+
handler->end_scan();
131133
error_2:
132-
print_error(err, MYF(0));
134+
handler->print_error(err, MYF(0));
133135
DBUG_RETURN(-1); // Error not sent to client
134136
}
135137

sql/group_by_handler.h

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
This file implements the group_by_handler interface. This interface
1919
can be used by storage handlers that can intercept summary or GROUP
2020
BY queries from MariaDB and itself return the result to the user or
21-
upper level.
21+
upper level. It is part of the Storage Engine API
2222
2323
Both main and sub queries are supported. Here are some examples of what the
2424
storage engine could intersept:
@@ -30,35 +30,26 @@
3030
SELECT a, (select sum(*) from t2 where t1.a=t2.a) from t2;
3131
*/
3232

33-
class JOIN;
34-
3533
class group_by_handler
3634
{
3735
public:
38-
/* Arguments for group_by_handler, for usage later */
3936
THD *thd;
40-
SELECT_LEX *select_lex;
4137
List<Item> *fields;
4238
TABLE_LIST *table_list;
4339
ORDER *group_by, *order_by;
4440
Item *where, *having;
45-
handlerton *ht; /* storage engine of this handler */
41+
handlerton *ht;
4642

4743
/* Temporary table where all results should be stored in record[0] */
4844
TABLE *table;
4945

50-
bool store_data_in_temp_table; /* Set by mariadb */
51-
52-
group_by_handler(THD *thd_arg, SELECT_LEX *select_lex_arg,
53-
List<Item> *fields_arg,
46+
group_by_handler(THD *thd_arg, List<Item> *fields_arg,
5447
TABLE_LIST *table_list_arg, ORDER *group_by_arg,
55-
ORDER *order_by_arg, Item *where_arg,
56-
Item *having_arg, handlerton *ht_arg)
57-
: thd(thd_arg), select_lex(select_lex_arg), fields(fields_arg),
58-
table_list(table_list_arg), group_by(group_by_arg),
59-
order_by(order_by_arg), where(where_arg), having(having_arg),
60-
ht(ht_arg), table(0), store_data_in_temp_table(0)
61-
{}
48+
ORDER *order_by_arg, Item *where_arg, Item *having_arg,
49+
handlerton *ht_arg)
50+
: thd(thd_arg), fields(fields_arg), table_list(table_list_arg),
51+
group_by(group_by_arg), order_by(order_by_arg), where(where_arg),
52+
having(having_arg), ht(ht_arg), table(0) {}
6253
virtual ~group_by_handler() {}
6354

6455
/*
@@ -118,9 +109,7 @@ class group_by_handler
118109
/* End scanning */
119110
virtual int end_scan()=0;
120111

121-
/* Function that calls the above scan functions */
122-
int execute(JOIN *join);
123-
124112
/* Report errors */
125113
virtual void print_error(int error, myf errflag);
126114
};
115+

sql/handler.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,8 +1267,7 @@ struct handlerton
12671267
The server guaranteeds that all tables in the list belong to this
12681268
storage engine.
12691269
*/
1270-
group_by_handler *(*create_group_by)(THD *thd, SELECT_LEX *select_lex,
1271-
List<Item> *fields,
1270+
group_by_handler *(*create_group_by)(THD *thd, List<Item> *fields,
12721271
TABLE_LIST *table_list, ORDER *group_by,
12731272
ORDER *order_by, Item *where,
12741273
Item *having);

sql/sql_select.cc

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,14 +1918,12 @@ JOIN::optimize_inner()
19181918
(one_storage_engine && one_storage_engine->create_group_by))
19191919
{
19201920
/* Check if the storage engine can intercept the query */
1921-
if ((storage_handler_for_group_by=
1922-
(one_storage_engine->create_group_by)(thd, select_lex,
1923-
&all_fields,
1924-
tables_list,
1925-
group_list, order,
1926-
conds, having)))
1927-
{
1928-
uint handler_flags= storage_handler_for_group_by->flags();
1921+
group_by_handler *gbh= one_storage_engine->create_group_by(thd, &all_fields,
1922+
tables_list, group_list, order, conds, having);
1923+
if (gbh)
1924+
{
1925+
pushdown_query= new (thd->mem_root) Pushdown_query(select_lex, gbh);
1926+
uint handler_flags= gbh->flags();
19291927
int err;
19301928

19311929
/*
@@ -1962,13 +1960,13 @@ JOIN::optimize_inner()
19621960
DBUG_RETURN(1);
19631961

19641962
/* Give storage engine access to temporary table */
1965-
if ((err= storage_handler_for_group_by->init(exec_tmp_table1,
1963+
if ((err= gbh->init(exec_tmp_table1,
19661964
having, order)))
19671965
{
1968-
storage_handler_for_group_by->print_error(err, MYF(0));
1966+
gbh->print_error(err, MYF(0));
19691967
DBUG_RETURN(1);
19701968
}
1971-
storage_handler_for_group_by->store_data_in_temp_table= need_tmp;
1969+
pushdown_query->store_data_in_temp_table= need_tmp;
19721970
/*
19731971
If no ORDER BY clause was specified explicitly, we should sort things
19741972
according to the group_by
@@ -2082,7 +2080,7 @@ int JOIN::init_execution()
20822080
thd->lex->set_limit_rows_examined();
20832081

20842082
/* Create a tmp table if distinct or if the sort is too complicated */
2085-
if (need_tmp && ! storage_handler_for_group_by)
2083+
if (need_tmp && !pushdown_query)
20862084
{
20872085
DBUG_PRINT("info",("Creating tmp table"));
20882086
THD_STAGE_INFO(thd, stage_copying_to_tmp_table);
@@ -12053,8 +12051,8 @@ void JOIN::cleanup(bool full)
1205312051
}
1205412052
tmp_table_param.cleanup();
1205512053

12056-
delete storage_handler_for_group_by;
12057-
storage_handler_for_group_by= 0;
12054+
delete pushdown_query;
12055+
pushdown_query= 0;
1205812056

1205912057
if (!join_tab)
1206012058
{
@@ -17854,15 +17852,14 @@ do_select(JOIN *join,List<Item> *fields,TABLE *table,Procedure *procedure)
1785417852
join->fields= fields;
1785517853
join->do_select_call_count++;
1785617854

17857-
if (join->storage_handler_for_group_by &&
17858-
join->do_select_call_count == 1)
17855+
if (join->pushdown_query && join->do_select_call_count == 1)
1785917856
{
1786017857
/* Select fields are in the temporary table */
1786117858
join->fields= &join->tmp_fields_list1;
1786217859
/* Setup HAVING to work with fields in temporary table */
1786317860
join->set_items_ref_array(join->items1);
1786417861
/* The storage engine will take care of the group by query result */
17865-
int res= join->storage_handler_for_group_by->execute(join);
17862+
int res= join->pushdown_query->execute(join);
1786617863
DBUG_RETURN(res);
1786717864
}
1786817865

@@ -24330,7 +24327,7 @@ int JOIN::save_explain_data_intern(Explain_query *output, bool need_tmp_table,
2433024327
explain->connection_type= Explain_node::EXPLAIN_NODE_DERIVED;
2433124328
output->add_node(explain);
2433224329
}
24333-
else if (storage_handler_for_group_by)
24330+
else if (pushdown_query)
2433424331
{
2433524332
explain= new (output->mem_root) Explain_select(output->mem_root,
2433624333
thd->lex->analyze_stmt);

sql/sql_select.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@ class JOIN_TAB_RANGE: public Sql_alloc
884884
JOIN_TAB *end;
885885
};
886886

887+
class Pushdown_query;
887888

888889
class JOIN :public Sql_alloc
889890
{
@@ -1088,7 +1089,7 @@ class JOIN :public Sql_alloc
10881089

10891090
/* points to a storage engine if all tables comes from the storage engine */
10901091
handlerton *one_storage_engine;
1091-
group_by_handler *storage_handler_for_group_by;
1092+
Pushdown_query *pushdown_query;
10921093
JOIN_TAB *original_join_tab;
10931094
uint original_table_count;
10941095

@@ -1391,7 +1392,7 @@ class JOIN :public Sql_alloc
13911392
no_rows_in_result_called= 0;
13921393
positions= best_positions= 0;
13931394
one_storage_engine= 0;
1394-
storage_handler_for_group_by= 0;
1395+
pushdown_query= 0;
13951396
original_join_tab= 0;
13961397
do_select_call_count= 0;
13971398

@@ -1964,5 +1965,21 @@ ulong check_selectivity(THD *thd,
19641965
TABLE *table,
19651966
List<COND_STATISTIC> *conds);
19661967

1968+
class Pushdown_query: public Sql_alloc
1969+
{
1970+
public:
1971+
SELECT_LEX *select_lex;
1972+
bool store_data_in_temp_table;
1973+
group_by_handler *handler;
1974+
1975+
Pushdown_query(SELECT_LEX *select_lex_arg, group_by_handler *handler_arg)
1976+
: select_lex(select_lex_arg), store_data_in_temp_table(0),
1977+
handler(handler_arg) {}
1978+
1979+
~Pushdown_query() { delete handler; }
1980+
1981+
/* Function that calls the above scan functions */
1982+
int execute(JOIN *join);
1983+
};
19671984

19681985
#endif /* SQL_SELECT_INCLUDED */

storage/sequence/sequence.cc

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,12 @@ class ha_seq_group_by_handler: public group_by_handler
362362
bool first_row;
363363

364364
public:
365-
ha_seq_group_by_handler(THD *thd, SELECT_LEX *select_lex,
366-
List<Item> *fields,
367-
TABLE_LIST *table_list, ORDER *group_by,
368-
ORDER *order_by, Item *where,
369-
Item *having)
370-
:group_by_handler(thd, select_lex, fields, table_list, group_by,
371-
order_by, where, having, sequence_hton)
372-
{
373-
}
365+
ha_seq_group_by_handler(THD *thd_arg, List<Item> *fields_arg,
366+
TABLE_LIST *table_list_arg, ORDER *group_by_arg,
367+
ORDER *order_by_arg, Item *where_arg,
368+
Item *having_arg)
369+
:group_by_handler(thd_arg, fields_arg, table_list_arg, group_by_arg,
370+
order_by_arg, where_arg, having_arg, sequence_hton) {}
374371
~ha_seq_group_by_handler() {}
375372
bool init(TABLE *temporary_table, Item *having_arg,
376373
ORDER *order_by_arg);
@@ -380,10 +377,8 @@ class ha_seq_group_by_handler: public group_by_handler
380377
};
381378

382379
static group_by_handler *
383-
create_group_by_handler(THD *thd, SELECT_LEX *select_lex,
384-
List<Item> *fields,
385-
TABLE_LIST *table_list, ORDER *group_by,
386-
ORDER *order_by, Item *where,
380+
create_group_by_handler(THD *thd, List<Item> *fields, TABLE_LIST *table_list,
381+
ORDER *group_by, ORDER *order_by, Item *where,
387382
Item *having)
388383
{
389384
ha_seq_group_by_handler *handler;
@@ -432,8 +427,7 @@ create_group_by_handler(THD *thd, SELECT_LEX *select_lex,
432427
}
433428

434429
/* Create handler and return it */
435-
handler= new ha_seq_group_by_handler(thd, select_lex, fields, table_list,
436-
group_by,
430+
handler= new ha_seq_group_by_handler(thd, fields, table_list, group_by,
437431
order_by, where, having);
438432
return handler;
439433
}

0 commit comments

Comments
 (0)