Skip to content

Commit 3867338

Browse files
Require partition filtering when partition field is set (#182)
* Require partition filtering when partition field is set * Add partition to test
1 parent b3fa29b commit 3867338

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

cloud/bqx/schema.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ func ParsePDT(fq string) (PDT, error) {
180180

181181
// UpdateTable will update an existing table. Returns error if the table
182182
// doesn't already exist, or if the schema changes are incompatible.
183-
func (pdt PDT) UpdateTable(ctx context.Context, client *bigquery.Client, schema bigquery.Schema) error {
183+
func (pdt PDT) UpdateTable(ctx context.Context, client *bigquery.Client, schema bigquery.Schema,
184+
partitioning *bigquery.TimePartitioning) error {
184185
// See if dataset exists, or create it.
185186
ds := client.Dataset(pdt.Dataset)
186187
_, err := ds.Metadata(ctx)
@@ -198,9 +199,14 @@ func (pdt PDT) UpdateTable(ctx context.Context, client *bigquery.Client, schema
198199
return err
199200
}
200201

202+
// If a partition field is set, enforce partition filtering.
203+
// This prevents unintentional full data scans on large tables.
204+
requirePartition := partitioning != nil && partitioning.Field != ""
205+
201206
// If table already exists, attempt to update the schema.
202207
changes := bigquery.TableMetadataToUpdate{
203-
Schema: schema,
208+
Schema: schema,
209+
RequirePartitionFilter: requirePartition,
204210
}
205211

206212
_, err = t.Update(ctx, changes, meta.ETag)
@@ -225,11 +231,16 @@ func (pdt PDT) CreateTable(ctx context.Context, client *bigquery.Client, schema
225231

226232
t := ds.Table(pdt.Table)
227233

234+
// If a partition field is set, enforce partition filtering.
235+
// This prevents unintentional full data scans on large tables.
236+
requirePartition := partitioning != nil && partitioning.Field != ""
237+
228238
meta := &bigquery.TableMetadata{
229-
Schema: schema,
230-
TimePartitioning: partitioning,
231-
Clustering: clustering,
232-
Description: description,
239+
Schema: schema,
240+
TimePartitioning: partitioning,
241+
RequirePartitionFilter: requirePartition,
242+
Clustering: clustering,
243+
Description: description,
233244
}
234245

235246
err := t.Create(ctx, meta)

cloud/bqx/schema_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ func TestCreateAndUpdate(t *testing.T) {
236236
log.Fatal(err)
237237
}
238238

239+
partition := &bigquery.TimePartitioning{Field: "Timestamp"}
240+
239241
name := "mlab-testing." + randName("ds") + randName(".tbl")
240242
t.Log("Using:", name)
241243
pdt, err := bqx.ParsePDT(name)
@@ -268,7 +270,7 @@ func TestCreateAndUpdate(t *testing.T) {
268270
t.Log("Created dataset for", name)
269271

270272
// Update non-existing table
271-
err = pdt.UpdateTable(ctx, client, schema)
273+
err = pdt.UpdateTable(ctx, client, schema, partition)
272274
if err == nil {
273275
t.Error("Update non-existing table should have failed")
274276
}
@@ -282,13 +284,13 @@ func TestCreateAndUpdate(t *testing.T) {
282284

283285
// Create should succeed now.
284286
err = pdt.CreateTable(ctx, client, schema, "description",
285-
&bigquery.TimePartitioning{Field: "Timestamp"}, nil)
287+
partition, nil)
286288
if err != nil {
287289
t.Error(err)
288290
}
289291

290292
// Update
291-
err = pdt.UpdateTable(ctx, client, schema)
293+
err = pdt.UpdateTable(ctx, client, schema, partition)
292294
if err != nil {
293295
t.Error(err)
294296
}

0 commit comments

Comments
 (0)