summaryrefslogtreecommitdiff
path: root/sql/sql_window.h
blob: 1c02740e7697b232c01bf4af27b6bcb7cd68a277 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
   Copyright (c) 2016, 2022 MariaDB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#ifndef SQL_WINDOW_INCLUDED
#define SQL_WINDOW_INCLUDED

#include "filesort.h"

class Item_window_func;

/*
  Window functions module. 
  
  Each instance of window function has its own element in SELECT_LEX::window_specs.
*/


class Window_frame_bound : public Sql_alloc
{

public:
 
  enum Bound_precedence_type
  {
    PRECEDING,
    CURRENT,           // Used for CURRENT ROW window frame bounds
    FOLLOWING
  };

  Bound_precedence_type precedence_type;
 

  /* 
    For UNBOUNDED PRECEDING / UNBOUNDED FOLLOWING window frame bounds
    precedence type is seto to PRECEDING / FOLLOWING and
    offset is set to NULL. 
    The offset is not meaningful with precedence type CURRENT 
  */
  Item *offset;

  Window_frame_bound(Bound_precedence_type prec_type,
                     Item *offset_val)
    : precedence_type(prec_type), offset(offset_val) {}

  bool is_unbounded() { return offset == NULL; }

  void print(String *str, enum_query_type query_type);

};


class Window_frame : public Sql_alloc
{
  
public:

  enum Frame_units
  {
    UNITS_ROWS,
    UNITS_RANGE
  };

  enum Frame_exclusion
  {
    EXCL_NONE,
    EXCL_CURRENT_ROW,
    EXCL_GROUP,
    EXCL_TIES
  };

  Frame_units units;

  Window_frame_bound *top_bound;

  Window_frame_bound *bottom_bound;

  Frame_exclusion exclusion;

  Window_frame(Frame_units win_frame_units,
               Window_frame_bound *win_frame_top_bound,
               Window_frame_bound *win_frame_bottom_bound,
               Frame_exclusion win_frame_exclusion)
    : units(win_frame_units), top_bound(win_frame_top_bound),
      bottom_bound(win_frame_bottom_bound), exclusion(win_frame_exclusion) {}

  bool check_frame_bounds();

  void print(String *str, enum_query_type query_type);

};

class Window_spec : public Sql_alloc
{
  bool window_names_are_checked;
 public:
  virtual ~Window_spec() = default;

  LEX_CSTRING *window_ref;

  SQL_I_List<ORDER> *partition_list;
  SQL_I_List<ORDER> *save_partition_list;

  SQL_I_List<ORDER> *order_list;
  SQL_I_List<ORDER> *save_order_list;

  Window_frame *window_frame;

  Window_spec *referenced_win_spec;

  /*
    Window_spec objects are numbered by the number of their appearance in the
    query. This is used by compare_order_elements() to provide a predictable
    ordering of PARTITION/ORDER BY clauses.
  */
  int win_spec_number;

  Window_spec(LEX_CSTRING *win_ref, SQL_I_List<ORDER> *part_list,
              SQL_I_List<ORDER> *ord_list, Window_frame *win_frame)
    : window_names_are_checked(false), window_ref(win_ref),
      partition_list(part_list), save_partition_list(NULL),
      order_list(ord_list), save_order_list(NULL),
      window_frame(win_frame), referenced_win_spec(NULL) {}

  virtual const char *name() { return NULL; }

  bool check_window_names(List_iterator_fast<Window_spec> &it);

  const char *window_reference()
  {
    return window_ref ? window_ref->str : NULL;
  }

  void join_partition_and_order_lists()
  {
    *(partition_list->next)= order_list->first;
  }

  void disjoin_partition_and_order_lists()
  {
    *(partition_list->next)= NULL;
  }

  void print(String *str, enum_query_type query_type);
  void print_order(String *str, enum_query_type query_type);
  void print_partition(String *str, enum_query_type query_type);

};

class Window_def : public Window_spec
{
 public:

  LEX_CSTRING *window_name;

  Window_def(LEX_CSTRING *win_name,
             LEX_CSTRING *win_ref, 
             SQL_I_List<ORDER> *part_list,
             SQL_I_List<ORDER> *ord_list,
             Window_frame *win_frame) 
    : Window_spec(win_ref, part_list, ord_list, win_frame),
      window_name(win_name) {}
 
  const char *name() { return window_name->str; }

};

int setup_windows(THD *thd, Ref_ptr_array ref_pointer_array, TABLE_LIST *tables,
	          List<Item> &fields, List<Item> &all_fields, 
                  List<Window_spec> &win_specs, List<Item_window_func> &win_funcs);


//////////////////////////////////////////////////////////////////////////////
// Classes that make window functions computation a part of SELECT's query plan
//////////////////////////////////////////////////////////////////////////////

class Frame_cursor;
/*
  This handles computation of one window function.

  Currently, we make a spearate filesort() call for each window function.
*/

class Window_func_runner : public Sql_alloc
{
public:
  /* Add the function to be computed during the execution pass  */
  bool add_function_to_run(Item_window_func *win_func);

  /* Compute and fill the fields in the table. */
  bool exec(THD *thd, TABLE *tbl, SORT_INFO *filesort_result);

private:
  /* A list of window functions for which this Window_func_runner will compute
     values during the execution phase. */
  List<Item_window_func> window_functions;
};


/*
  Represents a group of window functions that require the same sorting of 
  rows and so share the filesort() call.

*/

class Window_funcs_sort : public Sql_alloc
{
public:
  bool setup(THD *thd, SQL_SELECT *sel, List_iterator<Item_window_func> &it,
             st_join_table *join_tab);
  bool exec(JOIN *join, bool keep_filesort_result);
  void cleanup() { delete filesort; }

  friend class Window_funcs_computation;

private:
  Window_func_runner runner;

  /* Window functions can be computed over this sorting */
  Filesort *filesort;
};


struct st_join_table;
class Explain_aggr_window_funcs;

/*
  This is a "window function computation phase": a single object of this class
  takes care of computing all window functions in a SELECT.

  - JOIN optimizer is exected to call setup() during query optimization.
  - JOIN::exec() should call exec() once it has collected join output in a
    temporary table.
*/

class Window_funcs_computation : public Sql_alloc
{
  List<Window_funcs_sort> win_func_sorts;
public:
  bool setup(THD *thd, List<Item_window_func> *window_funcs, st_join_table *tab);
  bool exec(JOIN *join, bool keep_last_filesort_result);

  Explain_aggr_window_funcs *save_explain_plan(MEM_ROOT *mem_root, bool is_analyze);
  void cleanup();
};


#endif /* SQL_WINDOW_INCLUDED */