OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ArrowResultSet Class Reference

#include <ArrowResultSet.h>

Public Member Functions

 ArrowResultSet (const std::shared_ptr< ResultSet > &rows, const std::vector< TargetMetaInfo > &targets_meta, const ExecutorDeviceType device_type=ExecutorDeviceType::CPU)
 
 ArrowResultSet (const std::shared_ptr< ResultSet > &rows, const ExecutorDeviceType device_type=ExecutorDeviceType::CPU)
 
std::vector< TargetValuegetNextRow (const bool translate_strings, const bool decimal_to_double) const
 
size_t colCount () const
 
SQLTypeInfo getColType (const size_t col_idx) const
 
bool definitelyHasNoRows () const
 
size_t rowCount () const
 

Static Public Member Functions

static void deallocateArrowResultBuffer (const ArrowResult &result, const ExecutorDeviceType device_type, const size_t device_id, std::shared_ptr< Data_Namespace::DataMgr > &data_mgr)
 

Public Attributes

 device_type
 

Private Member Functions

void resultSetArrowLoopback (const ExecutorDeviceType device_type=ExecutorDeviceType::CPU)
 
template<typename Type , typename ArrayType >
void appendValue (std::vector< TargetValue > &row, const arrow::Array &column, const Type null_val, const size_t idx) const
 

Private Attributes

std::shared_ptr< ArrowResultresults_
 
std::shared_ptr< ResultSetrows_
 
std::vector< TargetMetaInfotargets_meta_
 
std::shared_ptr
< arrow::RecordBatch > 
record_batch_
 
arrow::ipc::DictionaryMemo dictionary_memo_
 
std::vector< std::shared_ptr
< arrow::Array > > 
columns_
 
size_t crt_row_idx_
 
std::vector< TargetMetaInfocolumn_metainfo_
 

Detailed Description

Definition at line 98 of file ArrowResultSet.h.

Constructor & Destructor Documentation

ArrowResultSet::ArrowResultSet ( const std::shared_ptr< ResultSet > &  rows,
const std::vector< TargetMetaInfo > &  targets_meta,
const ExecutorDeviceType  device_type = ExecutorDeviceType::CPU 
)

Definition at line 74 of file ArrowResultSet.cpp.

References column_metainfo_, columns_, field(), i, record_batch_, resultSetArrowLoopback(), and anonymous_namespace{ArrowResultSet.cpp}::type_from_arrow_field().

77  : rows_(rows), targets_meta_(targets_meta), crt_row_idx_(0) {
79  auto schema = record_batch_->schema();
80  for (int i = 0; i < schema->num_fields(); ++i) {
81  std::shared_ptr<arrow::Field> field = schema->field(i);
82  SQLTypeInfo type_info = type_from_arrow_field(*schema->field(i));
83  column_metainfo_.emplace_back(field->name(), type_info);
84  columns_.emplace_back(record_batch_->column(i));
85  }
86 }
std::shared_ptr< ResultSet > rows_
SQLTypeInfo type_from_arrow_field(const arrow::Field &field)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
std::vector< TargetMetaInfo > column_metainfo_
void resultSetArrowLoopback(const ExecutorDeviceType device_type=ExecutorDeviceType::CPU)
std::shared_ptr< arrow::RecordBatch > record_batch_
std::vector< TargetMetaInfo > targets_meta_
std::vector< std::shared_ptr< arrow::Array > > columns_

+ Here is the call graph for this function:

ArrowResultSet::ArrowResultSet ( const std::shared_ptr< ResultSet > &  rows,
const ExecutorDeviceType  device_type = ExecutorDeviceType::CPU 
)
inline

Definition at line 103 of file ArrowResultSet.h.

105  : ArrowResultSet(rows, {}, device_type) {}
ArrowResultSet(const std::shared_ptr< ResultSet > &rows, const std::vector< TargetMetaInfo > &targets_meta, const ExecutorDeviceType device_type=ExecutorDeviceType::CPU)

Member Function Documentation

template<typename Type , typename ArrayType >
void ArrowResultSet::appendValue ( std::vector< TargetValue > &  row,
const arrow::Array &  column,
const Type  null_val,
const size_t  idx 
) const
private

Definition at line 89 of file ArrowResultSet.cpp.

92  {
93  const auto& col = static_cast<const ArrayType&>(column);
94  row.emplace_back(col.IsNull(idx) ? null_val : static_cast<Type>(col.Value(idx)));
95 }
size_t ArrowResultSet::colCount ( ) const

Definition at line 200 of file ArrowResultSet.cpp.

References column_metainfo_.

200  {
201  return column_metainfo_.size();
202 }
std::vector< TargetMetaInfo > column_metainfo_
void ArrowResultSet::deallocateArrowResultBuffer ( const ArrowResult result,
const ExecutorDeviceType  device_type,
const size_t  device_id,
std::shared_ptr< Data_Namespace::DataMgr > &  data_mgr 
)
static

Definition at line 1016 of file ArrowResultSetConverter.cpp.

References CHECK_EQ, CPU, ArrowResult::df_handle, ArrowResult::df_size, ArrowResult::sm_handle, ArrowResult::sm_size, and to_string().

Referenced by DBHandler::deallocate_df().

1020  {
1021 #ifndef _MSC_VER
1022  // CPU buffers skip the sm handle, serializing the entire RecordBatch to df.
1023  // Remove shared memory on sysmem
1024  if (!result.sm_handle.empty()) {
1025  CHECK_EQ(sizeof(key_t), result.sm_handle.size());
1026  const key_t& schema_key = *(key_t*)(&result.sm_handle[0]);
1027  auto shm_id = shmget(schema_key, result.sm_size, 0666);
1028  if (shm_id < 0) {
1029  throw std::runtime_error(
1030  "failed to get an valid shm ID w/ given shm key of the schema");
1031  }
1032  if (-1 == shmctl(shm_id, IPC_RMID, 0)) {
1033  throw std::runtime_error("failed to deallocate Arrow schema on errorno(" +
1034  std::to_string(errno) + ")");
1035  }
1036  }
1037 
1039  CHECK_EQ(sizeof(key_t), result.df_handle.size());
1040  const key_t& df_key = *(key_t*)(&result.df_handle[0]);
1041  auto shm_id = shmget(df_key, result.df_size, 0666);
1042  if (shm_id < 0) {
1043  throw std::runtime_error(
1044  "failed to get an valid shm ID w/ given shm key of the data");
1045  }
1046  if (-1 == shmctl(shm_id, IPC_RMID, 0)) {
1047  throw std::runtime_error("failed to deallocate Arrow data frame");
1048  }
1049  }
1050  // CUDA buffers become owned by the caller, and will automatically be freed
1051  // TODO: What if the client never takes ownership of the result? we may want to
1052  // establish a check to see if the GPU buffer still exists, and then free it.
1053 #endif
1054 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
std::vector< char > sm_handle
std::string to_string(char const *&&v)
std::vector< char > df_handle
int64_t sm_size
int64_t df_size

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool ArrowResultSet::definitelyHasNoRows ( ) const

Definition at line 209 of file ArrowResultSet.cpp.

References rowCount().

209  {
210  return !rowCount();
211 }
size_t rowCount() const

+ Here is the call graph for this function:

SQLTypeInfo ArrowResultSet::getColType ( const size_t  col_idx) const

Definition at line 204 of file ArrowResultSet.cpp.

References CHECK_LT, and column_metainfo_.

204  {
205  CHECK_LT(col_idx, column_metainfo_.size());
206  return column_metainfo_[col_idx].get_type_info();
207 }
#define CHECK_LT(x, y)
Definition: Logger.h:221
std::vector< TargetMetaInfo > column_metainfo_
std::vector< TargetValue > ArrowResultSet::getNextRow ( const bool  translate_strings,
const bool  decimal_to_double 
) const

Definition at line 189 of file ArrowResultSet.cpp.

References CHECK_LT, crt_row_idx_, and rowCount().

190  {
191  if (crt_row_idx_ == rowCount()) {
192  return {};
193  }
195  auto row = getRowAt(crt_row_idx_);
196  ++crt_row_idx_;
197  return row;
198 }
size_t rowCount() const
#define CHECK_LT(x, y)
Definition: Logger.h:221

+ Here is the call graph for this function:

void ArrowResultSet::resultSetArrowLoopback ( const ExecutorDeviceType  device_type = ExecutorDeviceType::CPU)
private

Definition at line 217 of file ArrowResultSet.cpp.

References ARROW_ASSIGN_OR_THROW, ARROW_THROW_NOT_OK, CHECK_EQ, device_type, dictionary_memo_, i, record_batch_, results_, rows_, targets_meta_, to_string(), and WIRE.

Referenced by ArrowResultSet().

217  {
218  std::vector<std::string> col_names;
219 
220  if (!targets_meta_.empty()) {
221  for (auto& meta : targets_meta_) {
222  col_names.push_back(meta.get_resname());
223  }
224  } else {
225  for (unsigned int i = 0; i < rows_->colCount(); i++) {
226  col_names.push_back("col_" + std::to_string(i));
227  }
228  }
229 
230  // We convert the given rows to arrow, which gets serialized
231  // into a buffer by Arrow Wire.
232  auto converter = ArrowResultSetConverter(rows_, col_names, -1);
233  converter.transport_method_ = ArrowTransport::WIRE;
234  converter.device_type_ = device_type;
235 
236  // Lifetime of the result buffer is that of ArrowResultSet
237  results_ = std::make_shared<ArrowResult>(converter.getArrowResult());
238 
239  // Create a reader for reading back serialized
240  arrow::io::BufferReader reader(
241  reinterpret_cast<const uint8_t*>(results_->df_buffer.data()), results_->df_size);
242 
243  ARROW_ASSIGN_OR_THROW(auto batch_reader,
244  arrow::ipc::RecordBatchStreamReader::Open(&reader));
245 
246  ARROW_THROW_NOT_OK(batch_reader->ReadNext(&record_batch_));
247 
248  // Collect dictionaries from the record batch into the dictionary memo.
250  arrow::ipc::internal::CollectDictionaries(*record_batch_, &dictionary_memo_));
251 
252  CHECK_EQ(record_batch_->schema()->num_fields(), record_batch_->num_columns());
253 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
#define ARROW_THROW_NOT_OK(s)
Definition: ArrowUtil.h:36
std::shared_ptr< ArrowResult > results_
std::shared_ptr< ResultSet > rows_
#define ARROW_ASSIGN_OR_THROW(lhs, rexpr)
Definition: ArrowUtil.h:60
arrow::ipc::DictionaryMemo dictionary_memo_
std::string to_string(char const *&&v)
std::shared_ptr< arrow::RecordBatch > record_batch_
std::vector< TargetMetaInfo > targets_meta_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t ArrowResultSet::rowCount ( ) const

Definition at line 213 of file ArrowResultSet.cpp.

References record_batch_.

Referenced by definitelyHasNoRows(), and getNextRow().

213  {
214  return record_batch_->num_rows();
215 }
std::shared_ptr< arrow::RecordBatch > record_batch_

+ Here is the caller graph for this function:

Member Data Documentation

std::vector<TargetMetaInfo> ArrowResultSet::column_metainfo_
private

Definition at line 161 of file ArrowResultSet.h.

Referenced by ArrowResultSet(), colCount(), and getColType().

std::vector<std::shared_ptr<arrow::Array> > ArrowResultSet::columns_
private

Definition at line 159 of file ArrowResultSet.h.

Referenced by ArrowResultSet().

size_t ArrowResultSet::crt_row_idx_
mutableprivate

Definition at line 160 of file ArrowResultSet.h.

Referenced by getNextRow().

ArrowResultSet::device_type
Initial value:
{}
ArrowResultSetRowIterator rowIterator(size_t from_index,
bool translate_strings,
bool decimal_to_double) const {
for (size_t i = 0; i < from_index; i++) {
++iter;
}
return iter;
}
ArrowResultSetRowIterator rowIterator(bool translate_strings,
bool decimal_to_double) const {
return rowIterator(0, translate_strings, decimal_to_double);
}
std::vector<TargetValue> getRowAt(const size_t index) const

Definition at line 105 of file ArrowResultSet.h.

Referenced by resultSetArrowLoopback().

arrow::ipc::DictionaryMemo ArrowResultSet::dictionary_memo_
private

Definition at line 155 of file ArrowResultSet.h.

Referenced by resultSetArrowLoopback().

std::shared_ptr<arrow::RecordBatch> ArrowResultSet::record_batch_
private

Definition at line 154 of file ArrowResultSet.h.

Referenced by ArrowResultSet(), resultSetArrowLoopback(), and rowCount().

std::shared_ptr<ArrowResult> ArrowResultSet::results_
private

Definition at line 151 of file ArrowResultSet.h.

Referenced by resultSetArrowLoopback().

std::shared_ptr<ResultSet> ArrowResultSet::rows_
private

Definition at line 152 of file ArrowResultSet.h.

Referenced by resultSetArrowLoopback().

std::vector<TargetMetaInfo> ArrowResultSet::targets_meta_
private

Definition at line 153 of file ArrowResultSet.h.

Referenced by resultSetArrowLoopback().


The documentation for this class was generated from the following files: