Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b8c9276

Browse filesBrowse files
docs: revised relax column mode sample (#1467)
* docs: Revised relax_column sample * add todo for snippets.py cleanup --------- Co-authored-by: Tim Swast <swast@google.com>
1 parent 40ba859 commit b8c9276
Copy full SHA for b8c9276

File tree

Expand file treeCollapse file tree

3 files changed

+100
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+100
-0
lines changed
Open diff view settings
Collapse file

‎docs/snippets.py‎

Copy file name to clipboardExpand all lines: docs/snippets.py
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ def test_relax_column(client, to_delete):
265265
dataset = client.create_dataset(dataset)
266266
to_delete.append(dataset)
267267

268+
# TODO(tswast): remove code sample once references to it on
269+
# cloud.google.com are updated to samples/snippets/relax_column.py
268270
# [START bigquery_relax_column]
269271
# from google.cloud import bigquery
270272
# client = bigquery.Client()
Collapse file

‎samples/snippets/relax_column.py‎

Copy file name to clipboard
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.cloud import bigquery
16+
17+
18+
def relax_column(table_id: str) -> bigquery.Table:
19+
orig_table_id = table_id
20+
21+
# [START bigquery_relax_column]
22+
from google.cloud import bigquery
23+
24+
client = bigquery.Client()
25+
26+
# TODO(dev): Change table_id to full name of the table you want to create.
27+
table_id = "your-project.your_dataset.your_table"
28+
29+
# [END bigquery_relax_column]
30+
table_id = orig_table_id
31+
32+
# [START bigquery_relax_column]
33+
table = client.get_table(table_id)
34+
new_schema = []
35+
for field in table.schema:
36+
if field.mode != "REQUIRED":
37+
new_schema.append(field)
38+
else:
39+
# SchemaField properties cannot be edited after initialization.
40+
# To make changes, construct new SchemaField objects.
41+
new_field = field.to_api_repr()
42+
new_field["mode"] = "NULLABLE"
43+
relaxed_field = bigquery.SchemaField.from_api_repr(new_field)
44+
new_schema.append(relaxed_field)
45+
46+
table.schema = new_schema
47+
table = client.update_table(table, ["schema"])
48+
49+
print(f"Updated {table_id} schema: {table.schema}.")
50+
51+
# [END bigquery_relax_column]
52+
return table
Collapse file
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import typing
16+
17+
from google.cloud import bigquery
18+
19+
import relax_column
20+
21+
if typing.TYPE_CHECKING:
22+
import pytest
23+
24+
25+
def test_relax_column(
26+
capsys: "pytest.CaptureFixture[str]",
27+
bigquery_client: bigquery.Client,
28+
random_table_id: str,
29+
) -> None:
30+
table = bigquery.Table(
31+
random_table_id,
32+
schema=[
33+
bigquery.SchemaField("string_col", "STRING", mode="NULLABLE"),
34+
bigquery.SchemaField("string_col2", "STRING", mode="REQUIRED"),
35+
],
36+
)
37+
38+
bigquery_client.create_table(table)
39+
table = relax_column.relax_column(random_table_id)
40+
41+
out, _ = capsys.readouterr()
42+
43+
assert all(field.mode == "NULLABLE" for field in table.schema)
44+
assert "REQUIRED" not in out
45+
assert "NULLABLE" in out
46+
assert random_table_id in out

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.