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

fix: SQL name resolving #5860

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion edb/pgsql/resolver/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ def _lookup_in_table(
def _lookup_table(tab_name: str, ctx: Context) -> context.Table:
matched_tables: List[context.Table] = []
for t in ctx.scope.tables:
if t.name == tab_name or t.alias == tab_name:
t_name = t.alias or t.name
if t_name == tab_name:
matched_tables.append(t)

if not matched_tables:
Expand Down
22 changes: 21 additions & 1 deletion tests/test_sql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ async def test_sql_query_11(self):
res = await self.scon.fetch(
'''
SELECT * FROM "Movie"
JOIN "Genre" g ON "Movie".genre_id = "Genre".id
JOIN "Genre" g ON "Movie".genre_id = g.id
'''
)
self.assert_shape(res, 2, 7)
Expand Down Expand Up @@ -566,6 +566,26 @@ async def test_sql_query_37(self):
)
self.assertEqual(res, [['24']])

async def test_sql_query_38(self):
res = await self.squery_values(
'''
WITH users AS (
SELECT 1 as id, NULL as managed_by
UNION ALL
SELECT 2 as id, 1 as managed_by
)
SELECT id, (
SELECT id FROM users e WHERE id = users.managed_by
) as managed_by
FROM users
ORDER BY id
'''
)
self.assertEqual(res, [
[1, None],
[2, 1],
])

async def test_sql_query_introspection_00(self):
dbname = self.con.dbname
res = await self.squery_values(
Expand Down