-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: add bigframes.bigquery.aead.* scalar functions
#17168
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
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3ccb3ce
feat: add `bigframes.bigquery.aead.*` scalar functions
tswast 4c94ec3
update function definitions to match https://docs.cloud.google.com/bi…
tswast c6b94fa
create a spec for code generation
tswast 7cb7e76
use uv for script reproducibility
tswast d2f1e98
first pass at code generation
tswast beb95ef
Merge remote-tracking branch 'origin/main' into tswast-bbq-gen
tswast 3a06e11
manual edits to generator
tswast 47c47af
remove imports after generation
tswast 639cfc7
generate tests
tswast e84cbdc
align pre-commit with migration to ruff
tswast 8fa1159
sort imports
tswast 8e4b7c5
sort imports
tswast 1332bc5
update gen to run ruff
tswast 4f35644
fix mypy
tswast c69a7f3
include namespace in bbq
tswast c555f4a
attempt to fix snapshot
tswast 9b09490
Merge branch 'main' into tswast-bbq-gen
tswast d8d2cbf
remove bigframes vendored as first-party
tswast be35b4c
Merge remote-tracking branch 'origin/tswast-bbq-gen' into tswast-bbq-gen
tswast 5ea61c2
ruff
tswast a463ea6
create template files
tswast 7ad2c2b
split main
tswast 8a48769
make generation more deterministic
tswast 15aa975
Merge remote-tracking branch 'origin/main' into tswast-bbq-gen
tswast bd939c8
adjust formatting
tswast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # Copyright 2026 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 | ||
| # | ||
| # http://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. | ||
|
|
||
| """Utilities for working with GoogleSqlScalarOps.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any, Union | ||
|
|
||
| import bigframes.core.col | ||
| import bigframes.core.expression as ex | ||
| import bigframes.core.sentinels as sentinels | ||
| import bigframes.series as series | ||
| from bigframes.operations import googlesql | ||
|
|
||
|
|
||
| def apply_googlesql_scalar_op( | ||
| op: googlesql.GoogleSqlScalarOp, | ||
| *args: Any, | ||
| ) -> Union[series.Series, bigframes.core.col.Expression]: | ||
| """Applies a GoogleSQL scalar operator to the given arguments. | ||
|
|
||
| Handles a mix of Series, Expression, and literal inputs. | ||
|
|
||
| Args: | ||
| op (googlesql.GoogleSqlScalarOp): | ||
| The operator to apply. | ||
| *args (Any): | ||
| The arguments to apply the operator to. | ||
|
|
||
| Returns: | ||
| bigframes.pandas.Series | bigframes.core.col.Expression: | ||
| The result of the operation. If any of ``args`` is a Series, returns | ||
| a Series. Otherwise, returns an Expression. | ||
| """ | ||
| # Find the first Series to use for alignment | ||
| first_series = None | ||
| for arg in args: | ||
| if isinstance(arg, series.Series): | ||
| first_series = arg | ||
| break | ||
|
|
||
| if first_series is not None: | ||
| processed_args: list[Union[bigframes.core.col.Expression, series.Series]] = [] | ||
| block = first_series._block | ||
| for arg in args: | ||
| if isinstance(arg, bigframes.core.col.Expression): | ||
| block, col_id = block.project_expr(bigframes.core.col._as_bf_expr(arg)) | ||
| processed_args.append(series.Series(block.select_column(col_id))) | ||
| elif arg is sentinels.Sentinel.ARGUMENT_DEFAULT: | ||
| processed_args.append(bigframes.core.col.Expression(ex.OmittedArg())) | ||
| else: | ||
| processed_args.append(arg) | ||
|
|
||
| # Apply the n-ary op. _apply_nary_op handles alignment of Series and literals. | ||
| result = first_series._apply_nary_op(op, processed_args, ignore_self=True) | ||
| result.name = None | ||
| return result | ||
|
|
||
| # No Series, return an Expression | ||
| expr_args = [] | ||
| for arg in args: | ||
| if isinstance(arg, bigframes.core.col.Expression): | ||
| expr_args.append(bigframes.core.col._as_bf_expr(arg)) | ||
| elif arg is sentinels.Sentinel.ARGUMENT_DEFAULT: | ||
| expr_args.append(ex.OmittedArg()) | ||
| else: | ||
| expr_args.append(ex.const(arg)) | ||
|
|
||
| return bigframes.core.col.Expression(ex.OpExpression(op, tuple(expr_args))) |
127 changes: 127 additions & 0 deletions
127
packages/bigframes/bigframes/bigquery/_operations/aead.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # Copyright 2026 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 | ||
| # | ||
| # http://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. | ||
| # | ||
| # DO NOT MODIFY THIS FILE DIRECTLY. | ||
| # This file was generated from: scripts/data/sql-functions/aead.yaml | ||
| # by the script: scripts/generate_bigframes_bigquery.py | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import datetime | ||
| from typing import Any, Literal, Optional, TypeVar, Union | ||
|
|
||
| import bigframes.bigquery._googlesql | ||
| import bigframes.core.col | ||
| import bigframes.core.expression as ex | ||
| import bigframes.core.sentinels as sentinels | ||
| import bigframes.operations as ops | ||
| import bigframes.series as series | ||
| from bigframes import dtypes | ||
| from bigframes.operations import googlesql | ||
|
|
||
| T = TypeVar("T", series.Series, bigframes.core.col.Expression) | ||
|
|
||
| _DECRYPT_BYTES_OP = googlesql.GoogleSqlScalarOp( | ||
| "AEAD.DECRYPT_BYTES", | ||
| args=(googlesql.ArgSpec(), googlesql.ArgSpec(), googlesql.ArgSpec()), | ||
| signature=lambda *args: dtypes.BYTES_DTYPE, | ||
| ) | ||
| _DECRYPT_STRING_OP = googlesql.GoogleSqlScalarOp( | ||
| "AEAD.DECRYPT_STRING", | ||
| args=(googlesql.ArgSpec(), googlesql.ArgSpec(), googlesql.ArgSpec()), | ||
| signature=lambda *args: dtypes.STRING_DTYPE, | ||
| ) | ||
| _ENCRYPT_OP = googlesql.GoogleSqlScalarOp( | ||
| "AEAD.ENCRYPT", | ||
| args=(googlesql.ArgSpec(), googlesql.ArgSpec(), googlesql.ArgSpec()), | ||
| signature=lambda *args: dtypes.BYTES_DTYPE, | ||
| ) | ||
|
|
||
|
|
||
| def decrypt_bytes( | ||
| keyset: Union[ | ||
| T, | ||
| bigframes.core.col.Expression, | ||
| Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes, dict], | ||
| ], | ||
| ciphertext: Union[ | ||
| T, | ||
| bigframes.core.col.Expression, | ||
| Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes], | ||
| ], | ||
| additional_data: Union[ | ||
| T, | ||
| bigframes.core.col.Expression, | ||
| Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes], | ||
| ], | ||
| ) -> T: | ||
| """Uses the matching key from keyset to decrypt ciphertext and verifies the integrity of the data using additional_data. Returns an error if decryption or verification fails.""" | ||
| return bigframes.bigquery._googlesql.apply_googlesql_scalar_op( | ||
| _DECRYPT_BYTES_OP, | ||
| keyset, | ||
| ciphertext, | ||
| additional_data, | ||
| ) # type: ignore | ||
|
|
||
|
|
||
| def decrypt_string( | ||
| keyset: Union[ | ||
| T, | ||
| bigframes.core.col.Expression, | ||
| Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes, dict], | ||
| ], | ||
| ciphertext: Union[ | ||
| T, | ||
| bigframes.core.col.Expression, | ||
| Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes], | ||
| ], | ||
| additional_data: Union[ | ||
| T, | ||
| bigframes.core.col.Expression, | ||
| Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], str], | ||
| ], | ||
| ) -> T: | ||
| """Like AEAD.DECRYPT_BYTES, but where additional_data is of type STRING.""" | ||
| return bigframes.bigquery._googlesql.apply_googlesql_scalar_op( | ||
| _DECRYPT_STRING_OP, | ||
| keyset, | ||
| ciphertext, | ||
| additional_data, | ||
| ) # type: ignore | ||
|
|
||
|
|
||
| def encrypt( | ||
| keyset: Union[ | ||
| T, | ||
| bigframes.core.col.Expression, | ||
| Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes, dict], | ||
| ], | ||
| plaintext: Union[ | ||
| T, | ||
| bigframes.core.col.Expression, | ||
| Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes, str], | ||
| ], | ||
| additional_data: Union[ | ||
| T, | ||
| bigframes.core.col.Expression, | ||
| Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes, str], | ||
| ], | ||
| ) -> T: | ||
| """Encrypts plaintext using the primary cryptographic key in keyset. The algorithm of the primary key must be AEAD_AES_GCM_256. Binds the ciphertext to the context defined by additional_data. Returns NULL if any input is NULL.""" | ||
| return bigframes.bigquery._googlesql.apply_googlesql_scalar_op( | ||
| _ENCRYPT_OP, | ||
| keyset, | ||
| plaintext, | ||
| additional_data, | ||
| ) # type: ignore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Copyright 2026 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 | ||
| # | ||
| # http://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. | ||
|
|
||
| """AEAD encryption functions""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from bigframes.bigquery._operations.aead import decrypt_bytes, decrypt_string, encrypt | ||
|
|
||
| __all__ = [ | ||
| "decrypt_bytes", | ||
| "decrypt_string", | ||
| "encrypt", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Copyright 2026 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 | ||
| # | ||
| # http://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. | ||
|
|
||
| """Sentinel values used throughout BigFrames.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import enum | ||
|
|
||
|
|
||
| class Sentinel(enum.Enum): | ||
| """Default values used throughout BigFrames.""" | ||
|
|
||
| """Default value for an optional argument. | ||
|
|
||
| When a parameter is set to this, that parameter is explicitly omitted | ||
| from the SQL text. This allows for NULL (None in Python) to be explicitly | ||
| passed in to optional parameters. | ||
| """ | ||
| ARGUMENT_DEFAULT = enum.auto() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| [build-system] | ||
| requires = ["setuptools"] | ||
| build-backend = "setuptools.build_meta" | ||
|
|
||
| [tool.ruff.lint.isort] | ||
| known-first-party = ["bigframes"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.