Skip to content

Commit bf18dac

Browse files
committed
Lay the groundwork for variable number of cursors.
Instead of relying solely on top bound and bottom bound cursors, now we create a list of cursors that are iterated over.
1 parent 44fdb56 commit bf18dac

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-25
lines changed

sql/item_windowfunc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,11 @@ class Item_window_func : public Item_func_or_sum
499499
force_return_blank(true),
500500
read_value_from_result_field(false) {}
501501

502-
Item_sum *window_func() { return (Item_sum *) args[0]; }
502+
Item_sum *window_func() const { return (Item_sum *) args[0]; }
503503

504504
void update_used_tables();
505505

506-
bool is_frame_prohibited()
506+
bool is_frame_prohibited() const
507507
{
508508
switch (window_func()->sum_func()) {
509509
case Item_sum::ROW_NUMBER_FUNC:
@@ -517,7 +517,7 @@ class Item_window_func : public Item_func_or_sum
517517
}
518518
}
519519

520-
bool is_order_list_mandatory()
520+
bool is_order_list_mandatory() const
521521
{
522522
switch (window_func()->sum_func()) {
523523
case Item_sum::RANK_FUNC:

sql/sql_window.cc

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,28 @@ Frame_cursor *get_frame_cursor(Window_frame *frame, bool is_top_bound)
12121212
return NULL;
12131213
}
12141214

1215+
List<Frame_cursor> get_window_func_required_cursors(
1216+
const Item_window_func* item_win)
1217+
{
1218+
List<Frame_cursor> result;
1219+
1220+
/*
1221+
If it is not a regular window function that follows frame specifications,
1222+
specific cursors are required.
1223+
*/
1224+
if (item_win->is_frame_prohibited())
1225+
{
1226+
DBUG_ASSERT(0); // TODO-cvicentiu not-implemented yet.
1227+
}
1228+
1229+
/* A regular window function follows the frame specification. */
1230+
result.push_back(get_frame_cursor(item_win->window_spec->window_frame,
1231+
false));
1232+
result.push_back(get_frame_cursor(item_win->window_spec->window_frame,
1233+
true));
1234+
1235+
return result;
1236+
}
12151237

12161238
/*
12171239
Streamed window function computation with window frames.
@@ -1254,21 +1276,21 @@ bool compute_window_func_with_frames(Item_window_func *item_win,
12541276
{
12551277
THD *thd= current_thd;
12561278
int err= 0;
1257-
Frame_cursor *top_bound;
1258-
Frame_cursor *bottom_bound;
12591279

12601280
Item_sum *sum_func= item_win->window_func();
12611281
/* This algorithm doesn't support DISTINCT aggregator */
12621282
sum_func->set_aggregator(Aggregator::SIMPLE_AGGREGATOR);
1263-
1264-
Window_frame *window_frame= item_win->window_spec->window_frame;
1265-
top_bound= get_frame_cursor(window_frame, true);
1266-
bottom_bound= get_frame_cursor(window_frame, false);
1267-
1268-
top_bound->init(thd, info, item_win->window_spec->partition_list,
1269-
item_win->window_spec->order_list);
1270-
bottom_bound->init(thd, info, item_win->window_spec->partition_list,
1271-
item_win->window_spec->order_list);
1283+
1284+
List<Frame_cursor> cursors= get_window_func_required_cursors(item_win);
1285+
1286+
1287+
List_iterator_fast<Frame_cursor> it(cursors);
1288+
Frame_cursor *c;
1289+
while((c= it++))
1290+
{
1291+
c->init(thd, info, item_win->window_spec->partition_list,
1292+
item_win->window_spec->order_list);
1293+
}
12721294

12731295
bool is_error= false;
12741296
longlong rownum= 0;
@@ -1293,24 +1315,28 @@ bool compute_window_func_with_frames(Item_window_func *item_win,
12931315
pre_XXX functions assume that tbl->record[0] contains current_row, and
12941316
they may not change it.
12951317
*/
1296-
bottom_bound->pre_next_partition(rownum, sum_func);
1297-
top_bound->pre_next_partition(rownum, sum_func);
1318+
it.rewind();
1319+
while ((c= it++))
1320+
c->pre_next_partition(rownum, sum_func);
12981321
/*
12991322
We move bottom_bound first, because we want rows to be added into the
13001323
aggregate before top_bound attempts to remove them.
13011324
*/
1302-
bottom_bound->next_partition(rownum, sum_func);
1303-
top_bound->next_partition(rownum, sum_func);
1325+
it.rewind();
1326+
while ((c= it++))
1327+
c->next_partition(rownum, sum_func);
13041328
}
13051329
else
13061330
{
13071331
/* Again, both pre_XXX function can find current_row in tbl->record[0] */
1308-
bottom_bound->pre_next_row(sum_func);
1309-
top_bound->pre_next_row(sum_func);
1332+
it.rewind();
1333+
while ((c= it++))
1334+
c->pre_next_row(sum_func);
13101335

13111336
/* These make no assumptions about tbl->record[0] and may change it */
1312-
bottom_bound->next_row(sum_func);
1313-
top_bound->next_row(sum_func);
1337+
it.rewind();
1338+
while ((c= it++))
1339+
c->next_row(sum_func);
13141340
}
13151341
rownum++;
13161342

@@ -1331,8 +1357,7 @@ bool compute_window_func_with_frames(Item_window_func *item_win,
13311357
}
13321358

13331359
my_free(rowid_buf);
1334-
delete top_bound;
1335-
delete bottom_bound;
1360+
cursors.delete_elements();
13361361
return is_error? true: false;
13371362
}
13381363

sql/sql_window.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ typedef bool (*window_compute_func_t)(Item_window_func *item_win,
150150
Currently, we make a spearate filesort() call for each window function.
151151
*/
152152

153-
class Window_func_runner : public Sql_alloc
153+
class Window_func_runner : public Sql_alloc
154154
{
155155
Item_window_func *win_func;
156156
/* Window function can be computed over this sorting */

0 commit comments

Comments
 (0)