From 93cf8e30578c5275612c2e4b198b74ef67fc0798 Mon Sep 17 00:00:00 2001 From: Monk-Liu <1152761042@qq.com> Date: Tue, 4 Apr 2023 00:14:55 +0000 Subject: [PATCH] [bugfix]: add constraint(pk) with constraint name, the constraint name shouldn't be ignored. --- src/rootserver/ob_ddl_service.cpp | 74 +++++++++++++++++-- src/rootserver/ob_ddl_service.h | 12 ++- .../resolver/ddl/ob_alter_table_resolver.cpp | 12 ++- 3 files changed, 87 insertions(+), 11 deletions(-) diff --git a/src/rootserver/ob_ddl_service.cpp b/src/rootserver/ob_ddl_service.cpp index 01bbc3425..e1fae0d74 100644 --- a/src/rootserver/ob_ddl_service.cpp +++ b/src/rootserver/ob_ddl_service.cpp @@ -2775,6 +2775,7 @@ int ObDDLService::create_hidden_table_with_pk_changed( { int ret = OB_SUCCESS; const bool bind_tablets = false; + ObString index_name(""); const bool is_drop_pk = ObIndexArg::DROP_PRIMARY_KEY == index_action_type; const bool is_add_or_alter_pk = (ObIndexArg::ADD_PRIMARY_KEY == index_action_type) || (ObIndexArg::ALTER_PRIMARY_KEY == index_action_type); // For add primary key and modify column in one sql, create user hidden table when modifing column. @@ -2788,20 +2789,72 @@ int ObDDLService::create_hidden_table_with_pk_changed( LOG_WARN("failed to add pk", K(ret), K(index_columns), K(new_table_schema)); } else if (is_drop_pk && OB_FAIL(drop_primary_key(new_table_schema))) { LOG_WARN("failed to add hidden pk column for heap table", K(ret)); - } else if (create_user_hidden_table_now - && OB_FAIL(create_user_hidden_table(origin_table_schema, + } else if (!create_user_hidden_table_now) { + } else if (OB_FAIL(get_add_pk_index_name(origin_table_schema, + new_table_schema, + index_action_type, + alter_table_arg.index_arg_list_, + schema_guard, + index_name))) { + LOG_WARN("fail to rename hidden table's pk constraint", K(ret)); + } else if (OB_FAIL(create_user_hidden_table(origin_table_schema, new_table_schema, &alter_table_arg.sequence_ddl_arg_, bind_tablets, schema_guard, ddl_operator, trans, - allocator))) { + allocator, + index_name))) { LOG_WARN("failed to alter table offline", K(ret)); } return ret; } +int ObDDLService::get_add_pk_index_name(const ObTableSchema &origin_table_schema, + ObTableSchema &new_table_schema, + const ObIndexArg::IndexActionType &index_action_type, + const ObIArray &index_arg_list, + ObSchemaGetterGuard &schema_guard, + ObString &index_name) +{ + int ret = OB_SUCCESS; + bool is_oracle_mode = false; + bool is_exist = false; + + if (OB_FAIL(origin_table_schema.check_if_oracle_compat_mode(is_oracle_mode))) { + LOG_WARN("fail to check oracle mode", K(ret)); + } else if (is_oracle_mode && index_action_type == ObIndexArg::ADD_PRIMARY_KEY) { + // find pk name; + ObIndexArg *tmp_index_arg = nullptr; + bool found = false; + for (int64_t i = 0; OB_SUCC(ret) && !found && i < index_arg_list.count(); i++) { + if (OB_ISNULL(tmp_index_arg = index_arg_list.at(i))) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("fail to get index arg", K(ret)); + } else if (tmp_index_arg->index_action_type_ == ObIndexArg::ADD_PRIMARY_KEY) { + found = true; + } + } + if (OB_SUCC(ret) && found && tmp_index_arg->index_name_.length() != 0) { + index_name.assign_ptr(tmp_index_arg->index_name_.ptr(), + tmp_index_arg->index_name_.length()); + // check constraint name is not duplicated. + if (OB_FAIL(check_constraint_name_is_exist(schema_guard, + origin_table_schema, + index_name, + false, + is_exist))) { + LOG_WARN("fail to check constraint exist", K(ret)); + } else if (is_exist) { + ret = OB_ERR_CONSTRAINT_NAME_DUPLICATE; + LOG_WARN("check constraint name is duplicate", K(ret), K(index_name)); + } + } + } + return ret; +} + int ObDDLService::check_is_change_column_order( const ObTableSchema &table_schema, const AlterColumnSchema &alter_column_schema, @@ -13338,7 +13391,8 @@ int ObDDLService::is_foreign_key_name_prefix_match(const ObForeignKeyInfo &origi int ObDDLService::prepare_hidden_table_schema(const ObTableSchema &orig_table_schema, ObIAllocator &allocator, - ObTableSchema &hidden_table_schema) + ObTableSchema &hidden_table_schema, + const ObString &index_name) { int ret = OB_SUCCESS; bool is_oracle_mode = false; @@ -13396,7 +13450,11 @@ int ObDDLService::prepare_hidden_table_schema(const ObTableSchema &orig_table_sc LOG_WARN("failed to get pk constraint name", K(ret)); } } else { - if (OB_FAIL(ObTableSchema::create_cons_name_automatically( + if (index_name.length() != 0) { + if (OB_FAIL(ob_write_string(allocator, index_name, pk_name))) { + LOG_WARN("fail to write string", K(ret)); + } + } else if (OB_FAIL(ObTableSchema::create_cons_name_automatically( pk_name, orig_table_schema.get_table_name_str(), allocator, CONSTRAINT_TYPE_PRIMARY_KEY, is_oracle_mode))) { LOG_WARN("create cons name automatically failed", K(ret)); @@ -13500,7 +13558,8 @@ int ObDDLService::create_user_hidden_table(const ObTableSchema &orig_table_schem ObSchemaGetterGuard &schema_guard, ObDDLOperator &ddl_operator, ObMySQLTransaction &trans, - ObIAllocator &allocator) + ObIAllocator &allocator, + const ObString &index_name/*default ""*/) { int ret = OB_SUCCESS; const uint64_t tenant_id = hidden_table_schema.get_tenant_id(); @@ -13519,7 +13578,8 @@ int ObDDLService::create_user_hidden_table(const ObTableSchema &orig_table_schem LOG_WARN("failed to check is add identity column", K(ret)); } else if (OB_FAIL(prepare_hidden_table_schema(orig_table_schema, allocator, - hidden_table_schema))) { + hidden_table_schema, + index_name))) { LOG_WARN("failed to prepare hidden table schema", K(ret)); } else if (OB_FAIL(ddl_operator.create_sequence_in_create_table(hidden_table_schema, trans, diff --git a/src/rootserver/ob_ddl_service.h b/src/rootserver/ob_ddl_service.h index 44028c9be..eb2fb9a56 100644 --- a/src/rootserver/ob_ddl_service.h +++ b/src/rootserver/ob_ddl_service.h @@ -610,6 +610,12 @@ public: common::ObMySQLTransaction &trans, common::ObArenaAllocator &allocator, const obrpc::ObIndexArg::IndexActionType &index_action_type); + int get_add_pk_index_name(const share::schema::ObTableSchema &origin_table_schema, + share::schema::ObTableSchema &new_table_schema, + const obrpc::ObIndexArg::IndexActionType &index_action_type, + const common::ObIArray &index_arg_list, + share::schema::ObSchemaGetterGuard &schema_guard, + ObString &index_name); virtual int rename_table(const obrpc::ObRenameTableArg &rename_table_arg); int collect_temporary_tables_in_session(const obrpc::ObDropTableArg &drop_table_arg); int need_collect_current_temp_table(share::schema::ObSchemaGetterGuard &schema_guard, @@ -1298,7 +1304,8 @@ private: share::schema::ObSchemaGetterGuard &schema_guard, ObDDLOperator &ddl_operator, common::ObMySQLTransaction &trans, - common::ObIAllocator &allocator); + common::ObIAllocator &allocator, + const ObString &index_name = ObString("")); int rebuild_triggers_on_hidden_table( const share::schema::ObTableSchema &orig_table_schema, const share::schema::ObTableSchema &hidden_table_schema, @@ -1331,7 +1338,8 @@ private: int prepare_hidden_table_schema( const share::schema::ObTableSchema &orig_table_schema, common::ObIAllocator &allocator, - share::schema::ObTableSchema &hidden_table_schema); + share::schema::ObTableSchema &hidden_table_schema, + const ObString &index_name); int rebuild_hidden_table_priv( const share::schema::ObTableSchema &orig_table_schema, const share::schema::ObTableSchema &hidden_table_schema, diff --git a/src/sql/resolver/ddl/ob_alter_table_resolver.cpp b/src/sql/resolver/ddl/ob_alter_table_resolver.cpp index 6ec7ac8f7..6a7321d42 100644 --- a/src/sql/resolver/ddl/ob_alter_table_resolver.cpp +++ b/src/sql/resolver/ddl/ob_alter_table_resolver.cpp @@ -2613,8 +2613,16 @@ int ObAlterTableResolver::resolve_add_primary(const ParseNode &node) if (OB_SUCC(ret)) { ObAlterTableStmt *alter_table_stmt = get_alter_table_stmt(); create_index_arg->index_type_ = INDEX_TYPE_PRIMARY; - create_index_arg->index_name_.assign_ptr(common::OB_PRIMARY_INDEX_NAME, - static_cast(strlen(common::OB_PRIMARY_INDEX_NAME))); + if (lib::is_oracle_mode()) { + if (node.num_child_ == 2 && OB_NOT_NULL(node.children_[1]) + && node.children_[1]->str_len_ != 0) { + create_index_arg->index_name_.assign_ptr(node.children_[1]->str_value_, + static_cast(node.children_[1]->str_len_)); + } + } else { + create_index_arg->index_name_.assign_ptr(common::OB_PRIMARY_INDEX_NAME, + static_cast(strlen(common::OB_PRIMARY_INDEX_NAME))); + } create_index_arg->tenant_id_ = session_info_->get_effective_tenant_id(); if (OB_ISNULL(alter_table_stmt)) { ret = OB_ERR_UNEXPECTED;