Closed
Description
I need to create a join of 2 BQ tables where one of them has an id
field that has to be compared with the record id in the repeated field (array of records) of another one. I'm struggling with SQLAlchemy implementation of the SQL query that works fine. Tried to solve this problem in different ways, but nothing was successful, most relevant and similar to sql-like solution below. Probably, there are some troubles with sqlalchemy interpretation of SQL queries or result fetch either, aren't?
Environment details
- OS type and version: MacOS, 12.0 beta
- Python version: 3.9.1
- pip version: 21.1.2
google-cloud-bigquery
version: 2.22.1
Steps to reproduce
-
Create the BQ table
Mock
with fields:- name:
mock_id
| type:STRING
| mode:NULLABLE
- name:
-
Create the BQ table
AnotherMock
with fields:- name:
objects
| type:RECORD
| mode:REPEATED
- name:
object_id
| type:STRING
| mode:NULLABLE
- name:
- name:
-
Run code below (It gonna fail).
-
Run SQL below (it gonna success).
SQL example
SELECT *
FROM db.dataset.mock mock
INNER JOIN (SELECT another_mock_objects FROM db.dataset.another_mock, UNNEST(objects) as another_mock_objects) another_mock
ON mock.mock_id = another_mock.another_mock_objects.object_id
Code example
subquery = session.query(
func.unnest(another_mock_model.c.objects).label('another_mock_objects')
).subquery()
session.execute(
select([]) \
.select_from(
mock_model
.join(
another_mock_model,
subquery.c.another_mock_objects.object_id == mock_model.c.mock_id,
)
)
)
Stack trace
AttributeError: 'Comparator' object has no attribute 'object_id'