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

BigQuery: Allow job to extract or load table across projects #4183

Merged
merged 3 commits into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -21,6 +21,7 @@
import com.google.api.services.bigquery.model.JobConfigurationTableCopy;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.util.List;
Expand Down Expand Up @@ -216,10 +217,15 @@ CopyJobConfiguration setProjectId(final String projectId) {
new Function<TableId, TableId>() {
@Override
public TableId apply(TableId tableId) {
return tableId.setProjectId(projectId);
if (Strings.isNullOrEmpty(tableId.getProject())) {
return tableId.setProjectId(projectId);
}
return tableId;
}
}));
builder.setDestinationTable(getDestinationTable().setProjectId(projectId));
if (Strings.isNullOrEmpty(getDestinationTable().getProject())) {
builder.setDestinationTable(getDestinationTable().setProjectId(projectId));
}
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.api.services.bigquery.model.JobConfigurationExtract;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -215,7 +216,10 @@ public int hashCode() {

@Override
ExtractJobConfiguration setProjectId(String projectId) {
return toBuilder().setSourceTable(getSourceTable().setProjectId(projectId)).build();
if (Strings.isNullOrEmpty(getSourceTable().getProject())) {
return toBuilder().setSourceTable(getSourceTable().setProjectId(projectId)).build();
}
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.api.services.bigquery.model.JobConfigurationLoad;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints;
import java.util.List;
Expand Down Expand Up @@ -398,7 +399,10 @@ public int hashCode() {

@Override
LoadJobConfiguration setProjectId(String projectId) {
return toBuilder().setDestinationTable(getDestinationTable().setProjectId(projectId)).build();
if (Strings.isNullOrEmpty(getDestinationTable().getProject())) {
return toBuilder().setDestinationTable(getDestinationTable().setProjectId(projectId)).build();
}
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.bigquery.JobInfo.WriteDisposition;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -769,7 +770,8 @@ public int hashCode() {
@Override
QueryJobConfiguration setProjectId(String projectId) {
Builder builder = toBuilder();
if (getDestinationTable() != null) {
if (getDestinationTable() != null
&& Strings.isNullOrEmpty(getDestinationTable().getProject())) {
builder.setDestinationTable(getDestinationTable().setProjectId(projectId));
}
if (getDefaultDataset() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@

import com.google.cloud.bigquery.JobInfo.CreateDisposition;
import com.google.cloud.bigquery.JobInfo.WriteDisposition;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.util.List;
import org.junit.Test;

public class CopyJobConfigurationTest {

private static final String TEST_PROJECT_ID = "test-project-id";
private static final TableId SOURCE_TABLE = TableId.of("dataset", "sourceTable");
private static final List<TableId> SOURCE_TABLES =
ImmutableList.of(
Expand Down Expand Up @@ -115,10 +118,33 @@ public void testToPbAndFromPb() {

@Test
public void testSetProjectId() {
CopyJobConfiguration configuration = COPY_JOB_CONFIGURATION_MULTIPLE_TABLES.setProjectId("p");
assertEquals("p", configuration.getDestinationTable().getProject());
CopyJobConfiguration configuration = COPY_JOB_CONFIGURATION_MULTIPLE_TABLES.setProjectId(TEST_PROJECT_ID);

This comment was marked as spam.

assertEquals(TEST_PROJECT_ID, configuration.getDestinationTable().getProject());
for (TableId sourceTable : configuration.getSourceTables()) {
assertEquals("p", sourceTable.getProject());
assertEquals(TEST_PROJECT_ID, sourceTable.getProject());
}
}

@Test
public void testSetProjectIdDoNotOverride() {
CopyJobConfiguration configuration =
COPY_JOB_CONFIGURATION_MULTIPLE_TABLES
.toBuilder()
.setSourceTables(
Lists.transform(
SOURCE_TABLES,
new Function<TableId, TableId>() {
@Override
public TableId apply(TableId tableId) {
return tableId.setProjectId(TEST_PROJECT_ID);
}
}))
.setDestinationTable(DESTINATION_TABLE.setProjectId(TEST_PROJECT_ID))
.build()
.setProjectId("do-not-update");
assertEquals(TEST_PROJECT_ID, configuration.getDestinationTable().getProject());
for (TableId sourceTable : configuration.getSourceTables()) {
assertEquals(TEST_PROJECT_ID, sourceTable.getProject());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

public class ExtractJobConfigurationTest {

private static final String TEST_PROJECT_ID = "test-project-id";
private static final List<String> DESTINATION_URIS = ImmutableList.of("uri1", "uri2");
private static final String DESTINATION_URI = "uri1";
private static final TableId TABLE_ID = TableId.of("dataset", "table");
Expand Down Expand Up @@ -118,8 +119,19 @@ public void testToPbAndFromPb() {

@Test
public void testSetProjectId() {
ExtractJobConfiguration configuration = EXTRACT_CONFIGURATION.setProjectId("p");
assertEquals("p", configuration.getSourceTable().getProject());
ExtractJobConfiguration configuration = EXTRACT_CONFIGURATION.setProjectId(TEST_PROJECT_ID);
assertEquals(TEST_PROJECT_ID, configuration.getSourceTable().getProject());
}

@Test
public void testSetProjectIdDoNotOverride() {
ExtractJobConfiguration configuration =
EXTRACT_CONFIGURATION
.toBuilder()
.setSourceTable(TABLE_ID.setProjectId(TEST_PROJECT_ID))
.build()
.setProjectId("do-not-update");
assertEquals(TEST_PROJECT_ID, configuration.getSourceTable().getProject());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

public class LoadJobConfigurationTest {

private static final String TEST_PROJECT_ID = "test-project-id";
private static final CsvOptions CSV_OPTIONS =
CsvOptions.newBuilder()
.setAllowJaggedRows(true)
Expand Down Expand Up @@ -146,8 +147,19 @@ public void testToPbAndFromPb() {

@Test
public void testSetProjectId() {
LoadConfiguration configuration = LOAD_CONFIGURATION_CSV.setProjectId("p");
assertEquals("p", configuration.getDestinationTable().getProject());
LoadConfiguration configuration = LOAD_CONFIGURATION_CSV.setProjectId(TEST_PROJECT_ID);
assertEquals(TEST_PROJECT_ID, configuration.getDestinationTable().getProject());
}

@Test
public void testSetProjectIdDoNotOverride() {
LoadConfiguration configuration =
LOAD_CONFIGURATION_CSV
.toBuilder()
.setDestinationTable(TABLE_ID.setProjectId(TEST_PROJECT_ID))
.build()
.setProjectId("do-not-update");
assertEquals(TEST_PROJECT_ID, configuration.getDestinationTable().getProject());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

public class QueryJobConfigurationTest {

private static final String TEST_PROJECT_ID = "test-project-id";
private static final String QUERY = "BigQuery SQL";
private static final DatasetId DATASET_ID = DatasetId.of("dataset");
private static final TableId TABLE_ID = TableId.of("dataset", "table");
Expand Down Expand Up @@ -140,9 +141,21 @@ public void testToPbAndFromPb() {

@Test
public void testSetProjectId() {
QueryJobConfiguration configuration = QUERY_JOB_CONFIGURATION.setProjectId("p");
assertEquals("p", configuration.getDefaultDataset().getProject());
assertEquals("p", configuration.getDestinationTable().getProject());
QueryJobConfiguration configuration = QUERY_JOB_CONFIGURATION.setProjectId(TEST_PROJECT_ID);
assertEquals(TEST_PROJECT_ID, configuration.getDefaultDataset().getProject());
assertEquals(TEST_PROJECT_ID, configuration.getDestinationTable().getProject());
}

@Test
public void testSetProjectIdDoNotOverride() {
QueryJobConfiguration configuration =
QUERY_JOB_CONFIGURATION
.toBuilder()
.setDestinationTable(TABLE_ID.setProjectId(TEST_PROJECT_ID))
.build()
.setProjectId("update-only-on-dataset");
assertEquals("update-only-on-dataset", configuration.getDefaultDataset().getProject());
assertEquals(TEST_PROJECT_ID, configuration.getDestinationTable().getProject());
}

@Test
Expand Down