OmniSciDB  04ee39c94c
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);
108  }
109 
110  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
111  return create(param);
112  }
113 };
114 
116  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
118  bool dictEncodedSource =
120  auto sourceDictId = param.source.get_type_info().get_comp_param();
121  return std::make_unique<StringValueConverter>(param.cat,
122  param.target,
123  param.num_rows,
124  dictEncodedSource,
125  sourceDictId,
126  param.literals_dictionary);
127  } else if (param.target->columnType.get_compression() == kENCODING_DICT) {
128  auto size = param.target->columnType.get_size();
129  if (4 == size) {
131  return factory.create(param);
132  } else if (2 == size) {
134  return factory.create(param);
135  } else if (1 == size) {
137  return factory.create(param);
138  }
139  }
140 
141  throw std::runtime_error("Unsupported text column type");
142  }
143 };
144 
145 template <typename ELEMENT_FACTORY>
147  ELEMENT_FACTORY element_factory_;
148 
149  std::unique_ptr<ArrayValueConverter<typename ELEMENT_FACTORY::ConverterType>> create(
150  ConverterCreateParameter param) {
151  auto elem_type = param.target->columnType.get_elem_type();
152  ConverterCreateParameter elementConverterFactoryParam{0,
153  param.cat,
154  param.source,
155  param.target,
156  elem_type,
157  true,
158  param.literals_dictionary};
159 
160  auto elementConverter = element_factory_.create(elementConverterFactoryParam);
161  return std::make_unique<ArrayValueConverter<typename ELEMENT_FACTORY::ConverterType>>(
162  param.target, param.num_rows, std::move(elementConverter), param.can_be_null);
163  }
164 
165  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
166  return create(param);
167  }
168 };
169 
171  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
172  const static std::map<std::tuple<SQLTypes, EncodingType>,
173  std::function<std::unique_ptr<TargetValueConverter>(
174  ConverterCreateParameter param)>>
175  array_converter_factories{
178  {{kINT, kENCODING_NONE},
190  {{kDATE, kENCODING_NONE},
192  {{kTIME, kENCODING_NONE},
200  {{kTEXT, kENCODING_DICT},
202  {{kCHAR, kENCODING_DICT},
206 
207  auto elem_type = param.target->columnType.get_elem_type();
208  auto factory = array_converter_factories.find(
209  {elem_type.get_type(), elem_type.get_compression()});
210 
211  if (factory != array_converter_factories.end()) {
212  return factory->second(param);
213  }
214 
215  throw std::runtime_error("Unsupported array column type");
216  }
217 };
218 
219 template <typename CONVERTER>
221  std::unique_ptr<TargetValueConverter> operator()(ConverterCreateParameter param) {
222  return std::make_unique<CONVERTER>(param.cat, param.num_rows, param.target);
223  }
224 };
225 
226 std::unique_ptr<TargetValueConverter> TargetValueConverterFactory::create(
227  ConverterCreateParameter param) {
228  static const std::map<SQLTypes,
229  std::function<std::unique_ptr<TargetValueConverter>(
230  ConverterCreateParameter param)>>
251 
252  auto factory = factories.find(param.target->columnType.get_type());
253 
254  if (factory != factories.end()) {
255  return factory->second(param);
256  } else {
257  throw std::runtime_error("Unsupported column type: " +
258  param.target->columnType.get_type_name());
259  }
260 }
const Catalog_Namespace::Catalog & cat
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
HOST DEVICE int get_size() const
Definition: sqltypes.h:333
Definition: sqltypes.h:51
SQLTypes
Definition: sqltypes.h:40
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
std::unique_ptr< ArrayValueConverter< typename ELEMENT_FACTORY::ConverterType > > create(ConverterCreateParameter param)
StringDictionaryProxy * literals_dictionary
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:323
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:331
std::string get_type_name() const
Definition: sqltypes.h:426
std::unique_ptr< ConverterType > create(ConverterCreateParameter param)
std::unique_ptr< ConverterType > create(ConverterCreateParameter param)
SQLTypeInfoCore get_elem_type() const
Definition: sqltypes.h:632
#define NULL_INT
Definition: sqltypes.h:173
Definition: sqltypes.h:54
Definition: sqltypes.h:55
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
constexpr float inline_fp_null_value< float >()
constexpr double inline_fp_null_value< double >()
Definition: sqltypes.h:43
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:332
#define CHECK(condition)
Definition: Logger.h:187
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
Definition: sqltypes.h:47
SQLTypeInfo columnType
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)
const SQLTypeInfo & get_type_info() const
std::unique_ptr< TargetValueConverter > create(ConverterCreateParameter param)
std::unique_ptr< TargetValueConverter > operator()(ConverterCreateParameter param)