This repository was archived by the owner on Aug 30, 2026. It is now read-only.
Repository navigation
fix: add support for repeated record query parameters #2698
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2c891e7
fix: add support for repeated record query parameters
obada-ab eefcdae
fix: set the query parameter type to an empty struct array if values …
obada-ab f8e36ce
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 8deb47a
Merge branch 'main' into struct-array
obada-ab f15630a
chore: add test for a real dataset
Neenu1995 6fc4085
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 0353c4e
chore: set up repeated record test to run in service account
obada-ab b5ac4af
doc: create and query repeated record sample
obada-ab 49fa251
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 746db7a
refactor: reformat repeated record sample
obada-ab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4062,6 +4062,163 @@ public void testStructNamedQueryParameters() throws InterruptedException { | |
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testRepeatedRecordNamedQueryParameters() throws InterruptedException { | ||
| String[] stringValues = new String[] {"test-stringField", "test-stringField2"}; | ||
| List<QueryParameterValue> tuples = new ArrayList<>(); | ||
| for (int i = 0; i < 2; i++) { | ||
| QueryParameterValue stringValue = QueryParameterValue.string(stringValues[i]); | ||
| Map<String, QueryParameterValue> 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 testUnnestRepeatedRecordNamedQueryParameter() throws InterruptedException { | ||
| Boolean[] boolValues = new Boolean[] {true, false}; | ||
| List<QueryParameterValue> tuples = new ArrayList<>(); | ||
| for (int i = 0; i < 2; i++) { | ||
| QueryParameterValue boolValue = QueryParameterValue.bool(boolValues[i]); | ||
| Map<String, QueryParameterValue> 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 testUnnestRepeatedRecordNamedQueryParameterFromDataset() throws InterruptedException { | ||
| List<QueryParameterValue> 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<String, QueryParameterValue> struct = new HashMap<>(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The order of fields inside structs matters when doing comparisons, and I tried replacing |
||
| 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 = {}; | ||
|
|
||
| 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 | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also write a test with UNNEST option, since that is the particular use case the customer is using?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, will work on it