forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.cc
More file actions
453 lines (384 loc) · 17.3 KB
/
Copy pathdataset.cc
File metadata and controls
453 lines (384 loc) · 17.3 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
// 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 <memory>
#include <utility>
#include "arrow/compute/exec/util.h"
#include "arrow/dataset/dataset.h"
#include "arrow/dataset/dataset_internal.h"
#include "arrow/dataset/scanner.h"
#include "arrow/table.h"
#include "arrow/util/async_generator.h"
#include "arrow/util/bit_util.h"
#include "arrow/util/byte_size.h"
#include "arrow/util/iterator.h"
#include "arrow/util/logging.h"
#include "arrow/util/thread_pool.h"
namespace arrow {
using internal::checked_pointer_cast;
namespace dataset {
const compute::Expression Fragment::kNoPartitionInformation = compute::literal(true);
Fragment::Fragment(compute::Expression partition_expression,
std::shared_ptr<Schema> physical_schema)
: partition_expression_(std::move(partition_expression)),
physical_schema_(std::move(physical_schema)) {}
Future<std::shared_ptr<InspectedFragment>> Fragment::InspectFragment(
const FragmentScanOptions* format_options, compute::ExecContext* exec_context) {
return Status::NotImplemented("Inspect fragment");
}
Future<std::shared_ptr<FragmentScanner>> Fragment::BeginScan(
const FragmentScanRequest& request, const InspectedFragment& inspected_fragment,
const FragmentScanOptions* format_options, compute::ExecContext* exec_context) {
return Status::NotImplemented("New scan method");
}
Result<std::shared_ptr<Schema>> Fragment::ReadPhysicalSchema() {
{
auto lock = physical_schema_mutex_.Lock();
if (physical_schema_ != nullptr) return physical_schema_;
}
// allow ReadPhysicalSchemaImpl to lock mutex_, if necessary
ARROW_ASSIGN_OR_RAISE(auto physical_schema, ReadPhysicalSchemaImpl());
auto lock = physical_schema_mutex_.Lock();
if (physical_schema_ == nullptr) {
physical_schema_ = std::move(physical_schema);
}
return physical_schema_;
}
Future<std::optional<int64_t>> Fragment::CountRows(compute::Expression,
const std::shared_ptr<ScanOptions>&) {
return Future<std::optional<int64_t>>::MakeFinished(std::nullopt);
}
Result<std::shared_ptr<Schema>> InMemoryFragment::ReadPhysicalSchemaImpl() {
return physical_schema_;
}
InMemoryFragment::InMemoryFragment(std::shared_ptr<Schema> schema,
RecordBatchVector record_batches,
compute::Expression partition_expression)
: Fragment(std::move(partition_expression), std::move(schema)),
record_batches_(std::move(record_batches)) {
DCHECK_NE(physical_schema_, nullptr);
}
InMemoryFragment::InMemoryFragment(RecordBatchVector record_batches,
compute::Expression partition_expression)
: Fragment(std::move(partition_expression), /*schema=*/nullptr),
record_batches_(std::move(record_batches)) {
// Order of argument evaluation is undefined, so compute physical_schema here
physical_schema_ = record_batches_.empty() ? schema({}) : record_batches_[0]->schema();
}
Result<RecordBatchGenerator> InMemoryFragment::ScanBatchesAsync(
const std::shared_ptr<ScanOptions>& options) {
struct State {
State(std::shared_ptr<InMemoryFragment> fragment, int64_t batch_size)
: fragment(std::move(fragment)),
batch_index(0),
offset(0),
batch_size(batch_size) {}
std::shared_ptr<RecordBatch> Next() {
const auto& next_parent = fragment->record_batches_[batch_index];
if (offset < next_parent->num_rows()) {
auto next = next_parent->Slice(offset, batch_size);
offset += batch_size;
return next;
}
batch_index++;
offset = 0;
return nullptr;
}
bool Finished() { return batch_index >= fragment->record_batches_.size(); }
std::shared_ptr<InMemoryFragment> fragment;
std::size_t batch_index;
int64_t offset;
int64_t batch_size;
};
struct Generator {
Generator(std::shared_ptr<InMemoryFragment> fragment, int64_t batch_size)
: state(std::make_shared<State>(std::move(fragment), batch_size)) {}
Future<std::shared_ptr<RecordBatch>> operator()() {
while (!state->Finished()) {
auto next = state->Next();
if (next) {
return Future<std::shared_ptr<RecordBatch>>::MakeFinished(std::move(next));
}
}
return AsyncGeneratorEnd<std::shared_ptr<RecordBatch>>();
}
std::shared_ptr<State> state;
};
return Generator(checked_pointer_cast<InMemoryFragment>(shared_from_this()),
options->batch_size);
}
Future<std::optional<int64_t>> InMemoryFragment::CountRows(
compute::Expression predicate, const std::shared_ptr<ScanOptions>& options) {
if (ExpressionHasFieldRefs(predicate)) {
return Future<std::optional<int64_t>>::MakeFinished(std::nullopt);
}
int64_t total = 0;
for (const auto& batch : record_batches_) {
total += batch->num_rows();
}
return Future<std::optional<int64_t>>::MakeFinished(total);
}
Future<std::shared_ptr<InspectedFragment>> InMemoryFragment::InspectFragment(
const FragmentScanOptions* format_options, compute::ExecContext* exec_context) {
return std::make_shared<InspectedFragment>(physical_schema_->field_names());
}
class InMemoryFragment::Scanner : public FragmentScanner {
public:
explicit Scanner(InMemoryFragment* fragment) : fragment_(fragment) {}
Future<std::shared_ptr<RecordBatch>> ScanBatch(int batch_number) override {
return Future<std::shared_ptr<RecordBatch>>::MakeFinished(
fragment_->record_batches_[batch_number]);
}
int64_t EstimatedDataBytes(int batch_number) override {
return arrow::util::TotalBufferSize(*fragment_->record_batches_[batch_number]);
}
int NumBatches() override {
return static_cast<int>(fragment_->record_batches_.size());
}
private:
InMemoryFragment* fragment_;
};
Future<std::shared_ptr<FragmentScanner>> InMemoryFragment::BeginScan(
const FragmentScanRequest& request, const InspectedFragment& inspected_fragment,
const FragmentScanOptions* format_options, compute::ExecContext* exec_context) {
return Future<std::shared_ptr<FragmentScanner>>::MakeFinished(
std::make_shared<InMemoryFragment::Scanner>(this));
}
Dataset::Dataset(std::shared_ptr<Schema> schema, compute::Expression partition_expression)
: schema_(std::move(schema)),
partition_expression_(std::move(partition_expression)) {}
Result<std::shared_ptr<ScannerBuilder>> Dataset::NewScan() {
return std::make_shared<ScannerBuilder>(this->shared_from_this());
}
Result<FragmentIterator> Dataset::GetFragments() {
return GetFragments(compute::literal(true));
}
Result<FragmentIterator> Dataset::GetFragments(compute::Expression predicate) {
ARROW_ASSIGN_OR_RAISE(
predicate, SimplifyWithGuarantee(std::move(predicate), partition_expression_));
return predicate.IsSatisfiable() ? GetFragmentsImpl(std::move(predicate))
: MakeEmptyIterator<std::shared_ptr<Fragment>>();
}
Result<FragmentGenerator> Dataset::GetFragmentsAsync() {
return GetFragmentsAsync(compute::literal(true));
}
Result<FragmentGenerator> Dataset::GetFragmentsAsync(compute::Expression predicate) {
ARROW_ASSIGN_OR_RAISE(
predicate, SimplifyWithGuarantee(std::move(predicate), partition_expression_));
return predicate.IsSatisfiable()
? GetFragmentsAsyncImpl(std::move(predicate),
arrow::internal::GetCpuThreadPool())
: MakeEmptyGenerator<std::shared_ptr<Fragment>>();
}
// Default impl delegating the work to `GetFragmentsImpl` and wrapping it into
// BackgroundGenerator/TransferredGenerator, which offloads potentially
// IO-intensive work to the default IO thread pool and then transfers the control
// back to the specified executor.
Result<FragmentGenerator> Dataset::GetFragmentsAsyncImpl(
compute::Expression predicate, arrow::internal::Executor* executor) {
ARROW_ASSIGN_OR_RAISE(auto iter, GetFragmentsImpl(std::move(predicate)));
ARROW_ASSIGN_OR_RAISE(
auto background_gen,
MakeBackgroundGenerator(std::move(iter), io::default_io_context().executor()));
auto transferred_gen = MakeTransferredGenerator(std::move(background_gen), executor);
return transferred_gen;
}
struct VectorRecordBatchGenerator : InMemoryDataset::RecordBatchGenerator {
explicit VectorRecordBatchGenerator(RecordBatchVector batches)
: batches_(std::move(batches)) {}
RecordBatchIterator Get() const final { return MakeVectorIterator(batches_); }
RecordBatchVector batches_;
};
InMemoryDataset::InMemoryDataset(std::shared_ptr<Schema> schema,
RecordBatchVector batches)
: Dataset(std::move(schema)),
get_batches_(new VectorRecordBatchGenerator(std::move(batches))) {}
struct TableRecordBatchGenerator : InMemoryDataset::RecordBatchGenerator {
explicit TableRecordBatchGenerator(std::shared_ptr<Table> table)
: table_(std::move(table)) {}
RecordBatchIterator Get() const final {
auto reader = std::make_shared<TableBatchReader>(*table_);
auto table = table_;
return MakeFunctionIterator([reader, table] { return reader->Next(); });
}
std::shared_ptr<Table> table_;
};
InMemoryDataset::InMemoryDataset(std::shared_ptr<Table> table)
: Dataset(table->schema()),
get_batches_(new TableRecordBatchGenerator(std::move(table))) {}
Result<std::shared_ptr<Dataset>> InMemoryDataset::ReplaceSchema(
std::shared_ptr<Schema> schema) const {
RETURN_NOT_OK(CheckProjectable(*schema_, *schema));
return std::make_shared<InMemoryDataset>(std::move(schema), get_batches_);
}
Result<FragmentIterator> InMemoryDataset::GetFragmentsImpl(compute::Expression) {
auto schema = this->schema();
auto create_fragment =
[schema](std::shared_ptr<RecordBatch> batch) -> Result<std::shared_ptr<Fragment>> {
RETURN_NOT_OK(CheckProjectable(*schema, *batch->schema()));
return std::make_shared<InMemoryFragment>(RecordBatchVector{std::move(batch)});
};
auto batches_it = get_batches_->Get();
return MakeMaybeMapIterator(std::move(create_fragment), std::move(batches_it));
}
Result<std::shared_ptr<UnionDataset>> UnionDataset::Make(std::shared_ptr<Schema> schema,
DatasetVector children) {
for (const auto& child : children) {
if (!child->schema()->Equals(*schema)) {
return Status::TypeError("child Dataset had schema ", *child->schema(),
" but the union schema was ", *schema);
}
}
return std::shared_ptr<UnionDataset>(
new UnionDataset(std::move(schema), std::move(children)));
}
Result<std::shared_ptr<Dataset>> UnionDataset::ReplaceSchema(
std::shared_ptr<Schema> schema) const {
auto children = children_;
for (auto& child : children) {
ARROW_ASSIGN_OR_RAISE(child, child->ReplaceSchema(schema));
}
return std::shared_ptr<Dataset>(
new UnionDataset(std::move(schema), std::move(children)));
}
Result<FragmentIterator> UnionDataset::GetFragmentsImpl(compute::Expression predicate) {
return GetFragmentsFromDatasets(children_, predicate);
}
namespace {
class BasicFragmentEvolution : public FragmentEvolutionStrategy {
public:
BasicFragmentEvolution(std::vector<int> ds_to_frag_map, Schema* dataset_schema)
: ds_to_frag_map(std::move(ds_to_frag_map)), dataset_schema(dataset_schema) {}
Result<compute::Expression> GetGuarantee(
const std::vector<FieldPath>& dataset_schema_selection) const override {
std::vector<compute::Expression> missing_fields;
for (const FieldPath& path : dataset_schema_selection) {
int top_level_field_idx = path[0];
if (ds_to_frag_map[top_level_field_idx] < 0) {
missing_fields.push_back(
compute::is_null(compute::field_ref(top_level_field_idx)));
}
}
if (missing_fields.empty()) {
return compute::literal(true);
}
if (missing_fields.size() == 1) {
return missing_fields[0];
}
return compute::and_(std::move(missing_fields));
}
Result<std::vector<FragmentSelectionColumn>> DevolveSelection(
const std::vector<FieldPath>& dataset_schema_selection) const override {
std::vector<FragmentSelectionColumn> desired_columns;
for (std::size_t selection_idx = 0; selection_idx < dataset_schema_selection.size();
selection_idx++) {
const FieldPath& path = dataset_schema_selection[selection_idx];
int top_level_field_idx = path[0];
int dest_top_level_idx = ds_to_frag_map[top_level_field_idx];
if (dest_top_level_idx >= 0) {
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<Field> field, path.Get(*dataset_schema));
std::vector<int> dest_path_indices(path.indices());
dest_path_indices[0] = dest_top_level_idx;
desired_columns.push_back(
FragmentSelectionColumn{FieldPath(dest_path_indices), field->type().get(),
static_cast<int>(selection_idx)});
}
}
return std::move(desired_columns);
};
Result<compute::Expression> DevolveFilter(
const compute::Expression& filter) const override {
return compute::ModifyExpression(
filter,
[&](compute::Expression expr) -> Result<compute::Expression> {
const FieldRef* ref = expr.field_ref();
if (ref) {
ARROW_ASSIGN_OR_RAISE(FieldPath path, ref->FindOne(*dataset_schema));
int top_level_idx = path[0];
std::vector<int> modified_indices(path.indices());
modified_indices[0] = ds_to_frag_map[top_level_idx];
if (modified_indices[0] < 0) {
return Status::Invalid(
"Filter cannot be applied. It refers to a missing field ",
ref->ToString(),
" in a way that cannot be satisfied even though we know that field is "
"null");
}
return compute::field_ref(FieldRef(std::move(modified_indices)));
}
return std::move(expr);
},
[](compute::Expression expr, compute::Expression* old_expr) { return expr; });
};
Result<compute::ExecBatch> EvolveBatch(
const std::shared_ptr<RecordBatch>& batch,
const std::vector<FieldPath>& dataset_selection,
const std::vector<FragmentSelectionColumn>& selection) const override {
std::vector<Datum> columns(dataset_selection.size());
DCHECK_EQ(batch->num_columns(), static_cast<int>(selection.size()));
// First go through and populate the columns we retrieved
for (int idx = 0; idx < batch->num_columns(); idx++) {
columns[selection[idx].selection_index] = batch->column(idx);
}
// Next go through and fill in the null columns
for (std::size_t idx = 0; idx < dataset_selection.size(); idx++) {
int top_level_idx = dataset_selection[idx][0];
if (ds_to_frag_map[top_level_idx] < 0) {
columns[idx] = MakeNullScalar(
dataset_schema->field(static_cast<int>(top_level_idx))->type());
}
}
return compute::ExecBatch(columns, batch->num_rows());
};
std::string ToString() const override { return "basic-fragment-evolution"; }
std::vector<int> ds_to_frag_map;
Schema* dataset_schema;
static std::unique_ptr<BasicFragmentEvolution> Make(
const std::shared_ptr<Schema>& dataset_schema,
const std::vector<std::string>& fragment_column_names) {
std::vector<int> ds_to_frag_map;
std::unordered_map<std::string, int> column_names_map;
for (size_t i = 0; i < fragment_column_names.size(); i++) {
column_names_map[fragment_column_names[i]] = static_cast<int>(i);
}
for (int idx = 0; idx < dataset_schema->num_fields(); idx++) {
const std::string& field_name = dataset_schema->field(idx)->name();
auto column_idx_itr = column_names_map.find(field_name);
if (column_idx_itr == column_names_map.end()) {
ds_to_frag_map.push_back(-1);
} else {
ds_to_frag_map.push_back(column_idx_itr->second);
}
}
return std::make_unique<BasicFragmentEvolution>(std::move(ds_to_frag_map),
dataset_schema.get());
}
};
class BasicDatasetEvolutionStrategy : public DatasetEvolutionStrategy {
std::unique_ptr<FragmentEvolutionStrategy> GetStrategy(
const Dataset& dataset, const Fragment& fragment,
const InspectedFragment& inspected_fragment) override {
return BasicFragmentEvolution::Make(dataset.schema(),
inspected_fragment.column_names);
}
std::string ToString() const override { return "basic-dataset-evolution"; }
};
} // namespace
std::unique_ptr<DatasetEvolutionStrategy> MakeBasicDatasetEvolutionStrategy() {
return std::make_unique<BasicDatasetEvolutionStrategy>();
}
} // namespace dataset
} // namespace arrow