Commit 97a37edc authored by Varun Gupta's avatar Varun Gupta

MDEV-17137: Syntax errors with VIEW using MEDIAN

The syntax error happened because we had not implemented a different print for
percentile functions. The syntax is a bit different when we use percentile functions
as window functions in comparision to normal window functions.
Implemented a seperate print function for percentile functions
parent 103b1df5
......@@ -348,3 +348,21 @@ MEDIAN(`a2`) OVER (PARTITION BY `pk`)
FROM t1;
MEDIAN(`a1`) OVER () MEDIAN(`a2`) OVER (PARTITION BY `pk`)
DROP TABLE t1;
#
# MDEV-17137: Syntax errors with VIEW using MEDIAN
#
CREATE TABLE t1(val int);
INSERT INTO t1 VALUES (1), (2), (3);
CREATE VIEW v1 AS SELECT MEDIAN(val) OVER() FROM t1;
select * from v1;
MEDIAN(val) OVER()
2.0000000000
2.0000000000
2.0000000000
select median(val) OVER () FROM t1;
median(val) OVER ()
2.0000000000
2.0000000000
2.0000000000
drop table t1;
drop view v1;
......@@ -127,3 +127,15 @@ SELECT MEDIAN(`a1`) OVER (),
MEDIAN(`a2`) OVER (PARTITION BY `pk`)
FROM t1;
DROP TABLE t1;
--echo #
--echo # MDEV-17137: Syntax errors with VIEW using MEDIAN
--echo #
CREATE TABLE t1(val int);
INSERT INTO t1 VALUES (1), (2), (3);
CREATE VIEW v1 AS SELECT MEDIAN(val) OVER() FROM t1;
select * from v1;
select median(val) OVER () FROM t1;
drop table t1;
drop view v1;
......@@ -545,6 +545,11 @@ void Item_sum_hybrid_simple::update_field()
void Item_window_func::print(String *str, enum_query_type query_type)
{
if (only_single_element_order_list())
{
print_for_percentile_functions(str, query_type);
return;
}
window_func()->print(str, query_type);
str->append(" over ");
#ifndef DBUG_OFF
......@@ -554,3 +559,15 @@ void Item_window_func::print(String *str, enum_query_type query_type)
#endif
window_spec->print(str, query_type);
}
void Item_window_func::print_for_percentile_functions(String *str, enum_query_type query_type)
{
window_func()->print(str, query_type);
str->append(" within group ");
str->append('(');
window_spec->print_order(str,query_type);
str->append(')');
str->append(" over ");
str->append('(');
window_spec->print_partition(str,query_type);
str->append(')');
}
......@@ -1155,6 +1155,7 @@ class Item_window_func : public Item_func_or_sum
*/
bool force_return_blank;
bool read_value_from_result_field;
void print_for_percentile_functions(String *str, enum_query_type query_type);
public:
void set_phase_to_initial()
......
......@@ -82,19 +82,32 @@ void
Window_spec::print(String *str, enum_query_type query_type)
{
str->append('(');
print_partition(str, query_type);
print_order(str, query_type);
if (window_frame)
window_frame->print(str, query_type);
str->append(')');
}
void
Window_spec::print_partition(String *str, enum_query_type query_type)
{
if (partition_list->first)
{
str->append(STRING_WITH_LEN(" partition by "));
st_select_lex::print_order(str, partition_list->first, query_type);
}
}
void
Window_spec::print_order(String *str, enum_query_type query_type)
{
if (order_list->first)
{
str->append(STRING_WITH_LEN(" order by "));
st_select_lex::print_order(str, order_list->first, query_type);
}
if (window_frame)
window_frame->print(str, query_type);
str->append(')');
}
bool
......
......@@ -147,6 +147,8 @@ class Window_spec : public Sql_alloc
}
void print(String *str, enum_query_type query_type);
void print_order(String *str, enum_query_type query_type);
void print_partition(String *str, enum_query_type query_type);
};
......
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