ClickHouse/src/Interpreters/InterpreterSelectQueryAnalyzer.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

176 lines
5.6 KiB
C++
Raw Normal View History

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
2023-02-02 18:25:32 +00:00
#include <DataTypes/DataTypesNumber.h>
2022-07-14 11:20:16 +00:00
#include <Analyzer/QueryTreeBuilder.h>
#include <Analyzer/QueryTreePassManager.h>
#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>
2022-10-12 11:26:02 +00:00
#include <Interpreters/QueryLog.h>
2022-07-14 11:20:16 +00:00
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
}
2023-02-02 18:25:32 +00:00
ContextMutablePtr buildContext(const ContextPtr & context, const SelectQueryOptions & select_query_options)
{
auto result_context = Context::createCopy(context);
if (select_query_options.shard_num)
result_context->addSpecialScalar(
"_shard_num",
Block{{DataTypeUInt32().createColumnConst(1, *select_query_options.shard_num), std::make_shared<DataTypeUInt32>(), "_shard_num"}});
if (select_query_options.shard_count)
result_context->addSpecialScalar(
"_shard_count",
Block{{DataTypeUInt32().createColumnConst(1, *select_query_options.shard_count), std::make_shared<DataTypeUInt32>(), "_shard_count"}});
return result_context;
}
QueryTreeNodePtr buildQueryTreeAndRunPasses(const ASTPtr & query, const SelectQueryOptions & select_query_options, 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);
if (select_query_options.ignore_ast_optimizations)
query_tree_pass_manager.run(query_tree, 1 /*up_to_pass_index*/);
else
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
}
2023-01-26 10:52:40 +00:00
PlannerConfiguration buildPlannerConfiguration(const SelectQueryOptions & select_query_options)
{
return {.only_analyze = select_query_options.only_analyze};
}
2022-08-15 16:34:10 +00:00
}
2022-07-18 17:20:28 +00:00
InterpreterSelectQueryAnalyzer::InterpreterSelectQueryAnalyzer(
const ASTPtr & query_,
const ContextPtr & context_,
const SelectQueryOptions & select_query_options_)
2022-12-06 09:44:38 +00:00
: query(normalizeAndValidateQuery(query_))
2023-02-02 18:25:32 +00:00
, context(buildContext(context_, select_query_options_))
2022-07-14 11:20:16 +00:00
, select_query_options(select_query_options_)
, query_tree(buildQueryTreeAndRunPasses(query, select_query_options, context))
2023-01-26 10:52:40 +00:00
, planner(query_tree, select_query_options, buildPlannerConfiguration(select_query_options))
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 ContextPtr & context_,
const SelectQueryOptions & select_query_options_)
2022-12-06 09:44:38 +00:00
: query(query_tree_->toAST())
2023-02-02 18:25:32 +00:00
, context(buildContext(context_, select_query_options_))
2022-08-31 15:21:17 +00:00
, select_query_options(select_query_options_)
, query_tree(query_tree_)
2023-01-26 10:52:40 +00:00
, planner(query_tree, select_query_options, buildPlannerConfiguration(select_query_options))
{
}
Block InterpreterSelectQueryAnalyzer::getSampleBlock(const ASTPtr & query,
const ContextPtr & context,
const SelectQueryOptions & select_query_options)
2022-08-31 15:21:17 +00:00
{
2023-01-26 10:52:40 +00:00
auto select_query_options_copy = select_query_options;
select_query_options_copy.only_analyze = true;
InterpreterSelectQueryAnalyzer interpreter(query, context, select_query_options_copy);
return interpreter.getSampleBlock();
}
Block InterpreterSelectQueryAnalyzer::getSampleBlock(const QueryTreeNodePtr & query,
const ContextPtr & context,
const SelectQueryOptions & select_query_options)
{
auto select_query_options_copy = select_query_options;
select_query_options_copy.only_analyze = true;
InterpreterSelectQueryAnalyzer interpreter(query, context, select_query_options_copy);
return interpreter.getSampleBlock();
2022-08-31 15:21:17 +00:00
}
2022-08-24 09:21:03 +00:00
Block InterpreterSelectQueryAnalyzer::getSampleBlock()
2022-07-18 17:20:28 +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
}
BlockIO InterpreterSelectQueryAnalyzer::execute()
{
2023-02-02 18:25:32 +00:00
auto pipeline_builder = buildQueryPipeline();
2022-07-14 11:20:16 +00:00
2022-10-12 11:26:02 +00:00
BlockIO result;
2023-02-02 18:25:32 +00:00
result.pipeline = QueryPipelineBuilder::getPipeline(std::move(pipeline_builder));
2022-10-12 11:26:02 +00:00
if (!select_query_options.ignore_quota && (select_query_options.to_stage == QueryProcessingStage::Complete))
2022-12-06 09:44:38 +00:00
result.pipeline.setQuota(context->getQuota());
2022-10-12 11:26:02 +00:00
return result;
}
QueryPlan && InterpreterSelectQueryAnalyzer::extractQueryPlan() &&
{
planner.buildQueryPlanIfNeeded();
return std::move(planner).extractQueryPlan();
}
2022-07-14 11:20:16 +00:00
2023-01-20 11:19:16 +00:00
QueryPipelineBuilder InterpreterSelectQueryAnalyzer::buildQueryPipeline()
2022-12-15 12:03:09 +00:00
{
planner.buildQueryPlanIfNeeded();
auto & query_plan = planner.getQueryPlan();
2023-02-02 18:25:32 +00:00
auto optimization_settings = QueryPlanOptimizationSettings::fromContext(context);
auto build_pipeline_settings = BuildQueryPipelineSettings::fromContext(context);
2022-12-15 12:03:09 +00:00
2023-01-20 11:19:16 +00:00
return std::move(*query_plan.buildQueryPipeline(optimization_settings, build_pipeline_settings));
2022-12-15 12:03:09 +00:00
}
2022-12-14 17:43:49 +00:00
void InterpreterSelectQueryAnalyzer::addStorageLimits(const StorageLimitsList & storage_limits)
{
planner.addStorageLimits(storage_limits);
}
2022-07-14 11:20:16 +00:00
}