2011-10-31 17:55:06 +00:00
|
|
|
#pragma once
|
2010-03-18 19:32:14 +00:00
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
#include <map>
|
|
|
|
#include <list>
|
2010-03-18 19:32:14 +00:00
|
|
|
#include <string>
|
2017-12-25 21:57:29 +00:00
|
|
|
#include <set>
|
2017-08-01 01:58:18 +00:00
|
|
|
#include <initializer_list>
|
2010-03-18 19:32:14 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/IDataType.h>
|
|
|
|
#include <Core/Names.h>
|
2010-03-18 19:32:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
struct NameAndTypePair
|
2014-07-09 10:46:26 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
String name;
|
|
|
|
DataTypePtr type;
|
2014-07-09 10:46:26 +00:00
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
NameAndTypePair() {}
|
|
|
|
NameAndTypePair(const String & name_, const DataTypePtr & type_) : name(name_), type(type_) {}
|
2014-07-09 11:45:51 +00:00
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
bool operator<(const NameAndTypePair & rhs) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return std::forward_as_tuple(name, type->getName()) < std::forward_as_tuple(rhs.name, rhs.type->getName());
|
|
|
|
}
|
2014-07-09 10:46:26 +00:00
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
bool operator==(const NameAndTypePair & rhs) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-23 01:55:46 +00:00
|
|
|
return name == rhs.name && type->equals(*rhs.type);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2014-07-09 10:46:26 +00:00
|
|
|
};
|
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
using NamesAndTypes = std::vector<NameAndTypePair>;
|
|
|
|
|
|
|
|
class NamesAndTypesList : public std::list<NameAndTypePair>
|
2014-07-09 13:39:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList() {}
|
2017-12-25 21:57:19 +00:00
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList(std::initializer_list<NameAndTypePair> init) : std::list<NameAndTypePair>(init) {}
|
2017-12-25 21:57:19 +00:00
|
|
|
|
|
|
|
template <typename Iterator>
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList(Iterator begin, Iterator end) : std::list<NameAndTypePair>(begin, end) {}
|
|
|
|
|
2017-08-01 01:58:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void readText(ReadBuffer & buf);
|
|
|
|
void writeText(WriteBuffer & buf) const;
|
2014-07-09 13:39:19 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String toString() const;
|
2017-12-25 21:57:29 +00:00
|
|
|
static NamesAndTypesList parse(const String & s);
|
2014-07-10 08:40:59 +00:00
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/// All `rhs` elements must be different.
|
2017-12-25 21:57:29 +00:00
|
|
|
bool isSubsetOf(const NamesAndTypesList & rhs) const;
|
2014-07-10 08:40:59 +00:00
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/// Hamming distance between sets
|
|
|
|
/// (in other words, the added and deleted columns are counted once, the columns that changed the type - twice).
|
2017-12-25 21:57:29 +00:00
|
|
|
size_t sizeOfDifference(const NamesAndTypesList & rhs) const;
|
2014-07-14 14:07:47 +00:00
|
|
|
|
2018-11-13 12:51:55 +00:00
|
|
|
/// If an element changes type, it is present both in deleted (with the old type) and in added (with the new type).
|
|
|
|
void getDifference(const NamesAndTypesList & rhs, NamesAndTypesList & deleted, NamesAndTypesList & added) const;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Names getNames() const;
|
2018-01-02 05:54:34 +00:00
|
|
|
DataTypes getTypes() const;
|
2014-07-14 14:07:47 +00:00
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/// Leave only the columns whose names are in the `names`. In `names` there can be superfluous columns.
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList filter(const NameSet & names) const;
|
2014-07-15 09:56:17 +00:00
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/// Leave only the columns whose names are in the `names`. In `names` there can be superfluous columns.
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList filter(const Names & names) const;
|
2014-07-17 13:41:47 +00:00
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/// Unlike `filter`, returns columns in the order in which they go in `names`.
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList addTypes(const Names & names) const;
|
2018-03-10 17:03:57 +00:00
|
|
|
|
2020-02-17 15:44:13 +00:00
|
|
|
/// Check that column contains in list
|
2018-03-10 17:03:57 +00:00
|
|
|
bool contains(const String & name) const;
|
2020-01-17 13:54:22 +00:00
|
|
|
|
2020-02-17 15:44:13 +00:00
|
|
|
/// Try to get column by name, return empty optional if column not found
|
2020-01-17 13:54:22 +00:00
|
|
|
std::optional<NameAndTypePair> tryGetByName(const std::string & name) const;
|
2014-07-09 13:39:19 +00:00
|
|
|
};
|
|
|
|
|
2010-03-18 19:32:14 +00:00
|
|
|
}
|