From 1b88c27061b48d8f566ddd5c4765e726d4f1ffb1 Mon Sep 17 00:00:00 2001 From: obdev Date: Wed, 20 Jul 2022 11:32:34 +0800 Subject: [PATCH] fix: json column default value(3.1_opensource) --- deps/oblib/src/common/object/ob_object.cpp | 7 +++- .../src/lib/mysqlclient/ob_mysql_result.h | 38 ++++++++++++++----- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/deps/oblib/src/common/object/ob_object.cpp b/deps/oblib/src/common/object/ob_object.cpp index 4794b04d3f..18e0a92846 100644 --- a/deps/oblib/src/common/object/ob_object.cpp +++ b/deps/oblib/src/common/object/ob_object.cpp @@ -276,6 +276,7 @@ DEF_TO_STRING(ObLobLocator) const char* ObObj::MIN_OBJECT_VALUE_STR = "__OB__MIN__"; const char* ObObj::MAX_OBJECT_VALUE_STR = "__OB__MAX__"; const char* ObObj::NOP_VALUE_STR = "__OB__NOP__"; +const char OB_JSON_NULL[2] = {'\0', '\0'}; // binary json null OB_SERIALIZE_MEMBER(ObDataType, meta_, accuracy_, is_zero_fill_); OB_SERIALIZE_MEMBER(ObEnumSetInnerValue, numberic_value_, string_value_); @@ -458,12 +459,14 @@ int ObObj::build_not_strict_default_value() case ObTinyTextType: case ObTextType: case ObMediumTextType: - case ObLongTextType: - case ObJsonType: { + case ObLongTextType: { ObString null_str; set_string(data_type, null_str); meta_.set_lob_inrow(); } break; + case ObJsonType: { + set_json_value(data_type, OB_JSON_NULL, 2); + } break; case ObBitType: set_bit(0); break; diff --git a/deps/oblib/src/lib/mysqlclient/ob_mysql_result.h b/deps/oblib/src/lib/mysqlclient/ob_mysql_result.h index d3afdce7fe..91c3b1369e 100644 --- a/deps/oblib/src/lib/mysqlclient/ob_mysql_result.h +++ b/deps/oblib/src/lib/mysqlclient/ob_mysql_result.h @@ -686,7 +686,34 @@ } \ ret = (class_obj).set_##column_name(dest_obj); \ } \ - } else { \ + } else if (ob_is_json(data_type) && share::is_mysql_mode()) \ + { /* MySQL json does not support default value except null, */ \ + /* need this defensive to compatible a bug in old version */ \ + ObObj def_obj; \ + ObObj default_value; \ + default_value.set_type(data_type); \ + ObArenaAllocator allocator(ObModIds::OB_SCHEMA); \ + ObObj dest_obj; \ + ObCastCtx cast_ctx(&allocator, NULL, CM_NONE, column.get_collation_type()); \ + if (OB_FAIL(default_value.build_not_strict_default_value())) { \ + SQL_LOG(WARN, "failed to build not strict default json value", K(ret)); \ + } else { \ + def_obj.set_json_value(data_type, \ + default_value.get_string().ptr(), \ + default_value.get_string().length()); \ + if (OB_FAIL(ObObjCaster::to_type(data_type, cast_ctx, def_obj, dest_obj))) \ + { \ + SQL_LOG(WARN, "cast obj failed, ", "src type", def_obj.get_type(), "dest type", data_type); \ + } \ + else \ + { \ + dest_obj.set_lob_inrow(); \ + dest_obj.meta_.set_collation_level(CS_LEVEL_IMPLICIT); \ + ret = (class_obj).set_##column_name(dest_obj); \ + } \ + } \ + } \ + else { \ ObObj def_obj; \ def_obj.set_varchar(str_value); \ ObArenaAllocator allocator(ObModIds::OB_SCHEMA); \ @@ -699,14 +726,7 @@ } else if (OB_FAIL(ObObjCaster::to_type(data_type, cast_ctx, def_obj, dest_obj))) { \ SQL_LOG(WARN, "cast obj failed, ", "src type", def_obj.get_type(), "dest type", data_type); \ } else { \ - if (ob_is_json(data_type)) { \ - dest_obj.set_lob_inrow(); \ - dest_obj.meta_.set_collation_level(CS_LEVEL_IMPLICIT); \ - } \ - else \ - { \ - dest_obj.set_scale(column.get_data_scale()); \ - } \ + dest_obj.set_scale(column.get_data_scale()); \ ret = (class_obj).set_##column_name(dest_obj); \ } \ } \ -- GitLab