Skip to content

Commit

Permalink
feat: return JobID with TableResult (#2689)
Browse files Browse the repository at this point in the history
* chore: improve job create retry

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* chore: remove option 2

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* chore: pass jobid from job.getQUeryResults()

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* choreL set jobID as null when empty

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* chore: add IT tests

* chore: complete tests

* chore: make setJobId package private

* chore: remove redundant job ID setting

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
Neenu1995 and gcf-owl-bot[bot] committed Jun 14, 2023
1 parent e371e65 commit aa38428
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 11 deletions.
8 changes: 4 additions & 4 deletions README.md
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
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
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
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

0 comments on commit aa38428

Please sign in to comment.