forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDataTypeEnum.h
More file actions
102 lines (76 loc) · 3.87 KB
/
Copy pathDataTypeEnum.h
File metadata and controls
102 lines (76 loc) · 3.87 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
#pragma once
#include <vector>
#include <DataTypes/IDataType.h>
#include <DataTypes/EnumValues.h>
#include <Columns/ColumnVector.h>
#include <Parsers/IAST_fwd.h>
namespace DB
{
class IDataTypeEnum : public IDataType
{
public:
virtual Field castToName(const Field & value_or_name) const = 0;
virtual Field castToValue(const Field & value_or_name) const = 0;
bool isParametric() const override { return true; }
bool haveSubtypes() const override { return false; }
bool isValueRepresentedByNumber() const override { return true; }
bool isValueRepresentedByInteger() const override { return true; }
bool isValueUnambiguouslyRepresentedInContiguousMemoryRegion() const override { return true; }
bool haveMaximumSizeOfValue() const override { return true; }
bool isCategorial() const override { return true; }
bool canBeInsideNullable() const override { return true; }
bool isComparable() const override { return true; }
virtual bool contains(const IDataType & rhs) const = 0;
virtual bool isAdd() const = 0;
};
template <typename Type>
class DataTypeEnum final : public IDataTypeEnum, public EnumValues<Type>
{
public:
using FieldType = Type;
using ColumnType = ColumnVector<FieldType>;
static constexpr auto type_id = sizeof(FieldType) == 1 ? TypeIndex::Enum8 : TypeIndex::Enum16;
using typename EnumValues<Type>::Values;
using RelativeFlags = std::vector<UInt8>;
static constexpr bool is_parametric = true;
private:
std::string type_name;
static std::string generateName(const Values & values);
bool is_add; // created by ALTER ... ADD ENUM VALUES
/// Aligned with temporary `ADD ENUM VALUES` elements in parser order.
/// Whenever this vector is populated, the base `EnumValues` must use
/// `ValidationMode::TemporaryAdd` so the element order stays unchanged.
/// `1` marks shorthand values remapped relative to the base enum.
RelativeFlags relative_flags;
public:
explicit DataTypeEnum(const Values & values_, bool is_add_ = false, RelativeFlags relative_flags_ = {});
std::string doGetName() const override { return type_name; }
const char * getFamilyName() const override;
TypeIndex getTypeId() const override { return type_id; }
TypeIndex getColumnType() const override { return sizeof(FieldType) == 1 ? TypeIndex::Int8 : TypeIndex::Int16; }
Field castToName(const Field & value_or_name) const override;
Field castToValue(const Field & value_or_name) const override;
MutableColumnPtr createColumn() const override { return ColumnType::create(); }
Field getDefault() const override;
Type getDefaultValue() const;
void insertDefaultInto(IColumn & column) const override;
bool equals(const IDataType & rhs) const override;
bool textCanContainOnlyValidUTF8() const override;
size_t getSizeOfValueInMemory() const override { return sizeof(FieldType); }
/// Check current Enum type extends another Enum type (contains all the same values and doesn't override name's with other values)
/// Example:
/// Enum('a' = 1, 'b' = 2) -> Enum('c' = 1, 'b' = 2, 'd' = 3) OK
/// Enum('a' = 1, 'b' = 2) -> Enum('a' = 2, 'b' = 1) NOT OK
bool contains(const IDataType & rhs) const override;
SerializationPtr doGetSerialization(const SerializationInfoSettings & settings) const override;
void updateHashImpl(SipHash & hash) const override;
bool isAdd() const override { return is_add; }
bool isRelativeAt(size_t index) const { return index < relative_flags.size() && relative_flags[index]; }
size_t getRelativeFlagsSize() const { return relative_flags.size(); }
};
template <typename TypeBase>
DataTypePtr mergeEnumTypes(const DataTypeEnum<TypeBase> & base, const DataTypeEnum<TypeBase> & add);
DataTypePtr createEnumAdd(const ASTPtr & arguments, bool is_enum16);
using DataTypeEnum8 = DataTypeEnum<Int8>;
using DataTypeEnum16 = DataTypeEnum<Int16>;
}