2015-12-21 13:15:13 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/IDataType.h>
|
|
|
|
#include <Columns/ColumnVector.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Common/HashTable/HashMap.h>
|
2015-12-21 13:15:13 +00:00
|
|
|
#include <vector>
|
2015-12-30 11:53:12 +00:00
|
|
|
#include <unordered_map>
|
2015-12-21 13:15:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-01-29 14:15:53 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 16:15:28 +00:00
|
|
|
|
2016-11-21 12:58:42 +00:00
|
|
|
class IDataTypeEnum : public IDataType
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
virtual Field castToName(const Field & value_or_name) const = 0;
|
|
|
|
virtual Field castToValue(const Field & value_or_name) const = 0;
|
2017-12-09 16:15:28 +00:00
|
|
|
|
|
|
|
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; }
|
2018-06-03 16:51:31 +00:00
|
|
|
bool isComparable() const override { return true; }
|
2016-11-21 12:58:42 +00:00
|
|
|
};
|
2015-12-21 13:15:13 +00:00
|
|
|
|
2017-03-12 10:13:45 +00:00
|
|
|
|
2015-12-21 13:15:13 +00:00
|
|
|
template <typename Type>
|
2016-11-21 12:58:42 +00:00
|
|
|
class DataTypeEnum final : public IDataTypeEnum
|
2015-12-21 13:15:13 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
using FieldType = Type;
|
|
|
|
using ColumnType = ColumnVector<FieldType>;
|
|
|
|
using Value = std::pair<std::string, FieldType>;
|
|
|
|
using Values = std::vector<Value>;
|
|
|
|
using NameToValueMap = HashMap<StringRef, FieldType, StringRefHash>;
|
|
|
|
using ValueToNameMap = std::unordered_map<FieldType, StringRef>;
|
2015-12-21 13:15:13 +00:00
|
|
|
|
2017-10-30 02:18:06 +00:00
|
|
|
static constexpr bool is_parametric = true;
|
|
|
|
|
2015-12-30 11:53:12 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
Values values;
|
|
|
|
NameToValueMap name_to_value_map;
|
|
|
|
ValueToNameMap value_to_name_map;
|
2018-08-10 04:02:56 +00:00
|
|
|
std::string type_name;
|
2015-12-25 15:43:38 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static std::string generateName(const Values & values);
|
|
|
|
void fillMaps();
|
2015-12-21 13:15:13 +00:00
|
|
|
|
|
|
|
public:
|
2018-01-23 22:56:46 +00:00
|
|
|
explicit DataTypeEnum(const Values & values_);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
const Values & getValues() const { return values; }
|
2018-12-13 13:41:47 +00:00
|
|
|
std::string doGetName() const override { return type_name; }
|
2017-07-16 03:05:40 +00:00
|
|
|
const char * getFamilyName() const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-08-24 12:55:31 +00:00
|
|
|
TypeIndex getTypeId() const override { return sizeof(FieldType) == 1 ? TypeIndex::Enum8 : TypeIndex::Enum16; }
|
2018-08-20 15:17:55 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const StringRef & getNameForValue(const FieldType & value) const
|
|
|
|
{
|
|
|
|
const auto it = value_to_name_map.find(value);
|
|
|
|
if (it == std::end(value_to_name_map))
|
2020-01-29 14:15:53 +00:00
|
|
|
throw Exception{"Unexpected value " + toString(value) + " for type " + getName(), ErrorCodes::BAD_ARGUMENTS};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2018-08-10 04:02:56 +00:00
|
|
|
FieldType getValue(StringRef field_name) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-08-10 04:02:56 +00:00
|
|
|
const auto it = name_to_value_map.find(field_name);
|
2019-08-20 09:58:44 +00:00
|
|
|
if (!it)
|
2020-01-29 14:15:53 +00:00
|
|
|
throw Exception{"Unknown element '" + field_name.toString() + "' for type " + getName(), ErrorCodes::BAD_ARGUMENTS};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-10-29 15:16:51 +00:00
|
|
|
return it->getMapped();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Field castToName(const Field & value_or_name) const override;
|
|
|
|
Field castToValue(const Field & value_or_name) const override;
|
|
|
|
|
|
|
|
void serializeBinary(const Field & field, WriteBuffer & ostr) const override;
|
|
|
|
void deserializeBinary(Field & field, ReadBuffer & istr) const override;
|
|
|
|
void serializeBinary(const IColumn & column, size_t row_num, WriteBuffer & ostr) const override;
|
|
|
|
void deserializeBinary(IColumn & column, ReadBuffer & istr) const override;
|
2018-06-05 21:39:01 +00:00
|
|
|
void serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
|
|
|
|
void serializeTextEscaped(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
|
|
|
|
void deserializeTextEscaped(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override;
|
|
|
|
void serializeTextQuoted(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
|
|
|
|
void deserializeTextQuoted(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override;
|
2019-05-18 21:07:23 +00:00
|
|
|
void deserializeWholeText(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override;
|
|
|
|
|
2018-06-05 21:39:01 +00:00
|
|
|
void serializeTextJSON(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
|
|
|
|
void deserializeTextJSON(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override;
|
2018-06-08 01:51:55 +00:00
|
|
|
void serializeTextXML(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
|
2018-06-05 21:39:01 +00:00
|
|
|
void serializeTextCSV(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
|
|
|
|
void deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-02-19 00:41:24 +00:00
|
|
|
void serializeBinaryBulk(const IColumn & column, WriteBuffer & ostr, const size_t offset, size_t limit) const override;
|
|
|
|
void deserializeBinaryBulk(IColumn & column, ReadBuffer & istr, const size_t limit, const double avg_value_size_hint) const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-02-26 14:06:05 +00:00
|
|
|
void serializeProtobuf(const IColumn & column, size_t row_num, ProtobufWriter & protobuf, size_t & value_index) const override;
|
2019-02-19 20:01:31 +00:00
|
|
|
void deserializeProtobuf(IColumn & column, ProtobufReader & protobuf, bool allow_add_row, bool & row_added) const override;
|
2019-01-23 19:41:18 +00:00
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
MutableColumnPtr createColumn() const override { return ColumnType::create(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
Field getDefault() const override;
|
2017-09-01 21:37:57 +00:00
|
|
|
void insertDefaultInto(IColumn & column) const override;
|
2017-12-09 06:32:22 +00:00
|
|
|
|
2017-12-23 01:05:29 +00:00
|
|
|
bool equals(const IDataType & rhs) const override;
|
|
|
|
|
2017-12-09 06:32:22 +00:00
|
|
|
bool textCanContainOnlyValidUTF8() const override;
|
2018-02-18 01:20:14 +00:00
|
|
|
size_t getSizeOfValueInMemory() const override { return sizeof(FieldType); }
|
2015-12-21 13:15:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-12-28 17:04:29 +00:00
|
|
|
using DataTypeEnum8 = DataTypeEnum<Int8>;
|
|
|
|
using DataTypeEnum16 = DataTypeEnum<Int16>;
|
2015-12-21 13:15:13 +00:00
|
|
|
|
|
|
|
}
|