1#pragma once
2
3#include <map>
4#include <list>
5#include <string>
6#include <set>
7#include <initializer_list>
8
9#include <DataTypes/IDataType.h>
10#include <Core/Names.h>
11
12
13namespace DB
14{
15
16struct NameAndTypePair
17{
18 String name;
19 DataTypePtr type;
20
21 NameAndTypePair() {}
22 NameAndTypePair(const String & name_, const DataTypePtr & type_) : name(name_), type(type_) {}
23
24 bool operator<(const NameAndTypePair & rhs) const
25 {
26 return std::forward_as_tuple(name, type->getName()) < std::forward_as_tuple(rhs.name, rhs.type->getName());
27 }
28
29 bool operator==(const NameAndTypePair & rhs) const
30 {
31 return name == rhs.name && type->equals(*rhs.type);
32 }
33};
34
35using NamesAndTypes = std::vector<NameAndTypePair>;
36
37class NamesAndTypesList : public std::list<NameAndTypePair>
38{
39public:
40 NamesAndTypesList() {}
41
42 NamesAndTypesList(std::initializer_list<NameAndTypePair> init) : std::list<NameAndTypePair>(init) {}
43
44 template <typename Iterator>
45 NamesAndTypesList(Iterator begin, Iterator end) : std::list<NameAndTypePair>(begin, end) {}
46
47
48 void readText(ReadBuffer & buf);
49 void writeText(WriteBuffer & buf) const;
50
51 String toString() const;
52 static NamesAndTypesList parse(const String & s);
53
54 /// All `rhs` elements must be different.
55 bool isSubsetOf(const NamesAndTypesList & rhs) const;
56
57 /// Hamming distance between sets
58 /// (in other words, the added and deleted columns are counted once, the columns that changed the type - twice).
59 size_t sizeOfDifference(const NamesAndTypesList & rhs) const;
60
61 /// If an element changes type, it is present both in deleted (with the old type) and in added (with the new type).
62 void getDifference(const NamesAndTypesList & rhs, NamesAndTypesList & deleted, NamesAndTypesList & added) const;
63
64 Names getNames() const;
65 DataTypes getTypes() const;
66
67 /// Leave only the columns whose names are in the `names`. In `names` there can be superfluous columns.
68 NamesAndTypesList filter(const NameSet & names) const;
69
70 /// Leave only the columns whose names are in the `names`. In `names` there can be superfluous columns.
71 NamesAndTypesList filter(const Names & names) const;
72
73 /// Unlike `filter`, returns columns in the order in which they go in `names`.
74 NamesAndTypesList addTypes(const Names & names) const;
75
76 bool contains(const String & name) const;
77};
78
79}
80