Working with the C++ Implementation

This section of the cookbook goes over basic concepts that will be needed regardless of how you intend to use the Arrow C++ implementation.

Working with Status and Result

C++ libraries often have to choose between throwing exceptions and returning error codes. Arrow chooses to return Status and Result objects as a middle ground. This makes it clear when a function can fail and is easier to use than integer arrow codes.

It is important to always check the value of a returned Status object to ensure that the operation succeeded. However, this can quickly become tedious:

Checking the status of every function manually
std::function<arrow::Status()> test_fn = [] {
  arrow::NullBuilder builder;
  arrow::Status st = builder.Reserve(2);
  // Tedious return value check
  if (!st.ok()) {
    return st;
  }
  st = builder.AppendNulls(-1);
  // Tedious return value check
  if (!st.ok()) {
    return st;
  }
  rout << "Appended -1 null values?" << std::endl;
  return arrow::Status::OK();
};
arrow::Status st = test_fn();
rout << st << std::endl;
Code Output
Invalid: length must be positive

The macro ARROW_RETURN_NOT_OK will take care of some of this boilerplate for you. It will run the contained expression and check the resulting Status or Result object. If it failed then it will return the failure.

Using ARROW_RETURN_NOT_OK to check the status
std::function<arrow::Status()> test_fn = [] {
  arrow::NullBuilder builder;
  ARROW_RETURN_NOT_OK(builder.Reserve(2));
  ARROW_RETURN_NOT_OK(builder.AppendNulls(-1));
  rout << "Appended -1 null values?" << std::endl;
  return arrow::Status::OK();
};
arrow::Status st = test_fn();
rout << st << std::endl;
Code Output
Invalid: length must be positive