OmniSciDB  5ade3759e0
ArrowResultSet Class Reference

#include <ArrowResultSet.h>

Public Member Functions

 ArrowResultSet (const std::shared_ptr< ResultSet > &rows, const std::vector< TargetMetaInfo > &targets_meta)
 
 ArrowResultSet (const std::shared_ptr< ResultSet > &rows)
 
ArrowResultSetRowIterator rowIterator (size_t from_index, bool translate_strings, bool decimal_to_double) const
 
ArrowResultSetRowIterator rowIterator (bool translate_strings, bool decimal_to_double) const
 
std::vector< TargetValuegetRowAt (const size_t index) const
 
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)
 

Private Member Functions

void resultSetArrowLoopback ()
 
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< ResultSetrows_
 
std::vector< TargetMetaInfotargets_meta_
 
std::shared_ptr< arrow::RecordBatch > record_batch_
 
std::vector< std::shared_ptr< arrow::Array > > columns_
 
size_t crt_row_idx_
 
std::vector< TargetMetaInfocolumn_metainfo_
 

Detailed Description

Definition at line 89 of file ArrowResultSet.h.

Constructor & Destructor Documentation

◆ ArrowResultSet() [1/2]

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

Definition at line 71 of file ArrowResultSet.cpp.

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

73  : rows_(rows), targets_meta_(targets_meta), crt_row_idx_(0) {
75  auto schema = record_batch_->schema();
76  for (int i = 0; i < schema->num_fields(); ++i) {
77  std::shared_ptr<arrow::Field> field = schema->field(i);
78  SQLTypeInfo type_info = type_from_arrow_field(*schema->field(i));
79  column_metainfo_.emplace_back(field->name(), type_info);
80  columns_.emplace_back(record_batch_->column(i));
81  }
82 }
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()
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() [2/2]

ArrowResultSet::ArrowResultSet ( const std::shared_ptr< ResultSet > &  rows)
inline

Definition at line 93 of file ArrowResultSet.h.

93 : ArrowResultSet(rows, {}) {}
ArrowResultSet(const std::shared_ptr< ResultSet > &rows, const std::vector< TargetMetaInfo > &targets_meta)

Member Function Documentation

◆ appendValue()

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 85 of file ArrowResultSet.cpp.

88  {
89  const auto& col = static_cast<const ArrayType&>(column);
90  row.emplace_back(col.IsNull(idx) ? null_val : static_cast<Type>(col.Value(idx)));
91 }

◆ colCount()

size_t ArrowResultSet::colCount ( ) const

Definition at line 190 of file ArrowResultSet.cpp.

References column_metainfo_.

190  {
191  return column_metainfo_.size();
192 }
std::vector< TargetMetaInfo > column_metainfo_

◆ deallocateArrowResultBuffer()

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 545 of file ArrowResultSetConverter.cpp.

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

Referenced by MapDHandler::deallocate_df().

549  {
550  // Remove shared memory on sysmem
551  CHECK_EQ(sizeof(key_t), result.sm_handle.size());
552  const key_t& schema_key = *(key_t*)(&result.sm_handle[0]);
553  auto shm_id = shmget(schema_key, result.sm_size, 0666);
554  if (shm_id < 0) {
555  throw std::runtime_error(
556  "failed to get an valid shm ID w/ given shm key of the schema");
557  }
558  if (-1 == shmctl(shm_id, IPC_RMID, 0)) {
559  throw std::runtime_error("failed to deallocate Arrow schema on errorno(" +
560  std::to_string(errno) + ")");
561  }
562 
563  if (device_type == ExecutorDeviceType::CPU) {
564  CHECK_EQ(sizeof(key_t), result.df_handle.size());
565  const key_t& df_key = *(key_t*)(&result.df_handle[0]);
566  auto shm_id = shmget(df_key, result.df_size, 0666);
567  if (shm_id < 0) {
568  throw std::runtime_error(
569  "failed to get an valid shm ID w/ given shm key of the data");
570  }
571  if (-1 == shmctl(shm_id, IPC_RMID, 0)) {
572  throw std::runtime_error("failed to deallocate Arrow data frame");
573  }
574  return;
575  }
576 
577  CHECK(device_type == ExecutorDeviceType::GPU);
578  if (!result.df_dev_ptr) {
579  throw std::runtime_error("null pointer to data frame on device");
580  }
581 
582  data_mgr->getCudaMgr()->freeDeviceMem(result.df_dev_ptr);
583 }
#define CHECK_EQ(x, y)
Definition: Logger.h:195
std::vector< char > sm_handle
std::string to_string(char const *&&v)
int8_t * df_dev_ptr
std::vector< char > df_handle
int64_t sm_size
int64_t df_size
#define CHECK(condition)
Definition: Logger.h:187
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ definitelyHasNoRows()

bool ArrowResultSet::definitelyHasNoRows ( ) const

Definition at line 199 of file ArrowResultSet.cpp.

References rowCount().

199  {
200  return !rowCount();
201 }
size_t rowCount() const
+ Here is the call graph for this function:

◆ getColType()

SQLTypeInfo ArrowResultSet::getColType ( const size_t  col_idx) const

Definition at line 194 of file ArrowResultSet.cpp.

References CHECK_LT, and column_metainfo_.

Referenced by getRowAt().

194  {
195  CHECK_LT(col_idx, column_metainfo_.size());
196  return column_metainfo_[col_idx].get_type_info();
197 }
#define CHECK_LT(x, y)
Definition: Logger.h:197
std::vector< TargetMetaInfo > column_metainfo_
+ Here is the caller graph for this function:

◆ getNextRow()

std::vector< TargetValue > ArrowResultSet::getNextRow ( const bool  translate_strings,
const bool  decimal_to_double 
) const

Definition at line 179 of file ArrowResultSet.cpp.

References CHECK_LT, crt_row_idx_, getRowAt(), and rowCount().

180  {
181  if (crt_row_idx_ == rowCount()) {
182  return {};
183  }
185  auto row = getRowAt(crt_row_idx_);
186  ++crt_row_idx_;
187  return row;
188 }
std::vector< TargetValue > getRowAt(const size_t index) const
#define CHECK_LT(x, y)
Definition: Logger.h:197
size_t rowCount() const
+ Here is the call graph for this function:

◆ getRowAt()

std::vector< TargetValue > ArrowResultSet::getRowAt ( const size_t  index) const

Definition at line 93 of file ArrowResultSet.cpp.

References CHECK, CHECK_EQ, CHECK_LT, columns_, getColType(), inline_fp_null_value< double >(), inline_fp_null_value< float >(), inline_int_null_val(), kBIGINT, kDATE, kDOUBLE, kENCODING_DICT, kFLOAT, kINT, kSMALLINT, kTEXT, kTIME, kTIMESTAMP, record_batch_, and rowCount().

Referenced by getNextRow(), and ArrowResultSetRowIterator::operator*().

93  {
94  if (index >= rowCount()) {
95  return {};
96  }
97 
98  CHECK_LT(index, rowCount());
99  std::vector<TargetValue> row;
100  for (int i = 0; i < record_batch_->num_columns(); ++i) {
101  const auto& column = *columns_[i];
102  const auto& column_typeinfo = getColType(i);
103  switch (column_typeinfo.get_type()) {
104  case kSMALLINT: {
105  CHECK_EQ(arrow::Type::INT16, column.type_id());
106  appendValue<int64_t, arrow::Int16Array>(
107  row, column, inline_int_null_val(column_typeinfo), index);
108  break;
109  }
110  case kINT: {
111  CHECK_EQ(arrow::Type::INT32, column.type_id());
112  appendValue<int64_t, arrow::Int32Array>(
113  row, column, inline_int_null_val(column_typeinfo), index);
114  break;
115  }
116  case kBIGINT: {
117  CHECK_EQ(arrow::Type::INT64, column.type_id());
118  appendValue<int64_t, arrow::Int64Array>(
119  row, column, inline_int_null_val(column_typeinfo), index);
120  break;
121  }
122  case kFLOAT: {
123  CHECK_EQ(arrow::Type::FLOAT, column.type_id());
124  appendValue<float, arrow::FloatArray>(
125  row, column, inline_fp_null_value<float>(), index);
126  break;
127  }
128  case kDOUBLE: {
129  CHECK_EQ(arrow::Type::DOUBLE, column.type_id());
130  appendValue<double, arrow::DoubleArray>(
131  row, column, inline_fp_null_value<double>(), index);
132  break;
133  }
134  case kTEXT: {
135  CHECK_EQ(kENCODING_DICT, column_typeinfo.get_compression());
136  CHECK_EQ(arrow::Type::DICTIONARY, column.type_id());
137  const auto& dict_column = static_cast<const arrow::DictionaryArray&>(column);
138  if (dict_column.IsNull(index)) {
139  row.emplace_back(NullableString(nullptr));
140  } else {
141  const auto& indices =
142  static_cast<const arrow::Int32Array&>(*dict_column.indices());
143  const auto& dictionary =
144  static_cast<const arrow::StringArray&>(*dict_column.dictionary());
145  row.emplace_back(dictionary.GetString(indices.Value(index)));
146  }
147  break;
148  }
149  case kTIMESTAMP: {
150  CHECK_EQ(arrow::Type::TIMESTAMP, column.type_id());
151  appendValue<int64_t, arrow::TimestampArray>(
152  row, column, inline_int_null_val(column_typeinfo), index);
153  break;
154  }
155  case kDATE: {
156  // TODO(wamsi): constexpr?
157  CHECK(arrow::Type::DATE32 == column.type_id() ||
158  arrow::Type::DATE64 == column.type_id());
159  column_typeinfo.is_date_in_days()
160  ? appendValue<int64_t, arrow::Date32Array>(
161  row, column, inline_int_null_val(column_typeinfo), index)
162  : appendValue<int64_t, arrow::Date64Array>(
163  row, column, inline_int_null_val(column_typeinfo), index);
164  break;
165  }
166  case kTIME: {
167  CHECK_EQ(arrow::Type::TIME32, column.type_id());
168  appendValue<int64_t, arrow::Time32Array>(
169  row, column, inline_int_null_val(column_typeinfo), index);
170  break;
171  }
172  default:
173  CHECK(false);
174  }
175  }
176  return row;
177 }
#define CHECK_EQ(x, y)
Definition: Logger.h:195
Definition: sqltypes.h:51
SQLTypeInfo getColType(const size_t col_idx) const
#define CHECK_LT(x, y)
Definition: Logger.h:197
Definition: sqltypes.h:54
Definition: sqltypes.h:55
constexpr float inline_fp_null_value< float >()
constexpr double inline_fp_null_value< double >()
boost::variant< std::string, void * > NullableString
Definition: TargetValue.h:155
#define CHECK(condition)
Definition: Logger.h:187
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
Definition: sqltypes.h:47
std::shared_ptr< arrow::RecordBatch > record_batch_
size_t rowCount() const
std::vector< std::shared_ptr< arrow::Array > > columns_
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ resultSetArrowLoopback()

void ArrowResultSet::resultSetArrowLoopback ( )
private

Definition at line 207 of file ArrowResultSet.cpp.

References ARROW_THROW_NOT_OK, CHECK_EQ, record_batch_, rows_, targets_meta_, and to_string().

Referenced by ArrowResultSet().

207  {
208  std::vector<std::string> col_names;
209 
210  if (!targets_meta_.empty()) {
211  for (auto& meta : targets_meta_) {
212  col_names.push_back(meta.get_resname());
213  }
214  } else {
215  for (unsigned int i = 0; i < rows_->colCount(); i++) {
216  col_names.push_back("col_" + std::to_string(i));
217  }
218  }
219  const auto converter = ArrowResultSetConverter(rows_, col_names, -1);
220  const auto serialized_arrow_output = converter.getSerializedArrowOutput();
221 
222  arrow::io::BufferReader schema_reader(serialized_arrow_output.schema);
223 
224  std::shared_ptr<arrow::Schema> schema;
225  ARROW_THROW_NOT_OK(arrow::ipc::ReadSchema(&schema_reader, &schema));
226 
227  arrow::io::BufferReader records_reader(serialized_arrow_output.records);
229  arrow::ipc::ReadRecordBatch(schema, &records_reader, &record_batch_));
230 
231  CHECK_EQ(schema->num_fields(), record_batch_->num_columns());
232 }
#define CHECK_EQ(x, y)
Definition: Logger.h:195
#define ARROW_THROW_NOT_OK(s)
Definition: ArrowUtil.h:28
std::shared_ptr< ResultSet > rows_
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:

◆ rowCount()

size_t ArrowResultSet::rowCount ( ) const

Definition at line 203 of file ArrowResultSet.cpp.

References record_batch_.

Referenced by definitelyHasNoRows(), getNextRow(), and getRowAt().

203  {
204  return record_batch_->num_rows();
205 }
std::shared_ptr< arrow::RecordBatch > record_batch_
+ Here is the caller graph for this function:

◆ rowIterator() [1/2]

ArrowResultSetRowIterator ArrowResultSet::rowIterator ( size_t  from_index,
bool  translate_strings,
bool  decimal_to_double 
) const
inline

Definition at line 95 of file ArrowResultSet.h.

97  {
98  ArrowResultSetRowIterator iter(this);
99  for (size_t i = 0; i < from_index; i++) {
100  ++iter;
101  }
102 
103  return iter;
104  }

◆ rowIterator() [2/2]

ArrowResultSetRowIterator ArrowResultSet::rowIterator ( bool  translate_strings,
bool  decimal_to_double 
) const
inline

Definition at line 106 of file ArrowResultSet.h.

References Experimental::Array, anonymous_namespace{TypedDataAccessors.h}::decimal_to_double(), and run-benchmark-import::result.

107  {
108  return rowIterator(0, translate_strings, decimal_to_double);
109  }
double decimal_to_double(const SQLTypeInfo &otype, int64_t oval)
ArrowResultSetRowIterator rowIterator(size_t from_index, bool translate_strings, bool decimal_to_double) const
+ Here is the call graph for this function:

Member Data Documentation

◆ column_metainfo_

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

Definition at line 146 of file ArrowResultSet.h.

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

◆ columns_

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

Definition at line 144 of file ArrowResultSet.h.

Referenced by ArrowResultSet(), and getRowAt().

◆ crt_row_idx_

size_t ArrowResultSet::crt_row_idx_
mutableprivate

Definition at line 145 of file ArrowResultSet.h.

Referenced by getNextRow().

◆ record_batch_

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

Definition at line 140 of file ArrowResultSet.h.

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

◆ rows_

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

Definition at line 138 of file ArrowResultSet.h.

Referenced by resultSetArrowLoopback().

◆ targets_meta_

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

Definition at line 139 of file ArrowResultSet.h.

Referenced by resultSetArrowLoopback().


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