OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TargetValueConvertersFactories.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 
19 
20 template <typename SOURCE_TYPE, typename TARGET_TYPE>
23 
24  std::unique_ptr<ConverterType> create(ConverterCreateParameter param) {
25  SOURCE_TYPE source_null_value =
26  static_cast<SOURCE_TYPE>(inline_int_null_value<SOURCE_TYPE>());
27 
28  using CasterFunc = std::function<TARGET_TYPE(SOURCE_TYPE, bool, TARGET_TYPE)>;
29 
30  CasterFunc caster = nullptr;
31 
32  switch (param.type.get_size()) {
33  case 8:
34  source_null_value = static_cast<SOURCE_TYPE>(inline_int_null_value<int64_t>());
35  caster = checked_cast<SOURCE_TYPE, TARGET_TYPE, int64_t>;
36  break;
37  case 4:
38  source_null_value = static_cast<SOURCE_TYPE>(inline_int_null_value<int32_t>());
39  if (param.source.get_physical_type_info().get_size() > 4) {
40  caster = checked_cast<SOURCE_TYPE, TARGET_TYPE, int32_t>;
41  }
42  break;
43  case 2:
44  source_null_value = static_cast<SOURCE_TYPE>(inline_int_null_value<int16_t>());
45  if (param.source.get_physical_type_info().get_size() > 2) {
46  caster = checked_cast<SOURCE_TYPE, TARGET_TYPE, int16_t>;
47  }
48  break;
49  case 1:
50  source_null_value = static_cast<SOURCE_TYPE>(inline_int_null_value<int8_t>());
51  if (param.source.get_physical_type_info().get_size() > 1) {
52  caster = checked_cast<SOURCE_TYPE, TARGET_TYPE, int8_t>;
53  }
54  break;
55  default:
56  CHECK(false);
57  }
58 
59  TARGET_TYPE target_null_value =
60  static_cast<TARGET_TYPE>(inline_int_null_value<TARGET_TYPE>());
61 
62  auto ret = std::make_unique<NumericValueConverter<SOURCE_TYPE, TARGET_TYPE>>(
63  param.target,
64  param.num_rows,
65  source_null_value,
66  target_null_value,
67  param.can_be_null);
68 
69  if (param.type.is_integer()) {
70  // only apply overflow checks for
71  // the types using the fixed encoder
72  ret->setValueCaster(std::move(caster));
73  }
74 
75  return ret;
76  }
77 
78  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
79  return create(param);
80  }
81 };
82 
83 template <>
84 std::unique_ptr<NumericValueConverter<double, double>>
86  double null_value = inline_fp_null_value<double>();
87  return std::make_unique<NumericValueConverter<double, double>>(
88  param.target, param.num_rows, null_value, null_value, param.can_be_null);
89 }
90 
91 template <>
92 std::unique_ptr<NumericValueConverter<float, float>>
94  float null_value = inline_fp_null_value<float>();
95  return std::make_unique<NumericValueConverter<float, float>>(
96  param.target, param.num_rows, null_value, null_value, param.can_be_null);
97 }
98 
99 template <typename TARGET_TYPE>
102 
103  std::unique_ptr<ConverterType> create(ConverterCreateParameter param) {
104  TARGET_TYPE target_null_value =
105  static_cast<TARGET_TYPE>(inline_int_null_value<TARGET_TYPE>());
106 
107  switch (param.type.get_size()) {
108  case 4:
109  target_null_value = static_cast<TARGET_TYPE>(inline_int_null_value<int32_t>());
110  break;
111  case 2:
112  target_null_value = static_cast<TARGET_TYPE>(inline_int_null_value<uint16_t>());
113  break;
114  case 1:
115  target_null_value = static_cast<TARGET_TYPE>(inline_int_null_value<uint8_t>());
116  break;
117  default:
118  CHECK(false);
119  }
120 
121  return std::make_unique<DictionaryValueConverter<TARGET_TYPE>>(
123  param.target,
124  param.target_cat,
125  param.num_rows,
126  target_null_value,
127  NULL_INT,
128  param.can_be_null,
129  param.literals_dictionary,
131  }
132 
133  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
134  return create(param);
135  }
136 };
137 
139  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
141  bool dictEncodedSource =
143  shared::StringDictKey source_dict_key;
144  if (dictEncodedSource) {
145  source_dict_key = param.source.get_type_info().getStringDictKey();
146  }
147  return std::make_unique<StringValueConverter>(param.target,
148  param.num_rows,
149  dictEncodedSource,
150  source_dict_key,
151  param.literals_dictionary);
152  } else if (param.target->columnType.get_compression() == kENCODING_DICT) {
153  auto size = param.target->columnType.get_size();
154  if (4 == size) {
156  return factory.create(param);
157  } else if (2 == size) {
159  return factory.create(param);
160  } else if (1 == size) {
162  return factory.create(param);
163  }
164  }
165 
166  throw std::runtime_error("Unsupported text column type");
167  }
168 };
169 
170 template <typename ELEMENT_FACTORY>
172  ELEMENT_FACTORY element_factory_;
173 
174  std::unique_ptr<ArrayValueConverter<typename ELEMENT_FACTORY::ConverterType>> create(
175  ConverterCreateParameter param) {
176  auto elem_type = param.target->columnType.get_elem_type();
177  ConverterCreateParameter elementConverterFactoryParam{0,
178  param.source,
179  param.target,
180  param.target_cat,
181  elem_type,
182  true,
183  param.literals_dictionary};
184 
185  auto elementConverter = element_factory_.create(elementConverterFactoryParam);
186  return std::make_unique<ArrayValueConverter<typename ELEMENT_FACTORY::ConverterType>>(
187  param.target, param.num_rows, std::move(elementConverter), param.can_be_null);
188  }
189 
190  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
191  return create(param);
192  }
193 };
194 
196  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
197  const static std::map<std::tuple<SQLTypes, EncodingType>,
198  std::function<std::unique_ptr<TargetValueConverter>(
199  ConverterCreateParameter param)>>
200  array_converter_factories{
203  {{kINT, kENCODING_NONE},
215  {{kDATE, kENCODING_NONE},
217  {{kTIME, kENCODING_NONE},
225  {{kTEXT, kENCODING_DICT},
227  {{kCHAR, kENCODING_DICT},
231 
232  auto elem_type = param.target->columnType.get_elem_type();
233  auto factory = array_converter_factories.find(
234  {elem_type.get_type(), elem_type.get_compression()});
235 
236  if (factory != array_converter_factories.end()) {
237  return factory->second(param);
238  }
239 
240  throw std::runtime_error("Unsupported array column type");
241  }
242 };
243 
244 template <typename CONVERTER, class Enable = void>
246  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
247  return std::make_unique<CONVERTER>(param.target_cat, param.num_rows, param.target);
248  }
249 };
250 
251 template <typename CONVERTER>
253  CONVERTER,
254  typename std::enable_if_t<std::is_same_v<GeoPolygonValueConverter, CONVERTER> ||
255  std::is_same_v<GeoMultiPolygonValueConverter, CONVERTER>>> {
256  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
257  return std::make_unique<CONVERTER>(param.target_cat, param.num_rows, param.target);
258  }
259 };
260 
261 std::unique_ptr<TargetValueConverter> TargetValueConverterFactory::create(
262  ConverterCreateParameter param) {
263  static const std::map<SQLTypes,
264  std::function<std::unique_ptr<TargetValueConverter>(
265  ConverterCreateParameter param)>>
266  factories{
289 
290  auto factory = factories.find(param.target->columnType.get_type());
291 
292  if (factory != factories.end()) {
293  return factory->second(param);
294  } else {
295  throw std::runtime_error("Unsupported column type: " +
296  param.target->columnType.get_type_name());
297  }
298 }
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
HOST DEVICE int get_size() const
Definition: sqltypes.h:403
Definition: sqltypes.h:76
SQLTypes
Definition: sqltypes.h:65
std::unique_ptr< ArrayValueConverter< typename ELEMENT_FACTORY::ConverterType > > create(ConverterCreateParameter param)
StringDictionaryProxy * literals_dictionary
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:391
const SQLTypeInfo & get_type_info() const
#define NULL_INT
const SQLTypeInfo & get_physical_type_info() const
std::unique_ptr< ConverterType > create(ConverterCreateParameter param)
bool is_integer() const
Definition: sqltypes.h:567
std::unique_ptr< ConverterType > create(ConverterCreateParameter param)
const Catalog_Namespace::Catalog & target_cat
Definition: sqltypes.h:79
Definition: sqltypes.h:80
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:399
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
constexpr float inline_fp_null_value< float >()
RETURN_TYPE checked_cast(SOURCE_TYPE val, bool check_null, RETURN_TYPE null_value)
constexpr double inline_fp_null_value< double >()
std::string get_type_name() const
Definition: sqltypes.h:484
Definition: sqltypes.h:68
#define CHECK(condition)
Definition: Logger.h:291
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
Definition: sqltypes.h:72
SQLTypeInfo columnType
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:977
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
std::unique_ptr< TargetValueConverter > create(ConverterCreateParameter param)
StringDictionaryProxy * source_dictionary_proxy
const shared::StringDictKey & getStringDictKey() const
Definition: sqltypes.h:1057
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)