Closed
Description
Describe the bug
It seems to me that sqlalchemy
does not wrap an SQL expression with brackets when the function type_coerce()
is used.
I have the following query
mulExpr = type_coerce((Model.a * Model.b), types.Float).label('mulExpr')
divExpr = (Model.c / mulExpr).label('divExpr')
session.query(divExpr, mulExpr).select_from(Model)
The code generates the following SQL query when I ran it on sqlite
.
SELECT Model.c / Model.a * Model.b AS "divExpr", Model.a * Model.b AS "mulExpr"
FROM listing
However, I would expect the following query instead where the sub-expression Model.a * Model.b
is wrapped by brackets.
SELECT Model.c / (Model.a * Model.b) AS "divExpr", Model.a * Model.b AS "mulExpr"
FROM listing
Note that this issue only occurs when the function type_coerce()
is applied to the sub-expression.
Have a nice day!
Metadata
Metadata
Assignees
Labels
Type
Projects
Relationships
Development
No branches or pull requests
Activity
CaselIT commentedon May 21, 2020
Hi,
You can use self_group to ensure parenthesis are used.
zzzeek commentedon May 21, 2020
self_group is likely what's broken here.
For the moment work around as:
zzzeek commentedon May 21, 2020
here's a test case that shows where this should be consistent:
proposed fix, needs to defer to the inner expression for correct grouped behavior:
CaselIT commentedon May 21, 2020
I'll look into this, since the documentation could also be improved for type_coerce, since it's not super clear why one should use it
CaselIT commentedon May 22, 2020
The documentation is not correct regarding the
anon_1
label:now renders
not
anon_1
as stated in the docsEdit: only in master, 1_3 still has anon_1
CaselIT commentedon May 22, 2020
Interestingly
renders
but the processing happens correctly
zzzeek commentedon May 22, 2020
yeah those anon_1 / label things change quite often, if you don't set an explicit label it's not defined what the format of an anonymous label will be so this is not critical
7 remaining items