1#pragma once
2#include <DataTypes/IDataType.h>
3#include <Columns/IColumnUnique.h>
4
5namespace DB
6{
7
8class DataTypeLowCardinality : public IDataType
9{
10private:
11 DataTypePtr dictionary_type;
12
13public:
14 DataTypeLowCardinality(DataTypePtr dictionary_type_);
15
16 const DataTypePtr & getDictionaryType() const { return dictionary_type; }
17
18 String doGetName() const override
19 {
20 return "LowCardinality(" + dictionary_type->getName() + ")";
21 }
22 const char * getFamilyName() const override { return "LowCardinality"; }
23 TypeIndex getTypeId() const override { return TypeIndex::LowCardinality; }
24
25 void enumerateStreams(const StreamCallback & callback, SubstreamPath & path) const override;
26
27 void serializeBinaryBulkStatePrefix(
28 SerializeBinaryBulkSettings & settings,
29 SerializeBinaryBulkStatePtr & state) const override;
30
31 void serializeBinaryBulkStateSuffix(
32 SerializeBinaryBulkSettings & settings,
33 SerializeBinaryBulkStatePtr & state) const override;
34
35 void deserializeBinaryBulkStatePrefix(
36 DeserializeBinaryBulkSettings & settings,
37 DeserializeBinaryBulkStatePtr & state) const override;
38
39 void serializeBinaryBulkWithMultipleStreams(
40 const IColumn & column,
41 size_t offset,
42 size_t limit,
43 SerializeBinaryBulkSettings & settings,
44 SerializeBinaryBulkStatePtr & state) const override;
45
46 void deserializeBinaryBulkWithMultipleStreams(
47 IColumn & column,
48 size_t limit,
49 DeserializeBinaryBulkSettings & settings,
50 DeserializeBinaryBulkStatePtr & state) const override;
51
52 void serializeBinary(const Field & field, WriteBuffer & ostr) const override;
53 void deserializeBinary(Field & field, ReadBuffer & istr) const override;
54 void serializeBinary(const IColumn & column, size_t row_num, WriteBuffer & ostr) const override;
55 void deserializeBinary(IColumn & column, ReadBuffer & istr) const override;
56 void serializeTextEscaped(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const override;
57 void deserializeTextEscaped(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const override;
58 void serializeTextQuoted(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const override;
59 void deserializeTextQuoted(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const override;
60 void deserializeWholeText(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const override;
61 void serializeTextCSV(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const override;
62 void deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const override;
63 void serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const override;
64 void serializeTextJSON(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const override;
65 void deserializeTextJSON(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const override;
66 void serializeTextXML(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const override;
67 void serializeProtobuf(const IColumn & column, size_t row_num, ProtobufWriter & protobuf, size_t & value_index) const override;
68 void deserializeProtobuf(IColumn & column, ProtobufReader & protobuf, bool allow_add_row, bool & row_added) const override;
69
70 MutableColumnPtr createColumn() const override;
71
72 Field getDefault() const override;
73
74 bool equals(const IDataType & rhs) const override;
75
76 bool isParametric() const override { return true; }
77 bool haveSubtypes() const override { return true; }
78 bool cannotBeStoredInTables() const override { return dictionary_type->cannotBeStoredInTables(); }
79 bool shouldAlignRightInPrettyFormats() const override { return dictionary_type->shouldAlignRightInPrettyFormats(); }
80 bool textCanContainOnlyValidUTF8() const override { return dictionary_type->textCanContainOnlyValidUTF8(); }
81 bool isComparable() const override { return dictionary_type->isComparable(); }
82 bool canBeComparedWithCollation() const override { return dictionary_type->canBeComparedWithCollation(); }
83 bool canBeUsedAsVersion() const override { return dictionary_type->canBeUsedAsVersion(); }
84 bool isSummable() const override { return dictionary_type->isSummable(); }
85 bool canBeUsedInBitOperations() const override { return dictionary_type->canBeUsedInBitOperations(); }
86 bool canBeUsedInBooleanContext() const override { return dictionary_type->canBeUsedInBooleanContext(); }
87 bool isValueRepresentedByNumber() const override { return dictionary_type->isValueRepresentedByNumber(); }
88 bool isValueRepresentedByInteger() const override { return dictionary_type->isValueRepresentedByInteger(); }
89 bool isValueUnambiguouslyRepresentedInContiguousMemoryRegion() const override { return true; }
90 bool haveMaximumSizeOfValue() const override { return dictionary_type->haveMaximumSizeOfValue(); }
91 size_t getMaximumSizeOfValueInMemory() const override { return dictionary_type->getMaximumSizeOfValueInMemory(); }
92 size_t getSizeOfValueInMemory() const override { return dictionary_type->getSizeOfValueInMemory(); }
93 bool isCategorial() const override { return false; }
94 bool isNullable() const override { return false; }
95 bool onlyNull() const override { return false; }
96 bool lowCardinality() const override { return true; }
97
98 static MutableColumnUniquePtr createColumnUnique(const IDataType & keys_type);
99 static MutableColumnUniquePtr createColumnUnique(const IDataType & keys_type, MutableColumnPtr && keys);
100
101private:
102
103 template <typename ... Params>
104 using SerializeFunctionPtr = void (IDataType::*)(const IColumn &, size_t, Params ...) const;
105
106 template <typename... Params, typename... Args>
107 void serializeImpl(const IColumn & column, size_t row_num, SerializeFunctionPtr<Params...> func, Args &&... args) const;
108
109 template <typename ... Params>
110 using DeserializeFunctionPtr = void (IDataType::*)(IColumn &, Params ...) const;
111
112 template <typename ... Params, typename... Args>
113 void deserializeImpl(IColumn & column, DeserializeFunctionPtr<Params...> func, Args &&... args) const;
114
115 template <typename Creator>
116 static MutableColumnUniquePtr createColumnUniqueImpl(const IDataType & keys_type, const Creator & creator);
117};
118
119/// Returns dictionary type if type is DataTypeLowCardinality, type otherwise.
120DataTypePtr removeLowCardinality(const DataTypePtr & type);
121
122/// Remove LowCardinality recursively from all nested types.
123DataTypePtr recursiveRemoveLowCardinality(const DataTypePtr & type);
124
125/// Remove LowCardinality recursively from all nested columns.
126ColumnPtr recursiveRemoveLowCardinality(const ColumnPtr & column);
127
128/// Convert column of type from_type to type to_type by converting nested LowCardinality columns.
129ColumnPtr recursiveTypeConversion(const ColumnPtr & column, const DataTypePtr & from_type, const DataTypePtr & to_type);
130
131}
132