Eliminate case expression if all branches are the same either way#360
Open
duairc wants to merge 1 commit intotomjaguarpaw:masterfrom
Open
Eliminate case expression if all branches are the same either way#360duairc wants to merge 1 commit intotomjaguarpaw:masterfrom
duairc wants to merge 1 commit intotomjaguarpaw:masterfrom
Conversation
Owner
|
Hello. Thanks for this. I'm sorry I haven't responded. It must have got lost because it was Christmas time. This sounds like a very nice idea. I'd probably prefer that the optimization was done on the |
Owner
|
The offending line seems to be an |
Owner
I've checked, and yes, that's the issue. I'm a bit surprised that Postgres itself doesn't do this. Anyway, I'm happy to add the optimization but it should be done on the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Okay, so this is a fairly simple fix, but I'm not sure it's the right place to fix this issue. I've attached a .zip file with a bunch of files that show an example of when this is an issue.
Basically, since PostgreSQL 9.0, PostgreSQL has the ability to optimise away outer joins in certain cases if none of the columns in the outer join are actually used in the end.
The problem is that when you use
Opaleye.FunctionalJoin, it generates a CASE expression against every such column, even if the result is the same regardless. From the PostgreSQL's point of view, the value of the column is "needed" then, so it can't optimise away the JOIN.This commit just makes Opaleye not generate the CASE expressions in the first place if every branch has the same result either way, which means PostgresSQL doesn't do the JOINs.
The .zip file I've attached contains a simple test case schema, a file with the appropriate Opaleye boilerplate for that schema, and the result of doing EXPLAIN on respective:
These show that the JOIN is optimised away in the first and third cases.