Skip to content

Commit 13cc6e6

Browse files
feat: add support for BI Engine Statistics (#1723)
fixes b/205146044
1 parent e7f1cf8 commit 13cc6e6

File tree

5 files changed

+235
-0
lines changed

5 files changed

+235
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.bigquery;
18+
19+
import com.google.auto.value.AutoValue;
20+
import java.io.Serializable;
21+
import javax.annotation.Nullable;
22+
23+
@AutoValue
24+
public abstract class BiEngineReason implements Serializable {
25+
26+
@AutoValue.Builder
27+
public abstract static class Builder {
28+
29+
/**
30+
* High-level BI Engine reason for partial or disabled acceleration.
31+
*
32+
* @param code code or {@code null} for none
33+
*/
34+
public abstract Builder setCode(String code);
35+
36+
/**
37+
* Free form human-readable reason for partial or disabled acceleration.
38+
*
39+
* @param message message or {@code null} for none
40+
*/
41+
public abstract Builder setMessage(String message);
42+
43+
/** Creates a {@code BiEngineReason} object. */
44+
public abstract BiEngineReason build();
45+
}
46+
47+
/**
48+
* High-level BI Engine reason for partial or disabled acceleration.
49+
*
50+
* @return value or {@code null} for none
51+
*/
52+
@Nullable
53+
public abstract String getCode();
54+
55+
/**
56+
* Free form human-readable reason for partial or disabled acceleration.
57+
*
58+
* @return value or {@code null} for none
59+
*/
60+
@Nullable
61+
public abstract String getMessage();
62+
63+
public abstract Builder toBuilder();
64+
65+
public static Builder newBuilder() {
66+
return new AutoValue_BiEngineReason.Builder();
67+
}
68+
69+
com.google.api.services.bigquery.model.BiEngineReason toPb() {
70+
com.google.api.services.bigquery.model.BiEngineReason biEngineReasonPb =
71+
new com.google.api.services.bigquery.model.BiEngineReason();
72+
if (getCode() != null) {
73+
biEngineReasonPb.setCode(getCode());
74+
}
75+
if (getMessage() != null) {
76+
biEngineReasonPb.setMessage(getMessage());
77+
}
78+
return biEngineReasonPb;
79+
}
80+
81+
static BiEngineReason fromPb(
82+
com.google.api.services.bigquery.model.BiEngineReason biEngineReasonPb) {
83+
Builder builder = newBuilder();
84+
if (biEngineReasonPb.getCode() != null) {
85+
builder.setCode(biEngineReasonPb.getCode());
86+
}
87+
if (biEngineReasonPb.getMessage() != null) {
88+
builder.setMessage(biEngineReasonPb.getMessage());
89+
}
90+
return builder.build();
91+
}
92+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.bigquery;
18+
19+
import com.google.api.services.bigquery.model.BiEngineStatistics;
20+
import com.google.auto.value.AutoValue;
21+
import java.io.Serializable;
22+
import java.util.List;
23+
import java.util.stream.Collectors;
24+
import javax.annotation.Nullable;
25+
26+
/** BIEngineStatistics contains query statistics specific to the use of BI Engine. */
27+
@AutoValue
28+
public abstract class BiEngineStats implements Serializable {
29+
30+
@AutoValue.Builder
31+
public abstract static class Builder {
32+
/**
33+
* Specifies which mode of BI Engine acceleration was performed (if any).
34+
*
35+
* @param biEngineMode biEngineMode or {@code null} for none
36+
*/
37+
public abstract Builder setBiEngineMode(String biEngineMode);
38+
39+
/**
40+
* In case of DISABLED or PARTIAL bi_engine_mode, these contain the explanatory reasons as to
41+
* why BI Engine could not accelerate. In case the full query was accelerated, this field is not
42+
* populated.
43+
*
44+
* @param biEngineReasons biEngineReasons or {@code null} for none
45+
*/
46+
public abstract Builder setBiEngineReasons(List<BiEngineReason> biEngineReasons);
47+
48+
/** Creates a @code BiEngineStats} object. */
49+
public abstract BiEngineStats build();
50+
}
51+
52+
/**
53+
* Specifies which mode of BI Engine acceleration was performed (if any).
54+
*
55+
* @return value or {@code null} for none
56+
*/
57+
@Nullable
58+
public abstract String getBiEngineMode();
59+
60+
/**
61+
* In case of DISABLED or PARTIAL bi_engine_mode, these contain the explanatory reasons as to why
62+
* BI Engine could not accelerate. In case the full query was accelerated, this field is not
63+
* populated.
64+
*
65+
* @return value or {@code null} for none
66+
*/
67+
@Nullable
68+
public abstract List<BiEngineReason> getBiEngineReasons();
69+
70+
public abstract Builder toBuilder();
71+
72+
public static Builder newBuilder() {
73+
return new AutoValue_BiEngineStats.Builder();
74+
}
75+
76+
BiEngineStatistics toPb() {
77+
BiEngineStatistics biEngineStatisticsPb = new BiEngineStatistics();
78+
if (getBiEngineMode() != null) {
79+
biEngineStatisticsPb.setBiEngineMode(getBiEngineMode());
80+
}
81+
if (getBiEngineReasons() != null) {
82+
biEngineStatisticsPb.setBiEngineReasons(
83+
getBiEngineReasons().stream().map(BiEngineReason::toPb).collect(Collectors.toList()));
84+
}
85+
return biEngineStatisticsPb;
86+
}
87+
88+
static BiEngineStats fromPb(BiEngineStatistics biEngineStatisticsPb) {
89+
Builder builder = newBuilder();
90+
if (biEngineStatisticsPb.getBiEngineMode() != null) {
91+
builder.setBiEngineMode(biEngineStatisticsPb.getBiEngineMode());
92+
}
93+
if (biEngineStatisticsPb.getBiEngineReasons() != null) {
94+
builder.setBiEngineReasons(
95+
biEngineStatisticsPb.getBiEngineReasons().stream()
96+
.map(BiEngineReason::fromPb)
97+
.collect(Collectors.toList()));
98+
}
99+
return builder.build();
100+
}
101+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ public static class QueryStatistics extends JobStatistics {
321321

322322
private static final long serialVersionUID = 7539354109226732353L;
323323

324+
private final BiEngineStats biEngineStats;
324325
private final Integer billingTier;
325326
private final Boolean cacheHit;
326327
private final String ddlOperationPerformed;
@@ -402,6 +403,7 @@ public static StatementType[] values() {
402403

403404
static final class Builder extends JobStatistics.Builder<QueryStatistics, Builder> {
404405

406+
private BiEngineStats biEngineStats;
405407
private Integer billingTier;
406408
private Boolean cacheHit;
407409
private String ddlOperationPerformed;
@@ -425,6 +427,10 @@ private Builder() {}
425427
private Builder(com.google.api.services.bigquery.model.JobStatistics statisticsPb) {
426428
super(statisticsPb);
427429
if (statisticsPb.getQuery() != null) {
430+
if (statisticsPb.getQuery().getBiEngineStatistics() != null) {
431+
this.biEngineStats =
432+
BiEngineStats.fromPb(statisticsPb.getQuery().getBiEngineStatistics());
433+
}
428434
this.billingTier = statisticsPb.getQuery().getBillingTier();
429435
this.cacheHit = statisticsPb.getQuery().getCacheHit();
430436
this.ddlOperationPerformed = statisticsPb.getQuery().getDdlOperationPerformed();
@@ -468,6 +474,11 @@ private Builder(com.google.api.services.bigquery.model.JobStatistics statisticsP
468474
}
469475
}
470476

477+
Builder setBiEngineStats(BiEngineStats biEngineStats) {
478+
this.biEngineStats = biEngineStats;
479+
return self();
480+
}
481+
471482
Builder setBillingTier(Integer billingTier) {
472483
this.billingTier = billingTier;
473484
return self();
@@ -566,6 +577,7 @@ QueryStatistics build() {
566577

567578
private QueryStatistics(Builder builder) {
568579
super(builder);
580+
this.biEngineStats = builder.biEngineStats;
569581
this.billingTier = builder.billingTier;
570582
this.cacheHit = builder.cacheHit;
571583
this.ddlOperationPerformed = builder.ddlOperationPerformed;
@@ -585,6 +597,11 @@ private QueryStatistics(Builder builder) {
585597
this.schema = builder.schema;
586598
}
587599

600+
/** Returns query statistics specific to the use of BI Engine. */
601+
public BiEngineStats getBiEngineStats() {
602+
return biEngineStats;
603+
}
604+
588605
/** Returns the billing tier for the job. */
589606
public Integer getBillingTier() {
590607
return billingTier;
@@ -701,6 +718,7 @@ public Schema getSchema() {
701718
@Override
702719
ToStringHelper toStringHelper() {
703720
return super.toStringHelper()
721+
.add("biEngineStats", biEngineStats)
704722
.add("billingTier", billingTier)
705723
.add("cacheHit", cacheHit)
706724
.add("totalBytesBilled", totalBytesBilled)
@@ -722,6 +740,7 @@ public final boolean equals(Object obj) {
722740
public final int hashCode() {
723741
return Objects.hash(
724742
baseHashCode(),
743+
biEngineStats,
725744
billingTier,
726745
cacheHit,
727746
totalBytesBilled,
@@ -733,6 +752,9 @@ public final int hashCode() {
733752
@Override
734753
com.google.api.services.bigquery.model.JobStatistics toPb() {
735754
JobStatistics2 queryStatisticsPb = new JobStatistics2();
755+
if (biEngineStats != null) {
756+
queryStatisticsPb.setBiEngineStatistics(biEngineStats.toPb());
757+
}
736758
queryStatisticsPb.setBillingTier(billingTier);
737759
queryStatisticsPb.setCacheHit(cacheHit);
738760
queryStatisticsPb.setDdlOperationPerformed(ddlOperationPerformed);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@
3636

3737
public class JobStatisticsTest {
3838

39+
private static final BiEngineReason BI_ENGINE_REASON =
40+
BiEngineReason.newBuilder()
41+
.setMessage("Detected unsupported join type")
42+
.setCode("UNSUPPORTED_SQL_TEXT")
43+
.build();
44+
private static final BiEngineStats BI_ENGINE_STATS =
45+
BiEngineStats.newBuilder()
46+
.setBiEngineReasons(ImmutableList.of(BI_ENGINE_REASON))
47+
.setBiEngineMode("DISABLED")
48+
.build();
3949
private static final Integer BILLING_TIER = 42;
4050
private static final Boolean CACHE_HIT = true;
4151
private static final String DDL_OPERATION_PERFORMED = "SKIP";
@@ -154,6 +164,7 @@ public class JobStatisticsTest {
154164
.setCreationTimestamp(CREATION_TIME)
155165
.setEndTime(END_TIME)
156166
.setStartTime(START_TIME)
167+
.setBiEngineStats(BI_ENGINE_STATS)
157168
.setBillingTier(BILLING_TIER)
158169
.setCacheHit(CACHE_HIT)
159170
.setDDLOperationPerformed(DDL_OPERATION_PERFORMED)
@@ -246,6 +257,7 @@ public void testBuilder() {
246257
assertEquals(CREATION_TIME, QUERY_STATISTICS.getCreationTime());
247258
assertEquals(START_TIME, QUERY_STATISTICS.getStartTime());
248259
assertEquals(END_TIME, QUERY_STATISTICS.getEndTime());
260+
assertEquals(BI_ENGINE_STATS, QUERY_STATISTICS.getBiEngineStats());
249261
assertEquals(BILLING_TIER, QUERY_STATISTICS.getBillingTier());
250262
assertEquals(CACHE_HIT, QUERY_STATISTICS.getCacheHit());
251263
assertEquals(DDL_OPERATION_PERFORMED, QUERY_STATISTICS.getDdlOperationPerformed());

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,6 +2900,14 @@ public void testQueryJob() throws InterruptedException, TimeoutException {
29002900
assertTrue(bigquery.delete(destinationTable));
29012901
Job queryJob = bigquery.getJob(remoteJob.getJobId());
29022902
JobStatistics.QueryStatistics statistics = queryJob.getStatistics();
2903+
if (statistics.getBiEngineStats() != null) {
2904+
assertEquals(statistics.getBiEngineStats().getBiEngineMode(), "DISABLED");
2905+
assertEquals(
2906+
statistics.getBiEngineStats().getBiEngineReasons().get(0).getCode(), "OTHER_REASON");
2907+
assertEquals(
2908+
statistics.getBiEngineStats().getBiEngineReasons().get(0).getMessage(),
2909+
"Query output to destination table is not supported.");
2910+
}
29032911
assertNotNull(statistics.getQueryPlan());
29042912
}
29052913

0 commit comments

Comments
 (0)