1#include "GatherUtils.h"
2#include "Sinks.h"
3#include "Sources.h"
4#include <Core/TypeListNumber.h>
5
6namespace DB::GatherUtils
7{
8/// Creates IValueSource from Column
9
10template <typename... Types>
11struct ValueSourceCreator;
12
13template <typename Type, typename... Types>
14struct ValueSourceCreator<Type, Types...>
15{
16 static std::unique_ptr<IValueSource> create(const IColumn & col, const NullMap * null_map, bool is_const, size_t total_rows)
17 {
18 using ColVecType = std::conditional_t<IsDecimalNumber<Type>, ColumnDecimal<Type>, ColumnVector<Type>>;
19
20 if (auto column_vector = typeid_cast<const ColVecType *>(&col))
21 {
22 if (null_map)
23 {
24 if (is_const)
25 return std::make_unique<ConstSource<NullableValueSource<NumericValueSource<Type>>>>(*column_vector, *null_map, total_rows);
26 return std::make_unique<NullableValueSource<NumericValueSource<Type>>>(*column_vector, *null_map);
27 }
28 if (is_const)
29 return std::make_unique<ConstSource<NumericValueSource<Type>>>(*column_vector, total_rows);
30 return std::make_unique<NumericValueSource<Type>>(*column_vector);
31 }
32
33 return ValueSourceCreator<Types...>::create(col, null_map, is_const, total_rows);
34 }
35};
36
37template <>
38struct ValueSourceCreator<>
39{
40 static std::unique_ptr<IValueSource> create(const IColumn & col, const NullMap * null_map, bool is_const, size_t total_rows)
41 {
42 if (null_map)
43 {
44 if (is_const)
45 return std::make_unique<ConstSource<NullableValueSource<GenericValueSource>>>(col, *null_map, total_rows);
46 return std::make_unique<NullableValueSource<GenericValueSource>>(col, *null_map);
47 }
48 if (is_const)
49 return std::make_unique<ConstSource<GenericValueSource>>(col, total_rows);
50 return std::make_unique<GenericValueSource>(col);
51 }
52};
53
54std::unique_ptr<IValueSource> createValueSource(const IColumn & col, bool is_const, size_t total_rows)
55{
56 using Creator = typename ApplyTypeListForClass<ValueSourceCreator, TypeListNumbers>::Type;
57 if (auto column_nullable = typeid_cast<const ColumnNullable *>(&col))
58 {
59 return Creator::create(column_nullable->getNestedColumn(), &column_nullable->getNullMapData(), is_const, total_rows);
60 }
61 return Creator::create(col, nullptr, is_const, total_rows);
62}
63
64}
65