|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "arrow/dataset/file_ipc.h" |
| 19 | + |
| 20 | +#include <memory> |
| 21 | +#include <utility> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include "arrow/dataset/dataset_internal.h" |
| 25 | +#include "arrow/dataset/filter.h" |
| 26 | +#include "arrow/dataset/test_util.h" |
| 27 | +#include "arrow/io/memory.h" |
| 28 | +#include "arrow/ipc/writer.h" |
| 29 | +#include "arrow/record_batch.h" |
| 30 | +#include "arrow/testing/generator.h" |
| 31 | +#include "arrow/testing/gtest_util.h" |
| 32 | +#include "arrow/testing/util.h" |
| 33 | + |
| 34 | +namespace arrow { |
| 35 | +namespace dataset { |
| 36 | + |
| 37 | +constexpr int64_t kDefaultOutputStreamSize = 1024; |
| 38 | +constexpr int64_t kBatchSize = 1UL << 12; |
| 39 | +constexpr int64_t kBatchRepetitions = 1 << 5; |
| 40 | +constexpr int64_t kNumRows = kBatchSize * kBatchRepetitions; |
| 41 | + |
| 42 | +class ArrowIpcWriterMixin : public ::testing::Test { |
| 43 | + public: |
| 44 | + std::shared_ptr<Buffer> Write(std::vector<RecordBatchReader*> readers) { |
| 45 | + EXPECT_OK_AND_ASSIGN(auto sink, io::BufferOutputStream::Create( |
| 46 | + kDefaultOutputStreamSize, default_memory_pool())); |
| 47 | + auto writer_schema = readers[0]->schema(); |
| 48 | + EXPECT_OK_AND_ASSIGN(auto writer, |
| 49 | + ipc::RecordBatchStreamWriter::Open(sink.get(), writer_schema)); |
| 50 | + |
| 51 | + for (auto reader : readers) { |
| 52 | + std::vector<std::shared_ptr<RecordBatch>> batches; |
| 53 | + ARROW_EXPECT_OK(reader->ReadAll(&batches)); |
| 54 | + for (auto batch : batches) { |
| 55 | + AssertSchemaEqual(batch->schema(), writer_schema); |
| 56 | + ARROW_EXPECT_OK(writer->WriteRecordBatch(*batch)); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + EXPECT_OK_AND_ASSIGN(auto out, sink->Finish()); |
| 61 | + return out; |
| 62 | + } |
| 63 | + |
| 64 | + std::shared_ptr<Buffer> Write(RecordBatchReader* reader) { |
| 65 | + return Write(std::vector<RecordBatchReader*>{reader}); |
| 66 | + } |
| 67 | + |
| 68 | + std::shared_ptr<Buffer> Write(const Table& table) { |
| 69 | + EXPECT_OK_AND_ASSIGN(auto sink, io::BufferOutputStream::Create( |
| 70 | + kDefaultOutputStreamSize, default_memory_pool())); |
| 71 | + EXPECT_OK_AND_ASSIGN(auto writer, |
| 72 | + ipc::RecordBatchStreamWriter::Open(sink.get(), table.schema())); |
| 73 | + ARROW_EXPECT_OK(writer->WriteTable(table)); |
| 74 | + EXPECT_OK_AND_ASSIGN(auto out, sink->Finish()); |
| 75 | + return out; |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +class IpcBufferFixtureMixin : public ArrowIpcWriterMixin { |
| 80 | + public: |
| 81 | + std::unique_ptr<FileSource> GetFileSource(RecordBatchReader* reader) { |
| 82 | + auto buffer = Write(reader); |
| 83 | + return internal::make_unique<FileSource>(std::move(buffer)); |
| 84 | + } |
| 85 | + |
| 86 | + std::unique_ptr<FileSource> GetFileSource(std::vector<RecordBatchReader*> readers) { |
| 87 | + auto buffer = Write(std::move(readers)); |
| 88 | + return internal::make_unique<FileSource>(std::move(buffer)); |
| 89 | + } |
| 90 | + |
| 91 | + std::unique_ptr<RecordBatchReader> GetRecordBatchReader() { |
| 92 | + auto batch = ConstantArrayGenerator::Zeroes(kBatchSize, schema_); |
| 93 | + int64_t i = 0; |
| 94 | + return MakeGeneratedRecordBatch( |
| 95 | + batch->schema(), [batch, i](std::shared_ptr<RecordBatch>* out) mutable { |
| 96 | + *out = i++ < kBatchRepetitions ? batch : nullptr; |
| 97 | + return Status::OK(); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + protected: |
| 102 | + std::shared_ptr<Schema> schema_ = schema({field("f64", float64())}); |
| 103 | +}; |
| 104 | + |
| 105 | +class TestIpcFileFormat : public IpcBufferFixtureMixin { |
| 106 | + protected: |
| 107 | + std::shared_ptr<ScanOptions> opts_; |
| 108 | + std::shared_ptr<ScanContext> ctx_ = std::make_shared<ScanContext>(); |
| 109 | +}; |
| 110 | + |
| 111 | +TEST_F(TestIpcFileFormat, ScanRecordBatchReader) { |
| 112 | + auto reader = GetRecordBatchReader(); |
| 113 | + auto source = GetFileSource(reader.get()); |
| 114 | + |
| 115 | + opts_ = ScanOptions::Make(reader->schema()); |
| 116 | + auto fragment = std::make_shared<IpcFragment>(*source, opts_); |
| 117 | + |
| 118 | + ASSERT_OK_AND_ASSIGN(auto scan_task_it, fragment->Scan(ctx_)); |
| 119 | + int64_t row_count = 0; |
| 120 | + |
| 121 | + for (auto maybe_task : scan_task_it) { |
| 122 | + ASSERT_OK_AND_ASSIGN(auto task, std::move(maybe_task)); |
| 123 | + ASSERT_OK_AND_ASSIGN(auto rb_it, task->Execute()); |
| 124 | + for (auto maybe_batch : rb_it) { |
| 125 | + ASSERT_OK_AND_ASSIGN(auto batch, std::move(maybe_batch)); |
| 126 | + row_count += batch->num_rows(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + ASSERT_EQ(row_count, kNumRows); |
| 131 | +} |
| 132 | + |
| 133 | +TEST_F(TestIpcFileFormat, OpenFailureWithRelevantError) { |
| 134 | + auto format = IpcFileFormat(); |
| 135 | + |
| 136 | + std::shared_ptr<Buffer> buf = std::make_shared<Buffer>(util::string_view("")); |
| 137 | + auto result = format.Inspect({buf}); |
| 138 | + EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, testing::HasSubstr("<Buffer>"), |
| 139 | + result.status()); |
| 140 | + |
| 141 | + constexpr auto file_name = "herp/derp"; |
| 142 | + ASSERT_OK_AND_ASSIGN( |
| 143 | + auto fs, fs::internal::MockFileSystem::Make(fs::kNoTime, {fs::File(file_name)})); |
| 144 | + result = format.Inspect({file_name, fs.get()}); |
| 145 | + EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, testing::HasSubstr(file_name), |
| 146 | + result.status()); |
| 147 | +} |
| 148 | + |
| 149 | +// TODO(bkietz) extend IpcFileFormat to support projection pushdown |
| 150 | +// TEST_F(TestIpcFileFormat, ScanRecordBatchReaderProjected) |
| 151 | +// TEST_F(TestParquetFileFormat, ScanRecordBatchReaderProjectedMissingCols) |
| 152 | + |
| 153 | +TEST_F(TestIpcFileFormat, Inspect) { |
| 154 | + auto reader = GetRecordBatchReader(); |
| 155 | + auto source = GetFileSource(reader.get()); |
| 156 | + auto format = IpcFileFormat(); |
| 157 | + |
| 158 | + ASSERT_OK_AND_ASSIGN(auto actual, format.Inspect(*source.get())); |
| 159 | + EXPECT_EQ(*actual, *schema_); |
| 160 | +} |
| 161 | + |
| 162 | +TEST_F(TestIpcFileFormat, IsSupported) { |
| 163 | + auto reader = GetRecordBatchReader(); |
| 164 | + auto source = GetFileSource(reader.get()); |
| 165 | + auto format = IpcFileFormat(); |
| 166 | + |
| 167 | + bool supported = false; |
| 168 | + |
| 169 | + std::shared_ptr<Buffer> buf = std::make_shared<Buffer>(util::string_view("")); |
| 170 | + ASSERT_OK_AND_ASSIGN(supported, format.IsSupported(FileSource(buf))); |
| 171 | + ASSERT_EQ(supported, false); |
| 172 | + |
| 173 | + buf = std::make_shared<Buffer>(util::string_view("corrupted")); |
| 174 | + ASSERT_OK_AND_ASSIGN(supported, format.IsSupported(FileSource(buf))); |
| 175 | + ASSERT_EQ(supported, false); |
| 176 | + |
| 177 | + ASSERT_OK_AND_ASSIGN(supported, format.IsSupported(*source)); |
| 178 | + EXPECT_EQ(supported, true); |
| 179 | +} |
| 180 | + |
| 181 | +} // namespace dataset |
| 182 | +} // namespace arrow |
0 commit comments