Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
MariaDB
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
MariaDB
Commits
960b221c
Commit
960b221c
authored
Apr 04, 2016
by
Vicențiu Ciorbaru
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert ntile to work with expressions as parameters.
parent
be3902fc
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
126 additions
and
9 deletions
+126
-9
mysql-test/r/win_ntile.result
mysql-test/r/win_ntile.result
+75
-0
mysql-test/t/win_ntile.test
mysql-test/t/win_ntile.test
+24
-0
sql/item_windowfunc.h
sql/item_windowfunc.h
+26
-3
sql/sql_yacc.yy
sql/sql_yacc.yy
+1
-6
No files found.
mysql-test/r/win_ntile.result
View file @
960b221c
...
...
@@ -357,4 +357,79 @@ pk a b ntile(20) over (partition by b order by pk)
17 2 20 3
19 4 20 4
20 4 20 5
select pk, a, b,
ntile(1 + 3) over (partition by b order by pk)
from t1;
pk a b ntile(1 + 3) over (partition by b order by pk)
11 0 10 1
12 0 10 1
13 1 10 2
14 1 10 3
18 2 10 4
15 2 20 1
16 2 20 1
17 2 20 2
19 4 20 3
20 4 20 4
select pk, a, b,
ntile((select 4)) over (partition by b order by pk)
from t1;
pk a b ntile((select 4)) over (partition by b order by pk)
11 0 10 1
12 0 10 1
13 1 10 2
14 1 10 3
18 2 10 4
15 2 20 1
16 2 20 1
17 2 20 2
19 4 20 3
20 4 20 4
select t1.a from t1 where pk = 11;
a
0
select pk, a, b,
ntile((select a from t1 where pk=11)) over (partition by b order by pk)
from t1;
ERROR HY000: Argument of NTILE must be greater than 0
select t1.a from t1 where pk = 13;
a
1
select pk, a, b,
ntile((select a from t1 where pk=13)) over (partition by b order by pk)
from t1;
pk a b ntile((select a from t1 where pk=13)) over (partition by b order by pk)
11 0 10 1
12 0 10 1
13 1 10 1
14 1 10 1
18 2 10 1
15 2 20 1
16 2 20 1
17 2 20 1
19 4 20 1
20 4 20 1
explain
select pk, a, b,
ntile((select a from t1 where pk=13)) over (partition by b order by pk)
from t1;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 10 Using temporary
2 SUBQUERY t1 const PRIMARY PRIMARY 4 const 1
select a from t1;
a
0
0
1
1
2
2
2
2
4
4
select pk, a, b,
ntile((select a from t1)) over (partition by b order by pk)
from t1;
ERROR 21000: Subquery returns more than 1 row
drop table t1;
mysql-test/t/win_ntile.test
View file @
960b221c
...
...
@@ -137,11 +137,35 @@ select pk, a, b,
ntile
(
20
)
over
(
partition
by
b
order
by
pk
)
from
t1
;
select
pk
,
a
,
b
,
ntile
(
1
+
3
)
over
(
partition
by
b
order
by
pk
)
from
t1
;
select
pk
,
a
,
b
,
ntile
((
select
4
))
over
(
partition
by
b
order
by
pk
)
from
t1
;
select
t1
.
a
from
t1
where
pk
=
11
;
--
error
ER_INVALID_NTILE_ARGUMENT
select
pk
,
a
,
b
,
ntile
((
select
a
from
t1
where
pk
=
11
))
over
(
partition
by
b
order
by
pk
)
from
t1
;
select
t1
.
a
from
t1
where
pk
=
13
;
select
pk
,
a
,
b
,
ntile
((
select
a
from
t1
where
pk
=
13
))
over
(
partition
by
b
order
by
pk
)
from
t1
;
explain
select
pk
,
a
,
b
,
ntile
((
select
a
from
t1
where
pk
=
13
))
over
(
partition
by
b
order
by
pk
)
from
t1
;
select
a
from
t1
;
--
error
ER_SUBQUERY_NO_1_ROW
select
pk
,
a
,
b
,
ntile
((
select
a
from
t1
))
over
(
partition
by
b
order
by
pk
)
from
t1
;
drop
table
t1
;
sql/item_windowfunc.h
View file @
960b221c
...
...
@@ -235,7 +235,10 @@ class Item_sum_window_with_row_count : public Item_sum_num
{
public:
Item_sum_window_with_row_count
(
THD
*
thd
)
:
Item_sum_num
(
thd
),
partition_row_count_
(
0
){}
partition_row_count_
(
0
)
{}
Item_sum_window_with_row_count
(
THD
*
thd
,
Item
*
arg
)
:
Item_sum_num
(
thd
,
arg
),
partition_row_count_
(
0
)
{};
void
set_row_count
(
ulonglong
count
)
{
partition_row_count_
=
count
;
}
...
...
@@ -398,8 +401,9 @@ class Item_sum_cume_dist: public Item_sum_window_with_row_count
class
Item_sum_ntile
:
public
Item_sum_window_with_row_count
{
public:
Item_sum_ntile
(
THD
*
thd
,
ulong
num_quantiles
)
:
Item_sum_window_with_row_count
(
thd
),
num_quantiles_
(
num_quantiles
),
Item_sum_ntile
(
THD
*
thd
,
Item
*
num_quantiles_expr
)
:
Item_sum_window_with_row_count
(
thd
,
num_quantiles_expr
),
num_quantiles_expr_
(
num_quantiles_expr
),
num_quantiles_
(
0
),
current_row_count_
(
0
)
{};
double
val_real
()
...
...
@@ -451,7 +455,26 @@ class Item_sum_ntile : public Item_sum_window_with_row_count
enum
Item_result
result_type
()
const
{
return
INT_RESULT
;
}
enum_field_types
field_type
()
const
{
return
MYSQL_TYPE_LONGLONG
;
}
bool
fix_fields
(
THD
*
thd
,
Item
**
ref
)
{
if
(
Item_sum_window_with_row_count
::
fix_fields
(
thd
,
ref
))
return
true
;
// TODO-cvicentiu is ref as a parameter here ok?
if
(
!
num_quantiles_expr_
->
fixed
)
num_quantiles_expr_
->
fix_fields
(
thd
,
ref
);
longlong
expr_value
=
num_quantiles_expr_
->
val_int
();
if
(
expr_value
<=
0
)
{
my_error
(
ER_INVALID_NTILE_ARGUMENT
,
MYF
(
0
));
return
true
;
}
num_quantiles_
=
static_cast
<
ulong
>
(
expr_value
);
return
false
;
}
private:
Item
*
num_quantiles_expr_
;
ulong
num_quantiles_
;
ulong
current_row_count_
;
};
...
...
sql/sql_yacc.yy
View file @
960b221c
...
...
@@ -10541,13 +10541,8 @@ simple_window_func:
MYSQL_YYABORT;
}
|
NTILE_SYM '('
int_num
')'
NTILE_SYM '('
expr
')'
{
if ($3 <= 0)
{
my_error(ER_INVALID_NTILE_ARGUMENT, MYF(0));
MYSQL_YYABORT;
}
$$= new (thd->mem_root) Item_sum_ntile(thd, $3);
if ($$ == NULL)
MYSQL_YYABORT;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment