Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
row_wise_conversion

Convert a vector of row-wise data into an Arrow table

While we want to use columnar data structures to build efficient operations, we often receive data in a row-wise fashion from other systems. In the following, we want give a brief introduction into the classes provided by Apache Arrow by showing how to transform row-wise data into a columnar table.

The data in this example is stored in the following struct:

1 struct data_row {
2  int64_t id;
3  double cost;
4  std::vector<double> cost_components;
5 };
6 
7 std::vector<data_row> rows;

The final representation should be an arrow::Table which in turn is made up of an arrow::Schema and a list of arrow::Column. An arrow::Column is again a named collection of one or more arrow::Array instances. As the first step, we will iterate over the data and build up the arrays incrementally. For this task, we provide arrow::ArrayBuilder classes that help in the construction of the final arrow::Array instances.

For each type, Arrow has a specially typed builder class. For the primitive values id and cost we can use the respective arrow::Int64Builder and arrow::DoubleBuilder. For the cost_components vector, we need to have two builders, a top-level arrow::ListBuilder that builds the array of offsets and a nested arrow::DoubleBuilder that constructs the underlying values array that is referenced by the offsets in the former array.

1 // The builders are more efficient using
2 // arrow::jemalloc::MemoryPool::default_pool() as this can increase the size of
3 // the underlying memory regions in-place. At the moment, arrow::jemalloc is only
4 // supported on Unix systems, not Windows.
5 
6 arrow::Int64Builder id_builder(arrow::default_memory_pool());
7 arrow::DoubleBuilder cost_builder(arrow::default_memory_pool());
8 std::shared_ptr<DoubleBuilder> components_values_builder =
9  std::make_shared<DoubleBuilder>(arrow::default_memory_pool());
10 arrow::ListBuilder components_builder(arrow::default_memory_pool(),
11  components_values_builder);

Now we can loop over our existing data and insert it into the builders. The Append calls here may fail (e.g. we cannot allocate enough additional memory). Thus we need to check their return values. For more information on these values, check the documentation about arrow::Status.

1 for (const data_row& row : rows) {
2  ARROW_RETURN_NOT_OK(id_builder.Append(row.id));
3  ARROW_RETURN_NOT_OK(cost_builder.Append(row.cost));
4 
5  // Indicate the start of a new list row. This will memorise the current
6  // offset in the values builder.
7  ARROW_RETURN_NOT_OK(components_builder.Append());
8  // Store the actual values. The final nullptr argument tells the underyling
9  // builder that all added values are valid, i.e. non-null.
10  ARROW_RETURN_NOT_OK(components_values_builder->Append(
11  row.cost_components.data(), row.cost_components.size(),
12  nullptr);
13 }

At the end, we finalise the arrays, declare the (type) schema and combine them into a single arrow::Table:

1 std::shared_ptr<arrow::Array> id_array;
2 ARROW_RETURN_NOT_OK(id_builder.Finish(&id_array));
3 std::shared_ptr<arrow::Array> cost_array;
4 ARROW_RETURN_NOT_OK(cost_builder.Finish(&cost_array));
5 std::shared_ptr<arrow::Array> cost_components_array;
6 ARROW_RETURN_NOT_OK(components_builder.Finish(&cost_components_array));
7 
8 std::vector<std::shared_ptr<arrow::Field>> schema_vector = {
9  arrow::field("id", arrow::int64()),
10  arrow::field("cost", arrow::float64()),
11  arrow::field("cost_components", arrow::list(arrow::float64()))
12 };
13 auto schema = std::make_shared<arrow::Schema>(schema_vector);
14 
15 std::shared_ptr<arrow::Table> table;
16 ARROW_RETURN_NOT_OK(MakeTable(schema,
17  {id_array, cost_array, cost_components_array}, &table));

The final table variable is the one we then can pass on to other functions that can consume Apache Arrow memory structures. This object has ownership of all referenced data, thus we don't have to care about undefined references once we leave the scope of the function building the table and its underlying arrays.

Converting an Arrow Table back into row-wise representation

To convert an Arrow table back into the same row-wise representation as in the above section, we first will check that the table conforms to our expected schema and then will build up the vector of rows incrementally.

For the check if the table is as expected, we can utilise solely its schema.

1 // This is our input that was passed in from the outside.
2 std::shared_ptr<arrow::Table> table;
3 
4 std::vector<std::shared_ptr<arrow::Field>> schema_vector = {
5  arrow::field("id", arrow::int64()),
6  arrow::field("cost", arrow::float64()),
7  arrow::field("cost_components", arrow::list(arrow::float64()))
8 };
9 auto expected_schema = std::make_shared<arrow::Schema>(schema_vector);
10 
11 if (!expected_schema->Equals(*table->schema())) {
12  // The table doesn't have the expected schema thus we cannot directly
13  // convert it to our target representation.
14  // TODO: Implement your custom error handling logic here.
15 }

As we have ensured that the table has the expected structure, we can unpack the underlying arrays. For the primitive columns id and cost we can use the high level functions to get the values whereas for the nested column cost_components we need to access the C-pointer to the data to copy its contents into the resulting std::vector<double>. Here we need to be care to also add the offset to the pointer. This offset is needed to enable zero-copy slicing operations. While this could be adjusted automatically for double arrays, this cannot be done for the accompanying bitmap as often the slicing border would be inside a byte.

1 // For simplicity, we assume that all arrays consist of a single chunk here.
2 // In a productive implementation this should either be explicitly check or code
3 // added that can treat chunked arrays.
4 
5 auto ids = std::static_pointer_cast<arrow::Int64Array>(
6  table->column(0)->data()->chunk(0));
7 auto costs = std::static_pointer_cast<arrow::DoubleArray(
8  table->column(1)->data()->chunk(0));
9 auto cost_components = std::static_pointer_cast<arrow::ListArray(
10  table->column(2)->data()->chunk(0));
11 auto cost_components_values = std::static_pointer_cast<arrow::DoubleArray>(
12  cost_components->values());
13 // To enable zero-copy slices, the native values pointer might need to account
14 // for this slicing offset. This is not needed for the higher level functions
15 // like Value(…) that already account for this offset internally.
16 const double* cost_components_values_ptr = cost_components_values->data()
17  + cost_components_values->offset();

After we have unpacked the arrays from the table, we can iterate over them in a row-wise fashion and fill our target, row-wise representation.

1 std::vector<data_row> rows;
2 
3 for (int64_t i = 0; i < table->num_rows(); i++) {
4  // Another simplification in this example is that we assume that there are
5  // no null entries, e.g. each row is fill with valid values.
6  int64_t id = ids->Value(i);
7  double cost = costs->Value(i);
8  const double* first = cost_components_values_ptr + cost_components->value_offset(i);
9  const double* last = cost_components_values_ptr + cost_components->value_offset(i + 1);
10  std::vector<double> components_vec(first, last);
11  rows.push_back({id, cost, components_vec});
12 }