-
-
Notifications
You must be signed in to change notification settings - Fork 577
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
Try and maintain ordering of functions #2265
Conversation
… into parents when the parent is relying on implicit ordering coming from a function or suitably flagged subquery.
🦋 Changeset detectedLatest commit: 135e8c1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
// Don't want to make this a join as it can result in the order being | ||
// messed up | ||
if (t2.hasImplicitOrder && !this.joinAsLateral && this.isUnique) { | ||
continue; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
22 commits... but this is the bit that matters.
Description
setof
function and relational data #2210For a statement like this:
PostgreSQL does not guarantee that the order of rows returned will be the same as the orders defined in the function - this is because you need an explicit
order by
statement in this statement to guarantee order.However, most of the time, the order is maintained due to the way in which PostgreSQL works... But this goes out the window once you add a join - if PostgreSQL decides it's better to evaluate your joined table first and merge with your function call afterwards, then your order can be lost.
This PR attempts to maintain the order of functions more reliably by refusing to inline PgSelect statements into parent queries if the parent query has originated from a set-returning function, and thus has an implicit order.
Originally (see the commit history) I was hoping that we could use
WITH ORDINALITY
to solve this, but alas this breaks computed column functions since we can no longer pass the resulting record into the function, since it gains anordinality
column. We could work around that, but it'd make the queries really ugly. Another option would be to move all joins to be subqueries instead, like in V4, but no thanks!In conclusion: you should explicitly order your functions... but if you don't, this should hopefully mean order is generally maintained, maybe, probably, we hope 🤞
Performance impact
Deliberately disables inlining of some queries, resulting in additional roundtrips to the PostgreSQL server.
Security impact
Other than performance, none known.
Checklist
yarn lint:fix
passes.yarn test
passes.RELEASE_NOTES.md
file (if one exists).