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>
|
2022-10-12 11:26:02 +00:00
|
|
|
#include <Interpreters/QueryLog.h>
|
2022-07-14 11:20:16 +00:00
|
|
|
|
2023-01-20 13:49:50 +00:00
|
|
|
#include <Core/ProtocolDefines.h>
|
2023-01-20 11:55:20 +00:00
|
|
|
#include "config_version.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
|
|
|
}
|
|
|
|
|
2022-12-23 17:45:28 +00:00
|
|
|
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);
|
2022-12-23 17:45:28 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:20:28 +00:00
|
|
|
InterpreterSelectQueryAnalyzer::InterpreterSelectQueryAnalyzer(
|
|
|
|
const ASTPtr & query_,
|
2022-12-23 17:45:28 +00:00
|
|
|
const ContextPtr & context_,
|
|
|
|
const SelectQueryOptions & select_query_options_)
|
2022-12-06 09:44:38 +00:00
|
|
|
: query(normalizeAndValidateQuery(query_))
|
2022-12-23 17:45:28 +00:00
|
|
|
, context(Context::createCopy(context_))
|
2022-07-14 11:20:16 +00:00
|
|
|
, select_query_options(select_query_options_)
|
2022-12-23 17:45:28 +00:00
|
|
|
, query_tree(buildQueryTreeAndRunPasses(query, select_query_options, context))
|
2022-12-06 09:44:38 +00:00
|
|
|
, planner(query_tree, 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_,
|
2022-12-23 17:45:28 +00:00
|
|
|
const ContextPtr & context_,
|
|
|
|
const SelectQueryOptions & select_query_options_)
|
2022-12-06 09:44:38 +00:00
|
|
|
: query(query_tree_->toAST())
|
2022-12-23 17:45:28 +00:00
|
|
|
, context(Context::createCopy(context_))
|
2022-08-31 15:21:17 +00:00
|
|
|
, select_query_options(select_query_options_)
|
2022-12-23 17:45:28 +00:00
|
|
|
, query_tree(query_tree_)
|
2022-12-06 09:44:38 +00:00
|
|
|
, planner(query_tree, select_query_options)
|
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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-10-12 11:26:02 +00:00
|
|
|
BlockIO result;
|
|
|
|
result.pipeline = QueryPipelineBuilder::getPipeline(std::move(*pipeline_builder));
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
QueryPlanOptimizationSettings optimization_settings;
|
|
|
|
BuildQueryPipelineSettings build_pipeline_settings;
|
|
|
|
|
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
|
|
|
}
|