Skip to content

feat(bigquery): add column data policies to SchemaField - #18710

Open
AliAbouelazm wants to merge 3 commits into
googleapis:mainfrom
AliAbouelazm:feat/bigquery-data-policies
Open

AliAbouelazm wants to merge 3 commits into
googleapis:mainfrom
AliAbouelazm:feat/bigquery-data-policies

Conversation

@AliAbouelazm

Copy link
Copy Markdown
Contributor

Adds data_policies to SchemaField so callers can read and set column data policy names without using _properties. Names are serialized as dataPolicies entries, and equality and hashing account for them regardless of order.

The tests cover unset, null, empty, multiple and nested policies, API round trips, repr, and schemas that use policy tags on other columns. The usage guide explains the API representation and the limits around removing the last policy and dataPolicyList precedence.

Validation on Python 3.12: 2,859 unit tests passed, 2 skipped. Black, flake8, mypy and the documentation build passed. The test process used default OpenTelemetry sampling because the inherited setting caused the same seven failures on unmodified main.

Fixes #17617.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for column-level data policies in BigQuery schemas by adding a data_policies property to SchemaField, along with documentation and comprehensive unit tests. The review feedback suggests two key improvements: adding a type check to prevent strings or bytes from being silently iterated character-by-character when passed to data_policies, and replacing fragile index slicing ([:-1]) in the __repr__ method with dynamic filtering based on the named tuple's fields to ensure future-proof code.

Comment thread packages/google-cloud-bigquery/google/cloud/bigquery/schema.py
Comment thread packages/google-cloud-bigquery/google/cloud/bigquery/schema.py Outdated
@AliAbouelazm
AliAbouelazm marked this pull request as ready for review October 16, 2026 06:21
@AliAbouelazm
AliAbouelazm requested review from a team as code owners October 16, 2026 06:21
@AliAbouelazm
AliAbouelazm requested review from tswast and removed request for a team October 16, 2026 06:21
@parthea parthea self-assigned this Oct 16, 2026
@parthea parthea added kokoro:force-run Add this label to force Kokoro to re-run the tests. kokoro:run Add this label to force Kokoro to re-run the tests. labels Oct 16, 2026
@yoshi-kokoro yoshi-kokoro removed kokoro:run Add this label to force Kokoro to re-run the tests. kokoro:force-run Add this label to force Kokoro to re-run the tests. labels Oct 16, 2026
@tswast tswast added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Oct 20, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Oct 20, 2026
rounding_mode: Union[enums.RoundingMode, str, None] = None,
foreign_type_definition: Optional[str] = None,
timestamp_precision: Optional[enums.TimestampPrecision] = None,
data_policies: Union[Iterable[str], None, _DefaultSentinel] = _DEFAULT_VALUE,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent accidental breaking changes in the future, let's use forced kwarg arguments here.

Suggested change
data_policies: Union[Iterable[str], None, _DefaultSentinel] = _DEFAULT_VALUE,
*,
data_policies: Union[Iterable[str], None, _DefaultSentinel] = _DEFAULT_VALUE,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, made data_policies keyword only and added a test to check that existing positional arguments still work.

adjusted_key = tuple(
getattr(key_to_show, field)
for field in key_to_show._fields
if field != "data_policies"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's create a kwarg_only_args tuple so that we can add to this in the future.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added kwarg_only_args and used it to handle the keyword arguments in repr.

"""
resource = self._properties.get("dataPolicies")
return (
tuple(policy["name"] for policy in resource)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It worries me to extract just a single field from this when the REST represention is an object. Is there any chance that we might add keys to this in the future? If so, let's create a class for DataPolicy.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Added a DataPolicy class with API conversion methods that preserve the full object. Updated the docs and tests too.

@parthea parthea added kokoro:force-run Add this label to force Kokoro to re-run the tests. kokoro:run Add this label to force Kokoro to re-run the tests. labels Oct 21, 2026
@yoshi-kokoro yoshi-kokoro removed kokoro:run Add this label to force Kokoro to re-run the tests. kokoro:force-run Add this label to force Kokoro to re-run the tests. labels Oct 21, 2026
@parthea parthea assigned tswast and unassigned parthea Oct 21, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

google-cloud-bigquery: support column-level data policies (dataPolicies) in SchemaField

4 participants