2019-05-28 18:27:00 +00:00
|
|
|
#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 <Storages/StorageInput.h>
|
2022-06-23 20:04:06 +00:00
|
|
|
#include <Storages/checkAndGetLiteralArgument.h>
|
2019-05-28 18:27:00 +00:00
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
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;
|
2022-03-30 12:44:53 +00:00
|
|
|
extern const int CANNOT_EXTRACT_TABLE_STRUCTURE;
|
2019-05-28 18:27:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void TableFunctionInput::parseArguments(const ASTPtr & ast_function, ContextPtr context)
|
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;
|
|
|
|
|
2022-03-30 12:44:53 +00:00
|
|
|
if (args.empty())
|
|
|
|
{
|
|
|
|
structure = "auto";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:27:00 +00:00
|
|
|
if (args.size() != 1)
|
|
|
|
throw Exception("Table function '" + getName() + "' requires exactly 1 argument: structure",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2022-06-23 20:04:06 +00:00
|
|
|
structure = checkAndGetLiteralArgument<String>(evaluateConstantExpressionOrIdentifierAsLiteral(args[0], context), "structure");
|
2020-10-14 12:19:29 +00:00
|
|
|
}
|
2020-08-26 20:56:30 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
ColumnsDescription TableFunctionInput::getActualTableStructure(ContextPtr context) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
2022-03-30 12:44:53 +00:00
|
|
|
if (structure == "auto")
|
|
|
|
{
|
|
|
|
if (structure_hint.empty())
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::CANNOT_EXTRACT_TABLE_STRUCTURE,
|
|
|
|
"Table function '{}' was used without structure argument but structure could not be determined automatically. Please, "
|
|
|
|
"provide structure manually",
|
|
|
|
getName());
|
|
|
|
return structure_hint;
|
|
|
|
}
|
2020-10-14 12:19:29 +00:00
|
|
|
return parseColumnsListFromString(structure, context);
|
|
|
|
}
|
2020-10-14 10:59:29 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
StoragePtr TableFunctionInput::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
2022-04-19 20:47:29 +00:00
|
|
|
auto storage = std::make_shared<StorageInput>(StorageID(getDatabaseName(), table_name), getActualTableStructure(context));
|
2020-10-14 12:19:29 +00:00
|
|
|
storage->startup();
|
2019-05-28 18:27:00 +00:00
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerTableFunctionInput(TableFunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<TableFunctionInput>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|