-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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: support lateral joins #24560
Comments
24561: sql: introduce the LATERAL keyword and mark it as unimplemented r=knz a=knz CockroachDB aims to implement lateral joins. Until then, recognize the syntax and link the error message to the appropriate support issue. This incidentally motivates keeping the keyword "reserved" in the grammar. Release note: None Informs #24489. Refers to #24560. cc @petermattis
@benesch found this:
https://www.postgresql.org/docs/current/static/sql-select.html In other words, a join with a SRF is always lateral. |
I think this is mostly straightforward, save for one minor hiccup, which is that we build our join trees right-to-left:
however, the semantics here imply that they're built left-to-right: an expression with
It's not really possible to globally change the direction we build our joins until we have more thorough join ordering (because it can make hand-optimized queries much slower), so I think we would have to conditionally restructure the join tree in the presence of |
Fixes cockroachdb#24676. Partial fix for cockroachdb#24560. This commit adds support for LATERAL in the FROM clause of a SELECT. LATERAL allows a subquery or SRF to refer to columns in tables earlier in the FROM clause. This is semantically an inner apply join. A hiccup with this is that we build join trees in a FROM clause right-deep, but the semantics of LATERAL force the tree to be left-deep. Changing the default to be left-deep could cause some hand-optimized queries to become slower, so we only change the order if there is a LATERAL subquery. Note that SRFs are always implicitly LATERAL. This commit does *not* add support for LATERAL as a keyword to explicit `JOIN` expressions. Release note (sql change): the LATERAL keyword in a FROM clause is now supported.
Fixes cockroachdb#24676. Partial fix for cockroachdb#24560. This commit adds support for LATERAL in the FROM clause of a SELECT. LATERAL allows a subquery or SRF to refer to columns in tables earlier in the FROM clause. This is semantically an inner apply join. A hiccup with this is that we build join trees in a FROM clause right-deep, but the semantics of LATERAL force the tree to be left-deep. Changing the default to be left-deep could cause some hand-optimized queries to become slower, so we only change the order if there is a LATERAL subquery. Note that SRFs are always implicitly LATERAL. This commit does *not* add support for LATERAL as a keyword to explicit `JOIN` expressions. Release note (sql change): the LATERAL keyword in a FROM clause is now supported.
36613: opt: add support for LATERAL in FROM clause r=justinj a=justinj Fixes #24676. Partial fix for #24560. This commit adds support for LATERAL in the FROM clause of a SELECT. LATERAL allows a subquery or SRF to refer to columns in tables earlier in the FROM clause. This is semantically an inner apply join. A hiccup with this is that we build join trees in a FROM clause right-deep, but the semantics of LATERAL force the tree to be left-deep. Changing the default to be left-deep could cause some hand-optimized queries to become slower, so we only change the order if there is a LATERAL subquery. Note that SRFs are always implicitly LATERAL. This commit does *not* add support for LATERAL as a keyword to explicit `JOIN` expressions. Release note (sql change): the LATERAL keyword in a FROM clause is now supported. Co-authored-by: Justin Jaffray <justin@cockroachlabs.com>
Explicit (using the JOIN keyword) joins that use the LATERAL keyword |
How big a project is this to complete? |
Pretty small, I just need to find the time to wrap it up |
That's exciting! Looking forward to this. |
I believe so! |
A join is "lateral" when its right operand depends on the left operand.
Lateral joins are one of the ways a subquery or relational expression can be correlated.
Lateral joins occur:
SELECT * FROM a, LATERAL b
SELECT generate_series(v) FROM kv
jsonb_object_keys()
, see sql: cannot project json_object_keys() on a json column #26110Note: LATERAL in the join syntax changes the name resolution rules!
For example, the following two queries have very different behavior:
vs.
The text was updated successfully, but these errors were encountered: