Closed
Description
From a customer support request:
Issue summary: This issue is closely related to Unnest function in BigQuery SQLAlchemy plugin joins twice when selecting other columns #353. After the previous issue was resolved, queries using the UNNEST function that are not in Common Table Expressions (CTE) or subqueries work as expected. See the following minimal example:
from sqlalchemy import create_engine, MetaData, select, func as F, Table, Column, Integer, ARRAY
metadata = MetaData()
table = Table(
"table1",
metadata,
Column("foo", Integer),
Column("bars", ARRAY(Integer))
)
engine = create_engine("bigquery://", future=True)
q = select(table.c.foo, F.unnest(table.c.bars).column_valued("bar"))
print(q.compile(engine))
This outputs the following, which is expected:
SELECT `table1`.`foo`, `bar`
FROM `table1`, unnest(`table1`.`bars`) AS `bar`
However, if we use the table as a subquery or CTE, the issue manifests again. The following code (continuing from above):
q = q.cte("cte")
q = select(*q.columns)
print(q.compile(engine))
Produces the next output:
WITH `cte` AS
(SELECT `table1`.`foo` AS `foo`, `bar`
FROM `table1`, `table1` `table1_1`, unnest(`table1_1`.`bars`) AS `bar`)
SELECT `cte`.`foo`, `cte`.`bar`
FROM `cte`
Note that table1
is duplicated in the FROM
clause again.
Expected behavior: It is expected to generate the following query instead (note the different FROM clause):
WITH `cte` AS
(SELECT `table1`.`foo`, `bar`
FROM `table1`, unnest(`table1`.`bars`) AS `bar`)
SELECT `cte`.`foo`, `cte`.`bar`
FROM `cte`