Skip to content
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

sql: Add support for JOIN LATERAL syntax #40945

Merged
merged 1 commit into from
Oct 9, 2019

Conversation

chrisseto
Copy link
Contributor

As correlated subqueries are already supported, only minor scoping
changes are required to fully support Postgres' JOIN LATERAL syntax.

h/t @justinj for the support

Release note (sql change): Adds support for JOIN LATERAL syntax.

@chrisseto chrisseto requested a review from a team as a code owner September 20, 2019 18:30
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@justinj justinj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 awesome!! couple nit things

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @chrisseto, @justinj, @RaduBerinde, and @rytaft)


pkg/sql/logictest/testdata/logic_test/subquery_correlated, line 1177 at r1 (raw file):

query ITT rowsort
SELECT
	c_id, bill, states

nit: spaces over tabs, if you use sqlfmt you can add the --use-spaces flag to enforce this, I have this in my vimrc to sqlfmt whatever I have visually selected:

noremap ,s :!cockroach sqlfmt --print-width 80 --use-spaces<cr><cr>

pkg/sql/opt/optbuilder/join.go, line 37 at r1 (raw file):

	isLateral := false
	inScopeRight := inScope
	// Detect if this is a lateral join, upgrade scope to leftScope need be

nit: if need be? also our style guide requires periods at the end of comments

maybe also worth including in the comment something like "the right side of a LATERAL join has in scope the columns produced from the left side" to make explicit why we have to do this?


pkg/sql/opt/optbuilder/join.go, line 370 at r1 (raw file):

		jb.filters,
		&memo.JoinPrivate{Flags: jb.joinFlags},
		false,

nit: false /* isLateral */,


pkg/sql/opt/optbuilder/testdata/lateral, line 292 at r1 (raw file):


build
SELECT * FROM x JOIN LATERAL (SELECT * FROM y WHERE b = x.a) ON true

probably should also test LEFT JOIN LATERAL, and we might also need to error appropriately on use of OUTER JOIN LATERAL and RIGHT JOIN LATERAL (I assume we just let those through at the moment)

Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: after you address @justinj's comments

Reviewed 3 of 3 files at r1.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @chrisseto and @RaduBerinde)

@chrisseto
Copy link
Contributor Author

Feedback addressed!
Full disclosure: The actual error from Postgres is

ERROR:  42P10: invalid reference to FROM-clause entry for table "users"
LINE 1: ...ray_agg(code) FROM user_promo_codes WHERE user_id=users.id) ...
                                                             ^
DETAIL:  The combining JOIN type must be INNER or LEFT for a LATERAL reference.
LOCATION:  check_lateral_ref_ok, parse_relation.c:452

I went with detail message and the syntax code as it seemed more appropriate.
Happy to change it if the other is preferred, I may need some pointers on how to generate it.

Copy link
Contributor

@justinj justinj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: with some nits, though reminder that this can't be merged until post-20.1-branching.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @chrisseto, @RaduBerinde, and @rytaft)


pkg/sql/opt/optbuilder/join.go, line 201 at r2 (raw file):

	case sqlbase.RightOuterJoin:
		if isLateral {
			panic(pgerror.New(pgcode.Syntax, "The combining JOIN type must be INNER or LEFT for a LATERAL reference."))

nit: hoist this error object into a top-level var (like this) and omit the period in the error message (i think lint will complain about the period)

Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 1 of 3 files at r2, 2 of 2 files at r3.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @chrisseto and @RaduBerinde)


pkg/sql/opt/optbuilder/join.go, line 181 at r3 (raw file):

}

var invalidLateralJoin = pgerror.New(pgcode.Syntax, "The combining JOIN type must be INNER or LEFT for a LATERAL reference")

[nit] Is this the error Postgres uses? The word "combining" seems unnecessary here and is a bit confusing. I'd just say "The JOIN type must be..."

@rytaft
Copy link
Collaborator

rytaft commented Oct 1, 2019


pkg/sql/opt/optbuilder/join.go, line 181 at r3 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

[nit] Is this the error Postgres uses? The word "combining" seems unnecessary here and is a bit confusing. I'd just say "The JOIN type must be..."

Never mind just saw your update above

Copy link
Contributor

@justinj justinj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we've branched I think this is good to go after a rebase!

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @chrisseto and @RaduBerinde)

As correlated subqueries are already supported, only minor scoping
changes are required to fully support Postgres' JOIN LATERAL syntax.

h/t @justinj for the support

Release note (sql change): Adds support for JOIN LATERAL syntax.
@chrisseto
Copy link
Contributor Author

bors r+

@craig
Copy link
Contributor

craig bot commented Oct 8, 2019

Build failed (retrying...)

@chrisseto
Copy link
Contributor Author

bors r+

craig bot pushed a commit that referenced this pull request Oct 9, 2019
40945: sql: Add support for JOIN LATERAL syntax r=chrisseto a=chrisseto

As correlated subqueries are already supported, only minor scoping
changes are required to fully support Postgres' JOIN LATERAL syntax.

h/t @justinj for the support

Release note (sql change): Adds support for JOIN LATERAL syntax.

Co-authored-by: Chris Seto <[email protected]>
@craig
Copy link
Contributor

craig bot commented Oct 9, 2019

Build succeeded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants