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

Code before:
        "required group field_id=-1 a (List) {\n"
        "  repeated group field_id=-1 array (List) {\n"
        "    repeated int32 field_id=-1 array;\n"
        "  }\n"
        "}\n";

    // Expected Arrow schema and data
    auto arrow_inner_list =
        field("array", list(field("array", ::arrow::int32(), /*nullable=*/false)),
              /*nullable=*/false);
    auto arrow_outer_list = list(arrow_inner_list);
    auto arrow_schema =
        ::arrow::schema({field("a", arrow_outer_list, /*nullable=*/false)});
    auto expected_table = TableFromJSON(arrow_schema, {R"([[[[1,2],[3,4]]]])"});

    // Verify Parquet schema
    auto root_group = file_reader->metadata()->schema()->group_node();
    ASSERT_EQ(1, root_group->field_count());
    std::stringstream nodeStr;
    PrintSchema(root_group->field(0).get(), nodeStr);
    ASSERT_EQ(kExpectedLegacyList, nodeStr.str());

    // Verify Arrow schema and data
    std::unique_ptr<FileReader> reader;
    ASSERT_OK_AND_ASSIGN(reader,
                         FileReader::Make(default_memory_pool(), std::move(file_reader)));
    std::shared_ptr<Table> table;
    ASSERT_OK(reader->ReadTable(&table));
    ASSERT_OK(table->ValidateFull());
    AssertTablesEqual(*expected_table, *table);
  };

  // Round-trip test for Parquet C++ reader and writer
  {
    // Create Parquet schema of legacy two-level encoding
    auto inner_list = GroupNode::Make("array", Repetition::REPEATED,
                                      {schema::Int32("array", Repetition::REPEATED)},
                                      LogicalType::List());
    auto outer_list =
        GroupNode::Make("a", Repetition::REQUIRED, {inner_list}, LogicalType::List());
    auto schema_node = GroupNode::Make("schema", Repetition::REQUIRED, {outer_list});

    // Create a Parquet writer to write values of nested list
    auto sink = CreateOutputStream();
    auto file_writer =
        ParquetFileWriter::Open(sink, std::dynamic_pointer_cast<GroupNode>(schema_node));
    auto row_group_writer = file_writer->AppendRowGroup();
    auto int_writer = dynamic_cast<Int32Writer*>(row_group_writer->NextColumn());
    ASSERT_TRUE(int_writer != nullptr);

    // Directly write a single row of nested list: [[1, 2],[3, 4]]

Review comment (suggestion):
```suggestion
    ASSERT_OK_AND_ASSIGN(auto reader,
                         FileReader::Make(default_memory_pool(), std::move(file_reader)));
```

Code after:
        "required group field_id=-1 a (List) {\n"
        "  repeated group field_id=-1 array (List) {\n"
        "    repeated int32 field_id=-1 array;\n"
        "  }\n"
        "}\n";

    // Expected Arrow schema and data
    auto arrow_inner_list =
        field("array", list(field("array", ::arrow::int32(), /*nullable=*/false)),
              /*nullable=*/false);
    auto arrow_outer_list = list(arrow_inner_list);
    auto arrow_schema =
        ::arrow::schema({field("a", arrow_outer_list, /*nullable=*/false)});
    auto expected_table = TableFromJSON(arrow_schema, {R"([[[[1,2],[3,4]]]])"});

    // Verify Parquet schema
    auto root_group = file_reader->metadata()->schema()->group_node();
    ASSERT_EQ(1, root_group->field_count());
    std::stringstream nodeStr;
    PrintSchema(root_group->field(0).get(), nodeStr);
    ASSERT_EQ(kExpectedLegacyList, nodeStr.str());

    // Verify Arrow schema and data
    ASSERT_OK_AND_ASSIGN(auto reader,
                         FileReader::Make(default_memory_pool(), std::move(file_reader)));
    std::shared_ptr<Table> table;
    ASSERT_OK(reader->ReadTable(&table));
    ASSERT_OK(table->ValidateFull());
    AssertTablesEqual(*expected_table, *table);
  };

  // Round-trip test for Parquet C++ reader and writer
  {
    // Create Parquet schema of legacy two-level encoding
    auto inner_list = GroupNode::Make("array", Repetition::REPEATED,
                                      {schema::Int32("array", Repetition::REPEATED)},
                                      LogicalType::List());
    auto outer_list =
        GroupNode::Make("a", Repetition::REQUIRED, {inner_list}, LogicalType::List());
    auto schema_node = GroupNode::Make("schema", Repetition::REQUIRED, {outer_list});

    // Create a Parquet writer to write values of nested list
    auto sink = CreateOutputStream();
    auto file_writer =
        ParquetFileWriter::Open(sink, std::dynamic_pointer_cast<GroupNode>(schema_node));
    auto row_group_writer = file_writer->AppendRowGroup();
    auto int_writer = dynamic_cast<Int32Writer*>(row_group_writer->NextColumn());
    ASSERT_TRUE(int_writer != nullptr);

    // Directly write a single row of nested list: [[1, 2],[3, 4]]
    constexpr int64_t kNumValues = 4;