OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RowFunctionManager.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 <boost/algorithm/string/predicate.hpp>
20 
21 #include "QueryEngine/Execute.h"
22 #include "Shared/toString.h"
23 
24 // copied from TableFunctionsFactory.cpp
25 namespace {
26 
27 std::string drop_suffix_impl(const std::string& str) {
28  const auto idx = str.find("__");
29  if (idx == std::string::npos) {
30  return str;
31  }
32  CHECK_GT(idx, std::string::size_type(0));
33  return str.substr(0, idx);
34 }
35 
36 std::list<const Analyzer::Expr*> find_function_oper(
37  const Analyzer::Expr* expr,
38  const std::string& func_name_wo_suffix) {
39  auto is_func_oper = [&func_name_wo_suffix](const Analyzer::Expr* e) -> bool {
40  auto function_oper = dynamic_cast<const Analyzer::FunctionOper*>(e);
41 
42  if (function_oper) {
43  std::string func_oper_name = drop_suffix_impl(function_oper->getName());
44  boost::algorithm::to_lower(func_oper_name);
45  if (func_name_wo_suffix == func_oper_name) {
46  return true;
47  }
48  }
49  return false;
50  };
51  std::list<const Analyzer::Expr*> funcoper_list;
52  expr->find_expr(is_func_oper, funcoper_list);
53  return funcoper_list;
54 }
55 
56 } // namespace
57 
58 struct RowFunctionManager {
59  RowFunctionManager(const Executor* executor, const RelAlgExecutionUnit& ra_exe_unit)
60  : executor_(executor), ra_exe_unit_(ra_exe_unit) {}
61 
62  inline std::string getString(int32_t db_id, int32_t dict_id, int32_t string_id) {
63  const auto proxy = executor_->getStringDictionaryProxy(
64  {db_id, dict_id}, executor_->getRowSetMemoryOwner(), true);
65  return proxy->getString(string_id);
66  }
67 
68  inline int32_t getDictDbId(const std::string& func_name, size_t arg_idx) {
69  std::string func_name_wo_suffix =
70  boost::algorithm::to_lower_copy(drop_suffix_impl(func_name));
71 
72  for (const auto& expr : ra_exe_unit_.target_exprs) {
73  for (const auto* op : find_function_oper(expr, func_name_wo_suffix)) {
74  const Analyzer::FunctionOper* function_oper =
75  dynamic_cast<const Analyzer::FunctionOper*>(op);
76  CHECK_LT(arg_idx, function_oper->getArity());
77  const SQLTypeInfo typ = function_oper->getArg(arg_idx)->get_type_info();
78  CHECK(typ.is_text_encoding_dict() || typ.is_text_encoding_dict_array());
79  return typ.getStringDictKey().db_id;
80  }
81  }
82  UNREACHABLE();
83  return 0;
84  }
85 
86  inline int32_t getDictId(const std::string& func_name, size_t arg_idx) {
87  std::string func_name_wo_suffix =
88  boost::algorithm::to_lower_copy(drop_suffix_impl(func_name));
89 
90  for (const auto& expr : ra_exe_unit_.target_exprs) {
91  for (const auto* op : find_function_oper(expr, func_name_wo_suffix)) {
92  const Analyzer::FunctionOper* function_oper =
93  dynamic_cast<const Analyzer::FunctionOper*>(op);
94  CHECK_LT(arg_idx, function_oper->getArity());
95  const SQLTypeInfo typ = function_oper->getArg(arg_idx)->get_type_info();
96  CHECK(typ.is_text_encoding_dict() || typ.is_text_encoding_dict_array());
97  return typ.getStringDictKey().dict_id;
98  }
99  }
100  UNREACHABLE();
101  return 0;
102  }
103 
104  inline int32_t getOrAddTransient(int32_t db_id, int32_t dict_id, std::string str) {
105  const auto proxy = executor_->getStringDictionaryProxy(
106  {db_id, dict_id}, executor_->getRowSetMemoryOwner(), true);
107  return proxy->getOrAddTransient(str);
108  }
109 
110  inline int8_t* getStringDictionaryProxy(int32_t db_id, int32_t dict_id) {
111  auto* proxy = executor_->getStringDictionaryProxy(
112  {db_id, dict_id}, executor_->getRowSetMemoryOwner(), true);
113  return reinterpret_cast<int8_t*>(proxy);
114  }
115 
116  inline int8_t* makeBuffer(int64_t element_count, int64_t element_size) {
117  int8_t* buffer =
118  reinterpret_cast<int8_t*>(checked_malloc((element_count + 1) * element_size));
119  executor_->getRowSetMemoryOwner()->addVarlenBuffer(buffer);
120  return buffer;
121  }
122 
123  // Executor
124  const Executor* executor_;
126 };
std::vector< Analyzer::Expr * > target_exprs
std::string to_lower(const std::string &str)
size_t getArity() const
Definition: Analyzer.h:2615
#define UNREACHABLE()
Definition: Logger.h:338
int32_t getDictDbId(const std::string &func_name, size_t arg_idx)
#define CHECK_GT(x, y)
Definition: Logger.h:305
const Executor * executor_
int8_t * getStringDictionaryProxy(int32_t db_id, int32_t dict_id)
int32_t getDictId(const std::string &func_name, size_t arg_idx)
std::string drop_suffix_impl(const std::string &str)
void * checked_malloc(const size_t size)
Definition: checked_alloc.h:45
std::list< const Analyzer::Expr * > find_function_oper(const Analyzer::Expr *expr, const std::string &func_name_wo_suffix)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
#define CHECK_LT(x, y)
Definition: Logger.h:303
int8_t * makeBuffer(int64_t element_count, int64_t element_size)
int32_t getOrAddTransient(int32_t db_id, int32_t dict_id, std::string str)
const Analyzer::Expr * getArg(const size_t i) const
Definition: Analyzer.h:2617
const RelAlgExecutionUnit & ra_exe_unit_
#define CHECK(condition)
Definition: Logger.h:291
std::string getString(int32_t db_id, int32_t dict_id, int32_t string_id)
RowFunctionManager(const Executor *executor, const RelAlgExecutionUnit &ra_exe_unit)
virtual void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const
Definition: Analyzer.h:163