OmniSciDB  1dac507f6e
 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  switch (param.type.get_size()) {
29  case 8:
30  source_null_value = static_cast<SOURCE_TYPE>(inline_int_null_value<int64_t>());
31  break;
32  case 4:
33  source_null_value = static_cast<SOURCE_TYPE>(inline_int_null_value<int32_t>());
34  break;
35  case 2:
36  source_null_value = static_cast<SOURCE_TYPE>(inline_int_null_value<int16_t>());
37  break;
38  case 1:
39  source_null_value = static_cast<SOURCE_TYPE>(inline_int_null_value<int8_t>());
40  break;
41  default:
42  CHECK(false);
43  }
44 
45  TARGET_TYPE target_null_value =
46  static_cast<TARGET_TYPE>(inline_int_null_value<TARGET_TYPE>());
47 
48  return std::make_unique<NumericValueConverter<SOURCE_TYPE, TARGET_TYPE>>(
49  param.target,
50  param.num_rows,
51  source_null_value,
52  target_null_value,
53  param.can_be_null);
54  }
55 
56  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
57  return create(param);
58  }
59 };
60 
61 template <>
62 std::unique_ptr<NumericValueConverter<double, double>>
64  double null_value = inline_fp_null_value<double>();
65  return std::make_unique<NumericValueConverter<double, double>>(
66  param.target, param.num_rows, null_value, null_value, param.can_be_null);
67 }
68 
69 template <>
70 std::unique_ptr<NumericValueConverter<float, float>>
72  float null_value = inline_fp_null_value<float>();
73  return std::make_unique<NumericValueConverter<float, float>>(
74  param.target, param.num_rows, null_value, null_value, param.can_be_null);
75 }
76 
77 template <typename TARGET_TYPE>
80 
81  std::unique_ptr<ConverterType> create(ConverterCreateParameter param) {
82  TARGET_TYPE target_null_value =
83  static_cast<TARGET_TYPE>(inline_int_null_value<TARGET_TYPE>());
84 
85  switch (param.type.get_size()) {
86  case 4:
87  target_null_value = static_cast<TARGET_TYPE>(inline_int_null_value<int32_t>());
88  break;
89  case 2:
90  target_null_value = static_cast<TARGET_TYPE>(inline_int_null_value<uint16_t>());
91  break;
92  case 1:
93  target_null_value = static_cast<TARGET_TYPE>(inline_int_null_value<uint8_t>());
94  break;
95  default:
96  CHECK(false);
97  }
98 
99  return std::make_unique<DictionaryValueConverter<TARGET_TYPE>>(
100  param.cat,
102  param.target,
103  param.num_rows,
104  target_null_value,
105  NULL_INT,
106  param.can_be_null,
107  param.literals_dictionary,
109  }
110 
111  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
112  return create(param);
113  }
114 };
115 
117  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
119  bool dictEncodedSource =
121  auto sourceDictId = param.source.get_type_info().get_comp_param();
122  return std::make_unique<StringValueConverter>(param.cat,
123  param.target,
124  param.num_rows,
125  dictEncodedSource,
126  sourceDictId,
127  param.literals_dictionary);
128  } else if (param.target->columnType.get_compression() == kENCODING_DICT) {
129  auto size = param.target->columnType.get_size();
130  if (4 == size) {
132  return factory.create(param);
133  } else if (2 == size) {
135  return factory.create(param);
136  } else if (1 == size) {
138  return factory.create(param);
139  }
140  }
141 
142  throw std::runtime_error("Unsupported text column type");
143  }
144 };
145 
146 template <typename ELEMENT_FACTORY>
148  ELEMENT_FACTORY element_factory_;
149 
150  std::unique_ptr<ArrayValueConverter<typename ELEMENT_FACTORY::ConverterType>> create(
151  ConverterCreateParameter param) {
152  auto elem_type = param.target->columnType.get_elem_type();
153  ConverterCreateParameter elementConverterFactoryParam{0,
154  param.cat,
155  param.source,
156  param.target,
157  elem_type,
158  true,
159  param.literals_dictionary};
160 
161  auto elementConverter = element_factory_.create(elementConverterFactoryParam);
162  return std::make_unique<ArrayValueConverter<typename ELEMENT_FACTORY::ConverterType>>(
163  param.target, param.num_rows, std::move(elementConverter), param.can_be_null);
164  }
165 
166  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
167  return create(param);
168  }
169 };
170 
172  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
173  const static std::map<std::tuple<SQLTypes, EncodingType>,
174  std::function<std::unique_ptr<TargetValueConverter>(
175  ConverterCreateParameter param)>>
176  array_converter_factories{
179  {{kINT, kENCODING_NONE},
191  {{kDATE, kENCODING_NONE},
193  {{kTIME, kENCODING_NONE},
201  {{kTEXT, kENCODING_DICT},
203  {{kCHAR, kENCODING_DICT},
207 
208  auto elem_type = param.target->columnType.get_elem_type();
209  auto factory = array_converter_factories.find(
210  {elem_type.get_type(), elem_type.get_compression()});
211 
212  if (factory != array_converter_factories.end()) {
213  return factory->second(param);
214  }
215 
216  throw std::runtime_error("Unsupported array column type");
217  }
218 };
219 
220 template <typename CONVERTER>
222  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
223  return std::make_unique<CONVERTER>(param.cat, param.num_rows, param.target);
224  }
225 };
226 
227 std::unique_ptr<TargetValueConverter> TargetValueConverterFactory::create(
228  ConverterCreateParameter param) {
229  static const std::map<SQLTypes,
230  std::function<std::unique_ptr<TargetValueConverter>(
231  ConverterCreateParameter param)>>
252 
253  auto factory = factories.find(param.target->columnType.get_type());
254 
255  if (factory != factories.end()) {
256  return factory->second(param);
257  } else {
258  throw std::runtime_error("Unsupported column type: " +
259  param.target->columnType.get_type_name());
260  }
261 }
const Catalog_Namespace::Catalog & cat
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
Definition: sqltypes.h:52
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:334
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:335
SQLTypes
Definition: sqltypes.h:41
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
std::unique_ptr< ArrayValueConverter< typename ELEMENT_FACTORY::ConverterType > > create(ConverterCreateParameter param)
StringDictionaryProxy * literals_dictionary
HOST DEVICE int get_size() const
Definition: sqltypes.h:336
const SQLTypeInfo & get_type_info() const
std::string get_type_name() const
Definition: sqltypes.h:429
std::unique_ptr< ConverterType > create(ConverterCreateParameter param)
CHECK(cgen_state)
std::unique_ptr< ConverterType > create(ConverterCreateParameter param)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
#define NULL_INT
Definition: sqltypes.h:176
Definition: sqltypes.h:55
Definition: sqltypes.h:56
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
constexpr float inline_fp_null_value< float >()
constexpr double inline_fp_null_value< double >()
Definition: sqltypes.h:44
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
Definition: sqltypes.h:48
SQLTypeInfoCore get_elem_type() const
Definition: sqltypes.h:659
SQLTypeInfo columnType
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
std::unique_ptr< TargetValueConverter > create(ConverterCreateParameter param)
StringDictionaryProxy * source_dictionary_proxy
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)