diff --git a/src/pl/sys_package/ob_dbms_stats.cpp b/src/pl/sys_package/ob_dbms_stats.cpp index 1818a81375..1b5c3118fe 100644 --- a/src/pl/sys_package/ob_dbms_stats.cpp +++ b/src/pl/sys_package/ob_dbms_stats.cpp @@ -3326,7 +3326,7 @@ int ObDbmsStats::init_column_stat_params(ObIAllocator &allocator, col_param.is_valid_hist_type_ = ObColumnStatParam::is_valid_histogram_type(col->get_meta_type().get_type()); col_param.need_truncate_str_ = ob_is_string_type(col->get_meta_type().get_type()) && - col->get_data_length() > OPT_STATS_MAX_VALUE_CAHR_LEN; + col->get_data_length() > OPT_STATS_MAX_VALUE_CHAR_LEN; } if (col->is_rowkey_column() && !table_schema.is_heap_table()) { col_param.set_is_index_column(); diff --git a/src/share/stat/ob_dbms_stats_executor.cpp b/src/share/stat/ob_dbms_stats_executor.cpp index ab76e5d2b0..37f0d9e12c 100644 --- a/src/share/stat/ob_dbms_stats_executor.cpp +++ b/src/share/stat/ob_dbms_stats_executor.cpp @@ -338,7 +338,8 @@ int ObDbmsStatsExecutor::set_column_stats(ObExecContext &ctx, } else if (OB_FAIL(mgr.update_column_stat(ctx.get_virtual_table_ctx().schema_guard_, param.table_param_.tenant_id_, column_stats, - true))) { + true, + CREATE_OBJ_PRINT_PARAM(ctx.get_my_session())))) { LOG_WARN("failed to update column stats", K(ret)); } else { LOG_TRACE("end set column stats", K(param), K(*col_stat)); diff --git a/src/share/stat/ob_dbms_stats_utils.cpp b/src/share/stat/ob_dbms_stats_utils.cpp index 31ec498e27..7d7de80911 100644 --- a/src/share/stat/ob_dbms_stats_utils.cpp +++ b/src/share/stat/ob_dbms_stats_utils.cpp @@ -121,7 +121,8 @@ int ObDbmsStatsUtils::batch_write(share::schema::ObSchemaGetterGuard *schema_gua const int64_t current_time, const bool is_index_stat, const bool is_history_stat, - const bool is_online_stat) + const bool is_online_stat, + const ObObjPrintParams &print_params) { int ret = OB_SUCCESS; if (OB_FAIL(ObOptStatManager::get_instance().batch_write(schema_guard, @@ -130,7 +131,8 @@ int ObDbmsStatsUtils::batch_write(share::schema::ObSchemaGetterGuard *schema_gua column_stats, current_time, is_index_stat, - is_history_stat))) { + is_history_stat, + print_params))) { LOG_WARN("failed to batch write stats", K(ret)); //histroy stat is from cache no need free. } else if (!is_history_stat && !is_online_stat) { @@ -296,7 +298,8 @@ int ObDbmsStatsUtils::split_batch_write(sql::ObExecContext &ctx, column_stats, is_index_stat, is_history_stat, - is_online_stat))) { + is_online_stat, + CREATE_OBJ_PRINT_PARAM(ctx.get_my_session())))) { LOG_WARN("failed to split batch write", K(ret)); } else {/*do nothing*/} return ret; @@ -308,7 +311,8 @@ int ObDbmsStatsUtils::split_batch_write(share::schema::ObSchemaGetterGuard *sche ObIArray &column_stats, const bool is_index_stat/*default false*/, const bool is_history_stat/*default false*/, - const bool is_online_stat /*default false*/) + const bool is_online_stat /*default false*/, + const ObObjPrintParams &print_params) { int ret = OB_SUCCESS; int64_t idx_tab_stat = 0; @@ -361,7 +365,8 @@ int ObDbmsStatsUtils::split_batch_write(share::schema::ObSchemaGetterGuard *sche current_time, is_index_stat, is_history_stat, - is_online_stat))) { + is_online_stat, + print_params))) { LOG_WARN("failed to batch write stats", K(ret), K(idx_tab_stat), K(idx_col_stat)); } else {/*do nothing*/} } diff --git a/src/share/stat/ob_dbms_stats_utils.h b/src/share/stat/ob_dbms_stats_utils.h index 3721e5ef57..25f30b6aae 100644 --- a/src/share/stat/ob_dbms_stats_utils.h +++ b/src/share/stat/ob_dbms_stats_utils.h @@ -51,7 +51,8 @@ public: ObIArray &column_stats, const bool is_index_stat = false, const bool is_history_stat = false, - const bool is_online_stat = false); + const bool is_online_stat = false, + const ObObjPrintParams &print_params = ObObjPrintParams()); static int batch_write_history_stats(sql::ObExecContext &ctx, ObIArray &history_tab_handles, @@ -107,7 +108,8 @@ private: const int64_t current_time, const bool is_index_stat, const bool is_history_stat, - const bool is_online_stat = false); + const bool is_online_stat = false, + const ObObjPrintParams &print_params = ObObjPrintParams()); }; diff --git a/src/share/stat/ob_opt_column_stat.cpp b/src/share/stat/ob_opt_column_stat.cpp index 12f5d667a4..e5134ab598 100644 --- a/src/share/stat/ob_opt_column_stat.cpp +++ b/src/share/stat/ob_opt_column_stat.cpp @@ -427,7 +427,7 @@ int ObOptColumnStat::merge_column_stat(const ObOptColumnStat &other) } // deep copy max/min, using inner_allocator -int ObOptColumnStat::merge_obj(ObObj &obj) +int ObOptColumnStat::merge_obj(const ObObj &obj) { int ret = OB_SUCCESS; //calc avg_len: avg_len should update before num_null, null_not_null -- since these are use in merge_avg_len. diff --git a/src/share/stat/ob_opt_column_stat.h b/src/share/stat/ob_opt_column_stat.h index 176eec103c..de2596e7d5 100644 --- a/src/share/stat/ob_opt_column_stat.h +++ b/src/share/stat/ob_opt_column_stat.h @@ -306,7 +306,7 @@ public: int merge_obj(common::ObObj &obj, ObIAllocator &max_alloc, ObIAllocator &min_alloc); - int merge_obj(ObObj &obj); + int merge_obj(const ObObj &obj); // normal copy //shallow copy void merge_max_val(const common::ObObj &max_val) { diff --git a/src/share/stat/ob_opt_stat_manager.cpp b/src/share/stat/ob_opt_stat_manager.cpp index 4ca34f991d..746d597142 100644 --- a/src/share/stat/ob_opt_stat_manager.cpp +++ b/src/share/stat/ob_opt_stat_manager.cpp @@ -222,7 +222,8 @@ int ObOptStatManager::get_table_stat(const uint64_t tenant_id, int ObOptStatManager::update_column_stat(share::schema::ObSchemaGetterGuard *schema_guard, const uint64_t tenant_id, const ObIArray &column_stats, - bool only_update_col_stat /*default false*/) + bool only_update_col_stat /*default false*/, + const ObObjPrintParams &print_params) { int ret = OB_SUCCESS; int64_t current_time = ObTimeUtility::current_time(); @@ -233,7 +234,9 @@ int ObOptStatManager::update_column_stat(share::schema::ObSchemaGetterGuard *sch tenant_id, column_stats, current_time, - only_update_col_stat))) { + only_update_col_stat, + false, + print_params))) { LOG_WARN("failed to update column stat.", K(ret)); } else { /*do nothing*/ } return ret; @@ -354,7 +357,8 @@ int ObOptStatManager::batch_write(share::schema::ObSchemaGetterGuard *schema_gua ObIArray &column_stats, const int64_t current_time, const bool is_index_stat, - const bool is_history_stat) + const bool is_history_stat, + const ObObjPrintParams &print_params) { int ret = OB_SUCCESS; if (OB_UNLIKELY(!inited_)) { @@ -374,7 +378,8 @@ int ObOptStatManager::batch_write(share::schema::ObSchemaGetterGuard *schema_gua column_stats, current_time, false, - is_history_stat))) { + is_history_stat, + print_params))) { LOG_WARN("failed to update coumn stats", K(ret)); } return ret; diff --git a/src/share/stat/ob_opt_stat_manager.h b/src/share/stat/ob_opt_stat_manager.h index 35358559da..f98aae1f7f 100644 --- a/src/share/stat/ob_opt_stat_manager.h +++ b/src/share/stat/ob_opt_stat_manager.h @@ -133,7 +133,8 @@ public: virtual int update_column_stat(share::schema::ObSchemaGetterGuard *schema_guard, const uint64_t tenant_id, const common::ObIArray &column_stats, - bool only_update_col_stat = false); + bool only_update_col_stat = false, + const ObObjPrintParams &print_params = ObObjPrintParams()); int delete_table_stat(const uint64_t tenant_id, const uint64_t ref_id, @@ -168,7 +169,8 @@ public: ObIArray &column_stats, const int64_t current_time, const bool is_index_stat, - const bool is_history_stat); + const bool is_history_stat, + const ObObjPrintParams &print_params); /** @brief 外部获取行统计信息的接口 */ virtual int get_table_stat(const uint64_t tenant_id, diff --git a/src/share/stat/ob_opt_stat_sql_service.cpp b/src/share/stat/ob_opt_stat_sql_service.cpp index 96d164d786..8f628771c3 100644 --- a/src/share/stat/ob_opt_stat_sql_service.cpp +++ b/src/share/stat/ob_opt_stat_sql_service.cpp @@ -421,7 +421,8 @@ int ObOptStatSqlService::update_column_stat(share::schema::ObSchemaGetterGuard * const ObIArray &column_stats, const int64_t current_time, bool only_update_col_stat /*default false*/, - bool is_history_stat/*default false*/) + bool is_history_stat/*default false*/, + const ObObjPrintParams &print_params) { int ret = OB_SUCCESS; ObMySQLTransaction trans; @@ -444,7 +445,8 @@ int ObOptStatSqlService::update_column_stat(share::schema::ObSchemaGetterGuard * column_stats, current_time, is_history_stat, - column_stats_sql))) { + column_stats_sql, + print_params))) { LOG_WARN("failed to construct column stat sql", K(ret)); // construct histogram delete column } else if (!only_update_col_stat && !is_history_stat && @@ -459,7 +461,8 @@ int ObOptStatSqlService::update_column_stat(share::schema::ObSchemaGetterGuard * current_time, is_history_stat, insert_histogram, - need_histogram))) { + need_histogram, + print_params))) { LOG_WARN("failed to construct histogram insert sql", K(ret)); } else if (OB_FAIL(trans.start(mysql_proxy_, exec_tenant_id))) { LOG_WARN("fail to start transaction", K(ret)); @@ -491,7 +494,8 @@ int ObOptStatSqlService::construct_column_stat_sql(share::schema::ObSchemaGetter const ObIArray &column_stats, const int64_t current_time, bool is_history_stat, - ObSqlString &column_stats_sql) + ObSqlString &column_stats_sql, + const ObObjPrintParams &print_params) { int ret = OB_SUCCESS; ObSqlString tmp; @@ -515,7 +519,7 @@ int ObOptStatSqlService::construct_column_stat_sql(share::schema::ObSchemaGetter LOG_WARN("failed to append fmt sql", K(ret)); } else if (OB_FAIL(get_column_stat_history_sql(tenant_id, allocator, *column_stats.at(i), current_time, - min_meta, max_meta, tmp))) { + min_meta, max_meta, tmp, print_params))) { LOG_WARN("failed to get column stat history sql", K(ret)); } else if (OB_FAIL(column_stats_sql.append_fmt("(%s)%s", tmp.ptr(), (i == column_stats.count() - 1 ? ";" : ",")))) { @@ -525,7 +529,7 @@ int ObOptStatSqlService::construct_column_stat_sql(share::schema::ObSchemaGetter LOG_WARN("failed to append sql", K(ret)); } else if (OB_FAIL(get_column_stat_sql(tenant_id, allocator, *column_stats.at(i), current_time, - min_meta, max_meta, tmp))) { + min_meta, max_meta, tmp, print_params))) { LOG_WARN("failed to get column stat", K(ret)); } else if (OB_FAIL(column_stats_sql.append_fmt("(%s)%s", tmp.ptr(), (i == column_stats.count() - 1 ? ";" : ",")))) { @@ -576,7 +580,8 @@ int ObOptStatSqlService::construct_histogram_insert_sql(share::schema::ObSchemaG const int64_t current_time, bool is_history_stat, ObSqlString &insert_histogram_sql, - bool &need_histogram) + bool &need_histogram, + const ObObjPrintParams &print_params) { int ret = OB_SUCCESS; ObSqlString tmp; @@ -606,7 +611,8 @@ int ObOptStatSqlService::construct_histogram_insert_sql(share::schema::ObSchemaG hist.get(j), current_time, endpoint_meta, - tmp))) { + tmp, + print_params))) { LOG_WARN("failed to get histogram stat history sql", K(ret)); } else if (OB_FAIL(insert_histogram_sql.append_fmt("%s (%s)", (!need_histogram ? "" : ","), tmp.ptr()))) { LOG_WARN("failed to append sql", K(ret)); @@ -616,7 +622,7 @@ int ObOptStatSqlService::construct_histogram_insert_sql(share::schema::ObSchemaG } else if (!need_histogram && OB_FAIL(insert_histogram_sql.append(INSERT_HISTOGRAM_STAT_SQL))) { LOG_WARN("failed to append sql", K(ret)); } else if (OB_FAIL(get_histogram_stat_sql(tenant_id, *column_stats.at(i), - allocator, hist.get(j), endpoint_meta, tmp))) { + allocator, hist.get(j), endpoint_meta, tmp, print_params))) { LOG_WARN("failed to get histogram sql", K(ret)); } else if (OB_FAIL(insert_histogram_sql.append_fmt("%s (%s)", (!need_histogram ? "" : ","), tmp.ptr()))) { LOG_WARN("failed to append sql", K(ret)); @@ -865,7 +871,8 @@ int ObOptStatSqlService::get_column_stat_sql(const uint64_t tenant_id, const int64_t current_time, ObObjMeta min_meta, ObObjMeta max_meta, - ObSqlString &sql_string) + ObSqlString &sql_string, + const ObObjPrintParams &print_params) { int ret = OB_SUCCESS; share::ObDMLSqlSplicer dml_splicer; @@ -878,8 +885,8 @@ int ObOptStatSqlService::get_column_stat_sql(const uint64_t tenant_id, char *llc_hex_buf = NULL; int64_t llc_comp_size = 0; int64_t llc_hex_size = 0; - if (OB_FAIL(get_valid_obj_str(stat.get_min_value(), min_meta, allocator, min_str)) || - OB_FAIL(get_valid_obj_str(stat.get_max_value(), max_meta, allocator, max_str))) { + if (OB_FAIL(get_valid_obj_str(stat.get_min_value(), min_meta, allocator, min_str, print_params)) || + OB_FAIL(get_valid_obj_str(stat.get_max_value(), max_meta, allocator, max_str, print_params))) { LOG_WARN("failed to get valid obj str", K(ret)); } else if (OB_FAIL(get_obj_binary_hex_str(stat.get_min_value(), allocator, b_min_str)) || OB_FAIL(get_obj_binary_hex_str(stat.get_max_value(), allocator, b_max_str))) { @@ -939,7 +946,8 @@ int ObOptStatSqlService::get_column_stat_history_sql(const uint64_t tenant_id, const int64_t saving_time, ObObjMeta min_meta, ObObjMeta max_meta, - ObSqlString &sql_string) + ObSqlString &sql_string, + const ObObjPrintParams &print_params) { int ret = OB_SUCCESS; share::ObDMLSqlSplicer dml_splicer; @@ -952,8 +960,8 @@ int ObOptStatSqlService::get_column_stat_history_sql(const uint64_t tenant_id, char *llc_hex_buf = NULL; int64_t llc_comp_size = 0; int64_t llc_hex_size = 0; - if (OB_FAIL(ObOptStatSqlService::get_valid_obj_str(stat.get_min_value(), min_meta, allocator, min_str)) || - OB_FAIL(ObOptStatSqlService::get_valid_obj_str(stat.get_max_value(), max_meta, allocator, max_str)) || + if (OB_FAIL(ObOptStatSqlService::get_valid_obj_str(stat.get_min_value(), min_meta, allocator, min_str, print_params)) || + OB_FAIL(ObOptStatSqlService::get_valid_obj_str(stat.get_max_value(), max_meta, allocator, max_str, print_params)) || OB_FAIL(ObOptStatSqlService::get_obj_binary_hex_str(stat.get_min_value(), allocator, b_min_str)) || OB_FAIL(ObOptStatSqlService::get_obj_binary_hex_str(stat.get_max_value(), allocator, b_max_str))) { LOG_WARN("failed to convert obj to str", K(ret)); @@ -1011,7 +1019,8 @@ int ObOptStatSqlService::get_histogram_stat_sql(const uint64_t tenant_id, ObIAllocator &allocator, ObHistBucket &bucket, ObObjMeta endpoint_meta, - ObSqlString &sql_string) + ObSqlString &sql_string, + const ObObjPrintParams &print_params) { int ret = OB_SUCCESS; ObString endpoint_value; @@ -1023,7 +1032,8 @@ int ObOptStatSqlService::get_histogram_stat_sql(const uint64_t tenant_id, if (OB_FAIL(get_valid_obj_str(bucket.endpoint_value_, endpoint_meta, allocator, - endpoint_value))) { + endpoint_value, + print_params))) { LOG_WARN("failed to get valid obj str", K(ret)); } else if (OB_FAIL(get_obj_binary_hex_str(bucket.endpoint_value_, allocator, b_endpoint_value))) { LOG_WARN("failed to convert obj to binary string", K(ret)); @@ -1050,7 +1060,8 @@ int ObOptStatSqlService::get_histogram_stat_history_sql(const uint64_t tenant_id const ObHistBucket &bucket, const int64_t saving_time, ObObjMeta endpoint_meta, - ObSqlString &sql_string) + ObSqlString &sql_string, + const ObObjPrintParams &print_params) { int ret = OB_SUCCESS; ObString endpoint_value; @@ -1062,7 +1073,8 @@ int ObOptStatSqlService::get_histogram_stat_history_sql(const uint64_t tenant_id if (OB_FAIL(ObOptStatSqlService::get_valid_obj_str(bucket.endpoint_value_, endpoint_meta, allocator, - endpoint_value))) { + endpoint_value, + print_params))) { LOG_WARN("failed to convert obj to string", K(ret)); } else if (OB_FAIL(ObOptStatSqlService::get_obj_binary_hex_str(bucket.endpoint_value_, allocator, b_endpoint_value))) { @@ -1115,7 +1127,8 @@ int ObOptStatSqlService::hex_str_to_obj(const char *buf, int ObOptStatSqlService::get_obj_str(const ObObj &obj, ObIAllocator &allocator, - ObString &out_str) + ObString &out_str, + const ObObjPrintParams &print_params) { int ret = OB_SUCCESS; char *buf = NULL; @@ -1125,11 +1138,11 @@ int ObOptStatSqlService::get_obj_str(const ObObj &obj, ret = OB_ALLOCATE_MEMORY_FAILED; LOG_WARN("failed to allocate memory", K(ret)); } else if (obj.is_string_type()) { - if (OB_FAIL(obj.print_varchar_literal(buf, buf_len, pos))) { + if (OB_FAIL(obj.print_varchar_literal(buf, buf_len, pos, print_params))) { LOG_WARN("failed to print sql literal", K(ret)); } else { /*do nothing*/ } } else if (obj.is_valid_type()) { - if (OB_FAIL(obj.print_sql_literal(buf, buf_len, pos))) { + if (OB_FAIL(obj.print_sql_literal(buf, buf_len, pos, print_params))) { LOG_WARN("failed to print_sql_literal", K(ret)); } else { /*do nothing*/ } } else { @@ -1619,47 +1632,46 @@ int ObOptStatSqlService::generate_in_list(const ObIArray &list, ObSqlS int ObOptStatSqlService::get_valid_obj_str(const ObObj &src_obj, common::ObObjMeta dst_column_meta, ObIAllocator &allocator, - ObString &dest_str) + ObString &dest_str, + const ObObjPrintParams &print_params) { int ret = OB_SUCCESS; if (src_obj.is_string_type()) { - ObString str; - int64_t well_formed_len = 0; - ObObj new_obj; - ObArenaAllocator calc_buf(ObModIds::OB_SQL_PARSER); - ObCastCtx cast_ctx(&calc_buf, NULL, CM_NONE, dst_column_meta.get_collation_type()); - if (src_obj.get_meta().get_charset_type() != dst_column_meta.get_charset_type()) { - if (OB_FAIL(ObObjCaster::to_type(dst_column_meta.get_type(), cast_ctx, src_obj, new_obj))) { - LOG_WARN("failed to cast number to double type", K(ret)); - } else {/*do nothing*/} - } else { - new_obj = src_obj; - } - if (OB_FAIL(ret)) {//do nothing - } else if (OB_FAIL(new_obj.get_string(str))) { - LOG_WARN("failed to get string", K(ret), K(str)); - } else if (OB_FAIL(ObCharset::well_formed_len(dst_column_meta.get_collation_type(), str.ptr(), - str.length(), well_formed_len))) { + ObObj dst_obj; + char *buf = NULL; + int32_t buf_len = src_obj.get_string_len() * ObCharset::CharConvertFactorNum; + uint32_t result_len = 0; + if (0 == buf_len) { + //do noting + } else if (OB_UNLIKELY(NULL == (buf = static_cast( + allocator.alloc(buf_len))))) { + ret = OB_ALLOCATE_MEMORY_FAILED; + LOG_ERROR("alloc memory failed", K(ret), K(buf_len)); + } else if (OB_FAIL(ObCharset::charset_convert(src_obj.get_collation_type(), src_obj.get_string_ptr(), + src_obj.get_string_len(), dst_column_meta.get_collation_type(), buf, buf_len, result_len))) { //for column which have invalid char ==> save obj binary to use, and obj value to // save "-4258: Incorrect string value" to show this obj have invalid. if (OB_ERR_INCORRECT_STRING_VALUE == ret) { - LOG_WARN("invalid string for charset", K(ret), K(dst_column_meta), K(str), K(well_formed_len), - KPHEX(str.ptr(), str.length())); + LOG_WARN("invalid string for charset", K(ret), K(dst_column_meta), K(dst_obj)); ret = OB_SUCCESS; const char *incorrect_string = "-4258: Incorrect string value, can't show."; ObObj incorrect_str_obj; - incorrect_str_obj.set_string(new_obj.get_type(), incorrect_string, static_cast(strlen(incorrect_string))); - if (OB_FAIL(get_obj_str(incorrect_str_obj, allocator, dest_str))) { + incorrect_str_obj.set_string(dst_column_meta.get_type(), incorrect_string, static_cast(strlen(incorrect_string))); + if (OB_FAIL(get_obj_str(incorrect_str_obj, allocator, dest_str, print_params))) { LOG_WARN("failed to get obj str", K(ret)); - } else {/*do nothing*/} + } else {} } else { LOG_WARN("failed to judge the string formed", K(ret)); } - } else if (OB_FAIL(get_obj_str(new_obj, allocator, dest_str))) { - LOG_WARN("failed to get obj str", K(ret)); - } else {/*do nothing*/} + } else { + dst_obj.set_string(src_obj.get_type(), buf, static_cast(result_len)); + dst_obj.set_collation_type(dst_column_meta.get_collation_type()); + if (OB_FAIL(get_obj_str(dst_obj, allocator, dest_str, print_params))) { + LOG_WARN("fail to get obj str", K(ret)); + } + } LOG_TRACE("succeed to get valid obj str", K(src_obj), K(dst_column_meta), K(dest_str)); - } else if (OB_FAIL(get_obj_str(src_obj, allocator, dest_str))) { + } else if (OB_FAIL(get_obj_str(src_obj, allocator, dest_str, print_params))) { LOG_WARN("failed to get obj str", K(ret)); } else {/*do nothing*/} return ret; diff --git a/src/share/stat/ob_opt_stat_sql_service.h b/src/share/stat/ob_opt_stat_sql_service.h index 1bd45f299f..92ad60a4f1 100644 --- a/src/share/stat/ob_opt_stat_sql_service.h +++ b/src/share/stat/ob_opt_stat_sql_service.h @@ -101,7 +101,8 @@ public: const common::ObIArray &column_stats, const int64_t current_time, bool only_update_col_stat = false, - bool is_history_stat = false); + bool is_history_stat = false, + const ObObjPrintParams &print_params = ObObjPrintParams()); int delete_table_stat(const uint64_t exec_tenant_id, const uint64_t table_id, @@ -133,7 +134,8 @@ public: static int get_obj_str(const common::ObObj &obj, common::ObIAllocator &allocator, - common::ObString &out_str); + common::ObString &out_str, + const ObObjPrintParams &print_params); static int get_obj_binary_hex_str(const common::ObObj &obj, common::ObIAllocator &allocator, @@ -147,7 +149,8 @@ public: static int get_valid_obj_str(const common::ObObj &src_obj, common::ObObjMeta dst_column_meta, common::ObIAllocator &allocator, - common::ObString &dest_str); + common::ObString &dest_str, + const ObObjPrintParams &print_params); int fetch_table_rowcnt(const uint64_t tenant_id, const uint64_t table_id, @@ -171,27 +174,31 @@ private: const int64_t current_time, ObObjMeta min_meta, ObObjMeta max_meta, - ObSqlString &sql_string); + ObSqlString &sql_string, + const ObObjPrintParams &print_params); int get_column_stat_history_sql(const uint64_t tenant_id, ObIAllocator &allocator, const ObOptColumnStat &stat, const int64_t saving_time, ObObjMeta min_meta, ObObjMeta max_meta, - ObSqlString &sql_string); + ObSqlString &sql_string, + const ObObjPrintParams &print_params); int get_histogram_stat_sql(const uint64_t tenant_id, const ObOptColumnStat &stat, common::ObIAllocator &allocator, ObHistBucket &bucket, ObObjMeta endpoint_meta, - ObSqlString &sql_string); + ObSqlString &sql_string, + const ObObjPrintParams &print_params); int get_histogram_stat_history_sql(const uint64_t tenant_id, const ObOptColumnStat &stat, ObIAllocator &allocator, const ObHistBucket &bucket, const int64_t saving_time, ObObjMeta endpoint_meta, - ObSqlString &sql_string); + ObSqlString &sql_string, + const ObObjPrintParams &print_params); int generate_in_list(const ObIArray &list, ObSqlString &sql_string); @@ -202,7 +209,8 @@ private: const ObIArray &column_stats, const int64_t current_time, bool is_history_stat, - ObSqlString &column_stats_sql); + ObSqlString &column_stats_sql, + const ObObjPrintParams &print_params); int construct_delete_column_histogram_sql(const uint64_t tenant_id, const ObIArray &column_stats, @@ -215,7 +223,8 @@ private: const int64_t current_time, bool is_history_stat, ObSqlString &insert_histogram_sql, - bool &need_histogram); + bool &need_histogram, + const ObObjPrintParams &print_params); int generate_specified_keys_list_str(const uint64_t tenant_id, ObIArray &key_col_stats, diff --git a/src/share/stat/ob_stat_define.h b/src/share/stat/ob_stat_define.h index 0e3fb7103c..aadf64c9db 100644 --- a/src/share/stat/ob_stat_define.h +++ b/src/share/stat/ob_stat_define.h @@ -53,7 +53,7 @@ enum StatOptionFlags }; const static double OPT_DEFAULT_STALE_PERCENT = 0.1; const static int64_t OPT_DEFAULT_STATS_RETENTION = 31; -const static int64_t OPT_STATS_MAX_VALUE_CAHR_LEN = 128; +const static int64_t OPT_STATS_MAX_VALUE_CHAR_LEN = 128; const int64_t MAGIC_SAMPLE_SIZE = 5500; const int64_t MAGIC_MAX_AUTO_SAMPLE_SIZE = 22000; const int64_t MAGIC_MIN_SAMPLE_SIZE = 2500; diff --git a/src/share/stat/ob_stat_item.cpp b/src/share/stat/ob_stat_item.cpp index 65823f1827..1e037e04db 100644 --- a/src/share/stat/ob_stat_item.cpp +++ b/src/share/stat/ob_stat_item.cpp @@ -112,7 +112,7 @@ int ObStatMaxValue::gen_expr(char *buf, const int64_t buf_len, int64_t &pos) " MAX(SUBSTR(`%.*s`, 1, %ld))", col_param_->column_name_.length(), col_param_->column_name_.ptr(), - OPT_STATS_MAX_VALUE_CAHR_LEN))) { + OPT_STATS_MAX_VALUE_CHAR_LEN))) { LOG_WARN("failed to print max(col) expr", K(ret)); } return ret; @@ -149,7 +149,7 @@ int ObStatMinValue::gen_expr(char *buf, const int64_t buf_len, int64_t &pos) " MIN(SUBSTR(`%.*s`, 1, %ld))", col_param_->column_name_.length(), col_param_->column_name_.ptr(), - OPT_STATS_MAX_VALUE_CAHR_LEN))) { + OPT_STATS_MAX_VALUE_CHAR_LEN))) { LOG_WARN("failed to print max(col) expr", K(ret)); } return ret; @@ -277,7 +277,7 @@ int ObStatTopKHist::gen_expr(char *buf, const int64_t buf_len, int64_t &pos) err_rate, col_param_->column_name_.length(), col_param_->column_name_.ptr(), - OPT_STATS_MAX_VALUE_CAHR_LEN, + OPT_STATS_MAX_VALUE_CHAR_LEN, bkt_num))) { LOG_WARN("failed to print buf topk hist expr", K(ret)); } @@ -696,7 +696,7 @@ int ObStatHybridHist::gen_expr(char *buf, const int64_t buf_len, int64_t &pos) " HYBRID_HIST(SUBSTR(`%.*s`, 1, %ld), %ld)", col_param_->column_name_.length(), col_param_->column_name_.ptr(), - OPT_STATS_MAX_VALUE_CAHR_LEN, + OPT_STATS_MAX_VALUE_CHAR_LEN, col_param_->bucket_num_))) { LOG_WARN("failed to print buf", K(ret)); } else {/*do nothing*/} diff --git a/src/sql/engine/opt_statistics/ob_optimizer_stats_gathering_op.cpp b/src/sql/engine/opt_statistics/ob_optimizer_stats_gathering_op.cpp index 008a729fe2..0e7a215410 100644 --- a/src/sql/engine/opt_statistics/ob_optimizer_stats_gathering_op.cpp +++ b/src/sql/engine/opt_statistics/ob_optimizer_stats_gathering_op.cpp @@ -100,7 +100,6 @@ void ObOptimizerStatsGatheringOp::reset() { } void ObOptimizerStatsGatheringOp::reuse_stats() { - // call each column stat's reset to free memory allocated by inner_max_alloc and inner_min_alloc FOREACH(it, column_stats_map_) { if (OB_NOT_NULL(it->second)) { it->second->reset(); @@ -489,9 +488,18 @@ void ObOptimizerStatsGatheringOp::set_col_stats_avg_len(StatItems &all_stats, in int ObOptimizerStatsGatheringOp::set_col_stats(StatItems &all_stats, ObObj &obj) { int ret = OB_SUCCESS; + const ObObj *tmp_obj; + if (OB_FAIL(ObOptimizerUtil::truncate_string_for_opt_stats(&obj, arena_, tmp_obj))) { + LOG_WARN("fail to truncate string", K(ret)); + } else { + obj = *tmp_obj; + } + all_stats.global_col_stat_->set_stat_level(StatLevel::TABLE_LEVEL); - if (OB_FAIL(all_stats.global_col_stat_->merge_obj(obj))) { - LOG_WARN("fail to set global column stat", K(ret), K(obj)); + if (OB_SUCC(ret)) { + if (OB_FAIL(all_stats.global_col_stat_->merge_obj(obj))) { + LOG_WARN("fail to set global column stat", K(ret), K(obj)); + } } if (OB_SUCC(ret) && MY_SPEC.is_part_table()) { all_stats.part_col_stat_->set_stat_level(StatLevel::PARTITION_LEVEL); @@ -583,10 +591,8 @@ int ObOptimizerStatsGatheringOp::merge_col_stat(ObOptColumnStat *src_col_stat) } else { } } - } else { - if (OB_FAIL(col_stat->merge_column_stat(*src_col_stat))) { - LOG_WARN("fail to merge two table stats", K(ret), K(col_stat), K(src_col_stat)); - } + } else if (OB_FAIL(col_stat->merge_column_stat(*src_col_stat))) { + LOG_WARN("fail to merge two table stats", K(ret), K(col_stat), K(src_col_stat)); } return ret; } diff --git a/src/sql/optimizer/ob_insert_log_plan.cpp b/src/sql/optimizer/ob_insert_log_plan.cpp index 9b0f1915e1..d54fe8d25b 100644 --- a/src/sql/optimizer/ob_insert_log_plan.cpp +++ b/src/sql/optimizer/ob_insert_log_plan.cpp @@ -166,7 +166,8 @@ int ObInsertLogPlan::check_need_online_stats_gather(bool &need_osg) LOG_WARN("get unexpected null pointer", K(ret), K(insert_stmt->get_insert_table_info())); } else if (OB_UNLIKELY(ins_table->is_system_table_ || ins_table->is_index_table_) || insert_stmt->is_insert_up() - || !get_optimizer_context().get_session_info()->is_user_session()) { + || insert_stmt->value_from_select() + || (!get_optimizer_context().get_session_info()->is_user_session())) { need_gathering = false; } @@ -184,7 +185,7 @@ int ObInsertLogPlan::check_need_online_stats_gather(bool &need_osg) && online_sys_var && ((get_optimizer_context().get_query_ctx()->get_global_hint().should_generate_osg_operator()) || (get_optimizer_context().use_pdml())); - LOG_TRACE("online insert stat", K(online_sys_var), K(need_osg)); + LOG_TRACE("online insert stat", K(online_sys_var), K(need_osg), K(need_gathering)); } return ret; } diff --git a/src/sql/optimizer/ob_opt_selectivity.cpp b/src/sql/optimizer/ob_opt_selectivity.cpp index 9ba5aa60cd..4d6567a6f1 100644 --- a/src/sql/optimizer/ob_opt_selectivity.cpp +++ b/src/sql/optimizer/ob_opt_selectivity.cpp @@ -4103,40 +4103,10 @@ int ObOptSelectivity::convert_valid_obj_for_opt_stats(const ObObj *old_obj, const ObObj *&new_obj) { int ret = OB_SUCCESS; - ObObj tmp_obj; - if (OB_ISNULL(old_obj)) { - ret = OB_ERR_UNEXPECTED; - LOG_WARN("get unexpected null", K(ret), K(old_obj)); - } else if (old_obj->is_string_type()) { - ObString str; - if (OB_FAIL(old_obj->get_string(str))) { - LOG_WARN("failed to get string", K(ret), K(str)); - } else { - int64_t mb_len = ObCharset::strlen_char(old_obj->get_collation_type(), str.ptr(), str.length()); - if (mb_len <= OPT_STATS_MAX_VALUE_CAHR_LEN) { - new_obj = old_obj; - } else { - //need truncate string, because the opt stats is gathered after truncate string, such as: max_value、min_value、histogram. - ObObj *tmp_obj = NULL; - if (OB_ISNULL(tmp_obj = static_cast(alloc.alloc(sizeof(ObObj))))) { - ret = OB_ALLOCATE_MEMORY_FAILED; - LOG_WARN("fail to alloc buf", K(ret)); - } else { - int64_t valid_len = ObCharset::charpos(old_obj->get_collation_type(), str.ptr(), - str.length(), OPT_STATS_MAX_VALUE_CAHR_LEN); - if (OB_UNLIKELY(valid_len <= 0)) { - ret = OB_ERR_UNEXPECTED; - LOG_WARN("get unexpected error", K(ret), K(valid_len), K(str), K(OPT_STATS_MAX_VALUE_CAHR_LEN)); - } else { - tmp_obj->set_varchar(str.ptr(), valid_len); - tmp_obj->set_meta_type(old_obj->get_meta()); - new_obj = tmp_obj; - } - } - } - } - } else { - new_obj = old_obj; + if (OB_FAIL(ObOptimizerUtil::truncate_string_for_opt_stats(old_obj, + alloc, + new_obj))) { + LOG_WARN("fail to truncated string", K(ret)); } LOG_TRACE("Succeed to convert valid obj for opt stats", KPC(old_obj), KPC(new_obj)); return ret; diff --git a/src/sql/optimizer/ob_optimizer_util.cpp b/src/sql/optimizer/ob_optimizer_util.cpp index 6654999a8f..ab899f7396 100644 --- a/src/sql/optimizer/ob_optimizer_util.cpp +++ b/src/sql/optimizer/ob_optimizer_util.cpp @@ -8611,3 +8611,44 @@ int ObOptimizerUtil::replace_gen_column(ObLogPlan *log_plan, ObRawExpr *part_exp } return ret; } + +int ObOptimizerUtil::truncate_string_for_opt_stats(const ObObj *old_obj, + ObIAllocator &alloc, + const ObObj *&new_obj) +{ + int ret = OB_SUCCESS; + bool is_truncated = false; + if (old_obj->is_string_type()) { + ObString str; + if (OB_FAIL(old_obj->get_string(str))) { + LOG_WARN("failed to get string", K(ret), K(str)); + } else { + int64_t mb_len = ObCharset::strlen_char(old_obj->get_collation_type(), str.ptr(), str.length()); + if (mb_len <= OPT_STATS_MAX_VALUE_CHAR_LEN) { + } else { + //need truncate string, because the opt stats is gathered after truncate string, such as: max_value、min_value、histogram. + ObObj *tmp_obj = NULL; + if (OB_ISNULL(tmp_obj = static_cast(alloc.alloc(sizeof(ObObj))))) { + ret = OB_ALLOCATE_MEMORY_FAILED; + LOG_WARN("fail to alloc buf", K(ret)); + } else { + int64_t valid_len = ObCharset::charpos(old_obj->get_collation_type(), str.ptr(), + str.length(), OPT_STATS_MAX_VALUE_CHAR_LEN); + if (OB_UNLIKELY(valid_len <= 0)) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("get unexpected error", K(ret), K(valid_len), K(str), K(OPT_STATS_MAX_VALUE_CHAR_LEN)); + } else { + tmp_obj->set_varchar(str.ptr(), valid_len); + tmp_obj->set_meta_type(old_obj->get_meta()); + new_obj = tmp_obj; + is_truncated = true; + } + } + } + } + } + if (OB_SUCC(ret) && !is_truncated) { + new_obj = old_obj; + } + return ret; +} diff --git a/src/sql/optimizer/ob_optimizer_util.h b/src/sql/optimizer/ob_optimizer_util.h index 88661d9b82..8878e41230 100644 --- a/src/sql/optimizer/ob_optimizer_util.h +++ b/src/sql/optimizer/ob_optimizer_util.h @@ -1436,6 +1436,10 @@ public: static int check_contain_my_exec_param(ObRawExpr* expr, const common::ObIArray & my_exec_params, bool &contain); + static int truncate_string_for_opt_stats(const ObObj *old_obj, + ObIAllocator &alloc, + const ObObj *&new_obj); + private: //disallow construct ObOptimizerUtil();