Implement type inference for INSERT INTO function null()

This commit is contained in:
fenglv 2022-04-17 15:07:56 +00:00
parent 71dc0f0616
commit 293d0a5d46
4 changed files with 21 additions and 6 deletions

View File

@ -21,13 +21,16 @@ void TableFunctionNull::parseArguments(const ASTPtr & ast_function, ContextPtr c
{
const auto * function = ast_function->as<ASTFunction>();
if (!function || !function->arguments)
throw Exception("Table function '" + getName() + "' requires 'structure'.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
throw Exception("Table function '" + getName() + "' requires 'structure'", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
const auto & arguments = function->arguments->children;
if (arguments.size() != 1)
throw Exception("Table function '" + getName() + "' requires 'structure'.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
if (!arguments.empty() && arguments.size() != 1)
throw Exception(
"Table function '" + getName() + "' requires 'structure' argument or empty argument",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
structure = evaluateConstantExpressionOrIdentifierAsLiteral(arguments[0], context)->as<ASTLiteral>()->value.safeGet<String>();
if (!arguments.empty())
structure = evaluateConstantExpressionOrIdentifierAsLiteral(arguments[0], context)->as<ASTLiteral>()->value.safeGet<String>();
}
ColumnsDescription TableFunctionNull::getActualTableStructure(ContextPtr context) const
@ -37,7 +40,11 @@ ColumnsDescription TableFunctionNull::getActualTableStructure(ContextPtr context
StoragePtr TableFunctionNull::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/) const
{
auto columns = getActualTableStructure(context);
ColumnsDescription columns;
if (structure != "auto")
columns = getActualTableStructure(context);
else if (!structure_hint.empty())
columns = structure_hint;
auto res = StorageNull::create(StorageID(getDatabaseName(), table_name), columns, ConstraintsDescription(), String{});
res->startup();
return res;

View File

@ -16,6 +16,10 @@ 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) const override;
const char * getStorageTypeName() const override { return "Null"; }
@ -23,7 +27,8 @@ private:
void parseArguments(const ASTPtr & ast_function, ContextPtr context) override;
ColumnsDescription getActualTableStructure(ContextPtr context) const override;
String structure;
String structure = "auto";
ColumnsDescription structure_hint;
};
}

View File

@ -0,0 +1,3 @@
INSERT INTO function null() SELECT 1;
INSERT INTO function null() SELECT number FROM numbers(10);
INSERT INTO function null() SELECT number, toString(number) FROM numbers(10);