-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
add "expressions" accessor to column attributes for special GROUP BY situations #5262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
total news to me that PG allows GROUP BY primary key with other columns present not in the GROUP BY. but there you go and it's SQL standard. what you want to do here is this:
we can add some accessor for that or MAYBE get an ORM group_by to figure that out, but we are in the process or removing automatic magical things from ORM queries right now including GROUP BY things, so, not clear if that should not remain explciit because this is an extremely unusual use case.
Already done, seven years ago:
Some kind of InstrumentedAttribute accessor is definitely doable here. Here it is for now:
this is used as: this query proceeds and returns:
is this the right SQL below?
|
here's a patch, really simple
we get some test or two written for that you can have it in 1.3.17. |
Totally understand! The accessor is probably what I'd prefer anyway over some magic done by group_by().
Excellent!
That query looks good, subject to my comments about the names above. |
I guess I don't understand how to use the patch. I applied it and tried:
but still got:
(the aliases were not used in the |
that didn't seem to be a very important behavior and it might be difficult to add. it also doesn't match any existing naming convention that SQLAlchemy uses , that is "myname_tablename", and I'm hesitant to add a whole new one for this special case. The "name" refers to the name of the subquery, but in the case of "flat", there is no subquery. I'm not sure if there are complications which could arise from this, seems unlikely but nothing surprises me. PRs with complete tests welcome of course, here's how it looks:
the behavior is standard SQL and has nothing to do with Postgresql.
the patch adds a new attribute .expressions, so it works like this:
|
|
patch is against master. here is 1.3, just remove the "self.prop.key" part:
|
That worked perfectly! thanks
Maybe I'll play with this. Re: the nameing convention. I wouldn't mind something else if you have an idea, but what I proposed actually matches the
A quick implementation against
Pending your input on naming, I'll see what tests may be involved and work up a PR. |
Perhaps this should be a separate issue, but is there some sort of global configuration for the |
FYI, you probably know this, but the flat style is significantly faster on postgres (probably because the subquery style imposes optimization fences on the query planner). Here are the results (in seconds) from a particularly nasty query (procedurally generated) with and without flat joins. This seems like good motivation to provide an easier way (perhaps default without breaking compatibility as described above) to use the flat style globally without adding flat=False flat=True |
no, that's definitely not how we would do things here. doing "global" API changes like that is a recipe for disaster. since this is Python, do the "global" thing on your end:
when the subquery thing came out, SQLite couldn't nest JOINs together. now it can and it's likely every database can. so we'd want to look towards changing the default at some point. |
I think the bigger perspective is that joined table inheritance is already a special case that generates pretty elaborate queries regardless that are usually going to need tuning. we should change this default however this whole use case is fairly niche so it has not been a priority. |
Understood. Thanks for the excellent support! I'll wrap aliased for now and
look forward to the default changing in the future. Are you hoping that
I'll write some tests for the expressions accessor and should I closed this
issues or do you do that?
…On Fri, Apr 17, 2020, 6:09 PM mike bayer ***@***.***> wrote:
FYI, you probably know this, but the flat style is significantly faster on
postgres (probably because the subquery style imposes optimization fences
on the query planner). Here are the results (in seconds) from a
particularly nasty query (procedurally generated) with and without flat
joins. This seems like good motivation to provide an easier way (perhaps
default without breaking compatibility as described above) to use the flat
style globally without adding flat=True across your whole code base.
flat=False
0.3603609240017249
30.251361631999316 ordered
flat=True
0.19413214000087464
0.7021742640063167 ordered
I think the bigger perspective is that joined table inheritance is already
a special case that generates pretty elaborate queries regardless that are
usually going to need tuning. we should change this default however this
whole use case is fairly niche so it has not been a priority.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#5262 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAVJ6ECGEFFJOCEZ2BUSGJTRNDVTLANCNFSM4MKM665Q>
.
|
oh, the patch has to be turned into something yes, we have folks here who might pick it up, the ORM tests are a little crufty to work with. if you want to propose a PR feel free, I'd look into test/orm/inheritance/test_basic.py. just something very quick to see that an .id attrifbute with two columns comes out as the two. |
Mike Bayer has proposed a fix for this issue in the master branch: Add ColumnProperty.Comparator.expressions https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/1906 |
Mike Bayer has proposed a fix for this issue in the rel_1_3 branch: Add ColumnProperty.Comparator.expressions https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/1907 |
Added an accessor :attr:`.ColumnProperty.Comparator.expressions` which provides access to the group of columns mapped under a multi-column :class:`.ColumnProperty` attribute. Fixes: #5262 Change-Id: I44cf53ff0e6cf76a0c90eee4638ca96da3df8088 (cherry picked from commit d9d724267afe867984a23abaa87f62e15786005f)
why did this close...hm.. welp reopening |
oh. this was closed. so confused, the PR is for a new issue that is as yet undefined |
Included below is a test case that demonstrates the problem with inline comments and a suggested solution.
Python 3.8.2
SQLAlchemy 1.3.16
tested with Postgres 12.2 and 9.6.6 (create a db named group_by)
The text was updated successfully, but these errors were encountered: