[FEAT MERGE]4_1_sql_feature

Co-authored-by: leslieyuchen <leslieyuchen@gmail.com>
Co-authored-by: Charles0429 <xiezhenjiang@gmail.com>
Co-authored-by: raywill <hustos@gmail.com>
This commit is contained in:
obdev
2023-01-28 16:01:26 +08:00
committed by ob-robot
parent 3080f2b66f
commit 2d19a9d8f5
846 changed files with 161957 additions and 116661 deletions

View File

@ -17,6 +17,7 @@
#include "sql/optimizer/ob_log_plan_factory.h"
#include "sql/optimizer/ob_log_values.h"
#include "sql/code_generator/ob_code_generator.h"
#include "sql/monitor/ob_sql_plan.h"
using namespace oceanbase;
using namespace sql;
@ -66,82 +67,38 @@ int ObExplainLogPlan::generate_raw_plan()
set_plan_root(top);
top->mark_is_plan_root();
child_plan->get_optimizer_context().set_batch_size(batch_size);
char *buf = NULL;
values_op = static_cast<ObLogValues*>(top);
if (OB_ISNULL(
buf = static_cast<char*>(get_allocator().alloc(ObLogValues::MAX_EXPLAIN_BUFFER_SIZE)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("Failed to allocate buffer", "buffer size",
static_cast<int64_t>(ObLogValues::MAX_EXPLAIN_BUFFER_SIZE), K(ret));
values_op->set_explain_plan(child_plan);
ObSqlPlan sql_plan(get_allocator());
sql_plan.set_session_info(optimizer_context_.get_session_info());
ObExplainLogPlan *explain_plan = static_cast<ObExplainLogPlan*>(child_plan);
ObSEArray<common::ObString, 64> plan_strs;
if (OB_FAIL(sql_plan.store_sql_plan_for_explain(child_plan,
explain_stmt->get_explain_type(),
explain_stmt->get_display_opt(),
plan_strs))) {
LOG_WARN("failed to store sql plan", K(ret));
} else {
int64_t pos = 0;
values_op->set_explain_plan(child_plan);
if (EXPLAIN_FORMAT_JSON == explain_stmt->get_explain_type()) {
char *pre_buf = NULL;
if (NULL
== (pre_buf = static_cast<char*>(get_allocator().alloc(
ObLogValues::MAX_EXPLAIN_BUFFER_SIZE)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("Failed to allocate buffer", "buffer size",
static_cast<int64_t>(ObLogValues::MAX_EXPLAIN_BUFFER_SIZE), K(ret));
} else if (NULL == child_plan->get_plan_root()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid argument", K(ret));
} else {
Value *child_value = NULL;
if (OB_FAIL(
child_plan->get_plan_root()->to_json(pre_buf, ObLogValues::MAX_EXPLAIN_BUFFER_SIZE,
pos, child_value))) {
LOG_WARN("to_json fails", K(ret), K(child_value));
} else {
Tidy tidy(child_value);
pos = tidy.to_string(buf, ObLogValues::MAX_EXPLAIN_BUFFER_SIZE);
if (pos < ObLogValues::MAX_EXPLAIN_BUFFER_SIZE - 2) {
buf[pos + 1] = '\0';
} else {
buf[ObLogValues::MAX_EXPLAIN_BUFFER_SIZE - 1] = '\0';
}
}
}
if (NULL != pre_buf) {
get_allocator().free(pre_buf);
pre_buf = NULL;
}
} else {
pos = child_plan->to_string(buf, ObLogValues::MAX_EXPLAIN_BUFFER_SIZE,
explain_stmt->get_explain_type(),
explain_stmt->get_display_opt());
if (pos < ObLogValues::MAX_EXPLAIN_BUFFER_SIZE - 2) {
buf[pos + 1] = '\0';
} else {
buf[ObLogValues::MAX_EXPLAIN_BUFFER_SIZE - 1] = '\0';
}
}
if (OB_SUCC(ret)) {
//For explain stmt, we can do pack at the stage of expr alloc,
//But we need to use the FALSE flag to tell the driver
//to use normal encoding instead of memcopy
optimizer_context_.set_packed(false);
ObObj obj;
obj.set_varchar(ObString::make_string(buf));
ObNewRow row;
row.cells_ = &obj;
row.count_ = 1;
if (OB_FAIL(values_op->add_row(row))) {
LOG_WARN("failed to add row", K(ret));
} else {
// set values_op operator id && set max operator id for LogPlan
values_op->set_op_id(0);
set_max_op_id(1);
}
}
if (NULL != buf) {
get_allocator().free(buf);
buf = NULL;
//For explain stmt, we can do pack at the stage of expr alloc,
//But we need to use the FALSE flag to tell the driver
//to use normal encoding instead of memcopy
optimizer_context_.set_packed(false);
}
for (int64_t i = 0; OB_SUCC(ret) && i < plan_strs.count(); ++i) {
ObObj obj;
obj.set_varchar(plan_strs.at(i));
ObNewRow row;
row.cells_ = &obj;
row.count_ = 1;
if (OB_FAIL(values_op->add_row(row))) {
LOG_WARN("failed to add row", K(ret));
}
}
if (OB_SUCC(ret)) {
// set values_op operator id && set max operator id for LogPlan
values_op->set_op_id(0);
set_max_op_id(1);
}
}
get_optimizer_context().get_all_exprs().reuse();
if (OB_FAIL(ret)) {