ueshin opened a new pull request #28740:
URL: https://github.com/apache/spark/pull/28740
### What changes were proposed in this pull request?
This is a backport of #28730.
In `Dataset.collectAsArrowToPython`, since the code block for
`serveToStream` is run in the separate thread, `withAction` finishes as soon as
it starts the thread. As a result, it doesn't collect the metrics of the actual
action and Query UI shows the plan graph without metrics.
We should call `serveToStream` first, then `withAction` in it.
### Why are the changes needed?
When calling toPandas, usually Query UI shows each plan node's metric:
```py
>>> df = spark.createDataFrame([(1, 10, 'abc'), (2, 20, 'def')],
schema=['x', 'y', 'z'])
>>> df.toPandas()
x y z
0 1 10 abc
1 2 20 def
```

but if Arrow execution is enabled, it shows only plan nodes and the duration
is not correct:
```py
>>> spark.conf.set('spark.sql.execution.arrow.enabled', True)
>>> df.toPandas()
x y z
0 1 10 abc
1 2 20 def
```

### Does this PR introduce _any_ user-facing change?
Yes, the Query UI will show the plan with the correct metrics.
### How was this patch tested?
I checked it manually in my local.

----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]