ClickHouse/src/Interpreters/InterpreterSelectQueryAnalyzer.cpp
2022-10-24 10:22:37 +02:00

121 lines
3.4 KiB
C++

#include <Interpreters/InterpreterSelectQueryAnalyzer.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/ASTSelectQuery.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTSubquery.h>
#include <Analyzer/QueryTreeBuilder.h>
#include <Analyzer/QueryTreePassManager.h>
#include <Processors/QueryPlan/IQueryPlanStep.h>
#include <Processors/QueryPlan/QueryPlan.h>
#include <Processors/QueryPlan/Optimizations/QueryPlanOptimizationSettings.h>
#include <QueryPipeline/QueryPipelineBuilder.h>
#include <Interpreters/Context.h>
#include <Interpreters/QueryLog.h>
namespace DB
{
namespace ErrorCodes
{
extern const int UNSUPPORTED_METHOD;
}
namespace
{
ASTPtr normalizeAndValidateQuery(const ASTPtr & query)
{
if (query->as<ASTSelectWithUnionQuery>() || query->as<ASTSelectQuery>())
{
return query;
}
else if (auto * subquery = query->as<ASTSubquery>())
{
return subquery->children[0];
}
else
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD,
"Expected ASTSelectWithUnionQuery or ASTSelectQuery. Actual {}",
query->formatForErrorMessage());
}
}
QueryTreeNodePtr buildQueryTreeAndRunPasses(const ASTPtr & query, const ContextPtr & context)
{
auto query_tree = buildQueryTree(query, context);
QueryTreePassManager query_tree_pass_manager(context);
addQueryTreePasses(query_tree_pass_manager);
query_tree_pass_manager.run(query_tree);
return query_tree;
}
}
InterpreterSelectQueryAnalyzer::InterpreterSelectQueryAnalyzer(
const ASTPtr & query_,
const SelectQueryOptions & select_query_options_,
ContextPtr context_)
: WithContext(context_)
, query(normalizeAndValidateQuery(query_))
, query_tree(buildQueryTreeAndRunPasses(query, context_))
, select_query_options(select_query_options_)
, planner(query_tree, select_query_options, context_)
{
}
InterpreterSelectQueryAnalyzer::InterpreterSelectQueryAnalyzer(
const QueryTreeNodePtr & query_tree_,
const SelectQueryOptions & select_query_options_,
ContextPtr context_)
: WithContext(context_)
, query(query_tree_->toAST())
, query_tree(query_tree_)
, select_query_options(select_query_options_)
, planner(query_tree, select_query_options, context_)
{
}
Block InterpreterSelectQueryAnalyzer::getSampleBlock()
{
planner.buildQueryPlanIfNeeded();
return planner.getQueryPlan().getCurrentDataStream().header;
}
BlockIO InterpreterSelectQueryAnalyzer::execute()
{
planner.buildQueryPlanIfNeeded();
auto & query_plan = planner.getQueryPlan();
QueryPlanOptimizationSettings optimization_settings;
BuildQueryPipelineSettings build_pipeline_settings;
auto pipeline_builder = query_plan.buildQueryPipeline(optimization_settings, build_pipeline_settings);
BlockIO result;
result.pipeline = QueryPipelineBuilder::getPipeline(std::move(*pipeline_builder));
if (!select_query_options.ignore_quota && (select_query_options.to_stage == QueryProcessingStage::Complete))
result.pipeline.setQuota(getContext()->getQuota());
return result;
}
QueryPlan && InterpreterSelectQueryAnalyzer::extractQueryPlan() &&
{
planner.buildQueryPlanIfNeeded();
return std::move(planner).extractQueryPlan();
}
void InterpreterSelectQueryAnalyzer::extendQueryLogElemImpl(QueryLogElement & elem, const ASTPtr &, ContextPtr) const
{
elem.query_kind = "Select";
}
}