ClickHouse/src/Interpreters/InterpreterSelectQueryAnalyzer.cpp

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

258 lines
8.8 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>
#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>
2023-02-11 11:10:53 +00:00
#include <Storages/IStorage.h>
#include <Analyzer/QueryTreeBuilder.h>
#include <Analyzer/QueryTreePassManager.h>
2023-02-15 14:03:52 +00:00
#include <Analyzer/IdentifierNode.h>
2023-02-11 11:10:53 +00:00
#include <Analyzer/QueryNode.h>
#include <Analyzer/UnionNode.h>
#include <Analyzer/TableNode.h>
2023-02-15 14:03:52 +00:00
#include <Analyzer/TableFunctionNode.h>
2023-02-11 11:10:53 +00:00
#include <Analyzer/Utils.h>
2022-07-14 11:20:16 +00:00
#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;
}
2023-02-14 11:20:01 +00:00
void replaceStorageInQueryTree(QueryTreeNodePtr & query_tree, const ContextPtr & context, const StoragePtr & storage)
2023-02-11 11:10:53 +00:00
{
auto query_to_replace_table_expression = query_tree;
2023-02-14 11:20:01 +00:00
QueryTreeNodePtr table_expression_to_replace;
2023-02-11 11:10:53 +00:00
2023-02-14 11:20:01 +00:00
while (!table_expression_to_replace)
2023-02-11 11:10:53 +00:00
{
if (auto * union_node = query_to_replace_table_expression->as<UnionNode>())
query_to_replace_table_expression = union_node->getQueries().getNodes().at(0);
auto & query_to_replace_table_expression_typed = query_to_replace_table_expression->as<QueryNode &>();
2023-02-14 11:20:01 +00:00
auto left_table_expression = extractLeftTableExpression(query_to_replace_table_expression_typed.getJoinTree());
2023-02-11 11:10:53 +00:00
auto left_table_expression_node_type = left_table_expression->getNodeType();
switch (left_table_expression_node_type)
{
case QueryTreeNodeType::QUERY:
case QueryTreeNodeType::UNION:
{
2023-02-14 11:20:01 +00:00
query_to_replace_table_expression = std::move(left_table_expression);
break;
2023-02-11 11:10:53 +00:00
}
case QueryTreeNodeType::TABLE:
2023-02-14 11:20:01 +00:00
case QueryTreeNodeType::TABLE_FUNCTION:
2023-02-11 11:10:53 +00:00
case QueryTreeNodeType::IDENTIFIER:
{
2023-02-14 11:20:01 +00:00
table_expression_to_replace = std::move(left_table_expression);
break;
2023-02-11 11:10:53 +00:00
}
default:
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD,
2023-02-14 11:20:01 +00:00
"Expected table, table function or identifier node to replace with storage. Actual {}",
2023-02-11 11:10:53 +00:00
left_table_expression->formatASTForErrorMessage());
}
}
}
2023-02-14 11:20:01 +00:00
auto replacement_table_expression = std::make_shared<TableNode>(storage, context);
2023-02-15 14:03:52 +00:00
std::optional<TableExpressionModifiers> table_expression_modifiers;
if (auto * table_node = table_expression_to_replace->as<TableNode>())
table_expression_modifiers = table_node->getTableExpressionModifiers();
else if (auto * table_function_node = table_expression_to_replace->as<TableFunctionNode>())
table_expression_modifiers = table_function_node->getTableExpressionModifiers();
else if (auto * identifier_node = table_expression_to_replace->as<IdentifierNode>())
table_expression_modifiers = identifier_node->getTableExpressionModifiers();
if (table_expression_modifiers)
replacement_table_expression->setTableExpressionModifiers(*table_expression_modifiers);
2023-02-14 11:20:01 +00:00
query_tree = query_tree->cloneAndReplace(table_expression_to_replace, std::move(replacement_table_expression));
2023-02-11 11:10:53 +00:00
}
QueryTreeNodePtr buildQueryTreeAndRunPasses(const ASTPtr & query,
const SelectQueryOptions & select_query_options,
const ContextPtr & context,
const StoragePtr & storage)
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
2023-02-14 11:20:01 +00:00
if (storage)
replaceStorageInQueryTree(query_tree, context, storage);
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_,
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_)
2023-02-11 11:10:53 +00:00
, query_tree(buildQueryTreeAndRunPasses(query, select_query_options, context, nullptr /*storage*/))
2023-02-15 14:03:52 +00:00
, planner(query_tree, select_query_options)
2023-02-11 11:10:53 +00:00
{
}
InterpreterSelectQueryAnalyzer::InterpreterSelectQueryAnalyzer(
const ASTPtr & query_,
const ContextPtr & context_,
2023-02-18 16:06:00 +00:00
const StoragePtr & storage_,
const SelectQueryOptions & select_query_options_)
2023-02-11 11:10:53 +00:00
: query(normalizeAndValidateQuery(query_))
, context(buildContext(context_, select_query_options_))
, select_query_options(select_query_options_)
2023-02-14 11:20:01 +00:00
, query_tree(buildQueryTreeAndRunPasses(query, select_query_options, context, storage_))
2023-02-15 14:03:52 +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_,
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-02-18 16:06:00 +00:00
, planner(query_tree_, select_query_options_)
2023-01-26 10:52:40 +00:00
{
}
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();
}
2023-02-18 16:06:00 +00:00
Block InterpreterSelectQueryAnalyzer::getSampleBlock(const QueryTreeNodePtr & query_tree,
2023-01-26 10:52:40 +00:00
const ContextPtr & context,
const SelectQueryOptions & select_query_options)
{
auto select_query_options_copy = select_query_options;
select_query_options_copy.only_analyze = true;
2023-02-18 16:06:00 +00:00
InterpreterSelectQueryAnalyzer interpreter(query_tree, context, select_query_options);
2023-01-26 10:52:40 +00:00
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
2023-02-14 11:20:01 +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;
}
2023-03-04 17:46:40 +00:00
QueryPlan & InterpreterSelectQueryAnalyzer::getQueryPlan()
{
planner.buildQueryPlanIfNeeded();
return planner.getQueryPlan();
}
2022-10-12 11:26:02 +00:00
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
}