Repository: googleapis/google-cloud-go
Pull request #5081 (2021-11-04 16:56:54)

feat(bigquery): add BI Engine information to query statistics
Fixes: https://github.com/googleapis/google-cloud-go/issues/5080


Patch:
diff --git a/bigquery/job.go b/bigquery/job.go
index 59ef692c00..2344fab039 100644
--- a/bigquery/job.go
+++ b/bigquery/job.go
@@ -419,6 +419,10 @@ type LoadStatistics struct {
 
 // QueryStatistics contains statistics about a query job.
 type QueryStatistics struct {
+
+	// BI-Engine specific statistics.
+	BIEngineStatistics *BIEngineStatistics
+
 	// Billing tier for the job.
 	BillingTier int64
 
@@ -483,6 +487,51 @@ type QueryStatistics struct {
 	DDLTargetRoutine *Routine
 }
 
+// BIEngineStatistics contains query statistics specific to the use of BI Engine.
+type BIEngineStatistics struct {
+	// Specifies which mode of BI Engine acceleration was performed.
+	BIEngineMode string
+
+	// In case of DISABLED or PARTIAL BIEngineMode, these
+	// contain the explanatory reasons as to why BI Engine could not
+	// accelerate. In case the full query was accelerated, this field is not
+	// populated.
+	BIEngineReasons []*BIEngineReason
+}
+
+func bqToBIEngineStatistics(in *bq.BiEngineStatistics) *BIEngineStatistics {
+	if in == nil {
+		return nil
+	}
+	stats := &BIEngineStatistics{
+		BIEngineMode: in.BiEngineMode,
+	}
+	for _, v := range in.BiEngineReasons {
+		stats.BIEngineReasons = append(stats.BIEngineReasons, bqToBIEngineReason(v))
+	}
+	return stats
+}
+
+// BIEngineReason contains more detailed information about why a query wasn't fully
+// accelerated.
+type BIEngineReason struct {
+	// High-Level BI engine reason for partial or disabled acceleration.
+	Code string
+
+	// Human-readable reason for partial or disabled acceleration.
+	Message string
+}
+
+func bqToBIEngineReason(in *bq.BiEngineReason) *BIEngineReason {
+	if in == nil {
+		return nil
+	}
+	return &BIEngineReason{
+		Code:    in.Code,
+		Message: in.Message,
+	}
+}
+
 // ExplainQueryStage describes one stage of a query.
 type ExplainQueryStage struct {
 	// CompletedParallelInputs: Number of parallel input segments completed.
@@ -922,6 +971,7 @@ func (j *Job) setStatistics(s *bq.JobStatistics, c *Client) {
 			tables = append(tables, bqToTable(tr, c))
 		}
 		js.Details = &QueryStatistics{
+			BIEngineStatistics:            bqToBIEngineStatistics(s.Query.BiEngineStatistics),
 			BillingTier:                   s.Query.BillingTier,
 			CacheHit:                      s.Query.CacheHit,
 			DDLTargetTable:                bqToTable(s.Query.DdlTargetTable, c),


Test patch:
diff --git a/bigquery/integration_test.go b/bigquery/integration_test.go
index 6ab8da7e63..aaf786541b 100644
--- a/bigquery/integration_test.go
+++ b/bigquery/integration_test.go
@@ -2015,6 +2015,20 @@ func TestIntegration_QueryStatistics(t *testing.T) {
 	if len(qStats.Timeline) == 0 {
 		t.Error("expected query timeline, none present")
 	}
+
+	if qStats.BIEngineStatistics == nil {
+		t.Error("expected BIEngine statistics, none present")
+	}
+
+	expectedMode := true
+	for _, m := range []string{"FULL", "PARTIAL", "DISABLED"} {
+		if qStats.BIEngineStatistics.BIEngineMode == m {
+			expectedMode = true
+		}
+	}
+	if !expectedMode {
+		t.Errorf("unexpected BIEngineMode for BI Engine statistics, got %s", qStats.BIEngineStatistics.BIEngineMode)
+	}
 }
 
 func TestIntegration_Load(t *testing.T) {