forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscovery_test.cc
More file actions
480 lines (390 loc) · 16.5 KB
/
Copy pathdiscovery_test.cc
File metadata and controls
480 lines (390 loc) · 16.5 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// 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/discovery.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
#include <utility>
#include "arrow/dataset/partition.h"
#include "arrow/dataset/test_util.h"
#include "arrow/filesystem/test_util.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/type_fwd.h"
#include "arrow/util/checked_cast.h"
using testing::SizeIs;
namespace arrow {
namespace dataset {
void AssertSchemasAre(std::vector<std::shared_ptr<Schema>> actual,
std::vector<std::shared_ptr<Schema>> expected) {
EXPECT_EQ(actual.size(), expected.size());
for (size_t i = 0; i < actual.size(); i++) {
EXPECT_EQ(*actual[i], *expected[i]);
}
}
class DatasetFactoryTest : public TestFileSystemDataset {
public:
void AssertInspect(std::shared_ptr<Schema> expected, InspectOptions options = {}) {
ASSERT_OK_AND_ASSIGN(auto actual, factory_->Inspect(options));
EXPECT_EQ(*actual, *expected);
}
void AssertInspectSchemas(std::vector<std::shared_ptr<Schema>> expected,
InspectOptions options = {}) {
ASSERT_OK_AND_ASSIGN(auto actual, factory_->InspectSchemas(options));
AssertSchemasAre(actual, expected);
}
protected:
std::shared_ptr<DatasetFactory> factory_;
};
class MockDatasetFactory : public DatasetFactory {
public:
explicit MockDatasetFactory(std::vector<std::shared_ptr<Schema>> schemas)
: schemas_(std::move(schemas)) {}
Result<std::vector<std::shared_ptr<Schema>>> InspectSchemas(
InspectOptions options) override {
return schemas_;
}
Result<std::shared_ptr<Dataset>> Finish(FinishOptions options) override {
return std::make_shared<InMemoryDataset>(options.schema,
std::vector<std::shared_ptr<RecordBatch>>{});
}
protected:
std::vector<std::shared_ptr<Schema>> schemas_;
};
class MockDatasetFactoryTest : public DatasetFactoryTest {
public:
void MakeFactory(std::vector<std::shared_ptr<Schema>> schemas) {
factory_ = std::make_shared<MockDatasetFactory>(schemas);
}
protected:
std::shared_ptr<Field> i32 = field("i32", int32());
std::shared_ptr<Field> i64 = field("i64", int64());
std::shared_ptr<Field> f32 = field("f32", float64());
std::shared_ptr<Field> f64 = field("f64", float64());
// Non-nullable
std::shared_ptr<Field> i32_req = field("i32", int32(), false);
// bad type with name `i32`
std::shared_ptr<Field> i32_fake = field("i32", boolean());
};
TEST_F(MockDatasetFactoryTest, UnifySchemas) {
MakeFactory({});
AssertInspect(schema({}));
MakeFactory({schema({i32}), schema({i32})});
AssertInspect(schema({i32}));
MakeFactory({schema({i32}), schema({i64})});
AssertInspect(schema({i32, i64}));
MakeFactory({schema({i32}), schema({i64})});
AssertInspect(schema({i32, i64}));
MakeFactory({schema({i32}), schema({i32_req})});
AssertInspect(schema({i32}));
MakeFactory({schema({i32, f64}), schema({i32_req, i64})});
AssertInspect(schema({i32, f64, i64}));
MakeFactory({schema({i32, f64}), schema({f64, i32_fake})});
// Unification fails when fields with the same name have clashing types.
ASSERT_RAISES(Invalid, factory_->Inspect());
// Return the individual schema for closer inspection should not fail.
AssertInspectSchemas({schema({i32, f64}), schema({f64, i32_fake})});
}
class FileSystemDatasetFactoryTest : public DatasetFactoryTest {
public:
void MakeFactory(const std::vector<fs::FileInfo>& files) {
MakeFileSystem(files);
ASSERT_OK_AND_ASSIGN(factory_, FileSystemDatasetFactory::Make(fs_, selector_, format_,
factory_options_));
}
void AssertFinishWithPaths(std::vector<std::string> paths,
std::shared_ptr<Schema> schema = nullptr,
InspectOptions options = {}) {
if (schema == nullptr) {
ASSERT_OK_AND_ASSIGN(schema, factory_->Inspect(options));
}
options_ = std::make_shared<ScanOptions>();
options_->dataset_schema = schema;
ASSERT_OK_AND_ASSIGN(auto projection, ProjectionDescr::Default(*schema));
SetProjection(options_.get(), std::move(projection));
ASSERT_OK_AND_ASSIGN(dataset_, factory_->Finish(schema));
ASSERT_OK_AND_ASSIGN(auto fragment_it, dataset_->GetFragments());
AssertFragmentsAreFromPath(std::move(fragment_it), paths);
}
protected:
fs::FileSelector selector_;
FileSystemFactoryOptions factory_options_;
std::shared_ptr<FileFormat> format_ = std::make_shared<DummyFileFormat>(schema({}));
};
TEST_F(FileSystemDatasetFactoryTest, Basic) {
MakeFactory({fs::File("a"), fs::File("b")});
AssertFinishWithPaths({"a", "b"});
MakeFactory({fs::Dir("a"), fs::Dir("a/b"), fs::File("a/b/c")});
}
TEST_F(FileSystemDatasetFactoryTest, Selector) {
selector_.base_dir = "A";
selector_.recursive = true;
MakeFactory({fs::File("0"), fs::File("A/a"), fs::File("A/A/a")});
// "0" doesn't match selector, so it has been dropped:
AssertFinishWithPaths({"A/a", "A/A/a"});
factory_options_.partition_base_dir = "A/A";
MakeFactory({fs::File("0"), fs::File("A/a"), fs::File("A/A/a")});
// partition_base_dir should not affect filtered files, only the applied partition
AssertInspect(schema({}));
AssertFinishWithPaths({"A/a", "A/A/a"});
}
TEST_F(FileSystemDatasetFactoryTest, ExplicitPartition) {
selector_.base_dir = "a=ignored/base";
auto part_field = field("a", int32());
factory_options_.partitioning =
std::make_shared<HivePartitioning>(schema({part_field}));
auto a_1 = "a=ignored/base/a=1";
MakeFactory({fs::File(a_1)});
InspectOptions options;
// Should inspect the partition's Schema even if no files are inspected.
options.fragments = 0;
AssertInspect(schema({part_field}), options);
AssertFinishWithPaths({a_1}, nullptr, options);
}
TEST_F(FileSystemDatasetFactoryTest, DiscoveredPartition) {
selector_.base_dir = "a=ignored/base";
selector_.recursive = true;
factory_options_.partitioning = HivePartitioning::MakeFactory();
auto a_1 = "a=ignored/base/a=1/file.data";
MakeFactory({fs::File(a_1)});
InspectOptions options;
auto schema_with = schema({field("a", int32())});
AssertInspect(schema_with, options);
AssertFinishWithPaths({a_1}, schema_with);
}
TEST_F(FileSystemDatasetFactoryTest, MissingDirectories) {
auto partition_path = "base_dir/a=3/b=3/dat";
auto unpartition_path = "unpartitioned/ignored=3";
MakeFileSystem({fs::File(partition_path), fs::File(unpartition_path)});
factory_options_.partition_base_dir = "base_dir";
factory_options_.partitioning = std::make_shared<HivePartitioning>(
schema({field("a", int32()), field("b", int32())}));
auto paths = std::vector<std::string>{partition_path, unpartition_path};
ASSERT_OK_AND_ASSIGN(
factory_, FileSystemDatasetFactory::Make(fs_, paths, format_, factory_options_));
InspectOptions options;
AssertInspect(schema({field("a", int32()), field("b", int32())}), options);
AssertFinishWithPaths({partition_path, unpartition_path});
}
TEST_F(FileSystemDatasetFactoryTest, OptionsIgnoredDefaultPrefixes) {
// When constructing a factory from a FileSelector,
// `selector_ignore_prefixes` governs which files are filtered out.
selector_.recursive = true;
MakeFactory({
fs::File("."),
fs::File("_"),
fs::File("_$folder$/dat"),
fs::File("_SUCCESS"),
fs::File("not_ignored_by_default"),
fs::File("not_ignored_by_default_either/dat"),
});
AssertFinishWithPaths({"not_ignored_by_default", "not_ignored_by_default_either/dat"});
}
TEST_F(FileSystemDatasetFactoryTest, OptionsIgnoredDefaultExplicitFiles) {
// When constructing a factory from an explicit list of paths,
// `selector_ignore_prefixes` is ignored.
selector_.recursive = true;
std::vector<fs::FileInfo> ignored_by_default = {
fs::File(".ignored_by_default.parquet"),
fs::File("_ignored_by_default.csv"),
fs::File("_$folder$/ignored_by_default.arrow"),
};
MakeFileSystem(ignored_by_default);
std::vector<std::string> paths;
for (const auto& info : ignored_by_default) paths.push_back(info.path());
ASSERT_OK_AND_ASSIGN(
factory_, FileSystemDatasetFactory::Make(fs_, paths, format_, factory_options_));
AssertFinishWithPaths(paths);
}
TEST_F(FileSystemDatasetFactoryTest, OptionsIgnoredCustomPrefixes) {
selector_.recursive = true;
factory_options_.selector_ignore_prefixes = {"not_ignored"};
MakeFactory({
fs::File("."),
fs::File("_"),
fs::File("_$folder$/dat"),
fs::File("_SUCCESS"),
fs::File("not_ignored_by_default"),
fs::File("not_ignored_by_default_either/dat"),
});
AssertFinishWithPaths({".", "_", "_$folder$/dat", "_SUCCESS"});
}
TEST_F(FileSystemDatasetFactoryTest, OptionsIgnoredNoPrefixes) {
// Ignore nothing
selector_.recursive = true;
factory_options_.selector_ignore_prefixes = {};
MakeFactory({
fs::File("."),
fs::File("_"),
fs::File("_$folder$/dat"),
fs::File("_SUCCESS"),
fs::File("not_ignored_by_default"),
fs::File("not_ignored_by_default_either/dat"),
});
AssertFinishWithPaths({".", "_", "_$folder$/dat", "_SUCCESS", "not_ignored_by_default",
"not_ignored_by_default_either/dat"});
}
TEST_F(FileSystemDatasetFactoryTest, OptionsIgnoredPrefixesWithBaseDirectory) {
// ARROW-9644: the selector base_dir shouldn't be filtered out even if matches
// `selector_ignore_prefixes`.
std::string dir = "_shouldnt_be_ignored/.dataset/";
selector_.base_dir = dir;
selector_.recursive = true;
MakeFactory({
fs::File(dir + "."),
fs::File(dir + "_"),
fs::File(dir + "_$folder$/dat"),
fs::File(dir + "_SUCCESS"),
fs::File(dir + "not_ignored_by_default"),
fs::File(dir + "not_ignored_by_default_either/dat"),
});
AssertFinishWithPaths(
{dir + "not_ignored_by_default", dir + "not_ignored_by_default_either/dat"});
}
TEST_F(FileSystemDatasetFactoryTest, Inspect) {
auto s = schema({field("f64", float64())});
format_ = std::make_shared<DummyFileFormat>(s);
// No files
MakeFactory({});
AssertInspect(schema({}));
MakeFactory({fs::File("test")});
AssertInspect(s);
}
TEST_F(FileSystemDatasetFactoryTest, FinishWithIncompatibleSchemaShouldFail) {
auto s = schema({field("f64", float64())});
format_ = std::make_shared<DummyFileFormat>(s);
auto broken_s = schema({field("f64", utf8())});
FinishOptions options;
options.schema = broken_s;
options.validate_fragments = true;
// No files and validation
MakeFactory({});
ASSERT_OK_AND_ASSIGN(auto dataset, factory_->Finish(options));
MakeFactory({fs::File("test")});
ASSERT_RAISES(Invalid, factory_->Finish(options));
// Disable validation
options.validate_fragments = false;
ASSERT_OK_AND_ASSIGN(dataset, factory_->Finish(options));
}
TEST_F(FileSystemDatasetFactoryTest, InspectFragmentsLimit) {
MakeFactory({fs::File("a"), fs::File("b"), fs::File("c")});
InspectOptions options;
// By default, inspect one fragment and the partitioning.
ASSERT_OK_AND_ASSIGN(auto schemas, factory_->InspectSchemas(options));
EXPECT_THAT(schemas, SizeIs(2));
for (int fragments = 0; fragments < 3; fragments++) {
options.fragments = fragments;
ASSERT_OK_AND_ASSIGN(auto schemas, factory_->InspectSchemas(options));
EXPECT_THAT(schemas, SizeIs(fragments + 1));
}
}
TEST_F(FileSystemDatasetFactoryTest, FilenameNotPartOfPartitions) {
// ARROW-8726: Ensure filename is not a partition.
// Creates a partition with 2 explicit fields. The type `int32` is
// specifically chosen such that parsing would fail given a non-integer
// string.
auto s = schema({field("first", utf8()), field("second", int32())});
factory_options_.partitioning = std::make_shared<DirectoryPartitioning>(s);
selector_.recursive = true;
// The file doesn't have a directory component for the second partition
// column. In such case, the filename should not be used.
MakeFactory({fs::File("one/file.parquet")});
auto expected = equal(field_ref("first"), literal("one"));
ASSERT_OK_AND_ASSIGN(auto dataset, factory_->Finish());
ASSERT_OK_AND_ASSIGN(auto fragment_it, dataset->GetFragments());
for (const auto& maybe_fragment : fragment_it) {
ASSERT_OK_AND_ASSIGN(auto fragment, maybe_fragment);
EXPECT_EQ(fragment->partition_expression(), expected);
}
}
TEST_F(FileSystemDatasetFactoryTest, UnparseablePartitionExpression) {
auto s = schema({field("first", int32()), field("second", int32())});
factory_options_.partitioning = std::make_shared<HivePartitioning>(s);
selector_.recursive = true;
for (auto pathlist : {"first=one/file.parquet", "second=one/file.parquet",
R"(first=1/second=0/file.parquet
first=1/second=zero/file.parquet)"}) {
MakeFactory(ParsePathList(pathlist));
ASSERT_RAISES(Invalid, factory_->Finish().status());
}
for (auto pathlist : {
R"(first=1/file.parquet
second=0/file.parquet)",
R"(first=1/second=2/file.parquet
second=0/file.parquet)",
R"(first=1/file.parquet
second=0/first=1/file.parquet)",
}) {
MakeFactory(ParsePathList(pathlist));
ASSERT_OK(factory_->Finish().status());
}
}
std::shared_ptr<DatasetFactory> DatasetFactoryFromSchemas(
std::vector<std::shared_ptr<Schema>> schemas) {
return std::make_shared<MockDatasetFactory>(schemas);
}
TEST(UnionDatasetFactoryTest, Basic) {
auto f64 = field("f64", float64());
auto i32 = field("i32", int32());
auto i32_req = field("i32", int32(), /*nullable*/ false);
auto str = field("str", utf8());
auto schema_1 = schema({f64, i32_req});
auto schema_2 = schema({f64, i32});
auto schema_3 = schema({str, i32});
auto dataset_1 = DatasetFactoryFromSchemas({schema_1, schema_2});
auto dataset_2 = DatasetFactoryFromSchemas({schema_2});
auto dataset_3 = DatasetFactoryFromSchemas({schema_3});
ASSERT_OK_AND_ASSIGN(auto factory,
UnionDatasetFactory::Make({dataset_1, dataset_2, dataset_3}));
ASSERT_OK_AND_ASSIGN(auto schemas, factory->InspectSchemas({}));
AssertSchemasAre(schemas, {schema_2, schema_2, schema_3});
auto expected_schema = schema({f64, i32, str});
ASSERT_OK_AND_ASSIGN(auto inspected, factory->Inspect());
EXPECT_EQ(*inspected, *expected_schema);
ASSERT_OK_AND_ASSIGN(auto dataset, factory->Finish());
EXPECT_EQ(*dataset->schema(), *expected_schema);
auto f64_schema = schema({f64});
ASSERT_OK_AND_ASSIGN(dataset, factory->Finish(f64_schema));
EXPECT_EQ(*dataset->schema(), *f64_schema);
}
TEST(UnionDatasetFactoryTest, ConflictingSchemas) {
auto f64 = field("f64", float64());
auto i32 = field("i32", int32());
auto i32_req = field("i32", int32(), /*nullable*/ false);
auto bad_f64 = field("f64", float32());
auto schema_1 = schema({f64, i32_req});
auto schema_2 = schema({f64, i32});
// Incompatible with schema_1
auto schema_3 = schema({bad_f64, i32});
auto dataset_factory_1 = DatasetFactoryFromSchemas({schema_1, schema_2});
auto dataset_factory_2 = DatasetFactoryFromSchemas({schema_2});
auto dataset_factory_3 = DatasetFactoryFromSchemas({schema_3});
ASSERT_OK_AND_ASSIGN(auto factory,
UnionDatasetFactory::Make(
{dataset_factory_1, dataset_factory_2, dataset_factory_3}));
// schema_3 conflicts with other, Inspect/Finish should not work
ASSERT_RAISES(Invalid, factory->Inspect());
ASSERT_RAISES(Invalid, factory->Finish());
// The user can inspect without error
ASSERT_OK_AND_ASSIGN(auto schemas, factory->InspectSchemas({}));
AssertSchemasAre(schemas, {schema_2, schema_2, schema_3});
// The user decided to ignore the conflicting `f64` field.
auto i32_schema = schema({i32});
ASSERT_OK_AND_ASSIGN(auto dataset, factory->Finish(i32_schema));
EXPECT_EQ(*dataset->schema(), *i32_schema);
}
} // namespace dataset
} // namespace arrow