2016-02-12 20:33:56 -08:00
|
|
|
#include "item_windowfunc.h"
|
2016-02-05 14:12:17 +03:00
|
|
|
#include "my_dbug.h"
|
|
|
|
#include "my_global.h"
|
|
|
|
#include "sql_select.h" // test if group changed
|
|
|
|
|
2016-02-12 20:33:56 -08:00
|
|
|
|
|
|
|
bool
|
|
|
|
Item_window_func::fix_fields(THD *thd, Item **ref)
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(fixed == 0);
|
|
|
|
|
2016-02-05 00:52:17 +03:00
|
|
|
/*
|
|
|
|
TODO: why the last parameter is 'ref' in this call? What if window_func
|
|
|
|
decides to substitute itself for something else and does *ref=.... ?
|
|
|
|
This will substitute *this (an Item_window_func object) with Item_sum
|
|
|
|
object. Is this the intent?
|
|
|
|
*/
|
2016-02-12 20:33:56 -08:00
|
|
|
if (window_func->fix_fields(thd, ref))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
fixed= 1;
|
2016-02-14 21:00:05 +03:00
|
|
|
force_return_blank= true;
|
2016-02-05 14:12:17 +03:00
|
|
|
read_value_from_result_field= false;
|
2016-02-12 20:33:56 -08:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-02-05 14:12:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
This must be called before advance_window() can be called.
|
|
|
|
|
|
|
|
@detail
|
|
|
|
If we attempt to do it in fix_fields(), partition_fields will refer
|
|
|
|
to the original window function arguments.
|
|
|
|
We need it to refer to temp.table columns.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Item_window_func::setup_partition_border_check(THD *thd)
|
|
|
|
{
|
2016-02-16 00:33:53 +02:00
|
|
|
for (ORDER *curr= window_spec->partition_list.first; curr; curr=curr->next)
|
|
|
|
{
|
2016-02-05 14:12:17 +03:00
|
|
|
//curr->item_ptr->fix_fields(thd, curr->item);
|
|
|
|
Cached_item *tmp= new_Cached_item(thd, curr->item[0], TRUE);
|
|
|
|
partition_fields.push_back(tmp);
|
|
|
|
}
|
2016-02-06 01:53:17 +03:00
|
|
|
window_func->setup_window_func(thd, window_spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Item_sum_rank::setup_window_func(THD *thd, Window_spec *window_spec)
|
|
|
|
{
|
|
|
|
/* TODO: move this into Item_window_func? */
|
2016-02-16 00:33:53 +02:00
|
|
|
for (ORDER *curr= window_spec->order_list.first; curr; curr=curr->next)
|
|
|
|
{
|
2016-02-06 01:53:17 +03:00
|
|
|
Cached_item *tmp= new_Cached_item(thd, curr->item[0], TRUE);
|
|
|
|
orderby_fields.push_back(tmp);
|
|
|
|
}
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2016-02-16 00:22:12 +02:00
|
|
|
void Item_sum_dense_rank::setup_window_func(THD *thd, Window_spec *window_spec)
|
|
|
|
{
|
|
|
|
/* TODO: consider moving this && Item_sum_rank's implementation */
|
2016-02-16 00:33:53 +02:00
|
|
|
for (ORDER *curr= window_spec->order_list.first; curr; curr=curr->next)
|
|
|
|
{
|
2016-02-16 00:22:12 +02:00
|
|
|
Cached_item *tmp= new_Cached_item(thd, curr->item[0], TRUE);
|
|
|
|
orderby_fields.push_back(tmp);
|
|
|
|
}
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Item_sum_dense_rank::add()
|
|
|
|
{
|
|
|
|
if (test_if_group_changed(orderby_fields) > -1)
|
|
|
|
dense_rank++;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-06 01:53:17 +03:00
|
|
|
|
|
|
|
bool Item_sum_rank::add()
|
|
|
|
{
|
|
|
|
row_number++;
|
|
|
|
if (test_if_group_changed(orderby_fields) > -1)
|
|
|
|
{
|
|
|
|
/* Row value changed */
|
|
|
|
cur_rank= row_number;
|
|
|
|
}
|
|
|
|
return false;
|
2016-02-05 14:12:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Item_window_func::advance_window() {
|
|
|
|
|
|
|
|
int changed = test_if_group_changed(partition_fields);
|
|
|
|
|
2016-02-16 00:33:53 +02:00
|
|
|
if (changed > -1)
|
|
|
|
{
|
2016-02-06 01:53:17 +03:00
|
|
|
/* Next partition */
|
2016-02-05 14:12:17 +03:00
|
|
|
window_func->clear();
|
|
|
|
}
|
|
|
|
window_func->add();
|
|
|
|
}
|