Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bigquery): support multistatement transaction statistics in jobs #4485

Merged
merged 3 commits into from Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
}
}