You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "reuvenlax (via GitHub)" <gi...@apache.org> on 2023/10/23 23:52:43 UTC
[PR] Support default values in storage-api sink [beam]
reuvenlax opened a new pull request, #29114:
URL: https://github.com/apache/beam/pull/29114
(no comment)
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax commented on code in PR #29114:
URL: https://github.com/apache/beam/pull/29114#discussion_r1370813802
##########
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()
Review Comment:
the latter. we are testing overriding the default with an explicit value, which requires us to know the schema. This is mostly so I don't have to create a separate input set for the tests where the input is not know.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #29114:
URL: https://github.com/apache/beam/pull/29114#issuecomment-1776281492
Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment `assign set of reviewers`
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax commented on PR #29114:
URL: https://github.com/apache/beam/pull/29114#issuecomment-1786162241
Run Java_GCP_IO_Direct PreCommit
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "ahmedabu98 (via GitHub)" <gi...@apache.org>.
ahmedabu98 commented on code in PR #29114:
URL: https://github.com/apache/beam/pull/29114#discussion_r1370710789
##########
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);
+ }
+ p.apply("Create rows", Create.of(tableRows))
+ .setIsBoundedInternal(PCollection.IsBounded.UNBOUNDED)
+ .apply("write", write);
+ p.run();
+
+ Map<String, TableRow> queryResponse =
+ BQ_CLIENT
+ .queryUnflattened(
+ String.format("SELECT * FROM %s", tableSpec), PROJECT, true, true, bigQueryLocation)
+ .stream()
+ .collect(Collectors.toMap(tr -> (String) tr.get("id"), Function.identity()));
+ assertThat(queryResponse.size(), equalTo(3));
+ System.err.println("MAP IS " + queryResponse);
Review Comment:
nit: cleanup
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.java:
##########
@@ -2143,6 +2144,8 @@ public static <T> Write<T> write() {
.setMaxRetryJobs(1000)
.setPropagateSuccessfulStorageApiWrites(false)
.setDirectWriteProtos(true)
+ .setDefaultMissingValueInterpretation(
+ AppendRowsRequest.MissingValueInterpretation.DEFAULT_VALUE)
Review Comment:
the old behavior would set missing values as `null`. Looking at [BQ docs](https://cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/2.38.0/com.google.cloud.bigquery.storage.v1.AppendRowsRequest.MissingValueInterpretation), if DEFAULT_VALUE is set but there is no default actually declared on the schema, it will write a null value.
The only affected case is for users that declared a default value. Instead of writing missing values as null, the sink would now write the default value. In that sense, this is probably more of a "bug fix" than a breaking change?
##########
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:
Should this also depend on the specified `MissingValueInterpretation`? For example if one chooses `MissingValueInterpretation.MISSING_VALUE_INTERPRETATION_UNSPECIFIED`, should we be keeping the field as required?
Also, how does this play out if the field's original mode is `REPEATED`?
##########
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()
Review Comment:
qq: is `ignoreUnknownValues` required to use missing value interpretation? or is it just added here so we can write the first row with "defaultliteral" missing from the schema?
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.java:
##########
@@ -2964,6 +2972,11 @@ public Write<T> withPrimaryKey(List<String> primaryKey) {
return toBuilder().setPrimaryKey(primaryKey).build();
}
+ public Write<T> withDefaultMissingValueInterpretation(
Review Comment:
Would be helpful to add some documentation on what this does and what the possible enums are
##########
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:
What happens when we specify `MISSING_VALUE_INTERPRETATION_UNSPECIFIED`?
##########
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:
More broadly, is there a good reason to change the field's mode? One can argue that `REQUIRED` mode should remain strict, even in the presence of a default value.
I don't have strong feelings about this but if we design it this way, it should be mentioned in the documentation.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax commented on code in PR #29114:
URL: https://github.com/apache/beam/pull/29114#discussion_r1372107806
##########
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);
+ }
+ p.apply("Create rows", Create.of(tableRows))
+ .setIsBoundedInternal(PCollection.IsBounded.UNBOUNDED)
+ .apply("write", write);
+ p.run();
+
+ Map<String, TableRow> queryResponse =
+ BQ_CLIENT
+ .queryUnflattened(
+ String.format("SELECT * FROM %s", tableSpec), PROJECT, true, true, bigQueryLocation)
+ .stream()
+ .collect(Collectors.toMap(tr -> (String) tr.get("id"), Function.identity()));
+ assertThat(queryResponse.size(), equalTo(3));
+ System.err.println("MAP IS " + queryResponse);
Review Comment:
done
##########
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:
added extra validation
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax commented on PR #29114:
URL: https://github.com/apache/beam/pull/29114#issuecomment-1785908916
Run Java_GCP_IO_Direct PreCommit
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax commented on PR #29114:
URL: https://github.com/apache/beam/pull/29114#issuecomment-1776542190
R: @ahmedabu98
R: @johnjcasey
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "ahmedabu98 (via GitHub)" <gi...@apache.org>.
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "ahmedabu98 (via GitHub)" <gi...@apache.org>.
ahmedabu98 commented on code in PR #29114:
URL: https://github.com/apache/beam/pull/29114#discussion_r1375472339
##########
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/StorageApiSinkDefaultValuesIT.java:
##########
@@ -0,0 +1,317 @@
+/*
+ * 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.containsInAnyOrder;
+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.Collection;
+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.hamcrest.Matchers;
+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);
+ }
Review Comment:
Non-blocking but do we know why this is broken?
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax commented on PR #29114:
URL: https://github.com/apache/beam/pull/29114#issuecomment-1781846425
Run Java_GCP_IO_Direct PreCommit
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax commented on code in PR #29114:
URL: https://github.com/apache/beam/pull/29114#discussion_r1370810930
##########
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:
this is a bit weird here:
- REQUIRED is basically a noop if taking a default value, as the value will never be missing.
- However if defaulting to NULL, then REQUIRED still has meaning.
- Also users can explicitly pass in NULL for field values (overriding the default value). In this case, REQUIRED prevents the user from doing this (I think).
The second and third bullet points aren't properly handled by this code, as I'm not entirely sure how to express that in the proto schema being generated here. I think this validation will still happen on the BQ side, so failing rows will still end up in the failedRows PCollection. We should probably double check the expected semantics with the BQ team though.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #29114:
URL: https://github.com/apache/beam/pull/29114#issuecomment-1776543235
Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax commented on code in PR #29114:
URL: https://github.com/apache/beam/pull/29114#discussion_r1372106446
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.java:
##########
@@ -2964,6 +2972,11 @@ public Write<T> withPrimaryKey(List<String> primaryKey) {
return toBuilder().setPrimaryKey(primaryKey).build();
}
+ public Write<T> withDefaultMissingValueInterpretation(
Review Comment:
done
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax merged PR #29114:
URL: https://github.com/apache/beam/pull/29114
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax commented on PR #29114:
URL: https://github.com/apache/beam/pull/29114#issuecomment-1781967614
All tests now passing
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "johnjcasey (via GitHub)" <gi...@apache.org>.
johnjcasey commented on code in PR #29114:
URL: https://github.com/apache/beam/pull/29114#discussion_r1370418741
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.java:
##########
@@ -2143,6 +2144,8 @@ public static <T> Write<T> write() {
.setMaxRetryJobs(1000)
.setPropagateSuccessfulStorageApiWrites(false)
.setDirectWriteProtos(true)
+ .setDefaultMissingValueInterpretation(
+ AppendRowsRequest.MissingValueInterpretation.DEFAULT_VALUE)
Review Comment:
I just want to double check we aren't breaking backwards compatability
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.java:
##########
@@ -2143,6 +2144,8 @@ public static <T> Write<T> write() {
.setMaxRetryJobs(1000)
.setPropagateSuccessfulStorageApiWrites(false)
.setDirectWriteProtos(true)
+ .setDefaultMissingValueInterpretation(
+ AppendRowsRequest.MissingValueInterpretation.DEFAULT_VALUE)
Review Comment:
Is DEFAULT_VALUE our current default, or do we default to null?
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax commented on code in PR #29114:
URL: https://github.com/apache/beam/pull/29114#discussion_r1375177602
##########
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:
done
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax commented on PR #29114:
URL: https://github.com/apache/beam/pull/29114#issuecomment-1785909605
Run PreCommit Java Examples Dataflow
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax commented on code in PR #29114:
URL: https://github.com/apache/beam/pull/29114#discussion_r1372107572
##########
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:
Added a test - passing in NULL still fails at the BQ level, and the row ends up in the failedRows PCollection.
Note: if there is a default value, then we _should_ make the proto field optional, as otherwise there is no good way to take the default value.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax commented on code in PR #29114:
URL: https://github.com/apache/beam/pull/29114#discussion_r1370814564
##########
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:
It should fail on the BQ side. We can validate this on our side too so that the failure comes earlier.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support default values in storage-api sink [beam]
Posted by "reuvenlax (via GitHub)" <gi...@apache.org>.
reuvenlax commented on code in PR #29114:
URL: https://github.com/apache/beam/pull/29114#discussion_r1370804678
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.java:
##########
@@ -2143,6 +2144,8 @@ public static <T> Write<T> write() {
.setMaxRetryJobs(1000)
.setPropagateSuccessfulStorageApiWrites(false)
.setDirectWriteProtos(true)
+ .setDefaultMissingValueInterpretation(
+ AppendRowsRequest.MissingValueInterpretation.DEFAULT_VALUE)
Review Comment:
Yeah - the BigQuery sink didn't really support default values before, so I consider the previous behavior as accidental, undefined behavior. Doesn't mean that customers didn't rely on it, but I'm ok with changing it especially since we're still providing a way to default to null.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org