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

Select Condition based on UNNEST #935

Open
aczire opened this issue Dec 19, 2023 · 4 comments
Open

Select Condition based on UNNEST #935

aczire opened this issue Dec 19, 2023 · 4 comments
Assignees
Labels
api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@aczire
Copy link

aczire commented Dec 19, 2023

I was trying to select rows from a BigQuery view based on a condition which is inside an array of structures,
Below is the SQL query

        SELECT id, enId, versionId, code_coding
        FROM `some.schema_views.table`, UNNEST(code_coding) AS cc
        WHERE (cc.code, cc.display) = ('H', 'BLAST')
        LIMIT 100

Below is my failed attempt to write the same in SQLAlchemy

        from sqlalchemy.sql.expression import cast
        from sqlalchemy import String, func
        code_coding = db.query(func.unnest(self.model.code_coding)).subquery()
        conditions.append(
            and_(cast(code_coding.c.code, String) == "H", cast(code_coding.c.display, String) == "BLAST")
        )

It says c.code is not found. Any idea? Thanks ahead!

@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. label Dec 19, 2023
@kiraksi
Copy link
Contributor

kiraksi commented Dec 20, 2023

@aczire The core problem seems to stem from how SQLAlchemy ORM constructs handles structures within arrays and how it references columns after UNNEST operations. I have been looking into unnest while working on a separate issue and from my limited knowledge, it might be because it needs an additional table alias assigned to the subquery because SQLAlchemy needs additional clarity about how to reference columns? It seems like adding that alias will solve your problem, but there are workarounds mentioned here for example. Can you try that and let me know if it still doesn't work and I will look into it.

@aczire
Copy link
Author

aczire commented Dec 21, 2023

Thanks @kiraksi

I tried adding the alias, code_coding = db.query(func.unnest(self.model.code_coding).alias('cc')).subquery()

But that still didnt work. Still got the KeyError, which means the unnest'd columns are still not in c.

Any idea?

Opened a discussion on the same here as well,
sqlalchemy/sqlalchemy#10779

@kiraksi kiraksi self-assigned this Jan 9, 2024
@kiraksi
Copy link
Contributor

kiraksi commented Jan 11, 2024

I plan on taking a look at this issue next week. I see the sqlalchemy team had a suggested fix, @aczire did that work for you?

@aczire
Copy link
Author

aczire commented Jan 12, 2024

Thanks @kiraksi
I couldn't get it done, so went with filtering it in the business logic instead of filtering it during the query phase. Any help is much appreciated!

@Linchin Linchin assigned Linchin and unassigned kiraksi Apr 30, 2024
@Linchin Linchin added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p2 Moderately-important priority. Fix may not be included in next release. labels Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

3 participants