Skip to content

Instantly share code, notes, and snippets.

@amoeba
Created January 10, 2024 18:59
Show Gist options
  • Save amoeba/9c747d0da2c31b278cd0b173f2d19e1d to your computer and use it in GitHub Desktop.
Save amoeba/9c747d0da2c31b278cd0b173f2d19e1d to your computer and use it in GitHub Desktop.
#include "arrow/array/array_base.h"
#include "arrow/record_batch.h"
#include "arrow/result.h"
#include <iostream>
#include <memory>
#include <stddef.h>
#include <arrow/api.h>
#include <arrow/compute/api.h>
arrow::Result<std::shared_ptr<arrow::RecordBatch>> MakeABatch() {
// Build an Array to add to the batch
std::vector<int32_t> vec{1, 2, 3, 4, 5};
size_t n = 5;
arrow::Int32Builder i32;
size_t size = vec.size();
i32.AppendValues(std::vector<int32_t>(size, n));
auto a_n = i32.Finish().ValueOrDie();
// Build an initial RecordBatch
auto field = arrow::field("f1", arrow::int32());
auto schema = ::arrow::schema({field});
auto rbatch = arrow::RecordBatch::Make(
schema, n, std::vector<std::shared_ptr<arrow::Array>>{a_n});
// Return the result of adding the column to the batch
return rbatch->AddColumn(0, arrow::field("n", arrow::int32(), false), a_n);
}
arrow::Status RunMain() {
ARROW_ASSIGN_OR_RAISE(auto new_batch, MakeABatch());
std::cout << new_batch->schema()->ToString() << std::endl;
std::cout << new_batch->num_rows() << "x" << new_batch->num_columns()
<< std::endl;
return arrow::Status::OK();
}
int main() {
arrow::Status st = RunMain();
if (!st.ok()) {
std::cerr << st << std::endl;
return 1;
}
return 0;
}
n: int32 not null
f1: int32
5x2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment