Parameterized views: Analyze SELECT query

This commit is contained in:
Dmitry Novik 2024-08-27 20:20:36 +02:00
parent 9822b2f306
commit 967ba9d3d4
4 changed files with 39 additions and 11 deletions

View File

@ -845,18 +845,22 @@ InterpreterCreateQuery::TableProperties InterpreterCreateQuery::getTableProperti
}
else if (create.select)
{
ASTPtr query = create.select->clone();
if (create.isParameterizedView())
return properties;
{
replaceQueryParametersWithDefaults(query);
}
Block as_select_sample;
if (getContext()->getSettingsRef().allow_experimental_analyzer)
{
as_select_sample = InterpreterSelectQueryAnalyzer::getSampleBlock(create.select->clone(), getContext());
as_select_sample = InterpreterSelectQueryAnalyzer::getSampleBlock(query, getContext());
}
else
{
as_select_sample = InterpreterSelectWithUnionQuery::getSampleBlock(create.select->clone(), getContext());
as_select_sample = InterpreterSelectWithUnionQuery::getSampleBlock(query, getContext());
}
properties.columns = ColumnsDescription(as_select_sample.getNamesAndTypesList());

View File

@ -156,4 +156,33 @@ void ReplaceQueryParameterVisitor::visitIdentifier(ASTPtr & ast)
ast_identifier->children.clear();
}
void replaceQueryParametersWithDefaults(ASTPtr & ast)
{
std::vector<ASTPtr> nodes_to_process{ ast };
while (!nodes_to_process.empty())
{
auto node = nodes_to_process.back();
nodes_to_process.pop_back();
for (auto & child : node->children)
{
if (auto * query_param = child->as<ASTQueryParameter>())
{
const auto data_type = DataTypeFactory::instance().get(query_param->type);
auto * old_ptr = child.get();
Field literal = data_type->getDefault();
if (typeid_cast<const DataTypeString *>(data_type.get()))
child = std::make_shared<ASTLiteral>(literal);
else
child = addTypeConversionToAST(std::make_shared<ASTLiteral>(literal), query_param->type);
node->updatePointerToChild(old_ptr, child.get());
}
else
nodes_to_process.push_back(child);
}
}
}
}

View File

@ -32,4 +32,6 @@ private:
void visitChildren(ASTPtr & ast);
};
void replaceQueryParametersWithDefaults(ASTPtr & ast);
}

View File

@ -116,14 +116,7 @@ StorageView::StorageView(
: IStorage(table_id_)
{
StorageInMemoryMetadata storage_metadata;
if (!is_parameterized_view_)
{
/// If CREATE query is to create parameterized view, then we dont want to set columns
if (!query.isParameterizedView())
storage_metadata.setColumns(columns_);
}
else
storage_metadata.setColumns(columns_);
storage_metadata.setColumns(columns_);
storage_metadata.setComment(comment);
if (query.sql_security)