|
1 | 1 | #include "type_parser.h"
|
2 | 2 | #include "../base/string_utils.h"
|
3 | 3 |
|
4 |
| -#include <iostream> |
| 4 | +#include <unordered_map> |
5 | 5 |
|
6 | 6 | namespace clickhouse {
|
7 | 7 |
|
| 8 | +static std::unordered_map<std::string, Type::Code> g_type_code = { |
| 9 | + { "Int8", Type::Int8 }, |
| 10 | + { "Int16", Type::Int16 }, |
| 11 | + { "Int32", Type::Int32 }, |
| 12 | + { "Int64", Type::Int64 }, |
| 13 | + { "UInt8", Type::UInt8 }, |
| 14 | + { "UInt16", Type::UInt16 }, |
| 15 | + { "UInt32", Type::UInt32 }, |
| 16 | + { "UInt64", Type::UInt64 }, |
| 17 | + { "Float32", Type::Float32 }, |
| 18 | + { "Float64", Type::Float64 }, |
| 19 | + { "String", Type::String }, |
| 20 | + { "FixedString", Type::FixedString }, |
| 21 | + { "DateTime", Type::DateTime }, |
| 22 | + { "Date", Type::Date }, |
| 23 | + { "Array", Type::Array }, |
| 24 | + { "Nullable", Type::Nullable }, |
| 25 | + { "Tuple", Type::Tuple }, |
| 26 | + { "Enum8", Type::Enum8 }, |
| 27 | + { "Enum16", Type::Enum16 }, |
| 28 | + { "UUID", Type::UUID }, |
| 29 | +}; |
| 30 | + |
| 31 | +static Type::Code GetTypeCode(const StringView& name) { |
| 32 | + std::string n = name.to_string(); |
| 33 | + auto it = g_type_code.find(n); |
| 34 | + if (it != g_type_code.end()) { |
| 35 | + return it->second; |
| 36 | + } |
| 37 | + return Type::Void; |
| 38 | +} |
| 39 | + |
8 | 40 | static TypeAst::Meta GetTypeMeta(const StringView& name) {
|
9 | 41 | if (name == "Array") {
|
10 | 42 | return TypeAst::Array;
|
@@ -49,11 +81,12 @@ bool TypeParser::Parse(TypeAst* type) {
|
49 | 81 | switch (token.type) {
|
50 | 82 | case Token::Name:
|
51 | 83 | type_->meta = GetTypeMeta(token.value);
|
52 |
| - type_->name = token.value; |
| 84 | + type_->name = token.value.to_string(); |
| 85 | + type_->code = GetTypeCode(token.value.to_string()); |
53 | 86 | break;
|
54 | 87 | case Token::Number:
|
55 | 88 | type_->meta = TypeAst::Number;
|
56 |
| - type_->value = FromString<int>(token.value); |
| 89 | + type_->value = std::stol(token.value.to_string()); |
57 | 90 | break;
|
58 | 91 | case Token::LPar:
|
59 | 92 | type_->elements.emplace_back(TypeAst());
|
@@ -130,4 +163,24 @@ TypeParser::Token TypeParser::NextToken() {
|
130 | 163 | return Token{Token::EOS, StringView()};
|
131 | 164 | }
|
132 | 165 |
|
| 166 | + |
| 167 | +const TypeAst* ParseTypeName(const std::string& type_name) { |
| 168 | + // Cache for type_name. |
| 169 | + // Usually we won't have too many type names in the cache, so do not try to |
| 170 | + // limit cache size. |
| 171 | + static std::unordered_map<std::string, TypeAst> ast_cache; |
| 172 | + |
| 173 | + auto it = ast_cache.find(type_name); |
| 174 | + if (it != ast_cache.end()) { |
| 175 | + return &it->second; |
| 176 | + } |
| 177 | + |
| 178 | + auto& ast = ast_cache[type_name]; |
| 179 | + if (TypeParser(type_name).Parse(&ast)) { |
| 180 | + return * |
| 181 | + } |
| 182 | + ast_cache.erase(type_name); |
| 183 | + return nullptr; |
| 184 | +} |
| 185 | + |
133 | 186 | }
|
0 commit comments