| 1 | #include <Storages/ColumnDefault.h> | 
|---|
| 2 | #include <Parsers/queryToString.h> | 
|---|
| 3 |  | 
|---|
| 4 | namespace | 
|---|
| 5 | { | 
|---|
| 6 |  | 
|---|
| 7 | struct AliasNames | 
|---|
| 8 | { | 
|---|
| 9 | static constexpr const char * DEFAULT = "DEFAULT"; | 
|---|
| 10 | static constexpr const char * MATERIALIZED = "MATERIALIZED"; | 
|---|
| 11 | static constexpr const char * ALIAS = "ALIAS"; | 
|---|
| 12 | }; | 
|---|
| 13 |  | 
|---|
| 14 | } | 
|---|
| 15 |  | 
|---|
| 16 | namespace DB | 
|---|
| 17 | { | 
|---|
| 18 |  | 
|---|
| 19 | namespace ErrorCodes | 
|---|
| 20 | { | 
|---|
| 21 | extern const int LOGICAL_ERROR; | 
|---|
| 22 | } | 
|---|
| 23 |  | 
|---|
| 24 |  | 
|---|
| 25 | ColumnDefaultKind columnDefaultKindFromString(const std::string & str) | 
|---|
| 26 | { | 
|---|
| 27 | static const std::unordered_map<std::string, ColumnDefaultKind> map{ | 
|---|
| 28 | { AliasNames::DEFAULT, ColumnDefaultKind::Default }, | 
|---|
| 29 | { AliasNames::MATERIALIZED, ColumnDefaultKind::Materialized }, | 
|---|
| 30 | { AliasNames::ALIAS, ColumnDefaultKind::Alias } | 
|---|
| 31 | }; | 
|---|
| 32 |  | 
|---|
| 33 | const auto it = map.find(str); | 
|---|
| 34 | if (it != std::end(map)) | 
|---|
| 35 | return it->second; | 
|---|
| 36 |  | 
|---|
| 37 | throw Exception{ "Unknown column default specifier: "+ str, ErrorCodes::LOGICAL_ERROR}; | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 |  | 
|---|
| 41 | std::string toString(const ColumnDefaultKind kind) | 
|---|
| 42 | { | 
|---|
| 43 | static const std::unordered_map<ColumnDefaultKind, std::string> map{ | 
|---|
| 44 | { ColumnDefaultKind::Default, AliasNames::DEFAULT }, | 
|---|
| 45 | { ColumnDefaultKind::Materialized, AliasNames::MATERIALIZED }, | 
|---|
| 46 | { ColumnDefaultKind::Alias, AliasNames::ALIAS } | 
|---|
| 47 | }; | 
|---|
| 48 |  | 
|---|
| 49 | const auto it = map.find(kind); | 
|---|
| 50 | if (it != std::end(map)) | 
|---|
| 51 | return it->second; | 
|---|
| 52 |  | 
|---|
| 53 | throw Exception{ "Invalid ColumnDefaultKind", ErrorCodes::LOGICAL_ERROR}; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 |  | 
|---|
| 57 | bool operator==(const ColumnDefault & lhs, const ColumnDefault & rhs) | 
|---|
| 58 | { | 
|---|
| 59 | auto expression_str = [](const ASTPtr & expr) { return expr ? queryToString(expr) : String(); }; | 
|---|
| 60 | return lhs.kind == rhs.kind && expression_str(lhs.expression) == expression_str(rhs.expression); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|