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

table.insert_data should allow for dictionaries #3396

Closed
podopie opened this issue May 10, 2017 · 6 comments
Closed

table.insert_data should allow for dictionaries #3396

podopie opened this issue May 10, 2017 · 6 comments
Assignees
Labels
api: bigquery Issues related to the BigQuery API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@podopie
Copy link

podopie commented May 10, 2017

table.insert_data currently only accepts tuples/lists in the order of the schema. It'd be a great feature allow lists of dictionaries to be passed.

Would expect cases where if the keys in the dictionary do not match the schema, it would return back an error.

FWIW, here's an example of how another python library handles it for a typical database connection:

https://github.com/pudo/dataset/blob/master/dataset/persistence/table.py#L62

@dhermes dhermes added the api: bigquery Issues related to the BigQuery API. label May 10, 2017
@dhermes
Copy link
Contributor

dhermes commented May 10, 2017

@lukesneeringer WDYT of this? It'd be somewhat easy to support with a helper that mapped a each row in rows into row_info (whether it started as a tuple or a dict).

@dhermes dhermes added the type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. label May 10, 2017
@tseaver
Copy link
Contributor

tseaver commented May 10, 2017

@dhermes I think we would be better off with another method which handles dicts (and likely calls the current insert_data).

@Lordie12
Copy link

+1 to this, it would be really helpful (especially when handling 2-3 level nested RECORDS). Its currently quite a change from using both the Java client and the NodeJS client (both of which accept a TableRow / dictionary).

@tseaver
Copy link
Contributor

tseaver commented May 16, 2017

I'd like to add a Table.row_from_mapping method which uses the schema to generate row tuples from a dict. Usage would then be:

table.insert_data([table.row_from_mapping(mapping) for mapping in json_rows])

@rmmh
Copy link

rmmh commented May 26, 2017

insert_data also doesn't fully support nested records-- it only runs conversion functions (datetime=>timestamps, etc) on the first level of data.

It feels quite pointless, since it just ends up reconstructing the dictionary internally. Users with a dictionary have to convert it to a tuple according to the schema (but only the first level!), and then insert_data converts it back into the same dictionary before issuing a POST.

Since the API endpoint itself expects a dictionary, it would make more sense for the method to accept a dictionary. Why is it a tuple/row interface anyways?

Polymorphism seems fine to me, or another method.

@tseaver
Copy link
Contributor

tseaver commented Jun 7, 2017

@rmmh The reason that insert_data takes tuples is that most "row oriented" Python modules (the DB-API, the csv stdlib module) deal with rows as tuples, not as mappings. We also work to consume / return the values as the correct native Python types, according to the schema type, which is not the same as the on-the-wire JSON values (e.g., datetimes, bools, bytes, etc.)

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 BigQuery API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

6 participants