Skip to content

Issues with precedence in generated SQL #181

Description

@foxbenjaminfox

datetime_add(t, a + b, "day") generates the SQL a + b || ' day'. Because || binds tighter than +, this is not the desired SQL, and always evaluates to NULL. Instead, it should be (a + b) || ' day'. Likewise, perhaps worse, datetime_add(t, a + b, "week") adds (a + b * 7), not (a + b) * 7, days, and similarly for datetime_add(t, a + b, "millisecond").

(date_add also has these issues.)

While investigating the above, I found some additional, though lesser, precedence issues:

is_nil(x and y) becomes x AND y IS NULL (should be (x AND y) IS NULL), is_nil(not x) becomes NOT (x) IS NULL (should be (NOT x) IS NULL), (not x) < y becomes NOT (x) < y (should be (NOT x) < y), and likewise with (not x) in [...] and x == (y in [...]) (becomes x = y IN (...), and = binds tighter than IN).

Also, within update, inc: [x: a + b] has a minor precedence bug that only matters when addition is non-associative (e.g. floating point, or integer overflow.) It becomes UPDATE ... SET x = x + a + b, which means x = (x + a) + b, but it should instead be x = x + (a + b).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions