Repository navigation
feat: support Managed Spark runtime 4.0 #233
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
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -1,53 +1,76 @@ | ||
| import json | ||
| import logging | ||
| import os | ||
| import tempfile | ||
| import uuid | ||
| from typing import TYPE_CHECKING | ||
| import re | ||
| from typing import Any, Sequence, TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from packaging.requirements import Requirement | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| DATAPROC_COMMAND_RUNNER = "org.apache.spark.sql.artifact.DataprocCommandRunner" | ||
| PIP_INSTALL_COMMAND = "PipInstallPackages" | ||
| _INSTALL_FAILURE_PATTERN = re.compile( | ||
| r"^(?:Pip|Pixi) install failed\b", re.MULTILINE | ||
| ) | ||
|
|
||
| class PyPiArtifacts: | ||
| """ | ||
| This is a helper class to serialize the PYPI package installation request with a "magic" file name | ||
| that Spark Connect server understands | ||
|
|
||
| def validate_pypi_requirements(packages: Sequence[str]) -> list["Requirement"]: | ||
| from packaging.requirements import Requirement | ||
|
|
||
| reqs = [Requirement(p) for p in packages] | ||
| if any(len(req.specifier) == 0 for req in reqs): | ||
| logger.debug("It is recommended to pin the version of the package") | ||
| return reqs | ||
|
|
||
|
|
||
| def install_pypi_packages( | ||
| client: Any, | ||
| packages: Sequence[str], | ||
| *, | ||
| validate: bool = True, | ||
| ) -> str: | ||
| """Install PyPI packages on the remote session via DataprocCommandRunner. | ||
|
|
||
| Used by both ``ManagedSparkSession.addArtifacts(..., pypi=True)`` and the | ||
| ``%dpip`` notebook magic. | ||
| """ | ||
| args = list(packages) | ||
| if validate: | ||
| args = list(dict.fromkeys(args)) | ||
| validate_pypi_requirements(args) | ||
| if not args: | ||
| return "" | ||
|
|
||
| # Imported lazily: PySpark (and with it pyarrow) is an optional extra, so | ||
| # importing this module or loading %dpip must work without it. | ||
| import pyarrow as pa | ||
| import pyspark.sql.connect.proto as pb2 | ||
|
|
||
| command = pb2.Command() | ||
| command.execute_external_command.runner = DATAPROC_COMMAND_RUNNER | ||
| command.execute_external_command.command = PIP_INSTALL_COMMAND | ||
| for index, package in enumerate(args): | ||
| command.execute_external_command.options[str(index)] = package | ||
|
|
||
| @staticmethod | ||
| def __try_parsing_package(packages: set[str]) -> list["Requirement"]: | ||
| from packaging.requirements import Requirement | ||
|
|
||
| reqs = [Requirement(p) for p in packages] | ||
| if 0 in [len(req.specifier) for req in reqs]: | ||
| logger.debug("It is recommended to pin the version of the package") | ||
| return reqs | ||
|
|
||
| def __init__(self, packages: set[str]): | ||
| self.requirements = PyPiArtifacts.__try_parsing_package(packages) | ||
|
|
||
| def write_packages_config(self, s8s_session_uuid: str) -> str: | ||
| """ | ||
| Can't use the same file-name as Spark throws exception that file already exists | ||
| Keep the filename/format in sync with server | ||
| """ | ||
| dependencies = { | ||
| "version": "0.5", | ||
| "packageType": "PYPI", | ||
| "packages": [str(req) for req in self.requirements], | ||
| } | ||
|
|
||
| file_path = os.path.join( | ||
| tempfile.gettempdir(), | ||
| s8s_session_uuid, | ||
| f"add-artifacts-1729-{uuid.uuid4()}.json", | ||
| _, properties, _ = client.execute_command(command) | ||
|
|
||
| try: | ||
| binary_data = properties["sql_command_result"].local_relation.data | ||
| except (KeyError, AttributeError, TypeError) as e: | ||
| raise RuntimeError( | ||
| "Unexpected response structure: missing binary data." | ||
| ) from e | ||
| if not binary_data: | ||
| raise RuntimeError( | ||
| "Unexpected response structure: missing binary data." | ||
| ) | ||
|
|
||
| os.makedirs(os.path.dirname(file_path), exist_ok=True) | ||
| with open(file_path, "w") as json_file: | ||
| json.dump(dependencies, json_file, indent=4) | ||
| logger.debug("Dumping dependencies request in file: " + file_path) | ||
| return file_path | ||
| try: | ||
| table = pa.ipc.RecordBatchStreamReader(binary_data).read_all() | ||
| output = "\n".join(table.column(0).to_pylist()) | ||
| except Exception as e: | ||
| raise RuntimeError(f"Error decoding Arrow data: {e}") from e | ||
|
|
||
| if _INSTALL_FAILURE_PATTERN.search(output): | ||
| raise RuntimeError(output) | ||
| return output |
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.