Skip to content

Commit b3f59b1

Browse files
author
Praful Makani
authored
fix: get recordvalue by field name (#718)
* fix: get recordvalue by field name * fix: remove duplicate code
1 parent 70417fc commit b3f59b1

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,24 @@ private static class TableDataPageFetcher implements NextPageFetcher<FieldValueL
177177
private final Map<BigQueryRpc.Option, ?> requestOptions;
178178
private final BigQueryOptions serviceOptions;
179179
private final TableId table;
180+
private final Schema schema;
180181

181182
TableDataPageFetcher(
182183
TableId table,
184+
Schema schema,
183185
BigQueryOptions serviceOptions,
184186
String cursor,
185187
Map<BigQueryRpc.Option, ?> optionMap) {
186188
this.requestOptions =
187189
PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap);
188190
this.serviceOptions = serviceOptions;
189191
this.table = table;
192+
this.schema = schema;
190193
}
191194

192195
@Override
193196
public Page<FieldValueList> getNextPage() {
194-
return listTableData(table, serviceOptions, requestOptions).x();
197+
return listTableData(table, schema, serviceOptions, requestOptions).x();
195198
}
196199
}
197200

@@ -1014,12 +1017,13 @@ public TableResult listTableData(
10141017
@Override
10151018
public TableResult listTableData(TableId tableId, Schema schema, TableDataListOption... options) {
10161019
Tuple<? extends Page<FieldValueList>, Long> data =
1017-
listTableData(tableId, getOptions(), optionMap(options));
1020+
listTableData(tableId, schema, getOptions(), optionMap(options));
10181021
return new TableResult(schema, data.y(), data.x());
10191022
}
10201023

10211024
private static Tuple<? extends Page<FieldValueList>, Long> listTableData(
10221025
final TableId tableId,
1026+
final Schema schema,
10231027
final BigQueryOptions serviceOptions,
10241028
final Map<BigQueryRpc.Option, ?> optionsMap) {
10251029
try {
@@ -1048,23 +1052,26 @@ public TableDataList call() {
10481052
String cursor = result.getPageToken();
10491053
return Tuple.of(
10501054
new PageImpl<>(
1051-
new TableDataPageFetcher(tableId, serviceOptions, cursor, optionsMap),
1055+
new TableDataPageFetcher(tableId, schema, serviceOptions, cursor, optionsMap),
10521056
cursor,
1053-
transformTableData(result.getRows())),
1057+
transformTableData(result.getRows(), schema)),
10541058
result.getTotalRows());
10551059
} catch (RetryHelper.RetryHelperException e) {
10561060
throw BigQueryException.translateAndThrow(e);
10571061
}
10581062
}
10591063

1060-
private static Iterable<FieldValueList> transformTableData(Iterable<TableRow> tableDataPb) {
1064+
private static Iterable<FieldValueList> transformTableData(
1065+
Iterable<TableRow> tableDataPb, final Schema schema) {
10611066
return ImmutableList.copyOf(
10621067
Iterables.transform(
10631068
tableDataPb != null ? tableDataPb : ImmutableList.<TableRow>of(),
10641069
new Function<TableRow, FieldValueList>() {
1070+
FieldList fields = schema != null ? schema.getFields() : null;
1071+
10651072
@Override
10661073
public FieldValueList apply(TableRow rowPb) {
1067-
return FieldValueList.fromPb(rowPb.getF(), null);
1074+
return FieldValueList.fromPb(rowPb.getF(), fields);
10681075
}
10691076
}));
10701077
}

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

+59-4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import com.google.cloud.bigquery.RoutineInfo;
8686
import com.google.cloud.bigquery.Schema;
8787
import com.google.cloud.bigquery.StandardSQLDataType;
88+
import com.google.cloud.bigquery.StandardSQLTypeName;
8889
import com.google.cloud.bigquery.StandardTableDefinition;
8990
import com.google.cloud.bigquery.Table;
9091
import com.google.cloud.bigquery.TableDataWriteChannel;
@@ -1547,15 +1548,69 @@ public void testStructNamedQueryParameters() throws InterruptedException {
15471548
for (FieldValueList values : result.iterateAll()) {
15481549
for (FieldValue value : values) {
15491550
for (FieldValue record : value.getRecordValue()) {
1550-
assertEquals(FieldValue.Attribute.RECORD, record.getAttribute());
1551-
assertEquals(true, record.getRecordValue().get(0).getBooleanValue());
1552-
assertEquals(10, record.getRecordValue().get(1).getLongValue());
1553-
assertEquals("test-stringField", record.getRecordValue().get(2).getStringValue());
1551+
assertsFieldValue(record);
15541552
}
15551553
}
15561554
}
15571555
}
15581556

1557+
@Test
1558+
public void testStructQuery() throws InterruptedException {
1559+
String tableName = "test_record_table_" + UUID.randomUUID().toString().substring(0, 8);
1560+
TableId tableId = TableId.of(DATASET, tableName);
1561+
try {
1562+
// create a table
1563+
Field booleanField = Field.of("booleanField", StandardSQLTypeName.BOOL);
1564+
Field integerField = Field.of("integerField", StandardSQLTypeName.INT64);
1565+
Field stringField = Field.of("stringField", StandardSQLTypeName.STRING);
1566+
Field recordField =
1567+
Field.newBuilder(
1568+
"recordField",
1569+
StandardSQLTypeName.STRUCT,
1570+
booleanField,
1571+
integerField,
1572+
stringField)
1573+
.setMode(Field.Mode.NULLABLE)
1574+
.build();
1575+
Schema schema = Schema.of(recordField);
1576+
StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema);
1577+
assertNotNull(bigquery.create(TableInfo.of(tableId, tableDefinition)));
1578+
// inserting data
1579+
Map<String, Object> content = new HashMap<>();
1580+
content.put("booleanField", true);
1581+
content.put("integerField", 10);
1582+
content.put("stringField", "test-stringField");
1583+
Map<String, Object> recordContent = new HashMap<>();
1584+
recordContent.put("recordField", content);
1585+
InsertAllResponse response =
1586+
bigquery.insertAll(InsertAllRequest.newBuilder(tableId).addRow(recordContent).build());
1587+
assertFalse(response.hasErrors());
1588+
// query into a table
1589+
String query = String.format("SELECT * FROM %s.%s", DATASET, tableName);
1590+
QueryJobConfiguration config =
1591+
QueryJobConfiguration.newBuilder(query)
1592+
.setDefaultDataset(DATASET)
1593+
.setUseLegacySql(false)
1594+
.build();
1595+
TableResult result = bigquery.query(config);
1596+
assertEquals(1, Iterables.size(result.getValues()));
1597+
for (FieldValueList values : result.iterateAll()) {
1598+
for (FieldValue record : values) {
1599+
assertsFieldValue(record);
1600+
}
1601+
}
1602+
} finally {
1603+
assertTrue(bigquery.delete(tableId));
1604+
}
1605+
}
1606+
1607+
private static void assertsFieldValue(FieldValue record) {
1608+
assertEquals(FieldValue.Attribute.RECORD, record.getAttribute());
1609+
assertEquals(true, record.getRecordValue().get("booleanField").getBooleanValue());
1610+
assertEquals(10, record.getRecordValue().get("integerField").getLongValue());
1611+
assertEquals("test-stringField", record.getRecordValue().get("stringField").getStringValue());
1612+
}
1613+
15591614
@Test
15601615
public void testNestedStructNamedQueryParameters() throws InterruptedException {
15611616
QueryParameterValue booleanValue = QueryParameterValue.bool(true);

0 commit comments

Comments
 (0)