ClickHouse/dbms/TableFunctions/TableFunctionInput.cpp

57 lines
1.8 KiB
C++
Raw Normal View History

2019-05-28 18:27:00 +00:00
#include <TableFunctions/ITableFunction.h>
#include <TableFunctions/TableFunctionInput.h>
#include <TableFunctions/TableFunctionFactory.h>
2019-09-05 13:17:01 +00:00
#include <TableFunctions/parseColumnsListForTableFunction.h>
2019-05-28 18:27:00 +00:00
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTLiteral.h>
#include <Common/Exception.h>
#include <Common/typeid_cast.h>
#include <Storages/StorageInput.h>
#include <DataTypes/DataTypeFactory.h>
#include <Interpreters/Context.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Access/AccessFlags.h>
2019-05-28 18:27:00 +00:00
#include <boost/algorithm/string.hpp>
2019-12-15 06:34:43 +00:00
#include "registerTableFunctions.h"
2019-05-28 18:27:00 +00:00
namespace DB
{
namespace ErrorCodes
{
2020-02-25 18:02:41 +00:00
extern const int LOGICAL_ERROR;
2019-05-28 18:27:00 +00:00
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
StoragePtr TableFunctionInput::executeImpl(const ASTPtr & ast_function, const Context & context, const std::string & table_name) const
2019-05-28 18:27:00 +00:00
{
const auto * function = ast_function->as<ASTFunction>();
if (!function->arguments)
throw Exception("Table function '" + getName() + "' must have arguments", ErrorCodes::LOGICAL_ERROR);
auto args = function->arguments->children;
if (args.size() != 1)
throw Exception("Table function '" + getName() + "' requires exactly 1 argument: structure",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
2020-01-24 16:20:36 +00:00
context.checkAccess(AccessType::input);
2019-05-28 18:27:00 +00:00
String structure = evaluateConstantExpressionOrIdentifierAsLiteral(args[0], context)->as<ASTLiteral &>().value.safeGet<String>();
2019-09-05 13:17:01 +00:00
auto columns = parseColumnsListFromString(structure, context);
2020-03-10 19:36:17 +00:00
StoragePtr storage = StorageInput::create(StorageID(getDatabaseName(), table_name), columns);
2019-05-28 18:27:00 +00:00
storage->startup();
return storage;
}
void registerTableFunctionInput(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionInput>();
}
}