Calculated columns in select (and where) not respecting brackets

Issue #144 resolved
Former user created an issue

(original reporter: rtl) When returning calculated columns the brackets are being ignored, with the resulting math being incorrect, e.g. the following test is returning "SELECT values.id, values.val2 - values.val1 / values.val1 FROM values" i.e. missing any brackets and a similar problem exists in the WHERE clause.

def testcalculatedcolumns(self):
    value_tbl = table('values',
        Column('id', Integer),
        Column('val1', Float),
        Column('val2', Float),                       
    )

    self.runtest(
        select([(value_tbl.c.val2 - value_tbl.c.val1)/value_tbl.c.val1](value_tbl.c.id,)), 
        "SELECT values.id, (values.val2 - values.val1) / values.val1 FROM values"
    )

    self.runtest(
        select([value_tbl.c.id](value_tbl.c.id), (value_tbl.c.val2 - value_tbl.c.val1)/value_tbl.c.val1 > 2.0), 
        "SELECT values.id FROM values WHERE (values.val2 - values.val1) / values.val1 > :literal"
    )

Comments (1)

  1. Mike Bayer repo owner

    I put a pretty simple rule in that hopefully covers all the bases (doesnt cover booleans like AND and OR but those use a slightly different rule), which is just: binary clause, if left is also a binary then parenthesize it, if right is also a binary then parenthesize it.

    this has the effect of the natural python order of operators manifesting itself as parenthesis to make the order explicit in all cases. i would be a little concerned about excess parenthesis but it seems to hold up pretty well so far, its in changeset:1236.

  2. Log in to comment