Skip to content

Commit d8a20d4

Browse files
committed
Post-merge fixes. win.test passes but further cleanup is needed.
1 parent a9ed132 commit d8a20d4

File tree

6 files changed

+77
-14
lines changed

6 files changed

+77
-14
lines changed

mysql-test/r/win.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ insert into t1 values (3, 10, 'xxx');
2929
insert into t1 values (3, 20, 'vvv');
3030
select a, row_number() over (partition by a order by b) from t1;
3131
a row_number() over (partition by a order by b)
32-
2 1
3332
2 2
33+
2 1
3434
2 3
3535
3 1
3636
3 2

sql/item_windowfunc.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Item_window_func::fix_fields(THD *thd, Item **ref)
1919
return TRUE;
2020

2121
fixed= 1;
22+
force_return_blank= true;
2223
read_value_from_result_field= false;
2324
return FALSE;
2425
}

sql/item_windowfunc.h

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ class Item_sum_row_number: public Item_sum_int
1919
{
2020
longlong count;
2121

22-
void clear() {}
23-
bool add() { return false; }
22+
public:
23+
void clear()
24+
{
25+
count= 0;
26+
}
27+
bool add()
28+
{
29+
count++;
30+
return false;
31+
}
2432
void update_field() {}
2533

26-
public:
2734
Item_sum_row_number(THD *thd)
2835
: Item_sum_int(thd), count(0) {}
2936

@@ -32,6 +39,10 @@ class Item_sum_row_number: public Item_sum_int
3239
return ROW_NUMBER_FUNC;
3340
}
3441

42+
longlong val_int()
43+
{
44+
return count;
45+
}
3546
const char*func_name() const
3647
{
3748
return "row_number";
@@ -251,26 +262,38 @@ class Item_sum_cume_dist: public Item_sum_num
251262

252263
class Item_window_func : public Item_result_field
253264
{
265+
/* Window function parameters as we've got them from the parser */
254266
Item_sum *window_func;
255267
LEX_STRING *window_name;
268+
public:
256269
Window_spec *window_spec;
257-
270+
271+
/*
272+
This stores the data bout the partition we're currently in.
273+
advance_window() uses this to tell when we've left one partition and
274+
entered another.
275+
*/
276+
List<Cached_item> partition_fields;
258277
public:
259278
Item_window_func(THD *thd, Item_sum *win_func, LEX_STRING *win_name)
260279
: Item_result_field(thd), window_func(win_func),
261280
window_name(win_name), window_spec(NULL),
281+
force_return_blank(true),
262282
read_value_from_result_field(false) {}
263283

264284
Item_window_func(THD *thd, Item_sum *win_func, Window_spec *win_spec)
265285
: Item_result_field(thd), window_func(win_func),
266286
window_name(NULL), window_spec(win_spec),
287+
force_return_blank(true),
267288
read_value_from_result_field(false) {}
268289

269290
/*
270291
Computation functions.
271292
*/
272293
void setup_partition_border_check(THD *thd);
273294

295+
void advance_window();
296+
274297
enum_field_types field_type() const { return window_func->field_type(); }
275298
enum Item::Type type() const { return Item::WINDOW_FUNC_ITEM; }
276299

@@ -292,7 +315,11 @@ class Item_window_func : public Item_result_field
292315
item_windowfunc->val_int() will be called.
293316
During Phase#3, read_value_from_result_field= true.
294317
*/
318+
public:
319+
// TODO: how to reset this for subquery re-execution??
320+
bool force_return_blank;
295321
private:
322+
296323
bool read_value_from_result_field;
297324

298325
public:
@@ -303,24 +330,32 @@ class Item_window_func : public Item_result_field
303330

304331
double val_real()
305332
{
333+
if (force_return_blank)
334+
return 0.0;
306335
return read_value_from_result_field? result_field->val_real() :
307336
window_func->val_real();
308337
}
309338

310339
longlong val_int()
311340
{
341+
if (force_return_blank)
342+
return 0;
312343
return read_value_from_result_field? result_field->val_int() :
313344
window_func->val_int();
314345
}
315346

316347
String* val_str(String* str)
317348
{
349+
if (force_return_blank)
350+
return str;
318351
return read_value_from_result_field? result_field->val_str(str) :
319352
window_func->val_str(str);
320353
}
321354

322355
my_decimal* val_decimal(my_decimal* dec)
323356
{
357+
if (force_return_blank)
358+
return dec;
324359
return read_value_from_result_field? result_field->val_decimal(dec) :
325360
window_func->val_decimal(dec);
326361
}

sql/sql_select.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
181181
static enum_nested_loop_state
182182
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
183183

184-
static int test_if_group_changed(List<Cached_item> &list);
185184
static int join_read_const_table(THD *thd, JOIN_TAB *tab, POSITION *pos);
186185
static int join_read_system(JOIN_TAB *tab);
187186
static int join_read_const(JOIN_TAB *tab);
@@ -234,7 +233,7 @@ static bool list_contains_unique_index(TABLE *table,
234233
bool (*find_func) (Field *, void *), void *data);
235234
static bool find_field_in_item_list (Field *field, void *data);
236235
static bool find_field_in_order_list (Field *field, void *data);
237-
static int create_sort_index(THD *thd, JOIN *join, JOIN_TAB *tab);
236+
int create_sort_index(THD *thd, JOIN *join, JOIN_TAB *tab);
238237
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field,
239238
Item *having);
240239
static int remove_dup_with_hash_index(THD *thd,TABLE *table,
@@ -3235,7 +3234,6 @@ void JOIN::exec_inner()
32353234
error= thd->is_error();
32363235
DBUG_VOID_RETURN;
32373236
}
3238-
process_window_functions(curr_fields_list);
32393237

32403238
THD_STAGE_INFO(thd, stage_sending_data);
32413239
DBUG_PRINT("info", ("%s", thd->proc_info));
@@ -8500,6 +8498,11 @@ bool JOIN::get_best_combination()
85008498
(tmp_table_param. using_outer_summary_function ? 2 : 1) : 0) +
85018499
(order ? 1 : 0) +
85028500
(select_options & (SELECT_BIG_RESULT | OPTION_BUFFER_RESULT) ? 1 : 0) ;
8501+
8502+
// psergey-temp:
8503+
if (select_lex->window_specs.elements)
8504+
aggr_tables++;
8505+
85038506
if (aggr_tables > 2)
85048507
aggr_tables= 2;
85058508
if (!(join_tab= (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*
@@ -19062,7 +19065,7 @@ int join_init_read_record(JOIN_TAB *tab)
1906219065

1906319066
if (tab->distinct && tab->remove_duplicates()) // Remove duplicates.
1906419067
return 1;
19065-
if (tab->filesort && tab->sort_table()) // Sort table.
19068+
if (tab->filesort && !tab->used_for_window_func && tab->sort_table()) // Sort table.
1906619069
return 1;
1906719070

1906819071
if (tab->select && tab->select->quick && (error= tab->select->quick->reset()))
@@ -21102,7 +21105,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
2110221105
1 No records
2110321106
*/
2110421107

21105-
static int
21108+
int
2110621109
create_sort_index(THD *thd, JOIN *join, JOIN_TAB *tab)
2110721110
{
2110821111
ha_rows examined_rows;
@@ -22347,7 +22350,7 @@ int test_if_item_cache_changed(List<Cached_item> &list)
2234722350
value>=0 - Number of the component where the group changed
2234822351
*/
2234922352

22350-
static int
22353+
int
2235122354
test_if_group_changed(List<Cached_item> &list)
2235222355
{
2235322356
DBUG_ENTER("test_if_group_changed");
@@ -25810,8 +25813,10 @@ AGGR_OP::end_send()
2581025813
table->file->print_error(new_errno,MYF(0));
2581125814
return NESTED_LOOP_ERROR;
2581225815
}
25816+
2581325817
// Update ref array
2581425818
join_tab->join->set_items_ref_array(*join_tab->ref_array);
25819+
join->process_window_functions(&join->fields_list); // location #2
2581525820
table->reginfo.lock_type= TL_UNLOCK;
2581625821

2581725822
bool in_first_read= true;

sql/sql_select.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ typedef struct st_join_table {
424424
/* Sorting related info */
425425
Filesort *filesort;
426426

427+
bool used_for_window_func;
428+
427429
/**
428430
List of topmost expressions in the select list. The *next* JOIN TAB
429431
in the plan should use it to obtain correct values. Same applicable to
@@ -2295,4 +2297,6 @@ class Pushdown_query: public Sql_alloc
22952297
};
22962298

22972299
bool test_if_order_compatible(SQL_I_List<ORDER> &a, SQL_I_List<ORDER> &b);
2300+
int test_if_group_changed(List<Cached_item> &list);
2301+
int create_sort_index(THD *thd, JOIN *join, JOIN_TAB *tab);
22982302
#endif /* SQL_SELECT_INCLUDED */

sql/sql_window.cc

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,24 @@ bool JOIN::process_window_functions(List<Item> *curr_fields_list)
233233
if (item->type() == Item::WINDOW_FUNC_ITEM)
234234
{
235235
Item_window_func *item_win = (Item_window_func *) item;
236+
item_win->force_return_blank= false;
236237
Window_spec *spec = item_win->window_spec;
238+
239+
DBUG_ASSERT(spec->partition_list.next[0] == NULL);
240+
*(spec->partition_list.next)= spec->order_list.first;
237241
// spec->partition_list
238242
// spec->order_list
243+
add_sorting_to_table(&join_tab[top_join_tab_count],
244+
spec->partition_list.first);
245+
join_tab[top_join_tab_count].used_for_window_func= true;
246+
247+
create_sort_index(this->thd, this, &join_tab[top_join_tab_count]);
248+
*(spec->partition_list.next)= NULL;
249+
//join_tab[top_join_tab_count] has the temp. table that we need.
250+
//bool JOIN::add_sorting_to_table(JOIN_TAB *tab, ORDER *order)
251+
252+
// spec->partition_list.first
253+
#if 0
239254
ha_rows examined_rows = 0;
240255
ha_rows found_rows = 0;
241256
ha_rows filesort_retval;
@@ -277,18 +292,19 @@ bool JOIN::process_window_functions(List<Item> *curr_fields_list)
277292
join_tab->records= found_rows;
278293

279294
my_free(s_order);
280-
295+
#endif
281296
/*
282297
Go through the sorted array and compute the window function
283298
*/
284299
READ_RECORD info;
285-
if (init_read_record(&info, thd, table[0], select, 0, 1, FALSE))
300+
//TABLE *tbl= *table;
301+
TABLE *tbl= join_tab[top_join_tab_count].table;
302+
if (init_read_record(&info, thd, tbl, select, 0, 1, FALSE))
286303
return true;
287304

288305
item_win->setup_partition_border_check(thd);
289306

290307
int err;
291-
TABLE *tbl= *table;
292308
while (!(err=info.read_record(&info)))
293309
{
294310
store_record(tbl,record[1]);
@@ -311,8 +327,10 @@ bool JOIN::process_window_functions(List<Item> *curr_fields_list)
311327
}
312328
item_win->set_read_value_from_result_field();
313329
end_read_record(&info);
330+
#if 0
314331
filesort_free_buffers(table[0], true);
315332
free_io_cache(table[0]);
333+
#endif
316334
}
317335
}
318336
}

0 commit comments

Comments
 (0)