You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As pointed out by the bug reporter, premature optimization leads us to
lose information about the ORDER BY, so that when the view information
is reparsed for use in a SELECT from the view, we see only an empty
table and accordingly produce an empty result set. Without the view
present, we see a single result row for an implicitly grouped query -
as expected.
The optimization removed the ORDER BY when resolving the query of the
view, since it is redundant in the presence of a single row result
set. Normally (when no view is involved) we keep the information that
the query is implicitly grouped even though the ORDER BY is been
optimized away. This is information lost for the view, hence the bug.
Solution: defer the optimization when analyzing the view until we
re-resolve it at view use time, i.e. we make sure the ORDER BY in the
view's definition isn't optimized away.
Change-Id: I22fd3d6a27aba9fb413748234f26667d2b1d9f87
v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select 1 AS `1` from `t1` order by count(0) OVER (PARTITION BY min(`t1`.`c0`) ) utf8mb4 utf8mb4_0900_ai_ci
4982
+
DROP VIEW v;
4983
+
# returns two rows as expected
4984
+
(SELECT 1 AS one FROM t1 ORDER BY COUNT(*) OVER (PARTITION BY MIN(t1.c0)))
4985
+
UNION ALL
4986
+
(SELECT 1 FROM t1 ORDER BY COUNT(*) OVER (PARTITION BY MIN(t1.c0)));
4987
+
one
4988
+
1
4989
+
1
4990
+
CREATE VIEW v AS (SELECT 1 AS one FROM t1 ORDER BY COUNT(*) OVER (PARTITION BY MIN(t1.c0)))
4991
+
UNION ALL
4992
+
(SELECT 1 FROM t1 ORDER BY COUNT(*) OVER (PARTITION BY MIN(t1.c0)));
v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS (select 1 AS `one` from `t1` order by count(0) OVER (PARTITION BY min(`t1`.`c0`) ) ) union all (select 1 AS `1` from `t1` order by count(0) OVER (PARTITION BY min(`t1`.`c0`) ) ) utf8mb4 utf8mb4_0900_ai_ci
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select 1 AS `1` union select 3 AS `3` order by `1` utf8mb4 utf8mb4_0900_ai_ci
2515
+
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS (select 1 AS `1` order by '') union (select 3 AS `3` order by '') order by `1` utf8mb4 utf8mb4_0900_ai_ci
2516
2516
DROP VIEW v1;
2517
2517
#
2518
2518
# Bug#29871803: REQUIRED PARENTHESES NO LONGER PRINTED FOR CERTAIN UNION
0 commit comments