Skip to content

Commit 261c8d9

Browse files
authored
feat(bigquery): add ExportDataStatstics to QueryStatistics (#9371)
Fixes https://togithub.com/googleapis/google-cloud-go/issues/8401
1 parent fa31ec0 commit 261c8d9

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

bigquery/integration_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -2762,6 +2762,78 @@ func TestIntegration_ExtractExternal(t *testing.T) {
27622762
}
27632763
}
27642764

2765+
func TestIntegration_ExportDataStatistics(t *testing.T) {
2766+
// Create a table, extract it to GCS using EXPORT DATA statement.
2767+
if client == nil {
2768+
t.Skip("Integration tests skipped")
2769+
}
2770+
ctx := context.Background()
2771+
schema := Schema{
2772+
{Name: "name", Type: StringFieldType},
2773+
{Name: "num", Type: IntegerFieldType},
2774+
}
2775+
table := newTable(t, schema)
2776+
defer table.Delete(ctx)
2777+
2778+
// Extract to a GCS object as CSV.
2779+
bucketName := testutil.ProjID()
2780+
uri := fmt.Sprintf("gs://%s/bq-export-test-*.csv", bucketName)
2781+
defer func() {
2782+
it := storageClient.Bucket(bucketName).Objects(ctx, &storage.Query{
2783+
MatchGlob: "bq-export-test-*.csv",
2784+
})
2785+
for {
2786+
obj, err := it.Next()
2787+
if err == iterator.Done {
2788+
break
2789+
}
2790+
if err != nil {
2791+
t.Logf("failed to delete bucket: %v", err)
2792+
continue
2793+
}
2794+
err = storageClient.Bucket(bucketName).Object(obj.Name).Delete(ctx)
2795+
t.Logf("deleted object %s: %v", obj.Name, err)
2796+
}
2797+
}()
2798+
2799+
// EXPORT DATA to GCS object.
2800+
sql := fmt.Sprintf(`EXPORT DATA
2801+
OPTIONS (
2802+
uri = '%s',
2803+
format = 'CSV',
2804+
overwrite = true,
2805+
header = true,
2806+
field_delimiter = ';'
2807+
)
2808+
AS (
2809+
SELECT 'a' as name, 1 as num
2810+
UNION ALL
2811+
SELECT 'b' as name, 2 as num
2812+
UNION ALL
2813+
SELECT 'c' as name, 3 as num
2814+
);`,
2815+
uri)
2816+
stats, _, err := runQuerySQL(ctx, sql)
2817+
if err != nil {
2818+
t.Fatal(err)
2819+
}
2820+
2821+
qStats, ok := stats.Details.(*QueryStatistics)
2822+
if !ok {
2823+
t.Fatalf("expected query statistics not present")
2824+
}
2825+
2826+
if qStats.ExportDataStatistics == nil {
2827+
t.Fatal("jobStatus missing ExportDataStatistics")
2828+
}
2829+
if qStats.ExportDataStatistics.FileCount != 1 {
2830+
t.Fatalf("expected ExportDataStatistics to have 1 file, but got %d files", qStats.ExportDataStatistics.FileCount)
2831+
}
2832+
if qStats.ExportDataStatistics.RowCount != 3 {
2833+
t.Fatalf("expected ExportDataStatistics to have 3 rows, got %d rows", qStats.ExportDataStatistics.RowCount)
2834+
}
2835+
}
2836+
27652837
func TestIntegration_ReadNullIntoStruct(t *testing.T) {
27662838
// Reading a null into a struct field should return an error (not panic).
27672839
if client == nil {

bigquery/job.go

+25
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,30 @@ type QueryStatistics struct {
505505

506506
// The DDL target table, present only for CREATE/DROP FUNCTION/PROCEDURE queries.
507507
DDLTargetRoutine *Routine
508+
509+
// Statistics for the EXPORT DATA statement as part of Query Job.
510+
ExportDataStatistics *ExportDataStatistics
511+
}
512+
513+
// ExportDataStatistics represents statistics for
514+
// a EXPORT DATA statement as part of Query Job.
515+
type ExportDataStatistics struct {
516+
// Number of destination files generated.
517+
FileCount int64
518+
519+
// Number of destination rows generated.
520+
RowCount int64
521+
}
522+
523+
func bqToExportDataStatistics(in *bq.ExportDataStatistics) *ExportDataStatistics {
524+
if in == nil {
525+
return nil
526+
}
527+
stats := &ExportDataStatistics{
528+
FileCount: in.FileCount,
529+
RowCount: in.RowCount,
530+
}
531+
return stats
508532
}
509533

510534
// BIEngineStatistics contains query statistics specific to the use of BI Engine.
@@ -1028,6 +1052,7 @@ func (j *Job) setStatistics(s *bq.JobStatistics, c *Client) {
10281052
DDLTargetTable: bqToTable(s.Query.DdlTargetTable, c),
10291053
DDLOperationPerformed: s.Query.DdlOperationPerformed,
10301054
DDLTargetRoutine: bqToRoutine(s.Query.DdlTargetRoutine, c),
1055+
ExportDataStatistics: bqToExportDataStatistics(s.Query.ExportDataStatistics),
10311056
StatementType: s.Query.StatementType,
10321057
TotalBytesBilled: s.Query.TotalBytesBilled,
10331058
TotalBytesProcessed: s.Query.TotalBytesProcessed,

0 commit comments

Comments
 (0)