diff --git a/src/sql/engine/aggregate/ob_aggregate_processor.cpp b/src/sql/engine/aggregate/ob_aggregate_processor.cpp index eeb470420e..881bbd138c 100644 --- a/src/sql/engine/aggregate/ob_aggregate_processor.cpp +++ b/src/sql/engine/aggregate/ob_aggregate_processor.cpp @@ -6559,7 +6559,8 @@ int ObAggregateProcessor::get_json_objectagg_result(const ObAggrInfo &aggr_info, // get key and value, and append to json_object ObString key_data; ObIJsonBase *json_val = NULL; - if (OB_SUCC(ret) && OB_FAIL(deep_copy_ob_string(tmp_alloc, key_string, key_data))) { + if (OB_FAIL(ret)) { + } else if (OB_FAIL(deep_copy_ob_string(tmp_alloc, key_string, key_data))) { LOG_WARN("fail copy string", K(ret), K(key_string.length())); } else { ObDatum converted_datum; @@ -6927,15 +6928,19 @@ int ObAggregateProcessor::get_ora_json_objectagg_result(const ObAggrInfo &aggr_i ObString key_string = datum_key.get_string(); ObIJsonBase *json_val = nullptr; + bool need_key_string_convert = (ObCharset::charset_type_by_coll(cs_type_key) != CHARSET_UTF8MB4); + if (OB_ISNULL(key_string.ptr())) { ret = OB_ERR_NULL_VALUE; LOG_WARN("unexpected null result", K(ret)); } else if (is_absent_on_null && ob_is_null(type_value)) { continue; - } else if (ObCharset::charset_type_by_coll(cs_type_key) != CHARSET_UTF8MB4 + } else if (need_key_string_convert && OB_FAIL(ObExprUtil::convert_string_collation(key_string, cs_type_key, key_string, CS_TYPE_UTF8MB4_BIN, tmp_alloc))) { LOG_WARN("convert key string collation failed", K(ret), K(cs_type_key), K(key_string.length())); + } else if (!need_key_string_convert && OB_FAIL(deep_copy_ob_string(tmp_alloc, key_string, key_string))) { + LOG_WARN("fail to deep copy string.", K(ret), K(key_string)); } else if (OB_FAIL(ObJsonExprHelper::oracle_datum2_json_val(&datum_value, meta_value, &tmp_alloc, eval_ctx_.exec_ctx_.get_my_session(), json_val, false, is_format_json, is_strict, false))) { LOG_WARN("failed to eval json val node.", K(ret), K(is_format_json), K(is_strict), K(meta_value)); diff --git a/src/sql/engine/expr/ob_expr_json_func_helper.cpp b/src/sql/engine/expr/ob_expr_json_func_helper.cpp index 8d71b8f143..b12ac7d16a 100644 --- a/src/sql/engine/expr/ob_expr_json_func_helper.cpp +++ b/src/sql/engine/expr/ob_expr_json_func_helper.cpp @@ -380,12 +380,14 @@ int ObJsonExprHelper::oracle_datum2_json_val(const ObDatum *json_datum, uint32_t parse_flag = ObJsonParser::JSN_RELAXED_FLAG; ObString j_str = json_datum->get_string(); + bool need_convert = (cs_type != CS_TYPE_INVALID && cs_type != CS_TYPE_BINARY); if (OB_FAIL(ObTextStringHelper::read_real_string_data( allocator, val_type, cs_type, data_meta.has_lob_header(), j_str))) { LOG_WARN("fail to get real data.", K(ret), K(j_str)); - } else if (cs_type != CS_TYPE_INVALID && cs_type != CS_TYPE_BINARY - && OB_FAIL(ObExprUtil::convert_string_collation(j_str, cs_type, j_str, CS_TYPE_UTF8MB4_BIN, *allocator))) { + } else if (need_convert && OB_FAIL(ObExprUtil::convert_string_collation(j_str, cs_type, j_str, CS_TYPE_UTF8MB4_BIN, *allocator))) { LOG_WARN("fail to convert charset.", K(ret), K(j_str), K(cs_type)); + } else if (!need_convert && OB_FAIL(deep_copy_ob_string(*allocator, j_str, j_str))) { + LOG_WARN("fail to deep copy string.", K(ret), K(j_str)); } else if (!is_format_json || is_raw_type) { if (is_raw_type) { ObObj tmp_result; @@ -436,6 +438,8 @@ int ObJsonExprHelper::oracle_datum2_json_val(const ObDatum *json_datum, if (OB_FAIL(ObTextStringHelper::read_real_string_data( allocator, val_type, cs_type, data_meta.has_lob_header(), j_str))) { LOG_WARN("fail to get real data.", K(ret), K(j_str)); + } else if (OB_FAIL(deep_copy_ob_string(*allocator, j_str, j_str))) { + LOG_WARN("fail to deep copy string.", K(ret), K(j_str)); } else if (OB_FAIL(ObJsonBaseFactory::get_json_base(allocator, j_str, ObJsonInType::JSON_BIN, to_type, j_base))) { ret = OB_ERR_INVALID_JSON_TEXT_IN_PARAM; LOG_WARN("fail to get json base", K(ret));