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

Code before:
  std::shared_ptr<Field> f_a, f_b, f_c, f_d, f_e, f_f, f_g, f_h, f_no_dict, f_compressed;

  a = gen.Int16(kBatchSize, -10000, 10000, kNullProbability);
  f_a = field("a", a->type());

  b = gen.Float64(kBatchSize, -1e10, 1e10, /*null_probability=*/0.0);
  f_b = field("b", b->type());

  // A column of tiny strings that will hopefully trigger dict encoding
  c = gen.String(kBatchSize, 0, 3, kNullProbability);
  f_c = field("c", c->type());

  // A column of lists
  {
    auto values = gen.Int64(kBatchSize * 10, -10000, 10000, kNullProbability);
    auto offsets = gen.Offsets(kBatchSize + 1, 0, static_cast<int32_t>(values->length()));
    ARROW_ASSIGN_OR_RAISE(d, ListArray::FromArrays(*offsets, *values));
  }
  f_d = field("d", d->type());

  // A column of a repeated constant that will hopefully trigger RLE encoding
  ARROW_ASSIGN_OR_RAISE(e, MakeArrayFromScalar(Int16Scalar(42), kBatchSize));
  f_e = field("e", e->type());

  // A column of lists of lists
  {
    auto inner_values = gen.Int64(kBatchSize * 9, -10000, 10000, kNullProbability);
    auto inner_offsets =
        gen.Offsets(kBatchSize * 3 + 1, 0, static_cast<int32_t>(inner_values->length()),
                    kNullProbability);
    ARROW_ASSIGN_OR_RAISE(auto inner_lists,
                          ListArray::FromArrays(*inner_offsets, *inner_values));
    auto offsets = gen.Offsets(
        kBatchSize + 1, 0, static_cast<int32_t>(inner_lists->length()), kNullProbability);
    ARROW_ASSIGN_OR_RAISE(f, ListArray::FromArrays(*offsets, *inner_lists));
  }
  f_f = field("f", f->type());

  // A column of nested non-nullable structs
  {
    ARROW_ASSIGN_OR_RAISE(
        auto inner_a,
        StructArray::Make({a, b}, std::vector<std::string>{"inner1_aa", "inner1_ab"}));
    ARROW_ASSIGN_OR_RAISE(
        g, StructArray::Make({inner_a, c},
                             {field("inner1_a", inner_a->type(), /*nullable=*/false),
                              field("inner1_c", c->type())}));
  }
  f_g = field("g", g->type(), /*nullable=*/false);

  // A column of nested nullable structs

Review comment (bug):
I should take a look at the dict encoding code, but the XXX might point to a bug.

Code after:
  std::string name;
  std::shared_ptr<Array> array;

  static std::function<std::string()> NameGenerator() {
    struct Gen {
      int num_col = 1;

      std::string operator()() {
        std::stringstream ss;
        ss << "col_" << num_col++;
        return std::move(ss).str();
      }
    };
    return Gen{};
  }
};

std::vector<WriteConfig> GetWriteConfigurations() {
  // clang-format off
  auto w_brotli = WriterProperties::Builder()
      .disable_dictionary("no_dict")
      ->compression("compressed", Compression::BROTLI)
      // Override current default of 1MB
      ->data_pagesize(20'000)
      // Reduce max dictionary page size so that less pages are dict-encoded.
      ->dictionary_pagesize_limit(1'000)
      // Emit various physical types for decimal columns
      ->enable_store_decimal_as_integer()
      ->build();
  // Store the Arrow schema so as to exercise more data types when reading
  auto a_default = ArrowWriterProperties::Builder{}
      .store_schema()
      ->build();
  // clang-format on

  std::vector<WriteConfig> configs;
  configs.push_back({w_brotli, a_default});
  return configs;
}

Result<std::shared_ptr<RecordBatch>> ExampleBatch1() {
  constexpr double kNullProbability = 0.2;

  random::RandomArrayGenerator gen(42);
  auto name_gen = Column::NameGenerator();

  auto field_for_array_named = [&](const std::shared_ptr<Array>& array,
                                   std::string name) {
    return field(std::move(name), array->type(), /*nullable=*/array->null_count() != 0);
  };
  auto field_for_array = [&](const std::shared_ptr<Array>& array) {