Fixed issues executeTableFunctions in Context.cpp to fall back on TableFunction if not parameterized view & updated test - 40907 Parameterized views as table functions

This commit is contained in:
Smita Kulkarni 2022-09-28 13:23:11 +02:00
parent e6672832b9
commit f78f846503
2 changed files with 35 additions and 33 deletions

View File

@ -1133,19 +1133,22 @@ StoragePtr Context::executeTableFunction(const ASTPtr & table_expression)
StoragePtr res = DatabaseCatalog::instance().getTable({getCurrentDatabase(), function->name}, getQueryContext()); StoragePtr res = DatabaseCatalog::instance().getTable({getCurrentDatabase(), function->name}, getQueryContext());
if (res.get()->isView() && res->as<StorageView>()->isParameterizedView()) if (res.get()->isView() && res->as<StorageView>()->isParameterizedView())
return res; return res;
else
{
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Not a parameterized view `{}`", function->name);
} }
}
else
{
auto hash = table_expression->getTreeHash(); auto hash = table_expression->getTreeHash();
String key = toString(hash.first) + '_' + toString(hash.second); String key = toString(hash.first) + '_' + toString(hash.second);
StoragePtr & res = table_function_results[key]; StoragePtr & res = table_function_results[key];
if (!res) if (!res)
{ {
TableFunctionPtr table_function_ptr = TableFunctionFactory::instance().get(table_expression, shared_from_this()); TableFunctionPtr table_function_ptr;
try
{
table_function_ptr = TableFunctionFactory::instance().get(table_expression, shared_from_this());
}
catch (Exception & e)
{
e.addMessage(" or incorrect parameterized view");
throw;
}
if (getSettingsRef().use_structure_from_insertion_table_in_table_functions && table_function_ptr->needStructureHint()) if (getSettingsRef().use_structure_from_insertion_table_in_table_functions && table_function_ptr->needStructureHint())
{ {
const auto & insertion_table = getInsertionTable(); const auto & insertion_table = getInsertionTable();
@ -1172,7 +1175,6 @@ StoragePtr Context::executeTableFunction(const ASTPtr & table_expression)
} }
return res; return res;
} }
}
throw Exception(ErrorCodes::LOGICAL_ERROR, "Unable to fetch function from query"); throw Exception(ErrorCodes::LOGICAL_ERROR, "Unable to fetch function from query");
} }

View File

@ -13,7 +13,7 @@ SELECT Price FROM v1(price=20);
SELECT Price FROM v123(price=20); -- { serverError UNKNOWN_FUNCTION } SELECT Price FROM v123(price=20); -- { serverError UNKNOWN_FUNCTION }
CREATE VIEW v10 AS SELECT * FROM Catalog WHERE Price=10; CREATE VIEW v10 AS SELECT * FROM Catalog WHERE Price=10;
SELECT Price FROM v10(price=10); -- { serverError BAD_ARGUMENTS } SELECT Price FROM v10(price=10); -- { serverError UNKNOWN_FUNCTION }
CREATE VIEW v2 AS SELECT * FROM Catalog WHERE Price={price:UInt64} AND Quantity={quantity:UInt64}; CREATE VIEW v2 AS SELECT * FROM Catalog WHERE Price={price:UInt64} AND Quantity={quantity:UInt64};