Merge pull request #36353 from ucasfl/type-infer-null

Implement type inference for INSERT INTO function null()
This commit is contained in:
Alexey Milovidov 2022-04-18 07:06:54 +03:00 committed by GitHub
commit f5e270b2f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 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,6 @@
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);
INSERT INTO function null('auto') SELECT 1;
INSERT INTO function null('auto') SELECT number FROM numbers(10);
INSERT INTO function null('auto') SELECT number, toString(number) FROM numbers(10);