Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 2836e29

Browse files
committed
Support for EXPLAIN statement
1 parent 19dcdfe commit 2836e29

File tree

8 files changed

+108
-2
lines changed

8 files changed

+108
-2
lines changed

src/common/internal_types.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,9 @@ std::string StatementTypeToString(StatementType type) {
471471
case StatementType::VARIABLE_SET: {
472472
return "SET";
473473
}
474+
case StatementType::EXPLAIN: {
475+
return "EXPLAIN";
476+
}
474477
default: {
475478
throw ConversionException(StringUtil::Format(
476479
"No string conversion for StatementType value '%d'",
@@ -572,6 +575,8 @@ std::string QueryTypeToString(QueryType query_type) {
572575
return "EXECUTE";
573576
case QueryType::QUERY_SELECT:
574577
return "SELECT";
578+
case QueryType::QUERY_EXPLAIN:
579+
return "EXPLAIN";
575580
case QueryType::QUERY_OTHER:
576581
default:
577582
return "OTHER";
@@ -630,6 +635,7 @@ QueryType StatementTypeToQueryType(StatementType stmt_type,
630635
{StatementType::DROP, QueryType::QUERY_DROP},
631636
{StatementType::SELECT, QueryType::QUERY_SELECT},
632637
{StatementType::VARIABLE_SET, QueryType::QUERY_SET},
638+
{StatementType::EXPLAIN, QueryType::QUERY_EXPLAIN}
633639
};
634640
QueryType query_type = QueryType::QUERY_OTHER;
635641
std::unordered_map<StatementType, QueryType,

src/include/common/internal_types.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@ enum class StatementType {
671671
ANALYZE = 15, // analyze type
672672
VARIABLE_SET = 16, // variable set statement type
673673
CREATE_FUNC = 17, // create func statement type
674+
EXPLAIN = 18 // explain statement type
674675
};
675676
std::string StatementTypeToString(StatementType type);
676677
StatementType StringToStatementType(const std::string &str);
@@ -704,7 +705,8 @@ enum class QueryType {
704705
QUERY_INVALID = 20,
705706
QUERY_CREATE_TRIGGER = 21,
706707
QUERY_CREATE_SCHEMA = 22,
707-
QUERY_CREATE_VIEW = 23
708+
QUERY_CREATE_VIEW = 23,
709+
QUERY_EXPLAIN = 24
708710
};
709711
std::string QueryTypeToString(QueryType query_type);
710712
QueryType StringToQueryType(std::string str);
@@ -1416,4 +1418,4 @@ enum class SSLLevel {
14161418
SSL_VERIIFY = 2,
14171419
};
14181420

1419-
} // namespace peloton
1421+
} // namespace peloton
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// explain_statement.h
6+
//
7+
// Identification: src/include/parser/explain_statement.h
8+
//
9+
// Copyright (c) 2018, Pranav Kant
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#pragma once
14+
15+
#include "common/logger.h"
16+
#include "common/sql_node_visitor.h"
17+
#include "expression/abstract_expression.h"
18+
#include "parser/sql_statement.h"
19+
#include "parser/table_ref.h"
20+
21+
namespace peloton {
22+
namespace parser {
23+
24+
/**
25+
* @class ExplainStatement
26+
* @brief Represents "EXPLAIN <query>"
27+
*/
28+
class ExplainStatement : public SQLStatement {
29+
public:
30+
ExplainStatement()
31+
: SQLStatement(StatementType::EXPLAIN) {}
32+
virtual ~ExplainStatement() {}
33+
34+
virtual void Accept(SqlNodeVisitor* /*v*/) override { }
35+
36+
std::unique_ptr<parser::SQLStatement> real_sql_stmt;
37+
};
38+
39+
} // namespace parser
40+
} // namespace peloton

src/include/parser/parsenodes.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,21 @@ typedef struct SelectStmt {
326326
/* Eventually add fields for CORRESPONDING spec here */
327327
} SelectStmt;
328328

329+
/* ----------------------
330+
* Explain Statement
331+
*
332+
* The "query" field is initially a raw parse tree, and is converted to a
333+
* Query node during parse analysis. Note that rewriting and planning
334+
* of the query are always postponed until execution.
335+
* ----------------------
336+
*/
337+
typedef struct ExplainStmt
338+
{
339+
NodeTag type;
340+
Node *query; /* the query (see comments above) */
341+
List *options; /* list of DefElem nodes */
342+
} ExplainStmt;
343+
329344
typedef struct TypeName {
330345
NodeTag type;
331346
List *names; /* qualified name (list of Value strings) */

src/include/parser/postgresparser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ class PostgresParser {
217217
// transform helper for select statements
218218
static parser::SelectStatement *SelectTransform(SelectStmt *root);
219219

220+
// transform helper for explain statements
221+
static parser::SQLStatement *ExplainTransform(ExplainStmt *root);
222+
220223
// transform helper for delete statements
221224
static parser::SQLStatement *DeleteTransform(DeleteStmt *root);
222225

src/include/parser/statements.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "delete_statement.h"
2222
#include "drop_statement.h"
2323
#include "execute_statement.h"
24+
#include "explain_statement.h"
2425
#include "insert_statement.h"
2526
#include "prepare_statement.h"
2627
#include "select_statement.h"

src/network/postgres_protocol_handler.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@
2626
#include "planner/abstract_plan.h"
2727
#include "planner/delete_plan.h"
2828
#include "planner/insert_plan.h"
29+
#include "planner/plan_util.h"
2930
#include "planner/update_plan.h"
3031
#include "settings/settings_manager.h"
3132
#include "traffic_cop/traffic_cop.h"
3233
#include "type/value.h"
3334
#include "type/value_factory.h"
35+
#include "network/marshal.h"
36+
#include "util/string_util.h"
3437

3538
#define SSL_MESSAGE_VERNO 80877103
3639
#define PROTO_MAJOR_VERSION(x) (x >> 16)
@@ -219,6 +222,33 @@ ProcessResult PostgresProtocolHandler::ExecQueryMessage(
219222
ExecQueryMessageGetResult(status);
220223
return ProcessResult::COMPLETE;
221224
};
225+
case QueryType::QUERY_EXPLAIN: {
226+
parser::ExplainStatement *explain_stmt =
227+
static_cast<parser::ExplainStatement *>(sql_stmt.get());
228+
std::unique_ptr<parser::SQLStatementList> unnamed_sql_stmt_list(new parser::SQLStatementList());
229+
unnamed_sql_stmt_list->PassInStatement(
230+
std::move(explain_stmt->real_sql_stmt));
231+
auto stmt = traffic_cop_->PrepareStatement(
232+
"explain", query, std::move(unnamed_sql_stmt_list), error_message);
233+
ResultType status = ResultType::UNKNOWN;
234+
if (stmt) {
235+
traffic_cop_->SetStatement(stmt);
236+
std::vector<std::string> plan_info = StringUtil::Split(
237+
planner::PlanUtil::GetInfo(stmt->GetPlanTree().get()), '\n');
238+
std::vector<FieldInfo> tuple_descriptor = {
239+
traffic_cop_->GetColumnFieldForValueType("Query plan",
240+
type::TypeId::VARCHAR)
241+
};
242+
stmt->SetTupleDescriptor(tuple_descriptor);
243+
traffic_cop_->SetResult(plan_info);
244+
status = ResultType::SUCCESS;
245+
} else {
246+
traffic_cop_->SetErrorMessage(error_message);
247+
status = ResultType::FAILURE;
248+
}
249+
ExecQueryMessageGetResult(status);
250+
return ProcessResult::COMPLETE;
251+
}
222252
default: {
223253
std::string stmt_name = "unamed";
224254
std::unique_ptr<parser::SQLStatementList> unnamed_sql_stmt_list(

src/parser/postgresparser.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,12 @@ parser::SelectStatement *PostgresParser::SelectTransform(SelectStmt *root) {
16861686
return result;
16871687
}
16881688

1689+
parser::SQLStatement *PostgresParser::ExplainTransform(ExplainStmt *root) {
1690+
parser::ExplainStatement *result = new parser::ExplainStatement();
1691+
result->real_sql_stmt.reset(NodeTransform(root->query));
1692+
return result;
1693+
}
1694+
16891695
// This function takes in a Postgres DeleteStmt parsenode
16901696
// and transfers into a Peloton DeleteStatement parsenode.
16911697
// Please refer to parser/parsenode.h for the definition of
@@ -1787,6 +1793,9 @@ parser::SQLStatement *PostgresParser::NodeTransform(Node *stmt) {
17871793
case T_VariableSetStmt:
17881794
result = VariableSetTransform((VariableSetStmt*)stmt);
17891795
break;
1796+
case T_ExplainStmt:
1797+
result = ExplainTransform(reinterpret_cast<ExplainStmt*>(stmt));
1798+
break;
17901799
default: {
17911800
throw NotImplementedException(StringUtil::Format(
17921801
"Statement of type %d not supported yet...\n", stmt->type));

0 commit comments

Comments
 (0)