Commit 8364a646 authored by evgen@moonbone.local's avatar evgen@moonbone.local

Fixed bug#15950: NOW() optimized away in VIEWs

This bug is a side-effect of bug fix #16377. NOW() is optimized in
BETWEEN to integer constants to speed up query execution. When view is being
created it saves already modified query and thus becomes wrong.

The agg_cmp_type() function now substitutes constant result DATE/TIME functions 
for their results only if the current query isn't CREATE VIEW or SHOW CREATE
VIEW.
parent 612c072b
...@@ -2850,3 +2850,10 @@ Tables_in_test ...@@ -2850,3 +2850,10 @@ Tables_in_test
t1 t1
DROP TABLE t1; DROP TABLE t1;
DROP VIEW IF EXISTS v1; DROP VIEW IF EXISTS v1;
create table t1 (f1 datetime);
create view v1 as select * from t1 where f1 between now() and now() + interval 1 minute;
show create view v1;
View Create View
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`f1` AS `f1` from `t1` where (`t1`.`f1` between now() and (now() + interval 1 minute))
drop view v1;
drop table t1;
...@@ -2718,3 +2718,12 @@ DROP TABLE t1; ...@@ -2718,3 +2718,12 @@ DROP TABLE t1;
--disable_warnings --disable_warnings
DROP VIEW IF EXISTS v1; DROP VIEW IF EXISTS v1;
--enable_warnings --enable_warnings
#
# Bug #15950: NOW() optimized away in VIEWs
#
create table t1 (f1 datetime);
create view v1 as select * from t1 where f1 between now() and now() + interval 1 minute;
show create view v1;
drop view v1;
drop table t1;
...@@ -125,6 +125,13 @@ static void agg_cmp_type(THD *thd, Item_result *type, Item **items, uint nitems) ...@@ -125,6 +125,13 @@ static void agg_cmp_type(THD *thd, Item_result *type, Item **items, uint nitems)
uchar null_byte; uchar null_byte;
Field *field= NULL; Field *field= NULL;
/*
Do not convert items while creating a or showing a view in order
to store/display the original query in these cases.
*/
if (thd->lex->sql_command != SQLCOM_CREATE_VIEW &&
thd->lex->sql_command != SQLCOM_SHOW_CREATE)
{
/* Search for date/time fields/functions */ /* Search for date/time fields/functions */
for (i= 0; i < nitems; i++) for (i= 0; i < nitems; i++)
{ {
...@@ -152,6 +159,7 @@ static void agg_cmp_type(THD *thd, Item_result *type, Item **items, uint nitems) ...@@ -152,6 +159,7 @@ static void agg_cmp_type(THD *thd, Item_result *type, Item **items, uint nitems)
break; break;
} }
} }
}
if (field) if (field)
{ {
/* Check the rest of the list for presence of a string field/function. */ /* Check the rest of the list for presence of a string field/function. */
......
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