Spenserrrr commented on code in PR #59892:
URL: https://github.com/apache/spark/pull/59892#discussion_r4548192698
##########
python/pyspark/sql/tests/pandas/test_pandas_map.py:
##########
@@ -627,6 +627,72 @@ def tearDownClass(cls):
time.tzset()
ReusedSQLTestCase.tearDownClass()
+ def test_phase_metrics(self):
+ def rebatch(dfs):
+ frames = list(dfs)
+ time.sleep(0.01)
+ if frames:
+ combined = pd.concat(frames)
+ yield combined.iloc[:1]
+ yield combined.iloc[1:]
+
+ phase_names = (
+ "pythonDataReadTime",
+ "pythonInputPreparationTime",
+ "pythonUDFExecutionTime",
+ "pythonOutputPreparationTime",
+ "pythonDataWriteTime",
+ )
+ with self.sql_conf(
+ {
+ "spark.sql.execution.arrow.maxRecordsPerBatch": "2",
+ "spark.sql.adaptive.enabled": "false",
+ "spark.sql.execution.pythonUDF.detailedTiming.enabled": "true",
+ }
+ ):
+ for num_rows, legacy in ((5, False), (0, False), (5, True)):
Review Comment:
Dropped the legacy=True case; kept nonempty and empty input.
##########
python/pyspark/sql/tests/pandas/test_pandas_map.py:
##########
@@ -627,6 +627,72 @@ def tearDownClass(cls):
time.tzset()
ReusedSQLTestCase.tearDownClass()
+ def test_phase_metrics(self):
+ def rebatch(dfs):
+ frames = list(dfs)
+ time.sleep(0.01)
+ if frames:
+ combined = pd.concat(frames)
+ yield combined.iloc[:1]
+ yield combined.iloc[1:]
+
+ phase_names = (
+ "pythonDataReadTime",
+ "pythonInputPreparationTime",
+ "pythonUDFExecutionTime",
+ "pythonOutputPreparationTime",
+ "pythonDataWriteTime",
+ )
+ with self.sql_conf(
+ {
+ "spark.sql.execution.arrow.maxRecordsPerBatch": "2",
+ "spark.sql.adaptive.enabled": "false",
+ "spark.sql.execution.pythonUDF.detailedTiming.enabled": "true",
+ }
+ ):
+ for num_rows, legacy in ((5, False), (0, False), (5, True)):
+ with (
+ self.subTest(num_rows=num_rows, legacy=legacy),
+ self.sql_conf(
+ {
+
"spark.sql.legacy.execution.pythonUDF.pandas.conversion.enabled": str(
+ legacy
+ ).lower()
+ }
+ ),
+ ):
+ data = self.spark.createDataFrame(
+ self.sc.parallelize([(i,) for i in range(num_rows)],
1), "id long"
+ )
+ result = data.mapInPandas(rebatch, "id long")
+ self.assertEqual(result.collect(), [Row(i) for i in
range(num_rows)])
+ metrics =
result._jdf.queryExecution().executedPlan().metrics()
+
self.assertEqual(metrics.apply("pythonNumTimingReports").value(), 1)
+ self.assertEqual(
+ metrics.apply("pythonNumTimedBatches").value(),
(num_rows + 1) // 2
+ )
+ phases = [metrics.apply(name).value() for name in
phase_names]
+ self.assertTrue(all(value >= 0 for value in phases))
+ self.assertGreaterEqual(phases[2], 10)
+ self.assertLessEqual(sum(phases),
metrics.apply("pythonProcessingTime").value())
+
+ def test_phase_metrics_follow_execution_config(self):
Review Comment:
Combined the checks in one test covering Arrow and MapInPandas in both
setting-change directions.
##########
python/pyspark/sql/tests/arrow/test_arrow_python_udf.py:
##########
@@ -651,28 +651,132 @@ def run():
"pythonNumTimedBatches",
)
for name in detailed_metrics:
- self.assertFalse(metrics.contains(name), name)
+ self.assertEqual(metrics.apply(name).value(), 0, name)
self.assertTrue(metrics.contains("pythonProcessingTime"))
self.assertTrue(metrics.contains("pythonNumPipelinedTasks"))
- def test_phase_metrics_on_materialized_plan_after_config_change(self):
+ def test_phase_metrics_follow_execution_config(self):
add_one = udf(lambda value: value + 1, "long", useArrow=True)
timing_conf = "spark.sql.execution.pythonUDF.detailedTiming.enabled"
with self.sql_conf({"spark.sql.adaptive.enabled": "false"}):
- with self.sql_conf({timing_conf: "true"}):
- result = self.spark.range(2,
numPartitions=1).select(add_one("id"))
- plan = result._jdf.queryExecution().executedPlan()
- while plan.nodeName() != "ArrowEvalPython":
- self.assertEqual(plan.children().size(), 1)
- plan = plan.children().apply(0)
- metrics = plan.metrics()
- self.assertTrue(metrics.contains("pythonNumTimingReports"))
+ for enabled in (True, False):
+ with self.subTest(enabled=enabled):
+ with self.sql_conf({timing_conf: str(enabled).lower()}):
+ result = self.spark.range(2,
numPartitions=1).select(add_one("id"))
+ plan = result._jdf.queryExecution().executedPlan()
+ while plan.nodeName() != "ArrowEvalPython":
+ self.assertEqual(plan.children().size(), 1)
+ plan = plan.children().apply(0)
+ metrics = plan.metrics()
+
self.assertTrue(metrics.contains("pythonNumTimingReports"))
+ with self.sql_conf({timing_conf: str(not
enabled).lower()}):
+ self.assertEqual([row[0] for row in result.collect()],
[1, 2])
+ self.assertEqual(
+ metrics.apply("pythonNumTimingReports").value(),
int(not enabled)
+ )
- self.assertEqual(self.spark.conf.get(timing_conf), "false")
- self.assertEqual([row[0] for row in result.collect()], [1, 2])
- self.assertEqual(metrics.apply("pythonNumTimingReports").value(),
1)
- self.assertEqual(metrics.apply("pythonNumTimedBatches").value(), 1)
+ def test_phase_metrics_disabled_for_unsupported_handlers(self):
Review Comment:
Merged the unsupported and legacy-switch tests. The legacy case still
creates the physical plan before changing the flag. Removed the in-UDF
collector checks and runner-config inspection.
##########
python/pyspark/sql/tests/pandas/test_pipelined_udf.py:
##########
@@ -78,6 +78,66 @@ def check_env(x: pd.Series) -> pd.Series:
plan = plan.children().apply(0)
self.assertEqual(plan.metrics().apply("pythonNumPipelinedTasks").value(), 1)
+ def test_detailed_timing_is_disabled(self):
Review Comment:
Reduced the pipeline test to correct output, pipeline task count 1 and
timing report count 0 for both handlers.
##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/StreamingAggregationSuite.scala:
##########
@@ -862,6 +862,13 @@ class StreamingAggregationSuite extends
StateStoreMetricsTest with Assertions {
StartStream(additionalConfs = Map(SQLConf.SHUFFLE_PARTITIONS.key ->
"3")),
AddData(inputData, 3, 2, 1, 3),
CheckLastBatch((3, 2), (2, 1), (1, 1)),
+ Execute { query =>
+ val stores = query.lastExecution.executedPlan.collect {
+ case store: StateStoreSaveExec => store
+ }
+ assert(stores.nonEmpty)
+ assert(stores.forall(_.metrics("pythonNumTimingReports").value == 0))
Review Comment:
The streaming test now checks that common Python fields remain and phase
fields are absent. In-process Python intentionally keeps the shared phase
schema.
##########
python/pyspark/worker_metrics.py:
##########
@@ -121,14 +123,23 @@ def measure(self, name: str) -> _WorkerTimer:
self._duration_totals_ns.setdefault(name, 0)
return _WorkerTimer(self._duration_totals_ns, name)
+ def subtract_duration_at_report(self, *, total_metric: str, nested_metric:
str) -> None:
+ """Subtract nested_metric from total_metric at report time without
registering either."""
+ self._duration_subtractions[total_metric] = nested_metric
Review Comment:
Removed this API entirely. Conversion and full input-iteration timing are
now tracked locally in the handler, so reporting no longer needs duration
subtraction.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]