OmniSciDB  cde582ebc3
 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: " << ::toString(string_op_info.getOpKind()) << ", literals: [";
27  bool first_elem = true;
28  for (const auto& literal_arg : string_op_info.literal_arg_map_) {
29  if (!first_elem) {
30  stream << ", ";
31  }
32  first_elem = false;
33  const auto datum_type = literal_arg.second.first;
34  const auto& datum = literal_arg.second.second;
35  stream << "{slot: " << literal_arg.first /* slot/idx */ << ", type: "
36  << ::toString(datum_type) << ", value: ";
37  if (string_op_info.isLiteralArgNull(datum_type, literal_arg.second.second)) {
38  stream << "NULL";
39  } else if (IS_STRING(datum_type)) {
40  stream << *datum.stringval;
41  } else {
42  CHECK(IS_INTEGER(datum_type));
43  const SQLTypeInfo ti(datum_type, false);
44  stream << extract_int_type_from_datum(datum, ti);
45  }
46  stream << "}";
47  }
48  stream << "]";
49  return stream;
50 }
51 
52 std::ostream& operator<<(std::ostream& stream,
53  const std::vector<StringOpInfo>& string_op_infos) {
54  stream << "[";
55  bool first_elem = true;
56  for (const auto& string_op_info : string_op_infos) {
57  if (!first_elem) {
58  stream << ", ";
59  }
60  first_elem = false;
61  stream << string_op_info;
62  }
63  stream << "]";
64  return stream;
65 }
66 
67 std::string toString(const std::vector<StringOpInfo>& string_op_infos) {
68  std::ostringstream oss;
69  oss << string_op_infos;
70  return oss.str();
71 }
72 
73 bool StringOpInfo::intLiteralArgAtIdxExists(const size_t index) const {
74  const auto literal_itr = literal_arg_map_.find(index);
75  if (literal_itr == literal_arg_map_.end()) {
76  return false;
77  }
78  CHECK(IS_INTEGER(literal_itr->second.first));
79  return true;
80 }
81 
82 bool StringOpInfo::stringLiteralArgAtIdxExists(const size_t index) const {
83  const auto literal_itr = literal_arg_map_.find(index);
84  if (literal_itr == literal_arg_map_.end()) {
85  return false;
86  }
87  CHECK(IS_STRING(literal_itr->second.first));
88  return true;
89 }
90 
91 std::string StringOpInfo::getStringLiteral(const size_t index) const {
92  const auto str_literal_datum = literal_arg_map_.find(index);
93  CHECK(str_literal_datum != literal_arg_map_.end());
94  CHECK(IS_STRING(str_literal_datum->second.first));
95  CHECK(!StringOpInfo::isLiteralArgNull(str_literal_datum->second.first,
96  str_literal_datum->second.second));
97  return *str_literal_datum->second.second.stringval;
98 }
99 
100 int64_t StringOpInfo::getIntLiteral(const size_t index) const {
101  const auto literal_datum = literal_arg_map_.find(index);
102  CHECK(literal_datum != literal_arg_map_.end());
103  const auto& datum_type = literal_datum->second.first;
104  CHECK(IS_INTEGER(datum_type));
105  const auto& datum = literal_datum->second.second;
106  CHECK(!StringOpInfo::isLiteralArgNull(datum_type, datum));
107  const SQLTypeInfo ti(datum_type, false);
108  return extract_int_type_from_datum(datum, ti);
109 }
110 
111 std::string StringOpInfo::toString() const {
112  std::ostringstream oss;
113  oss << *this;
114  return oss.str();
115 }
116 
117 // oss << "StringOp(" << "operator: " << ::toString(getOpKind())
118 // << ", literals: [";
119 // bool first_arg = true;
120 // for (const auto& literal_arg : literal_arg_map_) {
121 // const auto datum_type = literal_arg.second.first;
122 // const auto& datum = literal_arg.second.second;
123 // if (!first_arg) {
124 // oss << ", ";
125 // }
126 // first_arg = false;
127 // oss << "{slot: " << literal_arg.first /* slot/idx */ << ", type: "
128 // << ::toString(datum_type) << ", value: ";
129 // if (isLiteralArgNull(datum_type, literal_arg.second.second)) {
130 // oss << "NULL";
131 // } else if (IS_STRING(datum_type)) {
132 // oss << *datum.stringval;
133 // } else {
134 // CHECK(IS_INTEGER(datum_type));
135 // const SQLTypeInfo ti(datum_type, false);
136 // oss << extract_int_type_from_datum(datum, ti);
137 // }
138 // oss << "}";
139 // }
140 // return oss.str();
141 //}
142 
143 bool StringOpInfo::isLiteralArgNull(const SQLTypes datum_type, const Datum& datum) {
144  if (datum_type == kNULLT) {
145  CHECK(datum.bigintval == 0);
146  return true;
147  }
148  if (IS_INTEGER(datum_type)) {
149  const SQLTypeInfo ti(datum_type, false);
150  return ti.is_null(datum);
151  }
152  CHECK(IS_STRING(datum_type));
153  // Currently null strings are empty strings
154  // Todo(todd): is this expressed centrally somewhere else in the codebase?
155  return datum.stringval == nullptr ? 1UL : 0UL;
156 }
157 
158 size_t StringOpInfo::calcNumNullLiteralArgs(const LiteralArgMap& literal_arg_map) {
159  size_t num_null_literals{0UL};
160  for (const auto& literal_arg : literal_arg_map) {
161  const auto& datum_type = literal_arg.second.first;
162  const auto& datum = literal_arg.second.second;
163  num_null_literals += StringOpInfo::isLiteralArgNull(datum_type, datum) ? 1UL : 0UL;
164  }
165  return num_null_literals;
166 }
167 
168 } // namespace StringOps_Namespace
bool stringLiteralArgAtIdxExists(const size_t index) const
SQLTypes
Definition: sqltypes.h:38
static size_t calcNumNullLiteralArgs(const LiteralArgMap &literal_arg_map)
static bool isLiteralArgNull(const SQLTypes datum_type, const Datum &datum)
std::map< size_t, std::pair< SQLTypes, Datum >> LiteralArgMap
Definition: StringOpInfo.h:30
bool intLiteralArgAtIdxExists(const size_t index) const
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:455
int64_t bigintval
Definition: sqltypes.h:215
std::string * stringval
Definition: sqltypes.h:220
HOST DEVICE bool is_null(const Datum &d) const
Definition: sqltypes.h:757
std::ostream & operator<<(std::ostream &stream, const StringOpInfo &string_op_info)
const LiteralArgMap literal_arg_map_
Definition: StringOpInfo.h:72
#define IS_INTEGER(T)
Definition: sqltypes.h:245
#define IS_STRING(T)
Definition: sqltypes.h:250
std::string getStringLiteral(const size_t index) const
#define CHECK(condition)
Definition: Logger.h:222
const SqlStringOpKind & getOpKind() const
Definition: StringOpInfo.h:39