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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
|
// 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 "contrib/libs/apache/arrow_next/cpp/src/arrow/table.h"
#include <algorithm>
#include <cstdlib>
#include <limits>
#include <memory>
#include <sstream>
#include <utility>
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/array/array_base.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/array/array_binary.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/array/array_nested.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/array/concatenate.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/array/util.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/chunked_array.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/compute/cast.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/pretty_print.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/record_batch.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/result.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/status.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/type.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/type_fwd.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/type_traits.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/checked_cast.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/logging.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/vector.h"
namespace arrow20 {
using internal::checked_cast;
class KeyValueMetadata;
class MemoryPool;
struct ArrayData;
// ----------------------------------------------------------------------
// Table methods
/// \class SimpleTable
/// \brief A basic, non-lazy in-memory table, like SimpleRecordBatch
class SimpleTable : public Table {
public:
SimpleTable(std::shared_ptr<Schema> schema,
std::vector<std::shared_ptr<ChunkedArray>> columns, int64_t num_rows = -1)
: columns_(std::move(columns)) {
schema_ = std::move(schema);
if (num_rows < 0) {
if (columns_.size() == 0) {
num_rows_ = 0;
} else {
num_rows_ = columns_[0]->length();
}
} else {
num_rows_ = num_rows;
}
}
SimpleTable(std::shared_ptr<Schema> schema,
const std::vector<std::shared_ptr<Array>>& columns, int64_t num_rows = -1) {
schema_ = std::move(schema);
if (num_rows < 0) {
if (columns.size() == 0) {
num_rows_ = 0;
} else {
num_rows_ = columns[0]->length();
}
} else {
num_rows_ = num_rows;
}
columns_.resize(columns.size());
for (size_t i = 0; i < columns.size(); ++i) {
columns_[i] = std::make_shared<ChunkedArray>(columns[i]);
}
}
std::shared_ptr<ChunkedArray> column(int i) const override { return columns_[i]; }
const std::vector<std::shared_ptr<ChunkedArray>>& columns() const override {
return columns_;
}
std::shared_ptr<Table> Slice(int64_t offset, int64_t length) const override {
auto sliced = columns_;
int64_t num_rows = length;
for (auto& column : sliced) {
column = column->Slice(offset, length);
num_rows = column->length();
}
return Table::Make(schema_, std::move(sliced), num_rows);
}
Result<std::shared_ptr<Table>> RemoveColumn(int i) const override {
ARROW_ASSIGN_OR_RAISE(auto new_schema, schema_->RemoveField(i));
return Table::Make(std::move(new_schema), internal::DeleteVectorElement(columns_, i),
this->num_rows());
}
Result<std::shared_ptr<Table>> AddColumn(
int i, std::shared_ptr<Field> field_arg,
std::shared_ptr<ChunkedArray> col) const override {
DCHECK(col != nullptr);
if (col->length() != num_rows_) {
return Status::Invalid(
"Added column's length must match table's length. Expected length ", num_rows_,
" but got length ", col->length());
}
if (!field_arg->type()->Equals(col->type())) {
return Status::Invalid("Field type did not match data type");
}
ARROW_ASSIGN_OR_RAISE(auto new_schema, schema_->AddField(i, field_arg));
return Table::Make(std::move(new_schema),
internal::AddVectorElement(columns_, i, std::move(col)));
}
Result<std::shared_ptr<Table>> SetColumn(
int i, std::shared_ptr<Field> field_arg,
std::shared_ptr<ChunkedArray> col) const override {
DCHECK(col != nullptr);
if (col->length() != num_rows_) {
return Status::Invalid(
"Added column's length must match table's length. Expected length ", num_rows_,
" but got length ", col->length());
}
if (!field_arg->type()->Equals(col->type())) {
return Status::Invalid("Field type did not match data type");
}
ARROW_ASSIGN_OR_RAISE(auto new_schema, schema_->SetField(i, field_arg));
return Table::Make(std::move(new_schema),
internal::ReplaceVectorElement(columns_, i, std::move(col)));
}
std::shared_ptr<Table> ReplaceSchemaMetadata(
const std::shared_ptr<const KeyValueMetadata>& metadata) const override {
auto new_schema = schema_->WithMetadata(metadata);
return Table::Make(std::move(new_schema), columns_);
}
Result<std::shared_ptr<Table>> Flatten(MemoryPool* pool) const override {
std::vector<std::shared_ptr<Field>> flattened_fields;
std::vector<std::shared_ptr<ChunkedArray>> flattened_columns;
for (int i = 0; i < num_columns(); ++i) {
std::vector<std::shared_ptr<Field>> new_fields = field(i)->Flatten();
ARROW_ASSIGN_OR_RAISE(auto new_columns, column(i)->Flatten(pool));
DCHECK_EQ(new_columns.size(), new_fields.size());
for (size_t j = 0; j < new_columns.size(); ++j) {
flattened_fields.push_back(new_fields[j]);
flattened_columns.push_back(new_columns[j]);
}
}
auto flattened_schema =
std::make_shared<Schema>(std::move(flattened_fields), schema_->metadata());
return Table::Make(std::move(flattened_schema), std::move(flattened_columns));
}
Status Validate() const override {
RETURN_NOT_OK(ValidateMeta());
for (int i = 0; i < num_columns(); ++i) {
const ChunkedArray* col = columns_[i].get();
Status st = col->Validate();
if (!st.ok()) {
std::stringstream ss;
ss << "Column " << i << ": " << st.message();
return st.WithMessage(ss.str());
}
}
return Status::OK();
}
Status ValidateFull() const override {
RETURN_NOT_OK(ValidateMeta());
for (int i = 0; i < num_columns(); ++i) {
const ChunkedArray* col = columns_[i].get();
Status st = col->ValidateFull();
if (!st.ok()) {
std::stringstream ss;
ss << "Column " << i << ": " << st.message();
return st.WithMessage(ss.str());
}
}
return Status::OK();
}
protected:
Status ValidateMeta() const {
// Make sure columns and schema are consistent
if (static_cast<int>(columns_.size()) != schema_->num_fields()) {
return Status::Invalid("Number of columns did not match schema");
}
for (int i = 0; i < num_columns(); ++i) {
const ChunkedArray* col = columns_[i].get();
if (col == nullptr) {
return Status::Invalid("Column ", i, " was null");
}
if (!col->type()->Equals(*schema_->field(i)->type())) {
return Status::Invalid("Column data for field ", i, " with type ",
col->type()->ToString(), " is inconsistent with schema ",
schema_->field(i)->type()->ToString());
}
}
// Make sure columns are all the same length, and validate them
for (int i = 0; i < num_columns(); ++i) {
const ChunkedArray* col = columns_[i].get();
if (col->length() != num_rows_) {
return Status::Invalid("Column ", i, " named ", field(i)->name(),
" expected length ", num_rows_, " but got length ",
col->length());
}
Status st = col->Validate();
if (!st.ok()) {
std::stringstream ss;
ss << "Column " << i << ": " << st.message();
return st.WithMessage(ss.str());
}
}
return Status::OK();
}
private:
std::vector<std::shared_ptr<ChunkedArray>> columns_;
};
Table::Table() : num_rows_(0) {}
std::vector<std::shared_ptr<Field>> Table::fields() const {
std::vector<std::shared_ptr<Field>> result;
for (int i = 0; i < this->num_columns(); ++i) {
result.emplace_back(this->field(i));
}
return result;
}
std::shared_ptr<Table> Table::Make(std::shared_ptr<Schema> schema,
std::vector<std::shared_ptr<ChunkedArray>> columns,
int64_t num_rows) {
return std::make_shared<SimpleTable>(std::move(schema), std::move(columns), num_rows);
}
std::shared_ptr<Table> Table::Make(std::shared_ptr<Schema> schema,
const std::vector<std::shared_ptr<Array>>& arrays,
int64_t num_rows) {
return std::make_shared<SimpleTable>(std::move(schema), arrays, num_rows);
}
Result<std::shared_ptr<Table>> Table::MakeEmpty(std::shared_ptr<Schema> schema,
MemoryPool* memory_pool) {
ChunkedArrayVector empty_table(schema->num_fields());
for (int i = 0; i < schema->num_fields(); i++) {
ARROW_ASSIGN_OR_RAISE(empty_table[i],
ChunkedArray::MakeEmpty(schema->field(i)->type(), memory_pool));
}
return Table::Make(schema, empty_table, 0);
}
Result<std::shared_ptr<Table>> Table::FromRecordBatchReader(RecordBatchReader* reader) {
return reader->ToTable();
}
Result<std::shared_ptr<Table>> Table::FromRecordBatches(
std::shared_ptr<Schema> schema,
const std::vector<std::shared_ptr<RecordBatch>>& batches) {
const int nbatches = static_cast<int>(batches.size());
const int ncolumns = static_cast<int>(schema->num_fields());
int64_t num_rows = 0;
for (int i = 0; i < nbatches; ++i) {
if (!batches[i]->schema()->Equals(*schema, false)) {
return Status::Invalid("Schema at index ", static_cast<int>(i),
" was different: \n", schema->ToString(), "\nvs\n",
batches[i]->schema()->ToString());
}
num_rows += batches[i]->num_rows();
}
std::vector<std::shared_ptr<ChunkedArray>> columns(ncolumns);
std::vector<std::shared_ptr<Array>> column_arrays(nbatches);
for (int i = 0; i < ncolumns; ++i) {
for (int j = 0; j < nbatches; ++j) {
column_arrays[j] = batches[j]->column(i);
}
columns[i] = std::make_shared<ChunkedArray>(column_arrays, schema->field(i)->type());
}
return Table::Make(std::move(schema), std::move(columns), num_rows);
}
Result<std::shared_ptr<Table>> Table::FromRecordBatches(
const std::vector<std::shared_ptr<RecordBatch>>& batches) {
if (batches.size() == 0) {
return Status::Invalid("Must pass at least one record batch or an explicit Schema");
}
return FromRecordBatches(batches[0]->schema(), batches);
}
Result<std::shared_ptr<Table>> Table::FromChunkedStructArray(
const std::shared_ptr<ChunkedArray>& array) {
auto type = array->type();
if (type->id() != Type::STRUCT) {
return Status::Invalid("Expected a chunked struct array, got ", *type);
}
int num_columns = type->num_fields();
int num_chunks = array->num_chunks();
const auto& struct_chunks = array->chunks();
std::vector<std::shared_ptr<ChunkedArray>> columns(num_columns);
for (int i = 0; i < num_columns; ++i) {
ArrayVector chunks(num_chunks);
std::transform(struct_chunks.begin(), struct_chunks.end(), chunks.begin(),
[i](const std::shared_ptr<Array>& struct_chunk) {
return static_cast<const StructArray&>(*struct_chunk).field(i);
});
columns[i] =
std::make_shared<ChunkedArray>(std::move(chunks), type->field(i)->type());
}
return Table::Make(::arrow20::schema(type->fields()), std::move(columns),
array->length());
}
std::vector<std::string> Table::ColumnNames() const {
std::vector<std::string> names(num_columns());
for (int i = 0; i < num_columns(); ++i) {
names[i] = field(i)->name();
}
return names;
}
Result<std::shared_ptr<Table>> Table::RenameColumns(
const std::vector<std::string>& names) const {
if (names.size() != static_cast<size_t>(num_columns())) {
return Status::Invalid("tried to rename a table of ", num_columns(),
" columns but only ", names.size(), " names were provided");
}
std::vector<std::shared_ptr<ChunkedArray>> columns(num_columns());
std::vector<std::shared_ptr<Field>> fields(num_columns());
for (int i = 0; i < num_columns(); ++i) {
columns[i] = column(i);
fields[i] = field(i)->WithName(names[i]);
}
return Table::Make(::arrow20::schema(std::move(fields)), std::move(columns), num_rows());
}
Result<std::shared_ptr<Table>> Table::SelectColumns(
const std::vector<int>& indices) const {
int n = static_cast<int>(indices.size());
std::vector<std::shared_ptr<ChunkedArray>> columns(n);
std::vector<std::shared_ptr<Field>> fields(n);
for (int i = 0; i < n; i++) {
int pos = indices[i];
if (pos < 0 || pos > num_columns() - 1) {
return Status::Invalid("Invalid column index ", pos, " to select columns.");
}
columns[i] = column(pos);
fields[i] = field(pos);
}
auto new_schema =
std::make_shared<arrow20::Schema>(std::move(fields), schema()->metadata());
return Table::Make(std::move(new_schema), std::move(columns), num_rows());
}
std::string Table::ToString() const {
std::stringstream ss;
ARROW_CHECK_OK(PrettyPrint(*this, 0, &ss));
return ss.str();
}
Result<std::shared_ptr<Table>> ConcatenateTables(
const std::vector<std::shared_ptr<Table>>& tables,
const ConcatenateTablesOptions options, MemoryPool* memory_pool) {
if (tables.size() == 0) {
return Status::Invalid("Must pass at least one table");
}
std::vector<std::shared_ptr<Table>> promoted_tables;
const std::vector<std::shared_ptr<Table>>* tables_to_concat = &tables;
if (options.unify_schemas) {
std::vector<std::shared_ptr<Schema>> schemas;
schemas.reserve(tables.size());
for (const auto& t : tables) {
schemas.push_back(t->schema());
}
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<Schema> unified_schema,
UnifySchemas(schemas, options.field_merge_options));
promoted_tables.reserve(tables.size());
for (const auto& t : tables) {
promoted_tables.emplace_back();
ARROW_ASSIGN_OR_RAISE(promoted_tables.back(),
PromoteTableToSchema(t, unified_schema, memory_pool));
}
tables_to_concat = &promoted_tables;
} else {
auto first_schema = tables[0]->schema();
for (size_t i = 1; i < tables.size(); ++i) {
if (!tables[i]->schema()->Equals(*first_schema, false)) {
return Status::Invalid("Schema at index ", i, " was different: \n",
first_schema->ToString(), "\nvs\n",
tables[i]->schema()->ToString());
}
}
}
std::shared_ptr<Schema> schema = tables_to_concat->front()->schema();
const int ncolumns = schema->num_fields();
std::vector<std::shared_ptr<ChunkedArray>> columns(ncolumns);
for (int i = 0; i < ncolumns; ++i) {
std::vector<std::shared_ptr<Array>> column_arrays;
for (const auto& table : *tables_to_concat) {
const std::vector<std::shared_ptr<Array>>& chunks = table->column(i)->chunks();
for (const auto& chunk : chunks) {
column_arrays.push_back(chunk);
}
}
columns[i] = std::make_shared<ChunkedArray>(column_arrays, schema->field(i)->type());
}
return Table::Make(std::move(schema), std::move(columns));
}
Result<std::shared_ptr<Table>> PromoteTableToSchema(const std::shared_ptr<Table>& table,
const std::shared_ptr<Schema>& schema,
MemoryPool* pool) {
return PromoteTableToSchema(table, schema, compute::CastOptions::Safe(), pool);
}
Result<std::shared_ptr<Table>> PromoteTableToSchema(const std::shared_ptr<Table>& table,
const std::shared_ptr<Schema>& schema,
const compute::CastOptions& options,
MemoryPool* pool) {
const std::shared_ptr<Schema> current_schema = table->schema();
if (current_schema->Equals(*schema, /*check_metadata=*/false)) {
return table->ReplaceSchemaMetadata(schema->metadata());
}
// fields_seen[i] == true iff that field is also in `schema`.
std::vector<bool> fields_seen(current_schema->num_fields(), false);
std::vector<std::shared_ptr<ChunkedArray>> columns;
columns.reserve(schema->num_fields());
const int64_t num_rows = table->num_rows();
auto AppendColumnOfNulls = [pool, &columns,
num_rows](const std::shared_ptr<DataType>& type) {
// TODO(bkietz): share the zero-filled buffers as much as possible across
// the null-filled arrays created here.
ARROW_ASSIGN_OR_RAISE(auto array_of_nulls, MakeArrayOfNull(type, num_rows, pool));
columns.push_back(std::make_shared<ChunkedArray>(array_of_nulls));
return Status::OK();
};
for (const auto& field : schema->fields()) {
const std::vector<int> field_indices =
current_schema->GetAllFieldIndices(field->name());
if (field_indices.empty()) {
RETURN_NOT_OK(AppendColumnOfNulls(field->type()));
continue;
}
if (field_indices.size() > 1) {
return Status::Invalid(
"PromoteTableToSchema cannot handle schemas with duplicate fields: ",
field->name());
}
const int field_index = field_indices[0];
const auto& current_field = current_schema->field(field_index);
if (!field->nullable() && current_field->nullable()) {
return Status::TypeError("Unable to promote field ", current_field->name(),
": it was nullable but the target schema was not.");
}
fields_seen[field_index] = true;
if (current_field->type()->Equals(field->type())) {
columns.push_back(table->column(field_index));
continue;
}
if (current_field->type()->id() == Type::NA) {
RETURN_NOT_OK(AppendColumnOfNulls(field->type()));
continue;
}
if (!compute::CanCast(*current_field->type(), *field->type())) {
return Status::TypeError("Unable to promote field ", field->name(),
": incompatible types: ", field->type()->ToString(),
" vs ", current_field->type()->ToString());
}
compute::ExecContext ctx(pool);
ARROW_ASSIGN_OR_RAISE(auto casted, compute::Cast(table->column(field_index),
field->type(), options, &ctx));
columns.push_back(casted.chunked_array());
}
auto unseen_field_iter = std::find(fields_seen.begin(), fields_seen.end(), false);
if (unseen_field_iter != fields_seen.end()) {
const size_t unseen_field_index = unseen_field_iter - fields_seen.begin();
return Status::Invalid(
"Incompatible schemas: field ",
current_schema->field(static_cast<int>(unseen_field_index))->name(),
" did not exist in the new schema.");
}
return Table::Make(schema, std::move(columns));
}
bool Table::Equals(const Table& other, bool check_metadata) const {
if (this == &other) {
return true;
}
if (!schema_->Equals(*other.schema(), check_metadata)) {
return false;
}
if (this->num_columns() != other.num_columns()) {
return false;
}
for (int i = 0; i < this->num_columns(); i++) {
if (!this->column(i)->Equals(other.column(i))) {
return false;
}
}
return true;
}
Result<std::shared_ptr<Table>> Table::CombineChunks(MemoryPool* pool) const {
const int ncolumns = num_columns();
std::vector<std::shared_ptr<ChunkedArray>> compacted_columns(ncolumns);
for (int i = 0; i < ncolumns; ++i) {
const auto& col = column(i);
if (col->num_chunks() <= 1) {
compacted_columns[i] = col;
continue;
}
if (is_binary_like(col->type()->id())) {
// ARROW-5744 Allow binary columns to be combined into multiple chunks to avoid
// buffer overflow
ArrayVector chunks;
int chunk_i = 0;
while (chunk_i < col->num_chunks()) {
ArrayVector safe_chunks;
int64_t data_length = 0;
for (; chunk_i < col->num_chunks(); ++chunk_i) {
const auto& chunk = col->chunk(chunk_i);
data_length += checked_cast<const BinaryArray&>(*chunk).total_values_length();
if (data_length >= kBinaryMemoryLimit) {
break;
}
safe_chunks.push_back(chunk);
}
chunks.emplace_back();
ARROW_ASSIGN_OR_RAISE(chunks.back(), Concatenate(safe_chunks, pool));
}
compacted_columns[i] = std::make_shared<ChunkedArray>(std::move(chunks));
} else {
ARROW_ASSIGN_OR_RAISE(auto compacted, Concatenate(col->chunks(), pool));
compacted_columns[i] = std::make_shared<ChunkedArray>(compacted);
}
}
return Table::Make(schema(), std::move(compacted_columns), num_rows_);
}
Result<std::shared_ptr<RecordBatch>> Table::CombineChunksToBatch(MemoryPool* pool) const {
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<Table> combined, CombineChunks(pool));
std::vector<std::shared_ptr<Array>> arrays;
for (const auto& column : combined->columns()) {
if (column->num_chunks() == 0) {
DCHECK_EQ(num_rows(), 0) << "Empty chunk with more than 0 rows";
ARROW_ASSIGN_OR_RAISE(auto chunk,
MakeArrayOfNull(column->type(), num_rows(), pool));
arrays.push_back(std::move(chunk));
} else {
arrays.push_back(column->chunk(0));
}
}
return RecordBatch::Make(schema_, num_rows_, std::move(arrays));
}
// ----------------------------------------------------------------------
// Convert a table to a sequence of record batches
TableBatchReader::TableBatchReader(const Table& table)
: owned_table_(nullptr),
table_(table),
column_data_(table.num_columns()),
chunk_numbers_(table.num_columns(), 0),
chunk_offsets_(table.num_columns(), 0),
absolute_row_position_(0),
max_chunksize_(std::numeric_limits<int64_t>::max()) {
for (int i = 0; i < table.num_columns(); ++i) {
column_data_[i] = table.column(i).get();
}
DCHECK(table_.Validate().ok());
}
TableBatchReader::TableBatchReader(std::shared_ptr<Table> table)
: owned_table_(std::move(table)),
table_(*owned_table_),
column_data_(owned_table_->num_columns()),
chunk_numbers_(owned_table_->num_columns(), 0),
chunk_offsets_(owned_table_->num_columns(), 0),
absolute_row_position_(0),
max_chunksize_(std::numeric_limits<int64_t>::max()) {
for (int i = 0; i < owned_table_->num_columns(); ++i) {
column_data_[i] = owned_table_->column(i).get();
}
DCHECK(table_.Validate().ok());
}
std::shared_ptr<Schema> TableBatchReader::schema() const { return table_.schema(); }
void TableBatchReader::set_chunksize(int64_t chunksize) { max_chunksize_ = chunksize; }
Status TableBatchReader::ReadNext(std::shared_ptr<RecordBatch>* out) {
if (absolute_row_position_ == table_.num_rows()) {
*out = nullptr;
return Status::OK();
}
// Determine the minimum contiguous slice across all columns
int64_t chunksize =
std::min(table_.num_rows() - absolute_row_position_, max_chunksize_);
std::vector<const Array*> chunks(table_.num_columns());
for (int i = 0; i < table_.num_columns(); ++i) {
auto chunk = column_data_[i]->chunk(chunk_numbers_[i]).get();
int64_t chunk_remaining = chunk->length() - chunk_offsets_[i];
if (chunk_remaining < chunksize) {
chunksize = chunk_remaining;
}
chunks[i] = chunk;
}
// Slice chunks and advance chunk index as appropriate
std::vector<std::shared_ptr<ArrayData>> batch_data(table_.num_columns());
for (int i = 0; i < table_.num_columns(); ++i) {
// Exhausted chunk
const Array* chunk = chunks[i];
const int64_t offset = chunk_offsets_[i];
std::shared_ptr<ArrayData> slice_data;
if ((chunk->length() - offset) == chunksize) {
++chunk_numbers_[i];
chunk_offsets_[i] = 0;
if (offset > 0) {
// Need to slice
slice_data = chunk->Slice(offset, chunksize)->data();
} else {
// No slice
slice_data = chunk->data();
}
} else {
chunk_offsets_[i] += chunksize;
slice_data = chunk->Slice(offset, chunksize)->data();
}
batch_data[i] = std::move(slice_data);
}
absolute_row_position_ += chunksize;
*out = RecordBatch::Make(table_.schema(), chunksize, std::move(batch_data));
return Status::OK();
}
} // namespace arrow20
|