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

Avoid listing table data for destination of CREATE VIEW DDL queries. #3469

Merged
merged 6 commits into from
Jul 16, 2018
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.
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
@@ -0,0 +1,33 @@
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.bigquery;

import com.google.api.core.InternalApi;
import com.google.cloud.PageImpl;

public class EmptyTableResult extends TableResult {

private static final long serialVersionUID = -4831062717210349819L;

/**
* An empty {@code TableResult} to avoid making API requests to unlistable tables.
*/
@InternalApi("Exposed for testing")
public EmptyTableResult() {
super(null, 0, new PageImpl<FieldValueList>(null, "", null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.cloud.bigquery.BigQuery.QueryResultsOption;
import com.google.cloud.bigquery.BigQuery.TableDataListOption;
import com.google.cloud.bigquery.JobConfiguration.Type;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -154,7 +155,8 @@ public Job build() {
* Checks if this job exists.
*
* <p>Example of checking that a job exists.
* <pre> {@code
*
* <pre>{@code
* if (!job.exists()) {
* // job doesn't exist
* }
Expand All @@ -173,7 +175,8 @@ public boolean exists() {
* not exist this method returns {@code true}.
*
* <p>Example of waiting for a job until it reports that it is done.
* <pre> {@code
*
* <pre>{@code
* while (!job.isDone()) {
* Thread.sleep(1000L);
* }
Expand All @@ -196,7 +199,8 @@ public boolean isDone() {
* 12 hours as a total timeout and unlimited number of attempts.
*
* <p>Example usage of {@code waitFor()}.
* <pre> {@code
*
* <pre>{@code
* Job completedJob = job.waitFor();
* if (completedJob == null) {
* // job no longer exists
Expand All @@ -208,7 +212,8 @@ public boolean isDone() {
* }</pre>
*
* <p>Example usage of {@code waitFor()} with checking period and timeout.
* <pre> {@code
*
* <pre>{@code
* Job completedJob =
* job.waitFor(
* RetryOption.initialRetryDelay(Duration.ofSeconds(1)),
Expand Down Expand Up @@ -285,10 +290,24 @@ public TableResult getQueryResults(QueryResultsOption... options)
QueryResponse response =
waitForQueryResults(
DEFAULT_JOB_WAIT_SETTINGS, waitOptions.toArray(new QueryResultsOption[0]));
if (response.getSchema() == null) {
throw new JobException(getJobId(), response.getErrors());

// Get the job resource to determine if it has errored.
Job job = this;
if (job.getStatus() == null || job.getStatus().getState() != JobStatus.State.DONE) {
job = reload();
}
if (job.getStatus() != null && job.getStatus().getError() != null) {
throw new JobException(
getJobId(), ImmutableList.copyOf(job.getStatus().getExecutionErrors()));

This comment was marked as spam.

This comment was marked as spam.

}


// If there are no rows in the result, this may have been a DDL query.
// 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();
}

TableId table = ((QueryJobConfiguration) getConfiguration()).getDestinationTable();
return bigquery.listTableData(
table, response.getSchema(), listOptions.toArray(new TableDataListOption[0]));
Expand Down Expand Up @@ -356,15 +375,17 @@ public boolean shouldRetry(Throwable prevThrowable, Job prevResponse) {
* Fetches current job's latest information. Returns {@code null} if the job does not exist.
*
* <p>Example of reloading all fields until job status is DONE.
* <pre> {@code
*
* <pre>{@code
* while (job.getStatus().getState() != JobStatus.State.DONE) {
* Thread.sleep(1000L);
* job = job.reload();
* }
* }</pre>
*
* <p>Example of reloading status field until job status is DONE.
* <pre> {@code
*
* <pre>{@code
* while (job.getStatus().getState() != JobStatus.State.DONE) {
* Thread.sleep(1000L);
* job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS));
Expand All @@ -384,7 +405,8 @@ public Job reload(JobOption... options) {
* Sends a job cancel request.
*
* <p>Example of cancelling a job.
* <pre> {@code
*
* <pre>{@code
* if (job.cancel()) {
* return true; // job successfully cancelled
* } else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;

/**
* A Google BigQuery Job status. Objects of this class can be examined when polling an asynchronous
Expand All @@ -35,9 +35,7 @@ public class JobStatus implements Serializable {

private static final long serialVersionUID = -714976456815445365L;

/**
* Possible states that a BigQuery Job can assume.
*/
/** Possible states that a BigQuery Job can assume. */
public static final class State extends StringEnumValue {
private static final long serialVersionUID = 818920627219751204L;

Expand All @@ -49,18 +47,12 @@ public State apply(String constant) {
}
};

private static final StringEnumType<State> type = new StringEnumType(
State.class,
CONSTRUCTOR);
private static final StringEnumType<State> type = new StringEnumType(State.class, CONSTRUCTOR);

/**
* The BigQuery Job is waiting to be executed.
*/
/** The BigQuery Job is waiting to be executed. */
public static final State PENDING = type.createAndRegister("PENDING");

/**
* The BigQuery Job is being executed.
*/
/** The BigQuery Job is being executed. */
public static final State RUNNING = type.createAndRegister("RUNNING");

/**
Expand All @@ -74,23 +66,19 @@ private State(String constant) {
}

/**
* Get the State for the given String constant, and throw an exception if the constant is
* not recognized.
* Get the State for the given String constant, and throw an exception if the constant is not
* recognized.
*/
public static State valueOfStrict(String constant) {
return type.valueOfStrict(constant);
}

/**
* Get the State for the given String constant, and allow unrecognized values.
*/
/** Get the State for the given String constant, and allow unrecognized values. */
public static State valueOf(String constant) {
return type.valueOf(constant);
}

/**
* Return the known values for State.
*/
/** Return the known values for State. */
public static State[] values() {
return type.values();
}
Expand All @@ -112,35 +100,33 @@ public static State[] values() {
this.executionErrors = executionErrors != null ? ImmutableList.copyOf(executionErrors) : null;
}


/**
* Returns the state of the job. A {@link State#PENDING} job is waiting to be executed. A
* {@link State#RUNNING} is being executed. A {@link State#DONE} job has completed either
* succeeding or failing. If failed {@link #getError()} will be non-null.
* Returns the state of the job. A {@link State#PENDING} job is waiting to be executed. A {@link
* State#RUNNING} is being executed. A {@link State#DONE} job has completed either succeeding or
* failing. If failed {@link #getError()} will be non-null.
*/
public State getState() {
return state;
}


/**
* Returns the final error result of the job. If present, indicates that the job has completed
* and was unsuccessful.
* Returns the final error result of the job. If present, indicates that the job has completed and
* was unsuccessful.
*
* @see <a href="https://cloud.google.com/bigquery/troubleshooting-errors">
* Troubleshooting Errors</a>
* @see <a href="https://cloud.google.com/bigquery/troubleshooting-errors">Troubleshooting
* Errors</a>
*/
@Nullable
public BigQueryError getError() {
return error;
}


/**
* Returns all errors encountered during the running of the job. Errors here do not necessarily
* mean that the job has completed or was unsuccessful.
*
* @see <a href="https://cloud.google.com/bigquery/troubleshooting-errors">
* Troubleshooting Errors</a>
* @see <a href="https://cloud.google.com/bigquery/troubleshooting-errors">Troubleshooting
* Errors</a>
*/
public List<BigQueryError> getExecutionErrors() {
return executionErrors;
Expand All @@ -164,8 +150,8 @@ public final int hashCode() {
public final boolean equals(Object obj) {
return obj == this
|| obj != null
&& obj.getClass().equals(JobStatus.class)
&& Objects.equals(toPb(), ((JobStatus) obj).toPb());
&& obj.getClass().equals(JobStatus.class)
&& Objects.equals(toPb(), ((JobStatus) obj).toPb());
}

com.google.api.services.bigquery.model.JobStatus toPb() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.google.cloud.bigquery;

import static com.google.common.truth.Truth.assertThat;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.anyString;
import static org.easymock.EasyMock.capture;
import static org.easymock.EasyMock.eq;
import static org.junit.Assert.assertArrayEquals;
Expand Down Expand Up @@ -962,7 +964,7 @@ public JobId get() {
.andThrow(new BigQueryException(409, "already exists, for some reason"));
EasyMock.expect(
bigqueryRpcMock.getJob(
EasyMock.anyString(),
anyString(),
EasyMock.eq(id),
EasyMock.eq((String) null),
EasyMock.eq(EMPTY_RPC_OPTIONS)))
Expand Down Expand Up @@ -1270,6 +1272,9 @@ public void testQueryRequestCompletedOnSecondAttempt() throws InterruptedExcepti
bigqueryRpcMock.create(
JOB_INFO.toPb(), Collections.<BigQueryRpc.Option, Object>emptyMap()))
.andReturn(jobResponsePb1);
EasyMock.expect(
bigqueryRpcMock.getJob(eq(PROJECT), eq(JOB), anyString(), anyObject(Map.class)))
.andReturn(jobResponsePb1);

EasyMock.expect(
bigqueryRpcMock.getQueryResults(
Expand Down
Loading