Skip to content

Commit

Permalink
fix(sqlalchemy): properly handle aliases of extracted subqueries
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist committed Jan 11, 2024
1 parent 5e1efe3 commit 38aaf8f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions ibis/backends/base/sql/alchemy/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ def _format_table(self, op):
elif ctx.is_extracted(op):
if isinstance(orig_op, ops.SelfReference):
result = ctx.get_ref(op)
elif isinstance(alias, str):
result = sa.table(
alias,
*translator._schema_to_sqlalchemy_columns(orig_op.schema),
)
else:
result = alias
else:
Expand Down
6 changes: 3 additions & 3 deletions ibis/backends/base/sql/compiler/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ def get_ref(self, node, search_parents=False):
"""Return the alias used to refer to an expression."""
assert isinstance(node, ops.Node), type(node)

if self.is_extracted(node):
return self.top_context.table_refs.get(node)

if (ref := self.table_refs.get(node)) is not None:
return ref

if self.is_extracted(node):
return self.top_context.table_refs.get(node)

if search_parents and (parent := self.parent) is not None:
return parent.get_ref(node, search_parents=search_parents)

Expand Down

0 comments on commit 38aaf8f

Please sign in to comment.