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

docs: revised create_partitioned_table sample #1447

Merged
merged 2 commits into from
Oct 5, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
45 changes: 45 additions & 0 deletions samples/snippets/create_partitioned_table.py
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions samples/snippets/create_partitioned_table_test.py
Original file line number Diff line number Diff line change
@@ -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"