OmniSciDB  16c4e035a1
 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  size_t entryCount() const;
136 
137  bool isEmpty() const;
138 
139  static void deallocateArrowResultBuffer(
140  const ArrowResult& result,
142  const size_t device_id,
143  std::shared_ptr<Data_Namespace::DataMgr>& data_mgr);
144 
145  private:
148  template <typename Type, typename ArrayType>
149  void appendValue(std::vector<TargetValue>& row,
150  const arrow::Array& column,
151  const Type null_val,
152  const size_t idx) const;
153 
154  std::shared_ptr<ArrowResult> results_;
155  std::shared_ptr<ResultSet> rows_;
156  std::vector<TargetMetaInfo> targets_meta_;
157  std::shared_ptr<arrow::RecordBatch> record_batch_;
158  arrow::ipc::DictionaryMemo dictionary_memo_;
159 
160  // Boxed arrays from the record batch. The result of RecordBatch::column is
161  // temporary, so we cache these for better performance
162  std::vector<std::shared_ptr<arrow::Array>> columns_;
163  mutable size_t crt_row_idx_;
164  std::vector<TargetMetaInfo> column_metainfo_;
165 };
166 
168  return result_set_->getRowAt(crt_row_idx_);
169 }
170 
171 class ExecutionResult;
172 
173 // Take results from the executor, serializes them to Arrow and then deserialize
174 // them to ArrowResultSet, which can then be used by the existing test framework.
175 std::unique_ptr<ArrowResultSet> result_set_arrow_loopback(const ExecutionResult& results);
176 
177 // QUERYENGINE_// Take results from the executor, serializes them to Arrow and then
178 // deserialize them to ArrowResultSet, which can then be used by the existing test
179 // framework.
180 std::unique_ptr<ArrowResultSet> result_set_arrow_loopback(
181  const ExecutionResult* results,
182  const std::shared_ptr<ResultSet>& rows,
183  const ExecutorDeviceType device_type = ExecutorDeviceType::CPU);
184 
186  public:
187  ArrowResultSetConverter(const std::shared_ptr<ResultSet>& results,
188  const std::shared_ptr<Data_Namespace::DataMgr> data_mgr,
189  const ExecutorDeviceType device_type,
190  const int32_t device_id,
191  const std::vector<std::string>& col_names,
192  const int32_t first_n,
193  const ArrowTransport transport_method)
194  : results_(results)
195  , data_mgr_(data_mgr)
196  , device_type_(device_type)
197  , device_id_(device_id)
198  , col_names_(col_names)
199  , top_n_(first_n)
200  , transport_method_(transport_method) {}
201 
202  ArrowResult getArrowResult() const;
203 
204  // TODO(adb): Proper namespacing for this set of functionality. For now, make this
205  // public and leverage the converter class as namespace
206  struct ColumnBuilder {
207  using TransientStrId = int32_t;
208  using ArrowStrId = int32_t;
209 
210  std::shared_ptr<arrow::Field> field;
211  std::unique_ptr<arrow::ArrayBuilder> builder;
214  std::unordered_map<TransientStrId, ArrowStrId> transient_string_remapping;
215  };
216 
217  ArrowResultSetConverter(const std::shared_ptr<ResultSet>& results,
218  const std::vector<std::string>& col_names,
219  const int32_t first_n)
220  : results_(results), col_names_(col_names), top_n_(first_n) {}
221 
222  std::shared_ptr<arrow::RecordBatch> convertToArrow() const;
223 
224  private:
225  std::shared_ptr<arrow::RecordBatch> getArrowBatch(
226  const std::shared_ptr<arrow::Schema>& schema) const;
227 
228  std::shared_ptr<arrow::Field> makeField(const std::string name,
229  const SQLTypeInfo& target_type) const;
230 
232  std::shared_ptr<arrow::Buffer> schema;
233  std::shared_ptr<arrow::Buffer> records;
234  };
236  arrow::ipc::DictionaryFieldMapper* mapper) const;
237 
238  void initializeColumnBuilder(ColumnBuilder& column_builder,
239  const SQLTypeInfo& col_type,
240  const std::shared_ptr<arrow::Field>& field) const;
241 
242  void append(ColumnBuilder& column_builder,
243  const ValueArray& values,
244  const std::shared_ptr<std::vector<bool>>& is_valid) const;
245 
246  inline std::shared_ptr<arrow::Array> finishColumnBuilder(
247  ColumnBuilder& column_builder) const;
248 
249  std::shared_ptr<ResultSet> results_;
250  std::shared_ptr<Data_Namespace::DataMgr> data_mgr_ = nullptr;
252  int32_t device_id_ = 0;
253  std::vector<std::string> col_names_;
254  int32_t top_n_;
256 
257  friend class ArrowResultSet;
258 };
259 
260 template <typename T>
261 constexpr auto scale_epoch_values() {
262  return std::is_same<T, arrow::Date32Builder>::value ||
263  std::is_same<T, arrow::Date64Builder>::value;
264 }
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_
size_t entryCount() const
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)
bool isEmpty() const
void resultSetArrowLoopback(const ExecutorDeviceType device_type=ExecutorDeviceType::CPU)
std::shared_ptr< arrow::RecordBatch > record_batch_
const ArrowResultSet * result_set_
std::unordered_map< TransientStrId, ArrowStrId > transient_string_remapping
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