ClickHouse/src/Storages/ColumnDefault.cpp

64 lines
1.6 KiB
C++
Raw Normal View History

#include <Storages/ColumnDefault.h>
#include <Parsers/queryToString.h>
2016-12-12 07:24:56 +00:00
namespace
{
struct AliasNames
2016-12-12 07:24:56 +00:00
{
static constexpr const char * DEFAULT = "DEFAULT";
static constexpr const char * MATERIALIZED = "MATERIALIZED";
static constexpr const char * ALIAS = "ALIAS";
};
}
2016-12-12 07:24:56 +00:00
namespace DB
{
2016-12-12 07:24:56 +00:00
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
2016-12-12 07:24:56 +00:00
ColumnDefaultKind columnDefaultKindFromString(const std::string & str)
2016-12-12 07:24:56 +00:00
{
static const std::unordered_map<std::string, ColumnDefaultKind> map{
{ AliasNames::DEFAULT, ColumnDefaultKind::Default },
{ AliasNames::MATERIALIZED, ColumnDefaultKind::Materialized },
{ AliasNames::ALIAS, ColumnDefaultKind::Alias }
};
const auto it = map.find(str);
2019-05-31 08:55:01 +00:00
if (it != std::end(map))
return it->second;
throw Exception{"Unknown column default specifier: " + str, ErrorCodes::LOGICAL_ERROR};
2016-12-12 07:24:56 +00:00
}
std::string toString(const ColumnDefaultKind kind)
2016-12-12 07:24:56 +00:00
{
static const std::unordered_map<ColumnDefaultKind, std::string> map{
{ ColumnDefaultKind::Default, AliasNames::DEFAULT },
{ ColumnDefaultKind::Materialized, AliasNames::MATERIALIZED },
{ ColumnDefaultKind::Alias, AliasNames::ALIAS }
};
const auto it = map.find(kind);
2019-05-31 08:55:01 +00:00
if (it != std::end(map))
return it->second;
throw Exception{"Invalid ColumnDefaultKind", ErrorCodes::LOGICAL_ERROR};
2016-12-12 07:24:56 +00:00
}
bool operator==(const ColumnDefault & lhs, const ColumnDefault & rhs)
{
auto expression_str = [](const ASTPtr & expr) { return expr ? queryToString(expr) : String(); };
return lhs.kind == rhs.kind && expression_str(lhs.expression) == expression_str(rhs.expression);
2016-12-12 07:24:56 +00:00
}
}