OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ArrowResultSet.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "CompilationOptions.h"
20 #include "DataMgr/DataMgr.h"
22 #include "ResultSet.h"
23 #include "TargetMetaInfo.h"
24 #include "TargetValue.h"
25 
26 #include <type_traits>
27 
28 #include "arrow/api.h"
29 #include "arrow/ipc/api.h"
30 #ifdef HAVE_CUDA
31 #include <arrow/gpu/cuda_api.h>
32 #endif // HAVE_CUDA
33 
34 static_assert(ARROW_VERSION >= 16000, "Apache Arrow v0.16.0 or above is required.");
35 
36 // TODO(wamsi): ValueArray is not optimal. Remove it and inherrit from base vector class.
37 using ValueArray = boost::variant<std::vector<bool>,
38  std::vector<int8_t>,
39  std::vector<int16_t>,
40  std::vector<int32_t>,
41  std::vector<int64_t>,
42  std::vector<arrow::Decimal128>,
43  std::vector<float>,
44  std::vector<double>,
45  std::vector<std::string>>;
46 
47 class ArrowResultSet;
48 
50  public:
51  using value_type = std::vector<TargetValue>;
52  using difference_type = std::ptrdiff_t;
53  using pointer = std::vector<TargetValue>*;
54  using reference = std::vector<TargetValue>&;
55  using iterator_category = std::input_iterator_tag;
56 
57  bool operator==(const ArrowResultSetRowIterator& other) const {
58  return result_set_ == other.result_set_ && crt_row_idx_ == other.crt_row_idx_;
59  }
60  bool operator!=(const ArrowResultSetRowIterator& other) const {
61  return !(*this == other);
62  }
63 
64  inline value_type operator*() const;
66  crt_row_idx_++;
67  return *this;
68  }
70  ArrowResultSetRowIterator iter(*this);
71  ++(*this);
72  return iter;
73  }
74 
75  private:
77  size_t crt_row_idx_;
78 
81 
82  friend class ArrowResultSet;
83 };
84 
85 enum class ArrowTransport { SHARED_MEMORY = 0, WIRE = 1 };
86 
87 struct ArrowResult {
88  std::vector<char> sm_handle;
89  int64_t sm_size;
90  std::vector<char> df_handle;
91  int64_t df_size;
92  std::string serialized_cuda_handle; // Only for GPU memory deallocation
93  std::vector<char> df_buffer; // Only present when transport is WIRE
94 };
95 
96 // Expose Arrow buffers as a subset of the ResultSet interface
97 // to make it work within the existing execution test framework.
99  public:
100  ArrowResultSet(const std::shared_ptr<ResultSet>& rows,
101  const std::vector<TargetMetaInfo>& targets_meta,
103  ArrowResultSet(const std::shared_ptr<ResultSet>& rows,
105  : ArrowResultSet(rows, {}, device_type) {}
106 
107  ArrowResultSetRowIterator rowIterator(size_t from_index,
108  bool translate_strings,
109  bool decimal_to_double) const {
110  ArrowResultSetRowIterator iter(this);
111  for (size_t i = 0; i < from_index; i++) {
112  ++iter;
113  }
114 
115  return iter;
116  }
117 
118  ArrowResultSetRowIterator rowIterator(bool translate_strings,
119  bool decimal_to_double) const {
120  return rowIterator(0, translate_strings, decimal_to_double);
121  }
122 
123  std::vector<TargetValue> getRowAt(const size_t index) const;
124 
125  std::vector<TargetValue> getNextRow(const bool translate_strings,
126  const bool decimal_to_double) const;
127 
128  size_t colCount() const;
129 
130  SQLTypeInfo getColType(const size_t col_idx) const;
131 
132  bool definitelyHasNoRows() const;
133 
134  size_t rowCount() const;
135 
136  static void deallocateArrowResultBuffer(
137  const ArrowResult& result,
139  const size_t device_id,
140  std::shared_ptr<Data_Namespace::DataMgr>& data_mgr);
141 
142  private:
145  template <typename Type, typename ArrayType>
146  void appendValue(std::vector<TargetValue>& row,
147  const arrow::Array& column,
148  const Type null_val,
149  const size_t idx) const;
150 
151  std::shared_ptr<ArrowResult> results_;
152  std::shared_ptr<ResultSet> rows_;
153  std::vector<TargetMetaInfo> targets_meta_;
154  std::shared_ptr<arrow::RecordBatch> record_batch_;
155  arrow::ipc::DictionaryMemo dictionary_memo_;
156 
157  // Boxed arrays from the record batch. The result of RecordBatch::column is
158  // temporary, so we cache these for better performance
159  std::vector<std::shared_ptr<arrow::Array>> columns_;
160  mutable size_t crt_row_idx_;
161  std::vector<TargetMetaInfo> column_metainfo_;
162 };
163 
165  return result_set_->getRowAt(crt_row_idx_);
166 }
167 
168 class ExecutionResult;
169 
170 // Take results from the executor, serializes them to Arrow and then deserialize
171 // them to ArrowResultSet, which can then be used by the existing test framework.
172 std::unique_ptr<ArrowResultSet> result_set_arrow_loopback(const ExecutionResult& results);
173 
174 // QUERYENGINE_// Take results from the executor, serializes them to Arrow and then
175 // deserialize them to ArrowResultSet, which can then be used by the existing test
176 // framework.
177 std::unique_ptr<ArrowResultSet> result_set_arrow_loopback(
178  const ExecutionResult* results,
179  const std::shared_ptr<ResultSet>& rows,
180  const ExecutorDeviceType device_type = ExecutorDeviceType::CPU);
181 
183  public:
184  ArrowResultSetConverter(const std::shared_ptr<ResultSet>& results,
185  const std::shared_ptr<Data_Namespace::DataMgr> data_mgr,
186  const ExecutorDeviceType device_type,
187  const int32_t device_id,
188  const std::vector<std::string>& col_names,
189  const int32_t first_n,
190  const ArrowTransport transport_method)
191  : results_(results)
192  , data_mgr_(data_mgr)
193  , device_type_(device_type)
194  , device_id_(device_id)
195  , col_names_(col_names)
196  , top_n_(first_n)
197  , transport_method_(transport_method) {}
198 
199  ArrowResult getArrowResult() const;
200 
201  // TODO(adb): Proper namespacing for this set of functionality. For now, make this
202  // public and leverage the converter class as namespace
203  struct ColumnBuilder {
204  std::shared_ptr<arrow::Field> field;
205  std::unique_ptr<arrow::ArrayBuilder> builder;
208  };
209 
210  ArrowResultSetConverter(const std::shared_ptr<ResultSet>& results,
211  const std::vector<std::string>& col_names,
212  const int32_t first_n)
213  : results_(results), col_names_(col_names), top_n_(first_n) {}
214 
215  std::shared_ptr<arrow::RecordBatch> convertToArrow() const;
216 
217  private:
218  std::shared_ptr<arrow::RecordBatch> getArrowBatch(
219  const std::shared_ptr<arrow::Schema>& schema) const;
220 
221  std::shared_ptr<arrow::Field> makeField(const std::string name,
222  const SQLTypeInfo& target_type) const;
223 
225  std::shared_ptr<arrow::Buffer> schema;
226  std::shared_ptr<arrow::Buffer> records;
227  };
229  arrow::ipc::DictionaryFieldMapper* mapper) const;
230 
231  void initializeColumnBuilder(ColumnBuilder& column_builder,
232  const SQLTypeInfo& col_type,
233  const std::shared_ptr<arrow::Field>& field) const;
234 
235  void append(ColumnBuilder& column_builder,
236  const ValueArray& values,
237  const std::shared_ptr<std::vector<bool>>& is_valid) const;
238 
239  inline std::shared_ptr<arrow::Array> finishColumnBuilder(
240  ColumnBuilder& column_builder) const;
241 
242  std::shared_ptr<ResultSet> results_;
243  std::shared_ptr<Data_Namespace::DataMgr> data_mgr_ = nullptr;
245  int32_t device_id_ = 0;
246  std::vector<std::string> col_names_;
247  int32_t top_n_;
249 
250  friend class ArrowResultSet;
251 };
252 
253 template <typename T>
254 constexpr auto scale_epoch_values() {
255  return std::is_same<T, arrow::Date32Builder>::value ||
256  std::is_same<T, arrow::Date64Builder>::value;
257 }
std::unique_ptr< arrow::ArrayBuilder > builder
ArrowResultSetRowIterator & operator++(void)
SQLTypes
Definition: sqltypes.h:38
double decimal_to_double(const SQLTypeInfo &otype, int64_t oval)
void append(ColumnBuilder &column_builder, const ValueArray &values, const std::shared_ptr< std::vector< bool >> &is_valid) const
ArrowResult getArrowResult() const
std::vector< char > sm_handle
ExecutorDeviceType
std::shared_ptr< ArrowResult > results_
std::shared_ptr< ResultSet > rows_
size_t rowCount() const
SQLTypeInfo getColType(const size_t col_idx) const
ArrowTransport
std::shared_ptr< Data_Namespace::DataMgr > data_mgr_
std::shared_ptr< arrow::Array > finishColumnBuilder(ColumnBuilder &column_builder) const
string name
Definition: setup.in.py:72
std::shared_ptr< arrow::Field > field
arrow::ipc::DictionaryMemo dictionary_memo_
ArrowResultSet(const std::shared_ptr< ResultSet > &rows, const std::vector< TargetMetaInfo > &targets_meta, const ExecutorDeviceType device_type=ExecutorDeviceType::CPU)
ArrowTransport transport_method_
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
std::shared_ptr< arrow::RecordBatch > getArrowBatch(const std::shared_ptr< arrow::Schema > &schema) const
std::vector< std::string > col_names_
std::shared_ptr< arrow::Buffer > records
std::vector< TargetValue > & reference
bool operator==(const ArrowResultSetRowIterator &other) const
ExecutorDeviceType device_type_
std::vector< char > df_handle
std::shared_ptr< arrow::Field > makeField(const std::string name, const SQLTypeInfo &target_type) const
ArrowResultSet(const std::shared_ptr< ResultSet > &rows, const ExecutorDeviceType device_type=ExecutorDeviceType::CPU)
std::vector< TargetValue > getNextRow(const bool translate_strings, const bool decimal_to_double) const
SerializedArrowOutput getSerializedArrowOutput(arrow::ipc::DictionaryFieldMapper *mapper) const
std::vector< TargetValue > * pointer
std::ptrdiff_t difference_type
ArrowResultSetConverter(const std::shared_ptr< ResultSet > &results, const std::vector< std::string > &col_names, const int32_t first_n)
static void deallocateArrowResultBuffer(const ArrowResult &result, const ExecutorDeviceType device_type, const size_t device_id, std::shared_ptr< Data_Namespace::DataMgr > &data_mgr)
std::input_iterator_tag iterator_category
size_t colCount() const
std::shared_ptr< ResultSet > results_
std::unique_ptr< ArrowResultSet > result_set_arrow_loopback(const ExecutionResult &results)
int64_t sm_size
ArrowResultSetRowIterator(const ArrowResultSet *rs)
std::vector< TargetValue > value_type
value_type operator*() const
boost::variant< std::vector< bool >, std::vector< int8_t >, std::vector< int16_t >, std::vector< int32_t >, std::vector< int64_t >, std::vector< arrow::Decimal128 >, std::vector< float >, std::vector< double >, std::vector< std::string >> ValueArray
std::string serialized_cuda_handle
std::vector< TargetMetaInfo > column_metainfo_
int64_t df_size
bool operator!=(const ArrowResultSetRowIterator &other) const
bool definitelyHasNoRows() const
ArrowResultSetRowIterator operator++(int)
void appendValue(std::vector< TargetValue > &row, const arrow::Array &column, const Type null_val, const size_t idx) const
Basic constructors and methods of the row set interface.
void initializeColumnBuilder(ColumnBuilder &column_builder, const SQLTypeInfo &col_type, const std::shared_ptr< arrow::Field > &field) const
ArrowResultSetConverter(const std::shared_ptr< ResultSet > &results, const std::shared_ptr< Data_Namespace::DataMgr > data_mgr, const ExecutorDeviceType device_type, const int32_t device_id, const std::vector< std::string > &col_names, const int32_t first_n, const ArrowTransport transport_method)
void resultSetArrowLoopback(const ExecutorDeviceType device_type=ExecutorDeviceType::CPU)
std::shared_ptr< arrow::RecordBatch > record_batch_
const ArrowResultSet * result_set_
std::shared_ptr< arrow::Buffer > schema
std::vector< TargetMetaInfo > targets_meta_
std::vector< char > df_buffer
constexpr auto scale_epoch_values()
std::vector< std::shared_ptr< arrow::Array > > columns_
std::shared_ptr< arrow::RecordBatch > convertToArrow() const