OmniSciDB  eb3a3d0a03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ResultSetBuilder.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, 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 
25 #include "ResultSetBuilder.h"
26 #include "RelAlgTranslator.h"
27 
29  const std::vector<TargetInfo>& targets,
30  const ExecutorDeviceType device_type,
32  const std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner,
33  const Executor* executor) {
34  return new ResultSet(targets,
35  device_type,
36  query_mem_desc,
37  row_set_mem_owner,
38  executor ? executor->getCatalog() : nullptr,
39  executor ? executor->blockSize() : 0,
40  executor ? executor->gridSize() : 0);
41 }
42 
43 void ResultSetBuilder::addVarlenBuffer(ResultSet* result_set,
44  std::vector<std::string>& varlen_storage) {
45  CHECK(result_set->serialized_varlen_buffer_.size() == 0);
46 
47  // init with an empty vector
48  result_set->serialized_varlen_buffer_.emplace_back(std::vector<std::string>());
49 
50  // copy the values into the empty vector
51  result_set->serialized_varlen_buffer_.front().assign(varlen_storage.begin(),
52  varlen_storage.end());
53 }
54 
56  const QueryMemoryDescriptor& _query_mem_desc,
57  const std::shared_ptr<RowSetMemoryOwner> _row_set_mem_owner)
58  : query_mem_desc(_query_mem_desc), row_set_mem_owner(_row_set_mem_owner) {}
59 
61  ResultSet* st = ResultSetBuilder::makeResultSet(std::vector<TargetInfo>{},
65  nullptr);
66  return st;
67 }
68 
70  const std::vector<TargetInfo>& _targets,
71  const QueryMemoryDescriptor& _query_mem_desc,
72  const std::shared_ptr<RowSetMemoryOwner> _row_set_mem_owner)
73  : logical_values(nullptr)
74  , targets(_targets)
75  , device_type(ExecutorDeviceType::CPU)
76  , query_mem_desc(_query_mem_desc)
77  , row_set_mem_owner(_row_set_mem_owner)
78  , executor(nullptr) {}
79 
81  const RelLogicalValues* _logical_values,
82  const std::vector<TargetInfo>& _targets,
83  const ExecutorDeviceType _device_type,
84  const QueryMemoryDescriptor& _query_mem_desc,
85  const std::shared_ptr<RowSetMemoryOwner> _row_set_mem_owner,
86  const Executor* _executor)
87  : logical_values(_logical_values)
88  , targets(_targets)
89  , device_type(_device_type)
90  , query_mem_desc(_query_mem_desc)
91  , row_set_mem_owner(_row_set_mem_owner)
92  , executor(_executor) {}
93 
95  ResultSet* rs = ResultSetBuilder::makeResultSet(
97 
100 
101  std::vector<std::string> separate_varlen_storage;
102 
103  auto storage = rs->allocateStorage();
104  auto buff = storage->getUnderlyingBuffer();
105 
106  for (size_t i = 0; i < logical_values->getNumRows(); i++) {
107  std::vector<std::shared_ptr<Analyzer::Expr>> row_literals;
108  int8_t* ptr = buff + i * query_mem_desc.getRowSize();
109 
110  for (size_t j = 0; j < logical_values->getRowsSize(); j++) {
111  auto rex_literal =
112  dynamic_cast<const RexLiteral*>(logical_values->getValueAt(i, j));
113  CHECK(rex_literal);
114  const auto expr = RelAlgTranslator::translateLiteral(rex_literal);
115  const auto constant = std::dynamic_pointer_cast<Analyzer::Constant>(expr);
116  CHECK(constant);
117 
118  if (constant->get_is_null()) {
119  CHECK(!targets[j].sql_type.is_varlen());
120  *reinterpret_cast<int64_t*>(ptr) = inline_int_null_val(targets[j].sql_type);
121  } else {
122  const auto ti = constant->get_type_info();
123  const auto datum = constant->get_constval();
124 
125  if (ti.is_string()) {
126  // get string from datum and push to vector
127  separate_varlen_storage.push_back(*(datum.stringval));
128 
129  // store the index/offset in ResultSet's storage
130  // (the # of the string in the varlen_storage, not strLen)
131  *reinterpret_cast<int64_t*>(ptr) =
132  static_cast<int64_t>(separate_varlen_storage.size() - 1);
133 
134  } else {
135  // Initialize the entire 8-byte slot
136  *reinterpret_cast<int64_t*>(ptr) = EMPTY_KEY_64;
137 
138  const auto sz = ti.get_size();
139  CHECK_GE(sz, int(0));
140  std::memcpy(ptr, &datum, sz);
141  }
142  }
143  ptr += 8;
144  }
145  }
146 
147  // store the varlen data (ex. strings) into the ResultSet
148  if (separate_varlen_storage.size()) {
149  ResultSetBuilder::addVarlenBuffer(rs, separate_varlen_storage);
150  rs->setSeparateVarlenStorageValid(true);
151  }
152  }
153 
154  return rs;
155 }
156 
157 // static
159  std::vector<TargetMetaInfo>& label_infos,
160  std::vector<RelLogicalValues::RowValues>& logical_values) {
161  // check to see if number of columns matches (at least the first row)
162  size_t numCols =
163  logical_values.size() ? logical_values.front().size() : label_infos.size();
164  CHECK_EQ(label_infos.size(), numCols);
165 
166  size_t numRows = logical_values.size();
167 
168  QueryMemoryDescriptor query_mem_desc(/*executor=*/nullptr,
169  /*entry_count=*/numRows,
171  /*is_table_function=*/false);
172 
173  // target_infos -> defines the table columns
174  std::vector<TargetInfo> target_infos;
175  SQLTypeInfo str_ti(kTEXT, true, kENCODING_NONE);
176  for (size_t col = 0; col < numCols; col++) {
177  SQLTypeInfo colType = label_infos[col].get_type_info();
178  target_infos.push_back(TargetInfo{
179  false, kSAMPLE, colType, colType, true, false, /*is_varlen_projection=*/false});
180  query_mem_desc.addColSlotInfo({std::make_tuple(colType.get_size(), 8)});
181  }
182 
183  std::shared_ptr<RelLogicalValues> rel_logical_values =
184  std::make_shared<RelLogicalValues>(label_infos, logical_values);
185 
186  const auto row_set_mem_owner =
187  std::make_shared<RowSetMemoryOwner>(Executor::getArenaBlockSize());
188 
189  // Construct ResultSet
190  ResultSet* rsp(ResultSetLogicalValuesBuilder(rel_logical_values.get(),
191  target_infos,
195  nullptr)
196  .build());
197 
198  return rsp;
199 }
bool hasRows() const
#define CHECK_EQ(x, y)
Definition: Logger.h:217
HOST DEVICE int get_size() const
Definition: sqltypes.h:339
#define EMPTY_KEY_64
size_t size() const override
ExecutorDeviceType
const std::vector< TargetInfo > & targets
size_t getNumRows() const
#define CHECK_GE(x, y)
Definition: Logger.h:222
ResultSet * makeResultSet(const std::vector< TargetInfo > &targets, const ExecutorDeviceType device_type, const QueryMemoryDescriptor &query_mem_desc, const std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner, const Executor *executor)
size_t getRowsSize() const
static std::shared_ptr< Analyzer::Expr > translateLiteral(const RexLiteral *)
const ExecutorDeviceType device_type
const std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner
static ResultSet * create(std::vector< TargetMetaInfo > &label_infos, std::vector< RelLogicalValues::RowValues > &logical_values)
Definition: sqltypes.h:52
Basic constructors and methods of the row set interface.
const QueryMemoryDescriptor & query_mem_desc
ResultSetDefaultBuilder(const QueryMemoryDescriptor &query_mem_desc, const std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner)
const RelLogicalValues * logical_values
const RexScalar * getValueAt(const size_t row_idx, const size_t col_idx) const
#define CHECK(condition)
Definition: Logger.h:209
ResultSetLogicalValuesBuilder(const std::vector< TargetInfo > &targets, const QueryMemoryDescriptor &query_mem_desc, const std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner)
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
void addColSlotInfo(const std::vector< std::tuple< int8_t, int8_t >> &slots_for_col)
static size_t getArenaBlockSize()
Definition: Execute.cpp:221
void addVarlenBuffer(ResultSet *result_set, std::vector< std::string > &varlen_storage)
const std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner
const QueryMemoryDescriptor & query_mem_desc