2020-09-14 11:36:14 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
2022-06-23 20:04:06 +00:00
|
|
|
#include <Storages/checkAndGetLiteralArgument.h>
|
2020-09-14 11:36:14 +00:00
|
|
|
#include <Storages/StorageNull.h>
|
2022-08-16 09:41:32 +00:00
|
|
|
#include <Interpreters/parseColumnsListForTableFunction.h>
|
2020-09-14 11:36:14 +00:00
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
2020-09-18 11:06:30 +00:00
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
2023-11-06 03:33:23 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <TableFunctions/ITableFunction.h>
|
2020-09-14 11:36:14 +00:00
|
|
|
#include "registerTableFunctions.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
2023-11-06 03:33:23 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/* null(structure) - creates a temporary null storage
|
|
|
|
*
|
|
|
|
* Used for testing purposes, for convenience writing tests and demos.
|
|
|
|
*/
|
|
|
|
class TableFunctionNull : public ITableFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "null";
|
|
|
|
std::string getName() const override { return name; }
|
|
|
|
|
|
|
|
bool needStructureHint() const override { return structure == "auto"; }
|
|
|
|
|
|
|
|
void setStructureHint(const ColumnsDescription & structure_hint_) override { structure_hint = structure_hint_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const String & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override;
|
|
|
|
const char * getStorageTypeName() const override { return "Null"; }
|
|
|
|
|
|
|
|
void parseArguments(const ASTPtr & ast_function, ContextPtr context) override;
|
|
|
|
ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override;
|
|
|
|
|
|
|
|
String structure = "auto";
|
|
|
|
ColumnsDescription structure_hint;
|
|
|
|
|
|
|
|
const ColumnsDescription default_structure{NamesAndTypesList{{"dummy", std::make_shared<DataTypeUInt8>()}}};
|
|
|
|
};
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void TableFunctionNull::parseArguments(const ASTPtr & ast_function, ContextPtr context)
|
2020-09-14 11:36:14 +00:00
|
|
|
{
|
2020-10-14 12:19:29 +00:00
|
|
|
const auto * function = ast_function->as<ASTFunction>();
|
|
|
|
if (!function || !function->arguments)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function '{}' requires 'structure'", getName());
|
2020-09-14 11:36:14 +00:00
|
|
|
|
2020-10-14 12:19:29 +00:00
|
|
|
const auto & arguments = function->arguments->children;
|
2022-04-17 15:07:56 +00:00
|
|
|
if (!arguments.empty() && arguments.size() != 1)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
|
|
|
|
"Table function '{}' requires 'structure' argument or empty argument", getName());
|
2020-09-14 11:36:14 +00:00
|
|
|
|
2022-04-17 15:07:56 +00:00
|
|
|
if (!arguments.empty())
|
2022-06-23 20:04:06 +00:00
|
|
|
structure = checkAndGetLiteralArgument<String>(evaluateConstantExpressionOrIdentifierAsLiteral(arguments[0], context), "structure");
|
2020-10-14 12:19:29 +00:00
|
|
|
}
|
2020-09-14 11:36:14 +00:00
|
|
|
|
2023-07-06 08:56:07 +00:00
|
|
|
ColumnsDescription TableFunctionNull::getActualTableStructure(ContextPtr context, bool /*is_insert_query*/) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
2023-02-28 12:30:07 +00:00
|
|
|
if (structure != "auto")
|
|
|
|
return parseColumnsListFromString(structure, context);
|
|
|
|
return default_structure;
|
2020-10-14 12:19:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 08:56:07 +00:00
|
|
|
StoragePtr TableFunctionNull::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/, bool /*is_insert_query*/) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
2022-04-17 15:07:56 +00:00
|
|
|
ColumnsDescription columns;
|
|
|
|
if (structure != "auto")
|
2023-02-28 12:30:07 +00:00
|
|
|
columns = parseColumnsListFromString(structure, context);
|
2022-04-17 15:07:56 +00:00
|
|
|
else if (!structure_hint.empty())
|
|
|
|
columns = structure_hint;
|
2023-02-28 11:44:30 +00:00
|
|
|
else
|
|
|
|
columns = default_structure;
|
|
|
|
|
2022-04-19 20:47:29 +00:00
|
|
|
auto res = std::make_shared<StorageNull>(StorageID(getDatabaseName(), table_name), columns, ConstraintsDescription(), String{});
|
2020-10-14 12:19:29 +00:00
|
|
|
res->startup();
|
|
|
|
return res;
|
2020-09-14 11:36:14 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 03:33:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 11:36:14 +00:00
|
|
|
void registerTableFunctionNull(TableFunctionFactory & factory)
|
|
|
|
{
|
2022-11-03 11:31:07 +00:00
|
|
|
factory.registerFunction<TableFunctionNull>({.documentation = {}, .allow_readonly = true});
|
2020-09-14 11:36:14 +00:00
|
|
|
}
|
2023-11-06 03:33:23 +00:00
|
|
|
|
2020-09-14 11:36:14 +00:00
|
|
|
}
|