Skip to content

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

@jimfulton

Description

@jimfulton

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}

¯_(ツ)_/¯

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: bigqueryIssues related to the googleapis/python-bigquery API.dbapitype: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions