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 02110-1335 USA */ 16 17 #ifndef GROUP_BY_HANDLER_INCLUDED 18 #define GROUP_BY_HANDLER_INCLUDED 19 20 class Select_limit_counters; 21 /* 22 This file implements the group_by_handler interface. This interface 23 can be used by storage handlers that can intercept summary or GROUP 24 BY queries from MariaDB and itself return the result to the user or 25 upper level. It is part of the Storage Engine API 26 27 Both main and sub queries are supported. Here are some examples of what the 28 storage engine could intersept: 29 30 SELECT count(*) FROM t1; 31 SELECT a,count(*) FROM t1 group by a; 32 SELECT a,count(*) as sum FROM t1 where b > 10 group by a, order by sum; 33 SELECT a,count(*) FROM t1,t2; 34 SELECT a, (select sum(*) from t2 where t1.a=t2.a) from t2; 35 */ 36 37 /** 38 The structure describing various parts of the query 39 40 The engine is supposed to take out parts that it can do internally. 41 For example, if the engine can return results sorted according to 42 the specified order_by clause, it sets Query::order_by=NULL before 43 returning. 44 45 At the moment the engine must take group_by (or return an error), and 46 optionally can take distinct, where, order_by, and having. 47 48 The engine should not modify the select list. It is the extended SELECT 49 clause (extended, because it has more items than the original 50 user-specified SELECT clause) and it contains all aggregate functions, 51 used in the query. 52 */ 53 struct Query 54 { 55 List<Item> *select; 56 bool distinct; 57 TABLE_LIST *from; 58 Item *where; 59 ORDER *group_by; 60 ORDER *order_by; 61 Item *having; 62 // LIMIT 63 Select_limit_counters *limit; 64 }; 65 66 class group_by_handler 67 { 68 public: 69 THD *thd; 70 handlerton *ht; 71 72 /* 73 Temporary table where all results should be stored in record[0] 74 The table has a field for every item from the Query::select list. 75 */ 76 TABLE *table; 77 group_by_handler(THD * thd_arg,handlerton * ht_arg)78 group_by_handler(THD *thd_arg, handlerton *ht_arg) 79 : thd(thd_arg), ht(ht_arg), table(0) {} ~group_by_handler()80 virtual ~group_by_handler() {} 81 82 /* 83 Functions to scan data. All these returns 0 if ok, error code in case 84 of error 85 */ 86 87 /* 88 Initialize group_by scan, prepare for next_row(). 89 If this is a sub query with group by, this can be called many times for 90 a query. 91 */ 92 virtual int init_scan()= 0; 93 94 /* 95 Return next group by result in table->record[0]. 96 Return 0 if row found, HA_ERR_END_OF_FILE if last row and other error 97 number in case of fatal error. 98 */ 99 virtual int next_row()= 0; 100 101 /* End scanning */ 102 virtual int end_scan()=0; 103 104 /* Report errors */ 105 virtual void print_error(int error, myf errflag); 106 }; 107 108 #endif //GROUP_BY_HANDLER_INCLUDED 109