Merge pull request #44230 from kitaisreal/analyzer-storage-view-crash-fix

Analyzer storage view crash fix
This commit is contained in:
Maksim Kita 2022-12-16 13:54:16 +03:00 committed by GitHub
commit 97e83eb53c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 53 additions and 6 deletions

View File

@ -112,6 +112,11 @@ QueryPlan && InterpreterSelectQueryAnalyzer::extractQueryPlan() &&
return std::move(planner).extractQueryPlan();
}
void InterpreterSelectQueryAnalyzer::addStorageLimits(const StorageLimitsList & storage_limits)
{
planner.addStorageLimits(storage_limits);
}
void InterpreterSelectQueryAnalyzer::extendQueryLogElemImpl(QueryLogElement & elem, const ASTPtr &, ContextPtr) const
{
elem.query_kind = "Select";

View File

@ -41,6 +41,8 @@ public:
QueryPlan && extractQueryPlan() &&;
void addStorageLimits(const StorageLimitsList & storage_limits);
bool supportsTransactions() const override { return true; }
bool ignoreLimits() const override { return select_query_options.ignore_limits; }

View File

@ -365,9 +365,9 @@ void Planner::buildQueryPlanIfNeeded()
select_query_info.query = select_query_info.original_query;
select_query_info.planner_context = planner_context;
StorageLimitsList storage_limits;
storage_limits.push_back(buildStorageLimits(*query_context, select_query_options));
select_query_info.storage_limits = std::make_shared<StorageLimitsList>(storage_limits);
auto current_storage_limits = storage_limits;
current_storage_limits.push_back(buildStorageLimits(*query_context, select_query_options));
select_query_info.storage_limits = std::make_shared<StorageLimitsList>(std::move(current_storage_limits));
collectTableExpressionData(query_tree, *planner_context);
checkStoragesSupportTransactions(planner_context);
@ -847,4 +847,10 @@ void Planner::buildQueryPlanIfNeeded()
extendQueryContextAndStoragesLifetime(query_plan, planner_context);
}
void Planner::addStorageLimits(const StorageLimitsList & limits)
{
for (const auto & limit : limits)
storage_limits.push_back(limit);
}
}

View File

@ -45,11 +45,14 @@ public:
return std::move(query_plan);
}
void addStorageLimits(const StorageLimitsList & limits);
private:
QueryTreeNodePtr query_tree;
QueryPlan query_plan;
SelectQueryOptions select_query_options;
PlannerContextPtr planner_context;
StorageLimitsList storage_limits;
};
}

View File

@ -1,5 +1,6 @@
#include <Interpreters/InterpreterSelectQuery.h>
#include <Interpreters/InterpreterSelectWithUnionQuery.h>
#include <Interpreters/InterpreterSelectQueryAnalyzer.h>
#include <Interpreters/Context.h>
#include <DataTypes/DataTypeLowCardinality.h>
@ -123,9 +124,19 @@ void StorageView::read(
}
auto options = SelectQueryOptions(QueryProcessingStage::Complete, 0, false, query_info.settings_limit_offset_done);
InterpreterSelectWithUnionQuery interpreter(current_inner_query, context, options, column_names);
interpreter.addStorageLimits(*query_info.storage_limits);
interpreter.buildQueryPlan(query_plan);
if (context->getSettingsRef().allow_experimental_analyzer)
{
InterpreterSelectQueryAnalyzer interpreter(current_inner_query, options, context);
interpreter.addStorageLimits(*query_info.storage_limits);
query_plan = std::move(interpreter).extractQueryPlan();
}
else
{
InterpreterSelectWithUnionQuery interpreter(current_inner_query, context, options, column_names);
interpreter.addStorageLimits(*query_info.storage_limits);
interpreter.buildQueryPlan(query_plan);
}
/// It's expected that the columns read from storage are not constant.
/// Because method 'getSampleBlockForColumns' is used to obtain a structure of result in InterpreterSelectQuery.

View File

@ -0,0 +1,19 @@
SET allow_experimental_analyzer = 1;
DROP TABLE IF EXISTS test_table;
CREATE TABLE test_table
(
f1 Int32,
f2 Int32,
pk Int32
) ENGINE = MergeTree PARTITION BY pk ORDER BY f1;
INSERT INTO test_table SELECT number, number, number FROM numbers(10);
DROP VIEW IF EXISTS test_view;
CREATE VIEW test_view AS SELECT f1, f2 FROM test_table WHERE pk = 2;
SELECT * FROM test_view;
DROP VIEW test_view;
DROP TABLE test_table;