1#pragma once
2
3#include <DataTypes/IDataType.h>
4#include <Parsers/IAST_fwd.h>
5#include <Common/IFactoryWithAliases.h>
6
7
8#include <functional>
9#include <memory>
10#include <unordered_map>
11
12
13namespace DB
14{
15
16class IDataType;
17using DataTypePtr = std::shared_ptr<const IDataType>;
18
19/** Creates a data type by name of data type family and parameters.
20 */
21class DataTypeFactory final : private boost::noncopyable, public IFactoryWithAliases<std::function<DataTypePtr(const String & type_name, const ASTPtr & parameters)>>
22{
23private:
24 using SimpleCreator = std::function<DataTypePtr(const String & type_name)>;
25 using DataTypesDictionary = std::unordered_map<String, Creator>;
26 using CreatorWithCustom = std::function<std::pair<DataTypePtr, DataTypeCustomDescPtr>(const String & type_name, const ASTPtr & parameters)>;
27 using SimpleCreatorWithCustom = std::function<std::pair<DataTypePtr, DataTypeCustomDescPtr>(const String & type_name)>;
28
29public:
30 static DataTypeFactory & instance();
31
32 DataTypePtr get(const String & full_name) const;
33 DataTypePtr get(const String & family_name, const ASTPtr & parameters) const;
34 DataTypePtr get(const ASTPtr & ast) const;
35
36 /// Register a type family by its name.
37 void registerDataType(const String & family_name, Creator creator, CaseSensitiveness case_sensitiveness = CaseSensitive);
38
39 /// Register a simple data type, that have no parameters.
40 void registerSimpleDataType(const String & name, SimpleCreator creator, CaseSensitiveness case_sensitiveness = CaseSensitive);
41
42 /// Register a customized type family
43 void registerDataTypeCustom(const String & family_name, CreatorWithCustom creator, CaseSensitiveness case_sensitiveness = CaseSensitive);
44
45 /// Register a simple customized data type
46 void registerSimpleDataTypeCustom(const String & name, SimpleCreatorWithCustom creator, CaseSensitiveness case_sensitiveness = CaseSensitive);
47
48private:
49 const Creator& findCreatorByName(const String & family_name) const;
50
51private:
52 DataTypesDictionary data_types;
53
54 /// Case insensitive data types will be additionally added here with lowercased name.
55 DataTypesDictionary case_insensitive_data_types;
56
57 DataTypeFactory();
58 ~DataTypeFactory() override;
59
60 const DataTypesDictionary & getCreatorMap() const override { return data_types; }
61
62 const DataTypesDictionary & getCaseInsensitiveCreatorMap() const override { return case_insensitive_data_types; }
63
64 String getFactoryName() const override { return "DataTypeFactory"; }
65};
66
67void registerDataTypeNumbers(DataTypeFactory & factory);
68void registerDataTypeDecimal(DataTypeFactory & factory);
69void registerDataTypeDate(DataTypeFactory & factory);
70void registerDataTypeDateTime(DataTypeFactory & factory);
71void registerDataTypeString(DataTypeFactory & factory);
72void registerDataTypeFixedString(DataTypeFactory & factory);
73void registerDataTypeEnum(DataTypeFactory & factory);
74void registerDataTypeArray(DataTypeFactory & factory);
75void registerDataTypeTuple(DataTypeFactory & factory);
76void registerDataTypeNullable(DataTypeFactory & factory);
77void registerDataTypeNothing(DataTypeFactory & factory);
78void registerDataTypeUUID(DataTypeFactory & factory);
79void registerDataTypeAggregateFunction(DataTypeFactory & factory);
80void registerDataTypeNested(DataTypeFactory & factory);
81void registerDataTypeInterval(DataTypeFactory & factory);
82void registerDataTypeLowCardinality(DataTypeFactory & factory);
83void registerDataTypeDomainIPv4AndIPv6(DataTypeFactory & factory);
84void registerDataTypeDomainSimpleAggregateFunction(DataTypeFactory & factory);
85void registerDataTypeDateTime64(DataTypeFactory & factory);
86
87}
88