OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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<float>,
43  std::vector<double>,
44  std::vector<std::string>>;
45 
46 class ArrowResultSet;
47 
49  public:
50  using value_type = std::vector<TargetValue>;
51  using difference_type = std::ptrdiff_t;
52  using pointer = std::vector<TargetValue>*;
53  using reference = std::vector<TargetValue>&;
54  using iterator_category = std::input_iterator_tag;
55 
56  bool operator==(const ArrowResultSetRowIterator& other) const {
57  return result_set_ == other.result_set_ && crt_row_idx_ == other.crt_row_idx_;
58  }
59  bool operator!=(const ArrowResultSetRowIterator& other) const {
60  return !(*this == other);
61  }
62 
63  inline value_type operator*() const;
65  crt_row_idx_++;
66  return *this;
67  }
69  ArrowResultSetRowIterator iter(*this);
70  ++(*this);
71  return iter;
72  }
73 
74  private:
76  size_t crt_row_idx_;
77 
80 
81  friend class ArrowResultSet;
82 };
83 
84 enum class ArrowTransport { SHARED_MEMORY = 0, WIRE = 1 };
85 
86 struct ArrowResult {
87  std::vector<char> sm_handle;
88  int64_t sm_size;
89  std::vector<char> df_handle;
90  int64_t df_size;
91  std::string serialized_cuda_handle; // Only for GPU memory deallocation
92  std::vector<char> df_buffer; // Only present when transport is WIRE
93 };
94 
95 // Expose Arrow buffers as a subset of the ResultSet interface
96 // to make it work within the existing execution test framework.
98  public:
99  ArrowResultSet(const std::shared_ptr<ResultSet>& rows,
100  const std::vector<TargetMetaInfo>& targets_meta,
102  ArrowResultSet(const std::shared_ptr<ResultSet>& rows,
104  : ArrowResultSet(rows, {}, device_type) {}
105 
106  ArrowResultSetRowIterator rowIterator(size_t from_index,
107  bool translate_strings,
108  bool decimal_to_double) const {
109  ArrowResultSetRowIterator iter(this);
110  for (size_t i = 0; i < from_index; i++) {
111  ++iter;
112  }
113 
114  return iter;
115  }
116 
117  ArrowResultSetRowIterator rowIterator(bool translate_strings,
118  bool decimal_to_double) const {
119  return rowIterator(0, translate_strings, decimal_to_double);
120  }
121 
122  std::vector<TargetValue> getRowAt(const size_t index) const;
123 
124  std::vector<TargetValue> getNextRow(const bool translate_strings,
125  const bool decimal_to_double) const;
126 
127  size_t colCount() const;
128 
129  SQLTypeInfo getColType(const size_t col_idx) const;
130 
131  bool definitelyHasNoRows() const;
132 
133  size_t rowCount() const;
134 
135  static void deallocateArrowResultBuffer(
136  const ArrowResult& result,
138  const size_t device_id,
139  std::shared_ptr<Data_Namespace::DataMgr>& data_mgr);
140 
141  private:
144  template <typename Type, typename ArrayType>
145  void appendValue(std::vector<TargetValue>& row,
146  const arrow::Array& column,
147  const Type null_val,
148  const size_t idx) const;
149 
150  std::shared_ptr<ArrowResult> results_;
151  std::shared_ptr<ResultSet> rows_;
152  std::vector<TargetMetaInfo> targets_meta_;
153  std::shared_ptr<arrow::RecordBatch> record_batch_;
154  arrow::ipc::DictionaryMemo dictionary_memo_;
155 
156  // Boxed arrays from the record batch. The result of RecordBatch::column is
157  // temporary, so we cache these for better performance
158  std::vector<std::shared_ptr<arrow::Array>> columns_;
159  mutable size_t crt_row_idx_;
160  std::vector<TargetMetaInfo> column_metainfo_;
161 };
162 
164  return result_set_->getRowAt(crt_row_idx_);
165 }
166 
167 class ExecutionResult;
168 
169 // Take results from the executor, serializes them to Arrow and then deserialize
170 // them to ArrowResultSet, which can then be used by the existing test framework.
171 std::unique_ptr<ArrowResultSet> result_set_arrow_loopback(const ExecutionResult& results);
172 
173 // QUERYENGINE_// Take results from the executor, serializes them to Arrow and then
174 // deserialize them to ArrowResultSet, which can then be used by the existing test
175 // framework.
176 std::unique_ptr<ArrowResultSet> result_set_arrow_loopback(
177  const ExecutionResult* results,
178  const std::shared_ptr<ResultSet>& rows,
179  const ExecutorDeviceType device_type = ExecutorDeviceType::CPU);
180 
182  public:
183  ArrowResultSetConverter(const std::shared_ptr<ResultSet>& results,
184  const std::shared_ptr<Data_Namespace::DataMgr> data_mgr,
185  const ExecutorDeviceType device_type,
186  const int32_t device_id,
187  const std::vector<std::string>& col_names,
188  const int32_t first_n,
189  const ArrowTransport transport_method)
190  : results_(results)
191  , data_mgr_(data_mgr)
192  , device_type_(device_type)
193  , device_id_(device_id)
194  , col_names_(col_names)
195  , top_n_(first_n)
196  , transport_method_(transport_method) {}
197 
198  ArrowResult getArrowResult() const;
199 
200  // TODO(adb): Proper namespacing for this set of functionality. For now, make this
201  // public and leverage the converter class as namespace
202  struct ColumnBuilder {
203  std::shared_ptr<arrow::Field> field;
204  std::unique_ptr<arrow::ArrayBuilder> builder;
207  };
208 
209  ArrowResultSetConverter(const std::shared_ptr<ResultSet>& results,
210  const std::vector<std::string>& col_names,
211  const int32_t first_n)
212  : results_(results), col_names_(col_names), top_n_(first_n) {}
213 
214  std::shared_ptr<arrow::RecordBatch> convertToArrow() const;
215 
216  private:
217  std::shared_ptr<arrow::RecordBatch> getArrowBatch(
218  const std::shared_ptr<arrow::Schema>& schema) const;
219 
220  std::shared_ptr<arrow::Field> makeField(const std::string name,
221  const SQLTypeInfo& target_type) const;
222 
224  std::shared_ptr<arrow::Buffer> schema;
225  std::shared_ptr<arrow::Buffer> records;
226  };
228  arrow::ipc::DictionaryFieldMapper* mapper) const;
229 
230  void initializeColumnBuilder(ColumnBuilder& column_builder,
231  const SQLTypeInfo& col_type,
232  const std::shared_ptr<arrow::Field>& field) const;
233 
234  void append(ColumnBuilder& column_builder,
235  const ValueArray& values,
236  const std::shared_ptr<std::vector<bool>>& is_valid) const;
237 
238  inline std::shared_ptr<arrow::Array> finishColumnBuilder(
239  ColumnBuilder& column_builder) const;
240 
241  std::shared_ptr<ResultSet> results_;
242  std::shared_ptr<Data_Namespace::DataMgr> data_mgr_ = nullptr;
244  int32_t device_id_ = 0;
245  std::vector<std::string> col_names_;
246  int32_t top_n_;
248 
249  friend class ArrowResultSet;
250 };
251 
252 template <typename T>
253 constexpr auto scale_epoch_values() {
254  return std::is_same<T, arrow::Date32Builder>::value ||
255  std::is_same<T, arrow::Date64Builder>::value;
256 }
std::unique_ptr< arrow::ArrayBuilder > builder
ArrowResultSetRowIterator & operator++(void)
SQLTypes
Definition: sqltypes.h:37
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_
boost::variant< std::vector< bool >, std::vector< int8_t >, std::vector< int16_t >, std::vector< int32_t >, std::vector< int64_t >, std::vector< float >, std::vector< double >, std::vector< std::string >> ValueArray
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
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