Skip to content

[SQL][PYTHON] GroupedPythonArrowInput and CoGroupedArrowPythonRunner undercount pythonDataSent #60274

Open

Description

@jischeng

What problem are you trying to solve?

Following up on SPARK-60313 (#59570) and SPARK-60343 (#59597), GroupedPythonArrowInput (used by grouped map / aggregate / window Pandas and Arrow UDFs) and CoGroupedArrowPythonRunner (used by cogrouped map Pandas and Arrow UDFs) still undercount the pythonDataSent (data sent to Python workers) SQL metric:

  1. Per-group header and Arrow schema omitted in GroupedPythonArrowInput:
    Unlike CoGroupedArrowPythonRunner (which records val startData = dataOut.size() at the start of writeNextInputToStream), GroupedPythonArrowInput records val startData = dataOut.size() only inside if (nextBatchStart.hasNext), after writing the 4-byte group marker (dataOut.writeInt(1)) and the per-group Arrow schema message (ArrowWriterWrapper.createAndStartArrowWriter, which calls streamWriter.start()). When there are many groups, the per-group Arrow schema messages represent a large fraction of the bytes sent to the Python worker and are never counted.

  2. Per-group Arrow stream footer (streamWriter.end()) lost across buffer refills in both GroupedPythonArrowInput and CoGroupedArrowPythonRunner:
    Each group (or left/right group in CoGroupedArrowPythonRunner) creates an ArrowWriterWrapper whose streamWriter: ArrowStreamWriter is bound to the dataOut: DataOutputStream of the first batch of that group. Subsequent batches in the group are serialized directly to the current call's dataOut via BatchedPythonArrowInput.writeSizedBatch. However, when the group finishes (!nextBatchStart.hasNext), streamWriter.end() writes the 8-byte Arrow end-of-stream marker through the first call's DataOutputStream. When ReaderInputStream (SPARK-44705) or PipelinedWriterRunnable (SPARK-60343) passes a new DataOutputStream on buffer refills, dataOut.size() - startData on the current stream does not include the bytes written by streamWriter.end().

  3. End-of-data marker (dataOut.writeInt(0)) omitted in both runners:
    When all groups are exhausted, both GroupedPythonArrowInput and CoGroupedArrowPythonRunner write the 4-byte end-of-data marker 0 and return false without adding those 4 bytes to pythonDataSent.

Proposed Fix

  • In GroupedPythonArrowInput, capture val startData = dataOut.size() at the beginning of writeNextInputToStream (matching CoGroupedArrowPythonRunner and BasePythonUDFRunner).
  • In both GroupedPythonArrowInput and CoGroupedArrowPythonRunner, measure dataOut.size() - startData before calling streamWriter.end(), and add the streamWriter.bytesWritten() delta around streamWriter.end() so the 8-byte Arrow stream footer is counted regardless of whether dataOut is the original stream or a new stream from a buffer refill.
  • Update pythonDataSent when writing the final dataOut.writeInt(0) end-of-data marker.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions