-
Notifications
You must be signed in to change notification settings - Fork 44
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
feat: support non vectorized managed function #1373
Open
jialuoo
wants to merge
17
commits into
main
Choose a base branch
from
mf3-scalar
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.
Open
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
86eb396
feat: support non vectorized managed function
jialuoo 63a5145
fix mf tests
jialuoo 538723d
fix dataframe apply
jialuoo de3ed7f
fix series apply
jialuoo 24caee3
fix the decorator in tests
jialuoo 2f0b88d
fix test remote func
jialuoo f1843ad
add more tests
jialuoo c7dba3b
remove unused import
jialuoo 63765fa
refactor rf in bff session
jialuoo 62f3baf
del udf args
jialuoo 7f0f752
fix docstring
jialuoo d9400bd
Merge branch 'main' into mf3-scalar
jialuoo 60724fa
Merge branch 'main' into mf3-scalar
jialuoo f6efbe7
Merge branch 'main' into mf3-scalar
jialuoo 9b5363d
resolve the comments
jialuoo eb758f7
fix the attribute naming
jialuoo 601b137
🦉 Updates from OwlBot post-processor
gcf-owl-bot[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 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 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 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 |
---|---|---|
|
@@ -55,39 +55,77 @@ | |
|
||
|
||
class FunctionClient: | ||
# Wait time (in seconds) for an IAM binding to take effect after creation | ||
# Wait time (in seconds) for an IAM binding to take effect after creation. | ||
_iam_wait_seconds = 120 | ||
|
||
# TODO(b/392707725): Convert all necessary parameters for cloud function | ||
# deployment into method parameters. | ||
def __init__( | ||
self, | ||
gcp_project_id, | ||
cloud_function_region, | ||
cloud_functions_client, | ||
bq_location, | ||
bq_dataset, | ||
bq_client, | ||
bq_connection_id, | ||
bq_connection_manager, | ||
cloud_function_service_account, | ||
cloud_function_kms_key_name, | ||
cloud_function_docker_repository, | ||
cloud_function_region=None, | ||
cloud_functions_client=None, | ||
cloud_function_service_account=None, | ||
cloud_function_kms_key_name=None, | ||
cloud_function_docker_repository=None, | ||
*, | ||
session: Session, | ||
): | ||
self._gcp_project_id = gcp_project_id | ||
self._cloud_function_region = cloud_function_region | ||
self._cloud_functions_client = cloud_functions_client | ||
self._bq_location = bq_location | ||
self._bq_dataset = bq_dataset | ||
self._bq_client = bq_client | ||
self._bq_connection_id = bq_connection_id | ||
self._bq_connection_manager = bq_connection_manager | ||
self._session = session | ||
|
||
# Optional attributes only for remote functions. | ||
self._cloud_function_region = cloud_function_region | ||
self._cloud_functions_client = cloud_functions_client | ||
self._cloud_function_service_account = cloud_function_service_account | ||
self._cloud_function_kms_key_name = cloud_function_kms_key_name | ||
self._cloud_function_docker_repository = cloud_function_docker_repository | ||
self._session = session | ||
|
||
def _create_bq_connection(self) -> None: | ||
if self._bq_connection_manager: | ||
self._bq_connection_manager.create_bq_connection( | ||
self._gcp_project_id, | ||
self._bq_location, | ||
self._bq_connection_id, | ||
"run.invoker", | ||
) | ||
|
||
def _ensure_dataset_exists(self) -> None: | ||
# Make sure the dataset exists, i.e. if it doesn't exist, go ahead and | ||
# create it. | ||
dataset = bigquery.Dataset( | ||
bigquery.DatasetReference.from_string( | ||
self._bq_dataset, default_project=self._gcp_project_id | ||
) | ||
) | ||
dataset.location = self._bq_location | ||
try: | ||
# This check does not require bigquery.datasets.create IAM | ||
# permission. So, if the data set already exists, then user can work | ||
# without having that permission. | ||
self._bq_client.get_dataset(dataset) | ||
except google.api_core.exceptions.NotFound: | ||
# This requires bigquery.datasets.create IAM permission. | ||
self._bq_client.create_dataset(dataset, exists_ok=True) | ||
|
||
def _create_bq_function(self, create_function_ddl: str) -> None: | ||
# TODO(swast): plumb through the original, user-facing api_name. | ||
_, query_job = bigframes.session._io.bigquery.start_query_with_client( | ||
self._session.bqclient, | ||
create_function_ddl, | ||
job_config=bigquery.QueryJobConfig(), | ||
) | ||
logger.info(f"Created bigframes function {query_job.ddl_target_routine}") | ||
|
||
def create_bq_remote_function( | ||
self, | ||
|
@@ -101,13 +139,7 @@ def create_bq_remote_function( | |
): | ||
"""Create a BigQuery remote function given the artifacts of a user defined | ||
function and the http endpoint of a corresponding cloud function.""" | ||
if self._bq_connection_manager: | ||
self._bq_connection_manager.create_bq_connection( | ||
self._gcp_project_id, | ||
self._bq_location, | ||
self._bq_connection_id, | ||
"run.invoker", | ||
) | ||
self._create_bq_connection() | ||
|
||
# Create BQ function | ||
# https://cloud.google.com/bigquery/docs/reference/standard-sql/remote-functions#create_a_remote_function_2 | ||
|
@@ -144,31 +176,78 @@ def create_bq_remote_function( | |
|
||
logger.info(f"Creating BQ remote function: {create_function_ddl}") | ||
|
||
# Make sure the dataset exists. I.e. if it doesn't exist, go ahead and | ||
# create it | ||
dataset = bigquery.Dataset( | ||
bigquery.DatasetReference.from_string( | ||
self._bq_dataset, default_project=self._gcp_project_id | ||
) | ||
) | ||
dataset.location = self._bq_location | ||
try: | ||
# This check does not require bigquery.datasets.create IAM | ||
# permission. So, if the data set already exists, then user can work | ||
# without having that permission. | ||
self._bq_client.get_dataset(dataset) | ||
except google.api_core.exceptions.NotFound: | ||
# This requires bigquery.datasets.create IAM permission | ||
self._bq_client.create_dataset(dataset, exists_ok=True) | ||
self._ensure_dataset_exists() | ||
self._create_bq_function(create_function_ddl) | ||
|
||
# TODO(swast): plumb through the original, user-facing api_name. | ||
_, query_job = bigframes.session._io.bigquery.start_query_with_client( | ||
self._session.bqclient, | ||
create_function_ddl, | ||
job_config=bigquery.QueryJobConfig(), | ||
def create_bq_managed_function( | ||
self, | ||
func, | ||
input_types, | ||
output_type, | ||
language, | ||
runtime_version, | ||
bq_function_name, | ||
packages, | ||
): | ||
"""Create a BigQuery managed function.""" | ||
self._create_bq_connection() | ||
|
||
import cloudpickle | ||
|
||
pickled = cloudpickle.dumps(func) | ||
|
||
code_block = f"""\ | ||
jialuoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import cloudpickle | ||
|
||
udf = cloudpickle.loads({pickled}) | ||
|
||
def managed_func(*args, **kwargs): | ||
return udf(*args, **kwargs) | ||
""" | ||
# Create BQ managed function. | ||
bq_function_args = [] | ||
bq_function_return_type = output_type | ||
|
||
input_args = inspect.getargs(func.__code__).args | ||
# We expect the input type annotations to be 1:1 with the input args. | ||
for name, type_ in zip(input_args, input_types): | ||
bq_function_args.append(f"{name} {type_}") | ||
|
||
managed_function_options = { | ||
"runtime_version": runtime_version, | ||
"entry_point": "managed_func", | ||
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. [nit] maybe call it "bigframes_handler" |
||
} | ||
|
||
managed_function_options_str = ", ".join( | ||
jialuoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[ | ||
f'{key}="{val}"' if isinstance(val, str) else f"{key}={val}" | ||
for key, val in managed_function_options.items() | ||
if val is not None | ||
] | ||
) | ||
if not packages: | ||
jialuoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
packages = ["cloudpickle"] | ||
if "cloudpickle" not in packages: | ||
packages += ["cloudpickle"] | ||
managed_function_options_str = ( | ||
f"{managed_function_options_str}, packages={packages}" | ||
) | ||
|
||
logger.info(f"Created remote function {query_job.ddl_target_routine}") | ||
persistent_func_id = ( | ||
f"`{self._gcp_project_id}.{self._bq_dataset}`.{bq_function_name}" | ||
) | ||
create_function_ddl = f""" | ||
CREATE OR REPLACE FUNCTION {persistent_func_id}({','.join(bq_function_args)}) | ||
RETURNS {bq_function_return_type} | ||
LANGUAGE {language} | ||
OPTIONS ({managed_function_options_str}) | ||
AS r''' | ||
{code_block} | ||
''' | ||
""" | ||
|
||
self._ensure_dataset_exists() | ||
self._create_bq_function(create_function_ddl) | ||
|
||
def get_cloud_function_fully_qualified_parent(self): | ||
"Get the fully qualilfied parent for a cloud function." | ||
|
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.
connection is not mandatory in managed function