SQLite Forum

operator precedence docs missing, suggested clarifications below
Login
### 7 Order of the highest priority items

I think the top items are not quite right.  I offer this analysis and a conclusion below.

Consulting the grammar

```
%left OR.
%left AND.
%right NOT.
%left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
%left GT LE LT GE.
%right ESCAPE.
%left BITAND BITOR LSHIFT RSHIFT.
%left PLUS MINUS.
%left STAR SLASH REM.
%left CONCAT.
%left COLLATE.
%right BITNOT.
```

```
sqlite> select -'1y'||'0x3';
-10x3
sqlite> select (-'1y')||'0x3';
-10x3
sqlite> select -('1y'||'0x3');
-1
```
This indicates that - is stronger than COLLATE, and indeed I think it's grouped with BITNOT.

Similarly

```
sqlite> select ~1||'0x3';
-20x3
sqlite> select (~1)||'0x3';
-20x3
sqlite> select ~(1||'0x3');
-11
```

indicates `~` is stronger than `||`

The priority of `~` and `-` cannot be distinguished because both are prefix operators binding inside out so they may as well be equal and indeed they are at the highest level of all, "BITNOT" in the grammar.

With regard to COLLATE, it is explicity placed higher than concat and lower than ~.

Now as to the `+` operator, you can't tell where it goes by experiment as it does nothing by defintion.

Even if you try to force a numeric conversion like I did with `-` and `~` it doesn't work...

```
sqlite> select +'1x'||'0000x3';
1x0000x3
```

```
sqlite> select +'1x';
1x
```

We have to get a little trickier:

```
sqlite> select -+'1y'||'0x3';
-10x3
sqlite> select -(+'1y')||'0x3'; -- interpretation 1
-10x3
sqlite> select -+('1y'||'0x3'); -- interpretation 2
-1
```

If `+` was weaker than `||` then we would have to have interpretation 2 above. But plainly we get the result
of interpretation 1.  So `+` is not weaker than `||` so we end up +/-/~ all in one group right to left.

Double checking the grammar I find this rule:

```
expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] {...}
```

This makes it clear that BITNOT includes PLUS MINUS and BITNOT.

From here I conclude that highest binding items are in this order

```
* / %   -- the multiply group
||      -- concatenation
COLLATE -- unary suffix
+ - ~   -- unary prefixes
```