fix(postgres): mark the null-extended side of every outer join - #4380
Open
heios wants to merge 2 commits into
Open
fix(postgres): mark the null-extended side of every outer join#4380heios wants to merge 2 commits into
heios wants to merge 2 commits into
Conversation
The planner commutes a plain `LEFT JOIN` into a `Right` join node, and it puts non-join nodes such as `Limit` above joins. Nullability inference marks the wrong side in the first shape, and marks nothing in the second shape.
PostgreSQL defines `JOIN_RIGHT` as "pairs + unmatched RHS tuples", so the Outer child of a `Right` join node is the null-extended side, not the Inner child. `visit_plan` now passes a `null_extended` flag to the null-extended input, and visits every child, so a node such as `Limit` no longer ends the walk.
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.
Does your PR solve an issue?
fixes #367
Is this a breaking change?
Yes. For a column brought in by an outer join, the generated type can widen
from
TtoOption<T>, and it can also narrow fromOption<T>toT.A column on the null-extended side widens to
Option<T>. As in #566, fewcallers should break. Such a column raises
UnexpectedNullat runtime today,unless the query already overrides the column with
foo as "foo?".A column on the preserved side narrows to
T. Code that matches on theOptionmust drop the match, or override the column withfoo as "foo!".Problem
visit_planhas two faults.First, it marks the wrong child of a
Rightjoin node. The planner oftenturns a plain
LEFT JOINinto such a node, so PostgreSQL and sqlx disagreeabout which column is nullable:
PostgreSQL returns
NULLfortweet_reply.text. sqlx reportsnullable: [true, false], which says thattweet.textis the nullable columnand that
tweet_reply.textcannot beNULL. Both answers are wrong.The plan shows why. The planner commuted the
LEFT JOINinto aRightjoinnode:
visit_planmarks the child that carriesParent Relationship: Inner, which isthe
Hashnode overtweet. UnderJoin Type: Rightthat child is thepreserved side, and the
Outerchild is the null-extended side.Parent Relationshipgives a child's position, not the preserved side.ExplainNodelabels the left childOuterand the right childInnerforevery node type.
Join Typeis what names the preserved side.Second fault:
visit_plandescends only through join nodes. ALimit, aSort, anAggregate, or a plain inner join above an outer join ends the walk,and no column is marked at all. This fault also applies to a
Leftjoin node, soselect t.text, r.text from tweet t left join tweet_reply r on ... limit 5reports both columns as
NOT NULL.Fix
visit_plannow passes anull_extendedflag to the null-extended input ofeach outer join, and visits every child.
Testing
I added four cases to
test_describe_outer_join_nullable, and each one failsbefore the fix. Every case asserts the preserved side as well as the
null-extended side. The
limitcase and theorder bycase are the same query,and the planner gives them opposite join types, so the pair covers a
Leftnodeand a
Rightnode. CI covers PostgreSQL 13 and 17. I also ran 14, 15, 16 and 18locally.
I also checked both repros from #367. On the schema in the original report,
describenow marksc2.color_uuidas nullable, which removes theUnexpectedNullErrorthat report shows, and it stops marking the preservedo.object_uuidas nullable. On the repro in the most recent comment,describenow reports[false, true]instead of[true, false].