Skip to content

Commit 5a3a79c

Browse files
committed
MDEV-9637 select nullif(count(col1),0) gives wrong result if in a view
don't do special SUM_FUNC_ITEM treatment in NULLIF for views (as before), but do it for derived tables (when context_analysis_only == CONTEXT_ANALYSIS_ONLY_DERIVED)
1 parent c689e93 commit 5a3a79c

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

mysql-test/r/null.result

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,17 @@ View Create View character_set_client collation_connection
15311531
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select nullif(count(distinct `t1`.`col1`),0) AS `nullif(count(distinct col1),0)` from `t1` latin1 latin1_swedish_ci
15321532
drop view v1;
15331533
drop table t1;
1534+
create table t1 (col1 varchar(50) default null);
1535+
insert into t1 (col1) values ('hello'), ('hello'), ('hello');
1536+
create view v1 as select nullif(count(col1),0) from t1;
1537+
select * from v1;
1538+
nullif(count(col1),0)
1539+
3
1540+
select nullif(count(col1),0) from t1;
1541+
nullif(count(col1),0)
1542+
3
1543+
drop view v1;
1544+
drop table t1;
15341545
#
15351546
# End of 10.1 tests
15361547
#

mysql-test/t/null.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,17 @@ show create view v1;
958958
drop view v1;
959959
drop table t1;
960960

961+
#
962+
# MDEV-9637 select nullif(count(col1),0) gives wrong result if in a view
963+
#
964+
create table t1 (col1 varchar(50) default null);
965+
insert into t1 (col1) values ('hello'), ('hello'), ('hello');
966+
create view v1 as select nullif(count(col1),0) from t1;
967+
select * from v1;
968+
select nullif(count(col1),0) from t1;
969+
drop view v1;
970+
drop table t1;
971+
961972
--echo #
962973
--echo # End of 10.1 tests
963974
--echo #

sql/item_cmpfunc.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2584,7 +2584,8 @@ Item_func_nullif::fix_length_and_dec()
25842584
args[0] and args[2] should still point to the same original l_expr.
25852585
*/
25862586
DBUG_ASSERT(args[0] == args[2] || thd->stmt_arena->is_stmt_execute());
2587-
if (args[0]->type() == SUM_FUNC_ITEM && !thd->lex->context_analysis_only)
2587+
if (args[0]->type() == SUM_FUNC_ITEM &&
2588+
!thd->lex->is_ps_or_view_context_analysis())
25882589
{
25892590
/*
25902591
NULLIF(l_expr, r_expr)

0 commit comments

Comments
 (0)