Skip to content

Commit

Permalink
feat(bigquery): support multistatement transaction statistics in jobs (
Browse files Browse the repository at this point in the history
…#4485)

* feat(bigquery): support multistatement transaction statistics in jobs
  • Loading branch information
shollyman committed Jul 22, 2021
1 parent e55a016 commit 4565eb7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bigquery/integration_test.go
Expand Up @@ -2435,6 +2435,7 @@ func TestIntegration_Scripting(t *testing.T) {
sql := `
-- Declare a variable to hold names as an array.
DECLARE top_names ARRAY<STRING>;
BEGIN TRANSACTION;
-- Build an array of the top 100 names from the year 2017.
SET top_names = (
SELECT ARRAY_AGG(name ORDER BY number DESC LIMIT 100)
Expand All @@ -2449,6 +2450,7 @@ func TestIntegration_Scripting(t *testing.T) {
SELECT word
FROM ` + "`bigquery-public-data`" + `.samples.shakespeare
);
COMMIT TRANSACTION;
`
q := client.Query(sql)
job, err := q.Run(ctx)
Expand Down Expand Up @@ -2506,6 +2508,12 @@ func TestIntegration_Scripting(t *testing.T) {
if cStatus.Statistics.ScriptStatistics.EvaluationKind == "" {
t.Errorf("child job %q didn't indicate evaluation kind", cj.ID())
}
if cStatus.Statistics.TransactionInfo == nil {
t.Errorf("child job %q didn't have transaction info present", cj.ID())
}
if cStatus.Statistics.TransactionInfo.TransactionID == "" {
t.Errorf("child job %q didn't have transactionID present", cj.ID())
}
}

}
Expand Down
19 changes: 19 additions & 0 deletions bigquery/job.go
Expand Up @@ -372,6 +372,9 @@ type JobStatistics struct {

// ReservationUsage attributes slot consumption to reservations.
ReservationUsage []*ReservationUsage

// TransactionInfo indicates the transaction ID associated with the job, if any.
TransactionInfo *TransactionInfo
}

// Statistics is one of ExtractStatistics, LoadStatistics or QueryStatistics.
Expand Down Expand Up @@ -880,6 +883,7 @@ func (j *Job) setStatistics(s *bq.JobStatistics, c *Client) {
ParentJobID: s.ParentJobId,
ScriptStatistics: bqToScriptStatistics(s.ScriptStatistics),
ReservationUsage: bqToReservationUsage(s.ReservationUsage),
TransactionInfo: bqToTransactionInfo(s.TransactionInfo),
}
switch {
case s.Extract != nil:
Expand Down Expand Up @@ -983,3 +987,18 @@ func timelineFromProto(timeline []*bq.QueryTimelineSample) []*QueryTimelineSampl
}
return res
}

// TransactionInfo contains information about a multi-statement transaction that may have associated with a job.
type TransactionInfo struct {
// TransactionID is the system-generated identifier for the transaction.
TransactionID string
}

func bqToTransactionInfo(in *bq.TransactionInfo) *TransactionInfo {
if in == nil {
return nil
}
return &TransactionInfo{
TransactionID: in.TransactionId,
}
}

0 comments on commit 4565eb7

Please sign in to comment.