Add --stage option for clickhouse-client

It is sometimes useful to process queries not up to the Complete stage
but intermediate some (i.e. for Distributed queries debugging and
similar).
This commit is contained in:
Azat Khuzhin 2020-11-10 22:37:44 +03:00
parent f39457bc77
commit ab8f13b885
7 changed files with 86 additions and 14 deletions

View File

@ -49,7 +49,6 @@ using Ports = std::vector<UInt16>;
namespace ErrorCodes namespace ErrorCodes
{ {
extern const int CANNOT_BLOCK_SIGNAL; extern const int CANNOT_BLOCK_SIGNAL;
extern const int BAD_ARGUMENTS;
extern const int EMPTY_DATA_PASSED; extern const int EMPTY_DATA_PASSED;
} }
@ -103,17 +102,7 @@ public:
/// (example: when using stage = 'with_mergeable_state') /// (example: when using stage = 'with_mergeable_state')
registerAggregateFunctions(); registerAggregateFunctions();
if (stage == "complete") query_processing_stage = QueryProcessingStage::fromString(stage);
query_processing_stage = QueryProcessingStage::Complete;
else if (stage == "fetch_columns")
query_processing_stage = QueryProcessingStage::FetchColumns;
else if (stage == "with_mergeable_state")
query_processing_stage = QueryProcessingStage::WithMergeableState;
else if (stage == "with_mergeable_state_after_aggregation")
query_processing_stage = QueryProcessingStage::WithMergeableStateAfterAggregation;
else
throw Exception("Unknown query processing stage: " + stage, ErrorCodes::BAD_ARGUMENTS);
} }
void initialize(Poco::Util::Application & self [[maybe_unused]]) override void initialize(Poco::Util::Application & self [[maybe_unused]]) override

View File

@ -223,6 +223,7 @@ private:
/// We will format query_id in interactive mode in various ways, the default is just to print Query id: ... /// We will format query_id in interactive mode in various ways, the default is just to print Query id: ...
std::vector<std::pair<String, String>> query_id_formats; std::vector<std::pair<String, String>> query_id_formats;
QueryProcessingStage::Enum query_processing_stage;
void initialize(Poco::Util::Application & self) override void initialize(Poco::Util::Application & self) override
{ {
@ -1441,7 +1442,7 @@ private:
connection_parameters.timeouts, connection_parameters.timeouts,
query_to_send, query_to_send,
context.getCurrentQueryId(), context.getCurrentQueryId(),
QueryProcessingStage::Complete, query_processing_stage,
&context.getSettingsRef(), &context.getSettingsRef(),
&context.getClientInfo(), &context.getClientInfo(),
true); true);
@ -1482,7 +1483,7 @@ private:
connection_parameters.timeouts, connection_parameters.timeouts,
query_to_send, query_to_send,
context.getCurrentQueryId(), context.getCurrentQueryId(),
QueryProcessingStage::Complete, query_processing_stage,
&context.getSettingsRef(), &context.getSettingsRef(),
&context.getClientInfo(), &context.getClientInfo(),
true); true);
@ -2304,6 +2305,7 @@ public:
("password", po::value<std::string>()->implicit_value("\n", ""), "password") ("password", po::value<std::string>()->implicit_value("\n", ""), "password")
("ask-password", "ask-password") ("ask-password", "ask-password")
("quota_key", po::value<std::string>(), "A string to differentiate quotas when the user have keyed quotas configured on server") ("quota_key", po::value<std::string>(), "A string to differentiate quotas when the user have keyed quotas configured on server")
("stage", po::value<std::string>()->default_value("complete"), "Request query processing up to specified stage: complete,fetch_columns,with_mergeable_state,with_mergeable_state_after_aggregation")
("query_id", po::value<std::string>(), "query_id") ("query_id", po::value<std::string>(), "query_id")
("query,q", po::value<std::string>(), "query") ("query,q", po::value<std::string>(), "query")
("database,d", po::value<std::string>(), "database") ("database,d", po::value<std::string>(), "database")
@ -2427,6 +2429,8 @@ public:
if (options.count("config-file") && options.count("config")) if (options.count("config-file") && options.count("config"))
throw Exception("Two or more configuration files referenced in arguments", ErrorCodes::BAD_ARGUMENTS); throw Exception("Two or more configuration files referenced in arguments", ErrorCodes::BAD_ARGUMENTS);
query_processing_stage = QueryProcessingStage::fromString(options["stage"].as<std::string>());
/// Save received data into the internal config. /// Save received data into the internal config.
if (options.count("config-file")) if (options.count("config-file"))
config().setString("config-file", options["config-file"].as<std::string>()); config().setString("config-file", options["config-file"].as<std::string>());

View File

@ -0,0 +1,35 @@
#include <Core/QueryProcessingStage.h>
#include <Common/Exception.h>
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
namespace QueryProcessingStage
{
Enum fromString(const std::string & stage_string)
{
Enum stage;
if (stage_string == "complete")
stage = Complete;
else if (stage_string == "fetch_columns")
stage = FetchColumns;
else if (stage_string == "with_mergeable_state")
stage = WithMergeableState;
else if (stage_string == "with_mergeable_state_after_aggregation")
stage = WithMergeableStateAfterAggregation;
else
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unknown query processing stage: {}", stage_string);
return stage;
}
}
}

View File

@ -43,6 +43,14 @@ namespace QueryProcessingStage
? data[stage] ? data[stage]
: "Unknown stage"; : "Unknown stage";
} }
/// This methid is used for the program options,
/// hence it accept under_score notation for stage:
/// - complete
/// - fetch_columns
/// - with_mergeable_state
/// - with_mergeable_state_after_aggregation
Enum fromString(const std::string & stage_string);
} }
} }

View File

@ -30,6 +30,7 @@ SRCS(
MySQL/PacketsReplication.cpp MySQL/PacketsReplication.cpp
NamesAndTypes.cpp NamesAndTypes.cpp
PostgreSQLProtocol.cpp PostgreSQLProtocol.cpp
QueryProcessingStage.cpp
Settings.cpp Settings.cpp
SettingsEnums.cpp SettingsEnums.cpp
SettingsFields.cpp SettingsFields.cpp

View File

@ -0,0 +1,15 @@
execute: default
"foo"
1
execute: --stage fetch_columns
"dummy"
0
execute: --stage with_mergeable_state
"1"
1
execute: --stage with_mergeable_state_after_aggregation
"1"
1
execute: --stage complete
"foo"
1

View File

@ -0,0 +1,20 @@
#!/usr/bin/env bash
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. "$CURDIR"/../shell_config.sh
function execute_query()
{
if [ $# -eq 0 ]; then
echo "execute: default"
else
echo "execute: $*"
fi
${CLICKHOUSE_CLIENT} "$@" --format CSVWithNames -q "SELECT 1 AS foo"
}
execute_query # default -- complete
execute_query --stage fetch_columns
execute_query --stage with_mergeable_state
execute_query --stage with_mergeable_state_after_aggregation
execute_query --stage complete