|
23 | 23 | #include "network/postgres_protocol_handler.h"
|
24 | 24 | #include "network/peloton_server.h"
|
25 | 25 | #include "parser/postgresparser.h"
|
| 26 | +#include "parser/statements.h" |
26 | 27 | #include "planner/abstract_plan.h"
|
27 | 28 | #include "planner/delete_plan.h"
|
28 | 29 | #include "planner/insert_plan.h"
|
| 30 | +#include "planner/plan_util.h" |
29 | 31 | #include "planner/update_plan.h"
|
30 | 32 | #include "settings/settings_manager.h"
|
31 | 33 | #include "traffic_cop/traffic_cop.h"
|
32 | 34 | #include "type/value.h"
|
33 | 35 | #include "type/value_factory.h"
|
| 36 | +#include "util/string_util.h" |
34 | 37 |
|
35 | 38 | #define SSL_MESSAGE_VERNO 80877103
|
36 | 39 | #define PROTO_MAJOR_VERSION(x) (x >> 16)
|
@@ -219,6 +222,12 @@ ProcessResult PostgresProtocolHandler::ExecQueryMessage(
|
219 | 222 | ExecQueryMessageGetResult(status);
|
220 | 223 | return ProcessResult::COMPLETE;
|
221 | 224 | };
|
| 225 | + case QueryType::QUERY_EXPLAIN: { |
| 226 | + auto status = ExecQueryExplain( |
| 227 | + query, static_cast<parser::ExplainStatement &>(*sql_stmt)); |
| 228 | + ExecQueryMessageGetResult(status); |
| 229 | + return ProcessResult::COMPLETE; |
| 230 | + } |
222 | 231 | default: {
|
223 | 232 | std::string stmt_name = "unamed";
|
224 | 233 | std::unique_ptr<parser::SQLStatementList> unnamed_sql_stmt_list(
|
@@ -248,6 +257,30 @@ ProcessResult PostgresProtocolHandler::ExecQueryMessage(
|
248 | 257 | }
|
249 | 258 | }
|
250 | 259 |
|
| 260 | +ResultType PostgresProtocolHandler::ExecQueryExplain( |
| 261 | + const std::string &query, parser::ExplainStatement &explain_stmt) { |
| 262 | + std::unique_ptr<parser::SQLStatementList> unnamed_sql_stmt_list( |
| 263 | + new parser::SQLStatementList()); |
| 264 | + unnamed_sql_stmt_list->PassInStatement(std::move(explain_stmt.real_sql_stmt)); |
| 265 | + auto stmt = traffic_cop_->PrepareStatement( |
| 266 | + "explain", query, std::move(unnamed_sql_stmt_list)); |
| 267 | + ResultType status = ResultType::UNKNOWN; |
| 268 | + if (stmt != nullptr) { |
| 269 | + traffic_cop_->SetStatement(stmt); |
| 270 | + std::vector<std::string> plan_info = StringUtil::Split( |
| 271 | + planner::PlanUtil::GetInfo(stmt->GetPlanTree().get()), '\n'); |
| 272 | + const std::vector<FieldInfo> tuple_descriptor = { |
| 273 | + traffic_cop_->GetColumnFieldForValueType("Query plan", |
| 274 | + type::TypeId::VARCHAR)}; |
| 275 | + stmt->SetTupleDescriptor(tuple_descriptor); |
| 276 | + traffic_cop_->SetResult(plan_info); |
| 277 | + status = ResultType::SUCCESS; |
| 278 | + } else { |
| 279 | + status = ResultType::FAILURE; |
| 280 | + } |
| 281 | + return status; |
| 282 | +} |
| 283 | + |
251 | 284 | void PostgresProtocolHandler::ExecQueryMessageGetResult(ResultType status) {
|
252 | 285 | std::vector<FieldInfo> tuple_descriptor;
|
253 | 286 | if (status == ResultType::SUCCESS) {
|
@@ -826,7 +859,7 @@ void PostgresProtocolHandler::ExecExecuteMessageGetResult(ResultType status) {
|
826 | 859 | return;
|
827 | 860 | }
|
828 | 861 | }
|
829 |
| -} // namespace network |
| 862 | +} |
830 | 863 |
|
831 | 864 | void PostgresProtocolHandler::GetResult() {
|
832 | 865 | traffic_cop_->ExecuteStatementPlanGetResult();
|
|
0 commit comments