ahmedabu98 commented on code in PR #29114:
URL: https://github.com/apache/beam/pull/29114#discussion_r1374655234
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.java:
##########
@@ -2964,6 +2972,17 @@ public Write<T> withPrimaryKey(List<String> primaryKey) {
return toBuilder().setPrimaryKey(primaryKey).build();
}
+ /** Specify how missing values should be interpreted when there is a
default value in the schema. Options are to
+ * take the default value or to write an explicit null (not an option of
the field is also required.). Note: this
+ * is only used when using one of the storage write API insert methods.
Review Comment:
Can we add some validation that fails when this feature is set with other
write methods?
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/TableRowToStorageApiProto.java:
##########
@@ -213,10 +213,15 @@ private static String
getPrettyFieldName(SchemaInformation schema) {
.put(TableFieldSchema.Type.JSON, "JSON")
.build();
- public static TableFieldSchema.Mode modeToProtoMode(String mode) {
+ public static TableFieldSchema.Mode modeToProtoMode(
+ @Nullable String defaultValueExpression, String mode) {
+ // If there is a default value expression, treat this field as if it were
nullable.
+ if (defaultValueExpression != null) {
+ return TableFieldSchema.Mode.NULLABLE;
+ }
Review Comment:
+1 to getting BQ team's perspective on this to align expectations @yirutang
##########
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/StorageApiSinkDefaultValuesIT.java:
##########
@@ -0,0 +1,280 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.io.gcp.bigquery;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import com.google.api.services.bigquery.model.Table;
+import com.google.api.services.bigquery.model.TableFieldSchema;
+import com.google.api.services.bigquery.model.TableReference;
+import com.google.api.services.bigquery.model.TableRow;
+import com.google.api.services.bigquery.model.TableSchema;
+import com.google.cloud.bigquery.storage.v1.AppendRowsRequest;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.extensions.gcp.options.GcpOptions;
+import org.apache.beam.sdk.io.gcp.testing.BigqueryClient;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.transforms.SerializableFunction;
+import org.apache.beam.sdk.util.Preconditions;
+import org.apache.beam.sdk.values.PCollection;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Lists;
+import org.joda.time.Duration;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public class StorageApiSinkDefaultValuesIT {
+ private static final BigqueryClient BQ_CLIENT =
+ new BigqueryClient("StorageApiSinkDefaultValuesIT");
+ private static final String PROJECT =
+ TestPipeline.testingPipelineOptions().as(GcpOptions.class).getProject();
+ private static final String BIG_QUERY_DATASET_ID =
+ "storage_api_sink_default_values" + System.nanoTime();
+
+ private static String bigQueryLocation;
+
+ @BeforeClass
+ public static void setUpTestEnvironment() throws IOException,
InterruptedException {
+ // Create one BQ dataset for all test cases.
+ bigQueryLocation =
+
TestPipeline.testingPipelineOptions().as(TestBigQueryOptions.class).getBigQueryLocation();
+ BQ_CLIENT.createNewDataset(PROJECT, BIG_QUERY_DATASET_ID, null,
bigQueryLocation);
+ }
+
+ @AfterClass
+ public static void cleanup() {
+ BQ_CLIENT.deleteDataset(PROJECT, BIG_QUERY_DATASET_ID);
+ }
+
+ private static String createAndGetTablespec(TableSchema tableSchema)
+ throws IOException, InterruptedException {
+ String tableName = "table" + System.nanoTime();
+ TableReference tableReference =
+ new TableReference()
+ .setProjectId(PROJECT)
+ .setDatasetId(BIG_QUERY_DATASET_ID)
+ .setTableId(tableName);
+ BQ_CLIENT.createNewTable(
+ PROJECT,
+ BIG_QUERY_DATASET_ID,
+ new Table().setSchema(tableSchema).setTableReference(tableReference));
+ return PROJECT + "." + BIG_QUERY_DATASET_ID + "." + tableName;
+ }
+
+ @Test
+ public void testMissingValueSchemaKnownTakeDefault() throws IOException,
InterruptedException {
+ runTest(true, true, false);
+ }
+
+ @Test
+ public void testMissingRequiredValueSchemaKnownTakeDefault()
+ throws IOException, InterruptedException {
+ runTest(true, true, true);
+ }
+
+ @Test
+ public void testMissingRequiredValueSchemaKnownTakeNull()
+ throws IOException, InterruptedException {
+ runTest(true, false, true);
+ }
+
+ @Test
+ public void testMissingRequiredValueSchemaUnknownTakeDefault()
+ throws IOException, InterruptedException {
+ runTest(false, true, true);
+ }
+
+ @Test
+ public void testMissingValueSchemaUnknownTakeDefault() throws IOException,
InterruptedException {
+
+ runTest(false, true, false);
+ }
+
+ @Test
+ public void testMissingValueSchemaKnownTakeNull() throws IOException,
InterruptedException {
+ runTest(true, false, false);
+ }
+
+ @Test
+ @Ignore // This currently appears broke in BigQuery.
+ public void testMissingValueSchemaUnknownTakeNull() throws IOException,
InterruptedException {
+ runTest(false, false, false);
+ }
+
+ public void runTest(
+ boolean sinkKnowsDefaultFields, boolean takeDefault, boolean
defaultFieldsRequired)
+ throws IOException, InterruptedException {
+ boolean expectDeadLetter = !takeDefault && defaultFieldsRequired;
+ TableSchema bqSchema;
+ if (defaultFieldsRequired) {
+ bqSchema =
+ new TableSchema()
+ .setFields(
+ ImmutableList.of(
+ new TableFieldSchema().setName("id").setType("STRING"),
+ new TableFieldSchema().setName("key2").setType("STRING"),
+ new
TableFieldSchema().setName("value").setType("STRING"),
+ new TableFieldSchema()
+ .setName("defaultliteral")
+ .setType("INT64")
+ .setDefaultValueExpression("42")
+ .setMode("REQUIRED"),
+ new TableFieldSchema()
+ .setName("defaulttime")
+ .setType("TIMESTAMP")
+ .setDefaultValueExpression("CURRENT_TIMESTAMP()")
+ .setMode("REQUIRED")));
Review Comment:
Can we add a REPEATED field with a default? I worry that changing the mode
to NULLABLE in `modeToProtoMode` may cause some weird things to happen
##########
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/StorageApiSinkDefaultValuesIT.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.io.gcp.bigquery;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import com.google.api.services.bigquery.model.Table;
+import com.google.api.services.bigquery.model.TableFieldSchema;
+import com.google.api.services.bigquery.model.TableReference;
+import com.google.api.services.bigquery.model.TableRow;
+import com.google.api.services.bigquery.model.TableSchema;
+import com.google.cloud.bigquery.storage.v1.AppendRowsRequest;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.extensions.gcp.options.GcpOptions;
+import org.apache.beam.sdk.io.gcp.testing.BigqueryClient;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.util.Preconditions;
+import org.apache.beam.sdk.values.PCollection;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Lists;
+import org.joda.time.Duration;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public class StorageApiSinkDefaultValuesIT {
+ private static final BigqueryClient BQ_CLIENT =
+ new BigqueryClient("StorageApiSinkDefaultValuesIT");
+ private static final String PROJECT =
+ TestPipeline.testingPipelineOptions().as(GcpOptions.class).getProject();
+ private static final String BIG_QUERY_DATASET_ID =
+ "storage_api_sink_default_values" + System.nanoTime();
+
+ private static String bigQueryLocation;
+
+ @BeforeClass
+ public static void setUpTestEnvironment() throws IOException,
InterruptedException {
+ // Create one BQ dataset for all test cases.
+ bigQueryLocation =
+
TestPipeline.testingPipelineOptions().as(TestBigQueryOptions.class).getBigQueryLocation();
+ BQ_CLIENT.createNewDataset(PROJECT, BIG_QUERY_DATASET_ID, null,
bigQueryLocation);
+ }
+
+ @AfterClass
+ public static void cleanup() {
+ BQ_CLIENT.deleteDataset(PROJECT, BIG_QUERY_DATASET_ID);
+ }
+
+ private static String createAndGetTablespec(TableSchema tableSchema)
+ throws IOException, InterruptedException {
+ String tableName = "table" + System.nanoTime();
+ TableReference tableReference =
+ new TableReference()
+ .setProjectId(PROJECT)
+ .setDatasetId(BIG_QUERY_DATASET_ID)
+ .setTableId(tableName);
+ BQ_CLIENT.createNewTable(
+ PROJECT,
+ BIG_QUERY_DATASET_ID,
+ new Table().setSchema(tableSchema).setTableReference(tableReference));
+ return PROJECT + "." + BIG_QUERY_DATASET_ID + "." + tableName;
+ }
+
+ @Test
+ public void testMissingValueSchemaKnownTakeDefault() throws IOException,
InterruptedException {
+ runTest(true, true, false);
+ }
+
+ @Test
+ public void testMissingRequiredValueSchemaKnownTakeDefault()
+ throws IOException, InterruptedException {
+ runTest(true, true, true);
+ }
+
+ @Test
+ public void testMissingRequiredValueSchemaUnknownTakeDefault()
+ throws IOException, InterruptedException {
+ runTest(false, true, true);
+ }
+
+ @Test
+ public void testMissingValueSchemaUnknownTakeDefault() throws IOException,
InterruptedException {
+
+ runTest(false, true, false);
+ }
+
+ @Test
+ public void testMissingValueSchemaKnownTakeNull() throws IOException,
InterruptedException {
+ runTest(true, false, false);
+ }
+
+ @Test
+ @Ignore // This currently appears broke in BigQuery.
+ public void testMissingValueSchemaUnknownTakeNull() throws IOException,
InterruptedException {
+ runTest(false, false, false);
+ }
+
+ public void runTest(
+ boolean sinkKnowsDefaultFields, boolean takeDefault, boolean
defaultFieldsRequired)
+ throws IOException, InterruptedException {
+ TableSchema bqSchema;
+ if (defaultFieldsRequired) {
+ bqSchema =
+ new TableSchema()
+ .setFields(
+ ImmutableList.of(
+ new TableFieldSchema().setName("id").setType("STRING"),
+ new TableFieldSchema().setName("key2").setType("STRING"),
+ new
TableFieldSchema().setName("value").setType("STRING"),
+ new TableFieldSchema()
+ .setName("defaultliteral")
+ .setType("INT64")
+ .setDefaultValueExpression("42")
+ .setMode("REQUIRED"),
+ new TableFieldSchema()
+ .setName("defaulttime")
+ .setType("TIMESTAMP")
+ .setDefaultValueExpression("CURRENT_TIMESTAMP()")
+ .setMode("REQUIRED")));
+ } else {
+ bqSchema =
+ new TableSchema()
+ .setFields(
+ ImmutableList.of(
+ new TableFieldSchema().setName("id").setType("STRING"),
+ new TableFieldSchema().setName("key2").setType("STRING"),
+ new
TableFieldSchema().setName("value").setType("STRING"),
+ new TableFieldSchema()
+ .setName("defaultliteral")
+ .setType("INT64")
+ .setDefaultValueExpression("42"),
+ new TableFieldSchema()
+ .setName("defaulttime")
+ .setType("TIMESTAMP")
+ .setDefaultValueExpression("CURRENT_TIMESTAMP()")));
+ }
+
+ TableSchema sinkSchema = bqSchema;
+ if (!sinkKnowsDefaultFields) {
+ sinkSchema =
+ new TableSchema()
+ .setFields(
+ bqSchema.getFields().stream()
+ .filter(tfs -> tfs.getDefaultValueExpression() == null)
+ .collect(Collectors.toList()));
+ }
+ final TableRow row1 =
+ new TableRow()
+ .set("id", "row1")
+ .set("key2", "bar0")
+ .set("value", "1")
+ .set("defaultliteral", 12);
+ final TableRow row2 = new TableRow().set("id", "row2").set("key2",
"bar1").set("value", "1");
+ final TableRow row3 = new TableRow().set("id", "row3").set("key2",
"bar2").set("value", "2");
+
+ List<TableRow> tableRows = Lists.newArrayList(row1, row2, row3);
+
+ String tableSpec = createAndGetTablespec(bqSchema);
+ Pipeline p = Pipeline.create();
+
+ BigQueryIO.Write<TableRow> write =
+ BigQueryIO.writeTableRows()
+ .to(tableSpec)
+ .withSchema(sinkSchema)
+ .withNumStorageWriteApiStreams(2)
+ .ignoreUnknownValues()
+ .withTriggeringFrequency(Duration.standardSeconds(1))
+ .withMethod(BigQueryIO.Write.Method.STORAGE_WRITE_API)
+
.withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_NEVER);
+ if (!takeDefault) {
+ write =
+ write.withDefaultMissingValueInterpretation(
+ AppendRowsRequest.MissingValueInterpretation.NULL_VALUE);
Review Comment:
SGTM we don't have to support that mode for now, it sounds very close to
what REQUIRED field mode does anyways
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]