1 | #pragma once |
2 | |
3 | #include <DataTypes/DataTypeWithSimpleSerialization.h> |
4 | |
5 | |
6 | namespace DB |
7 | { |
8 | |
9 | /** Tuple data type. |
10 | * Used as an intermediate result when evaluating expressions. |
11 | * Also can be used as a column - the result of the query execution. |
12 | * |
13 | * Tuple elements can have names. |
14 | * If an element is unnamed, it will have automatically assigned name like '1', '2', '3' corresponding to its position. |
15 | * Manually assigned names must not begin with digit. Names must be unique. |
16 | * |
17 | * All tuples with same size and types of elements are equivalent for expressions, regardless to names of elements. |
18 | */ |
19 | class DataTypeTuple final : public DataTypeWithSimpleSerialization |
20 | { |
21 | private: |
22 | DataTypes elems; |
23 | Strings names; |
24 | bool have_explicit_names; |
25 | public: |
26 | static constexpr bool is_parametric = true; |
27 | |
28 | DataTypeTuple(const DataTypes & elems); |
29 | DataTypeTuple(const DataTypes & elems, const Strings & names); |
30 | |
31 | TypeIndex getTypeId() const override { return TypeIndex::Tuple; } |
32 | std::string doGetName() const override; |
33 | const char * getFamilyName() const override { return "Tuple" ; } |
34 | |
35 | bool canBeInsideNullable() const override { return false; } |
36 | |
37 | void serializeBinary(const Field & field, WriteBuffer & ostr) const override; |
38 | void deserializeBinary(Field & field, ReadBuffer & istr) const override; |
39 | void serializeBinary(const IColumn & column, size_t row_num, WriteBuffer & ostr) const override; |
40 | void deserializeBinary(IColumn & column, ReadBuffer & istr) const override; |
41 | void serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
42 | void deserializeText(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override; |
43 | void serializeTextJSON(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
44 | void deserializeTextJSON(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override; |
45 | void serializeTextXML(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
46 | |
47 | /// Tuples in CSV format will be serialized as separate columns (that is, losing their nesting in the tuple). |
48 | void serializeTextCSV(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
49 | void deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override; |
50 | |
51 | /** Each sub-column in a tuple is serialized in separate stream. |
52 | */ |
53 | void enumerateStreams(const StreamCallback & callback, SubstreamPath & path) const override; |
54 | |
55 | void serializeBinaryBulkStatePrefix( |
56 | SerializeBinaryBulkSettings & settings, |
57 | SerializeBinaryBulkStatePtr & state) const override; |
58 | |
59 | void serializeBinaryBulkStateSuffix( |
60 | SerializeBinaryBulkSettings & settings, |
61 | SerializeBinaryBulkStatePtr & state) const override; |
62 | |
63 | void deserializeBinaryBulkStatePrefix( |
64 | DeserializeBinaryBulkSettings & settings, |
65 | DeserializeBinaryBulkStatePtr & state) const override; |
66 | |
67 | void serializeBinaryBulkWithMultipleStreams( |
68 | const IColumn & column, |
69 | size_t offset, |
70 | size_t limit, |
71 | SerializeBinaryBulkSettings & settings, |
72 | SerializeBinaryBulkStatePtr & state) const override; |
73 | |
74 | void deserializeBinaryBulkWithMultipleStreams( |
75 | IColumn & column, |
76 | size_t limit, |
77 | DeserializeBinaryBulkSettings & settings, |
78 | DeserializeBinaryBulkStatePtr & state) const override; |
79 | |
80 | void serializeProtobuf(const IColumn & column, size_t row_num, ProtobufWriter & protobuf, size_t & value_index) const override; |
81 | void deserializeProtobuf(IColumn & column, ProtobufReader & reader, bool allow_add_row, bool & row_added) const override; |
82 | |
83 | MutableColumnPtr createColumn() const override; |
84 | |
85 | Field getDefault() const override; |
86 | void insertDefaultInto(IColumn & column) const override; |
87 | |
88 | bool equals(const IDataType & rhs) const override; |
89 | |
90 | bool isParametric() const override { return true; } |
91 | bool haveSubtypes() const override { return !elems.empty(); } |
92 | bool isComparable() const override; |
93 | bool textCanContainOnlyValidUTF8() const override; |
94 | bool haveMaximumSizeOfValue() const override; |
95 | size_t getMaximumSizeOfValueInMemory() const override; |
96 | size_t getSizeOfValueInMemory() const override; |
97 | |
98 | const DataTypes & getElements() const { return elems; } |
99 | const Strings & getElementNames() const { return names; } |
100 | |
101 | size_t getPositionByName(const String & name) const; |
102 | |
103 | bool haveExplicitNames() const { return have_explicit_names; } |
104 | }; |
105 | |
106 | } |
107 | |
108 | |