OmniSciDB  340b00dbf6
 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);
101  ArrowResultSet(const std::shared_ptr<ResultSet>& rows) : ArrowResultSet(rows, {}) {}
102 
103  ArrowResultSetRowIterator rowIterator(size_t from_index,
104  bool translate_strings,
105  bool decimal_to_double) const {
106  ArrowResultSetRowIterator iter(this);
107  for (size_t i = 0; i < from_index; i++) {
108  ++iter;
109  }
110 
111  return iter;
112  }
113 
114  ArrowResultSetRowIterator rowIterator(bool translate_strings,
115  bool decimal_to_double) const {
116  return rowIterator(0, translate_strings, decimal_to_double);
117  }
118 
119  std::vector<TargetValue> getRowAt(const size_t index) const;
120 
121  std::vector<TargetValue> getNextRow(const bool translate_strings,
122  const bool decimal_to_double) const;
123 
124  size_t colCount() const;
125 
126  SQLTypeInfo getColType(const size_t col_idx) const;
127 
128  bool definitelyHasNoRows() const;
129 
130  size_t rowCount() const;
131 
132  static void deallocateArrowResultBuffer(
133  const ArrowResult& result,
134  const ExecutorDeviceType device_type,
135  const size_t device_id,
136  std::shared_ptr<Data_Namespace::DataMgr>& data_mgr);
137 
138  private:
139  void resultSetArrowLoopback();
140  template <typename Type, typename ArrayType>
141  void appendValue(std::vector<TargetValue>& row,
142  const arrow::Array& column,
143  const Type null_val,
144  const size_t idx) const;
145 
146  std::shared_ptr<ResultSet> rows_;
147  std::vector<TargetMetaInfo> targets_meta_;
148  std::shared_ptr<arrow::RecordBatch> record_batch_;
149  arrow::ipc::DictionaryMemo dictionary_memo_;
150 
151  // Boxed arrays from the record batch. The result of RecordBatch::column is
152  // temporary, so we cache these for better performance
153  std::vector<std::shared_ptr<arrow::Array>> columns_;
154  mutable size_t crt_row_idx_;
155  std::vector<TargetMetaInfo> column_metainfo_;
156 };
157 
159  return result_set_->getRowAt(crt_row_idx_);
160 }
161 
162 class ExecutionResult;
163 
164 // Take results from the executor, serializes them to Arrow and then deserialize
165 // them to ArrowResultSet, which can then be used by the existing test framework.
166 std::unique_ptr<ArrowResultSet> result_set_arrow_loopback(const ExecutionResult& results);
167 
168 // QUERYENGINE_// Take results from the executor, serializes them to Arrow and then
169 // deserialize them to ArrowResultSet, which can then be used by the existing test
170 // framework.
171 std::unique_ptr<ArrowResultSet> result_set_arrow_loopback(
172  const ExecutionResult* results,
173  const std::shared_ptr<ResultSet>& rows);
174 
176  public:
177  ArrowResultSetConverter(const std::shared_ptr<ResultSet>& results,
178  const std::shared_ptr<Data_Namespace::DataMgr> data_mgr,
179  const ExecutorDeviceType device_type,
180  const int32_t device_id,
181  const std::vector<std::string>& col_names,
182  const int32_t first_n,
183  const ArrowTransport transport_method)
184  : results_(results)
185  , data_mgr_(data_mgr)
186  , device_type_(device_type)
187  , device_id_(device_id)
188  , col_names_(col_names)
189  , top_n_(first_n)
190  , transport_method_(transport_method) {}
191 
192  ArrowResult getArrowResult() const;
193 
194  // TODO(adb): Proper namespacing for this set of functionality. For now, make this
195  // public and leverage the converter class as namespace
196  struct ColumnBuilder {
197  std::shared_ptr<arrow::Field> field;
198  std::unique_ptr<arrow::ArrayBuilder> builder;
201  };
202 
203  private:
204  ArrowResultSetConverter(const std::shared_ptr<ResultSet>& results,
205  const std::vector<std::string>& col_names,
206  const int32_t first_n)
207  : results_(results), col_names_(col_names), top_n_(first_n) {}
208 
209  std::shared_ptr<arrow::RecordBatch> convertToArrow() const;
210 
211  std::shared_ptr<arrow::RecordBatch> getArrowBatch(
212  const std::shared_ptr<arrow::Schema>& schema) const;
213 
214  std::shared_ptr<arrow::Field> makeField(const std::string name,
215  const SQLTypeInfo& target_type) const;
216 
218  std::shared_ptr<arrow::Buffer> schema;
219  std::shared_ptr<arrow::Buffer> records;
220  };
221  SerializedArrowOutput getSerializedArrowOutput(arrow::ipc::DictionaryMemo* memo) const;
222 
223  void initializeColumnBuilder(ColumnBuilder& column_builder,
224  const SQLTypeInfo& col_type,
225  const std::shared_ptr<arrow::Field>& field) const;
226 
227  void append(ColumnBuilder& column_builder,
228  const ValueArray& values,
229  const std::shared_ptr<std::vector<bool>>& is_valid) const;
230 
231  inline std::shared_ptr<arrow::Array> finishColumnBuilder(
232  ColumnBuilder& column_builder) const;
233 
234  std::shared_ptr<ResultSet> results_;
235  std::shared_ptr<Data_Namespace::DataMgr> data_mgr_ = nullptr;
237  int32_t device_id_ = 0;
238  std::vector<std::string> col_names_;
239  int32_t top_n_;
241 
242  // Here we hold temporary buffers for arrow arrays when
243  // array builders are not used.
244  mutable std::vector<std::unique_ptr<int8_t[]>> values_;
245  mutable std::vector<std::unique_ptr<uint8_t[]>> is_valid_;
246 
247  friend class ArrowResultSet;
248 };
249 
250 template <typename T>
251 constexpr auto scale_epoch_values() {
252  return std::is_same<T, arrow::Date32Builder>::value ||
253  std::is_same<T, arrow::Date64Builder>::value;
254 }
std::unique_ptr< arrow::ArrayBuilder > builder
ArrowResultSetRowIterator & operator++(void)
SQLTypes
Definition: sqltypes.h:40
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< ResultSet > rows_
size_t rowCount() const
SQLTypeInfo getColType(const size_t col_idx) const
ArrowResultSet(const std::shared_ptr< ResultSet > &rows)
ArrowTransport
std::shared_ptr< Data_Namespace::DataMgr > data_mgr_
std::shared_ptr< arrow::Array > finishColumnBuilder(ColumnBuilder &column_builder) const
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
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
std::vector< std::unique_ptr< int8_t[]> > values_
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
std::vector< TargetValue > getNextRow(const bool translate_strings, const bool decimal_to_double) const
SerializedArrowOutput getSerializedArrowOutput(arrow::ipc::DictionaryMemo *memo) 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
void resultSetArrowLoopback()
std::vector< std::unique_ptr< uint8_t[]> > is_valid_
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)
std::shared_ptr< arrow::RecordBatch > record_batch_
string name
Definition: setup.py:35
const ArrowResultSet * result_set_
ArrowResultSet(const std::shared_ptr< ResultSet > &rows, const std::vector< TargetMetaInfo > &targets_meta)
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