OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StringOpInfo.cpp
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 #include "StringOpInfo.h"
18 #include "Logger/Logger.h"
19 
20 #include <sstream>
21 
22 namespace StringOps_Namespace {
23 
24 std::ostream& operator<<(std::ostream& stream, const StringOpInfo& string_op_info) {
25  stream << "StringOp("
26  << "operator: " << string_op_info.getOpKind()
27  << "return_ti: " << toString(string_op_info.getReturnType().get_type())
28  << " dim: " << string_op_info.getReturnType().get_dimension()
29  << " scale: " << string_op_info.getReturnType().get_scale() << ", literals: [";
30  bool first_elem = true;
31  for (const auto& literal_arg : string_op_info.literal_arg_map_) {
32  if (!first_elem) {
33  stream << ", ";
34  }
35  first_elem = false;
36  const auto datum_type = literal_arg.second.first;
37  const auto& datum = literal_arg.second.second;
38  stream << "{slot: " << literal_arg.first /* slot/idx */ << ", type: "
39  << ::toString(datum_type) << ", value: ";
40  if (string_op_info.isLiteralArgNull(datum_type, literal_arg.second.second)) {
41  stream << "NULL";
42  } else if (IS_STRING(datum_type)) {
43  stream << *datum.stringval;
44  } else {
45  CHECK(IS_INTEGER(datum_type));
46  const SQLTypeInfo ti(datum_type, false);
47  stream << extract_int_type_from_datum(datum, ti);
48  }
49  stream << "}";
50  }
51  stream << "]";
52  return stream;
53 }
54 
55 std::ostream& operator<<(std::ostream& stream,
56  const std::vector<StringOpInfo>& string_op_infos) {
57  stream << "[";
58  bool first_elem = true;
59  for (const auto& string_op_info : string_op_infos) {
60  if (!first_elem) {
61  stream << ", ";
62  }
63  first_elem = false;
64  stream << string_op_info;
65  }
66  stream << "]";
67  return stream;
68 }
69 
70 std::string toString(const std::vector<StringOpInfo>& string_op_infos) {
71  std::ostringstream oss;
72  oss << string_op_infos;
73  return oss.str();
74 }
75 
76 bool StringOpInfo::intLiteralArgAtIdxExists(const size_t index) const {
77  const auto literal_itr = literal_arg_map_.find(index);
78  if (literal_itr == literal_arg_map_.end()) {
79  return false;
80  }
81  CHECK(IS_INTEGER(literal_itr->second.first));
82  return true;
83 }
84 
85 bool StringOpInfo::stringLiteralArgAtIdxExists(const size_t index) const {
86  const auto literal_itr = literal_arg_map_.find(index);
87  if (literal_itr == literal_arg_map_.end()) {
88  return false;
89  }
90  CHECK(IS_STRING(literal_itr->second.first));
91  return true;
92 }
93 
94 std::string StringOpInfo::getStringLiteral(const size_t index) const {
95  const auto str_literal_datum = literal_arg_map_.find(index);
96  CHECK(str_literal_datum != literal_arg_map_.end());
97  CHECK(IS_STRING(str_literal_datum->second.first));
98  CHECK(!StringOpInfo::isLiteralArgNull(str_literal_datum->second.first,
99  str_literal_datum->second.second));
100  return *str_literal_datum->second.second.stringval;
101 }
102 
103 int64_t StringOpInfo::getIntLiteral(const size_t index) const {
104  const auto literal_datum = literal_arg_map_.find(index);
105  CHECK(literal_datum != literal_arg_map_.end());
106  const auto& datum_type = literal_datum->second.first;
107  CHECK(IS_INTEGER(datum_type));
108  const auto& datum = literal_datum->second.second;
109  CHECK(!StringOpInfo::isLiteralArgNull(datum_type, datum));
110  const SQLTypeInfo ti(datum_type, false);
111  return extract_int_type_from_datum(datum, ti);
112 }
113 
114 std::string StringOpInfo::toString() const {
115  std::ostringstream oss;
116  oss << *this;
117  return oss.str();
118 }
119 
120 bool StringOpInfo::isLiteralArgNull(const SQLTypes datum_type, const Datum& datum) {
121  if (datum_type == kNULLT) {
122  CHECK(datum.bigintval == 0);
123  return true;
124  }
125  if (IS_INTEGER(datum_type)) {
126  const SQLTypeInfo ti(datum_type, false);
127  return ti.is_null(datum);
128  }
129  CHECK(IS_STRING(datum_type));
130  // Currently null strings are empty strings
131  // Todo(todd): is this expressed centrally somewhere else in the codebase?
132  return datum.stringval == nullptr ? 1UL : 0UL;
133 }
134 
135 size_t StringOpInfo::calcNumNullLiteralArgs(const LiteralArgMap& literal_arg_map) {
136  size_t num_null_literals{0UL};
137  for (const auto& literal_arg : literal_arg_map) {
138  const auto& datum_type = literal_arg.second.first;
139  const auto& datum = literal_arg.second.second;
140  num_null_literals += StringOpInfo::isLiteralArgNull(datum_type, datum) ? 1UL : 0UL;
141  }
142  return num_null_literals;
143 }
144 
145 } // namespace StringOps_Namespace
const SQLTypeInfo & getReturnType() const
Definition: StringOpInfo.h:58
bool stringLiteralArgAtIdxExists(const size_t index) const
SQLTypes
Definition: sqltypes.h:53
static size_t calcNumNullLiteralArgs(const LiteralArgMap &literal_arg_map)
static bool isLiteralArgNull(const SQLTypes datum_type, const Datum &datum)
HOST DEVICE int get_scale() const
Definition: sqltypes.h:385
std::map< size_t, std::pair< SQLTypes, Datum >> LiteralArgMap
Definition: StringOpInfo.h:30
bool intLiteralArgAtIdxExists(const size_t index) const
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:380
int64_t getIntLiteral(const size_t index) const
std::string toString(const std::vector< StringOpInfo > &string_op_infos)
int64_t extract_int_type_from_datum(const Datum datum, const SQLTypeInfo &ti)
Definition: Datum.cpp:521
int64_t bigintval
Definition: Datum.h:72
std::string * stringval
Definition: Datum.h:77
HOST DEVICE bool is_null(const Datum &d) const
Definition: sqltypes.h:849
std::ostream & operator<<(std::ostream &stream, const StringOpInfo &string_op_info)
const LiteralArgMap literal_arg_map_
Definition: StringOpInfo.h:78
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:382
#define IS_INTEGER(T)
Definition: sqltypes.h:292
#define IS_STRING(T)
Definition: sqltypes.h:297
std::string getStringLiteral(const size_t index) const
#define CHECK(condition)
Definition: Logger.h:289
const SqlStringOpKind & getOpKind() const
Definition: StringOpInfo.h:42
Definition: Datum.h:67