Skip to content

Commit 17048ed

Browse files
authored
fix(bigquery): fix invalid mappings in domain models (#14646)
1 parent 275063b commit 17048ed

7 files changed

Lines changed: 64 additions & 39 deletions

File tree

‎java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ExternalTableDefinition.java‎

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,16 @@
4242
public abstract class ExternalTableDefinition extends TableDefinition {
4343

4444
static final Function<ExternalDataConfiguration, ExternalTableDefinition>
45-
FROM_EXTERNAL_DATA_FUNCTION =
46-
new Function<ExternalDataConfiguration, ExternalTableDefinition>() {
47-
@Override
48-
public ExternalTableDefinition apply(ExternalDataConfiguration pb) {
49-
return ExternalTableDefinition.fromExternalDataConfiguration(pb);
50-
}
51-
};
45+
FROM_EXTERNAL_DATA_FUNCTION = ExternalTableDefinition::fromExternalDataConfiguration;
5246
static final Function<ExternalTableDefinition, ExternalDataConfiguration>
53-
TO_EXTERNAL_DATA_FUNCTION =
54-
new Function<ExternalTableDefinition, ExternalDataConfiguration>() {
55-
@Override
56-
public ExternalDataConfiguration apply(ExternalTableDefinition tableInfo) {
57-
return tableInfo.toExternalDataConfigurationPb();
58-
}
59-
};
47+
TO_EXTERNAL_DATA_FUNCTION = ExternalTableDefinition::toExternalDataConfigurationPb;
6048

6149
private static final long serialVersionUID = -5951580238459622025L;
6250

6351
public static final class SourceColumnMatch extends StringEnumValue {
6452
private static final long serialVersionUID = 818920627219751207L;
6553
private static final ApiFunction<String, SourceColumnMatch> CONSTRUCTOR =
66-
new ApiFunction<String, SourceColumnMatch>() {
67-
@Override
68-
public SourceColumnMatch apply(String constant) {
69-
return new SourceColumnMatch(constant);
70-
}
71-
};
54+
SourceColumnMatch::new;
7255

7356
private static final StringEnumType<SourceColumnMatch> type =
7457
new StringEnumType<SourceColumnMatch>(SourceColumnMatch.class, CONSTRUCTOR);
@@ -898,7 +881,7 @@ static ExternalTableDefinition fromExternalDataConfiguration(
898881
builder.setTimeFormat(externalDataConfiguration.getTimeFormat());
899882
}
900883
if (externalDataConfiguration.getTimestampFormat() != null) {
901-
builder.setTimestampFormat(externalDataConfiguration.getTimeFormat());
884+
builder.setTimestampFormat(externalDataConfiguration.getTimestampFormat());
902885
}
903886
if (externalDataConfiguration.getCsvOptions() != null) {
904887
if (externalDataConfiguration.getCsvOptions().getSourceColumnMatch() != null) {

‎java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/IndexUnusedReason.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ com.google.api.services.bigquery.model.IndexUnusedReason toPb() {
101101
com.google.api.services.bigquery.model.IndexUnusedReason indexUnusedReason =
102102
new com.google.api.services.bigquery.model.IndexUnusedReason();
103103
if (getIndexName() != null) {
104-
indexUnusedReason.setIndexName(indexUnusedReason.getIndexName());
104+
indexUnusedReason.setIndexName(getIndexName());
105105
}
106106
if (getCode() != null) {
107-
indexUnusedReason.setCode(indexUnusedReason.getCode());
107+
indexUnusedReason.setCode(getCode());
108108
}
109109
if (getMessage() != null) {
110-
indexUnusedReason.setMessage(indexUnusedReason.getMessage());
110+
indexUnusedReason.setMessage(getMessage());
111111
}
112112
if (getBaseTableId() != null) {
113113
indexUnusedReason.setBaseTable(getBaseTableId().toPb());

‎java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,7 @@ protected Builder(com.google.api.services.bigquery.model.JobStatistics statistic
16501650
if (statisticsPb.getScriptStatistics() != null) {
16511651
this.scriptStatistics = ScriptStatistics.fromPb(statisticsPb.getScriptStatistics());
16521652
}
1653-
if (reservationUsage != null) {
1653+
if (statisticsPb.getReservationUsage() != null) {
16541654
this.reservationUsage =
16551655
Lists.transform(statisticsPb.getReservationUsage(), ReservationUsage.FROM_PB_FUNCTION);
16561656
}
@@ -1692,6 +1692,11 @@ B setSessionInfo(SessionInfo sessionInfo) {
16921692
return self();
16931693
}
16941694

1695+
B setReservationUsage(List<ReservationUsage> reservationUsage) {
1696+
this.reservationUsage = reservationUsage;
1697+
return self();
1698+
}
1699+
16951700
abstract T build();
16961701
}
16971702

‎java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryParameterValue.java‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@
6565
* <li>Double: StandardSQLTypeName.FLOAT64
6666
* <li>Float: StandardSQLTypeName.FLOAT64
6767
* <li>BigDecimal: StandardSQLTypeName.NUMERIC
68-
* <li>BigNumeric: StandardSQLTypeName.BIGNUMERIC
69-
* <li>JSON: StandardSQLTypeName.JSON
70-
* <li>INTERVAL: StandardSQLTypeName.INTERVAL
68+
* <li>Date: StandardSQLTypeName.DATE
69+
* <li>JsonObject: StandardSQLTypeName.JSON
7170
* </ul>
7271
*
7372
* <p>No other types are supported through that entry point. The other types can be created by
@@ -424,13 +423,27 @@ public static QueryParameterValue struct(Map<String, QueryParameterValue> struct
424423
.build();
425424
}
426425

426+
/**
427+
* Default instances created automatically by the static {@link #of(Object, Class)} method.
428+
* Contains these mappings ONLY for natively supported types.
429+
*
430+
* <ul>
431+
* <li>Boolean: StandardSQLTypeName.BOOL
432+
* <li>String: StandardSQLTypeName.STRING
433+
* <li>Integer: StandardSQLTypeName.INT64
434+
* <li>Long: StandardSQLTypeName.INT64
435+
* <li>Double: StandardSQLTypeName.FLOAT64
436+
* <li>Float: StandardSQLTypeName.FLOAT64
437+
* <li>BigDecimal: StandardSQLTypeName.NUMERIC
438+
* <li>Date: StandardSQLTypeName.DATE
439+
* <li>JsonObject: StandardSQLTypeName.JSON
440+
* </ul>
441+
*/
427442
private static <T> StandardSQLTypeName classToType(Class<T> type) {
428443
if (Boolean.class.isAssignableFrom(type)) {
429444
return StandardSQLTypeName.BOOL;
430445
} else if (String.class.isAssignableFrom(type)) {
431446
return StandardSQLTypeName.STRING;
432-
} else if (String.class.isAssignableFrom(type)) {
433-
return StandardSQLTypeName.GEOGRAPHY;
434447
} else if (Integer.class.isAssignableFrom(type)) {
435448
return StandardSQLTypeName.INT64;
436449
} else if (Long.class.isAssignableFrom(type)) {
@@ -443,8 +456,6 @@ private static <T> StandardSQLTypeName classToType(Class<T> type) {
443456
return StandardSQLTypeName.NUMERIC;
444457
} else if (Date.class.isAssignableFrom(type)) {
445458
return StandardSQLTypeName.DATE;
446-
} else if (String.class.isAssignableFrom(type)) {
447-
return StandardSQLTypeName.JSON;
448459
} else if (JsonObject.class.isAssignableFrom(type)) {
449460
return StandardSQLTypeName.JSON;
450461
}
@@ -487,9 +498,14 @@ private static <T> String valueToStringOrNull(T value, StandardSQLTypeName type)
487498
case GEOGRAPHY:
488499
return value.toString();
489500
case JSON:
490-
if (value instanceof String || value instanceof JsonObject) return value.toString();
501+
if (value instanceof String || value instanceof JsonObject) {
502+
return value.toString();
503+
}
504+
break;
491505
case INTERVAL:
492-
if (value instanceof String || value instanceof PeriodDuration) return value.toString();
506+
if (value instanceof String || value instanceof PeriodDuration) {
507+
return value.toString();
508+
}
493509
break;
494510
case STRUCT:
495511
throw new IllegalArgumentException("Cannot convert STRUCT to String value");

‎java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ExternalTableDefinitionTest.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ void testToAndFromPb() {
162162
ExternalTableDefinition.newBuilder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS).build();
163163
compareExternalTableDefinition(
164164
externalTableDefinition, ExternalTableDefinition.fromPb(externalTableDefinition.toPb()));
165+
compareExternalTableDefinition(
166+
EXTERNAL_TABLE_DEFINITION.toBuilder().setMaxStaleness(null).build(),
167+
ExternalTableDefinition.fromExternalDataConfiguration(
168+
EXTERNAL_TABLE_DEFINITION.toExternalDataConfigurationPb()));
165169
}
166170

167171
@Test

‎java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,27 @@ public class JobStatisticsTest {
177177
private static final List<QueryStage> QUERY_PLAN = ImmutableList.of(QUERY_STAGE);
178178
private static final Schema SCHEMA = Schema.of(Field.of("column", LegacySQLTypeName.DATETIME));
179179
private static final String UNUSED_INDEX_USAGE_MODE = "UNUSED";
180+
private static final IndexUnusedReason INDEX_UNUSED_REASON =
181+
IndexUnusedReason.newBuilder()
182+
.setIndexName("idx_search")
183+
.setCode("BASE_TABLE_TOO_SMALL")
184+
.setMessage("Base table is too small to use search index")
185+
.setBaseTableId(DDL_TARGET_TABLE)
186+
.build();
180187
private static final SearchStats SEARCH_STATS =
181-
SearchStats.newBuilder().setIndexUsageMode(UNUSED_INDEX_USAGE_MODE).build();
188+
SearchStats.newBuilder()
189+
.setIndexUsageMode(UNUSED_INDEX_USAGE_MODE)
190+
.setIndexUnusedReasons(ImmutableList.of(INDEX_UNUSED_REASON))
191+
.build();
182192

183193
private static final MetadataCacheStats METADATA_CACHE_STATS =
184194
MetadataCacheStats.newBuilder()
185195
.setTableMetadataCacheUsage(
186196
ImmutableList.of(
187197
TableMetadataCacheUsage.newBuilder().setExplanation("test explanation").build()))
188198
.build();
199+
private static final ReservationUsage RESERVATION_USAGE =
200+
ReservationUsage.newBuilder().setName(NAME).setSlotMs(SLOTMS).build();
189201

190202
private static final QueryStatistics QUERY_STATISTICS =
191203
QueryStatistics.newBuilder()
@@ -213,6 +225,7 @@ public class JobStatisticsTest {
213225
.setSchema(SCHEMA)
214226
.setSearchStats(SEARCH_STATS)
215227
.setMetadataCacheStats(METADATA_CACHE_STATS)
228+
.setReservationUsage(ImmutableList.of(RESERVATION_USAGE))
216229
.build();
217230
private static final QueryStatistics QUERY_STATISTICS_INCOMPLETE =
218231
QueryStatistics.newBuilder()
@@ -262,8 +275,6 @@ public class JobStatisticsTest {
262275
.setEvaluationKind(EVALUATIONKIND_TYPE_EXPRESSION)
263276
.setStackFrames(ImmutableList.of(EXPRESSION_STACK_FRAME))
264277
.build();
265-
private static final ReservationUsage RESERVATION_USAGE =
266-
ReservationUsage.newBuilder().setName(NAME).setSlotMs(SLOTMS).build();
267278

268279
private static final TransactionInfo TRANSACTION_INFO =
269280
TransactionInfo.newbuilder().setTransactionId(TRANSACTION_ID).build();
@@ -460,8 +471,7 @@ private void compareQueryStatistics(QueryStatistics expected, QueryStatistics va
460471
assertEquals(expected.getQueryPlan(), value.getQueryPlan());
461472
assertEquals(expected.getReferencedTables(), value.getReferencedTables());
462473
assertEquals(expected.getSchema(), value.getSchema());
463-
assertEquals(
464-
expected.getSearchStats().getIndexUsageMode(), value.getSearchStats().getIndexUsageMode());
474+
assertEquals(expected.getSearchStats(), value.getSearchStats());
465475
assertEquals(expected.getMetadataCacheStats(), value.getMetadataCacheStats());
466476
assertEquals(expected.getStatementType(), value.getStatementType());
467477
assertEquals(expected.getTimeline(), value.getTimeline());
@@ -479,6 +489,7 @@ private void compareStatistics(JobStatistics expected, JobStatistics value) {
479489
assertEquals(expected.getNumChildJobs(), value.getNumChildJobs());
480490
assertEquals(expected.getParentJobId(), value.getParentJobId());
481491
assertEquals(expected.getScriptStatistics(), value.getScriptStatistics());
492+
assertEquals(expected.getReservationUsage(), value.getReservationUsage());
482493
assertEquals(expected.getTotalSlotMs(), value.getTotalSlotMs());
483494
}
484495

‎java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryParameterValueTest.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ public void testJson() {
195195
assertThat(value1.getArrayType()).isNull();
196196
assertThat(value.getArrayValues()).isNull();
197197
assertThat(value1.getArrayType()).isNull();
198+
assertThrows(
199+
IllegalArgumentException.class,
200+
() -> QueryParameterValue.of(123, StandardSQLTypeName.JSON));
198201
}
199202

200203
@Test
@@ -213,6 +216,9 @@ public void testInterval() {
213216
assertThat(value.getArrayType()).isNull();
214217
assertThat(value1.getArrayType()).isNull();
215218
assertThat(value2.getArrayType()).isNull();
219+
assertThrows(
220+
IllegalArgumentException.class,
221+
() -> QueryParameterValue.of(123, StandardSQLTypeName.INTERVAL));
216222
}
217223

218224
@Test

0 commit comments

Comments
 (0)