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

table not available error for query with LEFT JOIN LATERAL #26

Closed
valichek opened this issue Mar 17, 2023 · 3 comments
Closed

table not available error for query with LEFT JOIN LATERAL #26

valichek opened this issue Mar 17, 2023 · 3 comments

Comments

@valichek
Copy link
Contributor

sqlvet fails with:

ERROR: table `w` not available for query

on query similar to:

SELECT d.id, coalesce(w.w_count,0) as w_count FROM my_table AS d
LEFT JOIN LATERAL ( 
    SELECT count(*) as w_count FROM my_another_table WHERE d.id = ext_id AND deleted_at IS NULL
) w ON true
WHERE d.deleted_at IS NULL;
@samiam2013
Copy link
Collaborator

samiam2013 commented Mar 18, 2023

Can you alias with AS w? or is that disallowed by postgres/does it fail in the same way?

@valichek
Copy link
Contributor Author

Can you alias with AS w? or is that disallowed by postgres/does it fail in the same way?

Using AS w is valid option for postgres with same result, anyway it still fails with the same sqlvet error

SELECT d.id, coalesce(w.w_count,0) as w_count FROM my_table AS d
LEFT JOIN LATERAL ( 
    SELECT count(*) as w_count FROM my_another_table WHERE d.id = ext_id AND deleted_at IS NULL
) AS w ON true
WHERE d.deleted_at IS NULL;

@houqp
Copy link
Owner

houqp commented Mar 20, 2023

I think the join clause parsing logic needs to be extended to handle table aliases so w can be registered as a table in ctx.schema on the fly:

func getUsedColumnsFromJoinClauses(fromClauseList []*pg_query.Node) []ColumnUsed {

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

3 participants