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

destination bigquery 1s1t: print subquery stats #28373

Merged
merged 5 commits into from
Jul 18, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.Job;
import com.google.cloud.bigquery.JobConfiguration;
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.JobStatistics;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.Table;
import com.google.cloud.bigquery.TableDefinition;
import com.google.cloud.bigquery.TableId;
import io.airbyte.integrations.base.destination.typing_deduping.SqlGenerator.StreamId;
import java.util.Comparator;
import java.util.Optional;
import java.util.UUID;
import org.slf4j.Logger;
Expand Down Expand Up @@ -47,13 +49,45 @@ public void execute(final String sql) throws InterruptedException {

Job job = bq.create(JobInfo.of(QueryJobConfiguration.newBuilder(sql).build()));
job = job.waitFor();
// waitFor() seems to throw an exception, but javadoc says we're supposed to handle this case
if (job.getStatus().getError() != null) {
throw new RuntimeException(job.getStatus().getError().toString());
}

JobStatistics.QueryStatistics statistics = job.getStatistics();
LOGGER.info("Completed sql {} in {} ms; processed {} bytes; billed for {} bytes",
LOGGER.info("Root-level job {} completed in {} ms; processed {} bytes; billed for {} bytes",
queryId,
statistics.getEndTime() - statistics.getStartTime(),
statistics.getTotalBytesProcessed(),
statistics.getTotalBytesBilled());

// SQL transactions can spawn child jobs, which are billed individually. Log their stats too.
if (statistics.getNumChildJobs() != null) {
// There isn't (afaict) anything resembling job.getChildJobs(), so we have to ask bq for them
bq.listJobs(BigQuery.JobListOption.parentJobId(job.getJobId().getJob())).streamAll()
.sorted(Comparator.comparing(childJob -> childJob.getStatistics().getEndTime()))
.forEach(childJob -> {
JobConfiguration configuration = childJob.getConfiguration();
if (configuration instanceof QueryJobConfiguration qc) {
JobStatistics.QueryStatistics childQueryStats = childJob.getStatistics();
String truncatedQuery = qc.getQuery()
edgao marked this conversation as resolved.
Show resolved Hide resolved
.substring(0, Math.min(100, qc.getQuery().length()))
.replaceAll("\n", " ");
LOGGER.info("Child sql {} completed in {} ms; processed {} bytes; billed for {} bytes",
truncatedQuery,
childQueryStats.getEndTime() - childQueryStats.getStartTime(),
childQueryStats.getTotalBytesProcessed(),
childQueryStats.getTotalBytesBilled());
} else {
// other job types are extract/copy/load
// we're probably not using them, but handle just in case?
JobStatistics childJobStats = childJob.getStatistics();
LOGGER.info("Non-query child job ({}) completed in {} ms",
configuration.getType(),
childJobStats.getEndTime() - childJobStats.getStartTime());
}
});
}
}

}
Loading