Skip to content
/ server Public
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
616 changes: 616 additions & 0 deletions mysql-test/main/aggregates-filter.result

Large diffs are not rendered by default.

1,087 changes: 1,087 additions & 0 deletions mysql-test/main/aggregates-filter.test

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions sql/item_func.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6683,7 +6683,8 @@ longlong Item_func_row_count::val_int()

Item_func_sp::Item_func_sp(THD *thd, Name_resolution_context *context_arg,
sp_name *name, const Sp_handler *sph):
Item_func(thd), Item_sp(thd, context_arg, name), m_handler(sph)
Item_func(thd), Item_sp(thd, context_arg, name), m_handler(sph),
m_filter(NULL)
{
set_maybe_null();
}
Expand All @@ -6692,7 +6693,8 @@ Item_func_sp::Item_func_sp(THD *thd, Name_resolution_context *context_arg,
Item_func_sp::Item_func_sp(THD *thd, Name_resolution_context *context_arg,
sp_name *name_arg, const Sp_handler *sph,
List<Item> &list):
Item_func(thd, list), Item_sp(thd, context_arg, name_arg), m_handler(sph)
Item_func(thd, list), Item_sp(thd, context_arg, name_arg), m_handler(sph),
m_filter(NULL)
{
set_maybe_null();
}
Expand Down Expand Up @@ -6904,6 +6906,12 @@ Item_func_sp::fix_fields(THD *thd, Item **ref)
if (res)
DBUG_RETURN(TRUE);

if (m_filter && m_sp->agg_type() != GROUP_AGGREGATE)
{
my_error(ER_WRONG_USAGE, MYF(0), "FILTER", "NON-AGGREGATE FUNCTION");
DBUG_RETURN(TRUE);
}

if (m_sp->agg_type() == GROUP_AGGREGATE)
{
Item_sum_sp *item_sp;
Expand All @@ -6926,6 +6934,10 @@ Item_func_sp::fix_fields(THD *thd, Item **ref)
DBUG_RETURN(TRUE);
*ref= item_sp;
item_sp->name= name;
if (m_filter)
{
item_sp->set_filter(m_filter);
}
bool err= item_sp->fix_fields(thd, ref);
if (err)
DBUG_RETURN(TRUE);
Expand Down
3 changes: 3 additions & 0 deletions sql/item_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -4054,6 +4054,7 @@ class Item_func_sp :public Item_func,
{
private:
const Sp_handler *m_handler;
Item *m_filter;

bool execute();

Expand All @@ -4076,6 +4077,8 @@ class Item_func_sp :public Item_func,

virtual ~Item_func_sp() = default;

void set_filter(Item *filter) { m_filter= filter; }

void update_used_tables() override;

void cleanup() override;
Expand Down
5 changes: 4 additions & 1 deletion sql/item_jsonfunc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4805,6 +4805,9 @@ Item_func_json_objectagg::fix_fields(THD *thd, Item **ref)
/ collation.collation->mbminlen
* collation.collation->mbmaxlen);

if (fix_filter(thd))
return TRUE;

if (check_sum_func(thd, ref))
return TRUE;

Expand Down Expand Up @@ -4842,7 +4845,7 @@ bool Item_func_json_objectagg::add()
String *key;

key= args[0]->val_str(&buf);
if (args[0]->is_null())
if (args[0]->is_null() || !filter_passed())
return 0;

null_value= 0;
Expand Down
Loading