Skip to content
This repository was archived by the owner on May 16, 2026. It is now read-only.
This repository was archived by the owner on May 16, 2026. It is now read-only.

Unnest function in BigQuery SQLAlchemy plugin joins twice when selecting other columns #353

Closed

Description

@tswast

From a customer support request:

Using the UNNEST function generates a CROSS JOIN with a table to itself.
A minimal example is included below. The following Python code:

from sqlalchemy import create_engine, MetaData, select, func as F, Table, Column, Integer, ARRAY
metadata = MetaData()
table = Table(
  "table1",
  metadata,
  Column("foo", Integer),
  Column("bar", ARRAY(Integer))
)
engine = create_engine("bigquery://", future=True)
q = select(table.c.foo).where(F.unnest(table.c.bar).column_valued() == 1)
print(q.compile(engine))

Generates this query:

SELECT `table1`.`foo` 
FROM `table1`, `table1` `table1_1`, unnest(`table1_1`.`bar`) AS `anon_1` 
WHERE `anon_1` = %(param_1:INT64)s

I expect it to generate this query instead (note the different FROM clause):

SELECT `table1`.`foo` 
FROM `table1`,  unnest(`table1`.`bar`) AS `anon_1` 
WHERE `anon_1` = %(param_1:INT64)s

Activity

  1. added
    api: bigqueryIssues related to the googleapis/python-bigquery-sqlalchemy API.
    priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    on Oct 11, 2021
  2. self-assigned this
    on Oct 26, 2021
  3. tseaver commented on Oct 26, 2021

    @tseaver
    Contributor

    @tswast I think I have a fix in hand for this one.

  4. added a commit that references this issue on Oct 26, 2021
    a98f818
  5. added a commit that references this issue on Oct 26, 2021
    1ce4e14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the googleapis/python-bigquery-sqlalchemy API.priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions