Commit d35aea54 authored by Igor Babaev's avatar Igor Babaev

Fixed bugs mdev-12051, mdev-10885.

These are different bugs, but the fixing code is the same:
if window functions are used over implicit grouping then
now the execution should follow the general path calling
the function set in JOIN::first_select.
parent f04efa83
...@@ -2850,6 +2850,78 @@ CREATE TABLE t1 (i INT); ...@@ -2850,6 +2850,78 @@ CREATE TABLE t1 (i INT);
INSERT INTO t1 VALUES (3), (1), (2); INSERT INTO t1 VALUES (3), (1), (2);
SELECT i, ROW_NUMBER() OVER () FROM t1 WHERE 1 = 2; SELECT i, ROW_NUMBER() OVER () FROM t1 WHERE 1 = 2;
i ROW_NUMBER() OVER () i ROW_NUMBER() OVER ()
NULL 1
SELECT i, COUNT(*) OVER () FROM t1 WHERE 1 = 2; SELECT i, COUNT(*) OVER () FROM t1 WHERE 1 = 2;
i COUNT(*) OVER () i COUNT(*) OVER ()
NULL 1
DROP TABLE t1; DROP TABLE t1;
#
# MDEV-12051: window function in query with implicit grouping
# on always empty set
#
create table t1 (a int, b varchar(8));
insert into t1 values (1,'foo'),(2,'bar');
select max(a), row_number() over () from t1 where a > 10;
max(a) row_number() over ()
NULL 1
select max(a), sum(max(a)) over () from t1 where a > 10;
max(a) sum(max(a)) over ()
NULL NULL
select max(a), sum(max(a)) over (partition by max(a)) from t1 where a > 10;
max(a) sum(max(a)) over (partition by max(a))
NULL NULL
select max(a), row_number() over () from t1 where 1 = 2;
max(a) row_number() over ()
NULL 1
select max(a), sum(max(a)) over () from t1 where 1 = 2;
max(a) sum(max(a)) over ()
NULL NULL
select max(a), sum(max(a)) over (partition by max(a)) from t1 where 1 = 2;
max(a) sum(max(a)) over (partition by max(a))
NULL NULL
select max(a), row_number() over () from t1 where 1 = 2
having max(a) is not null;
max(a) row_number() over ()
select max(a), sum(max(a)) over () from t1 where 1 = 2
having max(a) is not null;
max(a) sum(max(a)) over ()
drop table t1;
#
# MDEV-10885: window function in query with implicit grouping
# with constant condition evaluated to false
#
CREATE TABLE t1 (a INT, b VARCHAR(8));
INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
CREATE TABLE t2 (c INT);
INSERT INTO t2 VALUES (3),(4);
CREATE TABLE t3 (d INT);
INSERT INTO t3 VALUES (5),(6);
SELECT MAX(a), ROW_NUMBER() OVER (PARTITION BY MAX(a)) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );
MAX(a) ROW_NUMBER() OVER (PARTITION BY MAX(a))
NULL 1
SELECT MAX(a), COUNT(MAX(a)) OVER (PARTITION BY MAX(a)) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );
MAX(a) COUNT(MAX(a)) OVER (PARTITION BY MAX(a))
NULL 0
SELECT MAX(a), SUM(MAX(a)) OVER (PARTITION BY MAX(a)) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );
MAX(a) SUM(MAX(a)) OVER (PARTITION BY MAX(a))
NULL NULL
SELECT MAX(a), ROW_NUMBER() OVER (PARTITION BY MAX(a)) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) )
HAVING MAX(a) IS NOT NULL;
MAX(a) ROW_NUMBER() OVER (PARTITION BY MAX(a))
SELECT a, MAX(a), ROW_NUMBER() OVER (PARTITION BY b) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );
a MAX(a) ROW_NUMBER() OVER (PARTITION BY b)
NULL NULL 1
SELECT a, COUNT(a), AVG(a) OVER (PARTITION BY b) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );
a COUNT(a) AVG(a) OVER (PARTITION BY b)
NULL 0 NULL
SELECT a, MAX(a), AVG(a) OVER (PARTITION BY b) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );
a MAX(a) AVG(a) OVER (PARTITION BY b)
NULL NULL NULL
DROP TABLE t1,t2,t3;
...@@ -1651,3 +1651,63 @@ SELECT i, COUNT(*) OVER () FROM t1 WHERE 1 = 2; ...@@ -1651,3 +1651,63 @@ SELECT i, COUNT(*) OVER () FROM t1 WHERE 1 = 2;
DROP TABLE t1; DROP TABLE t1;
--echo #
--echo # MDEV-12051: window function in query with implicit grouping
--echo # on always empty set
--echo #
create table t1 (a int, b varchar(8));
insert into t1 values (1,'foo'),(2,'bar');
select max(a), row_number() over () from t1 where a > 10;
select max(a), sum(max(a)) over () from t1 where a > 10;
select max(a), sum(max(a)) over (partition by max(a)) from t1 where a > 10;
select max(a), row_number() over () from t1 where 1 = 2;
select max(a), sum(max(a)) over () from t1 where 1 = 2;
select max(a), sum(max(a)) over (partition by max(a)) from t1 where 1 = 2;
select max(a), row_number() over () from t1 where 1 = 2
having max(a) is not null;
select max(a), sum(max(a)) over () from t1 where 1 = 2
having max(a) is not null;
drop table t1;
--echo #
--echo # MDEV-10885: window function in query with implicit grouping
--echo # with constant condition evaluated to false
--echo #
CREATE TABLE t1 (a INT, b VARCHAR(8));
INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
CREATE TABLE t2 (c INT);
INSERT INTO t2 VALUES (3),(4);
CREATE TABLE t3 (d INT);
INSERT INTO t3 VALUES (5),(6);
SELECT MAX(a), ROW_NUMBER() OVER (PARTITION BY MAX(a)) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );
SELECT MAX(a), COUNT(MAX(a)) OVER (PARTITION BY MAX(a)) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );
SELECT MAX(a), SUM(MAX(a)) OVER (PARTITION BY MAX(a)) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );
SELECT MAX(a), ROW_NUMBER() OVER (PARTITION BY MAX(a)) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) )
HAVING MAX(a) IS NOT NULL;
SELECT a, MAX(a), ROW_NUMBER() OVER (PARTITION BY b) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );
SELECT a, COUNT(a), AVG(a) OVER (PARTITION BY b) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );
SELECT a, MAX(a), AVG(a) OVER (PARTITION BY b) FROM t1
WHERE EXISTS ( SELECT * FROM t2 WHERE c IN ( SELECT MAX(d) FROM t3 ) );
DROP TABLE t1,t2,t3;
...@@ -3342,13 +3342,21 @@ void JOIN::exec_inner() ...@@ -3342,13 +3342,21 @@ void JOIN::exec_inner()
if (zero_result_cause) if (zero_result_cause)
{ {
(void) return_zero_rows(this, result, select_lex->leaf_tables, if (select_lex->have_window_funcs())
*columns_list, {
send_row_on_empty_set(), const_tables= table_count;
select_options, first_select= sub_select_postjoin_aggr;
zero_result_cause, }
having ? having : tmp_having, all_fields); else
DBUG_VOID_RETURN; {
(void) return_zero_rows(this, result, select_lex->leaf_tables,
*columns_list,
send_row_on_empty_set(),
select_options,
zero_result_cause,
having ? having : tmp_having, all_fields);
DBUG_VOID_RETURN;
}
} }
/* /*
......
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