2022-07-14 11:20:16 +00:00
|
|
|
#include <Interpreters/InterpreterSelectQueryAnalyzer.h>
|
|
|
|
|
|
|
|
#include <Parsers/ASTSelectWithUnionQuery.h>
|
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
2022-07-18 17:20:28 +00:00
|
|
|
#include <Parsers/ASTSubquery.h>
|
2022-07-14 11:20:16 +00:00
|
|
|
|
|
|
|
#include <Analyzer/QueryTreeBuilder.h>
|
|
|
|
#include <Analyzer/QueryTreePassManager.h>
|
|
|
|
|
2022-10-07 13:20:45 +00:00
|
|
|
#include <Processors/QueryPlan/IQueryPlanStep.h>
|
2022-07-14 11:20:16 +00:00
|
|
|
#include <Processors/QueryPlan/QueryPlan.h>
|
|
|
|
#include <Processors/QueryPlan/Optimizations/QueryPlanOptimizationSettings.h>
|
|
|
|
#include <QueryPipeline/QueryPipelineBuilder.h>
|
|
|
|
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int UNSUPPORTED_METHOD;
|
|
|
|
}
|
|
|
|
|
2022-08-24 09:21:03 +00:00
|
|
|
namespace
|
2022-08-15 16:34:10 +00:00
|
|
|
{
|
|
|
|
|
2022-08-24 09:21:03 +00:00
|
|
|
ASTPtr normalizeAndValidateQuery(const ASTPtr & query)
|
2022-08-15 16:34:10 +00:00
|
|
|
{
|
2022-08-24 09:21:03 +00:00
|
|
|
if (query->as<ASTSelectWithUnionQuery>() || query->as<ASTSelectQuery>())
|
2022-08-15 16:34:10 +00:00
|
|
|
{
|
2022-08-24 09:21:03 +00:00
|
|
|
return query;
|
2022-08-15 16:34:10 +00:00
|
|
|
}
|
2022-08-24 09:21:03 +00:00
|
|
|
else if (auto * subquery = query->as<ASTSubquery>())
|
2022-08-15 16:34:10 +00:00
|
|
|
{
|
2022-08-24 09:21:03 +00:00
|
|
|
return subquery->children[0];
|
2022-08-15 16:34:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::UNSUPPORTED_METHOD,
|
2022-08-24 09:21:03 +00:00
|
|
|
"Expected ASTSelectWithUnionQuery or ASTSelectQuery. Actual {}",
|
|
|
|
query->formatForErrorMessage());
|
2022-08-20 14:01:50 +00:00
|
|
|
}
|
2022-08-15 16:34:10 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 09:21:03 +00:00
|
|
|
QueryTreeNodePtr buildQueryTreeAndRunPasses(const ASTPtr & query, const ContextPtr & context)
|
2022-08-21 11:46:07 +00:00
|
|
|
{
|
2022-09-27 15:04:03 +00:00
|
|
|
auto query_tree = buildQueryTree(query, context);
|
2022-08-21 11:46:07 +00:00
|
|
|
|
2022-08-24 09:21:03 +00:00
|
|
|
QueryTreePassManager query_tree_pass_manager(context);
|
|
|
|
addQueryTreePasses(query_tree_pass_manager);
|
|
|
|
query_tree_pass_manager.run(query_tree);
|
2022-08-15 16:34:10 +00:00
|
|
|
|
2022-08-24 09:21:03 +00:00
|
|
|
return query_tree;
|
2022-08-15 16:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:20:28 +00:00
|
|
|
InterpreterSelectQueryAnalyzer::InterpreterSelectQueryAnalyzer(
|
|
|
|
const ASTPtr & query_,
|
2022-07-14 11:20:16 +00:00
|
|
|
const SelectQueryOptions & select_query_options_,
|
|
|
|
ContextPtr context_)
|
|
|
|
: WithContext(context_)
|
2022-08-24 09:21:03 +00:00
|
|
|
, query(normalizeAndValidateQuery(query_))
|
|
|
|
, query_tree(buildQueryTreeAndRunPasses(query, context_))
|
2022-07-14 11:20:16 +00:00
|
|
|
, select_query_options(select_query_options_)
|
2022-08-24 09:21:03 +00:00
|
|
|
, planner(query_tree, select_query_options, context_)
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
2022-07-18 17:20:28 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 15:21:17 +00:00
|
|
|
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_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-24 09:21:03 +00:00
|
|
|
Block InterpreterSelectQueryAnalyzer::getSampleBlock()
|
2022-07-18 17:20:28 +00:00
|
|
|
{
|
2022-08-24 14:50:53 +00:00
|
|
|
planner.buildQueryPlanIfNeeded();
|
2022-08-24 09:21:03 +00:00
|
|
|
return planner.getQueryPlan().getCurrentDataStream().header;
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 09:21:03 +00:00
|
|
|
QueryPlan && InterpreterSelectQueryAnalyzer::extractQueryPlan() &&
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
2022-08-24 14:50:53 +00:00
|
|
|
planner.buildQueryPlanIfNeeded();
|
2022-08-24 09:21:03 +00:00
|
|
|
return std::move(planner).extractQueryPlan();
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockIO InterpreterSelectQueryAnalyzer::execute()
|
|
|
|
{
|
2022-08-24 14:50:53 +00:00
|
|
|
planner.buildQueryPlanIfNeeded();
|
2022-08-24 09:21:03 +00:00
|
|
|
auto & query_plan = planner.getQueryPlan();
|
2022-07-14 11:20:16 +00:00
|
|
|
|
|
|
|
QueryPlanOptimizationSettings optimization_settings;
|
|
|
|
BuildQueryPipelineSettings build_pipeline_settings;
|
|
|
|
auto pipeline_builder = query_plan.buildQueryPipeline(optimization_settings, build_pipeline_settings);
|
|
|
|
|
|
|
|
BlockIO res;
|
|
|
|
res.pipeline = QueryPipelineBuilder::getPipeline(std::move(*pipeline_builder));
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|