Is MySQL arithmetic plus left-to-right?

102 Views Asked by At

Following my comment over at How to recalculate a field on GROUP where I try to cheat the

However, the order of evaluation for expressions involving user variables is undefined.

curse of user variables by using the plus operator in the following fashion:

SELECT @a + LEAST(0, @a:= @a + 1)

Using LEAST / GREATEST in this fashion is of course not my idea.

I have tried to find documentation on the execution order of the plus operator but curiously enough, I can't. The Arithmetic Operators manual page has nothing on the topic neither has Operator Precedence. So, do plus operations always run from left to right or is it also undefined?

1

There are 1 best solutions below

5
On

A little digging finds this MySQL bug which contains this YACC snippet (which of course can be found in the real source code tree, for eg. here ):

%left   '-' '+'

There we go! It is undocumented but the source easily trumps documentation.