-
Notifications
You must be signed in to change notification settings - Fork 163
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
feat(incremental): copy multiple tables in parallel (#1237) #1413
Open
AxelThevenot
wants to merge
1
commit into
dbt-labs:main
Choose a base branch
from
AxelThevenot:feat/1237/copy-tables-and-partition-in-parallel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: copy tables and partitions in parallel | ||
time: 2024-11-26T00:02:41.54479+01:00 | ||
custom: | ||
Author: AxelThevenot | ||
Issue: "1237" |
This file contains 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 |
---|---|---|
|
@@ -402,17 +402,14 @@ def standard_to_legacy(table): | |
_, iterator = self.raw_execute(sql, use_legacy_sql=True) | ||
return self.get_table_from_response(iterator) | ||
|
||
def copy_bq_table(self, source, destination, write_disposition) -> None: | ||
def copy_bq_table(self, source, destination, write_disposition, partition_ids=None) -> None: | ||
conn = self.get_thread_connection() | ||
client: Client = conn.handle | ||
|
||
# ------------------------------------------------------------------------------- | ||
# BigQuery allows to use copy API using two different formats: | ||
# 1. client.copy_table(source_table_id, destination_table_id) | ||
# where source_table_id = "your-project.source_dataset.source_table" | ||
# 2. client.copy_table(source_table_ids, destination_table_id) | ||
# where source_table_ids = ["your-project.your_dataset.your_table_name", ...] | ||
# Let's use uniform function call and always pass list there | ||
# BigQuery allows to use copy API on the same table in parallel | ||
# so each source (and if partition of each source if given) is copied | ||
# into the destination table in parallel. | ||
# ------------------------------------------------------------------------------- | ||
if type(source) is not list: | ||
source = [source] | ||
|
@@ -436,14 +433,32 @@ def copy_bq_table(self, source, destination, write_disposition) -> None: | |
", ".join(source_ref.path for source_ref in source_ref_array), | ||
destination_ref.path, | ||
) | ||
|
||
with self.exception_handler(msg): | ||
copy_job = client.copy_table( | ||
source_ref_array, | ||
destination_ref, | ||
job_config=CopyJobConfig(write_disposition=write_disposition), | ||
retry=self._retry.create_reopen_with_deadline(conn), | ||
) | ||
copy_job.result(timeout=self._retry.create_job_execution_timeout(fallback=300)) | ||
|
||
copy_jobs = [] | ||
|
||
# Runs all the copy jobs in parallel | ||
for source_ref in source_ref_array: | ||
|
||
for partition_id in partition_ids or [None]: | ||
source_ref_partition = ( | ||
f"{source_ref}${partition_id}" if partition_id else source_ref | ||
) | ||
destination_ref_partition = ( | ||
f"{destination_ref}${partition_id}" if partition_id else destination_ref | ||
) | ||
copy_job = client.copy_table( | ||
source_ref_partition, | ||
destination_ref_partition, | ||
job_config=CopyJobConfig(write_disposition=write_disposition), | ||
retry=self._retry.create_reopen_with_deadline(conn), | ||
) | ||
copy_jobs.append(copy_job) | ||
Comment on lines
+441
to
+457
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. Would being explicit here clarify the logic?
|
||
|
||
# Waits for the jobs to finish | ||
for copy_job in copy_jobs: | ||
copy_job.result(timeout=self._retry.create_job_execution_timeout(fallback=300)) | ||
|
||
def write_dataframe_to_table( | ||
self, | ||
|
This file contains 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will there be more than one element in
source_ref_partition
?If we ever be in the scenario where we have
source_ref_array
greater than one andwrite_disposition
set toWRITE_TRUNCATE
, we'll be overwriting the same data.