code: def _union_array_to_array_payload(
      a: "pyarrow.UnionArray",
  ) -> "PicklableArrayPayload":
      """Serialize union arrays to PicklableArrayPayload."""
      import pyarrow as pa
  
      # Dedicated path for UnionArrays.
      # UnionArrays have a top-level bitmap buffer and type code buffer, and one or
      # more children arrays.
      # Buffer scheme: [None, typecodes, child_bitmap, child_data, ...]
      assert not _is_dense_union(a.type)
      buffers = a.buffers()
      assert len(buffers) > 1, len(buffers)
  
      bitmap_buf = buffers[0]
      assert bitmap_buf is None, bitmap_buf
  
      # Copy type code buffer, if needed.
      type_code_buf = buffers[1]
      type_code_buf = _copy_buffer_if_needed(
          type_code_buf, pa.int8(), a.offset, len(a)
      )
  
      # Get field children payload.
      # Offsets and truncations are already propagated to the field arrays, so we can
      # serialize them as-is.
      children = [
          _array_to_array_payload(a.field(i))
          for i in range(a.type.num_fields)
      ]
      return PicklableArrayPayload(
          type=a.type,
          length=len(a),
          buffers=[bitmap_buf, type_code_buf],
          null_count=a.null_count,
          offset=0,
          children=children,
      )
  
imports: from dataclasses import dataclass
  import logging
  import os
  import sys
  from typing import List, Tuple, Optional, TYPE_CHECKING
label_desc: Use image node_id 20 for calling a global function with example usage: _union_array_to_array_payload(a) and returns: PicklableArrayPayload