forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_builder.cc
More file actions
137 lines (114 loc) · 4.92 KB
/
Copy pathtable_builder.cc
File metadata and controls
137 lines (114 loc) · 4.92 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
// 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/table_builder.h"
#include <memory>
#include <utility>
#include "arrow/array/array_base.h"
#include "arrow/array/builder_base.h"
#include "arrow/record_batch.h"
#include "arrow/status.h"
#include "arrow/type.h"
#include "arrow/util/logging.h"
namespace arrow {
// ----------------------------------------------------------------------
// RecordBatchBuilder
RecordBatchBuilder::RecordBatchBuilder(const std::shared_ptr<Schema>& schema,
MemoryPool* pool, int64_t initial_capacity)
: schema_(schema), initial_capacity_(initial_capacity), pool_(pool) {}
Status RecordBatchBuilder::Make(const std::shared_ptr<Schema>& schema, MemoryPool* pool,
std::unique_ptr<RecordBatchBuilder>* builder) {
ARROW_ASSIGN_OR_RAISE(*builder, Make(schema, pool, kMinBuilderCapacity))
return Status::OK();
}
Status RecordBatchBuilder::Make(const std::shared_ptr<Schema>& schema, MemoryPool* pool,
int64_t initial_capacity,
std::unique_ptr<RecordBatchBuilder>* builder) {
ARROW_ASSIGN_OR_RAISE(*builder, Make(schema, pool, initial_capacity))
return Status::OK();
}
Result<std::unique_ptr<RecordBatchBuilder>> RecordBatchBuilder::Make(
const std::shared_ptr<Schema>& schema, MemoryPool* pool) {
return Make(schema, pool, kMinBuilderCapacity);
}
Result<std::unique_ptr<RecordBatchBuilder>> RecordBatchBuilder::Make(
const std::shared_ptr<Schema>& schema, MemoryPool* pool, int64_t initial_capacity) {
auto builder = std::unique_ptr<RecordBatchBuilder>(
new RecordBatchBuilder(schema, pool, initial_capacity));
RETURN_NOT_OK(builder->CreateBuilders());
RETURN_NOT_OK(builder->InitBuilders());
return std::move(builder);
}
Status RecordBatchBuilder::Flush(bool reset_builders,
std::shared_ptr<RecordBatch>* batch) {
ARROW_ASSIGN_OR_RAISE(*batch, Flush(reset_builders));
return Status::OK();
}
Status RecordBatchBuilder::Flush(std::shared_ptr<RecordBatch>* batch) {
ARROW_ASSIGN_OR_RAISE(*batch, Flush(true));
return Status::OK();
}
Result<std::shared_ptr<RecordBatch>> RecordBatchBuilder::Flush(bool reset_builders) {
std::vector<std::shared_ptr<Array>> fields;
fields.resize(this->num_fields());
int64_t length = 0;
for (int i = 0; i < this->num_fields(); ++i) {
RETURN_NOT_OK(raw_field_builders_[i]->Finish(&fields[i]));
if (i > 0 && fields[i]->length() != length) {
return Status::Invalid("All fields must be same length when calling Flush");
}
length = fields[i]->length();
}
// For certain types like dictionaries, types may not be fully
// determined before we have flushed. Make sure that the RecordBatch
// gets the correct types in schema.
// See: #ARROW-9969
std::vector<std::shared_ptr<Field>> schema_fields(schema_->fields());
for (int i = 0; i < this->num_fields(); ++i) {
if (!schema_fields[i]->type()->Equals(fields[i]->type())) {
schema_fields[i] = schema_fields[i]->WithType(fields[i]->type());
}
}
std::shared_ptr<Schema> schema =
std::make_shared<Schema>(std::move(schema_fields), schema_->metadata());
std::shared_ptr<RecordBatch> batch =
RecordBatch::Make(std::move(schema), length, std::move(fields));
if (reset_builders) {
ARROW_RETURN_NOT_OK(InitBuilders());
}
return batch;
}
Result<std::shared_ptr<RecordBatch>> RecordBatchBuilder::Flush() { return Flush(true); }
void RecordBatchBuilder::SetInitialCapacity(int64_t capacity) {
ARROW_CHECK_GT(capacity, 0) << "Initial capacity must be positive";
initial_capacity_ = capacity;
}
Status RecordBatchBuilder::CreateBuilders() {
field_builders_.resize(this->num_fields());
raw_field_builders_.resize(this->num_fields());
for (int i = 0; i < this->num_fields(); ++i) {
RETURN_NOT_OK(MakeBuilder(pool_, schema_->field(i)->type(), &field_builders_[i]));
raw_field_builders_[i] = field_builders_[i].get();
}
return Status::OK();
}
Status RecordBatchBuilder::InitBuilders() {
for (int i = 0; i < this->num_fields(); ++i) {
RETURN_NOT_OK(raw_field_builders_[i]->Reserve(initial_capacity_));
}
return Status::OK();
}
} // namespace arrow