diff --git a/mysql-test/r/win.result b/mysql-test/r/win.result index 3c3eae7e462..05704590793 100644 --- a/mysql-test/r/win.result +++ b/mysql-test/r/win.result @@ -29,8 +29,8 @@ insert into t1 values (3, 10, 'xxx'); insert into t1 values (3, 20, 'vvv'); select a, row_number() over (partition by a order by b) from t1; a row_number() over (partition by a order by b) -2 2 2 1 +2 2 2 3 3 1 3 2 diff --git a/mysql-test/t/win.test b/mysql-test/t/win.test index 1161acf75a2..a977db8fb18 100644 --- a/mysql-test/t/win.test +++ b/mysql-test/t/win.test @@ -38,6 +38,7 @@ insert into t1 values (2, 20, 'yy'); insert into t1 values (3, 10, 'xxx'); insert into t1 values (3, 20, 'vvv'); +--sorted_result select a, row_number() over (partition by a order by b) from t1; select a, b, x, row_number() over (partition by a order by x) from t1; diff --git a/sql/sql_window.cc b/sql/sql_window.cc index e0527744389..1aa4ac03947 100644 --- a/sql/sql_window.cc +++ b/sql/sql_window.cc @@ -235,69 +235,32 @@ bool JOIN::process_window_functions(List *curr_fields_list) Item_window_func *item_win = (Item_window_func *) item; item_win->force_return_blank= false; Window_spec *spec = item_win->window_spec; - + /* + The sorting criteria should be + (spec->partition_list, spec->order_list) + + Connect the two lists for the duration of add_sorting_to_table() + call. + */ DBUG_ASSERT(spec->partition_list.next[0] == NULL); *(spec->partition_list.next)= spec->order_list.first; - // spec->partition_list - // spec->order_list + + /* + join_tab[top_join_tab_count].table is the temp. table where join + output was stored. + */ add_sorting_to_table(&join_tab[top_join_tab_count], spec->partition_list.first); join_tab[top_join_tab_count].used_for_window_func= true; create_sort_index(this->thd, this, &join_tab[top_join_tab_count]); + /* Disconnect order_list from partition_list */ *(spec->partition_list.next)= NULL; - //join_tab[top_join_tab_count] has the temp. table that we need. - //bool JOIN::add_sorting_to_table(JOIN_TAB *tab, ORDER *order) - // spec->partition_list.first -#if 0 - ha_rows examined_rows = 0; - ha_rows found_rows = 0; - ha_rows filesort_retval; - /* - psergey: Igor suggests to use create_sort_index() here, but I think - it doesn't make sense: create_sort_index() assumes that it operates - on a base table in the join. - It calls test_if_skip_sort_order, checks for quick_select and what - not. - It also assumes that ordering comes either from ORDER BY or GROUP BY. - todo: check this again. - */ - uint total_size= spec->partition_list.elements + - spec->order_list.elements; - SORT_FIELD *s_order= - (SORT_FIELD *) my_malloc(sizeof(SORT_FIELD) * (total_size+1), - MYF(MY_WME | MY_ZEROFILL | MY_THREAD_SPECIFIC)); - size_t pos= 0; - for (ORDER* curr = spec->partition_list.first; curr; curr=curr->next, pos++) - s_order[pos].item = *curr->item; - - for (ORDER* curr = spec->order_list.first; curr; curr=curr->next, pos++) - s_order[pos].item = *curr->item; - - /* This is free'd by free_io_cache call below. */ - table[0]->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE), - MYF(MY_WME | MY_ZEROFILL| - MY_THREAD_SPECIFIC)); - - Filesort_tracker dummy_tracker(false); - filesort_retval= filesort(thd, table[0], s_order, - total_size, - this->select, HA_POS_ERROR, FALSE, - &examined_rows, &found_rows, - &dummy_tracker); - table[0]->sort.found_records= filesort_retval; - - join_tab->read_first_record = join_init_read_record; - join_tab->records= found_rows; - - my_free(s_order); -#endif /* Go through the sorted array and compute the window function */ READ_RECORD info; - //TABLE *tbl= *table; TABLE *tbl= join_tab[top_join_tab_count].table; if (init_read_record(&info, thd, tbl, select, 0, 1, FALSE)) return true; @@ -326,11 +289,12 @@ bool JOIN::process_window_functions(List *curr_fields_list) return true; } item_win->set_read_value_from_result_field(); + /* This calls filesort_free_buffers(): */ end_read_record(&info); -#if 0 - filesort_free_buffers(table[0], true); - free_io_cache(table[0]); -#endif + + delete join_tab[top_join_tab_count].filesort; + join_tab[top_join_tab_count].filesort= NULL; + free_io_cache(tbl); } } }