|
85 | 85 | import com.google.cloud.bigquery.RoutineInfo;
|
86 | 86 | import com.google.cloud.bigquery.Schema;
|
87 | 87 | import com.google.cloud.bigquery.StandardSQLDataType;
|
| 88 | +import com.google.cloud.bigquery.StandardSQLTypeName; |
88 | 89 | import com.google.cloud.bigquery.StandardTableDefinition;
|
89 | 90 | import com.google.cloud.bigquery.Table;
|
90 | 91 | import com.google.cloud.bigquery.TableDataWriteChannel;
|
@@ -1547,15 +1548,69 @@ public void testStructNamedQueryParameters() throws InterruptedException {
|
1547 | 1548 | for (FieldValueList values : result.iterateAll()) {
|
1548 | 1549 | for (FieldValue value : values) {
|
1549 | 1550 | 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); |
1554 | 1552 | }
|
1555 | 1553 | }
|
1556 | 1554 | }
|
1557 | 1555 | }
|
1558 | 1556 |
|
| 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 | + |
1559 | 1614 | @Test
|
1560 | 1615 | public void testNestedStructNamedQueryParameters() throws InterruptedException {
|
1561 | 1616 | QueryParameterValue booleanValue = QueryParameterValue.bool(true);
|
|
0 commit comments