TableFunctionMerge.cpp 2.7 KB
Newer Older
1
#include <Common/OptimizedRegularExpression.h>
2

3 4 5 6 7 8 9 10 11 12
#include <Storages/StorageMerge.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTFunction.h>
#include <TableFunctions/ITableFunction.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Interpreters/Context.h>
#include <Databases/IDatabase.h>
#include <TableFunctions/TableFunctionMerge.h>
13 14 15 16 17 18 19 20


namespace DB
{


namespace ErrorCodes
{
21 22
    extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
    extern const int UNKNOWN_TABLE;
23 24 25 26 27
}


static NamesAndTypesList chooseColumns(const String & source_database, const String & table_name_regexp_, const Context & context)
{
28
    OptimizedRegularExpression table_name_regexp(table_name_regexp_);
29

30
    StoragePtr any_table;
31

32 33 34
    {
        auto database = context.getDatabase(source_database);
        auto iterator = database->getIterator();
35

36 37 38 39 40 41 42
        while (iterator->isValid())
        {
            if (table_name_regexp.match(iterator->name()))
            {
                any_table = iterator->table();
                break;
            }
43

44 45 46
            iterator->next();
        }
    }
47

48 49 50
    if (!any_table)
        throw Exception("Error while executing table function merge. In database " + source_database + " no one matches regular expression: "
            + table_name_regexp_, ErrorCodes::UNKNOWN_TABLE);
51

52
    return any_table->getColumnsList();
53 54 55
}


56
StoragePtr TableFunctionMerge::execute(const ASTPtr & ast_function, const Context & context) const
57
{
58
    ASTs & args_func = typeid_cast<ASTFunction &>(*ast_function).children;
59

60 61 62 63
    if (args_func.size() != 1)
        throw Exception("Storage Merge requires exactly 2 parameters"
            " - name of source database and regexp for table names.",
            ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
64

65
    ASTs & args = typeid_cast<ASTExpressionList &>(*args_func.at(0)).children;
66

67 68 69 70
    if (args.size() != 2)
        throw Exception("Storage Merge requires exactly 2 parameters"
            " - name of source database and regexp for table names.",
            ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
71

72 73
    args[0] = evaluateConstantExpressionOrIdentidierAsLiteral(args[0], context);
    args[1] = evaluateConstantExpressionAsLiteral(args[1], context);
74

A
Alexey Milovidov 已提交
75 76
    String source_database = static_cast<const ASTLiteral &>(*args[0]).value.safeGet<String>();
    String table_name_regexp = static_cast<const ASTLiteral &>(*args[1]).value.safeGet<String>();
77

78 79 80 81 82 83
    return StorageMerge::create(
        getName(),
        std::make_shared<NamesAndTypesList>(chooseColumns(source_database, table_name_regexp, context)),
        source_database,
        table_name_regexp,
        context);
84 85 86
}

}