This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
fix: reduce bigquery table modification via DML for to_gbq #1737
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,9 +29,11 @@ | |||||||||||||
|
|
||||||||||||||
| import bigframes.core | ||||||||||||||
| from bigframes.core import compile, rewrite | ||||||||||||||
| import bigframes.core.compile.sqlglot.sqlglot_ir as sqlglot_ir | ||||||||||||||
| import bigframes.core.guid | ||||||||||||||
| import bigframes.core.nodes as nodes | ||||||||||||||
| import bigframes.core.ordering as order | ||||||||||||||
| import bigframes.core.schema as schemata | ||||||||||||||
| import bigframes.core.tree_properties as tree_properties | ||||||||||||||
| import bigframes.dtypes | ||||||||||||||
| import bigframes.exceptions as bfe | ||||||||||||||
|
|
@@ -206,17 +208,45 @@ def export_gbq( | |||||||||||||
| if bigframes.options.compute.enable_multi_query_execution: | ||||||||||||||
| self._simplify_with_caching(array_value) | ||||||||||||||
|
|
||||||||||||||
| dispositions = { | ||||||||||||||
| "fail": bigquery.WriteDisposition.WRITE_EMPTY, | ||||||||||||||
| "replace": bigquery.WriteDisposition.WRITE_TRUNCATE, | ||||||||||||||
| "append": bigquery.WriteDisposition.WRITE_APPEND, | ||||||||||||||
| } | ||||||||||||||
| table_exists = True | ||||||||||||||
| try: | ||||||||||||||
| table = self.bqclient.get_table(destination) | ||||||||||||||
| if if_exists == "fail": | ||||||||||||||
| raise ValueError(f"Table already exists: {destination.__str__()}") | ||||||||||||||
| except google.api_core.exceptions.NotFound: | ||||||||||||||
| table_exists = False | ||||||||||||||
|
|
||||||||||||||
| if len(cluster_cols) != 0: | ||||||||||||||
| if table_exists and table.clustering_fields != cluster_cols: | ||||||||||||||
| raise ValueError( | ||||||||||||||
| "Table clustering fields cannot be changed after the table has " | ||||||||||||||
| f"been created. Existing clustering fields: {table.clustering_fields}" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| sql = self.to_sql(array_value, ordered=False) | ||||||||||||||
| job_config = bigquery.QueryJobConfig( | ||||||||||||||
| write_disposition=dispositions[if_exists], | ||||||||||||||
| destination=destination, | ||||||||||||||
| clustering_fields=cluster_cols if cluster_cols else None, | ||||||||||||||
| ) | ||||||||||||||
| if table_exists and _if_schama_match(table.schema, array_value.schema): | ||||||||||||||
| # b/409086472: Uses DML for table appends and replacements to avoid | ||||||||||||||
| # BigQuery `RATE_LIMIT_EXCEEDED` errors, as per quota limits: | ||||||||||||||
| # https://cloud.google.com/bigquery/quotas#standard_tables | ||||||||||||||
| job_config = bigquery.QueryJobConfig() | ||||||||||||||
| ir = sqlglot_ir.SQLGlotIR.from_query_string(sql) | ||||||||||||||
| if if_exists == "append": | ||||||||||||||
| sql = ir.insert(destination) | ||||||||||||||
| else: # for "replace" | ||||||||||||||
| assert if_exists == "replace" | ||||||||||||||
| sql = ir.replace(destination) | ||||||||||||||
| else: | ||||||||||||||
| dispositions = { | ||||||||||||||
| "fail": bigquery.WriteDisposition.WRITE_EMPTY, | ||||||||||||||
| "replace": bigquery.WriteDisposition.WRITE_TRUNCATE, | ||||||||||||||
| "append": bigquery.WriteDisposition.WRITE_APPEND, | ||||||||||||||
| } | ||||||||||||||
| job_config = bigquery.QueryJobConfig( | ||||||||||||||
| write_disposition=dispositions[if_exists], | ||||||||||||||
| destination=destination, | ||||||||||||||
| clustering_fields=cluster_cols if cluster_cols else None, | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| # TODO(swast): plumb through the api_name of the user-facing api that | ||||||||||||||
| # caused this query. | ||||||||||||||
| _, query_job = self._run_execute_query( | ||||||||||||||
|
|
@@ -572,6 +602,21 @@ def _execute_plan( | |||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _if_schama_match( | ||||||||||||||
|
tswast marked this conversation as resolved.
Outdated
|
||||||||||||||
| table_schema: Tuple[bigquery.SchemaField, ...], schema: schemata.ArraySchema | ||||||||||||||
| ) -> bool: | ||||||||||||||
| if len(table_schema) != len(schema.items): | ||||||||||||||
| return False | ||||||||||||||
| for field in table_schema: | ||||||||||||||
| if field.name not in schema.names: | ||||||||||||||
| return False | ||||||||||||||
| if bigframes.dtypes.convert_schema_field(field)[1] != schema.get_type( | ||||||||||||||
|
Contributor
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. Do we need to do anything special here for the duration/timedelta type that we added recently?
Contributor
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.
python-bigquery-dataframes/bigframes/dtypes.py Lines 717 to 722 in d937be0
I believe we are good
Contributor
Author
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. Thanks! |
||||||||||||||
| field.name | ||||||||||||||
| ): | ||||||||||||||
| return False | ||||||||||||||
| return True | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _sanitize( | ||||||||||||||
| schema: Tuple[bigquery.SchemaField, ...] | ||||||||||||||
| ) -> Tuple[bigquery.SchemaField, ...]: | ||||||||||||||
|
|
||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.