-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-48756][CONNECT][PYTHON]Support for df.debug() in Connect Mode
#47153
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| .. 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. | ||
| =========== | ||
| Spark Connect - Execution Info and Debug | ||
| =========== | ||
|
|
||
|
|
||
| Execution Info | ||
| -------------- | ||
|
|
||
| The ``executionInfo`` property of the DataFrame allows users to access execution | ||
| metrics about a previously executed operation. In Spark Connect mode, the | ||
| plan metrics of the execution are always submitted as the last elements of the | ||
| response allowing users an easy way to present this information. | ||
|
|
||
| .. code-block:: python | ||
| df = spark.range(100) | ||
| df.collect() | ||
| ei = df.executionInfo | ||
| # Access the execution metrics: | ||
| metrics = ei.metrics | ||
| print(metrics.toText()) | ||
| Debugging DataFrame Data Flows | ||
| ------------------------------- | ||
| Sometimes it is useful to understand the data flow of a DataFrame operation. Whereas | ||
| metrics allow to track row counts between different operators, the execution plan | ||
| does not always resemble the semantic execution. | ||
|
|
||
| The ``debug`` method allows users to inject predefiend observation points into the | ||
| query execution. After execution the user can access the observations and access | ||
| the associated metrics. | ||
|
|
||
| By default, calling ``debug()`` will inject a single observation that counts the number | ||
| of rows flowing out of the DataFrame. | ||
|
|
||
|
|
||
| .. code-block:: python | ||
| df = spark.range(100).debug() | ||
| filtered = df.where(df.id < 10).debug() | ||
| filtered.collect() | ||
| ei = df.executionInfo | ||
| for op in ei.observations: | ||
| print(op.debugString()) | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1843,6 +1843,12 @@ def executionInfo(self) -> Optional["ExecutionInfo"]: | |||||
| message_parameters={"member": "queryExecution"}, | ||||||
| ) | ||||||
|
|
||||||
| def debug(self) -> "DataFrame": | ||||||
|
Member
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. ditto.
Suggested change
|
||||||
| raise PySparkValueError( | ||||||
| error_class="CLASSIC_OPERATION_NOT_SUPPORTED_ON_DF", | ||||||
| message_parameters={"member": "debug"}, | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| def _to_scala_map(sc: "SparkContext", jm: Dict) -> "JavaObject": | ||||||
| """ | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -50,8 +50,10 @@ | |||||
| import warnings | ||||||
| from collections.abc import Iterable | ||||||
| import functools | ||||||
| from uuid import uuid4 | ||||||
|
|
||||||
| from pyspark import _NoValue | ||||||
| from pyspark.errors.utils import call_site_stack | ||||||
| from pyspark._globals import _NoValueType | ||||||
| from pyspark.util import is_remote_only | ||||||
| from pyspark.sql.types import Row, StructType, _create_row | ||||||
|
|
@@ -84,6 +86,7 @@ | |||||
| from pyspark.sql.connect.functions import builtin as F | ||||||
| from pyspark.sql.pandas.types import from_arrow_schema, to_arrow_schema | ||||||
| from pyspark.sql.pandas.functions import _validate_pandas_udf # type: ignore[attr-defined] | ||||||
| from pyspark.sql.metrics import DataDebugOp | ||||||
|
|
||||||
|
|
||||||
| if TYPE_CHECKING: | ||||||
|
|
@@ -101,7 +104,7 @@ | |||||
| from pyspark.sql.connect.observation import Observation | ||||||
| from pyspark.sql.connect.session import SparkSession | ||||||
| from pyspark.pandas.frame import DataFrame as PandasOnSparkDataFrame | ||||||
| from pyspark.sql.metrics import ExecutionInfo | ||||||
| from pyspark.sql.metrics import ExecutionInfo, DataDebugOp | ||||||
|
|
||||||
|
|
||||||
| class DataFrame(ParentDataFrame): | ||||||
|
|
@@ -2227,8 +2230,38 @@ def rdd(self) -> "RDD[Row]": | |||||
|
|
||||||
| @property | ||||||
| def executionInfo(self) -> Optional["ExecutionInfo"]: | ||||||
| # Update the observations if needed. | ||||||
| if self._plan.observations: | ||||||
| if self._execution_info and not self._execution_info.observations: | ||||||
| self._execution_info.setObservations(self._plan.observations) | ||||||
| return self._execution_info | ||||||
|
|
||||||
| def debug(self, *other: List["DataDebugOp"]) -> "DataFrame": | ||||||
|
Member
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. If the usage is: spark.range(100).debug(DataDebugOp.max_value("id"), DataDebugOp.count_null_values("id"))instead of: spark.range(100).debug([DataDebugOp.max_value("id"), DataDebugOp.count_null_values("id")])
Suggested change
|
||||||
| # Needs to be imported here to avoid the recursive import. | ||||||
| from pyspark.sql.connect.observation import Observation | ||||||
|
|
||||||
| # Extract the stack | ||||||
| stack = call_site_stack(depth=10) | ||||||
| frames = [f"{s.filename}:{s.lineno}@{s.function}" for s in stack] | ||||||
|
|
||||||
| # Check that all elements are of type 'DataDebugOp' | ||||||
| for op in other: | ||||||
| if not isinstance(op, DataDebugOp): | ||||||
| raise PySparkTypeError( | ||||||
| error_class="UNSUPPORTED_DATADEBUGOP", | ||||||
| message_parameters={"arg_name": "other", "arg_type": type(op).__name__}, | ||||||
| ) | ||||||
|
|
||||||
| # Capture the expressions for the debug op. | ||||||
| ops: List[DataDebugOp] = [ | ||||||
| DataDebugOp.count_values(), | ||||||
| ] + list(other) | ||||||
| exprs = list(map(lambda x: x(), ops)) | ||||||
|
|
||||||
| # Create the Observation that captures all the expressions for this "debug" op. | ||||||
| obs = Observation(name=f"debug:{uuid4()}", call_site=frames, plan_id=self._plan.plan_id) | ||||||
| return self.observe(obs, *exprs) | ||||||
|
|
||||||
|
|
||||||
| class DataFrameNaFunctions(ParentDataFrameNaFunctions): | ||||||
| def __init__(self, df: ParentDataFrame): | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -6307,6 +6307,21 @@ def executionInfo(self) -> Optional["ExecutionInfo"]: | |||||||
| """ | ||||||||
| ... | ||||||||
|
|
||||||||
| def debug(self) -> "DataFrame": | ||||||||
|
Member
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. The signature should all the same:
Suggested change
|
||||||||
| """ | ||||||||
| Helper function that allows to debug the query execution with customer observations. | ||||||||
|
|
||||||||
| Essentially, this method is a wrapper around the `observe()` method, but simplifies | ||||||||
| the usage. In addition, it makes sure that the captured metrics are properly collected | ||||||||
| as part of the execution info. | ||||||||
|
|
||||||||
| .. versionadded:: 4.0.0 | ||||||||
|
Member
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.
Suggested change
Otherwise the HTML output is malformed |
||||||||
| Returns | ||||||||
| ------- | ||||||||
| DataFrame instance with the observations added | ||||||||
| """ | ||||||||
| ... | ||||||||
|
|
||||||||
|
|
||||||||
| class DataFrameNaFunctions: | ||||||||
| """Functionality for working with missing data in :class:`DataFrame`. | ||||||||
|
|
||||||||
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.
Should have
===========to match with its size - otherwise Sphinx warns and complains about itThere 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.
Done