Merge pull request #21579 from azat/dist-query-zero-shards-fix

Fix optimize_skip_unused_shards for zero shards case
This commit is contained in:
Nikolai Kochetov 2021-03-17 11:54:08 +03:00 committed by GitHub
commit 3a3f800f6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 0 deletions

View File

@ -48,6 +48,9 @@
#include <Interpreters/getTableExpressions.h>
#include <Functions/IFunction.h>
#include <Processors/QueryPlan/ReadFromPreparedSource.h>
#include <Processors/Sources/NullSource.h>
#include <Core/Field.h>
#include <Core/Settings.h>
@ -83,6 +86,7 @@ namespace DB
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
extern const int NOT_IMPLEMENTED;
extern const int STORAGE_REQUIRES_PARAMETER;
extern const int BAD_ARGUMENTS;
@ -532,6 +536,17 @@ void StorageDistributed::read(
Block header =
InterpreterSelectQuery(query_info.query, context, SelectQueryOptions(processed_stage).analyze()).getSampleBlock();
/// Return directly (with correct header) if no shard to query.
if (query_info.cluster->getShardsInfo().empty())
{
Pipe pipe(std::make_shared<NullSource>(header));
auto read_from_pipe = std::make_unique<ReadFromPreparedSource>(std::move(pipe));
read_from_pipe->setStepDescription("Read from NullSource (Distributed)");
query_plan.addStep(std::move(read_from_pipe));
return;
}
const Scalars & scalars = context.hasQueryContext() ? context.getQueryContext().getScalars() : Scalars{};
bool has_virtual_shard_num_column = std::find(column_names.begin(), column_names.end(), "_shard_num") != column_names.end();
@ -546,6 +561,10 @@ void StorageDistributed::read(
ClusterProxy::executeQuery(query_plan, select_stream_factory, log,
modified_query_ast, context, query_info);
/// This is a bug, it is possible only when there is no shards to query, and this is handled earlier.
if (!query_plan.isInitialized())
throw Exception("Pipeline is not initialized", ErrorCodes::LOGICAL_ERROR);
}

View File

@ -0,0 +1,2 @@
create table dist_01756 (dummy UInt8) ENGINE = Distributed('test_cluster_two_shards', 'system', 'one', dummy);
select ignore(1), * from dist_01756 where 0 settings optimize_skip_unused_shards=1, force_optimize_skip_unused_shards=1

View File

@ -222,3 +222,4 @@
01305_polygons_union
01306_polygons_intersection
01702_system_query_log
01759_optimize_skip_unused_shards_zero_shards