OmniSciDB  cde582ebc3
 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 2022 HEAVY.AI, 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::vector<int8_t>>,
46  std::vector<std::vector<int16_t>>,
47  std::vector<std::vector<int32_t>>,
48  std::vector<std::vector<int64_t>>,
49  std::vector<std::vector<float>>,
50  std::vector<std::vector<double>>,
51  std::vector<std::string>>;
52 
53 template <typename T>
54 using Vec2 = std::vector<std::vector<T>>;
55 
56 class ArrowResultSet;
57 
59  public:
60  using value_type = std::vector<TargetValue>;
61  using difference_type = std::ptrdiff_t;
62  using pointer = std::vector<TargetValue>*;
63  using reference = std::vector<TargetValue>&;
64  using iterator_category = std::input_iterator_tag;
65 
66  bool operator==(const ArrowResultSetRowIterator& other) const {
67  return result_set_ == other.result_set_ && crt_row_idx_ == other.crt_row_idx_;
68  }
69  bool operator!=(const ArrowResultSetRowIterator& other) const {
70  return !(*this == other);
71  }
72 
73  inline value_type operator*() const;
75  crt_row_idx_++;
76  return *this;
77  }
79  ArrowResultSetRowIterator iter(*this);
80  ++(*this);
81  return iter;
82  }
83 
84  private:
86  size_t crt_row_idx_;
87 
90 
91  friend class ArrowResultSet;
92 };
93 
94 enum class ArrowTransport { SHARED_MEMORY = 0, WIRE = 1 };
95 
96 struct ArrowResult {
97  std::vector<char> sm_handle;
98  int64_t sm_size;
99  std::vector<char> df_handle;
100  int64_t df_size;
101  std::string serialized_cuda_handle; // Only for GPU memory deallocation
102  std::vector<char> df_buffer; // Only present when transport is WIRE
103 };
104 
105 // Expose Arrow buffers as a subset of the ResultSet interface
106 // to make it work within the existing execution test framework.
108  public:
109  ArrowResultSet(const std::shared_ptr<ResultSet>& rows,
110  const std::vector<TargetMetaInfo>& targets_meta,
112 
114  const std::shared_ptr<ResultSet>& rows,
115  const std::vector<TargetMetaInfo>& targets_meta,
117  const size_t min_result_size_for_bulk_dictionary_fetch,
118  const double max_dictionary_to_result_size_ratio_for_bulk_dictionary_fetch);
119 
120  ArrowResultSet(const std::shared_ptr<ResultSet>& rows,
121  const ExecutorDeviceType device_type = ExecutorDeviceType::CPU)
122  : ArrowResultSet(rows, {}, device_type) {}
123 
124  ArrowResultSetRowIterator rowIterator(size_t from_index,
125  bool translate_strings,
126  bool decimal_to_double) const {
127  ArrowResultSetRowIterator iter(this);
128  for (size_t i = 0; i < from_index; i++) {
129  ++iter;
130  }
131 
132  return iter;
133  }
134 
135  ArrowResultSetRowIterator rowIterator(bool translate_strings,
136  bool decimal_to_double) const {
137  return rowIterator(0, translate_strings, decimal_to_double);
138  }
139 
140  std::vector<std::string> getDictionaryStrings(const size_t col_idx) const;
141 
142  std::vector<TargetValue> getRowAt(const size_t index) const;
143 
144  std::vector<TargetValue> getNextRow(const bool translate_strings,
145  const bool decimal_to_double) const;
146 
147  size_t colCount() const;
148 
149  SQLTypeInfo getColType(const size_t col_idx) const;
150 
151  bool definitelyHasNoRows() const;
152 
153  size_t rowCount() const;
154  size_t entryCount() const;
155 
156  bool isEmpty() const;
157 
158  static void deallocateArrowResultBuffer(
159  const ArrowResult& result,
160  const ExecutorDeviceType device_type,
161  const size_t device_id,
162  std::shared_ptr<Data_Namespace::DataMgr>& data_mgr);
163 
164  private:
166  const ExecutorDeviceType device_type = ExecutorDeviceType::CPU);
167 
169  const ExecutorDeviceType device_type,
170  const size_t min_result_size_for_bulk_dictionary_fetch,
171  const double max_dictionary_to_result_size_ratio_for_bulk_dictionary_fetch);
172 
173  template <typename Type, typename ArrayType>
174  void appendValue(std::vector<TargetValue>& row,
175  const arrow::Array& column,
176  const Type null_val,
177  const size_t idx) const;
178 
179  std::shared_ptr<ArrowResult> results_;
180  std::shared_ptr<ResultSet> rows_;
181  std::vector<TargetMetaInfo> targets_meta_;
182  std::shared_ptr<arrow::RecordBatch> record_batch_;
183  arrow::ipc::DictionaryMemo dictionary_memo_;
184 
185  // Boxed arrays from the record batch. The result of RecordBatch::column is
186  // temporary, so we cache these for better performance
187  std::vector<std::shared_ptr<arrow::Array>> columns_;
188  mutable size_t crt_row_idx_;
189  std::vector<TargetMetaInfo> column_metainfo_;
190 };
191 
194 }
195 
196 class ExecutionResult;
197 
198 // The following result_set_arrow_loopback methods are used by our test
199 // framework (ExecuteTest specifically) to take results from the executor,
200 // serialize them to Arrow and then deserialize them to an ArrowResultSet,
201 // which can then be used by the test framework.
202 
203 std::unique_ptr<ArrowResultSet> result_set_arrow_loopback(const ExecutionResult& results);
204 
205 std::unique_ptr<ArrowResultSet> result_set_arrow_loopback(
206  const ExecutionResult* results,
207  const std::shared_ptr<ResultSet>& rows,
208  const ExecutorDeviceType device_type = ExecutorDeviceType::CPU);
209 
210 // This version of result_set_arrow_loopback allows setting the parameters that
211 // drive the choice between dense and sparse dictionary conversion, used for
212 // Select.ArrowDictionaries tests in ExecuteTest
213 
214 std::unique_ptr<ArrowResultSet> result_set_arrow_loopback(
215  const ExecutionResult* results,
216  const std::shared_ptr<ResultSet>& rows,
217  const ExecutorDeviceType device_type,
218  const size_t min_result_size_for_bulk_dictionary_fetch,
219  const double max_dictionary_to_result_size_ratio_for_bulk_dictionary_fetch);
220 
224  INVALID
225 };
226 
228  public:
229  static constexpr size_t default_min_result_size_for_bulk_dictionary_fetch{10000UL};
230  static constexpr double
232 
233  ArrowResultSetConverter(const std::shared_ptr<ResultSet>& results,
234  const std::shared_ptr<Data_Namespace::DataMgr> data_mgr,
235  const ExecutorDeviceType device_type,
236  const int32_t device_id,
237  const std::vector<std::string>& col_names,
238  const int32_t first_n,
239  const ArrowTransport transport_method)
240  : results_(results)
241  , data_mgr_(data_mgr)
242  , device_type_(device_type)
243  , device_id_(device_id)
244  , col_names_(col_names)
245  , top_n_(first_n)
246  , transport_method_(transport_method)
252 
254  const std::shared_ptr<ResultSet>& results,
255  const std::shared_ptr<Data_Namespace::DataMgr> data_mgr,
256  const ExecutorDeviceType device_type,
257  const int32_t device_id,
258  const std::vector<std::string>& col_names,
259  const int32_t first_n,
260  const ArrowTransport transport_method,
261  const size_t min_result_size_for_bulk_dictionary_fetch,
262  const double max_dictionary_to_result_size_ratio_for_bulk_dictionary_fetch)
263  : results_(results)
264  , data_mgr_(data_mgr)
265  , device_type_(device_type)
266  , device_id_(device_id)
267  , col_names_(col_names)
268  , top_n_(first_n)
269  , transport_method_(transport_method)
271  min_result_size_for_bulk_dictionary_fetch)
273  max_dictionary_to_result_size_ratio_for_bulk_dictionary_fetch) {}
274 
275  ArrowResult getArrowResult() const;
276 
277  // TODO(adb): Proper namespacing for this set of functionality. For now, make this
278  // public and leverage the converter class as namespace
279  struct ColumnBuilder {
280  using StrId = int32_t;
281  using ArrowStrId = int32_t;
282 
283  std::shared_ptr<arrow::Field> field;
284  std::unique_ptr<arrow::ArrayBuilder> builder;
288  std::unordered_map<StrId, ArrowStrId> string_remapping;
289  };
290 
291  ArrowResultSetConverter(const std::shared_ptr<ResultSet>& results,
292  const std::vector<std::string>& col_names,
293  const int32_t first_n)
294  : results_(results)
295  , col_names_(col_names)
296  , top_n_(first_n)
302 
304  const std::shared_ptr<ResultSet>& results,
305  const std::vector<std::string>& col_names,
306  const int32_t first_n,
307  const size_t min_result_size_for_bulk_dictionary_fetch,
308  const double max_dictionary_to_result_size_ratio_for_bulk_dictionary_fetch)
309  : results_(results)
310  , col_names_(col_names)
311  , top_n_(first_n)
313  min_result_size_for_bulk_dictionary_fetch)
315  max_dictionary_to_result_size_ratio_for_bulk_dictionary_fetch) {}
316 
317  std::shared_ptr<arrow::RecordBatch> convertToArrow() const;
318 
319  private:
320  std::shared_ptr<arrow::RecordBatch> getArrowBatch(
321  const std::shared_ptr<arrow::Schema>& schema) const;
322 
323  std::shared_ptr<arrow::Field> makeField(const std::string name,
324  const SQLTypeInfo& target_type) const;
325 
327  std::shared_ptr<arrow::Buffer> schema;
328  std::shared_ptr<arrow::Buffer> records;
329  };
331  arrow::ipc::DictionaryFieldMapper* mapper) const;
332 
333  void initializeColumnBuilder(ColumnBuilder& column_builder,
334  const SQLTypeInfo& col_type,
335  const size_t result_col_idx,
336  const std::shared_ptr<arrow::Field>& field) const;
337 
338  void append(ColumnBuilder& column_builder,
339  const ValueArray& values,
340  const std::shared_ptr<std::vector<bool>>& is_valid) const;
341 
342  inline std::shared_ptr<arrow::Array> finishColumnBuilder(
343  ColumnBuilder& column_builder) const;
344 
345  std::shared_ptr<ResultSet> results_;
346  std::shared_ptr<Data_Namespace::DataMgr> data_mgr_ = nullptr;
348  int32_t device_id_ = 0;
349  std::vector<std::string> col_names_;
350  int32_t top_n_;
354  friend class ArrowResultSet;
355 };
356 
357 template <typename T>
358 constexpr auto scale_epoch_values() {
359  return std::is_same<T, arrow::Date32Builder>::value ||
360  std::is_same<T, arrow::Date64Builder>::value;
361 }
std::vector< std::vector< T >> Vec2
const size_t min_result_size_for_bulk_dictionary_fetch_
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
void initializeColumnBuilder(ColumnBuilder &column_builder, const SQLTypeInfo &col_type, const size_t result_col_idx, const std::shared_ptr< arrow::Field > &field) const
std::shared_ptr< ArrowResult > results_
std::shared_ptr< ResultSet > rows_
size_t rowCount() const
const double max_dictionary_to_result_size_ratio_for_bulk_dictionary_fetch_
SQLTypeInfo getColType(const size_t col_idx) const
ArrowTransport
std::shared_ptr< Data_Namespace::DataMgr > data_mgr_
ArrowStringRemapMode
std::shared_ptr< arrow::Array > finishColumnBuilder(ColumnBuilder &column_builder) const
std::shared_ptr< arrow::Field > field
arrow::ipc::DictionaryMemo dictionary_memo_
High-level representation of SQL values.
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
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, const size_t min_result_size_for_bulk_dictionary_fetch, const double max_dictionary_to_result_size_ratio_for_bulk_dictionary_fetch)
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
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::vector< int8_t >>, std::vector< std::vector< int16_t >>, std::vector< std::vector< int32_t >>, std::vector< std::vector< int64_t >>, std::vector< std::vector< float >>, std::vector< std::vector< double >>, std::vector< std::string >> ValueArray
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
std::unordered_map< StrId, ArrowStrId > string_remapping
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)
ArrowResultSetConverter(const std::shared_ptr< ResultSet > &results, const std::vector< std::string > &col_names, const int32_t first_n, const size_t min_result_size_for_bulk_dictionary_fetch, const double max_dictionary_to_result_size_ratio_for_bulk_dictionary_fetch)
static constexpr size_t default_min_result_size_for_bulk_dictionary_fetch
static constexpr double default_max_dictionary_to_result_size_ratio_for_bulk_dictionary_fetch
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.
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::vector< TargetValue > getRowAt(const size_t index) const
std::shared_ptr< arrow::RecordBatch > record_batch_
string name
Definition: setup.in.py:72
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