forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_ipc_test.cc
More file actions
180 lines (153 loc) · 6.89 KB
/
Copy pathfile_ipc_test.cc
File metadata and controls
180 lines (153 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// 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/file_ipc.h"
#include <memory>
#include <utility>
#include <vector>
#include "arrow/dataset/dataset_internal.h"
#include "arrow/dataset/discovery.h"
#include "arrow/dataset/file_base.h"
#include "arrow/dataset/partition.h"
#include "arrow/dataset/test_util.h"
#include "arrow/io/memory.h"
#include "arrow/ipc/reader.h"
#include "arrow/ipc/writer.h"
#include "arrow/record_batch.h"
#include "arrow/table.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/util.h"
#include "arrow/util/key_value_metadata.h"
namespace arrow {
using internal::checked_pointer_cast;
namespace dataset {
class IpcFormatHelper {
public:
using FormatType = IpcFileFormat;
static Result<std::shared_ptr<Buffer>> Write(RecordBatchReader* reader) {
ARROW_ASSIGN_OR_RAISE(auto sink, io::BufferOutputStream::Create());
ARROW_ASSIGN_OR_RAISE(auto writer, ipc::MakeFileWriter(sink, reader->schema()));
ARROW_ASSIGN_OR_RAISE(auto batches, reader->ToRecordBatches());
for (auto batch : batches) {
RETURN_NOT_OK(writer->WriteRecordBatch(*batch));
}
RETURN_NOT_OK(writer->Close());
return sink->Finish();
}
static std::shared_ptr<IpcFileFormat> MakeFormat() {
return std::make_shared<IpcFileFormat>();
}
};
class TestIpcFileFormat : public FileFormatFixtureMixin<IpcFormatHelper> {};
TEST_F(TestIpcFileFormat, WriteRecordBatchReader) { TestWrite(); }
TEST_F(TestIpcFileFormat, WriteRecordBatchReaderCustomOptions) {
auto reader = GetRecordBatchReader(schema({field("f64", float64())}));
auto source = GetFileSource(reader.get());
auto ipc_options =
checked_pointer_cast<IpcFileWriteOptions>(format_->DefaultWriteOptions());
if (util::Codec::IsAvailable(Compression::ZSTD)) {
EXPECT_OK_AND_ASSIGN(ipc_options->options->codec,
util::Codec::Create(Compression::ZSTD));
}
ipc_options->metadata = key_value_metadata({{"hello", "world"}});
auto written = WriteToBuffer(reader->schema(), ipc_options);
EXPECT_OK_AND_ASSIGN(auto ipc_reader, ipc::RecordBatchFileReader::Open(
std::make_shared<io::BufferReader>(written)));
EXPECT_EQ(ipc_reader->metadata()->sorted_pairs(),
ipc_options->metadata->sorted_pairs());
}
TEST_F(TestIpcFileFormat, InspectFailureWithRelevantError) {
TestInspectFailureWithRelevantError(StatusCode::Invalid, "IPC");
}
TEST_F(TestIpcFileFormat, Inspect) { TestInspect(); }
TEST_F(TestIpcFileFormat, IsSupported) { TestIsSupported(); }
TEST_F(TestIpcFileFormat, CountRows) { TestCountRows(); }
TEST_F(TestIpcFileFormat, FragmentEquals) { TestFragmentEquals(); }
class TestIpcFileSystemDataset : public testing::Test,
public WriteFileSystemDatasetMixin {
public:
void SetUp() override {
MakeSourceDataset();
auto ipc_format = std::make_shared<IpcFileFormat>();
format_ = ipc_format;
SetWriteOptions(ipc_format->DefaultWriteOptions());
}
};
TEST_F(TestIpcFileSystemDataset, WriteWithIdenticalPartitioningSchema) {
TestWriteWithIdenticalPartitioningSchema();
}
TEST_F(TestIpcFileSystemDataset, WriteWithUnrelatedPartitioningSchema) {
TestWriteWithUnrelatedPartitioningSchema();
}
TEST_F(TestIpcFileSystemDataset, WriteWithSupersetPartitioningSchema) {
TestWriteWithSupersetPartitioningSchema();
}
TEST_F(TestIpcFileSystemDataset, WriteWithEmptyPartitioningSchema) {
TestWriteWithEmptyPartitioningSchema();
}
TEST_F(TestIpcFileSystemDataset, WriteExceedsMaxPartitions) {
write_options_.partitioning = std::make_shared<DirectoryPartitioning>(
SchemaFromColumnNames(source_schema_, {"model"}));
// require that no batch be grouped into more than 2 written batches:
write_options_.max_partitions = 2;
auto scanner_builder = ScannerBuilder(dataset_, scan_options_);
EXPECT_OK_AND_ASSIGN(auto scanner, scanner_builder.Finish());
EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, testing::HasSubstr("This exceeds the maximum"),
FileSystemDataset::Write(write_options_, scanner));
}
class TestIpcFileFormatScan : public FileFormatScanMixin<IpcFormatHelper> {};
TEST_P(TestIpcFileFormatScan, ScanRecordBatchReader) { TestScan(); }
TEST_P(TestIpcFileFormatScan, ScanBatchSize) { TestScanBatchSize(); }
TEST_P(TestIpcFileFormatScan, ScanNoReadahead) { TestScanNoReadahead(); }
TEST_P(TestIpcFileFormatScan, ScanRecordBatchReaderProjected) { TestScanProjected(); }
TEST_P(TestIpcFileFormatScan, ScanRecordBatchReaderProjectedNested) {
TestScanProjectedNested();
}
TEST_P(TestIpcFileFormatScan, ScanRecordBatchReaderProjectedMissingCols) {
TestScanProjectedMissingCols();
}
TEST_P(TestIpcFileFormatScan, ScanRecordBatchReaderWithVirtualColumn) {
TestScanWithVirtualColumn();
}
TEST_P(TestIpcFileFormatScan, ScanRecordBatchReaderWithDuplicateColumn) {
TestScanWithDuplicateColumn();
}
TEST_P(TestIpcFileFormatScan, ScanRecordBatchReaderWithDuplicateColumnError) {
TestScanWithDuplicateColumnError();
}
TEST_P(TestIpcFileFormatScan, ScanWithPushdownNulls) { TestScanWithPushdownNulls(); }
TEST_P(TestIpcFileFormatScan, FragmentScanOptions) {
auto reader = GetRecordBatchReader(
// ARROW-12077: on Windows/mimalloc/release, nullable list column leads to crash
schema({field("list", list(float64()), false,
key_value_metadata({{"max_length", "1"}})),
field("f64", float64())}));
auto source = GetFileSource(reader.get());
SetSchema(reader->schema()->fields());
auto fragment = MakeFragment(*source);
// Set scan options that ensure reading fails
auto fragment_scan_options = std::make_shared<IpcFragmentScanOptions>();
fragment_scan_options->options = std::make_shared<ipc::IpcReadOptions>();
fragment_scan_options->options->max_recursion_depth = 0;
opts_->fragment_scan_options = fragment_scan_options;
ASSERT_OK_AND_ASSIGN(auto batch_gen, fragment->ScanBatchesAsync(opts_));
ASSERT_FINISHES_AND_RAISES(Invalid, CollectAsyncGenerator(batch_gen));
}
INSTANTIATE_TEST_SUITE_P(TestScan, TestIpcFileFormatScan,
::testing::ValuesIn(TestFormatParams::Values()),
TestFormatParams::ToTestNameString);
} // namespace dataset
} // namespace arrow