1#include "ProtobufColumnMatcher.h"
2#if USE_PROTOBUF
3#include <Common/Exception.h>
4#include <google/protobuf/descriptor.h>
5#include <google/protobuf/descriptor.pb.h>
6#include <Poco/String.h>
7
8
9namespace DB
10{
11namespace ErrorCodes
12{
13 extern const int NO_COMMON_COLUMNS_WITH_PROTOBUF_SCHEMA;
14}
15
16
17namespace
18{
19 String columnNameToSearchableForm(const String & str)
20 {
21 return Poco::replace(Poco::toUpper(str), ".", "_");
22 }
23}
24
25namespace ProtobufColumnMatcher
26{
27 namespace details
28 {
29 ColumnNameMatcher::ColumnNameMatcher(const std::vector<String> & column_names) : column_usage(column_names.size())
30 {
31 column_usage.resize(column_names.size(), false);
32 for (size_t i = 0; i != column_names.size(); ++i)
33 column_name_to_index_map.emplace(columnNameToSearchableForm(column_names[i]), i);
34 }
35
36 size_t ColumnNameMatcher::findColumn(const String & field_name)
37 {
38 auto it = column_name_to_index_map.find(columnNameToSearchableForm(field_name));
39 if (it == column_name_to_index_map.end())
40 return -1;
41 size_t column_index = it->second;
42 if (column_usage[column_index])
43 return -1;
44 column_usage[column_index] = true;
45 return column_index;
46 }
47
48 void throwNoCommonColumns()
49 {
50 throw Exception("No common columns with provided protobuf schema", ErrorCodes::NO_COMMON_COLUMNS_WITH_PROTOBUF_SCHEMA);
51 }
52 }
53}
54
55}
56#endif
57