small refactoring of parsing enum values by numeric enum ids

(for CSV, TSV, JSON formats)
This commit is contained in:
Vasily Kozhukhovskiy 2020-10-16 10:50:20 +03:00
parent 04c7131489
commit bffa1ed28c
2 changed files with 19 additions and 27 deletions

View File

@ -149,12 +149,7 @@ template <typename Type>
void DataTypeEnum<Type>::deserializeTextEscaped(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
{
if (settings.tsv.input_format_enum_as_number)
{
FieldType x;
readText(x, istr);
static_cast<void>(getNameForValue(x));
assert_cast<ColumnType &>(column).getData().push_back(x);
}
assert_cast<ColumnType &>(column).getData().push_back(readValue(istr));
else
{
/// NOTE It would be nice to do without creating a temporary object - at least extract std::string out.
@ -182,12 +177,7 @@ template <typename Type>
void DataTypeEnum<Type>::deserializeWholeText(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
{
if (settings.tsv.input_format_enum_as_number)
{
FieldType x;
readText(x, istr);
static_cast<void>(getNameForValue(x));
assert_cast<ColumnType &>(column).getData().push_back(x);
}
assert_cast<ColumnType &>(column).getData().push_back(readValue(istr));
else
{
std::string field_name;
@ -211,13 +201,8 @@ void DataTypeEnum<Type>::serializeTextXML(const IColumn & column, size_t row_num
template <typename Type>
void DataTypeEnum<Type>::deserializeTextJSON(IColumn & column, ReadBuffer & istr, const FormatSettings &) const
{
if (*istr.position() != '"')
{
FieldType x;
readText(x, istr);
static_cast<void>(getNameForValue(x));
assert_cast<ColumnType &>(column).getData().push_back(x);
}
if (!istr.eof() && *istr.position() != '"')
assert_cast<ColumnType &>(column).getData().push_back(readValue(istr));
else
{
std::string field_name;
@ -236,12 +221,7 @@ template <typename Type>
void DataTypeEnum<Type>::deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
{
if (settings.csv.input_format_enum_as_number)
{
FieldType x;
readText(x, istr);
static_cast<void>(getNameForValue(x));
assert_cast<ColumnType &>(column).getData().push_back(x);
}
assert_cast<ColumnType &>(column).getData().push_back(readValue(istr));
else
{
std::string field_name;

View File

@ -66,13 +66,18 @@ public:
TypeIndex getTypeId() const override { return sizeof(FieldType) == 1 ? TypeIndex::Enum8 : TypeIndex::Enum16; }
const StringRef & getNameForValue(const FieldType & value) const
auto findByValue(const FieldType & value) const
{
const auto it = value_to_name_map.find(value);
if (it == std::end(value_to_name_map))
throw Exception{"Unexpected value " + toString(value) + " for type " + getName(), ErrorCodes::BAD_ARGUMENTS};
return it->second;
return it;
}
const StringRef & getNameForValue(const FieldType & value) const
{
return findByValue(value)->second;
}
FieldType getValue(StringRef field_name) const
@ -84,6 +89,13 @@ public:
return it->getMapped();
}
FieldType readValue(ReadBuffer & istr) const
{
FieldType x;
readText(x, istr);
return findByValue(x)->first;
}
Field castToName(const Field & value_or_name) const override;
Field castToValue(const Field & value_or_name) const override;