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

fix: allow to_gbq to run without bigquery.tables.create permission. #539

Merged
merged 1 commit into from
Jun 16, 2022

Conversation

acarmel
Copy link
Contributor

@acarmel acarmel commented Jun 15, 2022

Fixes #538 🦕

@acarmel acarmel requested a review from a team as a code owner June 15, 2022 07:15
@acarmel acarmel requested review from a team and prash-mi June 15, 2022 07:15
@google-cla
Copy link

google-cla bot commented Jun 15, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@product-auto-label product-auto-label bot added size: xs Pull request size is extra small. api: bigquery Issues related to the googleapis/python-bigquery-pandas API. labels Jun 15, 2022
@steffnay steffnay added the owlbot:run Add this label to trigger the Owlbot post processor. label Jun 15, 2022
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Jun 15, 2022
Copy link
Collaborator

@tswast tswast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the investigation and the fix!

Note: this might make it harder to create tables via the load job as I had planned in #425 but we can address that if/when we try to do that.

@tswast tswast added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 16, 2022
@tswast tswast requested a review from steffnay June 16, 2022 15:53
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 16, 2022
@acarmel
Copy link
Contributor Author

acarmel commented Jun 16, 2022

Thanks for the investigation and the fix!

Note: this might make it harder to create tables via the load job as I had planned in #425 but we can address that if/when we try to do that.

Thanks!

In the current design you already create the table before the loading.

try:
table = bqclient.get_table(destination_table_ref)
except google_exceptions.NotFound:
table_connector = _Table(
project_id_table,
dataset_id,
location=location,
credentials=connector.credentials,
)
table_connector.create(table_id, table_schema)
else:
original_schema = pandas_gbq.schema.to_pandas_gbq(table.schema)
if if_exists == "fail":
raise TableCreationError(
"Could not create the table because it "
"already exists. "
"Change the if_exists parameter to "
"'append' or 'replace' data."
)
elif if_exists == "replace":
connector.delete_and_recreate_table(
project_id_table, dataset_id, table_id, table_schema
)
else:
if not pandas_gbq.schema.schema_is_subset(original_schema, table_schema):
raise InvalidSchema(
"Please verify that the structure and "
"data types in the DataFrame match the "
"schema of the destination table.",
table_schema,
original_schema,
)
# Update the local `table_schema` so mode (NULLABLE/REQUIRED)
# matches. See: https://github.com/pydata/pandas-gbq/issues/315
table_schema = pandas_gbq.schema.update_schema(
table_schema, original_schema
)
)

And when you load you create your own JobConfig.

job_config = bigquery.LoadJobConfig()
job_config.write_disposition = "WRITE_APPEND"
job_config.source_format = "PARQUET"

I guess that once you add the option to let the user provide config then you can go with that config and not create one of yours and otherwise create a default config with reasonable default.

@steffnay steffnay merged commit 3988306 into googleapis:main Jun 16, 2022
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-pandas API. size: xs Pull request size is extra small.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

to_gbq requires bigquery.tables.create permission even with if_exists="append"
4 participants