forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.h
More file actions
331 lines (255 loc) · 11.6 KB
/
Copy pathtable.h
File metadata and controls
331 lines (255 loc) · 11.6 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
// 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.
#ifndef ARROW_TABLE_H
#define ARROW_TABLE_H
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "arrow/array.h"
#include "arrow/type.h"
#include "arrow/util/macros.h"
#include "arrow/util/visibility.h"
namespace arrow {
class KeyValueMetadata;
class Status;
using ArrayVector = std::vector<std::shared_ptr<Array>>;
/// \class ChunkedArray
/// \brief A data structure managing a list of primitive Arrow arrays logically
/// as one large array
class ARROW_EXPORT ChunkedArray {
public:
explicit ChunkedArray(const ArrayVector& chunks);
/// \return the total length of the chunked array; computed on construction
int64_t length() const { return length_; }
int64_t null_count() const { return null_count_; }
int num_chunks() const { return static_cast<int>(chunks_.size()); }
/// \return chunk a particular chunk from the chunked array
std::shared_ptr<Array> chunk(int i) const { return chunks_[i]; }
const ArrayVector& chunks() const { return chunks_; }
std::shared_ptr<DataType> type() const;
bool Equals(const ChunkedArray& other) const;
bool Equals(const std::shared_ptr<ChunkedArray>& other) const;
protected:
ArrayVector chunks_;
int64_t length_;
int64_t null_count_;
};
/// \brief An immutable column data structure consisting of a field (type
/// metadata) and a logical chunked data array
class ARROW_EXPORT Column {
public:
Column(const std::shared_ptr<Field>& field, const ArrayVector& chunks);
Column(const std::shared_ptr<Field>& field, const std::shared_ptr<ChunkedArray>& data);
Column(const std::shared_ptr<Field>& field, const std::shared_ptr<Array>& data);
// Construct from name and array
Column(const std::string& name, const std::shared_ptr<Array>& data);
int64_t length() const { return data_->length(); }
int64_t null_count() const { return data_->null_count(); }
std::shared_ptr<Field> field() const { return field_; }
/// \brief The column name
/// \return the column's name in the passed metadata
const std::string& name() const { return field_->name(); }
/// \brief The column type
/// \return the column's type according to the metadata
std::shared_ptr<DataType> type() const { return field_->type(); }
/// \brief The column data as a chunked array
/// \return the column's data as a chunked logical array
std::shared_ptr<ChunkedArray> data() const { return data_; }
bool Equals(const Column& other) const;
bool Equals(const std::shared_ptr<Column>& other) const;
/// \brief Verify that the column's array data is consistent with the passed
/// field's metadata
Status ValidateData();
protected:
std::shared_ptr<Field> field_;
std::shared_ptr<ChunkedArray> data_;
private:
ARROW_DISALLOW_COPY_AND_ASSIGN(Column);
};
/// \class RecordBatch
/// \brief Collection of equal-length arrays matching a particular Schema
///
/// A record batch is table-like data structure consisting of an internal
/// sequence of fields, each a contiguous Arrow array
class ARROW_EXPORT RecordBatch {
public:
/// \param[in] schema The record batch schema
/// \param[in] num_rows length of fields in the record batch. Each array
/// should have the same length as num_rows
/// \param[in] columns the record batch fields as vector of arrays
RecordBatch(const std::shared_ptr<Schema>& schema, int64_t num_rows,
const std::vector<std::shared_ptr<Array>>& columns);
/// \brief Move-based constructor for a vector of Array instances
RecordBatch(const std::shared_ptr<Schema>& schema, int64_t num_rows,
std::vector<std::shared_ptr<Array>>&& columns);
/// \brief Construct record batch from vector of internal data structures
/// \since 0.5.0
///
/// This class is only provided with an rvalue-reference for the input data,
/// and is intended for internal use, or advanced users.
///
/// \param schema the record batch schema
/// \param num_rows the number of semantic rows in the record batch. This
/// should be equal to the length of each field
/// \param columns the data for the batch's columns
RecordBatch(const std::shared_ptr<Schema>& schema, int64_t num_rows,
std::vector<std::shared_ptr<ArrayData>>&& columns);
/// \brief Construct record batch by copying vector of array data
/// \since 0.5.0
RecordBatch(const std::shared_ptr<Schema>& schema, int64_t num_rows,
const std::vector<std::shared_ptr<ArrayData>>& columns);
/// \brief Determine if two record batches are exactly equal
/// \return true if batches are equal
bool Equals(const RecordBatch& other) const;
/// \brief Determine if two record batches are approximately equal
bool ApproxEquals(const RecordBatch& other) const;
// \return the table's schema
/// \return true if batches are equal
std::shared_ptr<Schema> schema() const { return schema_; }
/// \brief Retrieve an array from the record batch
/// \param[in] i field index, does not boundscheck
/// \return an Array object
std::shared_ptr<Array> column(int i) const;
std::shared_ptr<ArrayData> column_data(int i) const { return columns_[i]; }
/// \brief Name in i-th column
const std::string& column_name(int i) const;
/// \return the number of columns in the table
int num_columns() const { return static_cast<int>(columns_.size()); }
/// \return the number of rows (the corresponding length of each column)
int64_t num_rows() const { return num_rows_; }
/// \brief Replace schema key-value metadata with new metadata (EXPERIMENTAL)
/// \since 0.5.0
///
/// \param[in] metadata new KeyValueMetadata
/// \return new RecordBatch
std::shared_ptr<RecordBatch> ReplaceSchemaMetadata(
const std::shared_ptr<const KeyValueMetadata>& metadata) const;
/// \brief Slice each of the arrays in the record batch
/// \param[in] offset the starting offset to slice, through end of batch
/// \return new record batch
std::shared_ptr<RecordBatch> Slice(int64_t offset) const;
/// \brief Slice each of the arrays in the record batch
/// \param[in] offset the starting offset to slice
/// \param[in] length the number of elements to slice from offset
/// \return new record batch
std::shared_ptr<RecordBatch> Slice(int64_t offset, int64_t length) const;
/// \brief Check for schema or length inconsistencies
/// \return Status
Status Validate() const;
private:
ARROW_DISALLOW_COPY_AND_ASSIGN(RecordBatch);
RecordBatch(const std::shared_ptr<Schema>& schema, int64_t num_rows);
std::shared_ptr<Schema> schema_;
int64_t num_rows_;
std::vector<std::shared_ptr<ArrayData>> columns_;
// Caching boxed array data
mutable std::vector<std::shared_ptr<Array>> boxed_columns_;
};
/// \class Table
/// \brief Logical table as sequence of chunked arrays
class ARROW_EXPORT Table {
public:
/// \brief Construct Table from schema and columns
/// If columns is zero-length, the table's number of rows is zero
/// \param schema The table schema (column types)
/// \param columns The table's columns
/// \param num_rows number of rows in table, -1 (default) to infer from columns
Table(const std::shared_ptr<Schema>& schema,
const std::vector<std::shared_ptr<Column>>& columns, int64_t num_rows = -1);
/// \brief Construct Table from schema and arrays
/// \param schema The table schema (column types)
/// \param arrays The table's columns as arrays
/// \param num_rows number of rows in table, -1 (default) to infer from columns
Table(const std::shared_ptr<Schema>& schema,
const std::vector<std::shared_ptr<Array>>& arrays, int64_t num_rows = -1);
// Construct table from RecordBatch, but only if all of the batch schemas are
// equal. Returns Status::Invalid if there is some problem
static Status FromRecordBatches(
const std::vector<std::shared_ptr<RecordBatch>>& batches,
std::shared_ptr<Table>* table);
/// \return the table's schema
std::shared_ptr<Schema> schema() const { return schema_; }
/// \param[in] i column index, does not boundscheck
/// \return the i-th column
std::shared_ptr<Column> column(int i) const { return columns_[i]; }
/// \brief Remove column from the table, producing a new Table
Status RemoveColumn(int i, std::shared_ptr<Table>* out) const;
/// \brief Add column to the table, producing a new Table
Status AddColumn(int i, const std::shared_ptr<Column>& column,
std::shared_ptr<Table>* out) const;
/// \brief Replace schema key-value metadata with new metadata (EXPERIMENTAL)
/// \since 0.5.0
///
/// \param[in] metadata new KeyValueMetadata
/// \return new Table
std::shared_ptr<Table> ReplaceSchemaMetadata(
const std::shared_ptr<const KeyValueMetadata>& metadata) const;
/// \return the number of columns in the table
int num_columns() const { return static_cast<int>(columns_.size()); }
/// \return the number of rows (the corresponding length of each column)
int64_t num_rows() const { return num_rows_; }
/// \brief Determine if semantic contents of tables are exactly equal
bool Equals(const Table& other) const;
/// \brief Perform any checks to validate the input arguments
Status ValidateColumns() const;
/// \brief Return true if any column has multiple chunks
bool IsChunked() const;
private:
ARROW_DISALLOW_COPY_AND_ASSIGN(Table);
std::shared_ptr<Schema> schema_;
std::vector<std::shared_ptr<Column>> columns_;
int64_t num_rows_;
};
/// \brief Abstract interface for reading stream of record batches
class ARROW_EXPORT RecordBatchReader {
public:
virtual ~RecordBatchReader();
/// \return the shared schema of the record batches in the stream
virtual std::shared_ptr<Schema> schema() const = 0;
/// Read the next record batch in the stream. Return null for batch when
/// reaching end of stream
///
/// \param[out] batch the next loaded batch, null at end of stream
/// \return Status
virtual Status ReadNext(std::shared_ptr<RecordBatch>* batch) = 0;
};
/// \brief Compute a sequence of record batches from a (possibly chunked) Table
class ARROW_EXPORT TableBatchReader : public RecordBatchReader {
public:
~TableBatchReader();
/// \brief Read batches with the maximum possible size
explicit TableBatchReader(const Table& table);
std::shared_ptr<Schema> schema() const override;
Status ReadNext(std::shared_ptr<RecordBatch>* out) override;
private:
class TableBatchReaderImpl;
std::unique_ptr<TableBatchReaderImpl> impl_;
};
/// \brief Construct table from multiple input tables.
/// \return Status, fails if any schemas are different
ARROW_EXPORT
Status ConcatenateTables(const std::vector<std::shared_ptr<Table>>& tables,
std::shared_ptr<Table>* table);
/// \brief Construct table from multiple input tables.
/// \return Status, fails if any schemas are different
ARROW_EXPORT
Status MakeTable(const std::shared_ptr<Schema>& schema,
const std::vector<std::shared_ptr<Array>>& arrays,
std::shared_ptr<Table>* table);
} // namespace arrow
#endif // ARROW_TABLE_H