diff --git a/docs/snippets.py b/docs/snippets.py index 3a46cd36c..7f9b4f59e 100644 --- a/docs/snippets.py +++ b/docs/snippets.py @@ -125,6 +125,8 @@ def test_create_partitioned_table(client, to_delete): dataset = client.create_dataset(dataset_ref) to_delete.append(dataset) + # TODO(tswast): remove this snippet once cloud.google.com is updated to use + # samples/snippets/create_partitioned_table.py # [START bigquery_create_table_partitioned] # from google.cloud import bigquery # client = bigquery.Client() diff --git a/samples/snippets/create_partitioned_table.py b/samples/snippets/create_partitioned_table.py new file mode 100644 index 000000000..0277d7d0f --- /dev/null +++ b/samples/snippets/create_partitioned_table.py @@ -0,0 +1,45 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def create_partitioned_table(table_id): + your_fully_qualified_table_id = table_id + + # [START bigquery_create_table_partitioned] + from google.cloud import bigquery + + client = bigquery.Client() + + # Use format "your-project.your_dataset.your_table_name" for table_id + table_id = your_fully_qualified_table_id + schema = [ + bigquery.SchemaField("name", "STRING"), + bigquery.SchemaField("post_abbr", "STRING"), + bigquery.SchemaField("date", "DATE"), + ] + table = bigquery.Table(table_id, schema=schema) + table.time_partitioning = bigquery.TimePartitioning( + type_=bigquery.TimePartitioningType.DAY, + field="date", # name of column to use for partitioning + expiration_ms=1000 * 60 * 60 * 24 * 90, + ) # 90 days + + table = client.create_table(table) + + print( + f"Created table {table.project}.{table.dataset_id}.{table.table_id}, " + f"partitioned on column {table.time_partitioning.field}." + ) + # [END bigquery_create_table_partitioned] + return table diff --git a/samples/snippets/create_partitioned_table_test.py b/samples/snippets/create_partitioned_table_test.py new file mode 100644 index 000000000..0f684fcb0 --- /dev/null +++ b/samples/snippets/create_partitioned_table_test.py @@ -0,0 +1,34 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import typing + +import create_partitioned_table + +if typing.TYPE_CHECKING: + import pytest + + +def test_create_partitioned_table( + capsys: "pytest.CaptureFixture[str]", + random_table_id: str, +) -> None: + table = create_partitioned_table.create_partitioned_table(random_table_id) + + out, _ = capsys.readouterr() + assert "Created" in out + assert random_table_id in out + + assert table.time_partitioning.type_ == "DAY" + assert table.time_partitioning.field == "date"