Commit 2412c151 authored by Alexander Barkov's avatar Alexander Barkov

MDEV-15870 Using aggregate and window function in unexpected places can crash the server

parent ae0aefb1
...@@ -8354,3 +8354,14 @@ drop procedure p3; ...@@ -8354,3 +8354,14 @@ drop procedure p3;
CREATE PROCEDURE foo ( IN i INT UNSIGNED ) BEGIN END; CREATE PROCEDURE foo ( IN i INT UNSIGNED ) BEGIN END;
CALL foo( LAST_INSERT_ID() ); CALL foo( LAST_INSERT_ID() );
DROP PROCEDURE foo; DROP PROCEDURE foo;
#
# MDEV-15870 Using aggregate and window function in unexpected places can crash the server
#
CREATE PROCEDURE p1 (a TEXT) BEGIN END;
CALL p1(RANK() OVER (ORDER BY 1));
ERROR HY000: Window function is allowed only in SELECT list and ORDER BY clause
CALL p1(ROW_NUMBER() OVER ());
ERROR HY000: Window function is allowed only in SELECT list and ORDER BY clause
CALL p1(SUM(1));
ERROR HY000: Invalid use of group function
DROP PROCEDURE p1;
...@@ -9865,3 +9865,17 @@ drop procedure p3; ...@@ -9865,3 +9865,17 @@ drop procedure p3;
CREATE PROCEDURE foo ( IN i INT UNSIGNED ) BEGIN END; CREATE PROCEDURE foo ( IN i INT UNSIGNED ) BEGIN END;
CALL foo( LAST_INSERT_ID() ); CALL foo( LAST_INSERT_ID() );
DROP PROCEDURE foo; DROP PROCEDURE foo;
--echo #
--echo # MDEV-15870 Using aggregate and window function in unexpected places can crash the server
--echo #
CREATE PROCEDURE p1 (a TEXT) BEGIN END;
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
CALL p1(RANK() OVER (ORDER BY 1));
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
CALL p1(ROW_NUMBER() OVER ());
--error ER_INVALID_GROUP_FUNC_USE
CALL p1(SUM(1));
DROP PROCEDURE p1;
...@@ -68,14 +68,14 @@ size_t Item_sum::ram_limitation(THD *thd) ...@@ -68,14 +68,14 @@ size_t Item_sum::ram_limitation(THD *thd)
bool Item_sum::init_sum_func_check(THD *thd) bool Item_sum::init_sum_func_check(THD *thd)
{ {
SELECT_LEX *curr_sel= thd->lex->current_select; SELECT_LEX *curr_sel= thd->lex->current_select;
if (!curr_sel->name_visibility_map) if (curr_sel && !curr_sel->name_visibility_map)
{ {
for (SELECT_LEX *sl= curr_sel; sl; sl= sl->context.outer_select()) for (SELECT_LEX *sl= curr_sel; sl; sl= sl->context.outer_select())
{ {
curr_sel->name_visibility_map|= (1 << sl-> nest_level); curr_sel->name_visibility_map|= (1 << sl-> nest_level);
} }
} }
if (!(thd->lex->allow_sum_func & curr_sel->name_visibility_map)) if (!curr_sel || !(thd->lex->allow_sum_func & curr_sel->name_visibility_map))
{ {
my_message(ER_INVALID_GROUP_FUNC_USE, ER_THD(thd, ER_INVALID_GROUP_FUNC_USE), my_message(ER_INVALID_GROUP_FUNC_USE, ER_THD(thd, ER_INVALID_GROUP_FUNC_USE),
MYF(0)); MYF(0));
......
...@@ -71,9 +71,9 @@ Item_window_func::fix_fields(THD *thd, Item **ref) ...@@ -71,9 +71,9 @@ Item_window_func::fix_fields(THD *thd, Item **ref)
{ {
DBUG_ASSERT(fixed == 0); DBUG_ASSERT(fixed == 0);
enum_parsing_place place= thd->lex->current_select->context_analysis_place; if (!thd->lex->current_select ||
(thd->lex->current_select->context_analysis_place != SELECT_LIST &&
if (!(place == SELECT_LIST || place == IN_ORDER_BY)) thd->lex->current_select->context_analysis_place != IN_ORDER_BY))
{ {
my_error(ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION, MYF(0)); my_error(ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION, MYF(0));
return true; return true;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment