From 2c891e78c8143de7cb98c76c4d4350913b2b3069 Mon Sep 17 00:00:00 2001 From: obada-ab Date: Mon, 15 May 2023 14:33:39 +0200 Subject: [PATCH 1/9] fix: add support for repeated record query parameters Add support for array of struct query parameters which are used to query repeated record fields --- .../cloud/bigquery/QueryParameterValue.java | 20 +++++++-- .../bigquery/QueryParameterValueTest.java | 43 +++++++++++++++++++ .../cloud/bigquery/it/ITBigQueryTest.java | 41 ++++++++++++++++++ 3 files changed, 100 insertions(+), 4 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryParameterValue.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryParameterValue.java index b92cb734c..a40743b52 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryParameterValue.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryParameterValue.java @@ -37,6 +37,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import javax.annotation.Nullable; import org.threeten.bp.Instant; import org.threeten.bp.ZoneOffset; @@ -349,7 +350,11 @@ public static QueryParameterValue array(T[] array, Class clazz) { public static QueryParameterValue array(T[] array, StandardSQLTypeName type) { List listValues = new ArrayList<>(); for (T obj : array) { - listValues.add(QueryParameterValue.of(obj, type)); + if (type == StandardSQLTypeName.STRUCT) { + listValues.add((QueryParameterValue) obj); + } else { + listValues.add(QueryParameterValue.of(obj, type)); + } } return QueryParameterValue.newBuilder() .setArrayValues(listValues) @@ -522,9 +527,16 @@ QueryParameterType toTypePb() { QueryParameterType typePb = new QueryParameterType(); typePb.setType(getType().toString()); if (getArrayType() != null) { - QueryParameterType arrayTypePb = new QueryParameterType(); - arrayTypePb.setType(getArrayType().toString()); - typePb.setArrayType(arrayTypePb); + if (getArrayType() == StandardSQLTypeName.STRUCT) { + List values = + Objects.requireNonNull(getArrayValues(), "Array of struct cannot be empty"); + QueryParameterType structType = values.get(0).toTypePb(); + typePb.setArrayType(structType); + } else { + QueryParameterType arrayTypePb = new QueryParameterType(); + arrayTypePb.setType(getArrayType().toString()); + typePb.setArrayType(arrayTypePb); + } } if (getStructTypes() != null) { List structTypes = new ArrayList<>(); diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryParameterValueTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryParameterValueTest.java index 05920df23..0534865b2 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryParameterValueTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryParameterValueTest.java @@ -28,6 +28,7 @@ import java.math.BigDecimal; import java.text.ParseException; import java.time.Period; +import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -563,6 +564,48 @@ public void testNestedStruct() { assertThat(nestedRecordField.getStructValues().size()).isEqualTo(structValue.size()); } + @Test + public void testStructArray() { + Boolean[] boolValues = new Boolean[] {true, false}; + Integer[] intValues = new Integer[] {15, 20}; + String[] stringValues = new String[] {"test-string", "test-string2"}; + List> fieldMaps = new ArrayList<>(); + List tuples = new ArrayList<>(); + for (int i = 0; i < 2; i++) { + QueryParameterValue booleanField = QueryParameterValue.bool(boolValues[i]); + QueryParameterValue integerField = QueryParameterValue.int64(intValues[i]); + QueryParameterValue stringField = QueryParameterValue.string(stringValues[i]); + ImmutableMap fieldMap = + ImmutableMap.of( + "booleanField", + booleanField, + "integerField", + integerField, + "stringField", + stringField); + fieldMaps.add(fieldMap); + QueryParameterValue recordField = QueryParameterValue.struct(fieldMap); + tuples.add(recordField); + } + QueryParameterValue repeatedRecordField = + QueryParameterValue.array(tuples.toArray(), StandardSQLTypeName.STRUCT); + com.google.api.services.bigquery.model.QueryParameterValue parameterValue = + repeatedRecordField.toValuePb(); + QueryParameterType parameterType = repeatedRecordField.toTypePb(); + QueryParameterValue queryParameterValue = + QueryParameterValue.fromPb(parameterValue, parameterType); + assertThat(queryParameterValue.getValue()).isNull(); + assertThat(queryParameterValue.getType()).isEqualTo(StandardSQLTypeName.ARRAY); + assertThat(queryParameterValue.getArrayType()).isEqualTo(StandardSQLTypeName.STRUCT); + assertThat(queryParameterValue.getArrayValues().size()).isEqualTo(2); + for (int i = 0; i < 2; i++) { + QueryParameterValue record = queryParameterValue.getArrayValues().get(i); + assertThat(record.getType()).isEqualTo(StandardSQLTypeName.STRUCT); + assertThat(record.getStructTypes()).isNotNull(); + assertThat(record.getStructValues()).isEqualTo(fieldMaps.get(i)); + } + } + private static void assertArrayDataEquals( String[] expectedValues, StandardSQLTypeName expectedType, diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index 6d61fd211..d6355129a 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -4062,6 +4062,47 @@ public void testStructNamedQueryParameters() throws InterruptedException { } } + @Test + public void testRepeatedRecordNamedQueryParameters() throws InterruptedException { + String[] stringValues = new String[] {"test-stringField", "test-stringField2"}; + List tuples = new ArrayList<>(); + for (int i = 0; i < 2; i++) { + QueryParameterValue stringValue = QueryParameterValue.string(stringValues[i]); + Map struct = new HashMap<>(); + struct.put("stringField", stringValue); + QueryParameterValue recordValue = QueryParameterValue.struct(struct); + tuples.add(recordValue); + } + + QueryParameterValue repeatedRecord = + QueryParameterValue.array(tuples.toArray(), StandardSQLTypeName.STRUCT); + String query = "SELECT @repeatedRecordField AS repeatedRecord"; + QueryJobConfiguration config = + QueryJobConfiguration.newBuilder(query) + .setDefaultDataset(DATASET) + .setUseLegacySql(false) + .addNamedParameter("repeatedRecordField", repeatedRecord) + .build(); + TableResult result = bigquery.query(config); + assertEquals(1, Iterables.size(result.getValues())); + + FieldList subSchema = result.getSchema().getFields().get("repeatedRecord").getSubFields(); + for (FieldValueList values : result.iterateAll()) { + for (FieldValue value : values) { + assertEquals(FieldValue.Attribute.REPEATED, value.getAttribute()); + assertEquals(2, value.getRepeatedValue().size()); + for (int i = 0; i < 2; i++) { + FieldValue record = value.getRepeatedValue().get(i); + assertEquals(FieldValue.Attribute.RECORD, record.getAttribute()); + FieldValueList recordValue = record.getRecordValue(); + assertEquals( + stringValues[i], + FieldValueList.of(recordValue, subSchema).get("stringField").getValue()); + } + } + } + } + @Test public void testStructQuery() throws InterruptedException { // query into a table From eefcdae6071219d99c6132688b948be41588bfdd Mon Sep 17 00:00:00 2001 From: obada-ab Date: Wed, 17 May 2023 14:49:00 +0200 Subject: [PATCH 2/9] fix: set the query parameter type to an empty struct array if values don't exist Make the toTypePb method produce an empty 'ARRAY>' type when no values exist an an array of struct, which was the original behaviour. Add IT tests for UNNESTing struct array and creating empty struct array query parameters. --- .../cloud/bigquery/QueryParameterValue.java | 6 +- .../cloud/bigquery/it/ITBigQueryTest.java | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryParameterValue.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryParameterValue.java index a40743b52..b21445d38 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryParameterValue.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryParameterValue.java @@ -37,7 +37,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Objects; import javax.annotation.Nullable; import org.threeten.bp.Instant; import org.threeten.bp.ZoneOffset; @@ -527,9 +526,8 @@ QueryParameterType toTypePb() { QueryParameterType typePb = new QueryParameterType(); typePb.setType(getType().toString()); if (getArrayType() != null) { - if (getArrayType() == StandardSQLTypeName.STRUCT) { - List values = - Objects.requireNonNull(getArrayValues(), "Array of struct cannot be empty"); + List values = getArrayValues(); + if (getArrayType() == StandardSQLTypeName.STRUCT && values != null && values.size() != 0) { QueryParameterType structType = values.get(0).toTypePb(); typePb.setArrayType(structType); } else { diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index d6355129a..c502848be 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -4103,6 +4103,67 @@ public void testRepeatedRecordNamedQueryParameters() throws InterruptedException } } + @Test + public void testUnnestRepeatedRecordNamedQueryParameter() throws InterruptedException { + Boolean[] boolValues = new Boolean[] {true, false}; + List tuples = new ArrayList<>(); + for (int i = 0; i < 2; i++) { + QueryParameterValue boolValue = QueryParameterValue.bool(boolValues[i]); + Map struct = new HashMap<>(); + struct.put("boolField", boolValue); + QueryParameterValue recordValue = QueryParameterValue.struct(struct); + tuples.add(recordValue); + } + + QueryParameterValue repeatedRecord = + QueryParameterValue.array(tuples.toArray(), StandardSQLTypeName.STRUCT); + String query = + "SELECT * FROM (SELECT STRUCT(" + + boolValues[0] + + " AS boolField) AS repeatedRecord) WHERE repeatedRecord IN UNNEST(@repeatedRecordField)"; + QueryJobConfiguration config = + QueryJobConfiguration.newBuilder(query) + .setDefaultDataset(DATASET) + .setUseLegacySql(false) + .addNamedParameter("repeatedRecordField", repeatedRecord) + .build(); + TableResult result = bigquery.query(config); + assertEquals(1, Iterables.size(result.getValues())); + + FieldList subSchema = result.getSchema().getFields().get("repeatedRecord").getSubFields(); + for (FieldValueList values : result.iterateAll()) { + for (FieldValue value : values) { + assertEquals(FieldValue.Attribute.RECORD, value.getAttribute()); + FieldValueList recordValue = value.getRecordValue(); + assertEquals( + boolValues[0], + FieldValueList.of(recordValue, subSchema).get("boolField").getBooleanValue()); + } + } + } + + @Test + public void testEmptyRepeatedRecordNamedQueryParameters() throws InterruptedException { + QueryParameterValue[] tuples = {}; + + QueryParameterValue repeatedRecord = + QueryParameterValue.array(tuples, StandardSQLTypeName.STRUCT); + String query = + "SELECT * FROM (SELECT STRUCT(false AS boolField) AS repeatedRecord) WHERE repeatedRecord IN UNNEST(@repeatedRecordField)"; + QueryJobConfiguration config = + QueryJobConfiguration.newBuilder(query) + .setDefaultDataset(DATASET) + .setUseLegacySql(false) + .addNamedParameter("repeatedRecordField", repeatedRecord) + .build(); + try { + bigquery.query(config); + fail("an empty array of struct query parameter shouldn't work with 'IN UNNEST'"); + } catch (BigQueryException e) { + // Nothing to do + } + } + @Test public void testStructQuery() throws InterruptedException { // query into a table From f8e36cea5116de7d76a99192b14370a834f5b9aa Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Wed, 17 May 2023 12:58:33 +0000 Subject: [PATCH 3/9] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20po?= =?UTF-8?q?st-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e7c05fa68..3e25a00e3 100644 --- a/README.md +++ b/README.md @@ -53,20 +53,20 @@ If you are using Maven without the BOM, add this to your dependencies: If you are using Gradle 5.x or later, add this to your dependencies: ```Groovy -implementation platform('com.google.cloud:libraries-bom:26.14.0') +implementation platform('com.google.cloud:libraries-bom:26.15.0') implementation 'com.google.cloud:google-cloud-bigquery' ``` If you are using Gradle without BOM, add this to your dependencies: ```Groovy -implementation 'com.google.cloud:google-cloud-bigquery:2.25.0' +implementation 'com.google.cloud:google-cloud-bigquery:2.26.1' ``` If you are using SBT, add this to your dependencies: ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "2.25.0" +libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "2.26.1" ``` @@ -348,7 +348,7 @@ Java is a registered trademark of Oracle and/or its affiliates. [kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-bigquery/java11.html [stability-image]: https://img.shields.io/badge/stability-stable-green [maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-bigquery.svg -[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigquery/2.25.0 +[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigquery/2.26.1 [authentication]: https://github.com/googleapis/google-cloud-java#authentication [auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes [predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles From f15630acdf3ed970bafdf08977dc0cddff751573 Mon Sep 17 00:00:00 2001 From: Neenu1995 Date: Thu, 18 May 2023 18:15:27 -0400 Subject: [PATCH 4/9] chore: add test for a real dataset --- .../cloud/bigquery/it/ITBigQueryTest.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index c502848be..7431c606c 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -4142,6 +4142,61 @@ public void testUnnestRepeatedRecordNamedQueryParameter() throws InterruptedExce } } + @Test + public void testUnnestRepeatedRecordNamedQueryParameterFromDataset() throws InterruptedException { + List tuples = new ArrayList<>(); + QueryParameterValue statusValue = QueryParameterValue.string("single"); + QueryParameterValue addressValue = QueryParameterValue.string("123 this lane"); + QueryParameterValue cityValue = QueryParameterValue.string("Torono"); + QueryParameterValue stateValue = QueryParameterValue.string("ON"); + QueryParameterValue zipValue = QueryParameterValue.string("1h2j34"); + QueryParameterValue numberOfYearsValue = QueryParameterValue.string("3"); + + Map struct = new HashMap<>(); + struct.put("statusValue", statusValue); + struct.put("addressValue", addressValue); + struct.put("cityValue", cityValue); + struct.put("stateValue", stateValue); + struct.put("zipValue", zipValue); + struct.put("numberOfYearsValue", numberOfYearsValue); + QueryParameterValue recordValue = QueryParameterValue.struct(struct); + tuples.add(recordValue); + + QueryParameterValue repeatedRecord = + QueryParameterValue.array(tuples.toArray(), StandardSQLTypeName.STRUCT); + + String unnestRecord = "SELECT TEMP from `neenutestproject.AVRODATASET.NESTEDREPEATEDTABLE`, UNNEST(@repeatedRecord) AS TEMP "; + QueryJobConfiguration queryJobConfiguration = QueryJobConfiguration.newBuilder(unnestRecord) + .setUseLegacySql(false) + .addNamedParameter("repeatedRecord", repeatedRecord) + .build(); + TableResult unnestResult = bigquery.query(queryJobConfiguration); + FieldValueList rowValue = unnestResult.getValues().iterator().next(); + //Why is this printing the one row 4 times? + unnestResult + .iterateAll() + .forEach(row -> row.forEach(val -> System.out.printf("%s\n", val.toString()))); + + System.out.println("**************FROM DATASET****************"); + // addresses is a REPEATED RECORD field with an array of Structs of the above type. + // @repeatedRecord is array of struct type namedQueryParameter with 1 struct + // the UI does return the correct 1 row output + // this test returns 0 results. + String query = "SELECT * FROM `neenutestproject.AVRODATASET.NESTEDREPEATEDTABLE`, UNNEST(@repeatedRecord) AS TEMP where TEMP IN UNNEST(addresses);"; + QueryJobConfiguration queryConfig = + QueryJobConfiguration.newBuilder(query) + .setUseLegacySql(false) + .addNamedParameter("repeatedRecord", repeatedRecord) + .build(); + TableResult results = bigquery.query(queryConfig); + + System.out.println( Iterables.size(results.getValues())); + results + .iterateAll() + .forEach(row -> row.forEach(val -> System.out.printf("%s", val.toString()))); + System.out.println("Query with Array of struct parameter performed successfully."); + } + @Test public void testEmptyRepeatedRecordNamedQueryParameters() throws InterruptedException { QueryParameterValue[] tuples = {}; From 6fc40856abc8ab489502d740b733156585bcbaaf Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Thu, 18 May 2023 22:17:59 +0000 Subject: [PATCH 5/9] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20po?= =?UTF-8?q?st-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- .../cloud/bigquery/it/ITBigQueryTest.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index 7431c606c..850e6b3d3 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -4145,34 +4145,36 @@ public void testUnnestRepeatedRecordNamedQueryParameter() throws InterruptedExce @Test public void testUnnestRepeatedRecordNamedQueryParameterFromDataset() throws InterruptedException { List tuples = new ArrayList<>(); - QueryParameterValue statusValue = QueryParameterValue.string("single"); + QueryParameterValue statusValue = QueryParameterValue.string("single"); QueryParameterValue addressValue = QueryParameterValue.string("123 this lane"); QueryParameterValue cityValue = QueryParameterValue.string("Torono"); QueryParameterValue stateValue = QueryParameterValue.string("ON"); - QueryParameterValue zipValue = QueryParameterValue.string("1h2j34"); + QueryParameterValue zipValue = QueryParameterValue.string("1h2j34"); QueryParameterValue numberOfYearsValue = QueryParameterValue.string("3"); - Map struct = new HashMap<>(); - struct.put("statusValue", statusValue); + Map struct = new HashMap<>(); + struct.put("statusValue", statusValue); struct.put("addressValue", addressValue); struct.put("cityValue", cityValue); struct.put("stateValue", stateValue); struct.put("zipValue", zipValue); struct.put("numberOfYearsValue", numberOfYearsValue); - QueryParameterValue recordValue = QueryParameterValue.struct(struct); - tuples.add(recordValue); + QueryParameterValue recordValue = QueryParameterValue.struct(struct); + tuples.add(recordValue); QueryParameterValue repeatedRecord = QueryParameterValue.array(tuples.toArray(), StandardSQLTypeName.STRUCT); - String unnestRecord = "SELECT TEMP from `neenutestproject.AVRODATASET.NESTEDREPEATEDTABLE`, UNNEST(@repeatedRecord) AS TEMP "; - QueryJobConfiguration queryJobConfiguration = QueryJobConfiguration.newBuilder(unnestRecord) - .setUseLegacySql(false) - .addNamedParameter("repeatedRecord", repeatedRecord) - .build(); + String unnestRecord = + "SELECT TEMP from `neenutestproject.AVRODATASET.NESTEDREPEATEDTABLE`, UNNEST(@repeatedRecord) AS TEMP "; + QueryJobConfiguration queryJobConfiguration = + QueryJobConfiguration.newBuilder(unnestRecord) + .setUseLegacySql(false) + .addNamedParameter("repeatedRecord", repeatedRecord) + .build(); TableResult unnestResult = bigquery.query(queryJobConfiguration); FieldValueList rowValue = unnestResult.getValues().iterator().next(); - //Why is this printing the one row 4 times? + // Why is this printing the one row 4 times? unnestResult .iterateAll() .forEach(row -> row.forEach(val -> System.out.printf("%s\n", val.toString()))); @@ -4182,7 +4184,8 @@ public void testUnnestRepeatedRecordNamedQueryParameterFromDataset() throws Inte // @repeatedRecord is array of struct type namedQueryParameter with 1 struct // the UI does return the correct 1 row output // this test returns 0 results. - String query = "SELECT * FROM `neenutestproject.AVRODATASET.NESTEDREPEATEDTABLE`, UNNEST(@repeatedRecord) AS TEMP where TEMP IN UNNEST(addresses);"; + String query = + "SELECT * FROM `neenutestproject.AVRODATASET.NESTEDREPEATEDTABLE`, UNNEST(@repeatedRecord) AS TEMP where TEMP IN UNNEST(addresses);"; QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query) .setUseLegacySql(false) @@ -4190,7 +4193,7 @@ public void testUnnestRepeatedRecordNamedQueryParameterFromDataset() throws Inte .build(); TableResult results = bigquery.query(queryConfig); - System.out.println( Iterables.size(results.getValues())); + System.out.println(Iterables.size(results.getValues())); results .iterateAll() .forEach(row -> row.forEach(val -> System.out.printf("%s", val.toString()))); From 0353c4ea2c05438836aaaf905e078af0ec3b8e56 Mon Sep 17 00:00:00 2001 From: obada-ab Date: Mon, 22 May 2023 17:19:58 +0200 Subject: [PATCH 6/9] chore: set up repeated record test to run in service account --- .../cloud/bigquery/it/ITBigQueryTest.java | 140 ++++++++++++++---- 1 file changed, 113 insertions(+), 27 deletions(-) diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index 850e6b3d3..b0dab8576 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -156,6 +156,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -231,6 +232,31 @@ public class ITBigQueryTest { .setMode(Field.Mode.REQUIRED) .setDescription("RecordDescription") .build(); + + private static final Field REPEATED_RECORD_FIELD_SCHEMA = + Field.newBuilder( + "Addresses", + LegacySQLTypeName.RECORD, + Field.newBuilder("Status", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("Address", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("City", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("State", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("Zip", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("NumberOfYears", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build()) + .setMode(Field.Mode.REPEATED) + .build(); private static final Field INTEGER_FIELD_SCHEMA = Field.newBuilder("IntegerField", LegacySQLTypeName.INTEGER) .setMode(Field.Mode.NULLABLE) @@ -422,6 +448,18 @@ public class ITBigQueryTest { .setMode(Field.Mode.NULLABLE) .build()); + private static final Schema REPEATED_RECORD_TABLE_SCHEMA = + Schema.of( + Field.newBuilder("ID", LegacySQLTypeName.STRING).setMode(Field.Mode.NULLABLE).build(), + Field.newBuilder("FirstName", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("LastName", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("DOB", LegacySQLTypeName.DATE).setMode(Field.Mode.NULLABLE).build(), + REPEATED_RECORD_FIELD_SCHEMA); + private static final Schema SIMPLE_SCHEMA = Schema.of(STRING_FIELD_SCHEMA); private static final Schema QUERY_RESULT_SCHEMA = Schema.of( @@ -4144,15 +4182,18 @@ public void testUnnestRepeatedRecordNamedQueryParameter() throws InterruptedExce @Test public void testUnnestRepeatedRecordNamedQueryParameterFromDataset() throws InterruptedException { + TableId tableId = TableId.of(DATASET, "test_repeated_record_table"); + setUpRepeatedRecordTable(tableId); + List tuples = new ArrayList<>(); QueryParameterValue statusValue = QueryParameterValue.string("single"); QueryParameterValue addressValue = QueryParameterValue.string("123 this lane"); - QueryParameterValue cityValue = QueryParameterValue.string("Torono"); + QueryParameterValue cityValue = QueryParameterValue.string("Toronto"); QueryParameterValue stateValue = QueryParameterValue.string("ON"); QueryParameterValue zipValue = QueryParameterValue.string("1h2j34"); QueryParameterValue numberOfYearsValue = QueryParameterValue.string("3"); - Map struct = new HashMap<>(); + Map struct = new LinkedHashMap<>(); struct.put("statusValue", statusValue); struct.put("addressValue", addressValue); struct.put("cityValue", cityValue); @@ -4165,39 +4206,84 @@ public void testUnnestRepeatedRecordNamedQueryParameterFromDataset() throws Inte QueryParameterValue repeatedRecord = QueryParameterValue.array(tuples.toArray(), StandardSQLTypeName.STRUCT); - String unnestRecord = - "SELECT TEMP from `neenutestproject.AVRODATASET.NESTEDREPEATEDTABLE`, UNNEST(@repeatedRecord) AS TEMP "; - QueryJobConfiguration queryJobConfiguration = - QueryJobConfiguration.newBuilder(unnestRecord) - .setUseLegacySql(false) - .addNamedParameter("repeatedRecord", repeatedRecord) - .build(); - TableResult unnestResult = bigquery.query(queryJobConfiguration); - FieldValueList rowValue = unnestResult.getValues().iterator().next(); - // Why is this printing the one row 4 times? - unnestResult - .iterateAll() - .forEach(row -> row.forEach(val -> System.out.printf("%s\n", val.toString()))); - - System.out.println("**************FROM DATASET****************"); - // addresses is a REPEATED RECORD field with an array of Structs of the above type. - // @repeatedRecord is array of struct type namedQueryParameter with 1 struct - // the UI does return the correct 1 row output - // this test returns 0 results. String query = - "SELECT * FROM `neenutestproject.AVRODATASET.NESTEDREPEATEDTABLE`, UNNEST(@repeatedRecord) AS TEMP where TEMP IN UNNEST(addresses);"; + "SELECT * FROM " + + tableId.getTable() + + ", UNNEST(@repeatedRecord) AS TEMP where TEMP IN UNNEST(addresses);"; QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query) + .setDefaultDataset(DATASET) .setUseLegacySql(false) .addNamedParameter("repeatedRecord", repeatedRecord) .build(); TableResult results = bigquery.query(queryConfig); - System.out.println(Iterables.size(results.getValues())); - results - .iterateAll() - .forEach(row -> row.forEach(val -> System.out.printf("%s", val.toString()))); - System.out.println("Query with Array of struct parameter performed successfully."); + assertEquals(1, Iterables.size(results.getValues())); + for (FieldValueList values : results.iterateAll()) { + assertEquals("1", values.get("ID").getStringValue()); + assertEquals("first_name1", values.get("FirstName").getStringValue()); + assertEquals(2, values.get("Addresses").getRecordValue().size()); + } + } + + private void setUpRepeatedRecordTable(TableId tableId) { + StandardTableDefinition tableDefinition = + StandardTableDefinition.of(REPEATED_RECORD_TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(tableId, tableDefinition); + bigquery.create(tableInfo); + + ImmutableMap.Builder builder1 = ImmutableMap.builder(); + builder1.put("ID", "1"); + builder1.put("FirstName", "first_name1"); + builder1.put("LastName", "last_name1"); + builder1.put("DOB", "1995-08-09"); + builder1.put( + "Addresses", + ImmutableList.of( + ImmutableMap.of( + "Status", "single", + "Address", "123 this lane", + "City", "Toronto", + "State", "ON", + "Zip", "1h2j34", + "NumberOfYears", "3"), + ImmutableMap.of( + "Status", "couple", + "Address", "345 that lane", + "City", "Maple", + "State", "ON", + "Zip", "1h2j34", + "NumberOfYears", "5"))); + + ImmutableMap.Builder builder2 = ImmutableMap.builder(); + builder2.put("ID", "2"); + builder2.put("FirstName", "first_name2"); + builder2.put("LastName", "last_name2"); + builder2.put("DOB", "1992-03-19"); + builder2.put( + "Addresses", + ImmutableList.of( + ImmutableMap.of( + "Status", "single", + "Address", "97 Kota lane", + "City", "Ottawa", + "State", "ON", + "Zip", "1h2j34", + "NumberOfYears", "3"), + ImmutableMap.of( + "Status", "couple", + "Address", "75 Malta lane", + "City", "Victoria", + "State", "AL", + "Zip", "1h2j34", + "NumberOfYears", "5"))); + + InsertAllRequest request = + InsertAllRequest.newBuilder(tableInfo.getTableId()) + .addRow(builder1.build()) + .addRow(builder2.build()) + .build(); + bigquery.insertAll(request); } @Test From b5ac4af0ed8ad15a2931d26b1a032462a96deb4c Mon Sep 17 00:00:00 2001 From: obada-ab Date: Thu, 25 May 2023 17:59:15 +0200 Subject: [PATCH 7/9] doc: create and query repeated record sample --- .../CreateAndQueryRepeatedRecordField.java | 195 ++++++++++++++++++ ...ueryWithArrayOfStructsNamedParameters.java | 2 +- .../CreateAndQueryRepeatedRecordFieldIT.java | 77 +++++++ 3 files changed, 273 insertions(+), 1 deletion(-) create mode 100644 samples/snippets/src/main/java/com/example/bigquery/CreateAndQueryRepeatedRecordField.java create mode 100644 samples/snippets/src/test/java/com/example/bigquery/CreateAndQueryRepeatedRecordFieldIT.java diff --git a/samples/snippets/src/main/java/com/example/bigquery/CreateAndQueryRepeatedRecordField.java b/samples/snippets/src/main/java/com/example/bigquery/CreateAndQueryRepeatedRecordField.java new file mode 100644 index 000000000..bff122db3 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/CreateAndQueryRepeatedRecordField.java @@ -0,0 +1,195 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +// [START bigquery_create_and_query_repeated_record] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.InsertAllRequest; +import com.google.cloud.bigquery.LegacySQLTypeName; +import com.google.cloud.bigquery.QueryJobConfiguration; +import com.google.cloud.bigquery.QueryParameterValue; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardSQLTypeName; +import com.google.cloud.bigquery.StandardTableDefinition; +import com.google.cloud.bigquery.TableId; +import com.google.cloud.bigquery.TableInfo; +import com.google.cloud.bigquery.TableResult; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +// Create a table with a repeated record field and query it using an array of struct named parameter +public class CreateAndQueryRepeatedRecordField { + + private static final Field REPEATED_RECORD_FIELD_SCHEMA = + Field.newBuilder( + "Addresses", + LegacySQLTypeName.RECORD, + Field.newBuilder("Status", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("Address", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("City", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("State", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("Zip", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("NumberOfYears", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build()) + .setMode(Field.Mode.REPEATED) + .build(); + private static final Schema REPEATED_RECORD_TABLE_SCHEMA = + Schema.of( + Field.newBuilder("ID", LegacySQLTypeName.STRING).setMode(Field.Mode.NULLABLE).build(), + Field.newBuilder("FirstName", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("LastName", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .build(), + Field.newBuilder("DOB", LegacySQLTypeName.DATE).setMode(Field.Mode.NULLABLE).build(), + REPEATED_RECORD_FIELD_SCHEMA); + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "MY_DATASET_NAME"; + String tableName = "MY_TABLE_NAME"; + createAndQueryRepeatedRecordField(datasetName, tableName); + } + + public static void createAndQueryRepeatedRecordField(String datasetName, String tableName) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + TableId tableId = TableId.of(datasetName, tableName); + + // Create a table with a repeated record field + StandardTableDefinition tableDefinition = + StandardTableDefinition.of(REPEATED_RECORD_TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(tableId, tableDefinition); + bigquery.create(tableInfo); + + // Insert some data + ImmutableMap.Builder builder1 = ImmutableMap.builder(); + builder1.put("ID", "1"); + builder1.put("FirstName", "first_name1"); + builder1.put("LastName", "last_name1"); + builder1.put("DOB", "1995-08-09"); + builder1.put( + "Addresses", + ImmutableList.of( + ImmutableMap.of( + "Status", "single", + "Address", "123 this lane", + "City", "Toronto", + "State", "ON", + "Zip", "1h2j34", + "NumberOfYears", "3"), + ImmutableMap.of( + "Status", "couple", + "Address", "345 that lane", + "City", "Maple", + "State", "ON", + "Zip", "1h2j34", + "NumberOfYears", "5"))); + + ImmutableMap.Builder builder2 = ImmutableMap.builder(); + builder2.put("ID", "2"); + builder2.put("FirstName", "first_name2"); + builder2.put("LastName", "last_name2"); + builder2.put("DOB", "1992-03-19"); + builder2.put( + "Addresses", + ImmutableList.of( + ImmutableMap.of( + "Status", "single", + "Address", "97 Kota lane", + "City", "Ottawa", + "State", "ON", + "Zip", "1h2j34", + "NumberOfYears", "3"), + ImmutableMap.of( + "Status", "couple", + "Address", "75 Malta lane", + "City", "Victoria", + "State", "AL", + "Zip", "1h2j34", + "NumberOfYears", "5"))); + + InsertAllRequest request = + InsertAllRequest.newBuilder(tableInfo.getTableId()) + .addRow(builder1.build()) + .addRow(builder2.build()) + .build(); + bigquery.insertAll(request); + + // Query using a named parameter + List tuples = new ArrayList<>(); + QueryParameterValue statusValue = QueryParameterValue.string("single"); + QueryParameterValue addressValue = QueryParameterValue.string("123 this lane"); + QueryParameterValue cityValue = QueryParameterValue.string("Toronto"); + QueryParameterValue stateValue = QueryParameterValue.string("ON"); + QueryParameterValue zipValue = QueryParameterValue.string("1h2j34"); + QueryParameterValue numberOfYearsValue = QueryParameterValue.string("3"); + + Map struct = new LinkedHashMap<>(); + struct.put("statusValue", statusValue); + struct.put("addressValue", addressValue); + struct.put("cityValue", cityValue); + struct.put("stateValue", stateValue); + struct.put("zipValue", zipValue); + struct.put("numberOfYearsValue", numberOfYearsValue); + QueryParameterValue recordValue = QueryParameterValue.struct(struct); + tuples.add(recordValue); + + QueryParameterValue repeatedRecord = + QueryParameterValue.array(tuples.toArray(), StandardSQLTypeName.STRUCT); + + String query = + "SELECT * FROM " + + tableId.getTable() + + ", UNNEST(@repeatedRecord) AS TEMP where TEMP IN UNNEST(addresses);"; + QueryJobConfiguration queryConfig = + QueryJobConfiguration.newBuilder(query) + .setDefaultDataset(datasetName) + .setUseLegacySql(false) + .addNamedParameter("repeatedRecord", repeatedRecord) + .build(); + TableResult results = bigquery.query(queryConfig); + results + .iterateAll() + .forEach(row -> row.forEach(val -> System.out.printf("%s\n", val.toString()))); + System.out.println("Query with Array of struct parameters performed successfully."); + } catch (BigQueryException | InterruptedException e) { + System.out.println("Query not performed \n" + e.toString()); + } + } +} +// [END bigquery_create_and_query_repeated_record] diff --git a/samples/snippets/src/main/java/com/example/bigquery/QueryWithArrayOfStructsNamedParameters.java b/samples/snippets/src/main/java/com/example/bigquery/QueryWithArrayOfStructsNamedParameters.java index 00cae0aa9..3a2074f9a 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/QueryWithArrayOfStructsNamedParameters.java +++ b/samples/snippets/src/main/java/com/example/bigquery/QueryWithArrayOfStructsNamedParameters.java @@ -53,7 +53,7 @@ public static void queryWithArrayOfStructsNamedParameters() { .setUseLegacySql(false) .addNamedParameter( "arrayOfStructField", - QueryParameterValue.array(arrayOfStructs.toArray(), StandardSQLTypeName.STRING)) + QueryParameterValue.array(arrayOfStructs.toArray(), StandardSQLTypeName.STRUCT)) .build(); TableResult results = bigquery.query(queryConfig); results diff --git a/samples/snippets/src/test/java/com/example/bigquery/CreateAndQueryRepeatedRecordFieldIT.java b/samples/snippets/src/test/java/com/example/bigquery/CreateAndQueryRepeatedRecordFieldIT.java new file mode 100644 index 000000000..ffdc28cfb --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/CreateAndQueryRepeatedRecordFieldIT.java @@ -0,0 +1,77 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class CreateAndQueryRepeatedRecordFieldIT { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String tableName; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME"); + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("BIGQUERY_DATASET_NAME"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + tableName = "MY_TABLE_NAME_" + UUID.randomUUID().toString().replace("-", "_"); + } + + @After + public void tearDown() { + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, "\n" + bout.toString()); + } + + @Test + public void testCreateAndQueryRepeatedRecordField() { + CreateAndQueryRepeatedRecordField.createAndQueryRepeatedRecordField(BIGQUERY_DATASET_NAME, tableName); + assertThat(bout.toString()).contains("Query with Array of struct parameters performed successfully."); + } +} From 49fa251f88c5ece81f451b436adee0923ba08899 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Thu, 25 May 2023 16:03:12 +0000 Subject: [PATCH 8/9] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20po?= =?UTF-8?q?st-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 1 + .../example/bigquery/CreateAndQueryRepeatedRecordField.java | 1 + .../bigquery/CreateAndQueryRepeatedRecordFieldIT.java | 6 ++++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8aca06dac..775b3d27d 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-bigquery/tree | Copy Multiple Tables | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/CopyMultipleTables.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CopyMultipleTables.java) | | Copy Table | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/CopyTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CopyTable.java) | | Copy Table Cmek | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/CopyTableCmek.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CopyTableCmek.java) | +| Create And Query Repeated Record Field | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/CreateAndQueryRepeatedRecordField.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CreateAndQueryRepeatedRecordField.java) | | Create Clustered Table | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/CreateClusteredTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CreateClusteredTable.java) | | Create Dataset | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/CreateDataset.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CreateDataset.java) | | Create Dataset Aws | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/CreateDatasetAws.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CreateDatasetAws.java) | diff --git a/samples/snippets/src/main/java/com/example/bigquery/CreateAndQueryRepeatedRecordField.java b/samples/snippets/src/main/java/com/example/bigquery/CreateAndQueryRepeatedRecordField.java index bff122db3..2b995dc01 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/CreateAndQueryRepeatedRecordField.java +++ b/samples/snippets/src/main/java/com/example/bigquery/CreateAndQueryRepeatedRecordField.java @@ -76,6 +76,7 @@ public class CreateAndQueryRepeatedRecordField { .build(), Field.newBuilder("DOB", LegacySQLTypeName.DATE).setMode(Field.Mode.NULLABLE).build(), REPEATED_RECORD_FIELD_SCHEMA); + public static void main(String[] args) { // TODO(developer): Replace these variables before running the sample. String datasetName = "MY_DATASET_NAME"; diff --git a/samples/snippets/src/test/java/com/example/bigquery/CreateAndQueryRepeatedRecordFieldIT.java b/samples/snippets/src/test/java/com/example/bigquery/CreateAndQueryRepeatedRecordFieldIT.java index ffdc28cfb..56ad47a5d 100644 --- a/samples/snippets/src/test/java/com/example/bigquery/CreateAndQueryRepeatedRecordFieldIT.java +++ b/samples/snippets/src/test/java/com/example/bigquery/CreateAndQueryRepeatedRecordFieldIT.java @@ -71,7 +71,9 @@ public void tearDown() { @Test public void testCreateAndQueryRepeatedRecordField() { - CreateAndQueryRepeatedRecordField.createAndQueryRepeatedRecordField(BIGQUERY_DATASET_NAME, tableName); - assertThat(bout.toString()).contains("Query with Array of struct parameters performed successfully."); + CreateAndQueryRepeatedRecordField.createAndQueryRepeatedRecordField( + BIGQUERY_DATASET_NAME, tableName); + assertThat(bout.toString()) + .contains("Query with Array of struct parameters performed successfully."); } } From 746db7a38fbb2e116f802c0564d9b50a11ae32cb Mon Sep 17 00:00:00 2001 From: obada-ab Date: Thu, 25 May 2023 18:20:03 +0200 Subject: [PATCH 9/9] refactor: reformat repeated record sample --- .../com/example/bigquery/CreateAndQueryRepeatedRecordField.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/bigquery/CreateAndQueryRepeatedRecordField.java b/samples/snippets/src/main/java/com/example/bigquery/CreateAndQueryRepeatedRecordField.java index 2b995dc01..2bb13eb12 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/CreateAndQueryRepeatedRecordField.java +++ b/samples/snippets/src/main/java/com/example/bigquery/CreateAndQueryRepeatedRecordField.java @@ -152,7 +152,6 @@ public static void createAndQueryRepeatedRecordField(String datasetName, String bigquery.insertAll(request); // Query using a named parameter - List tuples = new ArrayList<>(); QueryParameterValue statusValue = QueryParameterValue.string("single"); QueryParameterValue addressValue = QueryParameterValue.string("123 this lane"); QueryParameterValue cityValue = QueryParameterValue.string("Toronto"); @@ -168,6 +167,7 @@ public static void createAndQueryRepeatedRecordField(String datasetName, String struct.put("zipValue", zipValue); struct.put("numberOfYearsValue", numberOfYearsValue); QueryParameterValue recordValue = QueryParameterValue.struct(struct); + List tuples = new ArrayList<>(); tuples.add(recordValue); QueryParameterValue repeatedRecord =