1#include <TableFunctions/ITableFunction.h>
2#include <TableFunctions/TableFunctionInput.h>
3#include <TableFunctions/TableFunctionFactory.h>
4#include <TableFunctions/parseColumnsListForTableFunction.h>
5#include <Parsers/ASTFunction.h>
6#include <Parsers/ASTLiteral.h>
7#include <Common/Exception.h>
8#include <Common/typeid_cast.h>
9#include <Storages/StorageInput.h>
10#include <DataTypes/DataTypeFactory.h>
11#include <Interpreters/Context.h>
12#include <Interpreters/evaluateConstantExpression.h>
13#include <boost/algorithm/string.hpp>
14#include "registerTableFunctions.h"
15
16
17namespace DB
18{
19
20namespace ErrorCodes
21{
22 extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
23}
24
25StoragePtr TableFunctionInput::executeImpl(const ASTPtr & ast_function, const Context & context, const std::string & table_name) const
26{
27 const auto * function = ast_function->as<ASTFunction>();
28
29 if (!function->arguments)
30 throw Exception("Table function '" + getName() + "' must have arguments", ErrorCodes::LOGICAL_ERROR);
31
32 auto args = function->arguments->children;
33
34 if (args.size() != 1)
35 throw Exception("Table function '" + getName() + "' requires exactly 1 argument: structure",
36 ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
37
38 String structure = evaluateConstantExpressionOrIdentifierAsLiteral(args[0], context)->as<ASTLiteral &>().value.safeGet<String>();
39 auto columns = parseColumnsListFromString(structure, context);
40 StoragePtr storage = StorageInput::create(table_name, columns);
41
42 storage->startup();
43
44 return storage;
45}
46
47void registerTableFunctionInput(TableFunctionFactory & factory)
48{
49 factory.registerFunction<TableFunctionInput>();
50}
51
52}
53