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

feat: return JobID with TableResult #2689

Merged
merged 16 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ If you are using Maven without the BOM, add this to your dependencies:
If you are using Gradle 5.x or later, add this to your dependencies:

```Groovy
implementation platform('com.google.cloud:libraries-bom:26.16.0')
implementation platform('com.google.cloud:libraries-bom:26.17.0')

implementation 'com.google.cloud:google-cloud-bigquery'
```
If you are using Gradle without BOM, add this to your dependencies:

```Groovy
implementation 'com.google.cloud:google-cloud-bigquery:2.27.0'
implementation 'com.google.cloud:google-cloud-bigquery:2.27.1'
```

If you are using SBT, add this to your dependencies:

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "2.27.0"
libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "2.27.1"
```
<!-- {x-version-update-end} -->

Expand Down Expand Up @@ -350,7 +350,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-bigquery/java11.html
[stability-image]: https://img.shields.io/badge/stability-stable-green
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-bigquery.svg
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigquery/2.27.0
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigquery/2.27.1
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public com.google.api.services.bigquery.model.Job call() {
}
},
getOptions().getRetrySettings(),
EXCEPTION_HANDLER,
BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER,
getOptions().getClock(),
DEFAULT_RETRY_CONFIG));
} catch (BigQueryRetryHelper.BigQueryRetryHelperException e) {
Expand Down Expand Up @@ -1334,7 +1334,7 @@ public com.google.api.services.bigquery.model.QueryResponse call() {
List<BigQueryError> bigQueryErrors =
Lists.transform(results.getErrors(), BigQueryError.FROM_PB_FUNCTION);
// Throwing BigQueryException since there may be no JobId and we want to stay consistent
// with the case where there there is a HTTP error
// with the case where there is an HTTP error
throw new BigQueryException(bigQueryErrors);
}

Expand Down Expand Up @@ -1369,7 +1369,9 @@ public com.google.api.services.bigquery.model.QueryResponse call() {
new QueryPageFetcher(jobId, schema, getOptions(), cursor, optionMap(options)),
cursor,
// cache first page of result
transformTableData(results.getRows(), schema)));
transformTableData(results.getRows(), schema)),
// Return the JobID of the successful job
jobId);
}
// only 1 page of result
return new TableResult(
Expand All @@ -1378,7 +1380,9 @@ public com.google.api.services.bigquery.model.QueryResponse call() {
new PageImpl<>(
new TableDataPageFetcher(null, schema, getOptions(), null, optionMap(options)),
null,
transformTableData(results.getRows(), schema)));
transformTableData(results.getRows(), schema)),
// Return the JobID of the successful job
results.getJobReference() != null ? JobId.fromPb(results.getJobReference()) : null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,20 @@ public TableResult getQueryResults(QueryResultsOption... options)
// Listing table data might fail, such as with CREATE VIEW queries.
// Avoid a tabledata.list API request by returning an empty TableResult.
if (response.getTotalRows() == 0) {
return new EmptyTableResult(response.getSchema());
TableResult emptyTableResult = new EmptyTableResult(response.getSchema());
emptyTableResult.setJobId(job.getJobId());
return emptyTableResult;
}

TableId table =
((QueryJobConfiguration) getConfiguration()).getDestinationTable() == null
? ((QueryJobConfiguration) job.getConfiguration()).getDestinationTable()
: ((QueryJobConfiguration) getConfiguration()).getDestinationTable();
return bigquery.listTableData(
table, response.getSchema(), listOptions.toArray(new TableDataListOption[0]));
TableResult tableResult =
bigquery.listTableData(
table, response.getSchema(), listOptions.toArray(new TableDataListOption[0]));
tableResult.setJobId(job.getJobId());
return tableResult;
}

private QueryResponse waitForQueryResults(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public class TableResult implements Page<FieldValueList>, Serializable {
@Nullable private final Schema schema;
private final long totalRows;
private final Page<FieldValueList> pageNoSchema;
@Nullable private JobId jobId = null;

// package-private so job id is not set outside the package.
void setJobId(@Nullable JobId jobId) {
this.jobId = jobId;
}

public JobId getJobId() {
return jobId;
}

/**
* If {@code schema} is non-null, {@code TableResult} adds the schema to {@code FieldValueList}s
Expand All @@ -47,6 +57,15 @@ public TableResult(Schema schema, long totalRows, Page<FieldValueList> pageNoSch
this.pageNoSchema = checkNotNull(pageNoSchema);
}

@InternalApi("Exposed for testing")
public TableResult(
Schema schema, long totalRows, Page<FieldValueList> pageNoSchema, JobId jobId) {
this.schema = schema;
this.totalRows = totalRows;
this.pageNoSchema = checkNotNull(pageNoSchema);
this.jobId = jobId;
}

/** Returns the schema of the results. Null if the schema is not supplied. */
public Schema getSchema() {
return schema;
Expand Down