[MDEV-31202] expressions with side effects should not be considered const Created: 2023-05-05  Updated: 2023-07-06  Resolved: 2023-07-06

Status: Closed
Project: MariaDB Server
Component/s: N/A
Affects Version/s: 10.3.38
Fix Version/s: N/A

Type: Bug Priority: Minor
Reporter: Leo Zandvliet Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: compat57


 Description   

Short circuit evaluation in IF() statements appear not to work as expected:
https://mariadb.com/kb/en/operator-precedence/#short-circuit-evaluation

Example queries:

SET @imchanged=0;
SELECT IF(FALSE AND (@imchanged:=1), 0, @imchanged) AS `im changed`; # output is '1', expect '0'
 
SET @imchanged=0;
SELECT IF(FALSE, IF((@imchanged:=1), 1, 1), @imchanged) AS `im changed`; # output is '0', expect '0'

Running these in any MySql version outputs are as expected.



 Comments   
Comment by Sergei Golubchik [ 2023-05-06 ]

short circuit evaluation works here all right:

SET @imchanged=0;
SELECT IF(FALSE AND (@imchanged:=rand()), 0, @imchanged) AS `im changed`;

this prints 0. In your case optimizer notices that @imchanged:=1 is a constant expression, no matter how many rows the result will have, this expression will always have the same result of 1. So it evaluates it early and replaces with 1.

It probably shouldn't do it for expressions with side effects, like an assignment.

Comment by Leo Zandvliet [ 2023-05-06 ]

Okay, thanks for your fast response and explanation!

Generated at Thu Feb 08 10:22:03 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.