Commit 459b87f6 authored by Dan Solodko's avatar Dan Solodko Committed by Anel

MDEV-9911: NTILE must return an error when parameter is not stable

parent cae4b3f8
...@@ -433,3 +433,81 @@ ntile((select a from t1)) over (partition by b order by pk) ...@@ -433,3 +433,81 @@ ntile((select a from t1)) over (partition by b order by pk)
from t1; from t1;
ERROR 21000: Subquery returns more than 1 row ERROR 21000: Subquery returns more than 1 row
drop table t1; drop table t1;
#
# MDEV-9911 NTILE must return an error when parameter is not stable
#
create table t1 (
pk int primary key,
c1 nvarchar(10),
c2 nvarchar(10),
c3 int
);
insert into t1 values
(1, 'Mark', 'Male', 5),
(2, 'John', 'Male', 5),
(3, 'Pam', 'Female', 6),
(4, 'Sara', 'Female', 6),
(5, 'Todd', 'Male', 5),
(6, 'Mary', 'Female', 6),
(7, 'Ben', 'Male', 5),
(8, 'Jodi', 'Female', 6),
(9, 'Tom', 'Male', 5),
(10, 'Lucky', 'Male', 5),
(11, 'Mark', 'Male', 5),
(12, 'John', 'Male', 5),
(13, 'Pam', 'Female', 6),
(14, 'Sara', 'Female', 6),
(15, 'Todd', 'Male', 5),
(16, 'Mary', 'Female', 6),
(17, 'Ben', 'Male', 5),
(18, 'Jodi', 'Female', 6),
(19, 'Tom', 'Male', 5),
(20, 'Lucky', 'Male', 5);
select c1, c2, c3, ntile(6) over (partition by c2 order by pk) from t1;
c1 c2 c3 ntile(6) over (partition by c2 order by pk)
Pam Female 6 1
Sara Female 6 1
Mary Female 6 2
Jodi Female 6 2
Pam Female 6 3
Sara Female 6 4
Mary Female 6 5
Jodi Female 6 6
Mark Male 5 1
John Male 5 1
Todd Male 5 2
Ben Male 5 2
Tom Male 5 3
Lucky Male 5 3
Mark Male 5 4
John Male 5 4
Todd Male 5 5
Ben Male 5 5
Tom Male 5 6
Lucky Male 5 6
select c1, c2, c3, ntile(c3) over (partition by c2 order by pk) from t1;
c1 c2 c3 ntile(c3) over (partition by c2 order by pk)
Pam Female 6 1
Sara Female 6 1
Mary Female 6 2
Jodi Female 6 2
Pam Female 6 3
Sara Female 6 4
Mary Female 6 5
Jodi Female 6 6
Mark Male 5 1
John Male 5 1
Todd Male 5 1
Ben Male 5 2
Tom Male 5 2
Lucky Male 5 2
Mark Male 5 3
John Male 5 3
Todd Male 5 4
Ben Male 5 4
Tom Male 5 5
Lucky Male 5 5
update t1 set c3= 1 where pk = 1;
select c1, c2, c3, ntile(c3) over (partition by c2 order by pk) from t1;
ERROR HY000: Argument of NTILE must be greater than 0
drop table t1;
...@@ -168,4 +168,47 @@ select pk, a, b, ...@@ -168,4 +168,47 @@ select pk, a, b,
from t1; from t1;
drop table t1;
--echo #
--echo # MDEV-9911 NTILE must return an error when parameter is not stable
--echo #
create table t1 (
pk int primary key,
c1 nvarchar(10),
c2 nvarchar(10),
c3 int
);
insert into t1 values
(1, 'Mark', 'Male', 5),
(2, 'John', 'Male', 5),
(3, 'Pam', 'Female', 6),
(4, 'Sara', 'Female', 6),
(5, 'Todd', 'Male', 5),
(6, 'Mary', 'Female', 6),
(7, 'Ben', 'Male', 5),
(8, 'Jodi', 'Female', 6),
(9, 'Tom', 'Male', 5),
(10, 'Lucky', 'Male', 5),
(11, 'Mark', 'Male', 5),
(12, 'John', 'Male', 5),
(13, 'Pam', 'Female', 6),
(14, 'Sara', 'Female', 6),
(15, 'Todd', 'Male', 5),
(16, 'Mary', 'Female', 6),
(17, 'Ben', 'Male', 5),
(18, 'Jodi', 'Female', 6),
(19, 'Tom', 'Male', 5),
(20, 'Lucky', 'Male', 5);
# Correct usage of NTILE with a fix argument NTILE(6).
select c1, c2, c3, ntile(6) over (partition by c2 order by pk) from t1;
# Correct usage - constant NTILE (argument) in each partition.
select c1, c2, c3, ntile(c3) over (partition by c2 order by pk) from t1;
update t1 set c3= 1 where pk = 1;
--error ER_INVALID_NTILE_ARGUMENT
select c1, c2, c3, ntile(c3) over (partition by c2 order by pk) from t1;
drop table t1; drop table t1;
...@@ -618,7 +618,8 @@ class Item_sum_ntile : public Item_sum_window_with_row_count ...@@ -618,7 +618,8 @@ class Item_sum_ntile : public Item_sum_window_with_row_count
public: public:
Item_sum_ntile(THD* thd, Item* num_quantiles_expr) : Item_sum_ntile(THD* thd, Item* num_quantiles_expr) :
Item_sum_window_with_row_count(thd, num_quantiles_expr), Item_sum_window_with_row_count(thd, num_quantiles_expr),
current_row_count_(0) {}; current_row_count_(0),
n_old_val_(0) {};
double val_real() double val_real()
{ {
...@@ -635,11 +636,13 @@ class Item_sum_ntile : public Item_sum_window_with_row_count ...@@ -635,11 +636,13 @@ class Item_sum_ntile : public Item_sum_window_with_row_count
longlong num_quantiles= get_num_quantiles(); longlong num_quantiles= get_num_quantiles();
if (num_quantiles <= 0) { if (num_quantiles <= 0 ||
(static_cast<ulonglong>(num_quantiles) != n_old_val_ && n_old_val_ > 0))
{
my_error(ER_INVALID_NTILE_ARGUMENT, MYF(0)); my_error(ER_INVALID_NTILE_ARGUMENT, MYF(0));
return true; return true;
} }
n_old_val_= static_cast<ulonglong>(num_quantiles);
null_value= false; null_value= false;
ulonglong quantile_size = get_row_count() / num_quantiles; ulonglong quantile_size = get_row_count() / num_quantiles;
ulonglong extra_rows = get_row_count() - quantile_size * num_quantiles; ulonglong extra_rows = get_row_count() - quantile_size * num_quantiles;
...@@ -665,6 +668,7 @@ class Item_sum_ntile : public Item_sum_window_with_row_count ...@@ -665,6 +668,7 @@ class Item_sum_ntile : public Item_sum_window_with_row_count
{ {
current_row_count_= 0; current_row_count_= 0;
set_row_count(0); set_row_count(0);
n_old_val_= 0;
} }
const char*func_name() const const char*func_name() const
...@@ -683,6 +687,7 @@ class Item_sum_ntile : public Item_sum_window_with_row_count ...@@ -683,6 +687,7 @@ class Item_sum_ntile : public Item_sum_window_with_row_count
private: private:
longlong get_num_quantiles() { return args[0]->val_int(); } longlong get_num_quantiles() { return args[0]->val_int(); }
ulong current_row_count_; ulong current_row_count_;
ulonglong n_old_val_;
}; };
......
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