2015-01-26 15:27:51 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
2015-03-20 15:21:29 +00:00
|
|
|
#include <DB/DataTypes/DataTypeFactory.h>
|
|
|
|
#include <DB/IO/ReadBufferFromString.h>
|
2015-10-12 07:05:54 +00:00
|
|
|
#include <DB/IO/WriteBuffer.h>
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
2015-01-26 16:53:44 +00:00
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
2015-01-26 15:27:51 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2015-01-27 13:00:20 +00:00
|
|
|
#include <map>
|
2015-09-07 17:22:54 +00:00
|
|
|
#include <experimental/optional>
|
|
|
|
|
2015-01-26 15:27:51 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2015-03-20 15:21:29 +00:00
|
|
|
enum class AttributeUnderlyingType
|
2015-01-27 13:00:20 +00:00
|
|
|
{
|
2015-03-20 15:21:29 +00:00
|
|
|
UInt8,
|
|
|
|
UInt16,
|
|
|
|
UInt32,
|
|
|
|
UInt64,
|
|
|
|
Int8,
|
|
|
|
Int16,
|
|
|
|
Int32,
|
|
|
|
Int64,
|
|
|
|
Float32,
|
|
|
|
Float64,
|
|
|
|
String
|
2015-01-27 13:00:20 +00:00
|
|
|
};
|
|
|
|
|
2015-03-20 15:21:29 +00:00
|
|
|
inline AttributeUnderlyingType getAttributeUnderlyingType(const std::string & type)
|
2015-01-27 13:00:20 +00:00
|
|
|
{
|
2015-03-20 15:21:29 +00:00
|
|
|
static const std::unordered_map<std::string, AttributeUnderlyingType> dictionary{
|
|
|
|
{ "UInt8", AttributeUnderlyingType::UInt8 },
|
|
|
|
{ "UInt16", AttributeUnderlyingType::UInt16 },
|
|
|
|
{ "UInt32", AttributeUnderlyingType::UInt32 },
|
|
|
|
{ "UInt64", AttributeUnderlyingType::UInt64 },
|
|
|
|
{ "Int8", AttributeUnderlyingType::Int8 },
|
|
|
|
{ "Int16", AttributeUnderlyingType::Int16 },
|
|
|
|
{ "Int32", AttributeUnderlyingType::Int32 },
|
|
|
|
{ "Int64", AttributeUnderlyingType::Int64 },
|
|
|
|
{ "Float32", AttributeUnderlyingType::Float32 },
|
|
|
|
{ "Float64", AttributeUnderlyingType::Float64 },
|
|
|
|
{ "String", AttributeUnderlyingType::String },
|
|
|
|
{ "Date", AttributeUnderlyingType::UInt16 },
|
|
|
|
{ "DateTime", AttributeUnderlyingType::UInt32 },
|
2015-01-27 13:00:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto it = dictionary.find(type);
|
|
|
|
if (it != std::end(dictionary))
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
throw Exception{
|
|
|
|
"Unknown type " + type,
|
|
|
|
ErrorCodes::UNKNOWN_TYPE
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-03-20 15:21:29 +00:00
|
|
|
inline std::string toString(const AttributeUnderlyingType type)
|
2015-01-28 13:20:20 +00:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2015-03-20 15:21:29 +00:00
|
|
|
case AttributeUnderlyingType::UInt8: return "UInt8";
|
|
|
|
case AttributeUnderlyingType::UInt16: return "UInt16";
|
|
|
|
case AttributeUnderlyingType::UInt32: return "UInt32";
|
|
|
|
case AttributeUnderlyingType::UInt64: return "UInt64";
|
|
|
|
case AttributeUnderlyingType::Int8: return "Int8";
|
|
|
|
case AttributeUnderlyingType::Int16: return "Int16";
|
|
|
|
case AttributeUnderlyingType::Int32: return "Int32";
|
|
|
|
case AttributeUnderlyingType::Int64: return "Int64";
|
|
|
|
case AttributeUnderlyingType::Float32: return "Float32";
|
|
|
|
case AttributeUnderlyingType::Float64: return "Float64";
|
|
|
|
case AttributeUnderlyingType::String: return "String";
|
2015-01-28 13:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw Exception{
|
2015-02-10 14:50:43 +00:00
|
|
|
"Unknown attribute_type " + toString(static_cast<int>(type)),
|
2015-01-28 13:20:20 +00:00
|
|
|
ErrorCodes::ARGUMENT_OUT_OF_BOUND
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
/// Min and max lifetimes for a dictionary or it's entry
|
2015-05-26 12:24:31 +00:00
|
|
|
struct DictionaryLifetime final
|
2015-01-30 15:18:13 +00:00
|
|
|
{
|
|
|
|
std::uint64_t min_sec;
|
|
|
|
std::uint64_t max_sec;
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
DictionaryLifetime(const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix)
|
2015-01-30 15:18:13 +00:00
|
|
|
{
|
|
|
|
const auto & lifetime_min_key = config_prefix + ".min";
|
|
|
|
const auto has_min = config.has(lifetime_min_key);
|
2015-02-10 14:50:43 +00:00
|
|
|
|
|
|
|
this->min_sec = has_min ? config.getInt(lifetime_min_key) : config.getInt(config_prefix);
|
|
|
|
this->max_sec = has_min ? config.getInt(config_prefix + ".max") : this->min_sec;
|
2015-01-30 15:18:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
/** Holds the description of a single dictionary attribute:
|
|
|
|
* - name, used for lookup into dictionary and source;
|
2015-03-20 15:21:29 +00:00
|
|
|
* - type, used in conjunction with DataTypeFactory and getAttributeUnderlyingTypeByname;
|
2015-02-10 14:50:43 +00:00
|
|
|
* - null_value, used as a default value for non-existent entries in the dictionary,
|
|
|
|
* decimal representation for numeric attributes;
|
|
|
|
* - hierarchical, whether this attribute defines a hierarchy;
|
|
|
|
* - injective, whether the mapping to parent is injective (can be used for optimization of GROUP BY?)
|
|
|
|
*/
|
2015-05-26 12:24:31 +00:00
|
|
|
struct DictionaryAttribute final
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
2015-05-26 12:24:31 +00:00
|
|
|
const std::string name;
|
|
|
|
const AttributeUnderlyingType underlying_type;
|
|
|
|
const DataTypePtr type;
|
|
|
|
const std::string expression;
|
|
|
|
const Field null_value;
|
|
|
|
const bool hierarchical;
|
|
|
|
const bool injective;
|
2015-01-26 15:27:51 +00:00
|
|
|
};
|
|
|
|
|
2015-09-07 17:22:54 +00:00
|
|
|
struct DictionarySpecialAttribute final
|
|
|
|
{
|
|
|
|
const std::string name;
|
|
|
|
const std::string expression;
|
|
|
|
|
|
|
|
DictionarySpecialAttribute(const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix)
|
|
|
|
: name{config.getString(config_prefix + ".name", "")},
|
|
|
|
expression{config.getString(config_prefix + ".expression", "")}
|
|
|
|
{
|
|
|
|
if (name.empty() && !expression.empty())
|
|
|
|
throw Exception{
|
|
|
|
"Element " + config_prefix + ".name is empty",
|
|
|
|
ErrorCodes::BAD_ARGUMENTS
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
/// Name of identifier plus list of attributes
|
2015-05-26 12:24:31 +00:00
|
|
|
struct DictionaryStructure final
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
2015-11-12 14:28:23 +00:00
|
|
|
std::experimental::optional<DictionarySpecialAttribute> id;
|
|
|
|
std::experimental::optional<std::vector<DictionaryAttribute>> key;
|
2015-01-26 15:27:51 +00:00
|
|
|
std::vector<DictionaryAttribute> attributes;
|
2015-09-07 17:22:54 +00:00
|
|
|
std::experimental::optional<DictionarySpecialAttribute> range_min;
|
|
|
|
std::experimental::optional<DictionarySpecialAttribute> range_max;
|
|
|
|
bool has_expressions = false;
|
2015-01-26 15:27:51 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
DictionaryStructure(const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix)
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
2015-11-12 14:28:23 +00:00
|
|
|
const auto has_id = config.has(config_prefix + ".id");
|
|
|
|
const auto has_key = config.has(config_prefix + ".key");
|
|
|
|
|
|
|
|
if (has_key && has_id)
|
|
|
|
throw Exception{"Only one of 'id' and 'key' should be specified", ErrorCodes::BAD_ARGUMENTS};
|
|
|
|
|
|
|
|
if (has_id)
|
|
|
|
id.emplace(config, config_prefix + ".id");
|
|
|
|
else if (has_key)
|
|
|
|
{
|
2015-11-13 01:44:41 +00:00
|
|
|
key.emplace(getAttributes(config, config_prefix + ".key", false, false));
|
2015-11-12 14:28:23 +00:00
|
|
|
if (key->empty())
|
|
|
|
throw Exception{"Empty 'key' supplied", ErrorCodes::BAD_ARGUMENTS};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception{"Dictionary structure should specify either 'id' or 'key'", ErrorCodes::BAD_ARGUMENTS};
|
|
|
|
|
|
|
|
if (id)
|
|
|
|
{
|
|
|
|
if (id->name.empty())
|
|
|
|
throw Exception{"'id' cannot be empty", ErrorCodes::BAD_ARGUMENTS};
|
2015-01-26 15:27:51 +00:00
|
|
|
|
2015-11-12 14:28:23 +00:00
|
|
|
if (config.has(config_prefix + ".range_min"))
|
|
|
|
range_min.emplace(config, config_prefix + ".range_min");
|
2015-09-07 17:22:54 +00:00
|
|
|
|
2015-11-12 14:28:23 +00:00
|
|
|
if (config.has(config_prefix + ".range_max"))
|
|
|
|
range_max.emplace(config, config_prefix + ".range_max");
|
|
|
|
|
|
|
|
if (!id->expression.empty() ||
|
|
|
|
(range_min && !range_min->expression.empty()) ||
|
|
|
|
(range_max && !range_max->expression.empty()))
|
|
|
|
has_expressions = true;
|
|
|
|
}
|
2015-09-07 17:22:54 +00:00
|
|
|
|
2015-11-12 14:28:23 +00:00
|
|
|
attributes = getAttributes(config, config_prefix);
|
|
|
|
if (attributes.empty())
|
|
|
|
throw Exception{"Dictionary has no attributes defined", ErrorCodes::BAD_ARGUMENTS};
|
|
|
|
}
|
2015-09-07 17:22:54 +00:00
|
|
|
|
2015-11-12 14:28:23 +00:00
|
|
|
private:
|
|
|
|
std::vector<DictionaryAttribute> getAttributes(
|
|
|
|
const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix,
|
2015-11-13 01:44:41 +00:00
|
|
|
const bool hierarchy_allowed = true, const bool allow_null_values = true)
|
2015-11-12 14:28:23 +00:00
|
|
|
{
|
2015-01-26 15:27:51 +00:00
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
|
|
|
config.keys(config_prefix, keys);
|
|
|
|
auto has_hierarchy = false;
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2015-11-12 14:28:23 +00:00
|
|
|
std::vector<DictionaryAttribute> attributes;
|
|
|
|
|
2015-01-26 15:27:51 +00:00
|
|
|
for (const auto & key : keys)
|
|
|
|
{
|
|
|
|
if (0 != strncmp(key.data(), "attribute", strlen("attribute")))
|
|
|
|
continue;
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
const auto prefix = config_prefix + '.' + key + '.';
|
2015-03-20 15:21:29 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
const auto name = config.getString(prefix + "name");
|
2015-03-20 15:21:29 +00:00
|
|
|
const auto type_string = config.getString(prefix + "type");
|
|
|
|
const auto type = DataTypeFactory::instance().get(type_string);
|
|
|
|
const auto underlying_type = getAttributeUnderlyingType(type_string);
|
|
|
|
|
2015-05-26 12:24:31 +00:00
|
|
|
const auto expression = config.getString(prefix + "expression", "");
|
2015-09-07 17:22:54 +00:00
|
|
|
if (!expression.empty())
|
|
|
|
has_expressions = true;
|
2015-05-26 12:24:31 +00:00
|
|
|
|
2015-03-20 15:21:29 +00:00
|
|
|
Field null_value;
|
2015-11-13 01:44:41 +00:00
|
|
|
if (allow_null_values)
|
2015-03-23 13:28:08 +00:00
|
|
|
{
|
2015-11-13 01:44:41 +00:00
|
|
|
const auto null_value_string = config.getString(prefix + "null_value");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ReadBufferFromString null_value_buffer{null_value_string};
|
|
|
|
type->deserializeText(null_value, null_value_buffer);
|
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
throw Exception{
|
|
|
|
std::string{"Error parsing null_value: "} + e.what(),
|
|
|
|
ErrorCodes::BAD_ARGUMENTS
|
|
|
|
};
|
|
|
|
}
|
2015-03-23 13:28:08 +00:00
|
|
|
}
|
2015-03-20 15:21:29 +00:00
|
|
|
|
2015-01-26 15:27:51 +00:00
|
|
|
const auto hierarchical = config.getBool(prefix + "hierarchical", false);
|
|
|
|
const auto injective = config.getBool(prefix + "injective", false);
|
2015-03-20 15:21:29 +00:00
|
|
|
if (name.empty())
|
2015-01-26 15:27:51 +00:00
|
|
|
throw Exception{
|
|
|
|
"Properties 'name' and 'type' of an attribute cannot be empty",
|
|
|
|
ErrorCodes::BAD_ARGUMENTS
|
|
|
|
};
|
|
|
|
|
2015-11-12 14:28:23 +00:00
|
|
|
if (has_hierarchy && !hierarchy_allowed)
|
|
|
|
throw Exception{
|
|
|
|
"Hierarchy not allowed in '" + prefix,
|
|
|
|
ErrorCodes::BAD_ARGUMENTS
|
|
|
|
};
|
|
|
|
|
2015-01-26 15:27:51 +00:00
|
|
|
if (has_hierarchy && hierarchical)
|
|
|
|
throw Exception{
|
|
|
|
"Only one hierarchical attribute supported",
|
|
|
|
ErrorCodes::BAD_ARGUMENTS
|
|
|
|
};
|
|
|
|
|
|
|
|
has_hierarchy = has_hierarchy || hierarchical;
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
attributes.emplace_back(DictionaryAttribute{
|
2015-05-26 12:24:31 +00:00
|
|
|
name, underlying_type, type, expression, null_value, hierarchical, injective
|
2015-02-10 14:50:43 +00:00
|
|
|
});
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:28:23 +00:00
|
|
|
return attributes;
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|