Skip to content

Commit 112bcde

Browse files
authored
feat(bigquery): add reservation usage stats to query statistics (#3403)
* feat: add reservation usage stats to query statistics This PR plumbs through reservation usage stats reported per-job. Also added a small integration test for testing query statistics, as we didn't have one previously.
1 parent 8538725 commit 112bcde

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

Diff for: bigquery/integration_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -1630,6 +1630,63 @@ func TestIntegration_TableUpdate(t *testing.T) {
16301630
}
16311631
}
16321632

1633+
func TestIntegration_QueryStatistics(t *testing.T) {
1634+
// Make a bunch of assertions on a simple query.
1635+
if client == nil {
1636+
t.Skip("Integration tests skipped")
1637+
}
1638+
ctx := context.Background()
1639+
1640+
q := client.Query("SELECT 17 as foo, 3.14 as bar")
1641+
// disable cache to ensure we have query statistics
1642+
q.DisableQueryCache = true
1643+
1644+
job, err := q.Run(ctx)
1645+
if err != nil {
1646+
t.Fatalf("job Run failure: %v", err)
1647+
}
1648+
status, err := job.Wait(ctx)
1649+
if err != nil {
1650+
t.Fatalf("job Wait failure: %v", err)
1651+
}
1652+
if status.Statistics == nil {
1653+
t.Fatal("expected job statistics, none found")
1654+
}
1655+
1656+
if status.Statistics.NumChildJobs != 0 {
1657+
t.Errorf("expected no children, %d reported", status.Statistics.NumChildJobs)
1658+
}
1659+
1660+
if status.Statistics.ParentJobID != "" {
1661+
t.Errorf("expected no parent, but parent present: %s", status.Statistics.ParentJobID)
1662+
}
1663+
1664+
if status.Statistics.Details == nil {
1665+
t.Fatal("expected job details, none present")
1666+
}
1667+
1668+
qStats, ok := status.Statistics.Details.(*QueryStatistics)
1669+
if !ok {
1670+
t.Fatalf("expected query statistics not present")
1671+
}
1672+
1673+
if qStats.CacheHit {
1674+
t.Error("unexpected cache hit")
1675+
}
1676+
1677+
if qStats.StatementType != "SELECT" {
1678+
t.Errorf("expected SELECT statement type, got: %s", qStats.StatementType)
1679+
}
1680+
1681+
if len(qStats.QueryPlan) == 0 {
1682+
t.Error("expected query plan, none present")
1683+
}
1684+
1685+
if len(qStats.Timeline) == 0 {
1686+
t.Error("expected query timeline, none present")
1687+
}
1688+
}
1689+
16331690
func TestIntegration_Load(t *testing.T) {
16341691
if client == nil {
16351692
t.Skip("Integration tests skipped")

Diff for: bigquery/job.go

+23
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ type JobStatistics struct {
347347
// ScriptStatistics includes information run as part of a child job within
348348
// a script.
349349
ScriptStatistics *ScriptStatistics
350+
351+
// ReservationUsage attributes slot consumption to reservations.
352+
ReservationUsage []*ReservationUsage
350353
}
351354

352355
// Statistics is one of ExtractStatistics, LoadStatistics or QueryStatistics.
@@ -561,6 +564,25 @@ type QueryTimelineSample struct {
561564
SlotMillis int64
562565
}
563566

567+
// ReservationUsage contains information about a job's usage of a single reservation.
568+
type ReservationUsage struct {
569+
// SlotMillis reports the slot milliseconds utilized within in the given reservation.
570+
SlotMillis int64
571+
// Name indicates the utilized reservation name, or "unreserved" for ondemand usage.
572+
Name string
573+
}
574+
575+
func bqToReservationUsage(ru []*bq.JobStatisticsReservationUsage) []*ReservationUsage {
576+
var usage []*ReservationUsage
577+
for _, in := range ru {
578+
usage = append(usage, &ReservationUsage{
579+
SlotMillis: in.SlotMs,
580+
Name: in.Name,
581+
})
582+
}
583+
return usage
584+
}
585+
564586
// ScriptStatistics report information about script-based query jobs.
565587
type ScriptStatistics struct {
566588
EvaluationKind string
@@ -810,6 +832,7 @@ func (j *Job) setStatistics(s *bq.JobStatistics, c *Client) {
810832
NumChildJobs: s.NumChildJobs,
811833
ParentJobID: s.ParentJobId,
812834
ScriptStatistics: bqToScriptStatistics(s.ScriptStatistics),
835+
ReservationUsage: bqToReservationUsage(s.ReservationUsage),
813836
}
814837
switch {
815838
case s.Extract != nil:

0 commit comments

Comments
 (0)