| 285 | } |
| 286 | |
| 287 | Result<std::shared_ptr<Table>> Table::FromRecordBatches( |
| 288 | std::shared_ptr<Schema> schema, |
| 289 | const std::vector<std::shared_ptr<RecordBatch>>& batches) { |
| 290 | const int nbatches = static_cast<int>(batches.size()); |
| 291 | const int ncolumns = static_cast<int>(schema->num_fields()); |
| 292 | |
| 293 | int64_t num_rows = 0; |
| 294 | for (int i = 0; i < nbatches; ++i) { |
| 295 | if (!batches[i]->schema()->Equals(*schema, false)) { |
| 296 | return Status::Invalid("Schema at index ", static_cast<int>(i), |
| 297 | " was different: \n", schema->ToString(), "\nvs\n", |
| 298 | batches[i]->schema()->ToString()); |
| 299 | } |
| 300 | num_rows += batches[i]->num_rows(); |
| 301 | } |
| 302 | |
| 303 | std::vector<std::shared_ptr<ChunkedArray>> columns(ncolumns); |
| 304 | std::vector<std::shared_ptr<Array>> column_arrays(nbatches); |
| 305 | |
| 306 | for (int i = 0; i < ncolumns; ++i) { |
| 307 | for (int j = 0; j < nbatches; ++j) { |
| 308 | column_arrays[j] = batches[j]->column(i); |
| 309 | } |
| 310 | columns[i] = std::make_shared<ChunkedArray>(column_arrays, schema->field(i)->type()); |
| 311 | } |
| 312 | |
| 313 | return Table::Make(std::move(schema), std::move(columns), num_rows); |
| 314 | } |
| 315 | |
| 316 | Result<std::shared_ptr<Table>> Table::FromRecordBatches( |
| 317 | const std::vector<std::shared_ptr<RecordBatch>>& batches) { |