-
Notifications
You must be signed in to change notification settings - Fork 50
feat: create deploy_remote_function
and deploy_udf
functions to immediately deploy functions to BigQuery
#1832
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
Draft
tswast
wants to merge
3
commits into
main
Choose a base branch
from
refactor-deploy-immediately
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
83d2a7c
Refactor function deployment to avoid code duplication
google-labs-jules[bot] 268b275
Refactor function deployment to use distinct methods
google-labs-jules[bot] 8455c82
Simplify internal deploy_remote_function and deploy_udf calls
google-labs-jules[bot] 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,6 +116,65 @@ def remote_function( | |
remote_function.__doc__ = inspect.getdoc(bigframes.session.Session.remote_function) | ||
|
||
|
||
def deploy_remote_function( | ||
# Make sure that the input/output types, and dataset can be used | ||
# positionally. This avoids the worst of the breaking change from 1.x to | ||
# 2.x while still preventing possible mixups between consecutive str | ||
# parameters. | ||
input_types: Union[None, type, Sequence[type]] = None, | ||
output_type: Optional[type] = None, | ||
dataset: Optional[str] = None, | ||
*, | ||
bigquery_connection: Optional[str] = None, | ||
reuse: bool = True, | ||
name: Optional[str] = None, | ||
packages: Optional[Sequence[str]] = None, | ||
cloud_function_service_account: str, | ||
cloud_function_kms_key_name: Optional[str] = None, | ||
cloud_function_docker_repository: Optional[str] = None, | ||
max_batching_rows: Optional[int] = 1000, | ||
cloud_function_timeout: Optional[int] = 600, | ||
cloud_function_max_instances: Optional[int] = None, | ||
cloud_function_vpc_connector: Optional[str] = None, | ||
cloud_function_memory_mib: Optional[int] = 1024, | ||
cloud_function_ingress_settings: Literal[ | ||
"all", "internal-only", "internal-and-gclb" | ||
] = "internal-only", | ||
cloud_build_service_account: Optional[str] = None, | ||
): | ||
"""Deploys a remote function immediately. | ||
|
||
This function takes the same arguments as :func:`~bigframes.pandas.remote_function`. | ||
The key difference is that this function ensures the remote function | ||
is created and deployed in BigQuery at the time of this call. | ||
|
||
For detailed argument descriptions, see :func:`~bigframes.pandas.remote_function`. | ||
""" | ||
return global_session.with_default_session( | ||
bigframes.session.Session.deploy_remote_function, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we're actually missing that method right now. |
||
input_types=input_types, | ||
output_type=output_type, | ||
dataset=dataset, | ||
bigquery_connection=bigquery_connection, | ||
reuse=reuse, | ||
name=name, | ||
packages=packages, | ||
cloud_function_service_account=cloud_function_service_account, | ||
cloud_function_kms_key_name=cloud_function_kms_key_name, | ||
cloud_function_docker_repository=cloud_function_docker_repository, | ||
max_batching_rows=max_batching_rows, | ||
cloud_function_timeout=cloud_function_timeout, | ||
cloud_function_max_instances=cloud_function_max_instances, | ||
cloud_function_vpc_connector=cloud_function_vpc_connector, | ||
cloud_function_memory_mib=cloud_function_memory_mib, | ||
cloud_function_ingress_settings=cloud_function_ingress_settings, | ||
cloud_build_service_account=cloud_build_service_account, | ||
) | ||
|
||
|
||
# deploy_remote_function.__doc__ is now set directly in the SEARCH block. | ||
|
||
|
||
def udf( | ||
*, | ||
input_types: Union[None, type, Sequence[type]] = None, | ||
|
@@ -139,6 +198,37 @@ def udf( | |
udf.__doc__ = inspect.getdoc(bigframes.session.Session.udf) | ||
|
||
|
||
def deploy_udf( | ||
*, | ||
input_types: Union[None, type, Sequence[type]] = None, | ||
output_type: Optional[type] = None, | ||
dataset: str, | ||
bigquery_connection: Optional[str] = None, | ||
name: str, | ||
packages: Optional[Sequence[str]] = None, | ||
): | ||
"""Deploys a UDF immediately. | ||
|
||
This function takes the same arguments as :func:`~bigframes.pandas.udf`. | ||
The key difference is that this function ensures the UDF | ||
is created and deployed in BigQuery at the time of this call. | ||
|
||
For detailed argument descriptions, see :func:`~bigframes.pandas.udf`. | ||
""" | ||
return global_session.with_default_session( | ||
bigframes.session.Session.deploy_udf, | ||
input_types=input_types, | ||
output_type=output_type, | ||
dataset=dataset, | ||
bigquery_connection=bigquery_connection, | ||
name=name, | ||
packages=packages, | ||
) | ||
|
||
|
||
# deploy_udf.__doc__ is now set directly in the SEARCH block. | ||
|
||
|
||
@typing.overload | ||
def to_datetime( | ||
arg: Union[ | ||
|
@@ -329,6 +419,8 @@ def reset_session(): | |
clean_up_by_session_id, | ||
concat, | ||
cut, | ||
deploy_remote_function, | ||
deploy_udf, | ||
get_default_session_id, | ||
get_dummies, | ||
merge, | ||
|
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 |
---|---|---|
|
@@ -89,3 +89,165 @@ def function_without_return_annotation(myparam: int): | |
match="'output_type' was not set .* missing a return type annotation", | ||
): | ||
remote_function_decorator(function_without_return_annotation) | ||
|
||
|
||
# --- Tests for bpd.deploy_remote_function --- | ||
@mock.patch("bigframes.session.Session.deploy_remote_function") | ||
def test_bpd_deploy_remote_function_calls_session_deploy_method(mock_session_deploy_rf_method): | ||
mock_pandas_session = mocks.create_bigquery_session() | ||
# bpd.deploy_remote_function uses global_session.with_default_session, | ||
# which will call the 'deploy_remote_function' method on the provided/global session instance. | ||
# The @mock.patch above targets the class method on Session. | ||
|
||
@bpd.deploy_remote_function(session=mock_pandas_session, cloud_function_service_account="test_sa@example.com") | ||
def my_remote_func(x: int) -> int: | ||
return x * 2 | ||
|
||
mock_session_deploy_rf_method.assert_called_once() | ||
# Check some key args passed to the session's deploy_remote_function method | ||
args, kwargs = mock_session_deploy_rf_method.call_args | ||
# The first arg to Session.deploy_remote_function is the actual python function | ||
assert callable(args[0]) | ||
assert args[0].__name__ == "my_remote_func" | ||
# Other args are passed as kwargs by with_default_session | ||
assert kwargs.get("cloud_function_service_account") == "test_sa@example.com" | ||
assert kwargs.get("reuse") is True # Default reuse is True | ||
assert kwargs.get("name") is None # Default name is None | ||
|
||
# Test that the function is still callable locally | ||
assert my_remote_func(10) == 20 | ||
Comment on lines
+106
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe better to assert that the create function query was called? |
||
|
||
|
||
@mock.patch("bigframes.session.Session.deploy_remote_function") | ||
def test_bpd_deploy_remote_function_no_reuse(mock_session_deploy_rf_method): | ||
mock_pandas_session = mocks.create_bigquery_session() | ||
|
||
@bpd.deploy_remote_function(session=mock_pandas_session, cloud_function_service_account="test_sa@example.com", reuse=False) | ||
def my_remote_func_no_reuse(x: int) -> int: | ||
return x * 3 | ||
|
||
mock_session_deploy_rf_method.assert_called_once() | ||
args, kwargs = mock_session_deploy_rf_method.call_args | ||
assert kwargs.get("reuse") is False | ||
|
||
assert my_remote_func_no_reuse(5) == 15 | ||
|
||
|
||
@mock.patch("bigframes.session.Session.deploy_remote_function") | ||
def test_bpd_deploy_remote_function_with_name(mock_session_deploy_rf_method): | ||
mock_pandas_session = mocks.create_bigquery_session() | ||
|
||
@bpd.deploy_remote_function( | ||
session=mock_pandas_session, | ||
cloud_function_service_account="test_sa@example.com", | ||
name="custom_name" | ||
) | ||
def my_named_remote_func(x: int) -> int: | ||
return x * 4 | ||
|
||
mock_session_deploy_rf_method.assert_called_once() | ||
args, kwargs = mock_session_deploy_rf_method.call_args | ||
assert kwargs.get("name") == "custom_name" | ||
|
||
assert my_named_remote_func(3) == 12 | ||
|
||
|
||
# --- Tests for bpd.remote_function --- | ||
@mock.patch("bigframes.session.Session.remote_function") | ||
def test_bpd_remote_function_calls_session_method(mock_session_rf_method): | ||
mock_pandas_session = mocks.create_bigquery_session() | ||
|
||
@bpd.remote_function(session=mock_pandas_session, cloud_function_service_account="test_sa@example.com") | ||
def my_std_remote_func(x: int) -> int: | ||
return x * 5 | ||
|
||
mock_session_rf_method.assert_called_once() | ||
args, kwargs = mock_session_rf_method.call_args | ||
assert callable(args[0]) | ||
assert args[0].__name__ == "my_std_remote_func" | ||
assert kwargs.get("cloud_function_service_account") == "test_sa@example.com" | ||
# No deploy_immediately flag to check here by design | ||
|
||
assert my_std_remote_func(6) == 30 | ||
|
||
|
||
# --- Tests for bpd.deploy_udf --- | ||
@mock.patch("bigframes.session.Session.deploy_udf") | ||
def test_bpd_deploy_udf_calls_session_deploy_method(mock_session_deploy_udf_method): | ||
mock_pandas_session = mocks.create_bigquery_session( | ||
default_project="test-project", | ||
default_location="us-central1" | ||
) | ||
|
||
@bpd.deploy_udf(session=mock_pandas_session, dataset="my_dataset", name="my_udf_1") | ||
def my_udf(y: str) -> str: | ||
return f"hello {y}" | ||
|
||
mock_session_deploy_udf_method.assert_called_once() | ||
args, kwargs = mock_session_deploy_udf_method.call_args | ||
assert callable(args[0]) | ||
assert args[0].__name__ == "my_udf" | ||
assert kwargs.get("name") == "my_udf_1" | ||
assert kwargs.get("dataset") == "my_dataset" | ||
|
||
assert my_udf("world") == "hello world" | ||
|
||
|
||
@mock.patch("bigframes.session.Session.deploy_udf") | ||
def test_bpd_deploy_udf_no_name(mock_session_deploy_udf_method): | ||
mock_pandas_session = mocks.create_bigquery_session( | ||
default_project="test-project", | ||
default_location="us-central1" | ||
) | ||
|
||
@bpd.deploy_udf(session=mock_pandas_session, dataset="my_dataset") # No explicit name | ||
def my_anon_udf(val: float) -> float: | ||
return val + 1.0 | ||
|
||
mock_session_deploy_udf_method.assert_called_once() | ||
args, kwargs = mock_session_deploy_udf_method.call_args | ||
assert kwargs.get("name") is None # Name should be None, letting system generate | ||
|
||
assert my_anon_udf(1.5) == 2.5 | ||
|
||
|
||
@mock.patch("bigframes.session.Session.deploy_udf") | ||
def test_bpd_deploy_udf_with_default_dataset(mock_session_deploy_udf_method): | ||
mock_pandas_session = mocks.create_bigquery_session( | ||
default_project="test-project", | ||
default_location="us-central1" | ||
) | ||
|
||
@bpd.deploy_udf(session=mock_pandas_session, name="my_udf_2") # dataset should come from session | ||
def my_udf_default_ds(z: bytes) -> bytes: | ||
return z + b" extra" | ||
|
||
mock_session_deploy_udf_method.assert_called_once() | ||
args, kwargs = mock_session_deploy_udf_method.call_args | ||
assert kwargs.get("name") == "my_udf_2" | ||
assert kwargs.get("dataset") is None # bpd wrapper passes None if not specified by user | ||
|
||
assert my_udf_default_ds(b"data") == b"data extra" | ||
|
||
|
||
# --- Tests for bpd.udf --- | ||
@mock.patch("bigframes.session.Session.udf") | ||
def test_bpd_udf_calls_session_method(mock_session_udf_method): | ||
mock_pandas_session = mocks.create_bigquery_session( | ||
default_project="test-project", | ||
default_location="us-central1" | ||
) | ||
|
||
@bpd.udf(session=mock_pandas_session, dataset="my_dataset_std", name="my_std_udf") | ||
def my_std_udf(y: str) -> str: | ||
return f"standard hello {y}" | ||
|
||
mock_session_udf_method.assert_called_once() | ||
args, kwargs = mock_session_udf_method.call_args | ||
assert callable(args[0]) | ||
assert args[0].__name__ == "my_std_udf" | ||
assert kwargs.get("name") == "my_std_udf" | ||
assert kwargs.get("dataset") == "my_dataset_std" | ||
# No deploy_immediately flag to check here by design | ||
|
||
assert my_std_udf("dev") == "standard hello dev" |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it makes more sense just to use
*args
and**kwargs
here since we're just passing through toremote_function
?