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

Code before:
  std::unique_ptr<arrow::ColumnReader> column_reader;
  ASSERT_OK(file_reader->GetColumn(0, &column_reader));
  std::shared_ptr<ChunkedArray> chunked_array;
  // This is the important part in this test.
  ASSERT_OK(column_reader->NextBatch(0, &chunked_array));
  ASSERT_EQ(0, chunked_array->length());
}

TEST(TestArrowColumnReader, NextBatchEmptyInput) {
  const int num_columns = 1;
  // This is the important part in this test.
  const int num_rows = 0;
  const int num_chunks = 1;

  std::shared_ptr<Table> table;
  ASSERT_NO_FATAL_FAILURE(MakeDoubleTable(num_columns, num_rows, num_chunks, &table));

  const int64_t row_group_size = num_rows;
  std::shared_ptr<Buffer> buffer;
  ASSERT_NO_FATAL_FAILURE(WriteTableToBuffer(table, row_group_size,
                                             default_arrow_writer_properties(), &buffer));

  auto reader = ParquetFileReader::Open(std::make_shared<BufferReader>(buffer));
  std::unique_ptr<FileReader> file_reader;
  ASSERT_OK_AND_ASSIGN(
      file_reader, FileReader::Make(::arrow::default_memory_pool(), std::move(reader)));
  std::unique_ptr<arrow::ColumnReader> column_reader;
  ASSERT_OK(file_reader->GetColumn(0, &column_reader));
  std::shared_ptr<ChunkedArray> chunked_array;
  ASSERT_OK(column_reader->NextBatch(10, &chunked_array));
  ASSERT_EQ(0, chunked_array->length());
}

// direct-as-possible translation of
// pyarrow/tests/test_parquet.py::test_validate_schema_write_table
TEST(TestArrowWriterAdHoc, SchemaMismatch) {
  auto pool = ::arrow::default_memory_pool();
  auto writer_schm = ::arrow::schema({field("POS", ::arrow::uint32())});
  auto table_schm = ::arrow::schema({field("POS", ::arrow::int64())});
  using ::arrow::io::BufferOutputStream;
  ASSERT_OK_AND_ASSIGN(auto outs, BufferOutputStream::Create(1 << 10, pool));
  auto props = default_writer_properties();
  std::unique_ptr<arrow::FileWriter> writer;
  ASSERT_OK_AND_ASSIGN(writer, arrow::FileWriter::Open(*writer_schm, pool, outs, props));
  std::shared_ptr<::arrow::Array> col;
  ::arrow::Int64Builder builder;
  ASSERT_OK(builder.Append(1));
  ASSERT_OK(builder.Finish(&col));
  auto tbl = ::arrow::Table::Make(table_schm, {col});
  ASSERT_RAISES(Invalid, writer->WriteTable(*tbl, 1));
}

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

Code after:
}

TEST(TestArrowColumnReader, NextBatchEmptyInput) {
  const int num_columns = 1;
  // This is the important part in this test.
  const int num_rows = 0;
  const int num_chunks = 1;

  std::shared_ptr<Table> table;
  ASSERT_NO_FATAL_FAILURE(MakeDoubleTable(num_columns, num_rows, num_chunks, &table));

  const int64_t row_group_size = num_rows;
  std::shared_ptr<Buffer> buffer;
  ASSERT_NO_FATAL_FAILURE(WriteTableToBuffer(table, row_group_size,
                                             default_arrow_writer_properties(), &buffer));

  auto reader = ParquetFileReader::Open(std::make_shared<BufferReader>(buffer));
  ASSERT_OK_AND_ASSIGN(auto file_reader, FileReader::Make(::arrow::default_memory_pool(),
                                                          std::move(reader)));
  std::unique_ptr<arrow::ColumnReader> column_reader;
  ASSERT_OK(file_reader->GetColumn(0, &column_reader));
  std::shared_ptr<ChunkedArray> chunked_array;
  ASSERT_OK(column_reader->NextBatch(10, &chunked_array));
  ASSERT_EQ(0, chunked_array->length());
}

// direct-as-possible translation of
// pyarrow/tests/test_parquet.py::test_validate_schema_write_table
TEST(TestArrowWriterAdHoc, SchemaMismatch) {
  auto pool = ::arrow::default_memory_pool();
  auto writer_schm = ::arrow::schema({field("POS", ::arrow::uint32())});
  auto table_schm = ::arrow::schema({field("POS", ::arrow::int64())});
  using ::arrow::io::BufferOutputStream;
  ASSERT_OK_AND_ASSIGN(auto outs, BufferOutputStream::Create(1 << 10, pool));
  auto props = default_writer_properties();
  std::unique_ptr<arrow::FileWriter> writer;
  ASSERT_OK_AND_ASSIGN(writer, arrow::FileWriter::Open(*writer_schm, pool, outs, props));
  std::shared_ptr<::arrow::Array> col;
  ::arrow::Int64Builder builder;
  ASSERT_OK(builder.Append(1));
  ASSERT_OK(builder.Finish(&col));
  auto tbl = ::arrow::Table::Make(table_schm, {col});
  ASSERT_RAISES(Invalid, writer->WriteTable(*tbl, 1));
}

class TestArrowWriteDictionary : public ::testing::TestWithParam<ParquetDataPageVersion> {
 public:
  ParquetDataPageVersion GetParquetDataPageVersion() { return GetParam(); }
};

TEST_P(TestArrowWriteDictionary, Statistics) {