BigQuery supports partitioned tables and table clustering. For more information about partitions and clusters in BigQuery, see Introduction to partitioned tables and Creating and using clustered tables.
Before you begin
In the Cloud Console, go to the Dataform page.
Select or create a repository.
Select or create a development workspace.
Required roles
To get the permissions that you need to configure table partitions and clusters,
ask your administrator to grant you the
Dataform Editor (roles/dataform.editor
) IAM role on workspaces.
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Create a table partition
To create a table partition, add a BigQuery
partition_expression
to the bigquery
block in a table definition SQLX file.
To create a table partition, follow these steps:
- Go to your development workspace.
- In the Files pane, expand
definitions/
. - Open a table definition SQLX file.
In the
config
block, add thebigquery
block below the table type declaration in the following format:config { type: "table", bigquery: { } }
In the
bigquery
block, enter the following code snippet:partitionBy: "PARTITION_EXPRESSION"
Replace PARTITION_EXPRESSION with an expression for partitioning the table.
Optional: Click Format.
The following code sample shows partitioning a table by hour in a table definition SQLX file:
config {
type: "table",
bigquery: {
partitionBy: "DATETIME_TRUNC(<timestamp_column>, HOUR)"
}
}
The following code sample shows partitioning a table by an integer value in a table definition SQLX file:
config {
type: "table",
bigquery: {
partitionBy: "RANGE_BUCKET(<integer_column>, GENERATE_ARRAY(0, 1000000, 1000))"
}
}
Set a partition filter
For more information on the partition filter in BigQuery, see Setting the require partition filter attribute on a partitioned table.
To set a partition filter, follow these steps:
- Go to your development workspace.
- In the Files pane, expand
definitions/
. - Open a partitioned table definition SQLX file.
In the
bigquery
block, enter the following code snippet:requirePartitionFilter : true
Optional: Click Format.
The following code sample shows a partition filter set in the bigquery
block
of a partitioned table SQLX file:
config {
type: "table",
bigquery: {
partitionBy: "DATE(ts)",
requirePartitionFilter : true
}
}
SELECT CURRENT_TIMESTAMP() AS ts
Set a retention period for partitions
To control retention of all partitions in a partitioned table, follow these steps:
- Go to your development workspace.
- In the Files pane, expand
definitions/
. - Open a partitioned table definition SQLX file.
In the
bigquery
block, enter the following code snippet:partitionExpirationDays: NUMBER_OF_DAYS
Replace NUMBER_OF_DAYS with the number of days that you want to retain partitions for.
Optional: Click Format.
The following code sample shows a retention period for partitions set to
14 days in the bigquery
block of a partitioned table SQLX file:
config {
type: "table",
bigquery: {
partitionBy: "DATE(ts)",
partitionExpirationDays: 14,
}
}
SELECT CURRENT_TIMESTAMP() AS ts
Create a table cluster
To create a table cluster, add a BigQuery clustering_column_list to the bigquery
block in
a table definition SQLX file.
To create a table cluster, follow these steps:
- Go to your development workspace.
- In the Files pane, expand
definitions/
. - Open a table definition SQLX file.
In the
bigquery
block, enter the following code snippet:clusterBy: ["CLUSTER_COLUMN"]
Replace CLUSTER_COLUMN with the name of the column by which you want to cluster the table.
Optional: Click Format.
The following code sample shows a partitioned table clustered by
name
and revenue
columns:
config {
type: "table",
bigquery: {
partitionBy: "DATE(ts)",
clusterBy: ["name", "revenue"]
}
}
SELECT CURRENT_TIMESTAMP() as ts, name, revenue
What's next
- To learn how to reuse code with includes, see Reuse variables and functions with includes.
- To learn how to configure incremental tables, see
Configure an incremental table.