1/*
2 Copyright (c) 2014, 2015 SkySQL Ab & MariaDB Foundation
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */
16
17/*
18 This file implements the group_by_handler interface. This interface
19 can be used by storage handlers that can intercept summary or GROUP
20 BY queries from MariaDB and itself return the result to the user or
21 upper level. It is part of the Storage Engine API
22
23 Both main and sub queries are supported. Here are some examples of what the
24 storage engine could intersept:
25
26 SELECT count(*) FROM t1;
27 SELECT a,count(*) FROM t1 group by a;
28 SELECT a,count(*) as sum FROM t1 where b > 10 group by a, order by sum;
29 SELECT a,count(*) FROM t1,t2;
30 SELECT a, (select sum(*) from t2 where t1.a=t2.a) from t2;
31*/
32
33/**
34 The structure describing various parts of the query
35
36 The engine is supposed to take out parts that it can do internally.
37 For example, if the engine can return results sorted according to
38 the specified order_by clause, it sets Query::order_by=NULL before
39 returning.
40
41 At the moment the engine must take group_by (or return an error), and
42 optionally can take distinct, where, order_by, and having.
43
44 The engine should not modify the select list. It is the extended SELECT
45 clause (extended, because it has more items than the original
46 user-specified SELECT clause) and it contains all aggregate functions,
47 used in the query.
48*/
49struct Query
50{
51 List<Item> *select;
52 bool distinct;
53 TABLE_LIST *from;
54 Item *where;
55 ORDER *group_by;
56 ORDER *order_by;
57 Item *having;
58 // LIMIT
59};
60
61class group_by_handler
62{
63public:
64 THD *thd;
65 handlerton *ht;
66
67 /*
68 Temporary table where all results should be stored in record[0]
69 The table has a field for every item from the Query::select list.
70 */
71 TABLE *table;
72
73 group_by_handler(THD *thd_arg, handlerton *ht_arg)
74 : thd(thd_arg), ht(ht_arg), table(0) {}
75 virtual ~group_by_handler() {}
76
77 /*
78 Functions to scan data. All these returns 0 if ok, error code in case
79 of error
80 */
81
82 /*
83 Initialize group_by scan, prepare for next_row().
84 If this is a sub query with group by, this can be called many times for
85 a query.
86 */
87 virtual int init_scan()= 0;
88
89 /*
90 Return next group by result in table->record[0].
91 Return 0 if row found, HA_ERR_END_OF_FILE if last row and other error
92 number in case of fatal error.
93 */
94 virtual int next_row()= 0;
95
96 /* End scanning */
97 virtual int end_scan()=0;
98
99 /* Report errors */
100 virtual void print_error(int error, myf errflag);
101};
102
103