aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Dictionaries/YAMLRegExpTreeDictionarySource.cpp
blob: aa2ef163a851242034a6c6fccf442c8275ee7d7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "YAMLRegExpTreeDictionarySource.h"

#include <cstdlib>
#include <iterator>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <Poco/Logger.h>
#include "Core/ColumnWithTypeAndName.h"
#include "DataTypes/DataTypeArray.h"

#if USE_YAML_CPP

#    error #include <yaml-cpp/exceptions.h>
#    error #include <yaml-cpp/node/node.h>
#    error #include <yaml-cpp/node/parse.h>
#    error #include <yaml-cpp/yaml.h>

#endif

#include <base/types.h>

#include <Columns/ColumnArray.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnsNumber.h>
#include <Columns/IColumn.h>

#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/Serializations/ISerialization.h>

#include <Core/ColumnsWithTypeAndName.h>

#include <Dictionaries/DictionarySourceFactory.h>
#include <Dictionaries/DictionarySourceHelpers.h>
#include <Dictionaries/DictionaryStructure.h>

#include <Interpreters/Context.h>
#include <Processors/Sources/SourceFromSingleChunk.h>

#include <Poco/Util/AbstractConfiguration.h>

#include <Common/filesystemHelpers.h>
#include <Common/logger_useful.h>

namespace DB
{

inline const String kYAMLRegExpTreeDictionarySource = "YAMLRegExpTreeDictionarySource";
inline const String kYAMLRegExpTree = "yamlregexptree";

namespace ErrorCodes
{
    extern const int LOGICAL_ERROR;
    extern const int SUPPORT_IS_DISABLED;
    extern const int INCORRECT_DICTIONARY_DEFINITION;
    extern const int CANNOT_OPEN_FILE;
    extern const int CANNOT_PARSE_YAML;
    extern const int INVALID_CONFIG_PARAMETER;
    extern const int PATH_ACCESS_DENIED;
}

void registerDictionarySourceYAMLRegExpTree(DictionarySourceFactory & factory)
{
    auto create_table_source = [=]([[maybe_unused]] const DictionaryStructure & dict_struct,
                                   [[maybe_unused]] const Poco::Util::AbstractConfiguration & config,
                                   [[maybe_unused]] const String & config_prefix,
                                   Block & ,
                                   [[maybe_unused]] ContextPtr global_context,
                                   const String &,
                                   [[maybe_unused]] bool created_from_ddl) -> DictionarySourcePtr
    {
#if USE_YAML_CPP
        if (dict_struct.has_expressions)
        {
            throw Exception(
                ErrorCodes::LOGICAL_ERROR, "Dictionary source of type `{}` does not support attribute expressions", kYAMLRegExpTree);
        }

        if (!dict_struct.key.has_value() || dict_struct.key.value().size() != 1 || (*dict_struct.key)[0].type->getName() != "String")
        {
            throw Exception(ErrorCodes::INCORRECT_DICTIONARY_DEFINITION,
                            "dictionary source `{}` should have one primary key with string value "
                            "to represent regular expressions", kYAMLRegExpTree);
        }

        const auto & filepath = config.getString(config_prefix + "." + kYAMLRegExpTree + ".path");

        const auto & context = copyContextAndApplySettingsFromDictionaryConfig(global_context, config, config_prefix);

        return std::make_unique<YAMLRegExpTreeDictionarySource>(filepath, dict_struct, context, created_from_ddl);
#else
        throw Exception(
            ErrorCodes::SUPPORT_IS_DISABLED,
            "Dictionary source of type `{}` is disabled because ClickHouse was built without yaml_cpp support",
            kYAMLRegExpTree);
#endif
    };

    factory.registerSource(kYAMLRegExpTree, create_table_source);
}

}

#if USE_YAML_CPP

namespace DB

{

/**
*   Configuration allowed fields
*
*   Example:
*   ```
*   - regexp: "MSIE (\\d+)"
*     attr1: "Vecna"
*     attr2: 22.8
*     (arbitrary name not in attribution list): # nested match node for subpattern
*           - regexp: "Windows"
*             attr2: 22.9
*           - regexp: "Linux"
*           ...
*   ```
*/

static const std::string kMatch = "match";
static const std::string kRegExp = "regexp";

/**
*   The data can be loaded from table (using any available dictionary source) with the following structure:
*   ```
*   id UInt64,
*   parent_id UInt64,
*   regexp String,
*   keys array<String>,
*   values array<String>,
*/

const std::string kId = "id";
const std::string kParentId = "parent_id";
const std::string kKeys = "keys";
const std::string kValues = "values";


struct MatchNode
{
    UInt64 id;
    UInt64 parent_id;
    String reg_exp;
    std::vector<Field> keys;
    std::vector<Field> values;
};

struct ResultColumns
{
    MutableColumnPtr ids = ColumnUInt64::create();
    MutableColumnPtr parent_ids = ColumnUInt64::create();
    MutableColumnPtr reg_exps = ColumnString::create();
    MutableColumnPtr keys = ColumnArray::create(ColumnString::create());
    MutableColumnPtr values = ColumnArray::create(ColumnString::create());
    ResultColumns() = default;
};

using StringToNode = std::unordered_map<String, YAML::Node>;

YAML::Node loadYAML(const String & filepath)
{
    try
    {
        return YAML::LoadFile(filepath);
    }
    catch (const YAML::BadFile & error)
    {
        throw Exception(ErrorCodes::CANNOT_OPEN_FILE, "Failed to open YAML-configuration file {}. Error: {}", filepath, error.what());
    }
    catch (const YAML::ParserException & error)
    {
        throw Exception(ErrorCodes::CANNOT_PARSE_YAML, "Failed to parse YAML-configuration file {}. Error: {}", filepath, error.what());
    }
}

static StringToNode parseYAMLMap(const YAML::Node & node)
{
    StringToNode result;

    for (const auto & pair : node)
    {
        const String & key = pair.first.as<String>();
        result[key] = pair.second;
    }

    return result;
}

void insertValues(const MatchNode & node, ResultColumns & result_columns)
{
    result_columns.ids->insert(node.id);
    result_columns.parent_ids->insert(node.parent_id);
    result_columns.reg_exps->insert(node.reg_exp);
    result_columns.keys->insert(Array(node.keys.begin(), node.keys.end()));
    result_columns.values->insert(Array(node.values.begin(), node.values.end()));
}

void parseMatchList(UInt64 parent_id, UInt64 & id, const YAML::Node & node, ResultColumns & names_to_attributes, const String & key_name, const DictionaryStructure & structure);

/// MatchNode has to be a map with structure:
/// 1. regex, indicating a regular expression
/// 2. attribute_name, indicating the attributes to set
/// 3. match (optional), indicating the nested match logic under this node
void parseMatchNode(UInt64 parent_id, UInt64 & id, const YAML::Node & node, ResultColumns & result, const String & key_name, const DictionaryStructure & structure)
{
    if (!node.IsMap())
    {
        throw Exception(ErrorCodes::INVALID_CONFIG_PARAMETER, "`{}` node must be map type", kMatch);
    }

    auto match = parseYAMLMap(node);

    MatchNode attributes_to_insert;

    attributes_to_insert.id = ++id;
    attributes_to_insert.parent_id = parent_id;

    if (!match.contains(key_name))
    {
        throw Exception(ErrorCodes::INVALID_CONFIG_PARAMETER, "Yaml match rule must contain key {}", key_name);
    }
    for (const auto & [key, node_] : match)
    {
        if (key == key_name)
        {
            if (!node_.IsScalar())
                throw Exception(ErrorCodes::INVALID_CONFIG_PARAMETER, "`{}` should be a String", key_name);

            attributes_to_insert.reg_exp = node_.as<String>();
        }
        else if (structure.hasAttribute(key))
        {
            attributes_to_insert.keys.push_back(key);
            attributes_to_insert.values.push_back(node_.as<String>());
        }
        else if (node_.IsSequence())
        {
            parseMatchList(attributes_to_insert.id, id, node_, result, key_name, structure);
        }
        /// unknown attributes.
    }
    insertValues(attributes_to_insert, result);
}

void parseMatchList(UInt64 parent_id, UInt64 & id, const YAML::Node & node, ResultColumns & names_to_attributes, const String & key_name, const DictionaryStructure & structure)
{
    if (!node.IsSequence())
    {
        throw Exception(ErrorCodes::INVALID_CONFIG_PARAMETER, "Configuration {} must be a yaml list of match rules", node.as<String>());
    }

    for (const auto & child_node : node)
    {
        parseMatchNode(parent_id, id, child_node, names_to_attributes, key_name, structure);
    }
}

Block parseYAMLAsRegExpTree(const YAML::Node & node, const String & key_name, const DictionaryStructure & structure)
{
    ResultColumns result_cols;
    UInt64 id = 0;
    parseMatchList(0, id, node, result_cols, key_name, structure);

    ColumnsWithTypeAndName columns;

    columns.push_back(ColumnWithTypeAndName(std::move(result_cols.ids), DataTypePtr(new DataTypeUInt64()), kId));
    columns.push_back(ColumnWithTypeAndName(std::move(result_cols.parent_ids), DataTypePtr(new DataTypeUInt64()), kParentId));
    columns.push_back(ColumnWithTypeAndName(std::move(result_cols.reg_exps), DataTypePtr(new DataTypeString()), kRegExp));
    columns.push_back(ColumnWithTypeAndName(std::move(result_cols.keys), DataTypePtr(new DataTypeArray(DataTypePtr(new DataTypeString()))), kKeys));
    columns.push_back(ColumnWithTypeAndName(std::move(result_cols.values), DataTypePtr(new DataTypeArray(DataTypePtr(new DataTypeString()))), kValues));

    return Block(std::move(columns));
}

YAMLRegExpTreeDictionarySource::YAMLRegExpTreeDictionarySource(
    const String & filepath_, const DictionaryStructure & dict_struct, ContextPtr context_, bool created_from_ddl)
    : filepath(filepath_), structure(dict_struct), context(context_), logger(&Poco::Logger::get(kYAMLRegExpTreeDictionarySource))
{
    key_name = (*structure.key)[0].name;

    const auto user_files_path = context->getUserFilesPath();

    if (created_from_ddl && !fileOrSymlinkPathStartsWith(filepath_, user_files_path))
    {
        throw Exception(ErrorCodes::PATH_ACCESS_DENIED, "File {} is not inside {}", filepath_, user_files_path);
    }
}

YAMLRegExpTreeDictionarySource::YAMLRegExpTreeDictionarySource(const YAMLRegExpTreeDictionarySource & other)
    : filepath(other.filepath)
    , key_name(other.key_name)
    , structure(other.structure)
    , context(Context::createCopy(other.context))
    , logger(other.logger)
    , last_modification(other.last_modification)
{
}

QueryPipeline YAMLRegExpTreeDictionarySource::loadAll()
{
    LOG_INFO(logger, "Loading regexp tree from yaml '{}'", filepath);
    last_modification = getLastModification();

    const auto node = loadYAML(filepath);

    return QueryPipeline(std::make_shared<SourceFromSingleChunk>(parseYAMLAsRegExpTree(node, key_name, structure)));
}

bool YAMLRegExpTreeDictionarySource::isModified() const
{
    /**
    *   We can't count on that the mtime increases or that it has
    *   a particular relation to system time, so just check for strict
    *   equality.
    */
    return getLastModification() != last_modification;
}

String YAMLRegExpTreeDictionarySource::toString() const
{
    return fmt::format("{} with path: {}", kYAMLRegExpTree, filepath);
}

Poco::Timestamp YAMLRegExpTreeDictionarySource::getLastModification() const
{
    return FS::getModificationTimestamp(filepath);
}

}

#endif