Repository: apache/arrow
File: cpp/src/parquet/column_writer.cc (C++)

Code before:
  Status Serialize(const ArrayType& array, ArrowWriteContext*, ParquetCType* out) {
    const ArrowCType* input = array.raw_values();
    if (array.null_count() > 0) {
      for (int64_t i = 0; i < array.length(); i++) {
        out[i] = static_cast<ParquetCType>(input[i]);
      }
    } else {
      std::copy(input, input + array.length(), out);
    }
    return Status::OK();
  }
};

template <typename ParquetType>
template <typename ArrowType>
Status TypedColumnWriterImpl<ParquetType>::WriteArrowSerialize(
    const int16_t* def_levels, const int16_t* rep_levels, int64_t num_levels,
    const ::arrow::Array& array, ArrowWriteContext* ctx, bool maybe_parent_nulls) {
  using ArrayType = typename ::arrow::TypeTraits<ArrowType>::ArrayType;
  using ParquetCType = typename ParquetType::c_type;

  ParquetCType* buffer = nullptr;
  PARQUET_THROW_NOT_OK(ctx->GetScratchData<ParquetCType>(array.length(), &buffer));

  SerializeFunctor<ParquetType, ArrowType> functor;
  // The value buffer could be empty if all values are nulls.
  if (array.null_count() != array.length()) {
    RETURN_NOT_OK(functor.Serialize(checked_cast<const ArrayType&>(array), ctx, buffer));
  }
  bool no_nulls =
      this->descr()->schema_node()->is_required() || (array.null_count() == 0);
  if (!maybe_parent_nulls && no_nulls) {
    PARQUET_CATCH_NOT_OK(WriteBatchInternal(num_levels, def_levels, rep_levels, buffer));
  } else {
    PARQUET_CATCH_NOT_OK(WriteBatchSpacedInternal(num_levels, def_levels, rep_levels,
                                                  array.null_bitmap_data(),
                                                  array.offset(), buffer));
  }
  return Status::OK();
}

#define WRITE_SERIALIZE_CASE(ArrowEnum)                                               \
  case ::arrow::Type::ArrowEnum: {                                                    \
    using ArrowType = typename ::arrow::TypeIdTraits<::arrow::Type::ArrowEnum>::Type; \
    return WriteArrowSerialize<ArrowType>(def_levels, rep_levels, num_levels, array,  \
                                          ctx, maybe_parent_nulls);                   \
  }

#define WRITE_ZERO_COPY_CASE(ArrowEnum)                                       \
  case ::arrow::Type::ArrowEnum:                                              \
    return WriteArrowZeroCopy(def_levels, rep_levels, num_levels, array, ctx, \

Review comment (suggestion):
```suggestion
  // The value buffer could be empty if all values are nulls.
  // The output buffer will then remain uninitialized, but that's ok since
  // null value slots are not written in Parquet.
```


Code after:
  Status Serialize(const ArrayType& array, ArrowWriteContext*, ParquetCType* out) {
    const ArrowCType* input = array.raw_values();
    if (array.null_count() > 0) {
      for (int64_t i = 0; i < array.length(); i++) {
        out[i] = static_cast<ParquetCType>(input[i]);
      }
    } else {
      std::copy(input, input + array.length(), out);
    }
    return Status::OK();
  }
};

template <typename ParquetType>
template <typename ArrowType>
Status TypedColumnWriterImpl<ParquetType>::WriteArrowSerialize(
    const int16_t* def_levels, const int16_t* rep_levels, int64_t num_levels,
    const ::arrow::Array& array, ArrowWriteContext* ctx, bool maybe_parent_nulls) {
  using ArrayType = typename ::arrow::TypeTraits<ArrowType>::ArrayType;
  using ParquetCType = typename ParquetType::c_type;

  ParquetCType* buffer = nullptr;
  PARQUET_THROW_NOT_OK(ctx->GetScratchData<ParquetCType>(array.length(), &buffer));

  SerializeFunctor<ParquetType, ArrowType> functor;
  // The value buffer could be empty if all values are nulls.
  // The output buffer will then remain uninitialized, but that's ok since
  // null value slots are not written in Parquet.
  if (array.null_count() != array.length()) {
    RETURN_NOT_OK(functor.Serialize(checked_cast<const ArrayType&>(array), ctx, buffer));
  }
  bool no_nulls =
      this->descr()->schema_node()->is_required() || (array.null_count() == 0);
  if (!maybe_parent_nulls && no_nulls) {
    PARQUET_CATCH_NOT_OK(WriteBatchInternal(num_levels, def_levels, rep_levels, buffer));
  } else {
    PARQUET_CATCH_NOT_OK(WriteBatchSpacedInternal(num_levels, def_levels, rep_levels,
                                                  array.null_bitmap_data(),
                                                  array.offset(), buffer));
  }
  return Status::OK();
}

#define WRITE_SERIALIZE_CASE(ArrowEnum)                                               \
  case ::arrow::Type::ArrowEnum: {                                                    \
    using ArrowType = typename ::arrow::TypeIdTraits<::arrow::Type::ArrowEnum>::Type; \
    return WriteArrowSerialize<ArrowType>(def_levels, rep_levels, num_levels, array,  \
                                          ctx, maybe_parent_nulls);                   \
  }

#define WRITE_ZERO_COPY_CASE(ArrowEnum)                                       \