Description
Following query fails in the month of December:-
select NOW(), MONTH(NOW()) m, CASE WHEN MONTH(NOW()) = 12 THEN TO_TIME(YEAR(NOW()) || '-12-31 23:59:59.999') ELSE TO_TIME(YEAR(NOW()) || '-' || ( MONTH(NOW()) + 1 ) || '-01 23:59:59.999') - 1 END AS this_month_end
It is due to an optimization we have during compilation where we evaluate the expression if they result in to constant so that we don't need to do it for every row.
Currently parsing stack evaluates every expression if possible without considering any condition, resulting in evaluation of all three expression for CASE node, MONTH(NOW()) = 12 , TO_TIME(YEAR(NOW()) || '12-31 23:59:59.999') ,TO_TIME(YEAR(NOW()) || '' || ( MONTH(NOW()) + 1 ) || '-01 23:59:59.999') - 1) but evaluation of 3rd one will fail because of invalid month.
Workaround: For the particular use-case , Following query though help in preventing the expressions of WHEN CASE to be evaluated to a constant at compile time.