Skip to content

Commit

Permalink
MDEV-12985: support percentile and median window functions
Browse files Browse the repository at this point in the history
Finalised the synatax and have started implementing the class for the PERCENTILE_DISC
  • Loading branch information
varunraiko committed Nov 1, 2017
1 parent fadfe44 commit 280945b
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 15 deletions.
3 changes: 2 additions & 1 deletion sql/item_sum.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ class Item_sum :public Item_func_or_sum
VARIANCE_FUNC, SUM_BIT_FUNC, UDF_SUM_FUNC, GROUP_CONCAT_FUNC,
ROW_NUMBER_FUNC, RANK_FUNC, DENSE_RANK_FUNC, PERCENT_RANK_FUNC,
CUME_DIST_FUNC, NTILE_FUNC, FIRST_VALUE_FUNC, LAST_VALUE_FUNC,
NTH_VALUE_FUNC, LEAD_FUNC, LAG_FUNC
NTH_VALUE_FUNC, LEAD_FUNC, LAG_FUNC, PERCENTILE_CONT_FUNC,
PERCENTILE_DISC_FUNC
};

Item **ref_by; /* pointer to a ref to the object used to register it */
Expand Down
69 changes: 69 additions & 0 deletions sql/item_windowfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,9 @@ class Item_sum_cume_dist: public Item_sum_window_with_row_count
Item_sum_cume_dist(THD *thd) : Item_sum_window_with_row_count(thd),
current_row_count_(0) {}

Item_sum_cume_dist(THD *thd, Item *arg) : Item_sum_window_with_row_count(thd,arg),
current_row_count_(0) {}

double val_real()
{
if (get_row_count() == 0)
Expand Down Expand Up @@ -618,6 +621,11 @@ class Item_sum_cume_dist: public Item_sum_window_with_row_count
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_sum_cume_dist>(thd, mem_root, this); }

ulonglong get_row_number()
{
return current_row_count_;
}

private:
ulonglong current_row_count_;
};
Expand Down Expand Up @@ -693,6 +701,61 @@ class Item_sum_ntile : public Item_sum_window_with_row_count
ulong current_row_count_;
};

class Item_sum_percentile_disc : public Item_sum_cume_dist
{
public:
Item_sum_percentile_disc(THD *thd, Item* arg) : Item_sum_cume_dist(thd, arg)
{}

double val_real()
{
if (get_row_count() == 0 || get_arg(0)->is_null())
{
null_value= true;
return 0;
}
null_value= false;
return 0;
}

bool add()
{
Item *arg = get_arg(0);
if (arg->is_null())
return true;
/*implementation to be done*/
return false;
}

enum Sumfunctype sum_func() const
{
return PERCENTILE_DISC_FUNC;
}

void clear()
{
//need to implement
}

const char*func_name() const
{
return "percentile_disc";
}

void update_field() {}
const Type_handler *type_handler() const { return &type_handler_double; }

void fix_length_and_dec()
{
decimals = 10; // TODO-cvicentiu find out how many decimals the standard
// requires.
}

Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_sum_percentile_disc>(thd, mem_root, this); }

};


class Item_window_func : public Item_func_or_sum
{
Expand Down Expand Up @@ -747,6 +810,8 @@ class Item_window_func : public Item_func_or_sum
case Item_sum::PERCENT_RANK_FUNC:
case Item_sum::CUME_DIST_FUNC:
case Item_sum::NTILE_FUNC:
case Item_sum::PERCENTILE_CONT_FUNC:
case Item_sum::PERCENTILE_DISC_FUNC:
return true;
default:
return false;
Expand All @@ -773,6 +838,8 @@ class Item_window_func : public Item_func_or_sum
case Item_sum::PERCENT_RANK_FUNC:
case Item_sum::CUME_DIST_FUNC:
case Item_sum::NTILE_FUNC:
case Item_sum::PERCENTILE_CONT_FUNC:
case Item_sum::PERCENTILE_DISC_FUNC:
return true;
default:
return false;
Expand All @@ -796,6 +863,8 @@ class Item_window_func : public Item_func_or_sum
case Item_sum::DENSE_RANK_FUNC:
case Item_sum::PERCENT_RANK_FUNC:
case Item_sum::CUME_DIST_FUNC:
case Item_sum::PERCENTILE_CONT_FUNC:
case Item_sum::PERCENTILE_DISC_FUNC:
return true;
default:
return false;
Expand Down
16 changes: 16 additions & 0 deletions sql/sql_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2490,6 +2490,20 @@ void add_special_frame_cursors(THD *thd, Cursor_manager *cursor_manager,
cursor_manager->add_cursor(fc);
break;
}
case Item_sum::PERCENTILE_DISC_FUNC:
{
fc= new Frame_unbounded_preceding(thd,
spec->partition_list,
spec->order_list);
fc->add_sum_func(item_sum);
cursor_manager->add_cursor(fc);
fc= new Frame_unbounded_following(thd,
spec->partition_list,
spec->order_list);
fc->add_sum_func(item_sum);
cursor_manager->add_cursor(fc);
break;
}
default:
fc= new Frame_unbounded_preceding(
thd, spec->partition_list, spec->order_list);
Expand All @@ -2514,6 +2528,8 @@ static bool is_computed_with_remove(Item_sum::Sumfunctype sum_func)
case Item_sum::NTILE_FUNC:
case Item_sum::FIRST_VALUE_FUNC:
case Item_sum::LAST_VALUE_FUNC:
case Item_sum::PERCENTILE_CONT_FUNC:
case Item_sum::PERCENTILE_DISC_FUNC:
return false;
default:
return true;
Expand Down
49 changes: 35 additions & 14 deletions sql/sql_yacc.yy
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
window_func
simple_window_func
inverse_distribution_function
inverse_distribution_function_def
function_call_keyword
function_call_nonkeyword
function_call_generic
Expand Down Expand Up @@ -10697,23 +10698,43 @@ simple_window_func:
}
;


inverse_distribution_function:
inverse_distribution_function_type '(' expr ')' WITHIN GROUP_SYM
'(' order_by_single_element_list ')' OVER_SYM '(' opt_window_ref opt_window_partition_clause ')'
{
my_yyabort_error((ER_VIEW_SELECT_VARIABLE, MYF(0)));
};

inverse_distribution_function_type:
PERCENTILE_CONT_SYM
{}
|PERCENTILE_DISC_SYM
{}
;
inverse_distribution_function_def WITHIN GROUP_SYM
'('
{ Select->prepare_add_window_spec(thd); }
order_by_single_element_list ')' OVER_SYM
'(' opt_window_ref opt_window_partition_clause ')'
{
LEX *lex= Lex;
if (Select->add_window_spec(thd, lex->win_ref,
Select->group_list,
Select->order_list,
NULL))
MYSQL_YYABORT;
$$= new (thd->mem_root) Item_window_func(thd, (Item_sum *) $1,
thd->lex->win_spec);
if ($$ == NULL)
MYSQL_YYABORT;
if (Select->add_window_func((Item_window_func *) $$))
MYSQL_YYABORT;
}
;

inverse_distribution_function_def:
PERCENTILE_CONT_SYM '(' expr ')'
{
//Not yet started implementing
}
| PERCENTILE_DISC_SYM '(' expr ')'
{
$$= new (thd->mem_root) Item_sum_percentile_disc(thd, $3);
if ($$ == NULL)
MYSQL_YYABORT;
}
;

order_by_single_element_list:
ORDER_SYM BY order_ident order_dir
ORDER_SYM BY order_list
;

window_name:
Expand Down

0 comments on commit 280945b

Please sign in to comment.