1#include <Functions/IFunctionImpl.h>
2#include <Functions/FunctionFactory.h>
3#include <DataTypes/DataTypeArray.h>
4#include <DataTypes/DataTypesNumber.h>
5#include <DataTypes/DataTypeDate.h>
6#include <DataTypes/DataTypeDateTime.h>
7#include <DataTypes/DataTypeDateTime64.h>
8#include <DataTypes/DataTypeString.h>
9#include <Columns/ColumnArray.h>
10#include <Columns/ColumnString.h>
11#include <Columns/ColumnsNumber.h>
12
13
14namespace DB
15{
16
17/// TODO Make it simple.
18
19template <typename Type> struct TypeToColumnType { using ColumnType = ColumnVector<Type>; };
20template <> struct TypeToColumnType<String> { using ColumnType = ColumnString; };
21
22template <typename DataType> struct DataTypeToName : TypeName<typename DataType::FieldType> { };
23template <> struct DataTypeToName<DataTypeDate> { static std::string get() { return "Date"; } };
24template <> struct DataTypeToName<DataTypeDateTime> { static std::string get() { return "DateTime"; } };
25
26
27template <typename DataType>
28struct FunctionEmptyArray : public IFunction
29{
30 static constexpr auto base_name = "emptyArray";
31 static const String name;
32 static FunctionPtr create(const Context &) { return std::make_shared<FunctionEmptyArray>(); }
33
34private:
35 String getName() const override
36 {
37 return name;
38 }
39
40 size_t getNumberOfArguments() const override { return 0; }
41
42 DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override
43 {
44 return std::make_shared<DataTypeArray>(std::make_shared<DataType>());
45 }
46
47 void executeImpl(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) override
48 {
49 using UnderlyingColumnType = typename TypeToColumnType<typename DataType::FieldType>::ColumnType;
50
51 block.getByPosition(result).column = ColumnArray::create(
52 UnderlyingColumnType::create(),
53 ColumnArray::ColumnOffsets::create(input_rows_count, 0));
54 }
55};
56
57
58template <typename DataType>
59const String FunctionEmptyArray<DataType>::name = FunctionEmptyArray::base_name + String(DataTypeToName<DataType>::get());
60
61using FunctionEmptyArrayUInt8 = FunctionEmptyArray<DataTypeUInt8>;
62using FunctionEmptyArrayUInt16 = FunctionEmptyArray<DataTypeUInt16>;
63using FunctionEmptyArrayUInt32 = FunctionEmptyArray<DataTypeUInt32>;
64using FunctionEmptyArrayUInt64 = FunctionEmptyArray<DataTypeUInt64>;
65using FunctionEmptyArrayInt8 = FunctionEmptyArray<DataTypeInt8>;
66using FunctionEmptyArrayInt16 = FunctionEmptyArray<DataTypeInt16>;
67using FunctionEmptyArrayInt32 = FunctionEmptyArray<DataTypeInt32>;
68using FunctionEmptyArrayInt64 = FunctionEmptyArray<DataTypeInt64>;
69using FunctionEmptyArrayFloat32 = FunctionEmptyArray<DataTypeFloat32>;
70using FunctionEmptyArrayFloat64 = FunctionEmptyArray<DataTypeFloat64>;
71using FunctionEmptyArrayDate = FunctionEmptyArray<DataTypeDate>;
72using FunctionEmptyArrayDateTime = FunctionEmptyArray<DataTypeDateTime>;
73using FunctionEmptyArrayString = FunctionEmptyArray<DataTypeString>;
74
75
76void registerFunctionsEmptyArray(FunctionFactory & factory)
77{
78 factory.registerFunction<FunctionEmptyArrayUInt8>();
79 factory.registerFunction<FunctionEmptyArrayUInt16>();
80 factory.registerFunction<FunctionEmptyArrayUInt32>();
81 factory.registerFunction<FunctionEmptyArrayUInt64>();
82 factory.registerFunction<FunctionEmptyArrayInt8>();
83 factory.registerFunction<FunctionEmptyArrayInt16>();
84 factory.registerFunction<FunctionEmptyArrayInt32>();
85 factory.registerFunction<FunctionEmptyArrayInt64>();
86 factory.registerFunction<FunctionEmptyArrayFloat32>();
87 factory.registerFunction<FunctionEmptyArrayFloat64>();
88 factory.registerFunction<FunctionEmptyArrayDate>();
89 factory.registerFunction<FunctionEmptyArrayDateTime>();
90 factory.registerFunction<FunctionEmptyArrayString>();
91}
92
93}
94