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

Support bound struct parameters with explicit types in db api interface #717

Closed
jimfulton opened this issue Jun 23, 2021 · 0 comments · Fixed by #718
Closed

Support bound struct parameters with explicit types in db api interface #717

jimfulton opened this issue Jun 23, 2021 · 0 comments · Fixed by #718
Labels
api: bigquery Issues related to the googleapis/python-bigquery API. dbapi type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@jimfulton
Copy link
Contributor

Allow passing struct data to queries if explicit type information is provided in the placeholder.

For example, given a table created with:

create table mydb.mytable (person struct<name string, children array<struct<name string, bdate date>>>)

You can insert data with:

insert into mydb.mytable (person) values (%(v:struct<name string, children array<struct<name string, bdate date>>>)s)

passing a value like:

dict(name='par',
                children=[
                    dict(name='ch1', bdate=datetime.date(2021, 1, 1)),
                    dict(name='ch2', bdate=datetime.date(2021, 1, 2)),
                    ])

We have to pass the type explicitly because struct data are dictionaries and may not preserve ordering reliably.

Even though, dictionaries are now ordered, the ordering is likely to be fragile.

>>> d = dict()
>>> d['a'] = 1
>>> d['b'] = 2
>>> d
{'a': 1, 'b': 2}
>>> del d['a']
>>> d['a'] = 4
>>> d
{'b': 2, 'a': 4}

¯_(ツ)_/¯

@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/python-bigquery API. label Jun 23, 2021
@jimfulton jimfulton added dbapi type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. labels Jun 23, 2021
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 API. dbapi type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant