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
fix: unnest failed in some cases (with table references failed when there were no other references to refrenced tables in a query) #290
Changes from 5 commits
904a3bb
ffcb3e7
806aa5c
89a9fe0
6a679ae
11ca126
32980ba
ea276ba
4de3d71
5cde0d4
2663e3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
import datetime | ||
import decimal | ||
|
||
sqlalchemy_version_info = tuple(map(int, sqlalchemy.__version__.split("."))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works, though I've been trying to use https://packaging.pypa.io/en/latest/version.html which is the canonical version parser. We already have it pulled in via a transitive dependency through There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. I'll switch to that. WRT how we get it, IMO, anything we import should be a direct dependency. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. I'm fine explicitly including it in our dependencies / test dependencies. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
ONE_ROW_CONTENTS_EXPANDED = [ | ||
588, | ||
|
@@ -725,3 +726,31 @@ class MyTable(Base): | |
) | ||
|
||
assert sorted(db.query(sqlalchemy.distinct(MyTable.my_column)).all()) == expected | ||
|
||
|
||
@pytest.mark.skipif( | ||
sqlalchemy_version_info < (1, 4), | ||
reason="unnest (and other table-valued-function) support required version 1.4", | ||
) | ||
def test_unnest(engine, bigquery_dataset): | ||
from sqlalchemy import select, func, String | ||
from sqlalchemy_bigquery import ARRAY | ||
|
||
conn = engine.connect() | ||
metadata = MetaData() | ||
table = Table( | ||
f"{bigquery_dataset}.test_unnest", metadata, Column("objects", ARRAY(String)), | ||
) | ||
metadata.create_all(engine) | ||
conn.execute( | ||
table.insert(), [dict(objects=["a", "b", "c"]), dict(objects=["x", "y"])] | ||
) | ||
query = select([func.unnest(table.c.objects).alias("foo_objects").column]) | ||
compiled = str(query.compile(engine)) | ||
assert " ".join(compiled.strip().split()) == ( | ||
f"SELECT `foo_objects`" | ||
f" FROM" | ||
f" `{bigquery_dataset}.test_unnest` `{bigquery_dataset}.test_unnest_1`," | ||
f" unnest(`{bigquery_dataset}.test_unnest_1`.`objects`) AS `foo_objects`" | ||
) | ||
assert sorted(r[0] for r in conn.execute(query)) == ["a", "b", "c", "x", "y"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the new array type count as a feature? Perhaps it'd look better in the changelog as a separate PR?
If it is a feature and it doesn't make sense to split out, we could also try the multi-change commit message feature when we squash and merge this: https://github.com/googleapis/release-please#what-if-my-pr-contains-multiple-fixes-or-features
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a new feature. It was just missing from the types list.
SQLAlchemy has basic array support that works with BigQuery (thanks to some previous effort on our part).