Commit d70dac20 authored by Alexander Barkov's avatar Alexander Barkov

MDEV-20278 PERCENTILE_DISC() returns a wrong data type

parent e978efd9
This diff is collapsed.
......@@ -146,3 +146,66 @@ select * from v1;
select median(val) OVER () FROM t1;
drop table t1;
drop view v1;
--echo #
--echo # MDEV-20278 PERCENTILE_DISC() returns a wrong data type
--echo #
--echo # INT variants
CREATE TABLE t1 (name CHAR(30), star_rating INT);
INSERT INTO t1 VALUES ('Lord of the Ladybirds', 5);
INSERT INTO t1 VALUES ('Lord of the Ladybirds', 3);
INSERT INTO t1 VALUES ('Lady of the Flies', 1);
INSERT INTO t1 VALUES ('Lady of the Flies', 2);
INSERT INTO t1 VALUES ('Lady of the Flies', 5);
CREATE OR REPLACE TABLE t2 AS SELECT name, PERCENTILE_DISC(0.5)
WITHIN GROUP (ORDER BY star_rating)
OVER (PARTITION BY name) AS pc FROM t1;
SHOW CREATE TABLE t2;
DROP TABLE t2, t1;
--echo # UNSIGNED INT variants
CREATE OR REPLACE TABLE t1 (name CHAR(30), star_rating BIGINT UNSIGNED);
INSERT INTO t1 VALUES ('Lord of the Ladybirds', 0x8000000000000005);
INSERT INTO t1 VALUES ('Lord of the Ladybirds', 0x8000000000000003);
INSERT INTO t1 VALUES ('Lady of the Flies', 0x8000000000000001);
INSERT INTO t1 VALUES ('Lady of the Flies', 0x8000000000000002);
INSERT INTO t1 VALUES ('Lady of the Flies', 0x8000000000000003);
CREATE OR REPLACE TABLE t2 AS SELECT name, PERCENTILE_DISC(0.5)
WITHIN GROUP (ORDER BY star_rating)
OVER (PARTITION BY name) AS pc FROM t1;
SHOW CREATE TABLE t2;
SELECT name, pc, HEX(pc) FROM t2 ORDER BY name, pc;
DROP TABLE t2, t1;
--echo # FLOAT variants
CREATE TABLE t1 (name CHAR(30), star_rating FLOAT);
INSERT INTO t1 VALUES ('Lord of the Ladybirds', 5);
INSERT INTO t1 VALUES ('Lord of the Ladybirds', 3);
INSERT INTO t1 VALUES ('Lady of the Flies', 1);
INSERT INTO t1 VALUES ('Lady of the Flies', 2);
INSERT INTO t1 VALUES ('Lady of the Flies', 5);
CREATE TABLE t2 AS SELECT name, PERCENTILE_DISC(0.5)
WITHIN GROUP (ORDER BY star_rating)
OVER (PARTITION BY name) AS pc FROM t1;
SHOW CREATE TABLE t2;
DROP TABLE t2, t1;
--echo # DECIMAL variants
CREATE OR REPLACE TABLE t1 (name CHAR(30), star_rating DECIMAL(30,2));
INSERT INTO t1 VALUES ('Lord of the Ladybirds', 50000000000);
INSERT INTO t1 VALUES ('Lord of the Ladybirds', 30000000000);
INSERT INTO t1 VALUES ('Lady of the Flies', 10000000000);
INSERT INTO t1 VALUES ('Lady of the Flies', 20000000000);
INSERT INTO t1 VALUES ('Lady of the Flies', 50000000000);
CREATE OR REPLACE TABLE t2 AS SELECT name, PERCENTILE_DISC(0.5)
WITHIN GROUP (ORDER BY star_rating)
OVER (PARTITION BY name) AS pc FROM t1;
SHOW CREATE TABLE t2;
SELECT * FROM t2 ORDER BY name, pc;
DROP TABLE t2, t1;
......@@ -174,7 +174,8 @@ bool Item_window_func::check_result_type_of_order_item()
{
if (only_single_element_order_list())
{
Item_result rtype= window_spec->order_list->first->item[0]->cmp_type();
Item *src_item= window_spec->order_list->first->item[0];
Item_result rtype= src_item->cmp_type();
// TODO (varun) : support date type in percentile_cont function
if (rtype != REAL_RESULT && rtype != INT_RESULT &&
rtype != DECIMAL_RESULT && rtype != TIME_RESULT)
......@@ -182,7 +183,14 @@ bool Item_window_func::check_result_type_of_order_item()
my_error(ER_WRONG_TYPE_FOR_PERCENTILE_FUNC, MYF(0), window_func()->func_name());
return TRUE;
}
setting_handler_for_percentile_functions(rtype);
if (window_func()->sum_func() == Item_sum::PERCENTILE_DISC_FUNC)
{
Item_sum_percentile_disc *func=
static_cast<Item_sum_percentile_disc*>(window_func());
func->set_handler(src_item->type_handler());
func->Type_std_attributes::set(src_item);
Type_std_attributes::set(src_item);
}
}
return FALSE;
}
......
......@@ -1097,17 +1097,6 @@ class Item_window_func : public Item_func_or_sum
}
}
void setting_handler_for_percentile_functions(Item_result rtype) const
{
switch (window_func()->sum_func()){
case Item_sum::PERCENTILE_DISC_FUNC:
((Item_sum_percentile_disc* ) window_func())->set_handler_by_cmp_type(rtype);
break;
default:
return;
}
}
bool check_result_type_of_order_item();
......
......@@ -3506,10 +3506,6 @@ class Type_handler_hybrid_field_type
{
return (m_type_handler= Type_handler::get_handler_by_result_type(type));
}
const Type_handler *set_handler_by_cmp_type(Item_result type)
{
return (m_type_handler= Type_handler::get_handler_by_cmp_type(type));
}
const Type_handler *set_handler_by_result_type(Item_result type,
uint max_octet_length,
CHARSET_INFO *cs)
......
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