liferoad commented on code in PR #36397:
URL: https://github.com/apache/beam/pull/36397#discussion_r2411515783


##########
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryNestedFFieldIT.java:
##########
@@ -0,0 +1,211 @@
+/*
+ * 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.junit.Assert.assertEquals;
+
+import com.google.api.services.bigquery.model.QueryResponse;
+import com.google.api.services.bigquery.model.TableFieldSchema;
+import com.google.api.services.bigquery.model.TableRow;
+import com.google.api.services.bigquery.model.TableSchema;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import java.io.IOException;
+import java.security.SecureRandom;
+import java.util.TreeMap;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.PipelineResult;
+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.DoFn;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.PCollection;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Integration test for BigQuery Storage API write with nested structures 
containing 'f' field. This
+ * test verifies the fix for IllegalArgumentException when setting a List 
field to Double in nested
+ * TableRow structures, based on the scenario from BigQuerySetFPipeline.java.
+ */
+@RunWith(JUnit4.class)
+public class BigQueryNestedFFieldIT {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(BigQueryNestedFFieldIT.class);
+  private static String project;
+  private static final String DATASET_ID =
+      "nested_f_field_it_" + System.currentTimeMillis() + "_" + new 
SecureRandom().nextInt(32);
+  private static final String TABLE_NAME = "nested_f_field_test";
+
+  private static TestBigQueryOptions bqOptions;
+  private static final BigqueryClient BQ_CLIENT = new 
BigqueryClient("BigQueryNestedFFieldIT");
+
+  @BeforeClass
+  public static void setup() throws Exception {
+    bqOptions = 
TestPipeline.testingPipelineOptions().as(TestBigQueryOptions.class);
+    project = bqOptions.as(GcpOptions.class).getProject();
+    // Create one BQ dataset for all test cases.
+    BQ_CLIENT.createNewDataset(project, DATASET_ID, null, 
bqOptions.getBigQueryLocation());
+  }
+
+  @AfterClass
+  public static void cleanup() {
+    BQ_CLIENT.deleteDataset(project, DATASET_ID);
+  }
+
+  /**
+   * Test case that reproduces the scenario from BigQuerySetFPipeline.java 
where a nested structure
+   * contains an 'f' field with a float value. This tests the fix for the 
IllegalArgumentException
+   * that occurred when TableRowToStorageApiProto tried to set a List field to 
a Double value.
+   */
+  @Test
+  public void testNestedFFieldWithFloat() throws IOException, 
InterruptedException {
+    // Define the table schema with nested structure containing 'f' field
+    TableSchema schema =
+        new TableSchema()
+            .setFields(
+                ImmutableList.of(
+                    new TableFieldSchema().setName("bytes").setType("BYTES"),
+                    new TableFieldSchema()
+                        .setName("sub")
+                        .setType("RECORD")
+                        .setFields(
+                            ImmutableList.of(
+                                new 
TableFieldSchema().setName("a").setType("STRING"),
+                                new 
TableFieldSchema().setName("c").setType("INTEGER"),
+                                new 
TableFieldSchema().setName("f").setType("FLOAT")))));
+
+    String tableSpec = String.format("%s:%s.%s", project, DATASET_ID, 
TABLE_NAME);
+
+    // Set up pipeline options for Storage API
+    bqOptions.setUseStorageWriteApi(true);
+    bqOptions.setUseStorageWriteApiAtLeastOnce(true);
+
+    Pipeline pipeline = Pipeline.create(bqOptions);
+
+    // Create test data similar to BigQuerySetFPipeline.java
+    WriteResult result =
+        pipeline
+            .apply("CreateInput", Create.of("test"))
+            .apply("GenerateTestData", ParDo.of(new GenerateTestDataFn()))
+            .apply("CreateTableRows", MapElements.via(new CreateTableRowFn()))
+            .apply(
+                "WriteToBigQuery",
+                BigQueryIO.writeTableRows()
+                    .to(tableSpec)
+                    .withSchema(schema)
+                    
.withMethod(BigQueryIO.Write.Method.STORAGE_API_AT_LEAST_ONCE)
+                    
.withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED)
+                    
.withWriteDisposition(BigQueryIO.Write.WriteDisposition.WRITE_APPEND));
+
+    // Validate failed inserts using PAssert
+    PCollection<BigQueryStorageApiInsertError> failedInserts = 
result.getFailedStorageApiInserts();
+
+    // Assert that we expect exactly 3 failed inserts (entire batch fails when 
one row exceeds size
+    // limit)
+    // The test intentionally creates a batch with one row that exceeds 
BigQuery's size limit
+    PAssert.that(failedInserts)
+        .satisfies(
+            (Iterable<BigQueryStorageApiInsertError> errors) -> {
+              int count = 0;
+              for (BigQueryStorageApiInsertError error : errors) {
+                count++;
+                if (!error.getErrorMessage().contains("Row payload too 
large")) {
+                  throw new AssertionError(
+                      "Expected 'Row payload too large' error, got: " + 
error.getErrorMessage());
+                }
+              }
+              if (count != 3) {

Review Comment:
   
https://github.com/baeminbo/dataflow-pipelines/blob/master/bq-limit/src/main/java/baeminbo/BigQuerySetFPipelineFixed.java#L61
 has the original test. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to