SQLite Forum

operator precedence

operator precedence

(1) By curmudgeon on 2020-07-05 12:36:44 [link] [source]

Re the bit operators

<<   >>   &    |

C gives the shift operators the higher precedence but they are all at the same level for sqlite (which I think I'm right in saying follows the SQL convention). Can it be assumed they will always be evaluated in the (left to right) order they appear in the statement?


select 5 & 1 << 1

returns 2

whereas in c it would return 0.

(2.1) By Larry Brasfield (LarryBrasfield) on 2020-07-05 15:14:17 edited from 2.0 in reply to 1 [source]

Oddly, the docs do not speak of association for most of the binary operators, not even at Operators. I would consider that an improvement opportunity.

The parser favors left association, which reduces stack depth, and SQLite explicitly [a] treats the binary operators of equal precedence as left-associative. Because of the backwards compatibility policy long imposed on the project, users can be quite confident that this associativity is not going to change. If SQLite differed from other DBMS's in this regard, that might be less certain, but it does not.

[a. The Lemon parser's "%left" directive is used in the SQLite3 grammar rules (parse.y) to specify operator associativity; it is not left to the parser generator's default. ]

For the same reasons, and additionally because it has been stated in the docs, the precedence of operators [b] is highly unlikely to change.

[b. Of course, precedence takes higher precedence than associativity. ]

(Edited to clear up an unintended ambiguity. ]

(3) By curmudgeon on 2020-07-05 14:40:00 in reply to 2.0 [link] [source]

Thanks Larry.