Conversion of range ofstd::tuple-like toTable instances#

While the above example shows a quite manual approach of a row to columnarconversion, Arrow also provides some template logic to convert ranges ofstd::tuple<..>-like objects to tables.

In the most simple case, you only need to provide the input data and thetype conversion is then inferred at compile time.

std::vector<std::tuple<double,std::string>>rows=..std::shared_ptr<Table>table;if(!arrow::stl::TableFromTupleRange(arrow::default_memory_pool(),rows,names,&table).ok()){// Error handling code should go here.}

In reverse, you can useTupleRangeFromTable to fill an alreadypre-allocated range with the data from aTable instance.

// An important aspect here is that the table columns need to be in the// same order as the columns will later appear in the tuple. As the tuple// is unnamed, matching is done on positions.std::shared_ptr<Table>table=..// The range needs to be pre-allocated to the respective amount of rows.// This allows us to pass in an arbitrary range object, not only// `std::vector`.std::vector<std::tuple<double,std::string>>rows(2);if(!arrow::stl::TupleRangeFromTable(*table,&rows).ok()){// Error handling code should go here.}

Arrow itself already supports some C(++) data types for this conversion. If youwant to support additional data types, you need to implement a specializationofarrow::stl::ConversionTraits<T> and the more generalarrow::CTypeTraits<T>.

namespacearrow{template<>structCTypeTraits<boost::posix_time::ptime>{usingArrowType=::arrow::TimestampType;staticstd::shared_ptr<::arrow::DataType>type_singleton(){return::arrow::timestamp(::arrow::TimeUnit::MICRO);}};}namespacearrow{namespacestl{template<>structConversionTraits<boost::posix_time::ptime>:publicCTypeTraits<boost::posix_time::ptime>{constexprstaticboolnullable=false;// This is the specialization to load a scalar value into an Arrow builder.staticStatusAppendRow(typenameTypeTraits<TimestampType>::BuilderType&builder,boost::posix_time::ptimecell){boost::posix_time::ptimeconstepoch({1970,1,1},{0,0,0,0});returnbuilder.Append((cell-epoch).total_microseconds());}// Specify how we can fill the tuple from the values stored in the Arrow// array.staticboost::posix_time::ptimeGetEntry(constTimestampArray&array,size_tj){returnpsapp::arrow::internal::timestamp_epoch+boost::posix_time::time_duration(0,0,0,array.Value(j));}};}}