make_record_batch_with_buf_addrs
Status make_record_batch_with_buf_addrs(SchemaPtr schema, int num_rows,
jlong* in_buf_addrs, jlong* in_buf_sizes,
int in_bufs_len,
std::shared_ptr<arrow::RecordBatch>* batch) {
std::vector<std::shared_ptr<arrow::ArrayData>> columns;
auto num_fields = schema->num_fields();
int buf_idx = 0;
int sz_idx = 0;
for (int i = 0; i < num_fields; i++) {
auto field = schema->field(i);
std::vector<std::shared_ptr<arrow::Buffer>> buffers;
if (buf_idx >= in_bufs_len) {
return Status::Invalid("insufficient number of in_buf_addrs");
}
jlong validity_addr = in_buf_addrs[buf_idx++];
jlong validity_size = in_buf_sizes[sz_idx++];
auto validity = std::shared_ptr<arrow::Buffer>(
new arrow::Buffer(reinterpret_cast<uint8_t*>(validity_addr), validity_size));
buffers.push_back(validity);
if (buf_idx >= in_bufs_len) {
return Status::Invalid("insufficient number of in_buf_addrs");
}
jlong value_addr = in_buf_addrs[buf_idx++];
jlong value_size = in_buf_sizes[sz_idx++];
auto data = std::shared_ptr<arrow::Buffer>(
new arrow::Buffer(reinterpret_cast<uint8_t*>(value_addr), value_size));
buffers.push_back(data);
if (arrow::is_binary_like(field->type()->id())) {
if (buf_idx >= in_bufs_len) {
return Status::Invalid("insufficient number of in_buf_addrs");
}
// add offsets buffer for variable-len fields.
jlong offsets_addr = in_buf_addrs[buf_idx++];
jlong offsets_size = in_buf_sizes[sz_idx++];
auto offsets = std::shared_ptr<arrow::Buffer>(
new arrow::Buffer(reinterpret_cast<uint8_t*>(offsets_addr), offsets_size));
buffers.push_back(offsets);
}
auto array_data = arrow::ArrayData::Make(field->type(), num_rows, std::move(buffers));
columns.push_back(array_data);
}
*batch = arrow::RecordBatch::Make(schema, num_rows, columns);
return Status::OK();
}