Closed Andre-lbc closed 3 weeks ago
Good catch! This has probably been broken since forever. Drift adds parentheses if inner expressions have a higher precedence than outer ones:
var x = a + b
return x * 3 // Generates (a + b) * 3
Doing this only when the inner precedence is higher is not correct when non-associative operators are involved. For safety, we should add parentheses if the inner precedence matches the outer one too.
Problem:
I'm encountering an issue with Drift where, under certain circumstances, the generated SQL does not include the necessary parentheses in expressions that should be arithmetically equivalent. This leads to incorrect query results due to operator precedence errors.
Context:
I have a
Tab
table with aposition
column that has a unique constraint. To update positions without violating this constraint (e.g., when moving tabs, deleting multiple tabs, etc), I temporarily assign negative positions to affected rows. After moving the necessary items, I normalize any negative positions back to their final positive positions.Code samples:
Note: Using Constant() instead of Variable() or in addition to it generates the same problems.