OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TargetValueConvertersFactories.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018, 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 
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() || param.type.is_timestamp() || param.type.is_time()) {
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>>(
122  param.cat,
124  param.target,
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  auto sourceDictId = param.source.get_type_info().get_comp_param();
144  return std::make_unique<StringValueConverter>(param.cat,
145  param.target,
146  param.num_rows,
147  dictEncodedSource,
148  sourceDictId,
149  param.literals_dictionary);
150  } else if (param.target->columnType.get_compression() == kENCODING_DICT) {
151  auto size = param.target->columnType.get_size();
152  if (4 == size) {
154  return factory.create(param);
155  } else if (2 == size) {
157  return factory.create(param);
158  } else if (1 == size) {
160  return factory.create(param);
161  }
162  }
163 
164  throw std::runtime_error("Unsupported text column type");
165  }
166 };
167 
168 template <typename ELEMENT_FACTORY>
170  ELEMENT_FACTORY element_factory_;
171 
172  std::unique_ptr<ArrayValueConverter<typename ELEMENT_FACTORY::ConverterType>> create(
173  ConverterCreateParameter param) {
174  auto elem_type = param.target->columnType.get_elem_type();
175  ConverterCreateParameter elementConverterFactoryParam{0,
176  param.cat,
177  param.source,
178  param.target,
179  elem_type,
180  true,
181  param.literals_dictionary};
182 
183  auto elementConverter = element_factory_.create(elementConverterFactoryParam);
184  return std::make_unique<ArrayValueConverter<typename ELEMENT_FACTORY::ConverterType>>(
185  param.target, param.num_rows, std::move(elementConverter), param.can_be_null);
186  }
187 
188  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
189  return create(param);
190  }
191 };
192 
194  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
195  const static std::map<std::tuple<SQLTypes, EncodingType>,
196  std::function<std::unique_ptr<TargetValueConverter>(
197  ConverterCreateParameter param)>>
198  array_converter_factories{
201  {{kINT, kENCODING_NONE},
213  {{kDATE, kENCODING_NONE},
215  {{kTIME, kENCODING_NONE},
223  {{kTEXT, kENCODING_DICT},
225  {{kCHAR, kENCODING_DICT},
229 
230  auto elem_type = param.target->columnType.get_elem_type();
231  auto factory = array_converter_factories.find(
232  {elem_type.get_type(), elem_type.get_compression()});
233 
234  if (factory != array_converter_factories.end()) {
235  return factory->second(param);
236  }
237 
238  throw std::runtime_error("Unsupported array column type");
239  }
240 };
241 
242 template <typename CONVERTER>
244  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
245  return std::make_unique<CONVERTER>(param.cat, param.num_rows, param.target);
246  }
247 };
248 
249 std::unique_ptr<TargetValueConverter> TargetValueConverterFactory::create(
250  ConverterCreateParameter param) {
251  static const std::map<SQLTypes,
252  std::function<std::unique_ptr<TargetValueConverter>(
253  ConverterCreateParameter param)>>
274 
275  auto factory = factories.find(param.target->columnType.get_type());
276 
277  if (factory != factories.end()) {
278  return factory->second(param);
279  } else {
280  throw std::runtime_error("Unsupported column type: " +
281  param.target->columnType.get_type_name());
282  }
283 }
const Catalog_Namespace::Catalog & cat
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
HOST DEVICE int get_size() const
Definition: sqltypes.h:340
Definition: sqltypes.h:51
SQLTypes
Definition: sqltypes.h:40
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
bool is_timestamp() const
Definition: sqltypes.h:733
std::unique_ptr< ArrayValueConverter< typename ELEMENT_FACTORY::ConverterType > > create(ConverterCreateParameter param)
StringDictionaryProxy * literals_dictionary
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:330
const SQLTypeInfo & get_type_info() const
bool is_time() const
Definition: sqltypes.h:493
const SQLTypeInfo & get_physical_type_info() const
std::unique_ptr< ConverterType > create(ConverterCreateParameter param)
bool is_integer() const
Definition: sqltypes.h:489
std::unique_ptr< ConverterType > create(ConverterCreateParameter param)
#define NULL_INT
Definition: sqltypes.h:254
Definition: sqltypes.h:54
Definition: sqltypes.h:55
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:338
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:433
Definition: sqltypes.h:43
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:339
#define CHECK(condition)
Definition: Logger.h:197
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
Definition: sqltypes.h:47
SQLTypeInfo columnType
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:703
std::unique_ptr< TargetValueConverter > create(ConverterCreateParameter param)
StringDictionaryProxy * source_dictionary_proxy
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)