Fix printer bugs

This commit is contained in:
xianyu-w
2023-06-22 03:42:42 +00:00
committed by ob-robot
parent 7851991b8b
commit 1858194fc4
5 changed files with 201 additions and 136 deletions

View File

@ -95,70 +95,30 @@ int ObCreateTableExecutor::prepare_stmt(ObCreateTableStmt &stmt,
return ret;
}
//准备查询插入的脚本
int ObCreateTableExecutor::prepare_ins_arg(ObCreateTableStmt &stmt,
const ObSQLSessionInfo *my_session,
ObSchemaGetterGuard *schema_guard,
const ParamStore *param_store,
ObSqlString &ins_sql) //out, 最终的查询插入语句
int ObCreateTableExecutor::ObInsSQLPrinter::inner_print(char *buf, int64_t buf_len, int64_t &res_len)
{
int ret = OB_SUCCESS;
ObArenaAllocator allocator("CreateTableExec");
char *buf = static_cast<char*>(allocator.alloc(OB_MAX_SQL_LENGTH));
int64_t buf_len = OB_MAX_SQL_LENGTH;
const char sep_char = lib::is_oracle_mode()? '"': '`';
const ObSelectStmt *select_stmt = NULL;
int64_t pos1 = 0;
bool is_set_subquery = false;
bool is_oracle_mode = lib::is_oracle_mode();
bool no_osg_hint = false;
bool online_sys_var = false;
const ObString &db_name = stmt.get_database_name();
const ObString &tab_name = stmt.get_table_name();
const char sep_char = is_oracle_mode? '"': '`';
ObSelectStmt *select_stmt = stmt.get_sub_select();
ObObjPrintParams obj_print_params(select_stmt->get_query_ctx()->get_timezone_info());
obj_print_params.print_origin_stmt_ = true;
ObSelectStmtPrinter select_stmt_printer(buf, buf_len, &pos1, select_stmt,
schema_guard,
obj_print_params,
param_store,
true);
select_stmt_printer.set_is_first_stmt_for_hint(true); // need print global hint
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("allocate memory failed");
} else if (OB_ISNULL(select_stmt) || OB_ISNULL(select_stmt->get_query_ctx()) || OB_ISNULL(my_session)) {
if (OB_ISNULL(stmt_) || OB_ISNULL(select_stmt= stmt_->get_sub_select())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("select stmt should not be null", K(ret));
} else {
//get hint
no_osg_hint = select_stmt->get_query_ctx()->get_global_hint().has_no_gather_opt_stat_hint();
//get system variable
ObObj online_sys_var_obj;
if (OB_FAIL(OB_FAIL(my_session->get_sys_variable(SYS_VAR__OPTIMIZER_GATHER_STATS_ON_LOAD, online_sys_var_obj)))) {
LOG_WARN("fail to get sys var", K(ret));
} else {
online_sys_var = online_sys_var_obj.get_bool();
LOG_DEBUG("online opt stat gather", K(online_sys_var), K(no_osg_hint));
}
}
if (OB_FAIL(ret)) {
LOG_WARN("null stmt", K(ret));
} else if (OB_FAIL(databuff_printf(buf, buf_len, pos1,
(!no_osg_hint && online_sys_var)
? "insert /*+GATHER_OPTIMIZER_STATISTICS*/ into %c%.*s%c.%c%.*s%c"
: "insert /*+NO_GATHER_OPTIMIZER_STATISTICS*/ into %c%.*s%c.%c%.*s%c",
sep_char,
db_name.length(),
db_name.ptr(),
sep_char,
sep_char,
tab_name.length(),
tab_name.ptr(),
sep_char))) {
LOG_WARN("fail to print insert into string", K(ret), K(db_name), K(tab_name));
do_osg_
? "insert /*+GATHER_OPTIMIZER_STATISTICS*/ into %c%.*s%c.%c%.*s%c"
: "insert /*+NO_GATHER_OPTIMIZER_STATISTICS*/ into %c%.*s%c.%c%.*s%c",
sep_char,
stmt_->get_database_name().length(),
stmt_->get_database_name().ptr(),
sep_char,
sep_char,
stmt_->get_table_name().length(),
stmt_->get_table_name().ptr(),
sep_char))) {
LOG_WARN("fail to print insert into string", K(ret));
} else if (lib::is_oracle_mode()) {
ObTableSchema &table_schema = stmt.get_create_table_arg().schema_;
const ObTableSchema &table_schema = stmt_->get_create_table_arg().schema_;
int64_t used_column_count = 0;
for (int64_t i = 0; OB_SUCC(ret) && i < table_schema.get_column_count(); ++i) {
const ObColumnSchemaV2 *column_schema = table_schema.get_column_schema_by_idx(i);
@ -197,11 +157,67 @@ int ObCreateTableExecutor::prepare_ins_arg(ObCreateTableStmt &stmt,
}
if (OB_SUCC(ret)) {
ObSelectStmtPrinter select_stmt_printer(buf, buf_len, &pos1, select_stmt,
schema_guard_,
print_params_,
param_store_,
true);
select_stmt_printer.set_is_first_stmt_for_hint(true); // need print global hint
if (OB_FAIL(databuff_printf(buf, buf_len, pos1, ") "))) {
LOG_WARN("fail to append ')'", K(ret));
} else if (OB_FAIL(select_stmt_printer.do_print())) {
LOG_WARN("fail to print select stmt", K(ret));
} else if (OB_FAIL(ins_sql.append(buf, pos1))){
} else {
res_len = pos1;
}
}
return ret;
}
//准备查询插入的脚本
int ObCreateTableExecutor::prepare_ins_arg(ObCreateTableStmt &stmt,
const ObSQLSessionInfo *my_session,
ObSchemaGetterGuard *schema_guard,
const ParamStore *param_store,
ObSqlString &ins_sql) //out, 最终的查询插入语句
{
int ret = OB_SUCCESS;
ObArenaAllocator allocator("CreateTableExec");
char *buf = static_cast<char*>(allocator.alloc(OB_MAX_SQL_LENGTH));
int64_t buf_len = OB_MAX_SQL_LENGTH;
int64_t pos1 = 0;
bool is_oracle_mode = lib::is_oracle_mode();
bool no_osg_hint = false;
bool online_sys_var = false;
ObSelectStmt *select_stmt = stmt.get_sub_select();
ObObjPrintParams obj_print_params(select_stmt->get_query_ctx()->get_timezone_info());
obj_print_params.print_origin_stmt_ = true;
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("allocate memory failed");
} else if (OB_ISNULL(select_stmt) || OB_ISNULL(select_stmt->get_query_ctx()) || OB_ISNULL(my_session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("select stmt should not be null", K(ret));
} else {
//get hint
no_osg_hint = select_stmt->get_query_ctx()->get_global_hint().has_no_gather_opt_stat_hint();
//get system variable
ObObj online_sys_var_obj;
if (OB_FAIL(OB_FAIL(my_session->get_sys_variable(SYS_VAR__OPTIMIZER_GATHER_STATS_ON_LOAD, online_sys_var_obj)))) {
LOG_WARN("fail to get sys var", K(ret));
} else {
online_sys_var = online_sys_var_obj.get_bool();
LOG_DEBUG("online opt stat gather", K(online_sys_var), K(no_osg_hint));
}
}
if (OB_SUCC(ret)) {
ObInsSQLPrinter sql_printer(&stmt, schema_guard, obj_print_params, param_store, !no_osg_hint && online_sys_var);
ObString sql;
if (OB_FAIL(sql_printer.do_print(allocator, sql))) {
LOG_WARN("failed to print", K(ret));
} else if (OB_FAIL(ins_sql.append(sql))){
LOG_WARN("fail to append insert into string", K(ret));
}
}

View File

@ -53,6 +53,28 @@ class ObTableStmt;
class ObCreateTableExecutor
{
class ObInsSQLPrinter : public ObISqlPrinter
{
public:
ObInsSQLPrinter(ObCreateTableStmt *stmt,
ObSchemaGetterGuard *schema_guard,
ObObjPrintParams print_params,
const ParamStore *param_store,
bool do_osg) :
stmt_(stmt),
schema_guard_(schema_guard),
print_params_(print_params),
param_store_(param_store),
do_osg_(do_osg)
{}
virtual int inner_print(char *buf, int64_t buf_len, int64_t &res_len) override;
private:
ObCreateTableStmt *stmt_;
ObSchemaGetterGuard *schema_guard_;
ObObjPrintParams print_params_;
const ParamStore *param_store_;
bool do_osg_;
};
public:
ObCreateTableExecutor();
virtual ~ObCreateTableExecutor();

View File

@ -2026,6 +2026,7 @@ int ObSQLUtils::reconstruct_sql(ObIAllocator &allocator, const ObStmt *stmt, ObS
const ParamStore *param_store)
{
int ret = OB_SUCCESS;
ObSqlPrinter sql_printer(stmt, schema_guard, print_params, param_store);
if (OB_ISNULL(stmt)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("stmt is NULL", K(stmt), K(ret));
@ -2035,78 +2036,86 @@ int ObSQLUtils::reconstruct_sql(ObIAllocator &allocator, const ObStmt *stmt, ObS
} else if (!stmt->is_dml_stmt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Unexpected stmt type", K(stmt->get_stmt_type()), K(stmt->get_query_ctx()->get_sql_stmt()), K(ret));
} else {
//First try 64K buf on the stack, if it fails, then try 128K.
//If it still fails, allocate 256K from the heap. If it continues to fail, expand twice each time.
SMART_VAR(char[OB_MAX_SQL_LENGTH], buf) {
if (OB_FAIL(print_sql(allocator,
buf,
sizeof(buf),
stmt,
sql,
schema_guard,
print_params,
param_store))) {
LOG_WARN("failed to print sql", K(sizeof(buf)), K(ret));
}
}
if (OB_SIZE_OVERFLOW == ret) {
ret = OB_SUCCESS;
SMART_VAR(char[OB_MAX_SQL_LENGTH * 2], buf) {
if (OB_FAIL(print_sql(allocator,
buf,
sizeof(buf),
stmt,
sql,
schema_guard,
print_params,
param_store))) {
LOG_WARN("failed to print sql", K(sizeof(buf)), K(ret));
}
}
}
if (OB_SIZE_OVERFLOW == ret) {
bool is_succ = false;
ret = OB_SUCCESS;
for (int64_t i = 4; OB_SUCC(ret) && !is_succ && i <= 1024; i = i * 2) {
ObArenaAllocator alloc;
const int64_t length = OB_MAX_SQL_LENGTH * i;
char *buf = NULL;
if (OB_ISNULL(buf = static_cast<char*>(alloc.alloc(length)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to alloc memory for set sql", K(ret), K(length));
} else if (OB_FAIL(print_sql(allocator,
buf,
length,
stmt,
sql,
schema_guard,
print_params,
param_store))) {
LOG_WARN("failed to print sql", K(length), K(i), K(ret));
}
if (OB_SUCC(ret)) {
is_succ = true;
} else if (OB_SIZE_OVERFLOW == ret) {
ret = OB_SUCCESS;
}
}
}
} else if (OB_FAIL(sql_printer.do_print(allocator, sql))) {
LOG_WARN("failed to print sql", K(ret));
}
return ret;
}
int ObSQLUtils::print_sql(ObIAllocator &allocator,
char *buf,
int ObISqlPrinter::do_print(ObIAllocator &allocator, ObString &result)
{
int ret = OB_SUCCESS;
//First try 64K buf on the stack, if it fails, then try 128K.
//If it still fails, allocate 256K from the heap. If it continues to fail, expand twice each time.
int64_t res_len = 0;
char *final_buf = NULL;
SMART_VAR(char[OB_MAX_SQL_LENGTH], buf) {
MEMSET(buf, 0, sizeof(buf));
if (OB_FAIL(inner_print(buf, sizeof(buf), res_len))) {
LOG_WARN("failed to print", K(sizeof(buf)), K(ret));
} else {
final_buf = buf;
}
}
if (OB_SIZE_OVERFLOW == ret) {
ret = OB_SUCCESS;
SMART_VAR(char[OB_MAX_SQL_LENGTH * 2], buf) {
MEMSET(buf, 0, sizeof(buf));
if (OB_FAIL(inner_print(buf, sizeof(buf), res_len))) {
LOG_WARN("failed to print", K(sizeof(buf)), K(ret));
} else {
final_buf = buf;
}
}
}
if (OB_SIZE_OVERFLOW == ret) {
bool is_succ = false;
ret = OB_SUCCESS;
for (int64_t i = 4; OB_SUCC(ret) && !is_succ && i <= 1024; i = i * 2) {
ObArenaAllocator alloc;
const int64_t length = OB_MAX_SQL_LENGTH * i;
char *buf = NULL;
if (OB_ISNULL(buf = static_cast<char*>(alloc.alloc(length)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to alloc memory for sql", K(ret), K(length));
} else if (FALSE_IT(MEMSET(buf, 0, length))) {
} else if (OB_FAIL(inner_print(buf, length, res_len))) {
LOG_WARN("failed to print", K(sizeof(buf)), K(ret));
}
if (OB_SUCC(ret)) {
is_succ = true;
final_buf = buf;
} else if (OB_SIZE_OVERFLOW == ret) {
ret = OB_SUCCESS;
}
}
}
if (OB_SUCC(ret) && OB_FAIL(ob_write_string(allocator, ObString(res_len, final_buf), result))) {
LOG_WARN("fail to deep copy delete stmt string", K(ret));
}
return ret;
}
int ObSqlPrinter::inner_print(char *buf, int64_t buf_len, int64_t &res_len)
{
return ObSQLUtils::print_sql(buf,
buf_len,
res_len,
stmt_,
schema_guard_,
print_params_,
param_store_);
}
int ObSQLUtils::print_sql(char *buf,
int64_t buf_len,
int64_t &res_len,
const ObStmt *stmt,
ObString &sql,
ObSchemaGetterGuard *schema_guard,
ObObjPrintParams print_params,
const ParamStore *param_store)
{
int ret = OB_SUCCESS;
MEMSET(buf, 0, buf_len);
int64_t pos = 0;
const ObDMLStmt *reconstruct_stmt = NULL;
if (OB_ISNULL(stmt)) {
@ -2141,8 +2150,6 @@ int ObSQLUtils::print_sql(ObIAllocator &allocator,
printer.enable_print_temp_table_as_cte();
if (OB_FAIL(printer.do_print())) {
LOG_WARN("fail to print select stmt", K(ret));
} else if (OB_FAIL(ob_write_string(allocator, ObString(pos, buf), sql))) {
LOG_WARN("fail to deep copy select stmt string", K(ret));
} else { /*do nothing*/ }
}
break;
@ -2158,8 +2165,6 @@ int ObSQLUtils::print_sql(ObIAllocator &allocator,
printer.set_is_first_stmt_for_hint(true);
if (OB_FAIL(printer.do_print())) {
LOG_WARN("fail to print insert stmt", K(ret));
} else if (OB_FAIL(ob_write_string(allocator, ObString(pos, buf), sql))) {
LOG_WARN("fail to deep copy insert stmt string", K(ret));
} else { /*do nothing*/ }
}
break;
@ -2176,8 +2181,6 @@ int ObSQLUtils::print_sql(ObIAllocator &allocator,
printer.set_is_first_stmt_for_hint(true);
if (OB_FAIL(printer.do_print())) {
LOG_WARN("fail to print insert stmt", K(ret));
} else if (OB_FAIL(ob_write_string(allocator, ObString(pos, buf), sql))) {
LOG_WARN("fail to deep copy insert stmt string", K(ret));
} else { /*do nothing*/ }
}
break;
@ -2193,8 +2196,6 @@ int ObSQLUtils::print_sql(ObIAllocator &allocator,
printer.set_is_first_stmt_for_hint(true);
if (OB_FAIL(printer.do_print())) {
LOG_WARN("fail to print delete stmt", K(ret));
} else if (OB_FAIL(ob_write_string(allocator, ObString(pos, buf), sql))) {
LOG_WARN("fail to deep copy delete stmt string", K(ret));
} else { /*do nothing*/ }
}
break;
@ -2210,8 +2211,6 @@ int ObSQLUtils::print_sql(ObIAllocator &allocator,
printer.set_is_first_stmt_for_hint(true);
if (OB_FAIL(printer.do_print())) {
LOG_WARN("fail to print update stmt", K(ret));
} else if (OB_FAIL(ob_write_string(allocator, ObString(pos, buf), sql))) {
LOG_WARN("fail to deep copy update stmt string", K(ret));
} else { /*do nothing*/ }
}
break;
@ -2227,8 +2226,6 @@ int ObSQLUtils::print_sql(ObIAllocator &allocator,
printer.set_is_first_stmt_for_hint(true);
if (OB_FAIL(printer.do_print())) {
LOG_WARN("failed to print merge stmt", K(ret));
} else if (OB_FAIL(ob_write_string(allocator, ObString(pos, buf), sql))) {
LOG_WARN("failed to deep copy merge stmt string", K(ret));
}
}
break;
@ -2238,6 +2235,7 @@ int ObSQLUtils::print_sql(ObIAllocator &allocator,
break;
}
}
res_len = pos;
return ret;
}

View File

@ -113,6 +113,36 @@ private:
int ret_;
};
class ObISqlPrinter
{
protected:
virtual int inner_print(char *buf, int64_t buf_len, int64_t &pos) = 0;
public:
virtual int do_print(ObIAllocator &allocator, ObString &result);
virtual ~ObISqlPrinter() = default;
};
class ObSqlPrinter : public ObISqlPrinter
{
public:
ObSqlPrinter(const ObStmt *stmt,
ObSchemaGetterGuard *schema_guard,
ObObjPrintParams print_params,
const ParamStore *param_store) :
stmt_(stmt),
schema_guard_(schema_guard),
print_params_(print_params),
param_store_(param_store)
{}
virtual int inner_print(char *buf, int64_t buf_len, int64_t &res_len) override;
protected:
const ObStmt *stmt_;
ObSchemaGetterGuard *schema_guard_;
ObObjPrintParams print_params_;
const ParamStore *param_store_;
};
class ObSQLUtils
{
public:
@ -368,11 +398,10 @@ public:
ObSchemaGetterGuard *schema_guard,
ObObjPrintParams print_params = ObObjPrintParams(),
const ParamStore *param_store = NULL);
static int print_sql(ObIAllocator &allocator,
char *buf,
static int print_sql(char *buf,
int64_t buf_len,
int64_t &pos,
const ObStmt *stmt,
ObString &sql,
ObSchemaGetterGuard *schema_guard,
ObObjPrintParams print_params,
const ParamStore *param_store = NULL);

View File

@ -4033,7 +4033,7 @@ int ObRawExprPrinter::print_cast_type(ObRawExpr *expr)
if (lib::is_oracle_mode()) {
DATA_PRINTF("char(%d %s)", len, get_length_semantics_str(length_semantics));
} else {
if (len > 0) {
if (len >= 0) {
DATA_PRINTF("char(%d) charset %s", len, ObCharset::charset_name(
static_cast<ObCollationType>(collation)));
} else {