westonpace commented on a change in pull request #10955:
URL: https://github.com/apache/arrow/pull/10955#discussion_r718816830



##########
File path: cpp/src/arrow/dataset/dataset_writer_test.cc
##########
@@ -0,0 +1,344 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/dataset/dataset_writer.h"
+
+#include <chrono>
+#include <mutex>
+#include <vector>
+
+#include "arrow/dataset/file_ipc.h"
+#include "arrow/filesystem/mockfs.h"
+#include "arrow/filesystem/test_util.h"
+#include "arrow/io/memory.h"
+#include "arrow/ipc/reader.h"
+#include "arrow/table.h"
+#include "arrow/testing/future_util.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/util/optional.h"
+#include "gtest/gtest.h"
+
+namespace arrow {
+namespace dataset {
+
+using arrow::fs::internal::MockFileInfo;
+using arrow::fs::internal::MockFileSystem;
+
+struct ExpectedFile {
+  std::string filename;
+  uint64_t start;
+  uint64_t num_rows;
+};
+
+class DatasetWriterTestFixture : public testing::Test {
+ protected:
+  void SetUp() override {
+    fs::TimePoint mock_now = std::chrono::system_clock::now();
+    ASSERT_OK_AND_ASSIGN(std::shared_ptr<fs::FileSystem> fs,
+                         MockFileSystem::Make(mock_now, 
{::arrow::fs::Dir("testdir")}));
+    filesystem_ = std::dynamic_pointer_cast<MockFileSystem>(fs);
+    schema_ = schema({field("int64", int64())});
+    write_options_.filesystem = filesystem_;
+    write_options_.basename_template = "part-{i}.arrow";
+    write_options_.base_dir = "testdir";
+    write_options_.writer_pre_finish = [this](FileWriter* writer) {
+      pre_finish_visited_.push_back(writer->destination().path);
+      return Status::OK();
+    };
+    write_options_.writer_post_finish = [this](FileWriter* writer) {
+      post_finish_visited_.push_back(writer->destination().path);
+      return Status::OK();
+    };
+    std::shared_ptr<FileFormat> format = std::make_shared<IpcFileFormat>();
+    write_options_.file_write_options = format->DefaultWriteOptions();
+  }
+
+  std::shared_ptr<fs::GatedMockFilesystem> UseGatedFs() {
+    fs::TimePoint mock_now = std::chrono::system_clock::now();
+    auto fs = std::make_shared<fs::GatedMockFilesystem>(mock_now);
+    ARROW_EXPECT_OK(fs->CreateDir("testdir"));
+    write_options_.filesystem = fs;
+    filesystem_ = fs;
+    return fs;
+  }
+
+  std::shared_ptr<RecordBatch> MakeBatch(uint64_t start, uint64_t num_rows) {
+    Int64Builder builder;
+    for (uint64_t i = 0; i < num_rows; i++) {
+      ARROW_EXPECT_OK(builder.Append(i + start));
+    }
+    EXPECT_OK_AND_ASSIGN(std::shared_ptr<Array> arr, builder.Finish());
+    return RecordBatch::Make(schema_, static_cast<int64_t>(num_rows), 
{std::move(arr)});
+  }
+
+  std::shared_ptr<RecordBatch> MakeBatch(uint64_t num_rows) {
+    std::shared_ptr<RecordBatch> batch = MakeBatch(counter_, num_rows);
+    counter_ += num_rows;
+    return batch;
+  }
+
+  util::optional<MockFileInfo> FindFile(const std::string& filename) {
+    for (const auto& mock_file : filesystem_->AllFiles()) {
+      if (mock_file.full_path == filename) {
+        return mock_file;
+      }
+    }
+    return util::nullopt;
+  }
+
+  void AssertVisited(const std::vector<std::string>& actual_paths,
+                     const std::string& expected_path) {
+    std::vector<std::string>::const_iterator found =
+        std::find(actual_paths.begin(), actual_paths.end(), expected_path);
+    ASSERT_NE(found, actual_paths.end())
+        << "The file " << expected_path << " was not in the list of files 
visited";
+  }
+
+  std::shared_ptr<RecordBatch> ReadAsBatch(util::string_view data) {
+    std::shared_ptr<io::RandomAccessFile> in_stream =
+        std::make_shared<io::BufferReader>(data);
+    EXPECT_OK_AND_ASSIGN(std::shared_ptr<ipc::RecordBatchFileReader> reader,
+                         
ipc::RecordBatchFileReader::Open(std::move(in_stream)));
+    RecordBatchVector batches;
+    for (int i = 0; i < reader->num_record_batches(); i++) {
+      EXPECT_OK_AND_ASSIGN(std::shared_ptr<RecordBatch> next_batch,
+                           reader->ReadRecordBatch(i));
+      batches.push_back(next_batch);
+    }
+    EXPECT_OK_AND_ASSIGN(std::shared_ptr<Table> table, 
Table::FromRecordBatches(batches));
+    EXPECT_OK_AND_ASSIGN(std::shared_ptr<Table> combined_table, 
table->CombineChunks());
+    EXPECT_OK_AND_ASSIGN(std::shared_ptr<RecordBatch> batch,
+                         TableBatchReader(*combined_table).Next());
+    return batch;
+  }
+
+  void AssertFileCreated(const util::optional<MockFileInfo>& maybe_file,
+                         const std::string& expected_filename) {
+    ASSERT_TRUE(maybe_file.has_value())
+        << "The file " << expected_filename << " was not created";
+    {
+      SCOPED_TRACE("pre_finish");
+      AssertVisited(pre_finish_visited_, expected_filename);
+    }
+    {
+      SCOPED_TRACE("post_finish");
+      AssertVisited(post_finish_visited_, expected_filename);
+    }
+  }
+
+  void AssertCreatedData(const std::vector<ExpectedFile>& expected_files) {
+    counter_ = 0;
+    for (const auto& expected_file : expected_files) {
+      util::optional<MockFileInfo> written_file = 
FindFile(expected_file.filename);
+      AssertFileCreated(written_file, expected_file.filename);
+      AssertBatchesEqual(*MakeBatch(expected_file.start, 
expected_file.num_rows),
+                         *ReadAsBatch(written_file->data));
+    }
+  }
+
+  void AssertFilesCreated(const std::vector<std::string>& expected_files) {
+    for (const std::string& expected_file : expected_files) {
+      util::optional<MockFileInfo> written_file = FindFile(expected_file);
+      AssertFileCreated(written_file, expected_file);
+    }
+  }
+
+  void AssertNotFiles(const std::vector<std::string>& expected_non_files) {
+    for (const auto& expected_non_file : expected_non_files) {
+      util::optional<MockFileInfo> file = FindFile(expected_non_file);
+      ASSERT_FALSE(file.has_value());
+    }
+  }
+
+  void AssertEmptyFiles(const std::vector<std::string>& expected_empty_files) {
+    for (const auto& expected_empty_file : expected_empty_files) {
+      util::optional<MockFileInfo> file = FindFile(expected_empty_file);
+      ASSERT_TRUE(file.has_value());
+      ASSERT_EQ("", file->data);
+    }
+  }
+
+  std::shared_ptr<MockFileSystem> filesystem_;
+  std::shared_ptr<Schema> schema_;
+  std::vector<std::string> pre_finish_visited_;
+  std::vector<std::string> post_finish_visited_;
+  FileSystemDatasetWriteOptions write_options_;
+  uint64_t counter_ = 0;
+};
+
+TEST_F(DatasetWriterTestFixture, Basic) {
+  EXPECT_OK_AND_ASSIGN(auto dataset_writer, 
DatasetWriter::Make(write_options_));
+  Future<> queue_fut = dataset_writer->WriteRecordBatch(MakeBatch(100), "");
+  AssertFinished(queue_fut);
+  ASSERT_FINISHES_OK(dataset_writer->Finish());
+  AssertCreatedData({{"testdir/part-0.arrow", 0, 100}});
+}
+
+TEST_F(DatasetWriterTestFixture, MaxRowsOneWrite) {
+  write_options_.max_rows_per_file = 10;
+  EXPECT_OK_AND_ASSIGN(auto dataset_writer, 
DatasetWriter::Make(write_options_));
+  Future<> queue_fut = dataset_writer->WriteRecordBatch(MakeBatch(35), "");
+  AssertFinished(queue_fut);
+  ASSERT_FINISHES_OK(dataset_writer->Finish());
+  AssertCreatedData({{"testdir/part-0.arrow", 0, 10},
+                     {"testdir/part-1.arrow", 10, 10},
+                     {"testdir/part-2.arrow", 20, 10},
+                     {"testdir/part-3.arrow", 30, 5}});
+}
+
+TEST_F(DatasetWriterTestFixture, MaxRowsManyWrites) {
+  write_options_.max_rows_per_file = 10;
+  EXPECT_OK_AND_ASSIGN(auto dataset_writer, 
DatasetWriter::Make(write_options_));
+  ASSERT_FINISHES_OK(dataset_writer->WriteRecordBatch(MakeBatch(3), ""));
+  ASSERT_FINISHES_OK(dataset_writer->WriteRecordBatch(MakeBatch(3), ""));
+  ASSERT_FINISHES_OK(dataset_writer->WriteRecordBatch(MakeBatch(3), ""));
+  ASSERT_FINISHES_OK(dataset_writer->WriteRecordBatch(MakeBatch(3), ""));
+  ASSERT_FINISHES_OK(dataset_writer->WriteRecordBatch(MakeBatch(3), ""));
+  ASSERT_FINISHES_OK(dataset_writer->WriteRecordBatch(MakeBatch(3), ""));
+  ASSERT_FINISHES_OK(dataset_writer->Finish());
+  AssertCreatedData({{"testdir/part-0.arrow", 0, 10}, {"testdir/part-1.arrow", 
10, 8}});
+}
+
+TEST_F(DatasetWriterTestFixture, ConcurrentWritesSameFile) {
+  auto gated_fs = UseGatedFs();

Review comment:
       I added a comment.  It is basically to ensure we queue up behind a 
single long running "open" job instead of trying a second open or trying to use 
an unopened file.

##########
File path: r/R/dataset-write.R
##########
@@ -111,7 +111,7 @@ write_dataset <- function(dataset,
     dataset <- dplyr::ungroup(dataset)
   }
 
-  scanner <- Scanner$create(dataset)
+  scanner <- Scanner$create(dataset, use_async=TRUE)

Review comment:
       Fixed.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to