forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldToDataType.cpp
More file actions
137 lines (110 loc) · 4.69 KB
/
FieldToDataType.cpp
File metadata and controls
137 lines (110 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <Common/FieldVisitors.h>
#include <DataTypes/FieldToDataType.h>
#include <DataTypes/DataTypeTuple.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypesDecimal.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeNothing.h>
#include <DataTypes/getLeastSupertype.h>
#include <DataTypes/DataTypeFactory.h>
#include <Common/Exception.h>
#include <ext/size.h>
namespace DB
{
namespace ErrorCodes
{
extern const int EMPTY_DATA_PASSED;
extern const int NOT_IMPLEMENTED;
}
DataTypePtr FieldToDataType::operator() (const Null &) const
{
return std::make_shared<DataTypeNullable>(std::make_shared<DataTypeNothing>());
}
DataTypePtr FieldToDataType::operator() (const UInt64 & x) const
{
if (x <= std::numeric_limits<UInt8>::max()) return std::make_shared<DataTypeUInt8>();
if (x <= std::numeric_limits<UInt16>::max()) return std::make_shared<DataTypeUInt16>();
if (x <= std::numeric_limits<UInt32>::max()) return std::make_shared<DataTypeUInt32>();
return std::make_shared<DataTypeUInt64>();
}
DataTypePtr FieldToDataType::operator() (const UInt128 &) const
{
throw Exception("There are no UInt128 literals in SQL", ErrorCodes::NOT_IMPLEMENTED);
}
DataTypePtr FieldToDataType::operator() (const Int64 & x) const
{
if (x <= std::numeric_limits<Int8>::max() && x >= std::numeric_limits<Int8>::min()) return std::make_shared<DataTypeInt8>();
if (x <= std::numeric_limits<Int16>::max() && x >= std::numeric_limits<Int16>::min()) return std::make_shared<DataTypeInt16>();
if (x <= std::numeric_limits<Int32>::max() && x >= std::numeric_limits<Int32>::min()) return std::make_shared<DataTypeInt32>();
return std::make_shared<DataTypeInt64>();
}
DataTypePtr FieldToDataType::operator() (const Int128 & x) const
{
if (x <= std::numeric_limits<Int8>::max() && x >= std::numeric_limits<Int8>::min()) return std::make_shared<DataTypeInt8>();
if (x <= std::numeric_limits<Int16>::max() && x >= std::numeric_limits<Int16>::min()) return std::make_shared<DataTypeInt16>();
if (x <= std::numeric_limits<Int32>::max() && x >= std::numeric_limits<Int32>::min()) return std::make_shared<DataTypeInt32>();
if (x <= std::numeric_limits<Int64>::max() && x >= std::numeric_limits<Int64>::min()) return std::make_shared<DataTypeInt64>();
return std::make_shared<DataTypeInt128>();
}
DataTypePtr FieldToDataType::operator() (const Float64 &) const
{
return std::make_shared<DataTypeFloat64>();
}
DataTypePtr FieldToDataType::operator() (const String &) const
{
return std::make_shared<DataTypeString>();
}
DataTypePtr FieldToDataType::operator() (const DecimalField<Decimal32> & x) const
{
using Type = DataTypeDecimal<Decimal32>;
return std::make_shared<Type>(Type::maxPrecision(), x.getScale());
}
DataTypePtr FieldToDataType::operator() (const DecimalField<Decimal64> & x) const
{
using Type = DataTypeDecimal<Decimal64>;
return std::make_shared<Type>(Type::maxPrecision(), x.getScale());
}
DataTypePtr FieldToDataType::operator() (const DecimalField<Decimal128> & x) const
{
using Type = DataTypeDecimal<Decimal128>;
return std::make_shared<Type>(Type::maxPrecision(), x.getScale());
}
DataTypePtr FieldToDataType::operator() (const DecimalField<Decimal256> & x) const
{
using Type = DataTypeDecimal<Decimal256>;
return std::make_shared<Type>(Type::maxPrecision(), x.getScale());
}
DataTypePtr FieldToDataType::operator() (const Array & x) const
{
DataTypes element_types;
element_types.reserve(x.size());
for (const Field & elem : x)
element_types.emplace_back(applyVisitor(FieldToDataType(), elem));
return std::make_shared<DataTypeArray>(getLeastSupertype(element_types));
}
DataTypePtr FieldToDataType::operator() (const Tuple & tuple) const
{
if (tuple.empty())
throw Exception("Cannot infer type of an empty tuple", ErrorCodes::EMPTY_DATA_PASSED);
DataTypes element_types;
element_types.reserve(ext::size(tuple));
for (const auto & element : tuple)
element_types.push_back(applyVisitor(FieldToDataType(), element));
return std::make_shared<DataTypeTuple>(element_types);
}
DataTypePtr FieldToDataType::operator() (const AggregateFunctionStateData & x) const
{
const auto & name = static_cast<const AggregateFunctionStateData &>(x).name;
return DataTypeFactory::instance().get(name);
}
DataTypePtr FieldToDataType::operator() (const UInt256 &) const
{
throw Exception("There are no UInt256 literals in SQL", ErrorCodes::NOT_IMPLEMENTED);
}
DataTypePtr FieldToDataType::operator() (const Int256 &) const
{
throw Exception("There are no Int256 literals in SQL", ErrorCodes::NOT_IMPLEMENTED);
}
}