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

LATERAL constant string doesn't have JOIN inside #2205

Closed
lephuongbg opened this issue Jun 16, 2020 · 3 comments
Closed

LATERAL constant string doesn't have JOIN inside #2205

lephuongbg opened this issue Jun 16, 2020 · 3 comments

Comments

@lephuongbg
Copy link

JOIN.LATERAL constant string currently only contains 'LATERAL', which causes syntax error with Postgresql. It should be either 'INNER JOIN LATERAL' or simply 'JOIN LATERAL'

JOIN.LEFT_LATERAL is correct though ('LEFT JOIN LATERAL').

@coleifer
Copy link
Owner

coleifer commented Jun 16, 2020

I'm following the examples provided here in pg's doc: https://www.postgresql.org/docs/current/queries-table-expressions.html#QUERIES-LATERAL

I suppose we probably also need to support an inner lateral join, too, though I'm not sure how that would be used.

@coleifer
Copy link
Owner

The way I'm currently going to address this is to add the lateral() method to the select query class.

This gives you the opportunity to use joins more naturally, and signifies only that the subquery being joined should be interpreted as a lateral join, which I think is more "correct" anyways.

Example:

# Here we use a subquery to get the most-recent two tweets by each user.
subq = (Tweet
        .select(Tweet.content, Tweet.timestamp)
        .where(Tweet.user == User.id)
        .order_by(Tweet.timestamp.desc())
        .limit(2)
        .lateral())

# Here we do an inner join on the (lateral) subquery of tweets.
query = (User
         .select(User, subq.c.content)
         .join(subq, on=True)
         .order_by(subq.c.timestamp))

@lephuongbg
Copy link
Author

The example that you referred to with only LATERAL is actually multiple FROM clause, which needs a comma between them.

image

Above clause is equivalent to INNER JOIN LATERAL or just JOIN LATERAL.

I do like the current API of LATERAL though (.join(sq, join_type=JOIN.LATERAL)), and I noticed there is also CROSS JOIN LATERAL too.

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

No branches or pull requests

2 participants