-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-52853][SDP] Prevent imperative PySpark methods in declarative pipelines #51590
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
Closed
JiaqiWang18
wants to merge
18
commits into
apache:master
from
JiaqiWang18:SPARK-52853-prevent-py-conf-set
Closed
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
66878c1
SPARK-52853: Prevent imperative conf set in declarative pipelines
jackywang-db 52c7b3b
style
jackywang-db cfbf024
don't block sql set here
jackywang-db da51940
block catalog
jackywang-db ec7f88c
block create tempview
jackywang-db a7a957d
refactor
jackywang-db 422dba8
block udfs
jackywang-db 4ae627e
refactor tests
jackywang-db 39a1eea
fmt
jackywang-db e90dbce
register new python test file
jackywang-db dba000b
fix import path
jackywang-db f5292aa
address feedback
jackywang-db 43f607f
use error subclasses for blocked methods
jackywang-db d0e7b5d
fmt
jackywang-db 8180841
fmt
jackywang-db 18eecde
rename to block_session_mutations
jackywang-db 452402c
nit
jackywang-db 9b7f5b2
Trigger build again, failure seems unrelated
jackywang-db 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,135 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You 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. | ||
| # | ||
| from contextlib import contextmanager | ||
| from typing import Generator, NoReturn, List, Callable | ||
|
|
||
| from pyspark.errors import PySparkException | ||
| from pyspark.sql.connect.catalog import Catalog | ||
| from pyspark.sql.connect.conf import RuntimeConf | ||
| from pyspark.sql.connect.dataframe import DataFrame | ||
| from pyspark.sql.connect.udf import UDFRegistration | ||
|
|
||
| # pyspark methods that should be blocked from executing in python pipeline definition files | ||
| ERROR_CLASS = "SESSION_MUTATION_IN_DECLARATIVE_PIPELINE" | ||
| BLOCKED_METHODS: List = [ | ||
| { | ||
| "class": RuntimeConf, | ||
| "method": "set", | ||
| "error_sub_class": "RUNTIME_CONF_SET", | ||
| }, | ||
| { | ||
| "class": Catalog, | ||
| "method": "setCurrentCatalog", | ||
| "error_sub_class": "SET_CURRENT_CATALOG", | ||
| }, | ||
| { | ||
| "class": Catalog, | ||
| "method": "setCurrentDatabase", | ||
| "error_sub_class": "SET_CURRENT_DATABASE", | ||
| }, | ||
| { | ||
| "class": Catalog, | ||
| "method": "dropTempView", | ||
| "error_sub_class": "DROP_TEMP_VIEW", | ||
| }, | ||
| { | ||
| "class": Catalog, | ||
| "method": "dropGlobalTempView", | ||
| "error_sub_class": "DROP_GLOBAL_TEMP_VIEW", | ||
| }, | ||
| { | ||
| "class": DataFrame, | ||
| "method": "createTempView", | ||
| "error_sub_class": "CREATE_TEMP_VIEW", | ||
| }, | ||
| { | ||
| "class": DataFrame, | ||
| "method": "createOrReplaceTempView", | ||
| "error_sub_class": "CREATE_OR_REPLACE_TEMP_VIEW", | ||
| }, | ||
| { | ||
| "class": DataFrame, | ||
| "method": "createGlobalTempView", | ||
| "error_sub_class": "CREATE_GLOBAL_TEMP_VIEW", | ||
| }, | ||
| { | ||
| "class": DataFrame, | ||
| "method": "createOrReplaceGlobalTempView", | ||
| "error_sub_class": "CREATE_OR_REPLACE_GLOBAL_TEMP_VIEW", | ||
| }, | ||
| { | ||
| "class": UDFRegistration, | ||
| "method": "register", | ||
| "error_sub_class": "REGISTER_UDF", | ||
| }, | ||
| { | ||
| "class": UDFRegistration, | ||
| "method": "registerJavaFunction", | ||
| "error_sub_class": "REGISTER_JAVA_UDF", | ||
| }, | ||
| { | ||
| "class": UDFRegistration, | ||
| "method": "registerJavaUDAF", | ||
| "error_sub_class": "REGISTER_JAVA_UDAF", | ||
| }, | ||
| ] | ||
|
|
||
|
|
||
| def _create_blocked_method(error_method_name: str, error_sub_class: str) -> Callable: | ||
| def blocked_method(*args: object, **kwargs: object) -> NoReturn: | ||
| raise PySparkException( | ||
| errorClass=f"{ERROR_CLASS}.{error_sub_class}", | ||
| messageParameters={ | ||
| "method": error_method_name, | ||
| }, | ||
| ) | ||
|
|
||
| return blocked_method | ||
|
|
||
|
|
||
| @contextmanager | ||
| def block_session_mutations() -> Generator[None, None, None]: | ||
| """ | ||
| Context manager that blocks imperative constructs found in a pipeline python definition file | ||
| See BLOCKED_METHODS above for a list | ||
| """ | ||
| # Store original methods | ||
| original_methods = {} | ||
| for method_info in BLOCKED_METHODS: | ||
| cls = method_info["class"] | ||
| method_name = method_info["method"] | ||
| original_methods[(cls, method_name)] = getattr(cls, method_name) | ||
|
|
||
| try: | ||
| # Replace methods with blocked versions | ||
| for method_info in BLOCKED_METHODS: | ||
| cls = method_info["class"] | ||
| method_name = method_info["method"] | ||
| error_method_name = f"'{cls.__name__}.{method_name}'" | ||
| blocked_method = _create_blocked_method( | ||
| error_method_name, method_info["error_sub_class"] | ||
| ) | ||
| setattr(cls, method_name, blocked_method) | ||
|
|
||
| yield | ||
| finally: | ||
| # Restore original methods | ||
| for method_info in BLOCKED_METHODS: | ||
| cls = method_info["class"] | ||
| method_name = method_info["method"] | ||
| original_method = original_methods[(cls, method_name)] | ||
| setattr(cls, method_name, original_method) |
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
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.
Nitpick: should the
SETbe on the other side ofRUNTIME_CONFfor consistency?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.
good catch!