oceanbase
11627 строк · 531.7 Кб
1/**
2* Copyright (c) 2021 OceanBase
3* OceanBase CE is licensed under Mulan PubL v2.
4* You can use this software according to the terms and conditions of the Mulan PubL v2.
5* You may obtain a copy of Mulan PubL v2 at:
6* http://license.coscl.org.cn/MulanPubL-2.0
7* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10* See the Mulan PubL v2 for more details.
11*/
12
13#define USING_LOG_PREFIX RS
14#include "rootserver/ob_ddl_operator.h"
15
16#include "lib/time/ob_time_utility.h"
17#include "lib/string/ob_sql_string.h"
18#include "lib/mysqlclient/ob_mysql_transaction.h"
19#include "lib/encrypt/ob_encrypted_helper.h"
20#include "common/sql_mode/ob_sql_mode.h"
21#include "share/ob_version.h"
22#include "share/ob_autoincrement_service.h"
23#include "share/ob_cluster_version.h"
24#include "share/resource_manager/ob_resource_manager_proxy.h"
25#include "share/schema/ob_schema_service.h"
26#include "share/schema/ob_schema_getter_guard.h"
27#include "share/schema/ob_multi_version_schema_service.h"
28#include "share/schema/ob_tenant_sql_service.h"
29#include "share/schema/ob_database_sql_service.h"
30#include "share/schema/ob_table_sql_service.h"
31#include "share/schema/ob_tablegroup_sql_service.h"
32#include "share/schema/ob_user_sql_service.h"
33#include "share/schema/ob_priv_sql_service.h"
34#include "share/schema/ob_outline_sql_service.h"
35#include "share/schema/ob_label_se_policy_sql_service.h"
36#include "share/schema/ob_profile_sql_service.h"
37#include "share/schema/ob_routine_info.h"
38#include "share/schema/ob_routine_sql_service.h"
39#include "share/schema/ob_udt_info.h"
40#include "share/schema/ob_udt_sql_service.h"
41#include "share/schema/ob_directory_sql_service.h"
42#include "share/schema/ob_package_info.h"
43#include "share/schema/ob_schema_service_sql_impl.h"
44#include "share/schema/ob_security_audit_sql_service.h"
45#include "share/sequence/ob_sequence_ddl_proxy.h"
46#include "share/inner_table/ob_inner_table_schema.h"
47#include "share/config/ob_server_config.h"
48#include "share/inner_table/ob_inner_table_schema.h"
49#include "share/ob_rpc_struct.h"
50#include "share/ob_label_security.h"
51#include "sql/resolver/ddl/ob_ddl_resolver.h"
52#include "sql/resolver/ob_resolver_define.h"
53#include "sql/resolver/ob_resolver_utils.h"
54#include "sql/resolver/expr/ob_raw_expr_modify_column_name.h"
55#include "sql/resolver/expr/ob_raw_expr_util.h"
56#include "sql/printer/ob_raw_expr_printer.h"
57#include "share/system_variable/ob_system_variable.h"
58#include "share/system_variable/ob_system_variable_factory.h"
59#include "share/resource_manager/ob_resource_plan_info.h"
60#include "rootserver/ob_ddl_sql_generator.h"
61#include "rootserver/ob_root_service.h"
62#include "share/schema/ob_part_mgr_util.h"
63#include "observer/ob_server_struct.h"
64#include "observer/ob_sql_client_decorator.h"
65#include "observer/omt/ob_tenant_config_mgr.h"
66#include "share/schema/ob_error_info.h"
67#include "rootserver/ob_root_service.h"
68#include "share/stat/ob_dbms_stats_preferences.h"
69#include "share/ob_tenant_info_proxy.h"//ObAllTenantInfo
70#include "rootserver/ob_tablet_drop.h"
71#include "share/ob_freeze_info_proxy.h"
72#include "share/ob_global_merge_table_operator.h"
73#include "share/ob_zone_merge_table_operator.h"
74#include "share/ob_zone_merge_info.h"
75#include "storage/tx/ob_i_ts_source.h"
76#include "share/stat/ob_dbms_stats_maintenance_window.h"
77#include "share/scn.h"
78#include "share/external_table/ob_external_table_file_mgr.h"
79#include "share/schema/ob_mview_info.h"
80#include "share/schema/ob_mview_refresh_stats_params.h"
81#include "storage/mview/ob_mview_sched_job_utils.h"
82
83namespace oceanbase
84{
85
86using namespace common;
87using namespace share;
88using namespace share::schema;
89using namespace obrpc;
90using namespace sql;
91using namespace storage;
92
93namespace rootserver
94{
95
96ObSysStat::Item::Item(ObSysStat::ItemList &list, const char *name, const char *info)
97: name_(name), info_(info)
98{
99value_.set_int(0);
100const bool add_success = list.add_last(this);
101if (!add_success) {
102LOG_WARN_RET(OB_ERR_UNEXPECTED, "add last failed");
103}
104}
105
106#define MAX_ID_NAME_INFO(id) ObMaxIdFetcher::get_max_id_name(id), ObMaxIdFetcher::get_max_id_info(id)
107ObSysStat::ObSysStat()
108: ob_max_used_tenant_id_(item_list_, MAX_ID_NAME_INFO(OB_MAX_USED_TENANT_ID_TYPE)),
109ob_max_used_unit_config_id_(item_list_, MAX_ID_NAME_INFO(OB_MAX_USED_UNIT_CONFIG_ID_TYPE)),
110ob_max_used_resource_pool_id_(item_list_, MAX_ID_NAME_INFO(OB_MAX_USED_RESOURCE_POOL_ID_TYPE)),
111ob_max_used_unit_id_(item_list_, MAX_ID_NAME_INFO(OB_MAX_USED_UNIT_ID_TYPE)),
112ob_max_used_server_id_(item_list_, MAX_ID_NAME_INFO(OB_MAX_USED_SERVER_ID_TYPE)),
113ob_max_used_ddl_task_id_(item_list_, MAX_ID_NAME_INFO(OB_MAX_USED_DDL_TASK_ID_TYPE)),
114ob_max_used_unit_group_id_(item_list_, MAX_ID_NAME_INFO(OB_MAX_USED_UNIT_GROUP_ID_TYPE)),
115ob_max_used_normal_rowid_table_tablet_id_(item_list_, MAX_ID_NAME_INFO(OB_MAX_USED_NORMAL_ROWID_TABLE_TABLET_ID_TYPE)),
116ob_max_used_extended_rowid_table_tablet_id_(item_list_, MAX_ID_NAME_INFO(OB_MAX_USED_EXTENDED_ROWID_TABLE_TABLET_ID_TYPE)),
117ob_max_used_ls_id_(item_list_, MAX_ID_NAME_INFO(OB_MAX_USED_LS_ID_TYPE)),
118ob_max_used_ls_group_id_(item_list_, MAX_ID_NAME_INFO(OB_MAX_USED_LS_GROUP_ID_TYPE)),
119ob_max_used_sys_pl_object_id_(item_list_, MAX_ID_NAME_INFO(OB_MAX_USED_SYS_PL_OBJECT_ID_TYPE)),
120ob_max_used_object_id_(item_list_, MAX_ID_NAME_INFO(OB_MAX_USED_OBJECT_ID_TYPE)),
121ob_max_used_rewrite_rule_version_(item_list_, MAX_ID_NAME_INFO(OB_MAX_USED_REWRITE_RULE_VERSION_TYPE))
122{
123}
124
125// set values after bootstrap
126int ObSysStat::set_initial_values(const uint64_t tenant_id)
127{
128int ret = OB_SUCCESS;
129if (is_sys_tenant(tenant_id)) {
130ob_max_used_tenant_id_.value_.set_int(OB_USER_TENANT_ID);
131ob_max_used_unit_config_id_.value_.set_int(OB_USER_UNIT_CONFIG_ID);
132ob_max_used_resource_pool_id_.value_.set_int(OB_USER_RESOURCE_POOL_ID);
133ob_max_used_unit_id_.value_.set_int(OB_USER_UNIT_ID);
134ob_max_used_server_id_.value_.set_int(OB_INIT_SERVER_ID - 1);
135ob_max_used_ddl_task_id_.value_.set_int(OB_INIT_DDL_TASK_ID);
136ob_max_used_unit_group_id_.value_.set_int(OB_USER_UNIT_GROUP_ID);
137} else {
138const int64_t root_own_count = 6;
139for (int64_t i = 0; i < root_own_count && OB_SUCC(ret); ++i) {
140const bool remove_succeed = item_list_.remove_first();
141if (!remove_succeed) {
142ret = OB_ERR_UNEXPECTED;
143LOG_WARN("remove_succeed should be true", K(ret));
144}
145}
146}
147if (OB_SUCC(ret)) {
148ob_max_used_normal_rowid_table_tablet_id_.value_.set_int(ObTabletID::MIN_USER_NORMAL_ROWID_TABLE_TABLET_ID);
149ob_max_used_extended_rowid_table_tablet_id_.value_.set_int(ObTabletID::MIN_USER_EXTENDED_ROWID_TABLE_TABLET_ID);
150ob_max_used_ls_id_.value_.set_int(ObLSID::MIN_USER_LS_ID);
151ob_max_used_ls_group_id_.value_.set_int(ObLSID::MIN_USER_LS_GROUP_ID);
152ob_max_used_sys_pl_object_id_.value_.set_int(OB_MIN_SYS_PL_OBJECT_ID);
153// Use OB_INITIAL_TEST_DATABASE_ID to avoid confict when create tenant with initial user schema objects.
154ob_max_used_object_id_.value_.set_int(OB_INITIAL_TEST_DATABASE_ID);
155ob_max_used_rewrite_rule_version_.value_.set_int(OB_INIT_REWRITE_RULE_VERSION);
156}
157return ret;
158}
159
160ObDDLOperator::ObDDLOperator(
161ObMultiVersionSchemaService &schema_service,
162common::ObMySQLProxy &sql_proxy)
163: schema_service_(schema_service),
164sql_proxy_(sql_proxy)
165{
166}
167
168ObDDLOperator::~ObDDLOperator()
169{
170}
171
172int ObDDLOperator::create_tenant(ObTenantSchema &tenant_schema,
173const ObSchemaOperationType op,
174ObMySQLTransaction &trans,
175const ObString *ddl_stmt_str/*=NULL*/)
176{
177int ret = OB_SUCCESS;
178int64_t start = ObTimeUtility::current_time();
179int64_t new_schema_version = OB_INVALID_VERSION;
180ObSchemaService *schema_service = schema_service_.get_schema_service();
181if (OB_DDL_ADD_TENANT != op
182&& OB_DDL_ADD_TENANT_START != op
183&& OB_DDL_ADD_TENANT_END != op) {
184ret = OB_INVALID_ARGUMENT;
185LOG_WARN("invalid operation type", K(ret), K(op));
186} else if (OB_ISNULL(schema_service)) {
187ret = OB_ERR_SYS;
188LOG_ERROR("schema_service must not null");
189} else if (OB_FAIL(schema_service_.gen_new_schema_version(OB_SYS_TENANT_ID, new_schema_version))) {
190LOG_WARN("fail to gen new schema_version", K(ret));
191} else {
192ObTenantStatus tenant_status = TENANT_STATUS_NORMAL;
193if (0 < tenant_schema.get_drop_tenant_time()) {
194// A dropping status tenant is created when standby cluster load snapshot.
195tenant_status = TENANT_STATUS_DROPPING;
196} else if (OB_DDL_ADD_TENANT_START == op) {
197if (tenant_schema.is_restore_tenant_status()) {
198tenant_status = TENANT_STATUS_RESTORE;
199} else if (tenant_schema.is_creating_standby_tenant_status()) {
200tenant_status = TENANT_STATUS_CREATING_STANDBY;
201} else {
202tenant_status = TENANT_STATUS_CREATING;
203}
204}
205tenant_schema.set_schema_version(new_schema_version);
206tenant_schema.set_status(tenant_status);
207if (OB_FAIL(schema_service->get_tenant_sql_service().insert_tenant(
208tenant_schema, op, trans, ddl_stmt_str))) {
209LOG_WARN("insert tenant failed", K(tenant_schema), K(ret));
210}
211}
212LOG_INFO("create tenant", K(ret), "tenant_id", tenant_schema.get_tenant_id(),
213"cost", ObTimeUtility::current_time() - start);
214return ret;
215}
216
217int ObDDLOperator::insert_tenant_merge_info(
218const ObSchemaOperationType op,
219const ObTenantSchema &tenant_schema,
220ObMySQLTransaction &trans)
221{
222int ret = OB_SUCCESS;
223const uint64_t tenant_id = tenant_schema.get_tenant_id();
224if (is_sys_tenant(tenant_id) || is_meta_tenant(tenant_id)) {
225// add zone merge info
226if ((OB_DDL_ADD_TENANT_START == op) || (OB_DDL_ADD_TENANT == op)) {
227HEAP_VARS_4((ObGlobalMergeInfo, global_info),
228(ObZoneMergeInfoArray, merge_info_array),
229(ObZoneArray, zone_list),
230(ObZoneMergeInfo, tmp_merge_info)) {
231
232global_info.tenant_id_ = tenant_id;
233tmp_merge_info.tenant_id_ = tenant_id;
234if (OB_FAIL(tenant_schema.get_zone_list(zone_list))) {
235LOG_WARN("fail to get zone list", KR(ret));
236}
237
238for (int64_t i = 0; (i < zone_list.count()) && OB_SUCC(ret); ++i) {
239tmp_merge_info.zone_ = zone_list.at(i);
240if (OB_FAIL(merge_info_array.push_back(tmp_merge_info))) {
241LOG_WARN("fail to push_back", KR(ret));
242}
243}
244// add zone merge info of current tenant(sys tenant or meta tenant)
245if (OB_SUCC(ret)) {
246if (OB_FAIL(ObGlobalMergeTableOperator::insert_global_merge_info(trans,
247tenant_id, global_info))) {
248LOG_WARN("fail to insert global merge info of current tenant", KR(ret), K(global_info));
249} else if (OB_FAIL(ObZoneMergeTableOperator::insert_zone_merge_infos(
250trans, tenant_id, merge_info_array))) {
251LOG_WARN("fail to insert zone merge infos of current tenant", KR(ret), K(tenant_id),
252K(merge_info_array));
253}
254}
255// add zone merge info of relative user tenant if current tenant is meta tenant
256if (OB_SUCC(ret) && is_meta_tenant(tenant_id)) {
257const uint64_t user_tenant_id = gen_user_tenant_id(tenant_id);
258global_info.tenant_id_ = user_tenant_id;
259for (int64_t i = 0; i < merge_info_array.count(); ++i) {
260merge_info_array.at(i).tenant_id_ = user_tenant_id;
261}
262if (OB_FAIL(ObGlobalMergeTableOperator::insert_global_merge_info(trans,
263user_tenant_id, global_info))) {
264LOG_WARN("fail to insert global merge info of user tenant", KR(ret), K(global_info));
265} else if (OB_FAIL(ObZoneMergeTableOperator::insert_zone_merge_infos(
266trans, user_tenant_id, merge_info_array))) {
267LOG_WARN("fail to insert zone merge infos of user tenant", KR(ret), K(user_tenant_id),
268K(merge_info_array));
269}
270}
271}
272}
273}
274
275return ret;
276}
277
278
279int ObDDLOperator::drop_tenant(const uint64_t tenant_id,
280ObMySQLTransaction &trans,
281const ObString *ddl_stmt_str/*=NULL*/)
282{
283int ret = OB_SUCCESS;
284int64_t new_schema_version = OB_INVALID_VERSION;
285ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
286
287if (OB_ISNULL(schema_service_impl)) {
288ret = OB_ERR_SYS;
289LOG_ERROR("schema_service_impl must not null");
290} else if (OB_FAIL(schema_service_.gen_new_schema_version(OB_SYS_TENANT_ID, new_schema_version))) {
291LOG_WARN("fail to gen new schema_version", K(ret));
292} else if (OB_FAIL(schema_service_impl->get_tenant_sql_service().delete_tenant(
293tenant_id, new_schema_version, trans, ddl_stmt_str))) {
294LOG_WARN("delete tenant failed", K(tenant_id), K(ret));
295}
296return ret;
297}
298
299int ObDDLOperator::delay_to_drop_tenant(
300ObTenantSchema &new_tenant_schema,
301ObMySQLTransaction &trans,
302const ObString *ddl_stmt_str/*=NULL*/)
303{
304int ret = OB_SUCCESS;
305int64_t new_schema_version = OB_INVALID_VERSION;
306ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
307const uint64_t tenant_id = new_tenant_schema.get_tenant_id();
308const int64_t timeout = THIS_WORKER.get_timeout_remain();
309if (OB_ISNULL(schema_service_impl)) {
310ret = OB_ERR_SYS;
311LOG_ERROR("schema_service_impl must not null");
312} else if (OB_FAIL(schema_service_.gen_new_schema_version(
313OB_SYS_TENANT_ID, new_schema_version))) {
314LOG_WARN("fail to gen new schema_version", K(ret));
315} else {
316new_tenant_schema.set_schema_version(new_schema_version);
317// FIXME: after 4.0, drop_tenant_time is not needed by backup&restore anymore.
318// So just record local timestmap for drop tenant.
319new_tenant_schema.set_drop_tenant_time(ObTimeUtility::current_time());
320new_tenant_schema.set_status(TENANT_STATUS_DROPPING);
321new_tenant_schema.set_in_recyclebin(false);
322if (OB_FAIL(schema_service_impl->get_tenant_sql_service().delay_to_drop_tenant(
323new_tenant_schema, trans, ddl_stmt_str))) {
324LOG_WARN("schema_service_impl delay to drop tenant failed", K(new_tenant_schema), K(ret));
325}
326}
327return ret;
328}
329
330int ObDDLOperator::drop_tenant_to_recyclebin(
331ObSqlString &new_tenant_name,
332ObTenantSchema &tenant_schema,
333ObMySQLTransaction &trans,
334const ObString *ddl_stmt_str/*=NULL*/)
335{
336int ret = OB_SUCCESS;
337const uint64_t tenant_id = tenant_schema.get_tenant_id();
338int64_t new_schema_version = OB_INVALID_VERSION;
339ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
340ObTenantSchema new_tenant_schema;
341if (OB_FAIL(new_tenant_schema.assign(tenant_schema))) {
342LOG_WARN("fail to assign", K(ret));
343} else if (OB_ISNULL(schema_service_impl)) {
344ret = OB_ERR_UNEXPECTED;
345LOG_ERROR("schema_service_impl must not null", K(ret));
346} else if (OB_INVALID_ID == tenant_id) {
347ret = OB_INVALID_ARGUMENT;
348LOG_WARN("tenant_id is invalid", K(ret), K(tenant_id));
349} else {
350ObRecycleObject recycle_object;
351recycle_object.set_original_name(tenant_schema.get_tenant_name_str());
352recycle_object.set_type(ObRecycleObject::TENANT);
353recycle_object.set_tenant_id(tenant_schema.get_tenant_id());
354// table_id is not usefull for recycled tenant, mock table_id as original tenant_id for DBA_RECYCLEBIN
355recycle_object.set_table_id(tenant_schema.get_tenant_id());
356recycle_object.set_tablegroup_id(OB_INVALID_ID);
357new_tenant_schema.set_in_recyclebin(true);
358if (OB_FAIL(schema_service_.gen_new_schema_version(OB_SYS_TENANT_ID, new_schema_version))) {
359LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
360} else if (FALSE_IT(new_tenant_schema.set_schema_version(new_schema_version))) {
361} else if (OB_FAIL(new_tenant_schema.set_tenant_name(new_tenant_name.string()))) {
362LOG_WARN("set tenant name failed", K(ret));
363} else if (FALSE_IT(recycle_object.set_object_name(new_tenant_name.string()))) {
364LOG_WARN("fail to set object name", K(ret));
365}
366if (OB_SUCC(ret)) {
367if (OB_FAIL(schema_service_impl->insert_recyclebin_object(recycle_object,
368trans))) {
369LOG_WARN("insert recycle object failed", K(ret));
370} else if (OB_FAIL(schema_service_impl->get_tenant_sql_service().drop_tenant_to_recyclebin(
371new_tenant_schema, trans, OB_DDL_DROP_TENANT_TO_RECYCLEBIN, ddl_stmt_str))) {
372LOG_WARN("alter_tenant failed,", K(ret));
373}
374}
375}
376return ret;
377}
378
379int ObDDLOperator::check_tenant_exist(share::schema::ObSchemaGetterGuard &schema_guard,
380const ObString &tenant_name,
381bool &is_exist)
382{
383int ret = OB_SUCCESS;
384is_exist = false;
385const ObTenantSchema *tenant = NULL;
386if (OB_FAIL(schema_guard.get_tenant_info(tenant_name, tenant))) {
387LOG_WARN("fail get tenant info", K(ret));
388} else if (OB_ISNULL(tenant)) {
389is_exist = false;
390} else {
391is_exist = true;
392}
393return ret;
394}
395
396int ObDDLOperator::flashback_tenant_from_recyclebin(
397const share::schema::ObTenantSchema &tenant_schema,
398ObMySQLTransaction &trans,
399const ObString &new_tenant_name,
400share::schema::ObSchemaGetterGuard &schema_guard,
401const ObString &ddl_stmt_str)
402{
403int ret = OB_SUCCESS;
404ObSchemaService *schema_service = schema_service_.get_schema_service();
405ObArray<ObRecycleObject> recycle_objs;
406ObTenantSchema new_tenant_schema;
407ObString final_tenant_name;
408if (OB_ISNULL(schema_service)) {
409ret = OB_ERR_UNEXPECTED;
410LOG_WARN("schema_service should not be null", K(ret));
411} else if (OB_INVALID_ID == tenant_schema.get_tenant_id()) {
412ret = OB_INVALID_ARGUMENT;
413LOG_WARN("tenant_id is invalid", K(ret));
414} else if (OB_FAIL(schema_service->fetch_recycle_object(
415OB_SYS_TENANT_ID,
416tenant_schema.get_tenant_name(),
417ObRecycleObject::TENANT,
418trans,
419recycle_objs))) {
420LOG_WARN("get_recycle_object failed", K(ret));
421} else if (recycle_objs.size() != 1) {
422ret = OB_ERR_UNEXPECTED;
423LOG_WARN("unexpected recycle object num", K(ret),
424"tenant_name", tenant_schema.get_tenant_name_str(),
425"size", recycle_objs.size());
426} else if (OB_FAIL(new_tenant_schema.assign(tenant_schema))) {
427LOG_WARN("fail to assign", K(ret));
428} else {
429const ObRecycleObject &recycle_obj = recycle_objs.at(0);
430if (new_tenant_name.empty()) {
431final_tenant_name = recycle_obj.get_original_name();
432} else {
433final_tenant_name = new_tenant_name;
434}
435new_tenant_schema.set_in_recyclebin(false);
436if (OB_FAIL(new_tenant_schema.set_tenant_name(final_tenant_name))) {
437LOG_WARN("set tenant name failed", K(final_tenant_name));
438}
439if (OB_SUCC(ret)) {
440bool is_tenant_exist = false;
441int64_t new_schema_version = OB_INVALID_VERSION;
442if (OB_FAIL(check_tenant_exist(schema_guard,
443new_tenant_schema.get_tenant_name_str(),
444is_tenant_exist))) {
445LOG_WARN("fail to check tenant", K(ret));
446} else if (is_tenant_exist) {
447ret = OB_TENANT_EXIST;
448LOG_USER_ERROR(OB_TENANT_EXIST, new_tenant_schema.get_tenant_name_str().ptr());
449} else if (OB_FAIL(schema_service_.gen_new_schema_version(OB_SYS_TENANT_ID,
450new_schema_version))) {
451LOG_WARN("fail to gen new schema_version", K(ret), K(OB_SYS_TENANT_ID));
452} else if (FALSE_IT(new_tenant_schema.set_schema_version(new_schema_version))) {
453} else if (OB_FAIL(schema_service->get_tenant_sql_service().alter_tenant(
454new_tenant_schema,
455trans,
456OB_DDL_FLASHBACK_TENANT,
457&ddl_stmt_str))) {
458LOG_WARN("update_tenant failed", K(ret), K(new_tenant_schema));
459} else if (OB_FAIL(schema_service->delete_recycle_object(
460OB_SYS_TENANT_ID,
461recycle_obj,
462trans))) {
463LOG_WARN("delete_recycle_object failed", K(ret), K(recycle_obj));
464}
465}
466}
467return ret;
468}
469
470int ObDDLOperator::purge_tenant_in_recyclebin(const share::schema::ObTenantSchema &tenant_schema,
471ObMySQLTransaction &trans,
472const ObString *ddl_stmt_str)
473{
474int ret = OB_SUCCESS;
475ObSchemaService *schema_service = schema_service_.get_schema_service();
476if (OB_ISNULL(schema_service)) {
477ret = OB_ERR_UNEXPECTED;
478LOG_WARN("schema_service should not be null", K(ret));
479} else if (OB_INVALID_ID == tenant_schema.get_tenant_id()) {
480ret = OB_INVALID_ARGUMENT;
481LOG_WARN("tenant_id is invalid", K(ret));
482} else {
483ObArray<ObRecycleObject> recycle_objs;
484if (OB_FAIL(schema_service->fetch_recycle_object(
485OB_SYS_TENANT_ID,
486tenant_schema.get_tenant_name_str(),
487ObRecycleObject::TENANT,
488trans,
489recycle_objs))) {
490LOG_WARN("get_recycle_object failed", K(ret));
491} else if (recycle_objs.size() != 1) {
492ret = OB_ERR_UNEXPECTED;
493LOG_WARN("unexpected recycle object num", K(ret),
494"tenant_name", tenant_schema.get_tenant_name_str(),
495"size", recycle_objs.size());
496} else {
497const ObRecycleObject &recycle_obj = recycle_objs.at(0);
498share::schema::ObTenantSchema new_tenant_schema;
499if (OB_FAIL(new_tenant_schema.assign(tenant_schema))) {
500LOG_WARN("fail to assign", K(ret));
501} else if (OB_FAIL(delay_to_drop_tenant(
502new_tenant_schema,
503trans,
504ddl_stmt_str))) {
505LOG_WARN("drop_table failed", K(ret));
506} else if (OB_FAIL(schema_service->delete_recycle_object(
507OB_SYS_TENANT_ID,
508recycle_obj,
509trans))) {
510LOG_WARN("delete_recycle_object failed", K(ret));
511}
512}
513}
514return ret;
515}
516
517int ObDDLOperator::rename_tenant(
518share::schema::ObTenantSchema &tenant_schema,
519common::ObMySQLTransaction &trans,
520const common::ObString *ddl_stmt_str /* = NULL */)
521{
522int ret = OB_SUCCESS;
523int64_t new_schema_version = OB_INVALID_VERSION;
524ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
525
526if (OB_ISNULL(schema_service_impl)) {
527ret = OB_ERR_SYS;
528LOG_ERROR("schema_service_impl must not null");
529} else if (!tenant_schema.is_valid()) {
530ret = OB_INVALID_ARGUMENT;
531LOG_WARN("invalid tenant schema", K(tenant_schema), K(ret));
532} else if (OB_FAIL(schema_service_.gen_new_schema_version(
533OB_SYS_TENANT_ID,
534new_schema_version))) {
535LOG_WARN("fail to gen new schema_version", K(ret));
536} else {
537tenant_schema.set_schema_version(new_schema_version);
538if (OB_FAIL(schema_service_impl->get_tenant_sql_service().rename_tenant(
539tenant_schema, trans, ddl_stmt_str))) {
540LOG_WARN("rename tenant failed", K(ret), K(tenant_schema));
541}
542}
543return ret;
544}
545
546int ObDDLOperator::alter_tenant(ObTenantSchema &tenant_schema,
547ObMySQLTransaction &trans,
548const ObString *ddl_stmt_str/*=NULL*/)
549{
550int ret = OB_SUCCESS;
551int64_t new_schema_version = OB_INVALID_VERSION;
552ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
553
554if (OB_ISNULL(schema_service_impl)) {
555ret = OB_ERR_SYS;
556LOG_ERROR("schema_service_impl must not null");
557} else if (!tenant_schema.is_valid()) {
558ret = OB_INVALID_ARGUMENT;
559LOG_WARN("invalid tenant schema", K(tenant_schema), K(ret));
560} else if (OB_FAIL(schema_service_.gen_new_schema_version(OB_SYS_TENANT_ID, new_schema_version))) {
561LOG_WARN("fail to gen new schema_version", K(ret));
562} else {
563tenant_schema.set_schema_version(new_schema_version);
564const ObSchemaOperationType op = OB_DDL_ALTER_TENANT;
565if (OB_FAIL(schema_service_impl->get_tenant_sql_service().alter_tenant(
566tenant_schema, trans, op, ddl_stmt_str))) {
567LOG_WARN("schema_service_impl alter_tenant failed", K(tenant_schema), K(ret));
568}
569}
570return ret;
571}
572
573int ObDDLOperator::replace_sys_variable(ObSysVariableSchema &sys_variable_schema,
574const int64_t schema_version,
575ObMySQLTransaction &trans,
576const ObSchemaOperationType &operation_type,
577const common::ObString *ddl_stmt_str)
578{
579int ret = OB_SUCCESS;
580int64_t start = ObTimeUtility::current_time();
581sys_variable_schema.set_schema_version(schema_version);
582ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
583
584if (schema_version < 0) {
585ret = OB_INVALID_ARGUMENT;
586LOG_WARN("invalid schema_version", K(ret), K(schema_version));
587} else if (OB_ISNULL(schema_service_impl)) {
588ret = OB_ERR_SYS;
589LOG_ERROR("schema_service_impl must not null");
590} else if (OB_FAIL(schema_service_impl->get_sys_variable_sql_service()
591.replace_sys_variable(sys_variable_schema, trans, operation_type, ddl_stmt_str))) {
592LOG_WARN("schema_service_impl update sys variable failed", K(sys_variable_schema), K(operation_type), K(ret));
593}
594LOG_INFO("replace sys variable", K(ret),
595"tenant_id", sys_variable_schema.get_tenant_id(),
596"cost", ObTimeUtility::current_time() - start);
597return ret;
598}
599
600int ObDDLOperator::create_database(ObDatabaseSchema &database_schema,
601ObMySQLTransaction &trans,
602const ObString *ddl_stmt_str/*=NULL*/)
603{
604int ret = OB_SUCCESS;
605//set the old database id
606const uint64_t tenant_id = database_schema.get_tenant_id();
607int64_t new_schema_version = OB_INVALID_VERSION;
608uint64_t new_database_id = database_schema.get_database_id();
609ObSchemaService *schema_service = schema_service_.get_schema_service();
610
611if (OB_ISNULL(schema_service)) {
612ret = OB_ERR_SYS;
613LOG_ERROR("schema_service must not null");
614} else if (OB_FAIL(schema_service->fetch_new_database_id(
615database_schema.get_tenant_id(), new_database_id))) {
616LOG_WARN("fetch new database id failed", K(database_schema.get_tenant_id()),
617K(ret));
618} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
619LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
620} else {
621database_schema.set_database_id(new_database_id);
622database_schema.set_schema_version(new_schema_version);
623if (OB_FAIL(schema_service->get_database_sql_service().insert_database(
624database_schema, trans, ddl_stmt_str))) {
625LOG_WARN("insert database failed", K(database_schema), K(ret));
626}
627}
628return ret;
629}
630
631int ObDDLOperator::alter_database(ObDatabaseSchema &new_database_schema,
632ObMySQLTransaction &trans,
633const ObSchemaOperationType op_type,
634const ObString *ddl_stmt_str/*=NULL*/,
635const bool need_update_schema_version/*=true*/)
636{
637int ret = OB_SUCCESS;
638ObSchemaService *schema_service = schema_service_.get_schema_service();
639if (OB_ISNULL(schema_service)) {
640ret = OB_ERR_SYS;
641RS_LOG(ERROR, "schema_service must not null");
642} else {
643if (need_update_schema_version) {
644const uint64_t tenant_id = new_database_schema.get_tenant_id();
645int64_t new_schema_version = OB_INVALID_VERSION;
646if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
647LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
648} else {
649new_database_schema.set_schema_version(new_schema_version);
650}
651}
652if (OB_FAIL(ret)) {
653} else if (OB_FAIL(schema_service->get_database_sql_service()
654.update_database(new_database_schema,
655trans,
656op_type,
657ddl_stmt_str))) {
658RS_LOG(WARN, "update database failed", K(new_database_schema), K(ret));
659}
660}
661return ret;
662}
663
664int ObDDLOperator::drop_database(const ObDatabaseSchema &db_schema,
665ObMySQLTransaction &trans,
666const ObString *ddl_stmt_str/*=NULL*/)
667{
668int ret = OB_SUCCESS;
669ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
670const uint64_t tenant_id = db_schema.get_tenant_id();
671const uint64_t database_id = db_schema.get_database_id();
672int64_t new_schema_version = OB_INVALID_VERSION;
673
674if (OB_ISNULL(schema_service_impl)) {
675ret = OB_ERR_SYS;
676LOG_ERROR("schama service_impl and schema manage must not null",
677"schema_service_impl", OB_P(schema_service_impl), K(ret));
678}
679//drop tables in recyclebin
680if (OB_SUCC(ret)) {
681if (OB_FAIL(purge_table_of_database(db_schema, trans))) {
682LOG_WARN("purge_table_in_db failed", K(ret));
683}
684}
685
686//delete triggers in database, 只删除trigger_database != base_table_database 的trigger
687// trigger_database == base_table_database 的trigger会在下面删除表的时候删除
688if (OB_SUCC(ret)) {
689ObArray<uint64_t> trigger_ids;
690ObSchemaGetterGuard schema_guard;
691if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
692LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
693} else if (OB_FAIL(schema_guard.get_trigger_ids_in_database(tenant_id, database_id, trigger_ids))) {
694LOG_WARN("get trigger infos in database failed", KR(ret), K(tenant_id), K(database_id));
695} else {
696for (int64_t i = 0; OB_SUCC(ret) && i < trigger_ids.count(); i++) {
697const ObTriggerInfo *tg_info = NULL;
698const uint64_t trigger_id = trigger_ids.at(i);
699if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
700LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
701} else if (OB_FAIL(schema_guard.get_trigger_info(tenant_id, trigger_id, tg_info))) {
702LOG_WARN("fail to get trigger info", KR(ret), K(tenant_id), K(trigger_id));
703} else if (OB_ISNULL(tg_info)) {
704ret = OB_ERR_UNEXPECTED;
705LOG_WARN("trigger info is NULL", K(ret));
706} else {
707const ObSimpleTableSchemaV2 * tbl_schema = NULL;
708OZ (schema_guard.get_simple_table_schema(tenant_id, tg_info->get_base_object_id(), tbl_schema));
709CK (OB_NOT_NULL(tbl_schema));
710if (OB_SUCC(ret) && database_id != tbl_schema->get_database_id()) {
711OZ (drop_trigger(*tg_info, trans, NULL));
712}
713}
714}
715}
716}
717
718// delete tables in database
719if (OB_SUCC(ret)) {
720ObArray<uint64_t> table_ids;
721ObSchemaGetterGuard schema_guard;
722if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
723LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
724} else if (OB_FAIL(schema_guard.get_table_ids_in_database(tenant_id, database_id, table_ids))) {
725LOG_WARN("get tables in database failed", K(tenant_id), KT(database_id), K(ret));
726} else {
727// drop index tables first
728for (int64_t cycle = 0; OB_SUCC(ret) && cycle < 2; ++cycle) {
729for (int64_t i = 0; OB_SUCC(ret) && i < table_ids.count(); ++i) {
730const ObTableSchema *table = NULL;
731const uint64_t table_id = table_ids.at(i);
732if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
733LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
734} else if (OB_FAIL(schema_guard.get_table_schema(tenant_id, table_id, table))) {
735LOG_WARN("fail to get table schema", KR(ret), K(tenant_id), K(table_id));
736} else if (OB_ISNULL(table)) {
737ret = OB_ERR_UNEXPECTED;
738LOG_WARN("table is NULL", K(ret));
739} else if (table->is_in_recyclebin()) {
740// already been dropped before
741} else {
742bool is_delete_first = table->is_aux_table() || table->is_mlog_table();
743if ((0 == cycle ? is_delete_first : !is_delete_first)) {
744// drop triggers before drop table
745if (OB_FAIL(drop_trigger_cascade(*table, trans))) {
746LOG_WARN("drop trigger failed", K(ret), K(table->get_table_id()));
747} else if (OB_FAIL(drop_table(*table, trans, NULL, false, NULL, true))) {
748LOG_WARN("drop table failed", K(ret), K(table->get_table_id()));
749}
750}
751}
752}
753}
754}
755}
756
757// delete outlines in database
758if (OB_SUCC(ret)) {
759ObArray<const ObSimpleOutlineSchema *> outline_schemas;
760ObSchemaGetterGuard schema_guard;
761if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
762LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
763} else if (OB_FAIL(schema_guard.get_simple_outline_schemas_in_database(tenant_id, database_id, outline_schemas))) {
764LOG_WARN("get outlines in database failed", K(tenant_id), KT(database_id), K(ret));
765} else {
766for (int64_t i = 0; OB_SUCC(ret) && i < outline_schemas.count(); ++i) {
767const ObSimpleOutlineSchema *outline_schema = outline_schemas.at(i);
768if (OB_ISNULL(outline_schema)) {
769ret = OB_ERR_UNEXPECTED;
770LOG_WARN("outline info is NULL", K(ret));
771} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
772LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
773} else if (OB_FAIL(schema_service_impl->get_outline_sql_service().delete_outline(tenant_id,
774database_id,
775outline_schema->get_outline_id(),
776new_schema_version, trans))) {
777LOG_WARN("drop outline failed", KR(ret), "outline_id", outline_schema->get_outline_id());
778}
779}
780}
781}
782// delete synonyms in database
783if (OB_SUCC(ret)) {
784ObSchemaGetterGuard schema_guard;
785ObArray<const ObSimpleSynonymSchema *> synonym_schemas;
786if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
787LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
788} else if (OB_FAIL(schema_guard.get_simple_synonym_schemas_in_database(tenant_id, database_id, synonym_schemas))) {
789LOG_WARN("get synonyms in database failed", K(tenant_id), KT(database_id), K(ret));
790} else {
791for (int64_t i = 0; OB_SUCC(ret) && i < synonym_schemas.count(); ++i) {
792const ObSimpleSynonymSchema *synonym_schema = synonym_schemas.at(i);
793if (OB_ISNULL(synonym_schema)) {
794ret = OB_ERR_UNEXPECTED;
795LOG_WARN("synonym info is NULL", K(ret));
796} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
797LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
798} else if (OB_FAIL(schema_service_impl->get_synonym_sql_service().delete_synonym(tenant_id,
799database_id,
800synonym_schema->get_synonym_id(),
801new_schema_version, &trans))) {
802LOG_WARN("drop synonym failed", KR(ret), "synonym_id", synonym_schema->get_synonym_id());
803}
804}
805}
806}
807
808// delete packages in database
809if (OB_SUCC(ret)) {
810ObSchemaGetterGuard schema_guard;
811ObArray<const ObSimplePackageSchema*> package_schemas;
812if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
813LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
814} else if (OB_FAIL(schema_guard.get_simple_package_schemas_in_database(tenant_id, database_id, package_schemas))) {
815LOG_WARN("get packages in database failed", K(tenant_id), KT(database_id), K(ret));
816} else {
817ObArray<const ObSAuditSchema *> audits;
818common::ObSqlString public_sql_string;
819for (int64_t i = 0; OB_SUCC(ret) && i < package_schemas.count(); ++i) {
820const ObSimplePackageSchema *package_schema = package_schemas.at(i);
821if (OB_ISNULL(package_schema)) {
822ret = OB_ERR_UNEXPECTED;
823LOG_WARN("package info is NULL", K(ret));
824} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
825LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
826} else if (OB_FAIL(schema_service_impl->get_routine_sql_service().drop_package(
827package_schema->get_tenant_id(),
828package_schema->get_database_id(),
829package_schema->get_package_id(),
830new_schema_version, trans))) {
831LOG_WARN("drop package failed", KR(ret), "package_id", package_schema->get_package_id());
832} else {
833// delete audit in package
834audits.reuse();
835public_sql_string.reuse();
836if (OB_FAIL(schema_guard.get_audit_schema_in_owner(tenant_id,
837AUDIT_PACKAGE,
838package_schema->get_package_id(),
839audits))) {
840LOG_WARN("get get_audit_schema_in_owner failed", K(tenant_id), K(ret));
841} else if (!audits.empty()) {
842for (int64_t i = 0; OB_SUCC(ret) && i < audits.count(); ++i) {
843const ObSAuditSchema *audit_schema = audits.at(i);
844if (OB_ISNULL(audit_schema)) {
845ret = OB_ERR_UNEXPECTED;
846LOG_WARN("audit_schema is NULL", K(ret));
847} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
848LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
849} else if (OB_FAIL(schema_service_impl->get_audit_sql_service().handle_audit_metainfo(
850*audit_schema,
851AUDIT_MT_DEL,
852false,
853new_schema_version,
854NULL,
855trans,
856public_sql_string))) {
857LOG_WARN("drop audit_schema failed", KPC(audit_schema), K(ret));
858} else {
859LOG_INFO("succ to delete audit_schema from drop package", KPC(audit_schema));
860}
861}
862} else {
863LOG_DEBUG("no need to delete audit_schema from drop package", K(audits), KPC(package_schema));
864}
865}
866}
867}
868}
869
870// delete routines in database
871if (OB_SUCC(ret)) {
872ObArray<uint64_t> routine_ids;
873ObSchemaGetterGuard schema_guard;
874if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
875LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
876} else if (OB_FAIL(schema_guard.get_routine_ids_in_database(tenant_id, database_id, routine_ids))) {
877LOG_WARN("get routines in database failed", K(tenant_id), KT(database_id), K(ret));
878} else {
879ObArray<const ObSAuditSchema *> audits;
880common::ObSqlString public_sql_string;
881for (int64_t i = 0; OB_SUCC(ret) && i < routine_ids.count(); ++i) {
882const ObRoutineInfo *routine_info = NULL;
883const uint64_t routine_id = routine_ids.at(i);
884int64_t new_schema_version = OB_INVALID_VERSION;
885if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
886LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
887} else if (OB_FAIL(schema_guard.get_routine_info(tenant_id, routine_id, routine_info))) {
888LOG_WARN("fail to get routine with id", KR(ret), K(tenant_id), K(routine_id));
889} else if (OB_ISNULL(routine_info)) {
890ret = OB_ERR_UNEXPECTED;
891LOG_WARN("routine info is NULL", K(ret));
892} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
893LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
894} else if (OB_FAIL(schema_service_impl->get_routine_sql_service().drop_routine(
895*routine_info, new_schema_version, trans))) {
896LOG_WARN("drop routine failed", KR(ret), "routine_id", routine_id);
897} else {
898// delete audit in routine
899audits.reuse();
900public_sql_string.reuse();
901if (OB_FAIL(schema_guard.get_audit_schema_in_owner(tenant_id,
902AUDIT_PROCEDURE,
903routine_id,
904audits))) {
905LOG_WARN("get get_audit_schema_in_owner failed", K(tenant_id), K(ret));
906} else if (!audits.empty()) {
907for (int64_t i = 0; OB_SUCC(ret) && i < audits.count(); ++i) {
908const ObSAuditSchema *audit_schema = audits.at(i);
909if (OB_ISNULL(audit_schema)) {
910ret = OB_ERR_UNEXPECTED;
911LOG_WARN("audit_schema is NULL", K(ret));
912} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
913LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
914} else if (OB_FAIL(schema_service_impl->get_audit_sql_service().handle_audit_metainfo(
915*audit_schema,
916AUDIT_MT_DEL,
917false,
918new_schema_version,
919NULL,
920trans,
921public_sql_string))) {
922LOG_WARN("drop audit_schema failed", KPC(audit_schema), K(ret));
923} else {
924LOG_INFO("succ to delete audit_schema from drop package", KPC(audit_schema));
925}
926}
927} else {
928LOG_DEBUG("no need to delete audit_schema from drop package", K(audits), KPC(routine_info));
929}
930}
931}
932}
933}
934
935// delete udts in database
936if (OB_SUCC(ret)) {
937ObArray<uint64_t> udt_ids;
938ObSchemaGetterGuard schema_guard;
939if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
940LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
941} else if (OB_FAIL(schema_guard.get_udt_ids_in_database(tenant_id, database_id, udt_ids))) {
942LOG_WARN("get udts in database failed", K(tenant_id), KT(database_id), K(ret));
943} else {
944for (int64_t i = 0; OB_SUCC(ret) && i < udt_ids.count(); ++i) {
945const ObUDTTypeInfo *udt_info = NULL;
946const uint64_t udt_id = udt_ids.at(i);
947int64_t new_schema_version = OB_INVALID_VERSION;
948if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
949LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
950} else if (OB_FAIL(schema_guard.get_udt_info(tenant_id, udt_id, udt_info))) {
951LOG_WARN("fail to get routine with id", KR(ret), K(tenant_id), K(udt_id));
952} else if (OB_ISNULL(udt_info)) {
953ret = OB_ERR_UNEXPECTED;
954LOG_WARN("routine info is NULL", K(ret));
955} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
956LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
957} else if (OB_FAIL(schema_service_impl->get_udt_sql_service().drop_udt(
958*udt_info, new_schema_version, trans))) {
959LOG_WARN("drop routine failed", "routine_id", udt_info->get_type_id(), K(ret));
960}
961}
962}
963}
964
965// delete sequences in database
966if (OB_SUCC(ret)) {
967ObSchemaGetterGuard schema_guard;
968ObArray<const ObSequenceSchema*> sequence_schemas;
969if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
970LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
971} else if (OB_FAIL(schema_guard.get_sequence_schemas_in_database(tenant_id,
972database_id,
973sequence_schemas))) {
974LOG_WARN("get sequences in database failed",
975K(tenant_id), KT(database_id), K(ret));
976} else {
977ObArray<const ObSAuditSchema *> audits;
978common::ObSqlString public_sql_string;
979for (int64_t i = 0; OB_SUCC(ret) && i < sequence_schemas.count(); ++i) {
980const ObSequenceSchema *sequence_schema = sequence_schemas.at(i);
981if (OB_ISNULL(sequence_schema)) {
982ret = OB_ERR_UNEXPECTED;
983LOG_WARN("sequence info is NULL", K(ret));
984} else if (sequence_schema->get_is_system_generated()) {
985continue;
986} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
987LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
988} else if (OB_FAIL(schema_service_impl
989->get_sequence_sql_service()
990.drop_sequence(*sequence_schema, new_schema_version, &trans))) {
991LOG_WARN("drop sequence failed",
992KR(ret), K(*sequence_schema));
993} else {
994// delete audit in table
995audits.reuse();
996public_sql_string.reuse();
997if (OB_FAIL(schema_guard.get_audit_schema_in_owner(tenant_id,
998AUDIT_SEQUENCE,
999sequence_schema->get_sequence_id(),
1000audits))) {
1001LOG_WARN("get get_audit_schema_in_owner failed", K(tenant_id), K(ret));
1002} else if (!audits.empty()) {
1003ObSchemaService *schema_service = schema_service_.get_schema_service();
1004for (int64_t i = 0; OB_SUCC(ret) && i < audits.count(); ++i) {
1005const ObSAuditSchema *audit_schema = audits.at(i);
1006if (OB_ISNULL(audit_schema)) {
1007ret = OB_ERR_UNEXPECTED;
1008LOG_WARN("audit_schema is NULL", K(ret));
1009} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))){
1010LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
1011} else if (OB_FAIL(schema_service->get_audit_sql_service().handle_audit_metainfo(
1012*audit_schema,
1013AUDIT_MT_DEL,
1014false,
1015new_schema_version,
1016NULL,
1017trans,
1018public_sql_string))) {
1019LOG_WARN("drop audit_schema failed", KPC(audit_schema), K(ret));
1020} else {
1021LOG_INFO("succ to delete audit_schema from drop sequence", KPC(audit_schema));
1022}
1023}
1024} else {
1025LOG_DEBUG("no need to delete audit_schema from drop sequence", K(audits), KPC(sequence_schema));
1026}
1027}
1028}
1029}
1030}
1031
1032// delete mock_fk_parent_tables in database
1033if (OB_SUCC(ret)) {
1034ObSchemaGetterGuard schema_guard;
1035ObArray<uint64_t> mock_fk_parent_table_ids;
1036if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
1037LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
1038} else if (OB_FAIL(schema_guard.get_mock_fk_parent_table_ids_in_database(tenant_id, database_id, mock_fk_parent_table_ids))) {
1039LOG_WARN("fail to get mock_fk_parent_table_schemas in database", K(ret), K(tenant_id), K(database_id));
1040} else {
1041ObArray<ObMockFKParentTableSchema> mock_fk_parent_table_schema_array;
1042int64_t new_schema_version = OB_INVALID_VERSION;
1043if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
1044LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
1045}
1046for (int64_t i = 0; OB_SUCC(ret) && i < mock_fk_parent_table_ids.count(); ++i) {
1047ObMockFKParentTableSchema tmp_mock_fk_parent_table_schema;
1048const uint64_t mock_fk_parent_table_id = mock_fk_parent_table_ids.at(i);
1049const ObMockFKParentTableSchema *mock_fk_parent_table_schema = NULL;
1050if (OB_FAIL(schema_guard.get_mock_fk_parent_table_schema_with_id(tenant_id,
1051mock_fk_parent_table_id,
1052mock_fk_parent_table_schema))) {
1053LOG_WARN("fail to get mock fk parent table schema", KR(ret), K(tenant_id), K(mock_fk_parent_table_id));
1054} else if (OB_ISNULL(mock_fk_parent_table_schema)) {
1055ret = OB_ERR_UNEXPECTED;
1056LOG_WARN("mock fk parent table schema is NULL", KR(ret), K(tenant_id), K(mock_fk_parent_table_id));
1057} else if (OB_FAIL(tmp_mock_fk_parent_table_schema.assign(*mock_fk_parent_table_schema))) {
1058LOG_WARN("fail to assign mock_fk_parent_table_schema", K(ret), K(tenant_id), K(database_id), KPC(mock_fk_parent_table_schema));
1059} else if (FALSE_IT(tmp_mock_fk_parent_table_schema.set_schema_version(new_schema_version))) {
1060} else if (FALSE_IT(tmp_mock_fk_parent_table_schema.set_operation_type(ObMockFKParentTableOperationType::MOCK_FK_PARENT_TABLE_OP_DROP_TABLE))) {
1061} else if (OB_FAIL(mock_fk_parent_table_schema_array.push_back(tmp_mock_fk_parent_table_schema))) {
1062LOG_WARN("push_back mock_fk_parent_table failed", K(ret), K(tmp_mock_fk_parent_table_schema));
1063}
1064}
1065if (FAILEDx(deal_with_mock_fk_parent_tables(trans, schema_guard, mock_fk_parent_table_schema_array))) {
1066LOG_WARN("drop mock_fk_parent_table failed", K(ret), K(mock_fk_parent_table_schema_array));
1067}
1068}
1069}
1070
1071if (OB_SUCC(ret)) {
1072int64_t new_schema_version = OB_INVALID_VERSION;
1073if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
1074LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
1075} else if (OB_FAIL(schema_service_impl->get_database_sql_service().delete_database(
1076db_schema,
1077new_schema_version,
1078trans,
1079ddl_stmt_str))) {
1080LOG_WARN("delete database failed", KT(database_id), K(ret));
1081}
1082}
1083return ret;
1084}
1085
1086// When delete database to recyclebin, it is necessary to update schema version
1087// of each table, in case of hitting plan cache.
1088// The key of plan cache is current database name, table_id and schema version.
1089int ObDDLOperator::update_table_version_of_db(const ObDatabaseSchema &database_schema,
1090ObMySQLTransaction &trans)
1091{
1092int ret = OB_SUCCESS;
1093const uint64_t tenant_id = database_schema.get_tenant_id();
1094const uint64_t database_id = database_schema.get_database_id();
1095int64_t new_schema_version = OB_INVALID_VERSION;
1096ObArray<uint64_t> table_ids;
1097ObSchemaGetterGuard schema_guard;
1098ObSchemaService *schema_service = schema_service_.get_schema_service();
1099if (OB_ISNULL(schema_service)) {
1100ret = OB_ERR_UNEXPECTED;
1101LOG_WARN("schema service should not be null", K(ret));
1102} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
1103LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
1104} else if (OB_FAIL(schema_guard.get_table_ids_in_database(tenant_id,
1105database_id,
1106table_ids))) {
1107LOG_WARN("get_table_schemas_in_database failed", K(ret), K(tenant_id));
1108}
1109const int64_t table_count = table_ids.count();
1110for (int64_t idx = 0; OB_SUCC(ret) && idx < table_count; ++idx) {
1111const ObTableSchema *table = NULL;
1112if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
1113LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
1114} else if (OB_FAIL(schema_guard.get_table_schema(tenant_id, table_ids.at(idx), table))) {
1115LOG_WARN("fail to get table schema", KR(ret), K(tenant_id), K(table_ids.at(idx)));
1116} else if (OB_ISNULL(table)) {
1117ret = OB_ERR_UNEXPECTED;
1118LOG_WARN("table schema should not be null", K(ret));
1119} else if (table->is_index_table()) {
1120continue;
1121} else {
1122ObSEArray<ObAuxTableMetaInfo, 16> simple_index_infos;
1123if (OB_FAIL(table->get_simple_index_infos(simple_index_infos))) {
1124LOG_WARN("get_index_tid_array failed", K(ret));
1125}
1126ObSchemaGetterGuard tmp_schema_guard;
1127for (int64_t i = 0; OB_SUCC(ret) && i < simple_index_infos.count(); ++i) {
1128const ObTableSchema *index_table_schema = NULL;
1129const uint64_t table_id = simple_index_infos.at(i).table_id_;
1130if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, tmp_schema_guard))) {
1131LOG_WARN("failed to get schema guard", KR(ret), K(tenant_id));
1132} else if (OB_FAIL(tmp_schema_guard.get_table_schema(tenant_id,
1133table_id,
1134index_table_schema))) {
1135LOG_WARN("get_table_schema failed", KR(ret), "table id", table_id);
1136} else if (OB_ISNULL(index_table_schema)) {
1137ret = OB_ERR_UNEXPECTED;
1138LOG_WARN("table schema should not be null", K(ret));
1139} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
1140LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
1141} else {
1142HEAP_VAR(ObTableSchema, new_index_schema) {
1143if (OB_FAIL(new_index_schema.assign(*index_table_schema))) {
1144LOG_WARN("fail to assign schema", KR(ret), K(tenant_id), K(table_id));
1145} else {
1146new_index_schema.set_schema_version(new_schema_version);
1147}
1148if (FAILEDx(schema_service->get_table_sql_service().update_table_options(
1149trans,
1150*index_table_schema,
1151new_index_schema,
1152OB_DDL_DROP_TABLE_TO_RECYCLEBIN,
1153NULL))) {
1154LOG_WARN("update_table_option failed", KR(ret), K(tenant_id), K(table_id));
1155}
1156}
1157}
1158}
1159if (OB_SUCC(ret)) {
1160HEAP_VAR(ObTableSchema, new_ts) {
1161ObSchemaOperationType op_type;
1162if (OB_FAIL(new_ts.assign(*table))) {
1163LOG_WARN("fail to assign schema", K(ret));
1164} else {
1165op_type = new_ts.is_view_table() ? OB_DDL_DROP_VIEW_TO_RECYCLEBIN : OB_DDL_DROP_TABLE_TO_RECYCLEBIN;
1166}
1167if (OB_FAIL(ret)) {
1168} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
1169LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
1170} else {
1171new_ts.set_schema_version(new_schema_version);
1172if (OB_FAIL(schema_service->get_table_sql_service().update_table_options(
1173trans, *table, new_ts, op_type, NULL))) {
1174LOG_WARN("update_table_option failed", K(ret));
1175}
1176}
1177}
1178}
1179}
1180}
1181return ret;
1182}
1183
1184int ObDDLOperator::drop_database_to_recyclebin(const ObDatabaseSchema &database_schema,
1185ObMySQLTransaction &trans,
1186const ObString *ddl_stmt_str)
1187{
1188int ret = OB_SUCCESS;
1189const uint64_t tenant_id = database_schema.get_tenant_id();
1190const uint64_t database_id = database_schema.get_database_id();
1191int64_t new_schema_version = OB_INVALID_VERSION;
1192ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
1193if (OB_ISNULL(schema_service_impl)) {
1194ret = OB_ERR_UNEXPECTED;
1195LOG_ERROR("schema_service_impl must not null", K(ret));
1196} else if (OB_INVALID_ID == tenant_id) {
1197ret = OB_INVALID_ARGUMENT;
1198LOG_WARN("tenant_id is invalid", K(ret), K(tenant_id));
1199} else {
1200ObSqlString new_db_name;
1201ObRecycleObject recycle_object;
1202ObDatabaseSchema new_database_schema;
1203ObSchemaService *schema_service = schema_service_.get_schema_service();
1204recycle_object.set_type(ObRecycleObject::DATABASE);
1205recycle_object.set_database_id(database_schema.get_database_id());
1206recycle_object.set_table_id(OB_INVALID_ID);
1207recycle_object.set_tablegroup_id(database_schema.get_default_tablegroup_id());
1208if (OB_FAIL(recycle_object.set_original_name(database_schema.get_database_name_str()))) {
1209LOG_WARN("fail to set original name for recycleb object", KR(ret), K(tenant_id), K(database_id));
1210} else if (OB_FAIL(new_database_schema.assign(database_schema))) {
1211LOG_WARN("fail to assign new database schema", KR(ret), K(tenant_id), K(database_id));
1212} else if (FALSE_IT(new_database_schema.set_in_recyclebin(true))) {
1213} else if (FALSE_IT(new_database_schema.set_default_tablegroup_id(OB_INVALID_ID))) {
1214// It ensure that db schema version of insert recyclebin and alter database
1215// is equal that updating table version and inserting recyclebin.
1216} else if (OB_FAIL(update_table_version_of_db(database_schema, trans))) {
1217LOG_WARN("update table version of db failed", K(ret), K(database_schema));
1218} else if (OB_ISNULL(schema_service)) {
1219ret = OB_ERR_SYS;
1220LOG_WARN("schema service should not be NULL");
1221} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
1222LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
1223} else if (FALSE_IT(new_database_schema.set_schema_version(new_schema_version))) {
1224} else if (OB_FAIL(construct_new_name_for_recyclebin(new_database_schema, new_db_name))) {
1225LOG_WARN("construct_new_name_for_recyclebin failed", K(ret));
1226} else if (OB_FAIL(new_database_schema.set_database_name(new_db_name.string()))) {
1227LOG_WARN("set database name failed", K(ret));
1228} else if (FALSE_IT(recycle_object.set_object_name(new_db_name.string()))) {
1229} else if (FALSE_IT(recycle_object.set_tenant_id(tenant_id))) {
1230} else if (OB_FAIL(schema_service_impl->insert_recyclebin_object(recycle_object,
1231trans))) {
1232LOG_WARN("insert recycle object failed", K(ret));
1233} else if (OB_FAIL(alter_database(new_database_schema, trans,
1234OB_DDL_DROP_DATABASE_TO_RECYCLEBIN,
1235ddl_stmt_str,
1236false /*no need_new_schema_version*/))) {
1237LOG_WARN("alter_database failed,", K(ret));
1238} else {
1239ObSchemaGetterGuard schema_guard;
1240ObArray<const ObSimpleTableSchemaV2 *> tables;
1241if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
1242LOG_WARN("fail to get schema guard", KR(ret), K(tenant_id));
1243} else if (OB_FAIL(schema_guard.get_table_schemas_in_database(tenant_id,
1244database_id,
1245tables))) {
1246LOG_WARN("get tables in database failed", KR(ret), K(tenant_id), K(database_id));
1247}
1248for (int64_t i = 0; OB_SUCC(ret) && i < tables.count(); ++i) {
1249const ObSimpleTableSchemaV2 *table_schema = tables.at(i);
1250if (OB_ISNULL(table_schema)) {
1251ret = OB_ERR_UNEXPECTED;
1252LOG_WARN("table is NULL", K(ret));
1253} else if (table_schema->is_view_table()
1254&& OB_FAIL(ObDependencyInfo::delete_schema_object_dependency(
1255trans,
1256tenant_id,
1257table_schema->get_table_id(),
1258table_schema->get_schema_version(),
1259ObObjectType::VIEW))) {
1260LOG_WARN("failed to delete_schema_object_dependency", K(ret), K(tenant_id),
1261K(table_schema->get_table_id()));
1262}
1263}
1264}
1265}
1266return ret;
1267}
1268
1269int ObDDLOperator::create_tablegroup(ObTablegroupSchema &tablegroup_schema,
1270ObMySQLTransaction &trans,
1271const ObString *ddl_stmt_str/*=NULL*/)
1272{
1273int ret = OB_SUCCESS;
1274uint64_t new_tablegroup_id = OB_INVALID_ID;
1275const uint64_t tenant_id = tablegroup_schema.get_tenant_id();
1276int64_t new_schema_version = OB_INVALID_VERSION;
1277ObSchemaService *schema_service = schema_service_.get_schema_service();
1278
1279if (OB_ISNULL(schema_service)) {
1280ret = OB_ERR_SYS;
1281LOG_ERROR("schema_service must not null");
1282} else if (OB_FAIL(schema_service->fetch_new_tablegroup_id(
1283tablegroup_schema.get_tenant_id(), new_tablegroup_id))) {
1284LOG_WARN("failed to fetch new_tablegroup_id",
1285"tenant_id", tablegroup_schema.get_tenant_id(), K(ret));
1286} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
1287LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
1288} else {
1289tablegroup_schema.set_tablegroup_id(new_tablegroup_id);
1290tablegroup_schema.set_schema_version(new_schema_version);
1291if (OB_FAIL(schema_service->get_tablegroup_sql_service().insert_tablegroup(
1292tablegroup_schema, trans, ddl_stmt_str))) {
1293LOG_WARN("insert tablegroup failed", K(ret));
1294}
1295}
1296return ret;
1297}
1298
1299int ObDDLOperator::drop_tablegroup(const ObTablegroupSchema &tablegroup_schema,
1300ObMySQLTransaction &trans,
1301const ObString *ddl_stmt_str/*=NULL*/)
1302{
1303int ret = OB_SUCCESS;
1304
1305ObSchemaGetterGuard schema_guard;
1306ObArenaAllocator allocator(ObModIds::OB_SCHEMA_OB_SCHEMA_ARENA);
1307ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
1308const uint64_t tenant_id = tablegroup_schema.get_tenant_id();
1309const uint64_t tablegroup_id = tablegroup_schema.get_tablegroup_id();
1310if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
1311LOG_WARN("failed to get schema guard", K(ret));
1312} else if (OB_ISNULL(schema_service_impl)) {
1313ret = OB_ERR_SYS;
1314LOG_ERROR("schama schema_service_impl and schema manage must not null",
1315"schema_service_impl", OB_P(schema_service_impl), K(ret));
1316} else {
1317// check whether tablegroup is empty, if not empty, return OB_TABLEGROUP_NOT_EMPTY
1318bool not_empty = false;
1319ObArray<const ObSimpleTableSchemaV2 *> tables;
1320if (OB_FAIL(schema_guard.get_table_schemas_in_tablegroup(
1321tenant_id, tablegroup_id, tables))) {
1322LOG_WARN("get table ids in tablegroup failed", K(tenant_id), KT(tablegroup_id), K(ret));
1323} else if (tables.count() > 0) {
1324// When tablegroup is dropped, there must not be table in tablegroup, otherwise it is failed to get tablegroup
1325// schema when getting derived relation property by table. As locality and primary_zone is add in tablegroup
1326// after 2.0
1327//
1328not_empty = true;
1329}
1330// check databases' default_tablegroup_id
1331if (OB_SUCC(ret) && !not_empty) {
1332if (OB_FAIL(schema_guard.check_database_exists_in_tablegroup(
1333tenant_id, tablegroup_id, not_empty))) {
1334LOG_WARN("failed to check whether database exists in table group",
1335K(tenant_id), KT(tablegroup_id), K(ret));
1336}
1337}
1338// check tenants' default_tablegroup_id
1339if (OB_SUCC(ret) && !not_empty) {
1340const ObTenantSchema *tenant_schema = NULL;
1341if (OB_FAIL(schema_guard.get_tenant_info(tenant_id, tenant_schema))) {
1342LOG_WARN("fail to get tenant info", K(ret), KT(tenant_id));
1343} else if (OB_ISNULL(tenant_schema)) {
1344ret = OB_ERR_UNEXPECTED;
1345LOG_WARN("tenant schema is null", K(ret), KT(tenant_id));
1346} else if (tablegroup_id == tenant_schema->get_default_tablegroup_id()) {
1347not_empty = true;
1348}
1349}
1350if (OB_SUCC(ret) && not_empty) {
1351ret = OB_TABLEGROUP_NOT_EMPTY;
1352LOG_WARN("tablegroup still has tables or is some databases' default tablegroup or is tenant default tablegroup, can't delete it",
1353KT(tablegroup_id), K(ret));
1354}
1355}
1356
1357// delete tablegroup and log ddl operation
1358if (OB_SUCC(ret)) {
1359int64_t new_schema_version = OB_INVALID_VERSION;
1360if (OB_FAIL(ret)) {
1361} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
1362LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
1363} else if (OB_FAIL(schema_service_impl->get_tablegroup_sql_service().
1364delete_tablegroup(tablegroup_schema,
1365new_schema_version,
1366trans, ddl_stmt_str))) {
1367LOG_WARN("delete tablegroup failed", KT(tablegroup_id), K(ret));
1368}
1369}
1370return ret;
1371}
1372
1373int ObDDLOperator::alter_tablegroup(ObTablegroupSchema &new_schema,
1374common::ObMySQLTransaction &trans,
1375const ObString *ddl_stmt_str)
1376{
1377int ret = OB_SUCCESS;
1378const uint64_t tenant_id = new_schema.get_tenant_id();
1379int64_t new_schema_version = OB_INVALID_VERSION;
1380ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
1381if (!new_schema.is_valid()) {
1382ret = OB_INVALID_ARGUMENT;
1383LOG_WARN("invalid argument", K(ret), K(new_schema));
1384} else if (OB_ISNULL(schema_service_impl)) {
1385ret = OB_ERR_SYS;
1386LOG_ERROR("schema schema_service_impl must not null",
1387"schema_service_impl", OB_P(schema_service_impl), K(ret));
1388} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
1389LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
1390} else {
1391new_schema.set_schema_version(new_schema_version);
1392if (OB_FAIL(schema_service_impl->get_tablegroup_sql_service().update_tablegroup(
1393new_schema, trans, ddl_stmt_str))) {
1394LOG_WARN("fail to get tablegroup sql service", K(ret));
1395}
1396}
1397return ret;
1398}
1399
1400int ObDDLOperator::alter_tablegroup(ObSchemaGetterGuard &schema_guard,
1401ObTableSchema &new_table_schema,
1402common::ObMySQLTransaction &trans,
1403const ObString *ddl_stmt_str/*=NULL*/)
1404{
1405int ret = OB_SUCCESS;
1406const uint64_t tenant_id = new_table_schema.get_tenant_id();
1407int64_t new_schema_version = OB_INVALID_VERSION;
1408ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
1409if (OB_ISNULL(schema_service_impl)) {
1410ret = OB_ERR_SYS;
1411RS_LOG(ERROR, "schema schema_service_impl must not null",
1412"schema_service_impl", OB_P(schema_service_impl), K(ret));
1413} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
1414LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
1415} else {
1416new_table_schema.set_schema_version(new_schema_version);
1417// check whether tablegroup is empty, if not empty, return OB_TABLEGROUP_NOT_EMPTY
1418if (OB_FAIL(schema_service_impl->get_table_sql_service().update_tablegroup(
1419schema_guard,
1420new_table_schema,
1421trans,
1422ddl_stmt_str))) {
1423RS_LOG(WARN, "alter tablegroup failed", K(ret));
1424}
1425}
1426return ret;
1427}
1428
1429int ObDDLOperator::handle_audit_metainfo(const share::schema::ObSAuditSchema &audit_schema,
1430const ObSAuditModifyType modify_type,
1431const bool need_update,
1432const ObString *ddl_stmt_str,
1433common::ObMySQLTransaction &trans,
1434common::ObSqlString &public_sql_string)
1435{
1436int ret = OB_SUCCESS;
1437ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
1438int64_t new_schema_version = OB_INVALID_VERSION;
1439if (OB_ISNULL(schema_sql_service)) {
1440ret = OB_ERR_SYS;
1441LOG_ERROR("schema_sql_service must not be null", K(ret));
1442} else if (OB_FAIL(schema_service_.gen_new_schema_version(audit_schema.get_tenant_id(),
1443new_schema_version))) {
1444LOG_WARN("fail to gen new schema_version", K(ret), K(audit_schema.get_tenant_id()));
1445} else {
1446ObAuditSqlService &tmp_audit_ss = schema_sql_service->get_audit_sql_service();
1447if (OB_FAIL(tmp_audit_ss.handle_audit_metainfo(audit_schema,
1448modify_type,
1449need_update,
1450new_schema_version,
1451ddl_stmt_str,
1452trans,
1453public_sql_string))) {
1454LOG_WARN("Failed to handle audit meta info", K(audit_schema), K(modify_type),
1455K(new_schema_version), K(ret));
1456}
1457}
1458return ret;
1459}
1460
1461int ObDDLOperator::get_user_id_for_inner_ur(ObUserInfo &user,
1462bool &is_inner_ur,
1463uint64_t &new_user_id)
1464{
1465int ret = OB_SUCCESS;
1466const char* ur_name = user.get_user_name();
1467ObSchemaService *schema_service = schema_service_.get_schema_service();
1468const ObSysVariableSchema *sys_variable_schema = NULL;
1469ObSchemaGetterGuard schema_guard;
1470bool is_oracle_mode = false;
1471const uint64_t tenant_id = user.get_tenant_id();
1472is_inner_ur = false;
1473if (OB_ISNULL(schema_service)) {
1474ret = OB_ERR_UNEXPECTED;
1475LOG_WARN("schema_service is NULL", K(ret));
1476} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
1477LOG_WARN("failed to get schema guard", K(ret));
1478} else if (OB_FAIL(schema_guard.get_sys_variable_schema(tenant_id, sys_variable_schema))) {
1479LOG_WARN("get sys variable schema failed", K(ret));
1480} else if (OB_ISNULL(sys_variable_schema)) {
1481ret = OB_ERR_UNEXPECTED;
1482LOG_WARN("sys variable schema is null", K(ret));
1483} else if (OB_FAIL(sys_variable_schema->get_oracle_mode(is_oracle_mode))) {
1484RS_LOG(WARN, "failed to get oracle mode", K(ret), K(tenant_id));
1485} else if (is_oracle_mode) {
1486if (STRCMP(ur_name, OB_ORA_LBACSYS_NAME) == 0) {
1487new_user_id = OB_ORA_LBACSYS_USER_ID;
1488is_inner_ur = true;
1489} else if (STRCMP(ur_name, OB_ORA_CONNECT_ROLE_NAME) == 0) {
1490new_user_id = OB_ORA_CONNECT_ROLE_ID;
1491is_inner_ur = true;
1492} else if (STRCMP(ur_name, OB_ORA_RESOURCE_ROLE_NAME) == 0) {
1493new_user_id = OB_ORA_RESOURCE_ROLE_ID;
1494is_inner_ur = true;
1495} else if (STRCMP(ur_name, OB_ORA_DBA_ROLE_NAME) == 0) {
1496new_user_id = OB_ORA_DBA_ROLE_ID;
1497is_inner_ur = true;
1498} else if (STRCMP(ur_name, OB_ORA_PUBLIC_ROLE_NAME) == 0) {
1499new_user_id = OB_ORA_PUBLIC_ROLE_ID;
1500is_inner_ur = true;
1501}
1502}
1503// both oracle mode and mysql mode
1504if (OB_SUCC(ret)) {
1505if (STRCMP(ur_name, OB_ORA_AUDITOR_NAME) == 0) {
1506new_user_id = OB_ORA_AUDITOR_USER_ID;
1507is_inner_ur = true;
1508}
1509}
1510return ret;
1511}
1512
1513int ObDDLOperator::create_user(ObUserInfo &user,
1514const ObString *ddl_stmt_str,
1515ObMySQLTransaction &trans)
1516{
1517int ret = OB_SUCCESS;
1518uint64_t new_user_id = user.get_user_id();
1519ObSchemaService *schema_service = schema_service_.get_schema_service();
1520bool is_inner_ur;
1521if (OB_ISNULL(schema_service)) {
1522ret = OB_ERR_SYS;
1523LOG_ERROR("schema_service must not null");
1524} else if (OB_FAIL(get_user_id_for_inner_ur(user, is_inner_ur, new_user_id))) {
1525LOG_WARN("failed to fetch_new_user_id", "tennat_id", user.get_tenant_id(), K(ret));
1526} else if (!is_inner_ur &&
1527OB_FAIL(schema_service->fetch_new_user_id(user.get_tenant_id(), new_user_id))) {
1528LOG_WARN("failed to fetch_new_user_id", "tennat_id", user.get_tenant_id(), K(ret));
1529} else {
1530user.set_user_id(new_user_id);
1531}
1532if (OB_SUCC(ret)) {
1533const uint64_t tenant_id = user.get_tenant_id();
1534int64_t new_schema_version = OB_INVALID_VERSION;
1535if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
1536LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
1537} else if (OB_FAIL(schema_service->get_user_sql_service().create_user(
1538user, new_schema_version, ddl_stmt_str, trans))) {
1539LOG_WARN("insert user failed", K(user), K(ret));
1540}
1541}
1542return ret;
1543}
1544
1545int ObDDLOperator::create_table(ObTableSchema &table_schema,
1546ObMySQLTransaction &trans,
1547const ObString *ddl_stmt_str/*=NULL*/,
1548const bool need_sync_schema_version,
1549const bool is_truncate_table /*false*/)
1550{
1551int ret = OB_SUCCESS;
1552const uint64_t tenant_id = table_schema.get_tenant_id();
1553int64_t new_schema_version = OB_INVALID_VERSION;
1554ObSchemaService *schema_service = schema_service_.get_schema_service();
1555ObSchemaGetterGuard schema_guard;
1556if (OB_ISNULL(schema_service)) {
1557ret = OB_ERR_SYS;
1558RS_LOG(ERROR, "schema_service must not null");
1559} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
1560LOG_WARN("failed to get schema guard", K(ret));
1561} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
1562LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
1563} else {
1564table_schema.set_schema_version(new_schema_version);
1565if (OB_FAIL(schema_service->get_table_sql_service().create_table(
1566table_schema,
1567trans,
1568ddl_stmt_str,
1569need_sync_schema_version,
1570is_truncate_table))) {
1571RS_LOG(WARN, "failed to create table", K(ret));
1572} else if (OB_FAIL(sync_version_for_cascade_table(tenant_id,
1573table_schema.get_depend_table_ids(), trans))) {
1574RS_LOG(WARN, "fail to sync cascade depend table", K(ret));
1575} else if (OB_FAIL(sync_version_for_cascade_mock_fk_parent_table(table_schema.get_tenant_id(), table_schema.get_depend_mock_fk_parent_table_ids(), trans))) {
1576LOG_WARN("fail to sync cascade depend_mock_fk_parent_table_ids table", K(ret));
1577}
1578}
1579
1580// add audit in table if necessary
1581if (OB_SUCC(ret) && !is_truncate_table && (table_schema.is_user_table() || table_schema.is_external_table())) {
1582const uint64_t tenant_id = table_schema.get_tenant_id();
1583ObArray<const ObSAuditSchema *> audits;
1584
1585ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
1586if (OB_FAIL(schema_guard.get_audit_schema_in_owner(tenant_id,
1587AUDIT_OBJ_DEFAULT,
1588OB_AUDIT_MOCK_USER_ID,
1589audits))) {
1590LOG_WARN("get get_audit_schema_in_owner failed", K(tenant_id), K(table_schema), K(ret));
1591} else if (!audits.empty()) {
1592common::ObSqlString public_sql_string;
1593ObSAuditSchema new_audit_schema;
1594for (int64_t i = 0; OB_SUCC(ret) && i < audits.count(); ++i) {
1595uint64_t new_audit_id = common::OB_INVALID_ID;
1596const ObSAuditSchema *audit_schema = audits.at(i);
1597if (OB_ISNULL(audit_schema)) {
1598ret = OB_ERR_UNEXPECTED;
1599LOG_WARN("audit_schema is NULL", K(ret));
1600} else if (!audit_schema->is_access_operation_for_table()) {
1601continue;
1602} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
1603LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
1604} else if (OB_FAIL(schema_service_impl->fetch_new_audit_id(tenant_id, new_audit_id))) {
1605LOG_WARN("Failed to fetch new_audit_id", K(ret));
1606} else if (OB_FAIL(new_audit_schema.assign(*audit_schema))) {
1607LOG_WARN("fail to assign audit schema", KR(ret));
1608} else {
1609new_audit_schema.set_schema_version(new_schema_version);
1610new_audit_schema.set_audit_id(new_audit_id);
1611new_audit_schema.set_audit_type(AUDIT_TABLE);
1612new_audit_schema.set_owner_id(table_schema.get_table_id());
1613if (OB_FAIL(schema_service_impl->get_audit_sql_service().handle_audit_metainfo(
1614new_audit_schema,
1615AUDIT_MT_ADD,
1616false,
1617new_schema_version,
1618NULL,
1619trans,
1620public_sql_string))) {
1621LOG_WARN("drop audit_schema failed", K(new_audit_schema), K(ret));
1622} else {
1623LOG_INFO("succ to add audit_schema from default", K(new_audit_schema));
1624}
1625}
1626}
1627}
1628}
1629return ret;
1630}
1631
1632int ObDDLOperator::sync_version_for_cascade_table(
1633const uint64_t tenant_id,
1634const ObIArray<uint64_t> &table_ids,
1635ObMySQLTransaction &trans)
1636{
1637int ret = OB_SUCCESS;
1638uint64_t id = OB_INVALID_ID;
1639const ObTableSchema *schema = NULL;
1640ObSchemaService *schema_service = schema_service_.get_schema_service();
1641if (OB_ISNULL(schema_service)) {
1642ret = OB_ERR_SYS;
1643RS_LOG(ERROR, "schema_service must not null");
1644} else {
1645for (int64_t i = 0; i < table_ids.count() && OB_SUCC(ret); i++) {
1646ObSchemaGetterGuard schema_guard;
1647id = table_ids.at(i);
1648int64_t new_schema_version = OB_INVALID_VERSION;
1649ObTableSchema tmp_schema;
1650if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
1651RS_LOG(WARN, "get schema guard failed", K(ret), K(tenant_id), K(id));
1652} else if (OB_FAIL(schema_guard.get_table_schema(tenant_id, id, schema))) {
1653LOG_WARN("fail to get table schema", K(ret), K(tenant_id), K(id));
1654} else if (!schema) {
1655ret = OB_ERR_UNEXPECTED;
1656LOG_WARN("schema is NULL", K(ret));
1657} else if (OB_FAIL(tmp_schema.assign(*schema))) {
1658LOG_WARN("fail to assign schema", K(ret), KPC(schema));
1659} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
1660LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
1661} else if (OB_FAIL(schema_service->get_table_sql_service().sync_schema_version_for_history(
1662trans,
1663tmp_schema,
1664new_schema_version))) {
1665RS_LOG(WARN, "fail to sync schema version", K(ret));
1666} else {
1667LOG_INFO("synced schema version for depend table", K(id),
1668"from", schema->get_schema_version(), "to", new_schema_version);
1669}
1670}
1671}
1672
1673return ret;
1674}
1675
1676// Notice that, truncate table, offline ddl should sync origin sequence values.
1677int ObDDLOperator::create_sequence_in_create_table(ObTableSchema &table_schema,
1678common::ObMySQLTransaction &trans,
1679share::schema::ObSchemaGetterGuard &schema_guard,
1680const obrpc::ObSequenceDDLArg *sequence_ddl_arg)
1681{
1682int ret = OB_SUCCESS;
1683if (!(table_schema.is_user_table() || table_schema.is_oracle_tmp_table())) {
1684// do nothing
1685} else {
1686for (ObTableSchema::const_column_iterator iter = table_schema.column_begin();
1687OB_SUCC(ret) && iter != table_schema.column_end(); ++iter) {
1688ObColumnSchemaV2 &column_schema = (**iter);
1689if (!column_schema.is_identity_column()) {
1690continue;
1691} else {
1692ObSequenceDDLProxy ddl_operator(schema_service_);
1693char temp_sequence_name[OB_MAX_SEQUENCE_NAME_LENGTH + 1] = { 0 };
1694int32_t len = snprintf(temp_sequence_name, sizeof(temp_sequence_name), "%s%lu%c%lu",
1695IDENTITY_COLUMN_SEQUENCE_OBJECT_NAME_PREFIX,
1696ObSchemaUtils::get_extract_schema_id(table_schema.get_tenant_id(), table_schema.get_table_id()),
1697'_',
1698column_schema.get_column_id());
1699if (OB_UNLIKELY(len < 0)) {
1700ret = OB_ERR_UNEXPECTED;
1701LOG_WARN("create sequence name fail", K(ret), K(column_schema));
1702} else {
1703ObString sequence_name = ObString::make_string(temp_sequence_name);
1704ObSequenceSchema sequence_schema;
1705if (nullptr != sequence_ddl_arg) {
1706sequence_schema = sequence_ddl_arg->seq_schema_;
1707} else {
1708const ObSequenceSchema *tmp_sequence_schema = NULL;
1709if (OB_FAIL(schema_guard.get_sequence_schema(table_schema.get_tenant_id(),
1710column_schema.get_sequence_id(),
1711tmp_sequence_schema))) {
1712LOG_WARN("get sequence schema failed", K(ret), K(column_schema));
1713} else if (OB_ISNULL(tmp_sequence_schema)) {
1714ret = OB_NOT_INIT;
1715LOG_WARN("sequence not found", K(ret), K(column_schema));
1716} else if (OB_FAIL(sequence_schema.assign(*tmp_sequence_schema))) {
1717LOG_WARN("fail to assign sequence schema", KR(ret));
1718} else {}
1719}
1720if (OB_SUCC(ret)) {
1721sequence_schema.set_database_id(table_schema.get_database_id());
1722sequence_schema.set_sequence_name(sequence_name);
1723if (nullptr == sequence_ddl_arg) {
1724// In some scenes like trunctae table and offline ddl, should inherit the sequce object from origin table except sequence id, etc.
1725// Validity check and set of option bitset are completed in creating origin table phase,
1726// thus we do not have to check the validity of option_bitset again for the hidden table.
1727if (OB_FAIL(ddl_operator.create_sequence_without_bitset(sequence_schema,
1728trans,
1729schema_guard,
1730nullptr))) {
1731LOG_WARN("create sequence fail", K(ret), K(table_schema));
1732} else {/* do nothing. */}
1733} else if (OB_FAIL(ddl_operator.create_sequence(sequence_schema,
1734sequence_ddl_arg->option_bitset_,
1735trans,
1736schema_guard,
1737NULL))) {
1738LOG_WARN("create sequence fail", K(ret), K(table_schema));
1739}
1740if (OB_SUCC(ret)) {
1741column_schema.set_sequence_id(sequence_schema.get_sequence_id());
1742char sequence_string[OB_MAX_SEQUENCE_NAME_LENGTH + 1] = { 0 };
1743uint64_t pure_sequence_id = ObSchemaUtils::get_extract_schema_id(table_schema.get_tenant_id(), sequence_schema.get_sequence_id());
1744len = snprintf(sequence_string, sizeof(sequence_string), "%lu", pure_sequence_id);
1745if (OB_UNLIKELY(len < 0)) {
1746ret = OB_ERR_UNEXPECTED;
1747LOG_WARN("create sequence name fail", K(ret), K(table_schema));
1748} else {
1749ObObjParam cur_default_value; // for desc table
1750ObObjParam orig_default_value; // for store pure_sequence_id
1751cur_default_value.set_varchar("SEQUENCE.NEXTVAL");
1752cur_default_value.set_collation_type(ObCharset::get_system_collation());
1753cur_default_value.set_collation_level(CS_LEVEL_IMPLICIT);
1754cur_default_value.set_param_meta();
1755orig_default_value.set_varchar(sequence_string);
1756orig_default_value.set_collation_type(ObCharset::get_system_collation());
1757orig_default_value.set_collation_level(CS_LEVEL_IMPLICIT);
1758orig_default_value.set_param_meta();
1759if (OB_FAIL(column_schema.set_cur_default_value(cur_default_value))) {
1760LOG_WARN("set current default value fail", K(ret));
1761} else if (OB_FAIL(column_schema.set_orig_default_value(orig_default_value))) {
1762LOG_WARN("set origin default value fail", K(ret), K(column_schema));
1763}
1764}
1765}
1766}
1767}
1768}
1769}
1770}
1771return ret;
1772}
1773
1774// Sequence_schema and table_schema have a one-to-one relationship.
1775// Sequence can only be changed through interfaces such as alter/drop table,
1776// So there is no need to increase the schema version number separately.
1777int ObDDLOperator::drop_sequence_in_drop_table(const ObTableSchema &table_schema,
1778common::ObMySQLTransaction &trans,
1779share::schema::ObSchemaGetterGuard &schema_guard)
1780{
1781int ret = OB_SUCCESS;
1782if (table_schema.is_user_table() || table_schema.is_oracle_tmp_table()) {
1783for (ObTableSchema::const_column_iterator iter = table_schema.column_begin();
1784OB_SUCC(ret) && iter != table_schema.column_end(); ++iter) {
1785ObColumnSchemaV2 &column_schema = (**iter);
1786if (OB_FAIL(drop_sequence_in_drop_column(column_schema, trans, schema_guard))) {
1787LOG_WARN("drop sequence in drop column fail", K(ret), K(column_schema));
1788}
1789}
1790}
1791return ret;
1792}
1793
1794int ObDDLOperator::create_sequence_in_add_column(const ObTableSchema &table_schema,
1795ObColumnSchemaV2 &column_schema,
1796ObMySQLTransaction &trans,
1797ObSchemaGetterGuard &schema_guard,
1798ObSequenceDDLArg &sequence_ddl_arg)
1799{
1800int ret = OB_SUCCESS;
1801if (column_schema.is_identity_column()) {
1802ObSequenceDDLProxy ddl_operator(schema_service_);
1803ObSequenceSchema sequence_schema = sequence_ddl_arg.sequence_schema();
1804char temp_sequence_name[OB_MAX_SEQUENCE_NAME_LENGTH + 1] = { 0 };
1805int32_t len = snprintf(temp_sequence_name, sizeof(temp_sequence_name), "%s%lu%c%lu",
1806"ISEQ$$_",
1807ObSchemaUtils::get_extract_schema_id(column_schema.get_tenant_id(), column_schema.get_table_id()),
1808'_',
1809column_schema.get_column_id());
1810if (OB_UNLIKELY(len < 0)) {
1811ret = OB_ERR_UNEXPECTED;
1812LOG_WARN("create sequence name fail", K(ret), K(column_schema));
1813} else {
1814ObString sequence_name = ObString::make_string(temp_sequence_name);
1815sequence_schema.set_database_id(table_schema.get_database_id());
1816sequence_schema.set_sequence_name(sequence_name);
1817if (OB_FAIL(ddl_operator.create_sequence(sequence_schema,
1818sequence_ddl_arg.option_bitset_,
1819trans,
1820schema_guard,
1821NULL))) {
1822LOG_WARN("create sequence fail", K(ret));
1823} else {
1824column_schema.set_sequence_id(sequence_schema.get_sequence_id());
1825char sequence_string[OB_MAX_SEQUENCE_NAME_LENGTH + 1] = { 0 };
1826uint64_t pure_sequence_id = ObSchemaUtils::get_extract_schema_id(column_schema.get_tenant_id(), column_schema.get_sequence_id());
1827len = snprintf(sequence_string, sizeof(sequence_string), "%lu", pure_sequence_id);
1828if (OB_UNLIKELY(len < 0)) {
1829ret = OB_ERR_UNEXPECTED;
1830LOG_WARN("create sequence name fail", K(ret), K(column_schema));
1831} else {
1832ObObjParam cur_default_value; // for desc table
1833ObObjParam orig_default_value; // for store pure_sequence_id
1834cur_default_value.set_varchar("SEQUENCE.NEXTVAL");
1835cur_default_value.set_collation_type(ObCharset::get_system_collation());
1836cur_default_value.set_collation_level(CS_LEVEL_IMPLICIT);
1837cur_default_value.set_param_meta();
1838orig_default_value.set_varchar(sequence_string);
1839orig_default_value.set_collation_type(ObCharset::get_system_collation());
1840orig_default_value.set_collation_level(CS_LEVEL_IMPLICIT);
1841orig_default_value.set_param_meta();
1842if (OB_FAIL(column_schema.set_cur_default_value(cur_default_value))) {
1843LOG_WARN("set current default value fail", K(ret));
1844} else if (OB_FAIL(column_schema.set_orig_default_value(orig_default_value))) {
1845LOG_WARN("set origin default value fail", K(ret), K(column_schema));
1846}
1847}
1848}
1849}
1850}
1851return ret;
1852}
1853
1854int ObDDLOperator::drop_sequence_in_drop_column(const ObColumnSchemaV2 &column_schema,
1855common::ObMySQLTransaction &trans,
1856share::schema::ObSchemaGetterGuard &schema_guard)
1857{
1858int ret = OB_SUCCESS;
1859if (column_schema.is_identity_column()) {
1860ObSequenceDDLProxy ddl_operator(schema_service_);
1861const ObSequenceSchema *temp_sequence_schema = NULL;
1862ObSequenceSchema sequence_schema;
1863if (OB_FAIL(schema_guard.get_sequence_schema(column_schema.get_tenant_id(),
1864column_schema.get_sequence_id(),
1865temp_sequence_schema))) {
1866LOG_WARN("get sequence schema fail", K(ret), K(column_schema));
1867if (ret == OB_ERR_UNEXPECTED) {
1868// sequence has been deleted externally.
1869// Oracle does not allow sequences internally created to be deleted externally.
1870// In the future, it will be solved by adding columns to the internal table,
1871// and then the error code conversion can be removed.
1872ret = OB_SUCCESS;
1873}
1874} else if (OB_ISNULL(temp_sequence_schema)) {
1875ret = OB_ERR_UNEXPECTED;
1876LOG_WARN("sequence not exist", KR(ret), K(column_schema));
1877} else if (OB_FAIL(sequence_schema.assign(*temp_sequence_schema))) {
1878LOG_WARN("fail to assign sequence schema", KR(ret));
1879} else if (OB_FAIL(ddl_operator.drop_sequence(sequence_schema,
1880trans,
1881schema_guard,
1882NULL,
1883FROM_TABLE_DDL))) {
1884LOG_WARN("drop sequence fail", K(ret), K(column_schema));
1885}
1886}
1887return ret;
1888}
1889
1890int ObDDLOperator::reinit_autoinc_row(const ObTableSchema &table_schema,
1891common::ObMySQLTransaction &trans)
1892{
1893int ret = OB_SUCCESS;
1894int64_t start_time = ObTimeUtility::current_time();
1895uint64_t table_id = table_schema.get_table_id();
1896ObString table_name = table_schema.get_table_name();
1897int64_t truncate_version = table_schema.get_truncate_version();
1898uint64_t column_id = table_schema.get_autoinc_column_id();
1899ObAutoincrementService &autoinc_service = share::ObAutoincrementService::get_instance();
1900
1901if (0 != column_id) {
1902bool is_oracle_mode = false;
1903if (OB_FAIL(table_schema.check_if_oracle_compat_mode(is_oracle_mode))) {
1904LOG_WARN("fail to check is oracle mode",
1905KR(ret), K(table_id), K(table_name), K(truncate_version), K(column_id));
1906} else if (is_oracle_mode) {
1907ret = OB_ERR_UNEXPECTED;
1908LOG_WARN("in oracle mode, autoic_column_id must be illegal",
1909KR(ret), K(table_id), K(table_name), K(truncate_version), K(column_id));
1910} else {
1911// reinit auto_increment value
1912uint64_t tenant_id = table_schema.get_tenant_id();
1913if (OB_FAIL(autoinc_service.reinit_autoinc_row(tenant_id, table_id,
1914column_id, truncate_version, trans))) {
1915LOG_WARN("failed to reint auto_increment",
1916KR(ret), K(tenant_id), K(table_id), K(table_name), K(truncate_version), K(column_id));
1917}
1918}
1919}
1920int64_t finish_time = ObTimeUtility::current_time();
1921LOG_INFO("finish reinit_auto_row", KR(ret), "cost_ts", finish_time - start_time);
1922return ret;
1923}
1924
1925int ObDDLOperator::try_reinit_autoinc_row(const ObTableSchema &table_schema,
1926common::ObMySQLTransaction &trans)
1927{
1928int ret = OB_SUCCESS;
1929bool need_reinit_inner_table = false;
1930const uint64_t table_id = table_schema.get_table_id();
1931const uint64_t tenant_id = table_schema.get_tenant_id();
1932const int64_t truncate_version = table_schema.get_truncate_version();
1933const uint64_t column_id = table_schema.get_autoinc_column_id();
1934ObAutoincrementService &autoinc_service = share::ObAutoincrementService::get_instance();
1935if (OB_FAIL(autoinc_service.try_lock_autoinc_row(tenant_id, table_id, column_id, truncate_version,
1936need_reinit_inner_table, trans))) {
1937LOG_WARN("fail to check inner autoinc version", KR(ret), K(tenant_id), K(table_id), K(column_id));
1938} else if (need_reinit_inner_table) {
1939if (OB_FAIL(autoinc_service.reset_autoinc_row(tenant_id, table_id, column_id,
1940truncate_version, trans))) {
1941LOG_WARN("fail to reinit autoinc row", KR(ret), K(tenant_id), K(table_id), K(column_id));
1942}
1943}
1944return ret;
1945}
1946
1947// Notice: this function process index.
1948int ObDDLOperator::alter_table_drop_aux_column(
1949ObTableSchema &new_table_schema,
1950const ObColumnSchemaV2 &orig_column_schema,
1951common::ObMySQLTransaction &trans,
1952const ObTableType table_type)
1953{
1954int ret = OB_SUCCESS;
1955//should update the aux table
1956const uint64_t tenant_id = orig_column_schema.get_tenant_id();
1957int64_t new_schema_version = OB_INVALID_VERSION;
1958const bool is_index = USER_INDEX == table_type;
1959ObSEArray<uint64_t, 16> aux_vp_tid_array; // for VP
1960ObSEArray<ObAuxTableMetaInfo, 16> simple_index_infos;
1961ObSchemaGetterGuard schema_guard;
1962ObSchemaService *schema_service = schema_service_.get_schema_service();
1963if (OB_ISNULL(schema_service)) {
1964ret = OB_ERR_SYS;
1965RS_LOG(ERROR, "schema_service must not null");
1966} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
1967RS_LOG(WARN, "get schema guard failed", K(ret));
1968} else if (!is_index
1969&& OB_FAIL(new_table_schema.get_aux_vp_tid_array(aux_vp_tid_array))) {
1970LOG_WARN("get_aux_tid_array failed", K(ret), K(is_index));
1971} else if (OB_FAIL(new_table_schema.get_simple_index_infos(
1972simple_index_infos))) {
1973LOG_WARN("get simple_index_infos failed", K(ret));
1974}
1975
1976//update all aux table schema
1977int64_t N = is_index ? simple_index_infos.count() : aux_vp_tid_array.count();
1978for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) {
1979const ObTableSchema *aux_table_schema = NULL;
1980uint64_t tid = is_index ? simple_index_infos.at(i).table_id_ : aux_vp_tid_array.at(i);
1981if (OB_FAIL(schema_guard.get_table_schema(
1982tenant_id, tid, aux_table_schema))) {
1983LOG_WARN("get table schema failed", K(ret), K(tenant_id), K(tid));
1984} else if (OB_ISNULL(aux_table_schema)) {
1985ret = OB_ERR_UNEXPECTED;
1986LOG_WARN("table schema should not be null", K(ret));
1987} else if (aux_table_schema->is_in_recyclebin()) {
1988ret = OB_ERR_OPERATION_ON_RECYCLE_OBJECT;
1989LOG_WARN("aux table is in recyclebin", K(ret));
1990} else {
1991const ObColumnSchemaV2 *delete_column_schema =
1992aux_table_schema->get_column_schema(orig_column_schema.get_column_id());
1993if (NULL != delete_column_schema) {
1994if (delete_column_schema->is_index_column()) {
1995ret = OB_ERR_ALTER_INDEX_COLUMN;
1996RS_LOG(WARN, "can't not drop index column", K(ret));
1997} else {
1998// Notice: when the last VP column is deleted, the VP table should be deleted.
1999// If other VP column is hidden, the VP partition should be deleted.
2000int64_t normal_column_count = 0;
2001for (int64_t i = 0; OB_SUCC(ret) && (normal_column_count < 2) && (i < aux_table_schema->get_column_count()); ++i) {
2002if (!aux_table_schema->get_column_schema_by_idx(i)->is_hidden()) {
2003++normal_column_count;
2004}
2005}
2006if (OB_FAIL(ret)) {
2007} else if (normal_column_count < 1) {
2008ret = OB_ERR_UNEXPECTED;
2009LOG_WARN("normal_column_count is error", K(ret), K(normal_column_count));
2010} else if (1 == normal_column_count) {
2011// DROP AUX_VERTIAL_PARTITION_TABLE
2012ObSEArray<uint64_t, 16> tmp_aux_vp_tid_array;
2013if (OB_FAIL(drop_table(*aux_table_schema, trans))) {
2014LOG_WARN("drop aux vertial partition table failed", K(ret), K(*aux_table_schema));
2015} else {
2016for (int64_t i = 0; OB_SUCC(ret) && (i < aux_vp_tid_array.count()); ++i) {
2017if (aux_vp_tid_array.at(i) == aux_table_schema->get_table_id()) {
2018// skip
2019} else if (OB_FAIL(tmp_aux_vp_tid_array.push_back(aux_vp_tid_array.at(i)))) {
2020LOG_WARN("push back to tmp_aux_vp_tid_array failed", K(ret), K(i), K(aux_vp_tid_array.at(i)));
2021}
2022}
2023if (OB_SUCC(ret)) {
2024// update aux_vp_tid_array of new_table_schema
2025if (OB_FAIL(new_table_schema.set_aux_vp_tid_array(tmp_aux_vp_tid_array))) {
2026LOG_WARN("set aux_vp_tid_array to new_table_schema failed", K(ret), K(tmp_aux_vp_tid_array));
2027}
2028}
2029}
2030} else {
2031ObTableSchema tmp_aux_table_schema;
2032if (OB_FAIL(tmp_aux_table_schema.assign(*aux_table_schema))) {
2033LOG_WARN("fail to assign schema", K(ret));
2034} else if (OB_FAIL(update_prev_id_for_delete_column(
2035*aux_table_schema,
2036tmp_aux_table_schema,
2037*delete_column_schema,
2038trans))) {
2039LOG_WARN("failed to update column previous id for delete column", K(ret));
2040} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2041LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2042} else if (OB_FAIL(schema_service->get_table_sql_service().delete_single_column(
2043new_schema_version,
2044trans,
2045*aux_table_schema,
2046*delete_column_schema))) {
2047RS_LOG(WARN, "failed to delete non-aux column!",
2048"table schema", *aux_table_schema, K(ret));
2049} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2050LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2051} else if (OB_FAIL(schema_service->get_table_sql_service().sync_aux_schema_version_for_history(
2052trans,
2053*aux_table_schema,
2054new_schema_version
2055))) {
2056RS_LOG(WARN, "fail to update aux schema version for update column");
2057}
2058}
2059}
2060}
2061}
2062}
2063
2064return ret;
2065}
2066
2067int ObDDLOperator::update_prev_id_for_delete_column(const ObTableSchema &origin_table_schema,
2068ObTableSchema &new_table_schema,
2069const ObColumnSchemaV2 &ori_column_schema,
2070common::ObMySQLTransaction &trans)
2071{
2072int ret = OB_SUCCESS;
2073const uint64_t tenant_id = origin_table_schema.get_tenant_id();
2074int64_t new_schema_version = OB_INVALID_VERSION;
2075// When a transaction currently add/drop column: origin_table_schema don't update prev&next column ID, so it need fetch from new table.
2076ObColumnSchemaV2 *new_origin_col = new_table_schema.get_column_schema(ori_column_schema.get_column_name());
2077ObSchemaService *schema_service = schema_service_.get_schema_service();
2078if (OB_ISNULL(schema_service)) {
2079ret = OB_ERR_SYS;
2080RS_LOG(ERROR, "schema_service must not null");
2081} else if (OB_ISNULL(new_origin_col)) {
2082ret = OB_ERR_UNEXPECTED;
2083LOG_WARN("Failed to get column from new table schema", K(ret));
2084} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2085LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2086} else {
2087ObColumnSchemaV2 *next_col = new_table_schema.get_column_schema_by_prev_next_id(new_origin_col->get_next_column_id());
2088if (OB_ISNULL(next_col)) {
2089// do nothing since local_column is tail column
2090} else {
2091next_col->set_prev_column_id(new_origin_col->get_prev_column_id());
2092next_col->set_schema_version(new_schema_version);
2093if (OB_FAIL(schema_service->get_table_sql_service().update_single_column(
2094trans,
2095origin_table_schema,
2096new_table_schema,
2097*next_col,
2098true /* record_ddl_operation */))) {
2099LOG_WARN("Failed to update single column", K(ret), K(next_col->get_column_name_str()));
2100}
2101}
2102}
2103return ret;
2104}
2105
2106int ObDDLOperator::update_table_foreign_keys(share::schema::ObTableSchema &new_table_schema,
2107common::ObMySQLTransaction &trans,
2108bool in_offline_ddl_white_list)
2109{
2110int ret = OB_SUCCESS;
2111ObSchemaService *schema_service = schema_service_.get_schema_service();
2112int64_t new_schema_version = OB_INVALID_VERSION;
2113const uint64_t tenant_id = new_table_schema.get_tenant_id();
2114
2115if (OB_ISNULL(schema_service)) {
2116ret = OB_ERR_UNEXPECTED;
2117LOG_WARN("schema_service is NULL", K(ret));
2118} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2119LOG_WARN("generate new schema version failed", K(ret));
2120} else if (FALSE_IT(new_table_schema.set_schema_version(new_schema_version))) {
2121} else if (OB_FAIL(schema_service->get_table_sql_service().update_foreign_key_state(
2122trans, new_table_schema))) {
2123LOG_WARN("update foreign keys enable option into inner table failed", K(ret));
2124} else {
2125uint64_t id = OB_INVALID_ID;
2126const ObTableSchema *schema = NULL;
2127const ObIArray<uint64_t> &table_ids = new_table_schema.get_depend_table_ids();
2128for (int64_t i = 0; i < table_ids.count() && OB_SUCC(ret); i++) {
2129ObSchemaGetterGuard schema_guard;
2130id = table_ids.at(i);
2131ObTableSchema tmp_schema;
2132if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
2133RS_LOG(WARN, "get schema guard failed", K(ret), K(id));
2134} else if (OB_FAIL(schema_guard.get_table_schema(tenant_id, id, schema))) {
2135LOG_WARN("fail to get table schema", K(ret), K(tenant_id), K(id));
2136} else if (!schema) {
2137ret = OB_ERR_UNEXPECTED;
2138LOG_WARN("schema is NULL", K(ret));
2139} else if (OB_FAIL(tmp_schema.assign(*schema))) {
2140LOG_WARN("fail to assign schema", K(ret), KPC(schema));
2141} else if (FALSE_IT(tmp_schema.set_in_offline_ddl_white_list(in_offline_ddl_white_list))) {
2142} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2143LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2144} else if (OB_FAIL(schema_service->get_table_sql_service().sync_schema_version_for_history(
2145trans,
2146tmp_schema,
2147new_schema_version))) {
2148RS_LOG(WARN, "fail to sync schema version", K(ret));
2149} else {
2150ObSchemaOperationType operation_type = OB_DDL_ALTER_TABLE;
2151if (OB_FAIL(update_table_attribute(new_table_schema,
2152trans,
2153operation_type))) {
2154LOG_WARN("failed to update data table schema attribute", K(ret));
2155}
2156}
2157}
2158}
2159return ret;
2160}
2161
2162int ObDDLOperator::add_table_foreign_keys(const share::schema::ObTableSchema &orig_table_schema,
2163share::schema::ObTableSchema &inc_table_schema,
2164common::ObMySQLTransaction &trans)
2165{
2166int ret = OB_SUCCESS;
2167ObSchemaService *schema_service = schema_service_.get_schema_service();
2168int64_t new_schema_version = OB_INVALID_VERSION;
2169const uint64_t tenant_id = orig_table_schema.get_tenant_id();
2170
2171if (OB_ISNULL(schema_service)) {
2172ret = OB_ERR_UNEXPECTED;
2173LOG_WARN("schema_service is NULL", K(ret));
2174} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2175LOG_WARN("generate new schema version failed", K(ret));
2176} else {
2177inc_table_schema.set_schema_version(new_schema_version);
2178}
2179if (OB_SUCC(ret)) {
2180if (OB_FAIL(schema_service->get_table_sql_service().add_foreign_key(trans, inc_table_schema, false))) {
2181LOG_WARN("insert foreign keys into inner tables failed", K(ret));
2182} else if (OB_FAIL(schema_service->get_table_sql_service().update_foreign_key_state(trans, inc_table_schema))) {
2183LOG_WARN("update foreign keys enable option into inner table failed", K(ret));
2184} else if (OB_FAIL(sync_version_for_cascade_table(tenant_id, inc_table_schema.get_depend_table_ids(), trans))) {
2185LOG_WARN("fail to sync cascade depend table", K(ret));
2186} else if (OB_FAIL(sync_version_for_cascade_mock_fk_parent_table(orig_table_schema.get_tenant_id(), inc_table_schema.get_depend_mock_fk_parent_table_ids(), trans))) {
2187LOG_WARN("fail to sync cascade depend_mock_fk_parent_table_ids table", K(ret));
2188}
2189}
2190
2191return ret;
2192}
2193
2194int ObDDLOperator::modify_check_constraints_state(
2195const ObTableSchema &orig_table_schema,
2196const ObTableSchema &inc_table_schema,
2197ObTableSchema &new_table_schema,
2198ObMySQLTransaction &trans)
2199{
2200int ret = OB_SUCCESS;
2201UNUSED(orig_table_schema);
2202const uint64_t tenant_id = new_table_schema.get_tenant_id();
2203int64_t new_schema_version = OB_INVALID_VERSION;
2204ObSchemaService *schema_service = schema_service_.get_schema_service();
2205ObTableSchema::const_constraint_iterator iter = inc_table_schema.constraint_begin();
2206
2207if (OB_ISNULL(schema_service)) {
2208ret = OB_ERR_UNEXPECTED;
2209LOG_WARN("schema_service is NULL", K(ret));
2210} else if (inc_table_schema.constraint_end() == iter) {
2211ret = OB_ERR_UNEXPECTED;
2212LOG_WARN("table doesn't have a check constraint", K(ret), K(inc_table_schema));
2213} else if (inc_table_schema.constraint_end() != iter + 1) {
2214ret = OB_ERR_UNEXPECTED;
2215LOG_WARN("update check constraint state couldn't be executed with other DDLs", K(ret), K(inc_table_schema));
2216} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2217LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2218} else {
2219(*iter)->set_schema_version(new_schema_version);
2220if (OB_FAIL(schema_service->get_table_sql_service().update_check_constraint_state(trans, new_table_schema, **iter))) {
2221LOG_WARN("insert single constraint failed", K(ret));
2222}
2223}
2224
2225return ret;
2226}
2227
2228int ObDDLOperator::add_table_constraints(const ObTableSchema &inc_table_schema,
2229ObTableSchema &new_table_schema,
2230ObMySQLTransaction &trans,
2231ObSArray<uint64_t> *cst_ids/*NULL*/)
2232{
2233int ret = OB_SUCCESS;
2234const uint64_t tenant_id = new_table_schema.get_tenant_id();
2235int64_t new_schema_version = OB_INVALID_VERSION;
2236ObSchemaService *schema_service = schema_service_.get_schema_service();
2237if (OB_ISNULL(schema_service)) {
2238ret = OB_ERR_UNEXPECTED;
2239LOG_WARN("schema_service is NULL", K(ret));
2240}
2241for (ObTableSchema::const_constraint_iterator iter = inc_table_schema.constraint_begin(); OB_SUCC(ret) &&
2242iter != inc_table_schema.constraint_end(); iter ++) {
2243uint64_t new_cst_id = OB_INVALID_ID;
2244if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2245LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2246} else if (OB_FAIL(schema_service->fetch_new_constraint_id(tenant_id, new_cst_id))) {
2247LOG_WARN("failed to fetch new constraint id", K(ret));
2248} else {
2249(*iter)->set_schema_version(new_schema_version);
2250(*iter)->set_tenant_id(new_table_schema.get_tenant_id());
2251(*iter)->set_table_id(new_table_schema.get_table_id());
2252(*iter)->set_constraint_id(new_cst_id);
2253(*iter)->set_constraint_type((*iter)->get_constraint_type());
2254if (OB_FAIL(schema_service->get_table_sql_service().insert_single_constraint(trans, new_table_schema, **iter))) {
2255LOG_WARN("insert single constraint failed", K(ret));
2256} else {
2257if (OB_NOT_NULL(cst_ids)) {
2258OZ(cst_ids->push_back(new_cst_id));
2259}
2260}
2261}
2262}
2263return ret;
2264}
2265
2266int ObDDLOperator::add_table_partitions(const ObTableSchema &orig_table_schema,
2267ObTableSchema &inc_table_schema,
2268ObTableSchema &new_table_schema,
2269ObMySQLTransaction &trans)
2270{
2271int ret = OB_SUCCESS;
2272const uint64_t tenant_id = new_table_schema.get_tenant_id();
2273int64_t new_schema_version = OB_INVALID_VERSION;
2274ObSchemaService *schema_service = schema_service_.get_schema_service();
2275if (OB_ISNULL(schema_service)) {
2276ret = OB_ERR_UNEXPECTED;
2277LOG_WARN("schema_service is NULL", K(ret));
2278} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2279LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2280} else if (OB_FAIL(schema_service->get_table_sql_service().add_inc_partition_info(trans,
2281orig_table_schema,
2282inc_table_schema,
2283new_schema_version,
2284false,
2285false))) {
2286LOG_WARN("add inc part info failed", K(ret));
2287} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2288LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2289} else {
2290const int64_t part_num = orig_table_schema.get_part_option().get_part_num();
2291const int64_t inc_part_num = inc_table_schema.get_partition_num();
2292const int64_t all_part_num = part_num + inc_part_num;
2293new_table_schema.get_part_option().set_part_num(all_part_num);
2294new_table_schema.set_schema_version(new_schema_version);
2295if (OB_FAIL(schema_service->get_table_sql_service()
2296.update_partition_option(trans, new_table_schema))) {
2297LOG_WARN("update partition option failed", K(ret), K(part_num), K(inc_part_num));
2298}
2299}
2300return ret;
2301}
2302
2303int ObDDLOperator::add_table_subpartitions(const ObTableSchema &orig_table_schema,
2304ObTableSchema &inc_table_schema,
2305ObTableSchema &new_table_schema,
2306ObMySQLTransaction &trans)
2307{
2308int ret = OB_SUCCESS;
2309const uint64_t tenant_id = new_table_schema.get_tenant_id();
2310int64_t new_schema_version = OB_INVALID_VERSION;
2311ObSchemaService *schema_service = schema_service_.get_schema_service();
2312ObArray<ObPartition*> update_part_array;
2313//FIXME:should move the related logic to ObDDLService
2314if (OB_ISNULL(schema_service)) {
2315ret = OB_ERR_UNEXPECTED;
2316LOG_WARN("schema_service is NULL", K(ret));
2317} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2318LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2319
2320} else if (OB_FAIL(get_part_array_from_table(new_table_schema, inc_table_schema, update_part_array))) {
2321LOG_WARN("fail to get_part_array_from_table", KR(ret));
2322} else if (update_part_array.count() <= 0) {
2323ret = OB_ERR_UNEXPECTED;
2324LOG_WARN("update part array count is not more than 0", KR(ret), K(update_part_array.count()));
2325} else if (update_part_array.count() != inc_table_schema.get_partition_num()) {
2326ret = OB_ERR_UNEXPECTED;
2327LOG_WARN("update_part_array count not equal inc_table part count",
2328KR(ret), K(update_part_array.count()), K(inc_table_schema.get_partition_num()));
2329} else {
2330for (int64_t i = 0; OB_SUCC(ret) && i < update_part_array.count(); i++) {
2331ObPartition *part = update_part_array.at(i);
2332if (OB_ISNULL(part)) {
2333ret = OB_ERR_UNEXPECTED;
2334LOG_WARN("update_part_array[i]", KR(ret), K(i));
2335} else if (i >= inc_table_schema.get_partition_num()) {
2336ret = OB_ERR_UNEXPECTED;
2337LOG_WARN("update_part_array[i] out of inc_part_array", KR(ret), K(i), K(inc_table_schema.get_partition_num()));
2338} else if (OB_ISNULL(inc_table_schema.get_part_array()[i])) {
2339ret = OB_ERR_UNEXPECTED;
2340LOG_WARN("inc_table_part_array[i]", KR(ret), K(i));
2341} else {
2342const int64_t subpart_num = part->get_subpartition_num();
2343const int64_t inc_subpart_num = inc_table_schema.get_part_array()[i]->get_subpartition_num();
2344part->set_sub_part_num(subpart_num + inc_subpart_num);
2345part->set_schema_version(new_schema_version);
2346}
2347}
2348if (OB_SUCC(ret)) {
2349if (OB_FAIL(schema_service->get_table_sql_service().add_inc_partition_info(trans,
2350orig_table_schema,
2351inc_table_schema,
2352new_schema_version,
2353false,
2354true))) {
2355LOG_WARN("add inc part info failed", K(ret));
2356}
2357new_table_schema.set_schema_version(new_schema_version);
2358if (FAILEDx(schema_service->get_table_sql_service().update_subpartition_option(trans,
2359new_table_schema, update_part_array))) {
2360LOG_WARN("update sub partition option failed");
2361}
2362}
2363}
2364return ret;
2365}
2366
2367int ObDDLOperator::truncate_table(const ObString *ddl_stmt_str,
2368const share::schema::ObTableSchema &orig_table_schema,
2369const share::schema::ObTableSchema &new_table_schema,
2370common::ObMySQLTransaction &trans)
2371{
2372int ret = OB_SUCCESS;
2373bool is_truncate_table = true;
2374bool is_truncate_partition = false;
2375uint64_t table_id = new_table_schema.get_table_id();
2376uint64_t schema_version = new_table_schema.get_schema_version();
2377ObSchemaOperationType operation_type = OB_DDL_TRUNCATE_TABLE;
2378ObSchemaService *schema_service = schema_service_.get_schema_service();
2379if (OB_ISNULL(schema_service)) {
2380ret = OB_ERR_UNEXPECTED;
2381LOG_WARN("schema_service is NULL", KR(ret));
2382} else if (new_table_schema.is_partitioned_table()) {
2383if (OB_INVALID_VERSION == schema_version) {
2384ret = OB_ERR_UNEXPECTED;
2385LOG_WARN("schema version is not legal", KR(ret), K(table_id), K(schema_version));
2386} else if (OB_FAIL(schema_service->get_table_sql_service()
2387.drop_inc_part_info(trans,
2388orig_table_schema,
2389orig_table_schema,
2390schema_version,
2391is_truncate_partition,
2392is_truncate_table))) {
2393LOG_WARN("delete part info failed", KR(ret), K(table_id), K(schema_version));
2394} else if (OB_FAIL(schema_service->get_table_sql_service()
2395.add_inc_part_info(trans,
2396orig_table_schema,
2397new_table_schema,
2398schema_version,
2399is_truncate_table))) {
2400LOG_WARN("add part info failed", KR(ret), K(table_id), K(schema_version));
2401}
2402}
2403if (FAILEDx(schema_service->get_table_sql_service()
2404.update_table_attribute(trans,
2405new_table_schema,
2406operation_type,
2407false,
2408ddl_stmt_str))) {
2409LOG_WARN("failed to update table schema attribute", KR(ret), K(table_id), K(schema_version));
2410}
2411return ret;
2412}
2413
2414int ObDDLOperator::update_boundary_schema_version(const uint64_t &tenant_id,
2415const uint64_t &boundary_schema_version,
2416common::ObMySQLTransaction &trans)
2417{
2418int ret = OB_SUCCESS;
2419ObSchemaService *schema_service = schema_service_.get_schema_service();
2420
2421if (OB_ISNULL(schema_service)) {
2422ret = OB_ERR_UNEXPECTED;
2423LOG_WARN("schema_service is NULL", KR(ret));
2424} else {
2425ObSchemaOperation schema_operation;
2426schema_operation.tenant_id_ = tenant_id;
2427schema_operation.op_type_ = OB_DDL_END_SIGN;
2428share::schema::ObDDLSqlService ddl_sql_service(*schema_service);
2429
2430if (OB_FAIL(ddl_sql_service.log_nop_operation(schema_operation,
2431boundary_schema_version,
2432NULL,
2433trans))) {
2434LOG_WARN("log end ddl operation failed", KR(ret), K(tenant_id), K(boundary_schema_version));
2435}
2436}
2437return ret;
2438}
2439
2440int ObDDLOperator::truncate_table_partitions(const share::schema::ObTableSchema &orig_table_schema,
2441share::schema::ObTableSchema &inc_table_schema,
2442share::schema::ObTableSchema &del_table_schema,
2443common::ObMySQLTransaction &trans)
2444{
2445int ret = OB_SUCCESS;
2446const uint64_t tenant_id = orig_table_schema.get_tenant_id();
2447int64_t new_schema_version = OB_INVALID_VERSION;
2448ObSchemaService *schema_service = schema_service_.get_schema_service();
2449if (OB_ISNULL(schema_service)) {
2450ret = OB_ERR_UNEXPECTED;
2451LOG_WARN("schema_service is NULL", KR(ret));
2452} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2453LOG_WARN("fail to gen new schema_version", KR(ret), K(tenant_id));
2454} else if (OB_FAIL(schema_service->get_table_sql_service().truncate_part_info(
2455trans,
2456orig_table_schema,
2457inc_table_schema,
2458del_table_schema,
2459new_schema_version))) {
2460LOG_WARN("delete inc part info failed", KR(ret));
2461}
2462
2463return ret;
2464}
2465
2466int ObDDLOperator::truncate_table_subpartitions(const share::schema::ObTableSchema &orig_table_schema,
2467share::schema::ObTableSchema &inc_table_schema,
2468share::schema::ObTableSchema &del_table_schema,
2469common::ObMySQLTransaction &trans)
2470{
2471int ret = OB_SUCCESS;
2472const uint64_t tenant_id = orig_table_schema.get_tenant_id();
2473int64_t new_schema_version = OB_INVALID_VERSION;
2474ObSchemaService *schema_service = schema_service_.get_schema_service();
2475if (OB_ISNULL(schema_service)) {
2476ret = OB_ERR_UNEXPECTED;
2477LOG_WARN("schema_service is NULL", KR(ret));
2478} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2479LOG_WARN("fail to gen new schema_version", KR(ret), K(tenant_id));
2480} else if (OB_FAIL(schema_service->get_table_sql_service().truncate_subpart_info(
2481trans,
2482orig_table_schema,
2483inc_table_schema,
2484del_table_schema,
2485new_schema_version))) {
2486LOG_WARN("delete inc part info failed", KR(ret));
2487}
2488
2489return ret;
2490}
2491
2492int ObDDLOperator::add_table_partitions(const ObTableSchema &orig_table_schema,
2493ObTableSchema &alter_table_schema,
2494const int64_t schema_version,
2495ObMySQLTransaction &trans)
2496{
2497int ret = OB_SUCCESS;
2498ObSchemaService *schema_service = schema_service_.get_schema_service();
2499if (OB_ISNULL(schema_service)) {
2500ret = OB_ERR_UNEXPECTED;
2501LOG_WARN("schema_service is NULL", K(ret));
2502} else if (schema_version <= 0) {
2503ret = OB_INVALID_ARGUMENT;
2504LOG_WARN("schema_version is invalid", K(ret), K(schema_version));
2505} else {
2506const int64_t inc_part_num = alter_table_schema.get_part_option().get_part_num();
2507if (OB_FAIL(schema_service->get_table_sql_service().add_inc_partition_info(trans,
2508orig_table_schema,
2509alter_table_schema,
2510schema_version,
2511false,
2512false))) {
2513LOG_WARN("add inc part info failed", K(ret));
2514} else {
2515const int64_t part_num = orig_table_schema.get_part_option().get_part_num();
2516const int64_t all_part_num = part_num + inc_part_num;
2517ObTableSchema new_table_schema;
2518if (OB_FAIL(new_table_schema.assign(orig_table_schema))) {
2519LOG_WARN("fail to assign schema", K(ret));
2520} else {
2521new_table_schema.get_part_option().set_part_num(all_part_num);
2522}
2523if (OB_FAIL(ret)) {
2524} else if (OB_FAIL(schema_service->get_table_sql_service()
2525.update_partition_option(trans, new_table_schema, schema_version))) {
2526LOG_WARN("update partition option failed", K(ret), K(part_num), K(inc_part_num));
2527}
2528}
2529}
2530return ret;
2531}
2532
2533int ObDDLOperator::get_part_array_from_table(const ObTableSchema &new_table_schema,
2534const ObTableSchema &inc_table_schema,
2535ObIArray<ObPartition*> &out_part_array)
2536{
2537int ret = OB_SUCCESS;
2538
2539ObPartition **inc_part_array = inc_table_schema.get_part_array();
2540const int64_t inc_part_sum = inc_table_schema.get_partition_num();
2541for (int64_t i = 0; OB_SUCC(ret) && i < inc_part_sum; i++) {
2542ObPartition *inc_part = inc_part_array[i];
2543if (OB_ISNULL(inc_part)) {
2544ret = OB_ERR_UNEXPECTED;
2545LOG_WARN("inc_part_array[i] is null", KR(ret), K(i));
2546} else {
2547ObPartition **part_array = new_table_schema.get_part_array();
2548const int64_t part_sum = new_table_schema.get_partition_num();
2549int64_t j = 0;
2550for (j = 0; OB_SUCC(ret) && j < part_sum; j++) {
2551ObPartition *part = part_array[j];
2552if (OB_ISNULL(part)) {
2553ret = OB_ERR_UNEXPECTED;
2554LOG_WARN("part_array[j] is NULL", K(ret), K(j));
2555} else if (part->get_part_id() == inc_part->get_part_id()) {
2556if (OB_FAIL(out_part_array.push_back(part))) {
2557LOG_WARN("push back failed", KR(ret), K(j));
2558}
2559break;
2560}
2561}
2562if (OB_SUCC(ret) && j >= part_sum) {
2563ret = OB_ERR_UNEXPECTED;
2564LOG_WARN("inc_part_array[i] is not in part_array",
2565KR(ret), K(i), KPC(inc_part), K(new_table_schema));
2566}
2567}
2568}
2569return ret;
2570}
2571
2572bool ObDDLOperator::is_list_values_equal(const common::ObRowkey &fir_values,
2573const common::ObIArray<common::ObNewRow> &sed_values)
2574{
2575bool equal = false;
2576int64_t s_count = sed_values.count();
2577common::ObRowkey rowkey;
2578for (int64_t j = 0; j < s_count; ++j) {
2579rowkey.reset();
2580rowkey.assign(sed_values.at(j).cells_, sed_values.at(j).count_);
2581if (fir_values == rowkey) {
2582equal = true;
2583break;
2584}
2585}
2586return equal;
2587}
2588
2589int ObDDLOperator::check_part_equal(
2590const share::schema::ObPartitionFuncType part_type,
2591const share::schema::ObPartition *r_part,
2592const share::schema::ObPartition *l_part,
2593bool &is_equal)
2594{
2595int ret = OB_SUCCESS;
2596is_equal = false;
2597
2598if (OB_ISNULL(r_part) || OB_ISNULL(l_part)) {
2599ret = OB_ERR_UNEXPECTED;
2600LOG_WARN("ptr is NULL", K(ret), K(r_part), K(l_part));
2601} else if (share::schema::is_range_part(part_type)) {
2602if (r_part->get_high_bound_val() == l_part->get_high_bound_val()) {
2603is_equal = true;
2604}
2605} else if (share::schema::is_list_part(part_type)) {
2606if (0 == r_part->get_list_row_values().count()) {
2607ret = OB_ERR_UNEXPECTED;
2608LOG_WARN("list values is empty", K(ret));
2609} else {
2610common::ObRowkey rowkey;
2611rowkey.assign(r_part->get_list_row_values().at(0).cells_,
2612r_part->get_list_row_values().at(0).count_);
2613if (is_list_values_equal(rowkey, l_part->get_list_row_values())) {
2614is_equal = true;
2615}
2616}
2617}
2618
2619return ret;
2620}
2621
2622int ObDDLOperator::rename_table_partitions(const ObTableSchema &orig_table_schema,
2623ObTableSchema &inc_table_schema,
2624ObTableSchema &new_table_schema,
2625ObMySQLTransaction &trans)
2626{
2627int ret = OB_SUCCESS;
2628const uint64_t tenant_id = orig_table_schema.get_tenant_id();
2629int64_t new_schema_version = OB_INVALID_VERSION;
2630ObSchemaService *schema_service = schema_service_.get_schema_service();
2631if (OB_ISNULL(schema_service)) {
2632ret = OB_ERR_UNEXPECTED;
2633LOG_WARN("schema_service is NULL", KR(ret));
2634} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2635LOG_WARN("fail to gen new schema version", KR(ret), K(tenant_id));
2636} else if (OB_FAIL(schema_service->get_table_sql_service().rename_inc_part_info(trans,
2637orig_table_schema,
2638inc_table_schema,
2639new_schema_version))) {
2640LOG_WARN("rename inc part info failed", KR(ret));
2641}
2642return ret;
2643}
2644
2645int ObDDLOperator::rename_table_subpartitions(const ObTableSchema &orig_table_schema,
2646ObTableSchema &inc_table_schema,
2647ObTableSchema &new_table_schema,
2648ObMySQLTransaction &trans)
2649{
2650int ret = OB_SUCCESS;
2651const uint64_t tenant_id = orig_table_schema.get_tenant_id();
2652int64_t new_schema_version = OB_INVALID_VERSION;
2653ObSchemaService *schema_service = schema_service_.get_schema_service();
2654if (OB_ISNULL(schema_service)) {
2655ret = OB_ERR_UNEXPECTED;
2656LOG_WARN("schema_service is NULL", KR(ret));
2657} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2658LOG_WARN("fail to gen new schema version", KR(ret), K(tenant_id));
2659} else if (OB_FAIL(schema_service->get_table_sql_service().rename_inc_subpart_info(trans,
2660orig_table_schema,
2661inc_table_schema,
2662new_schema_version))) {
2663LOG_WARN("rename inc subpart info failed", KR(ret));
2664}
2665return ret;
2666}
2667
2668int ObDDLOperator::drop_table_partitions(const ObTableSchema &orig_table_schema,
2669ObTableSchema &inc_table_schema,
2670ObTableSchema &new_table_schema,
2671ObMySQLTransaction &trans)
2672{
2673int ret = OB_SUCCESS;
2674bool is_truncate_table = false;
2675bool is_truncate_partition = false;
2676const uint64_t tenant_id = orig_table_schema.get_tenant_id();
2677int64_t new_schema_version = OB_INVALID_VERSION;
2678ObSchemaService *schema_service = schema_service_.get_schema_service();
2679if (OB_ISNULL(schema_service)) {
2680ret = OB_ERR_UNEXPECTED;
2681LOG_WARN("schema_service is NULL", K(ret));
2682} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2683LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2684}
2685if (OB_FAIL(ret)) {
2686} else if (OB_FAIL(schema_service->get_table_sql_service().drop_inc_part_info(trans,
2687orig_table_schema,
2688inc_table_schema,
2689new_schema_version,
2690is_truncate_partition,
2691is_truncate_table))) {
2692LOG_WARN("delete inc part info failed", K(ret));
2693} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2694LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2695} else {
2696const int64_t part_num = orig_table_schema.get_part_option().get_part_num();
2697const int64_t inc_part_num = inc_table_schema.get_partition_num();
2698const int64_t all_part_num = part_num - inc_part_num;
2699new_table_schema.get_part_option().set_part_num(all_part_num);
2700new_table_schema.set_schema_version(new_schema_version);
2701if (OB_FAIL(schema_service->get_table_sql_service()
2702.update_partition_option(trans, new_table_schema))) {
2703LOG_WARN("update partition option failed", K(ret), K(part_num), K(inc_part_num));
2704}
2705}
2706return ret;
2707}
2708
2709int ObDDLOperator::drop_table_subpartitions(const ObTableSchema &orig_table_schema,
2710ObTableSchema &inc_table_schema,
2711ObTableSchema &new_table_schema,
2712ObMySQLTransaction &trans)
2713{
2714int ret = OB_SUCCESS;
2715const uint64_t tenant_id = orig_table_schema.get_tenant_id();
2716int64_t new_schema_version = OB_INVALID_VERSION;
2717ObSchemaService *schema_service = schema_service_.get_schema_service();
2718if (OB_ISNULL(schema_service)) {
2719ret = OB_ERR_UNEXPECTED;
2720LOG_WARN("schema_service is NULL", K(ret));
2721} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2722LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2723} else if (OB_FAIL(schema_service->get_table_sql_service().drop_inc_subpart_info(trans,
2724orig_table_schema,
2725inc_table_schema,
2726new_schema_version))) {
2727LOG_WARN("delete inc part info failed", K(ret));
2728} else {
2729//FIXME:should move the related logic to ObDDLService
2730ObArray<ObPartition*> update_part_array;
2731if (OB_FAIL(get_part_array_from_table(new_table_schema, inc_table_schema, update_part_array))) {
2732LOG_WARN("fail to get part array from tableschema", KR(ret));
2733} else if (update_part_array.count() <= 0) {
2734ret = OB_ERR_UNEXPECTED;
2735LOG_WARN("update_part_array count less than 0", K(ret), K(update_part_array.count()));
2736} else if (update_part_array.count() != inc_table_schema.get_partition_num()) {
2737ret = OB_ERR_UNEXPECTED;
2738LOG_WARN("update_part_array count not equal inc_table part count",
2739KR(ret), K(update_part_array.count()), K(inc_table_schema.get_partition_num()));
2740} else {
2741for (int64_t i = 0; OB_SUCC(ret) && i < update_part_array.count(); i++) {
2742ObPartition *part = update_part_array.at(i);
2743if (i >= inc_table_schema.get_partition_num()) {
2744ret = OB_ERR_UNEXPECTED;
2745LOG_WARN("update_part_array[i] out of inc_part_array", KR(ret), K(i), K(inc_table_schema.get_partition_num()));
2746} else if (OB_ISNULL(part)) {
2747ret = OB_ERR_UNEXPECTED;
2748LOG_WARN("update_part_array[i]", KR(ret), K(i));
2749} else if (OB_ISNULL(inc_table_schema.get_part_array()[i])) {
2750ret = OB_ERR_UNEXPECTED;
2751LOG_WARN("inc_table_part_array[i]", KR(ret), K(i));
2752} else {
2753const int64_t subpart_num = part->get_sub_part_num();
2754const int64_t inc_subpart_num = inc_table_schema.get_part_array()[i]->get_subpartition_num();
2755part->set_sub_part_num(subpart_num - inc_subpart_num);
2756part->set_schema_version(new_schema_version);
2757}
2758}
2759if (OB_SUCC(ret)) {
2760new_table_schema.set_schema_version(new_schema_version);
2761if (OB_FAIL(schema_service->get_table_sql_service().update_subpartition_option(trans,
2762new_table_schema, update_part_array))) {
2763LOG_WARN("update sub partition option failed");
2764}
2765}
2766}
2767}
2768return ret;
2769}
2770
2771int ObDDLOperator::drop_table_constraints(const ObTableSchema &orig_table_schema,
2772const ObTableSchema &inc_table_schema,
2773ObTableSchema &new_table_schema,
2774ObMySQLTransaction &trans)
2775{
2776int ret = OB_SUCCESS;
2777const uint64_t tenant_id = orig_table_schema.get_tenant_id();
2778int64_t new_schema_version = OB_INVALID_VERSION;
2779UNUSED(orig_table_schema);
2780
2781ObSchemaService *schema_service = schema_service_.get_schema_service();
2782if (OB_ISNULL(schema_service)) {
2783ret = OB_ERR_UNEXPECTED;
2784LOG_WARN("schema_service is NULL", K(ret));
2785} else {
2786for (ObTableSchema::const_constraint_iterator iter = inc_table_schema.constraint_begin(); OB_SUCC(ret) &&
2787iter != inc_table_schema.constraint_end(); iter ++) {
2788(*iter)->set_tenant_id(orig_table_schema.get_tenant_id());
2789(*iter)->set_table_id(orig_table_schema.get_table_id());
2790if (nullptr == new_table_schema.get_constraint((*iter)->get_constraint_id())) {
2791LOG_INFO("constraint has already been dropped", K(ret), K(**iter));
2792} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2793LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2794} else if (OB_FAIL(schema_service->get_table_sql_service().delete_single_constraint(
2795new_schema_version, trans, new_table_schema, **iter))) {
2796RS_LOG(WARN, "failed to delete constraint", K(ret));
2797}
2798}
2799}
2800return ret;
2801}
2802
2803int ObDDLOperator::drop_table_partitions(const ObTableSchema &orig_table_schema,
2804ObTableSchema &inc_table_schema,
2805const int64_t schema_version,
2806ObMySQLTransaction &trans)
2807{
2808int ret = OB_SUCCESS;
2809ObSchemaService *schema_service = schema_service_.get_schema_service();
2810bool is_truncate_table = false;
2811bool is_truncate_partition = false;
2812if (OB_ISNULL(schema_service)) {
2813ret = OB_ERR_UNEXPECTED;
2814LOG_WARN("schema_service is NULL", K(ret));
2815} else if (schema_version <= 0) {
2816ret = OB_INVALID_ARGUMENT;
2817LOG_WARN("schema_version is invalid", K(ret), K(schema_version));
2818} else if (OB_FAIL(schema_service->get_table_sql_service().drop_inc_part_info(
2819trans,
2820orig_table_schema,
2821inc_table_schema,
2822schema_version,
2823is_truncate_partition,
2824is_truncate_table))) {
2825LOG_WARN("delete inc part info failed", K(ret));
2826} else {
2827ObTableSchema new_table_schema;
2828if (OB_FAIL(new_table_schema.assign(orig_table_schema))) {
2829LOG_WARN("fail to assign schema", K(ret));
2830} else {
2831const int64_t part_num = orig_table_schema.get_part_option().get_part_num();
2832const int64_t inc_part_num = inc_table_schema.get_part_option().get_part_num();
2833const int64_t all_part_num = part_num - inc_part_num;
2834new_table_schema.get_part_option().set_part_num(all_part_num);
2835if (OB_FAIL(schema_service->get_table_sql_service()
2836.update_partition_option(trans, new_table_schema, schema_version))) {
2837LOG_WARN("update partition option failed", K(ret), K(part_num), K(inc_part_num));
2838}
2839}
2840}
2841return ret;
2842}
2843
2844int ObDDLOperator::insert_column_groups(ObMySQLTransaction &trans, const ObTableSchema &new_table_schema)
2845{
2846int ret = OB_SUCCESS;
2847int64_t new_schema_version = OB_INVALID_VERSION;
2848const uint64_t tenant_id = new_table_schema.get_tenant_id();
2849ObSchemaService *schema_service = schema_service_.get_schema_service();
2850if (OB_ISNULL(schema_service)) {
2851ret = OB_ERR_UNEXPECTED;
2852LOG_WARN("schema_service is NULL", K(ret));
2853} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2854LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2855} else if (OB_FAIL(schema_service->get_table_sql_service().add_column_groups(trans, new_table_schema, new_schema_version))) {
2856LOG_WARN("insert alter column group failed", K(ret), K(new_table_schema));
2857}
2858return ret;
2859}
2860
2861int ObDDLOperator::insert_column_ids_into_column_group(ObMySQLTransaction &trans,
2862const ObTableSchema &new_table_schema,
2863const ObIArray<uint64_t> &column_ids,
2864const ObColumnGroupSchema &column_group)
2865{
2866int ret = OB_SUCCESS;
2867int64_t new_schema_version = OB_INVALID_VERSION;
2868const uint64_t tenant_id = new_table_schema.get_tenant_id();
2869ObSchemaService *schema_service = schema_service_.get_schema_service();
2870if (OB_ISNULL(schema_service)) {
2871ret = OB_ERR_UNEXPECTED;
2872LOG_WARN("schema_service is NULL", K(ret));
2873} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2874LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2875} else if (OB_FAIL(schema_service->get_table_sql_service().insert_column_ids_into_column_group(trans, new_table_schema, new_schema_version, column_ids, column_group))) {
2876LOG_WARN("insert alter column group failed", K(ret), K(new_table_schema), K(column_group));
2877}
2878return ret;
2879}
2880
2881
2882int ObDDLOperator::insert_single_column(ObMySQLTransaction &trans,
2883const ObTableSchema &new_table_schema,
2884ObColumnSchemaV2 &new_column)
2885{
2886int ret = OB_SUCCESS;
2887const uint64_t tenant_id = new_table_schema.get_tenant_id();
2888int64_t new_schema_version = OB_INVALID_VERSION;
2889ObSchemaService *schema_service = schema_service_.get_schema_service();
2890if (OB_ISNULL(schema_service)) {
2891ret = OB_ERR_UNEXPECTED;
2892LOG_WARN("schema_service is NULL", K(ret));
2893} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2894LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2895} else if (FALSE_IT(new_column.set_schema_version(new_schema_version))) {
2896//do nothing
2897} else if (OB_FAIL(schema_service->get_table_sql_service().insert_single_column(
2898trans, new_table_schema, new_column, true))) {
2899LOG_WARN("insert single column failed", K(ret));
2900}
2901return ret;
2902}
2903
2904int ObDDLOperator::delete_single_column(ObMySQLTransaction &trans,
2905ObTableSchema &new_table_schema,
2906const ObString &column_name)
2907{
2908int ret = OB_SUCCESS;
2909const uint64_t tenant_id = new_table_schema.get_tenant_id();
2910int64_t new_schema_version = OB_INVALID_VERSION;
2911ObColumnSchemaV2 *orig_column = NULL;
2912ObSchemaService *schema_service = schema_service_.get_schema_service();
2913if (OB_ISNULL(schema_service)) {
2914ret = OB_ERR_UNEXPECTED;
2915LOG_WARN("schema_service is NULL", K(ret));
2916} else if (OB_ISNULL(orig_column = new_table_schema.get_column_schema(column_name))) {
2917ret = OB_ERR_UNEXPECTED;
2918LOG_WARN("get column schema from table failed", K(column_name));
2919} else if (OB_FAIL(new_table_schema.delete_column(column_name))) {
2920//drop column will do some check on the new table schema
2921RS_LOG(WARN, "failed to drop column schema", K(ret), K(column_name));
2922} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2923LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2924} else if (OB_FAIL(schema_service->get_table_sql_service().delete_single_column(
2925new_schema_version, trans, new_table_schema, *orig_column))) {
2926RS_LOG(WARN, "failed to delete column", K(orig_column), K(ret));
2927}
2928return ret;
2929}
2930
2931int ObDDLOperator::alter_table_create_index(const ObTableSchema &new_table_schema,
2932ObIArray<ObColumnSchemaV2*> &gen_columns,
2933ObTableSchema &index_schema,
2934common::ObMySQLTransaction &trans)
2935{
2936int ret = OB_SUCCESS;
2937const uint64_t tenant_id = new_table_schema.get_tenant_id();
2938int64_t new_schema_version = OB_INVALID_VERSION;
2939ObSchemaService *schema_service = schema_service_.get_schema_service();
2940if (OB_ISNULL(schema_service)) {
2941ret = OB_ERR_UNEXPECTED;
2942LOG_WARN("schema_service is NULL", K(ret));
2943} else {
2944uint64_t index_table_id = OB_INVALID_ID;
2945//index schema can't not create with specified table id
2946if (OB_UNLIKELY(index_schema.get_table_id() != OB_INVALID_ID)) {
2947ret = OB_ERR_UNEXPECTED;
2948LOG_WARN("table_id of index should be invalid", K(ret), K(index_schema.get_table_id()));
2949} else if (OB_FAIL(schema_service->fetch_new_table_id(
2950new_table_schema.get_tenant_id(), index_table_id))) {
2951RS_LOG(WARN, "failed to update_max_used_table_id, ", K(ret));
2952} else {
2953index_schema.set_table_id(index_table_id);
2954}
2955if (OB_SUCC(ret)) {
2956if (gen_columns.empty()) {
2957//create normal index table.
2958if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2959LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2960} else {
2961index_schema.set_schema_version(new_schema_version);
2962if (OB_FAIL(schema_service->get_table_sql_service().create_table(index_schema, trans))) {
2963RS_LOG(WARN, "alter table create index failed", K(index_schema), K(ret));
2964}
2965}
2966} else {
2967// First increase internal generated column, and then create an index on the column
2968for (int64_t i = 0; OB_SUCC(ret) && i < gen_columns.count(); ++i) {
2969ObColumnSchemaV2 *new_column_schema = gen_columns.at(i);
2970if (OB_ISNULL(new_column_schema)) {
2971ret = OB_ERR_UNEXPECTED;
2972LOG_WARN("new column schema is null");
2973} else if (OB_FAIL(insert_single_column(trans, new_table_schema, *new_column_schema))) {
2974LOG_WARN("failed to create table schema, ", K(ret));
2975}
2976}
2977if (OB_SUCC(ret)) {
2978if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
2979LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
2980} else {
2981index_schema.set_schema_version(new_schema_version);
2982if (OB_FAIL(schema_service->get_table_sql_service().create_table(index_schema, trans))) {
2983LOG_WARN("failed to create index schema", K(ret));
2984}
2985}
2986}
2987}
2988}
2989}
2990return ret;
2991}
2992
2993int ObDDLOperator::alter_table_drop_index(
2994const ObTableSchema *index_table_schema,
2995ObTableSchema &new_data_table_schema,
2996common::ObMySQLTransaction &trans)
2997{
2998int ret = OB_SUCCESS;
2999ObSchemaService *schema_service = schema_service_.get_schema_service();
3000//drop inner generated index column
3001ObSchemaGetterGuard schema_guard;
3002if (OB_ISNULL(index_table_schema)) {
3003ret = OB_INVALID_ARGUMENT;
3004LOG_WARN("invalid arguments", K(ret), KP(index_table_schema));
3005} else if (OB_ISNULL(schema_service)) {
3006ret = OB_ERR_UNEXPECTED;
3007LOG_WARN("schema_service is NULL", K(ret));
3008} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(index_table_schema->get_tenant_id(), schema_guard))) {
3009LOG_WARN("failed to get schema guard", K(ret));
3010} else if (OB_FAIL(drop_inner_generated_index_column(trans, schema_guard, *index_table_schema, new_data_table_schema))) {
3011LOG_WARN("drop inner generated index column failed", K(ret));
3012} else if (OB_FAIL(drop_table(*index_table_schema, trans))) { // drop index table
3013RS_LOG(WARN, "ddl_operator drop_table failed",
3014"table schema", *index_table_schema, K(ret));
3015}
3016if (OB_SUCC(ret)) {
3017RS_LOG(INFO, "finish drop index", K(*index_table_schema), K(ret));
3018}
3019return ret;
3020}
3021
3022int ObDDLOperator::alter_table_alter_index(
3023const uint64_t tenant_id,
3024const uint64_t data_table_id,
3025const uint64_t database_id,
3026const ObAlterIndexArg &alter_index_arg,
3027ObMySQLTransaction &trans)
3028{
3029int ret = OB_SUCCESS;
3030ObSchemaService *schema_service = schema_service_.get_schema_service();
3031ObSchemaGetterGuard schema_guard;
3032if (OB_ISNULL(schema_service)) {
3033ret = OB_ERR_UNEXPECTED;
3034LOG_WARN("schema_service is NULL", K(ret));
3035} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
3036LOG_WARN("failed to get schema guard", K(ret));
3037} else {
3038const uint64_t tenant_id = alter_index_arg.tenant_id_;
3039int64_t new_schema_version = OB_INVALID_VERSION;
3040RS_LOG(INFO, "start alter table alter index", K(alter_index_arg));
3041const ObTableSchema *index_table_schema = NULL;
3042ObString index_table_name;
3043ObArenaAllocator allocator(ObModIds::OB_SCHEMA);
3044const ObString &index_name = alter_index_arg.index_name_;
3045
3046//build index name and get index schema
3047if (OB_FAIL(ObTableSchema::build_index_table_name(allocator,
3048data_table_id,
3049index_name,
3050index_table_name))) {
3051RS_LOG(WARN, "build_index_table_name failed", K(data_table_id), K(index_name), K(ret));
3052} else {
3053const bool is_index = true;
3054ObTableSchema new_index_table_schema;
3055if (OB_FAIL(schema_guard.get_table_schema(tenant_id,
3056database_id,
3057index_table_name,
3058is_index,
3059index_table_schema))) {
3060LOG_WARN("fail to get table schema", K(ret), K(tenant_id), K(database_id), K(index_table_schema));
3061} else if (OB_UNLIKELY(NULL == index_table_schema)) {
3062ret = OB_ERR_UNEXPECTED;
3063RS_LOG(WARN, "get index table schema failed", K(tenant_id),
3064K(database_id), K(index_table_name), K(ret));
3065} else if (index_table_schema->is_in_recyclebin()) {
3066ret = OB_ERR_OPERATION_ON_RECYCLE_OBJECT;
3067RS_LOG(WARN, "index table is in recyclebin", K(ret));
3068} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
3069LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
3070} else if (OB_FAIL(new_index_table_schema.assign(*index_table_schema))) {
3071LOG_WARN("fail to assign schema", K(ret));
3072} else {
3073new_index_table_schema.set_index_visibility(alter_index_arg.index_visibility_);
3074new_index_table_schema.set_schema_version(new_schema_version);
3075if(OB_FAIL(schema_service->get_table_sql_service().update_table_options(
3076trans,
3077*index_table_schema,
3078new_index_table_schema,
3079index_table_schema->is_global_index_table() ? OB_DDL_ALTER_GLOBAL_INDEX: OB_DDL_ALTER_TABLE))) {
3080RS_LOG(WARN, "schema service update_table_options failed",
3081K(*index_table_schema), K(ret));
3082}
3083}
3084}
3085RS_LOG(INFO, "finish alter table alter index", K(alter_index_arg), K(ret));
3086}
3087return ret;
3088}
3089
3090// description: delete foreign key of table in a transaction
3091//
3092// @param [in] table_schema
3093// @param [in] drop_foreign_key_arg
3094// @param [in] trans
3095//
3096// @return oceanbase error code defined in lib/ob_errno.def
3097int ObDDLOperator::alter_table_drop_foreign_key(const ObTableSchema &table_schema,
3098const ObDropForeignKeyArg &drop_foreign_key_arg,
3099ObMySQLTransaction &trans,
3100const ObForeignKeyInfo *&parent_table_mock_foreign_key_info,
3101const bool parent_table_in_offline_ddl_white_list)
3102{
3103int ret = OB_SUCCESS;
3104const uint64_t tenant_id = table_schema.get_tenant_id();
3105int64_t new_schema_version = OB_INVALID_VERSION;
3106ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
3107ObTableSqlService *table_sql_service = NULL;
3108const ObString &foreign_key_name = drop_foreign_key_arg.foreign_key_name_;
3109if (OB_ISNULL(schema_service_impl)) {
3110ret = OB_ERR_SYS;
3111LOG_ERROR("schema_service_impl must not null", K(ret));
3112} else if (FALSE_IT(table_sql_service = &schema_service_impl->get_table_sql_service())) {
3113} else {
3114const ObIArray<ObForeignKeyInfo> &foreign_key_infos = table_schema.get_foreign_key_infos();
3115const ObForeignKeyInfo *foreign_key_info = NULL;
3116for (int64_t i = 0; OB_SUCC(ret) && i < foreign_key_infos.count(); i++) {
3117if (0 == foreign_key_name.case_compare(foreign_key_infos.at(i).foreign_key_name_)
3118&& table_schema.get_table_id() == foreign_key_infos.at(i).child_table_id_) {
3119foreign_key_info = &foreign_key_infos.at(i);
3120break;
3121}
3122}
3123if (OB_SUCC(ret) && OB_ISNULL(foreign_key_info)) {
3124bool is_oracle_mode = false;
3125if (OB_FAIL(ObCompatModeGetter::check_is_oracle_mode_with_table_id(
3126table_schema.get_tenant_id(), table_schema.get_table_id(), is_oracle_mode))) {
3127LOG_WARN("fail to check is oracle mode", K(ret), K(table_schema));
3128} else if (is_oracle_mode) {
3129ret = OB_ERR_NONEXISTENT_CONSTRAINT;
3130LOG_WARN("Cannot drop foreign key constraint - nonexistent constraint", K(ret), K(foreign_key_name), K(table_schema.get_table_name_str()));
3131} else {
3132ret = OB_ERR_CANT_DROP_FIELD_OR_KEY;
3133LOG_USER_ERROR(OB_ERR_CANT_DROP_FIELD_OR_KEY, foreign_key_name.length(), foreign_key_name.ptr());
3134LOG_WARN("Cannot drop foreign key constraint - nonexistent constraint", K(ret), K(foreign_key_name), K(table_schema.get_table_name_str()));
3135}
3136}
3137if (OB_FAIL(ret)) {
3138} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
3139LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
3140} else if (OB_FAIL(table_sql_service->drop_foreign_key(
3141new_schema_version, trans, table_schema, foreign_key_info, parent_table_in_offline_ddl_white_list))) {
3142LOG_WARN("failed to drop foreign key", K(ret), K(foreign_key_name));
3143} else if (foreign_key_info->is_parent_table_mock_) {
3144parent_table_mock_foreign_key_info = foreign_key_info;
3145}
3146}
3147return ret;
3148}
3149
3150int ObDDLOperator::create_mock_fk_parent_table(
3151ObMySQLTransaction &trans,
3152const share::schema::ObMockFKParentTableSchema &mock_fk_parent_table_schema,
3153const bool need_update_foreign_key)
3154{
3155int ret = OB_SUCCESS;
3156ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
3157if (OB_FAIL(schema_service_impl->get_table_sql_service().add_mock_fk_parent_table(
3158&trans, mock_fk_parent_table_schema, need_update_foreign_key))) {
3159LOG_WARN("insert mock_fk_parent_table failed", K(ret), K(mock_fk_parent_table_schema));
3160} else if (need_update_foreign_key) { // if need_update_foreign_key, then need_sync_version_for_cascade_child_table
3161ObArray<uint64_t> child_table_ids;
3162for (int64_t i = 0; OB_SUCC(ret) && i < mock_fk_parent_table_schema.get_foreign_key_infos().count(); ++i) {
3163if (OB_FAIL(child_table_ids.push_back(mock_fk_parent_table_schema.get_foreign_key_infos().at(i).child_table_id_))) {
3164LOG_WARN("fail to push back child_table_id", K(ret), K(mock_fk_parent_table_schema.get_foreign_key_infos().at(i)));
3165}
3166}
3167if (FAILEDx(sync_version_for_cascade_table(mock_fk_parent_table_schema.get_tenant_id(), child_table_ids, trans))) {
3168LOG_WARN("fail to sync versin for children tables", K(ret), K(child_table_ids));
3169}
3170}
3171return ret;
3172}
3173
3174int ObDDLOperator::alter_mock_fk_parent_table(
3175ObMySQLTransaction &trans,
3176share::schema::ObMockFKParentTableSchema &mock_fk_parent_table_schema)
3177{
3178int ret = OB_SUCCESS;
3179ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
3180if (OB_FAIL(schema_service_impl->get_table_sql_service().alter_mock_fk_parent_table(
3181&trans, mock_fk_parent_table_schema))) {
3182LOG_WARN("alter mock_fk_parent_table failed", K(ret), K(mock_fk_parent_table_schema));
3183}
3184return ret;
3185}
3186
3187int ObDDLOperator::drop_mock_fk_parent_table(
3188ObMySQLTransaction &trans,
3189const share::schema::ObMockFKParentTableSchema &mock_fk_parent_table_schema)
3190{
3191int ret = OB_SUCCESS;
3192ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
3193if (OB_FAIL(schema_service_impl->get_table_sql_service().drop_mock_fk_parent_table(
3194&trans, mock_fk_parent_table_schema))) {
3195LOG_WARN("drop mock_fk_parent_table failed", K(ret), K(mock_fk_parent_table_schema));
3196}
3197return ret;
3198}
3199
3200int ObDDLOperator::replace_mock_fk_parent_table(
3201ObMySQLTransaction &trans,
3202share::schema::ObSchemaGetterGuard &schema_guard,
3203const share::schema::ObMockFKParentTableSchema &mock_fk_parent_table_schema)
3204{
3205int ret = OB_SUCCESS;
3206ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
3207const ObMockFKParentTableSchema *ori_mock_fk_parent_table_schema_ptr = NULL;
3208if (OB_FAIL(schema_guard.get_mock_fk_parent_table_schema_with_id(
3209mock_fk_parent_table_schema.get_tenant_id(), mock_fk_parent_table_schema.get_mock_fk_parent_table_id(),
3210ori_mock_fk_parent_table_schema_ptr))) {
3211LOG_WARN("check_mock_fk_parent_table_exist_by_id failed", K(ret), K(mock_fk_parent_table_schema.get_tenant_id()), K(mock_fk_parent_table_schema));
3212} else if (OB_ISNULL(ori_mock_fk_parent_table_schema_ptr)) {
3213ret = OB_ERR_UNEXPECTED;
3214LOG_WARN("ori_mock_fk_parent_table_schema_ptr is null", K(ret), KPC(ori_mock_fk_parent_table_schema_ptr), K(mock_fk_parent_table_schema));
3215} else if (mock_fk_parent_table_schema.get_foreign_key_infos().count() <= 0) {
3216ret = OB_ERR_UNEXPECTED;
3217LOG_WARN("count of foreign_key_infos in mock_fk_parent_table_schema is zero", K(ret), KPC(ori_mock_fk_parent_table_schema_ptr), K(mock_fk_parent_table_schema.get_foreign_key_infos().count()));
3218} else if (OB_FAIL(schema_service_impl->get_table_sql_service().replace_mock_fk_parent_table(
3219&trans, mock_fk_parent_table_schema, ori_mock_fk_parent_table_schema_ptr))) {
3220LOG_WARN("replace mock_fk_parent_table failed", K(ret), KPC(ori_mock_fk_parent_table_schema_ptr), K(mock_fk_parent_table_schema));
3221} else { // update schema version of child tables and new parent table after replace mock_fk_parent_table with new parent table
3222ObArray<uint64_t> child_table_ids;
3223uint64_t new_parent_table_id = mock_fk_parent_table_schema.get_foreign_key_infos().at(0).parent_table_id_;
3224for (int64_t i = 0; OB_SUCC(ret) && i < mock_fk_parent_table_schema.get_foreign_key_infos().count(); ++i) {
3225if (OB_FAIL(child_table_ids.push_back(mock_fk_parent_table_schema.get_foreign_key_infos().at(i).child_table_id_))) {
3226LOG_WARN("fail to push back child_table_id", K(ret), K(mock_fk_parent_table_schema.get_foreign_key_infos().at(i)));
3227}
3228}
3229if (FAILEDx(sync_version_for_cascade_table(mock_fk_parent_table_schema.get_tenant_id(), child_table_ids, trans))) {
3230LOG_WARN("fail to sync versin for children tables", K(ret), K(child_table_ids));
3231}
3232if (FAILEDx(schema_service_impl->get_table_sql_service().update_data_table_schema_version(trans, mock_fk_parent_table_schema.get_tenant_id(), new_parent_table_id, false))) {
3233LOG_WARN("failed to update parent table schema version", K(ret), K(mock_fk_parent_table_schema.get_foreign_key_infos().at(0)));
3234}
3235}
3236return ret;
3237}
3238
3239int ObDDLOperator::sync_version_for_cascade_mock_fk_parent_table(
3240const uint64_t tenant_id,
3241const common::ObIArray<uint64_t> &table_ids,
3242common::ObMySQLTransaction &trans)
3243{
3244int ret = OB_SUCCESS;
3245uint64_t id = OB_INVALID_ID;
3246const ObMockFKParentTableSchema *schema = NULL;
3247ObSchemaService *schema_service = schema_service_.get_schema_service();
3248if (OB_ISNULL(schema_service)) {
3249ret = OB_ERR_SYS;
3250RS_LOG(ERROR, "schema_service must not null");
3251} else {
3252for (int64_t i = 0; OB_SUCC(ret) && i < table_ids.count() ; ++i) {
3253ObSchemaGetterGuard schema_guard;
3254id = table_ids.at(i);
3255ObMockFKParentTableSchema tmp_schema;
3256if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
3257RS_LOG(WARN, "get schema guard failed", K(ret), K(tenant_id), K(id));
3258} else if (OB_FAIL(schema_guard.get_mock_fk_parent_table_schema_with_id(tenant_id, id, schema))) {
3259LOG_WARN("fail to get table schema", K(ret), K(id));
3260} else if (!schema) {
3261ret = OB_ERR_UNEXPECTED;
3262LOG_WARN("schema is NULL", K(ret));
3263} else if (OB_FAIL(tmp_schema.assign(*schema))) {
3264LOG_WARN("fail to assign schema", K(ret), KPC(schema));
3265} else if (OB_FAIL(schema_service->get_table_sql_service().update_mock_fk_parent_table_schema_version(
3266&trans,
3267tmp_schema))) {
3268RS_LOG(WARN, "fail to sync schema version", K(ret), K(tmp_schema));
3269}
3270}
3271}
3272
3273return ret;
3274}
3275
3276int ObDDLOperator::deal_with_mock_fk_parent_table(
3277ObMySQLTransaction &trans,
3278share::schema::ObSchemaGetterGuard &schema_guard,
3279ObMockFKParentTableSchema &mock_fk_parent_table_schema)
3280{
3281int ret = OB_SUCCESS;
3282int64_t new_schema_version = OB_INVALID_VERSION;
3283if (OB_FAIL(schema_service_.gen_new_schema_version(mock_fk_parent_table_schema.get_tenant_id(), new_schema_version))) {
3284LOG_WARN("fail to gen new schema version", K(ret), K(mock_fk_parent_table_schema.get_tenant_id()));
3285} else if (FALSE_IT(mock_fk_parent_table_schema.set_schema_version(new_schema_version))) {
3286} else if (MOCK_FK_PARENT_TABLE_OP_CREATE_TABLE_BY_DROP_PARENT_TABLE == mock_fk_parent_table_schema.get_operation_type()) {
3287// One scenes :
3288// 1. dropped real parent table
3289if (OB_FAIL(create_mock_fk_parent_table(trans, mock_fk_parent_table_schema, true))) {
3290LOG_WARN("create mock_fk_parent_table failed", K(ret), K(mock_fk_parent_table_schema));
3291}
3292} else if (MOCK_FK_PARENT_TABLE_OP_CREATE_TABLE_BY_ADD_FK_IN_CHILD_TBALE == mock_fk_parent_table_schema.get_operation_type()) {
3293// Two scenes :
3294// 1. create child table with a fk references a mock fk parent table
3295// 2. alter child table add fk references a mock fk parent table
3296if (OB_FAIL(create_mock_fk_parent_table(trans, mock_fk_parent_table_schema, false))) {
3297LOG_WARN("create mock_fk_parent_table failed", K(ret), K(mock_fk_parent_table_schema));
3298}
3299} else if (MOCK_FK_PARENT_TABLE_OP_DROP_TABLE == mock_fk_parent_table_schema.get_operation_type()) {
3300// Three scenes :
3301// 1. drop child table with a fk references a mock fk parent table existed
3302// 2. drop fk from a child table with a fk references a mock fk parent table existed
3303// 3. drop database
3304if (OB_FAIL(drop_mock_fk_parent_table(trans, mock_fk_parent_table_schema))) {
3305LOG_WARN("drop mock_fk_parent_table failed", K(ret), K(mock_fk_parent_table_schema.get_operation_type()), K(mock_fk_parent_table_schema));
3306}
3307} else if (MOCK_FK_PARENT_TABLE_OP_ADD_COLUMN == mock_fk_parent_table_schema.get_operation_type()
3308|| MOCK_FK_PARENT_TABLE_OP_DROP_COLUMN == mock_fk_parent_table_schema.get_operation_type()
3309|| MOCK_FK_PARENT_TABLE_OP_UPDATE_SCHEMA_VERSION == mock_fk_parent_table_schema.get_operation_type()) {
3310// Three scenes :
3311// 1. create child table with a fk references a mock fk parent table existed
3312// 2. alter child table add fk references a mock fk parent table existed
3313// 3. drop fk from a child table with a fk references a mock fk parent table existed
3314if (OB_FAIL(alter_mock_fk_parent_table(trans, mock_fk_parent_table_schema))) {
3315LOG_WARN("alter mock_fk_parent_table failed", K(ret), K(mock_fk_parent_table_schema.get_operation_type()), K(mock_fk_parent_table_schema));
3316}
3317} else if (MOCK_FK_PARENT_TABLE_OP_REPLACED_BY_REAL_PREANT_TABLE == mock_fk_parent_table_schema.get_operation_type()) {
3318// Five scenes :
3319// 1. create table (as select)
3320// 2. create table like
3321// 3. rename table
3322// 4. alter table rename to
3323// 5. flashback table to before drop
3324if (OB_FAIL(replace_mock_fk_parent_table(trans, schema_guard, mock_fk_parent_table_schema))) {
3325LOG_WARN("replace mock_fk_parent_table failed", K(ret), K(mock_fk_parent_table_schema.get_operation_type()), K(mock_fk_parent_table_schema));
3326}
3327} else {
3328ret = OB_ERR_UNEXPECTED;
3329LOG_WARN("operation_type is INVALID", K(ret), K(mock_fk_parent_table_schema.get_operation_type()), K(mock_fk_parent_table_schema), K(lbt()));
3330}
3331return ret;
3332}
3333
3334int ObDDLOperator::deal_with_mock_fk_parent_tables(
3335ObMySQLTransaction &trans,
3336share::schema::ObSchemaGetterGuard &schema_guard,
3337ObIArray<ObMockFKParentTableSchema> &mock_fk_parent_table_schema_array)
3338{
3339int ret = OB_SUCCESS;
3340for (int64_t i = 0; OB_SUCC(ret) && i < mock_fk_parent_table_schema_array.count(); ++i) {
3341if (OB_FAIL(deal_with_mock_fk_parent_table(trans, schema_guard, mock_fk_parent_table_schema_array.at(i)))) {
3342LOG_WARN("deal_with_mock_fk_parent_tables failed", K(ret), K(mock_fk_parent_table_schema_array.at(i)));
3343}
3344}
3345return ret;
3346}
3347
3348int ObDDLOperator::alter_index_drop_options(const ObTableSchema &index_table_schema,
3349const ObString &table_name,
3350ObTableSchema &new_index_table_schema,
3351ObMySQLTransaction &trans) {
3352int ret = OB_SUCCESS;
3353const int INVISIBLE = 1;
3354const uint64_t DROPINDEX = 1;
3355const uint64_t tenant_id = index_table_schema.get_tenant_id();
3356int64_t new_schema_version = OB_INVALID_VERSION;
3357ObArenaAllocator allocator(ObModIds::OB_SCHEMA);
3358ObSchemaService *schema_service = schema_service_.get_schema_service();
3359if (OB_ISNULL(schema_service)) {
3360ret = OB_ERR_UNEXPECTED;
3361LOG_WARN("schema_service is NULL", K(ret));
3362} else if (!index_table_schema.is_index_table()) {
3363ret = OB_ERR_UNEXPECTED;
3364LOG_WARN("index_table_schema is not index", K(ret));
3365} else if (OB_FAIL(new_index_table_schema.assign(index_table_schema))) {
3366LOG_WARN("fail to assign schema", K(ret));
3367} else {
3368uint64_t INVISIBLEBEFORE = 0;
3369if (!new_index_table_schema.is_index_visible()) {
3370INVISIBLEBEFORE = 1;
3371} else {
3372INVISIBLEBEFORE = 0;
3373new_index_table_schema.set_index_visibility(INVISIBLE);
3374}
3375new_index_table_schema.set_invisible_before(INVISIBLEBEFORE);
3376new_index_table_schema.set_drop_index(DROPINDEX);
3377
3378ObSqlString sql;
3379ObString index_name;
3380if (OB_FAIL(ObTableSchema::get_index_name(allocator,
3381index_table_schema.get_data_table_id(),
3382index_table_schema.get_table_name_str(),
3383index_name))) {
3384LOG_WARN("failed to build index table name", K(ret));
3385} else if (OB_FAIL(sql.append_fmt("DROP INDEX %.*s on %.*s",
3386index_name.length(),
3387index_name.ptr(),
3388table_name.length(),
3389table_name.ptr()))) {
3390LOG_WARN("failed to append sql", K(ret));
3391} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
3392LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
3393} else {
3394ObString ddl_stmt_str = sql.string();
3395new_index_table_schema.set_schema_version(new_schema_version);
3396if (OB_FAIL(schema_service->get_table_sql_service().update_table_options(
3397trans,
3398index_table_schema,
3399new_index_table_schema,
3400OB_DDL_DROP_INDEX_TO_RECYCLEBIN,
3401&ddl_stmt_str))) {
3402RS_LOG(WARN, "schema service update_table_optinos failed", K(index_table_schema), K(ret));
3403}
3404}
3405}
3406return ret;
3407}
3408
3409int ObDDLOperator::alter_table_rename_index(
3410const uint64_t tenant_id,
3411const uint64_t data_table_id,
3412const uint64_t database_id,
3413const obrpc::ObRenameIndexArg &rename_index_arg,
3414const ObIndexStatus *new_index_status,
3415common::ObMySQLTransaction &trans,
3416schema::ObTableSchema &new_index_table_schema)
3417{
3418int ret = OB_SUCCESS;
3419ObSchemaService *schema_service = schema_service_.get_schema_service();
3420ObSchemaGetterGuard schema_guard;
3421if (OB_ISNULL(schema_service)) {
3422ret = OB_ERR_UNEXPECTED;
3423LOG_WARN("schema_service is NULL", K(ret));
3424} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
3425LOG_WARN("failed to get schema guard", K(ret));
3426} else {
3427int64_t new_schema_version = OB_INVALID_VERSION;
3428RS_LOG(INFO, "start alter table rename index", K(rename_index_arg));
3429const ObTableSchema *index_table_schema = NULL;
3430ObString index_table_name;
3431ObString new_index_table_name;
3432ObArenaAllocator allocator(ObModIds::OB_SCHEMA);
3433const ObString &index_name = rename_index_arg.origin_index_name_;
3434const ObString &new_index_name = rename_index_arg.new_index_name_;
3435
3436if (OB_FAIL(ObTableSchema::build_index_table_name(allocator,
3437data_table_id,
3438index_name,
3439index_table_name))) {
3440RS_LOG(WARN, "build_index_table_name failed", K(data_table_id), K(index_name), K(ret));
3441} else if (OB_FAIL(ObTableSchema::build_index_table_name(allocator,
3442data_table_id,
3443new_index_name,
3444new_index_table_name))) {
3445RS_LOG(WARN, "build_index_table_name failed", K(data_table_id), K(index_name), K(ret));
3446} else {
3447const bool is_index = true;
3448if (OB_FAIL(schema_guard.get_table_schema(tenant_id,
3449database_id,
3450index_table_name,
3451is_index,
3452index_table_schema))) {
3453LOG_WARN("fail to get table schema", K(ret), K(tenant_id), K(database_id), K(index_table_schema));
3454} else if (OB_UNLIKELY(NULL == index_table_schema)) {
3455ret = OB_ERR_UNEXPECTED;
3456RS_LOG(WARN, "get index table schema failed", K(tenant_id), K(database_id), K(index_table_name), K(ret));
3457} else if (index_table_schema->is_in_recyclebin()) {
3458ret = OB_ERR_OPERATION_ON_RECYCLE_OBJECT;
3459LOG_WARN("index table is in recyclebin", K(ret));
3460} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
3461LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
3462} else {
3463if (OB_FAIL(new_index_table_schema.assign(*index_table_schema))) {
3464LOG_WARN("fail to assign schema", K(ret));
3465} else {
3466new_index_table_schema.set_schema_version(new_schema_version);
3467if (nullptr != new_index_status) {
3468new_index_table_schema.set_index_status(*new_index_status);
3469}
3470new_index_table_schema.set_name_generated_type(GENERATED_TYPE_USER);
3471}
3472if (OB_FAIL(ret)) {
3473} else if (OB_FAIL(new_index_table_schema.set_table_name(new_index_table_name))) {
3474RS_LOG(WARN, "failed to set new table name!", K(new_index_table_schema), K(ret));
3475} else if (OB_FAIL(schema_service->get_table_sql_service().update_table_options(
3476trans,
3477*index_table_schema,
3478new_index_table_schema,
3479index_table_schema->is_global_index_table() ? OB_DDL_RENAME_GLOBAL_INDEX: OB_DDL_RENAME_INDEX))) {
3480RS_LOG(WARN, "schema service update_table_options failed", K(*index_table_schema), K(ret));
3481}
3482}
3483}
3484}
3485return ret;
3486}
3487
3488int ObDDLOperator::alter_index_table_parallel(
3489const uint64_t tenant_id,
3490const uint64_t data_table_id,
3491const uint64_t database_id,
3492const obrpc::ObAlterIndexParallelArg &alter_parallel_arg,
3493common::ObMySQLTransaction &trans)
3494{
3495int ret = OB_SUCCESS;
3496ObSchemaService *schema_service = schema_service_.get_schema_service();
3497ObSchemaGetterGuard schema_guard;
3498if (OB_ISNULL(schema_service)) {
3499ret = OB_ERR_UNEXPECTED;
3500LOG_WARN("schema_service is NULL", K(ret));
3501} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
3502LOG_WARN("failed to get schema guard", K(ret));
3503} else {
3504int64_t new_schema_version = OB_INVALID_VERSION;
3505RS_LOG(INFO, "start alter table alter index parallel", K(alter_parallel_arg));
3506const ObTableSchema *index_table_schema = NULL;
3507ObString index_table_name;
3508ObArenaAllocator allocator(ObModIds::OB_SCHEMA);
3509const ObString &index_name = alter_parallel_arg.index_name_;
3510
3511if (OB_FAIL(ObTableSchema::build_index_table_name(allocator,
3512data_table_id,
3513index_name,
3514index_table_name))) {
3515RS_LOG(WARN, "build_index_table_name failed", K(data_table_id), K(index_name), K(ret));
3516} else {
3517const bool is_index = true;
3518if (OB_FAIL(schema_guard.get_table_schema(tenant_id,
3519database_id,
3520index_table_name,
3521is_index,
3522index_table_schema))) {
3523LOG_WARN("fail to get table schema",
3524K(ret), K(tenant_id), K(database_id), K(index_table_schema));
3525} else if (OB_UNLIKELY(NULL == index_table_schema)) {
3526ret = OB_ERR_UNEXPECTED;
3527RS_LOG(WARN, "get index table schema failed",
3528K(tenant_id), K(database_id), K(index_table_name), K(ret));
3529} else if (index_table_schema->is_in_recyclebin()) {
3530ret = OB_ERR_OPERATION_ON_RECYCLE_OBJECT;
3531LOG_WARN("index table is in recyclebin", K(ret));
3532} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
3533LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
3534} else {
3535ObTableSchema new_index_table_schema;
3536if (OB_FAIL(new_index_table_schema.assign(*index_table_schema))) {
3537LOG_WARN("fail to assign schema", K(ret));
3538} else {
3539new_index_table_schema.set_schema_version(new_schema_version);
3540}
3541if (OB_SUCC(ret)) {
3542new_index_table_schema.set_dop(alter_parallel_arg.new_parallel_);
3543if (OB_FAIL(schema_service->get_table_sql_service().update_table_options(
3544trans,
3545*index_table_schema,
3546new_index_table_schema,
3547OB_DDL_ALTER_INDEX_PARALLEL))) {
3548RS_LOG(WARN, "schema service update_table_options failed",
3549K(*index_table_schema), K(ret));
3550}
3551}
3552}
3553}
3554}
3555return ret;
3556}
3557
3558int ObDDLOperator::alter_index_table_tablespace(const uint64_t data_table_id,
3559const uint64_t database_id,
3560const obrpc::ObAlterIndexTablespaceArg &alter_tablespace_arg,
3561ObSchemaGetterGuard &schema_guard,
3562common::ObMySQLTransaction &trans)
3563{
3564int ret = OB_SUCCESS;
3565ObSchemaService *schema_service = schema_service_.get_schema_service();
3566if (OB_ISNULL(schema_service)) {
3567ret = OB_ERR_UNEXPECTED;
3568LOG_WARN("schema_service is NULL", K(ret));
3569} else {
3570const uint64_t tenant_id = alter_tablespace_arg.tenant_id_;
3571int64_t new_schema_version = OB_INVALID_VERSION;
3572const ObTableSchema *index_table_schema = NULL;
3573ObString index_table_name;
3574ObArenaAllocator allocator(ObModIds::OB_SCHEMA);
3575const ObString &index_name = alter_tablespace_arg.index_name_;
3576
3577if (OB_FAIL(ObTableSchema::build_index_table_name(allocator,
3578data_table_id,
3579index_name,
3580index_table_name))) {
3581RS_LOG(WARN, "build_index_table_name failed", K(data_table_id), K(index_name), K(ret));
3582} else if (OB_FAIL(schema_guard.get_table_schema(tenant_id,
3583database_id,
3584index_table_name,
3585true,
3586index_table_schema))) {
3587LOG_WARN("fail to get table schema",
3588K(ret), K(tenant_id), K(database_id), K(index_table_schema));
3589} else if (OB_ISNULL(index_table_schema)) {
3590ret = OB_ERR_UNEXPECTED;
3591RS_LOG(WARN, "get index table schema failed",
3592K(tenant_id), K(database_id), K(index_table_name), K(ret));
3593} else if (index_table_schema->is_in_recyclebin()) {
3594ret = OB_ERR_OPERATION_ON_RECYCLE_OBJECT;
3595LOG_WARN("index table is in recyclebin", K(ret));
3596} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
3597LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
3598} else {
3599ObTableSchema new_index_table_schema;
3600if (OB_FAIL(new_index_table_schema.assign(*index_table_schema))) {
3601LOG_WARN("fail to assign schema", K(ret));
3602} else {
3603new_index_table_schema.set_schema_version(new_schema_version);
3604new_index_table_schema.set_tablespace_id(alter_tablespace_arg.tablespace_id_);
3605new_index_table_schema.set_encryption_str(alter_tablespace_arg.encryption_);
3606if (OB_FAIL(schema_service->get_table_sql_service().update_table_options(
3607trans,
3608*index_table_schema,
3609new_index_table_schema,
3610OB_DDL_ALTER_TABLE))) {
3611RS_LOG(WARN, "schema service update_table_options failed",
3612K(*index_table_schema), K(ret));
3613}
3614}
3615}
3616}
3617return ret;
3618}
3619
3620//hualong delete later
3621//int ObDDLOperator::log_ddl_operation(ObSchemaOperation &ddl_operation,
3622// ObMySQLTransaction &trans)
3623//{
3624// int ret = OB_SUCCESS;
3625// ObSchemaService *schema_service = schema_service_.get_schema_service();
3626// if (OB_ISNULL(schema_service)) {
3627// ret = OB_ERR_UNEXPECTED;
3628// LOG_WARN("schema_service is NULL", K(ret));
3629// } else if (OB_FAIL(schema_service->log_operation(ddl_operation, &trans))) {
3630// RS_LOG(WARN, "failed to log ddl operation!", K(ret));
3631// } else {
3632// // do-nothing
3633// }
3634// return ret;
3635//}
3636
3637int ObDDLOperator::alter_table_options(
3638ObSchemaGetterGuard &schema_guard,
3639ObTableSchema &new_table_schema,
3640const ObTableSchema &table_schema,
3641const bool update_index_table,
3642ObMySQLTransaction &trans,
3643const ObIArray<ObTableSchema> *global_idx_schema_array/*=NULL*/)
3644{
3645int ret = OB_SUCCESS;
3646const uint64_t tenant_id = table_schema.get_tenant_id();
3647int64_t new_schema_version = OB_INVALID_VERSION;
3648ObSchemaService *schema_service = schema_service_.get_schema_service();
3649if (OB_ISNULL(schema_service)) {
3650ret = OB_ERR_SYS;
3651RS_LOG(WARN, "schema sql service must not be null",
3652K(schema_service), K(ret));
3653} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
3654LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
3655} else {
3656new_table_schema.set_schema_version(new_schema_version);
3657if (OB_FAIL(schema_service->get_table_sql_service().update_table_options(
3658trans,
3659table_schema,
3660new_table_schema,
3661OB_DDL_ALTER_TABLE))) {
3662RS_LOG(WARN, "failed to alter table option!", K(ret));
3663} else if (update_index_table) {
3664if (OB_FAIL(update_aux_table(table_schema,
3665new_table_schema,
3666schema_guard,
3667trans,
3668USER_INDEX,
3669global_idx_schema_array))) {
3670RS_LOG(WARN, "failed to update_index_table!", K(ret), K(table_schema), K(new_table_schema));
3671} else if (OB_FAIL(update_aux_table(table_schema,
3672new_table_schema,
3673schema_guard,
3674trans,
3675AUX_VERTIAL_PARTITION_TABLE))) {
3676RS_LOG(WARN, "failed to update_aux_vp_table!", K(ret), K(table_schema), K(new_table_schema));
3677} else if (OB_FAIL(update_aux_table(table_schema,
3678new_table_schema,
3679schema_guard,
3680trans,
3681AUX_LOB_META))) {
3682RS_LOG(WARN, "failed to update_aux_vp_table!", K(ret), K(table_schema), K(new_table_schema));
3683} else if (OB_FAIL(update_aux_table(table_schema,
3684new_table_schema,
3685schema_guard,
3686trans,
3687AUX_LOB_PIECE))) {
3688RS_LOG(WARN, "failed to update_aux_vp_table!", K(ret), K(table_schema), K(new_table_schema));
3689}
3690}
3691}
3692return ret;
3693}
3694
3695int ObDDLOperator::update_aux_table(
3696const ObTableSchema &table_schema,
3697const ObTableSchema &new_table_schema,
3698ObSchemaGetterGuard &schema_guard,
3699ObMySQLTransaction &trans,
3700const ObTableType table_type,
3701const ObIArray<ObTableSchema> *global_idx_schema_array/*=NULL*/)
3702{
3703int ret = OB_SUCCESS;
3704const uint64_t tenant_id = table_schema.get_tenant_id();
3705const bool is_index = USER_INDEX == table_type;
3706ObSEArray<uint64_t, 16> aux_tid_array;
3707ObSEArray<ObAuxTableMetaInfo, 16> simple_index_infos;
3708uint64_t lob_meta_table_id = OB_INVALID_ID;
3709uint64_t lob_piece_table_id = OB_INVALID_ID;
3710int64_t N = 0;
3711ObSchemaService *schema_service = schema_service_.get_schema_service();
3712if (OB_ISNULL(schema_service)) {
3713ret = OB_ERR_SYS;
3714RS_LOG(WARN, "schema sql service must not be null",
3715K(schema_service), K(ret));
3716} else {
3717if (table_type == USER_INDEX) {
3718if (OB_FAIL(new_table_schema.get_simple_index_infos(simple_index_infos))) {
3719LOG_WARN("get_aux_tid_array failed", K(ret), K(table_type));
3720} else {
3721N = simple_index_infos.count();
3722}
3723} else if (table_type == AUX_VERTIAL_PARTITION_TABLE) {
3724if (OB_FAIL(new_table_schema.get_aux_vp_tid_array(aux_tid_array))) {
3725LOG_WARN("get_aux_tid_array failed", K(ret), K(table_type));
3726} else {
3727N = aux_tid_array.count();
3728}
3729} else if (table_type == AUX_LOB_META) {
3730lob_meta_table_id = new_table_schema.get_aux_lob_meta_tid();
3731N = (table_schema.has_lob_aux_table() && new_table_schema.has_lob_aux_table()) ? 1 : 0;
3732} else if (table_type == AUX_LOB_PIECE) {
3733lob_piece_table_id = new_table_schema.get_aux_lob_piece_tid();
3734N = (table_schema.has_lob_aux_table() && new_table_schema.has_lob_aux_table()) ? 1 : 0;
3735} else {
3736ret = OB_ERR_UNEXPECTED;
3737LOG_WARN("invalid table type", K(ret), K(table_type));
3738}
3739}
3740if (OB_SUCC(ret)) {
3741ObTableSchema new_aux_table_schema;
3742for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) {
3743const ObTableSchema *aux_table_schema = NULL;
3744if (is_index && OB_NOT_NULL(global_idx_schema_array) && !global_idx_schema_array->empty()) {
3745for (int64_t j = 0; OB_SUCC(ret) && j < global_idx_schema_array->count(); ++j) {
3746if (simple_index_infos.at(i).table_id_ == global_idx_schema_array->at(j).get_table_id()) {
3747aux_table_schema = &(global_idx_schema_array->at(j));
3748break;
3749}
3750}
3751}
3752uint64_t tid = 0;
3753if (table_type == USER_INDEX) {
3754tid = simple_index_infos.at(i).table_id_;
3755} else if (table_type == AUX_VERTIAL_PARTITION_TABLE) {
3756tid = aux_tid_array.at(i);
3757} else if (table_type == AUX_LOB_META) {
3758tid = lob_meta_table_id;
3759} else if (table_type == AUX_LOB_PIECE) {
3760tid = lob_piece_table_id;
3761}
3762if (OB_FAIL(ret)) {
3763} else if (OB_ISNULL(aux_table_schema)
3764&& OB_FAIL(schema_guard.get_table_schema(tenant_id, tid, aux_table_schema))) {
3765RS_LOG(WARN, "get_table_schema failed", K(tenant_id), "table id", tid, K(ret));
3766} else if (OB_ISNULL(aux_table_schema)) {
3767ret = OB_ERR_UNEXPECTED;
3768RS_LOG(WARN, "table schema should not be null", K(ret));
3769} else {
3770new_aux_table_schema.reset();
3771if (OB_FAIL(new_aux_table_schema.assign(*aux_table_schema))) {
3772LOG_WARN("fail to assign schema", K(ret));
3773} else {
3774if (!aux_table_schema->is_global_index_table()) {
3775// tablegroup of global index should not inherit the tablegroup of the data table.
3776// the partitions numbers of all table of tablegroup are equal.
3777new_aux_table_schema.set_tablegroup_id(new_table_schema.get_tablegroup_id());
3778}
3779new_aux_table_schema.set_database_id(new_table_schema.get_database_id());
3780new_aux_table_schema.set_read_only(new_table_schema.is_read_only());
3781new_aux_table_schema.set_progressive_merge_num(new_table_schema.get_progressive_merge_num());
3782new_aux_table_schema.set_tablet_size(new_table_schema.get_tablet_size());
3783new_aux_table_schema.set_pctfree(new_table_schema.get_pctfree());
3784new_aux_table_schema.set_block_size(new_table_schema.get_block_size());
3785new_aux_table_schema.set_row_store_type(new_table_schema.get_row_store_type());
3786new_aux_table_schema.set_store_format(new_table_schema.get_store_format());
3787new_aux_table_schema.set_progressive_merge_round(new_table_schema.get_progressive_merge_round());
3788new_aux_table_schema.set_storage_format_version(new_table_schema.get_storage_format_version());
3789// index table should only inherit table mode and table state flag from data table
3790new_aux_table_schema.set_table_mode(new_table_schema.get_table_mode_flag());
3791new_aux_table_schema.set_table_state_flag(new_table_schema.get_table_state_flag());
3792}
3793if (OB_FAIL(ret)) {
3794} else if (OB_FAIL(new_aux_table_schema.set_compress_func_name(new_table_schema.get_compress_func_name()))) {
3795LOG_WARN("set_compress_func_name failed", K(new_table_schema));
3796} else if (aux_table_schema->is_in_recyclebin()) {
3797const uint64_t tenant_id = aux_table_schema->get_tenant_id();
3798ObArray<ObRecycleObject> recycle_objs;
3799ObRecycleObject::RecycleObjType recycle_type = ObRecycleObject::get_type_by_table_schema(*aux_table_schema);
3800new_aux_table_schema.set_database_id(aux_table_schema->get_database_id());
3801if (OB_INVALID_ID == tenant_id) {
3802ret = OB_INVALID_ARGUMENT;
3803LOG_WARN("tenant_id is invalid", K(ret));
3804} else if (OB_FAIL(schema_service->fetch_recycle_object(
3805tenant_id,
3806aux_table_schema->get_table_name_str(),
3807recycle_type,
3808trans,
3809recycle_objs))) {
3810LOG_WARN("get recycle object failed", K(tenant_id), K(ret));
3811} else if (recycle_objs.size() != 1) {
3812ret = OB_ERR_UNEXPECTED;
3813LOG_WARN("unexpected recycle object num", K(ret), K(*aux_table_schema), "size", recycle_objs.size());
3814} else if (OB_FAIL(schema_service->delete_recycle_object(
3815tenant_id,
3816recycle_objs.at(0),
3817trans))) {
3818LOG_WARN("delete recycle object failed", K(ret));
3819} else {
3820ObRecycleObject &recycle_obj = recycle_objs.at(0);
3821recycle_obj.set_database_id(new_table_schema.get_database_id());
3822recycle_obj.set_tablegroup_id(new_table_schema.get_tablegroup_id());
3823if (OB_FAIL(schema_service->insert_recyclebin_object(recycle_obj, trans))) {
3824LOG_WARN("insert recyclebin object failed", K(ret));
3825}
3826}
3827}
3828int64_t new_schema_version = OB_INVALID_VERSION;
3829if (OB_FAIL(ret)) {
3830} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
3831LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
3832} else {
3833new_aux_table_schema.set_schema_version(new_schema_version);
3834if (OB_FAIL(schema_service->get_table_sql_service().update_table_options(
3835trans,
3836*aux_table_schema,
3837new_aux_table_schema,
3838OB_DDL_ALTER_TABLE))) {
3839RS_LOG(WARN, "schema service update_table_options failed",
3840K(*aux_table_schema), K(ret));
3841}
3842}
3843}
3844}
3845}
3846return ret;
3847}
3848
3849int ObDDLOperator::rename_table(const ObTableSchema &table_schema,
3850const ObString &new_table_name,
3851const uint64_t new_db_id,
3852const bool need_reset_object_status,
3853ObMySQLTransaction &trans,
3854const ObString *ddl_stmt_str)
3855{
3856int ret = OB_SUCCESS;
3857const uint64_t tenant_id = table_schema.get_tenant_id();
3858int64_t new_schema_version = OB_INVALID_VERSION;
3859ObSchemaGetterGuard schema_guard;
3860ObSchemaService *schema_service = schema_service_.get_schema_service();
3861if (OB_ISNULL(schema_service)) {
3862ret = OB_ERR_SYS;
3863RS_LOG(WARN, "schema sql service must not be null",
3864K(schema_service), K(ret));
3865} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
3866RS_LOG(WARN, "get schema guard failed", K(ret));
3867} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
3868LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
3869} else {
3870bool update_aux_table = false;
3871ObTableSchema new_table_schema;
3872if (OB_FAIL(new_table_schema.assign(table_schema))) {
3873LOG_WARN("fail to assign schema", K(ret));
3874} else {
3875new_table_schema.set_schema_version(new_schema_version);
3876}
3877if (need_reset_object_status) {
3878new_table_schema.set_object_status(ObObjectStatus::INVALID);
3879}
3880if (OB_FAIL(ret)) {
3881} else if (OB_FAIL(new_table_schema.set_table_name(new_table_name))) {
3882RS_LOG(WARN, "failed to set new table name!", K(new_table_name), K(table_schema), K(ret));
3883} else if (new_db_id != table_schema.get_database_id()) {
3884update_aux_table = true;
3885new_table_schema.set_database_id(new_db_id);
3886}
3887
3888if (OB_SUCC(ret)) {
3889if (OB_FAIL(schema_service->get_table_sql_service().update_table_options(
3890trans,
3891table_schema,
3892new_table_schema,
3893OB_DDL_TABLE_RENAME,
3894ddl_stmt_str))) {
3895RS_LOG(WARN, "failed to alter table option!", K(ret));
3896} else if (update_aux_table) {
3897HEAP_VAR(ObTableSchema, new_aux_table_schema) {
3898{ // update index table
3899ObSEArray<ObAuxTableMetaInfo, 16> simple_index_infos;
3900if (OB_FAIL(table_schema.get_simple_index_infos(simple_index_infos))) {
3901RS_LOG(WARN, "get_index_tid_array failed", K(ret));
3902} else {
3903for (int64_t i = 0; OB_SUCC(ret) && i < simple_index_infos.count(); ++i) {
3904if (OB_FAIL(rename_aux_table(new_table_schema,
3905simple_index_infos.at(i).table_id_,
3906schema_guard,
3907trans,
3908new_aux_table_schema))) {
3909RS_LOG(WARN, "fail to rename update index table", K(ret));
3910}
3911}
3912}
3913}
3914if (OB_SUCC(ret) && table_schema.has_lob_aux_table()) {
3915uint64_t mtid = table_schema.get_aux_lob_meta_tid();
3916uint64_t ptid = table_schema.get_aux_lob_piece_tid();
3917if (OB_INVALID_ID == mtid || OB_INVALID_ID == ptid) {
3918ret = OB_ERR_UNEXPECTED;
3919RS_LOG(WARN, "Expect meta tid and piece tid valid", KR(ret), K(mtid), K(ptid));
3920} else if (OB_FAIL(rename_aux_table(new_table_schema,
3921mtid,
3922schema_guard,
3923trans,
3924new_aux_table_schema))) {
3925RS_LOG(WARN, "fail to rename update lob meta table", KR(ret), K(mtid));
3926} else if (OB_FAIL(rename_aux_table(new_table_schema,
3927ptid,
3928schema_guard,
3929trans,
3930new_aux_table_schema))) {
3931RS_LOG(WARN, "fail to rename update lob piece table", KR(ret), K(ptid));
3932}
3933}
3934}
3935}
3936}
3937}
3938return ret;
3939}
3940
3941int ObDDLOperator::rename_aux_table(
3942const ObTableSchema &new_table_schema,
3943const uint64_t table_id,
3944ObSchemaGetterGuard &schema_guard,
3945ObMySQLTransaction &trans,
3946ObTableSchema &new_aux_table_schema)
3947{
3948int ret = OB_SUCCESS;
3949const uint64_t tenant_id = new_table_schema.get_tenant_id();
3950ObSchemaService *schema_service = schema_service_.get_schema_service();
3951const ObTableSchema *aux_table_schema = NULL;
3952int64_t new_schema_version = OB_INVALID_VERSION;
3953if (OB_FAIL(schema_guard.get_table_schema(
3954tenant_id, table_id, aux_table_schema))) {
3955RS_LOG(WARN, "get_table_schema failed", K(tenant_id),
3956"table id", table_id, K(ret));
3957} else if (OB_ISNULL(aux_table_schema)) {
3958ret = OB_ERR_UNEXPECTED;
3959RS_LOG(WARN, "table schema should not be null", K(ret));
3960} else {
3961new_aux_table_schema.reset();
3962if (OB_FAIL(new_aux_table_schema.assign(*aux_table_schema))) {
3963LOG_WARN("fail to assign schema", K(ret));
3964} else {
3965new_aux_table_schema.set_database_id(new_table_schema.get_database_id());
3966}
3967if (OB_FAIL(ret)) {
3968} else if (aux_table_schema->is_in_recyclebin()) {
3969ret = OB_ERR_UNEXPECTED;
3970LOG_WARN("aux table is in recycle bin while main table not in", K(ret), KPC(aux_table_schema));
3971} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
3972LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
3973} else {
3974new_aux_table_schema.set_schema_version(new_schema_version);
3975if (OB_FAIL(schema_service->get_table_sql_service().update_table_options(
3976trans,
3977*aux_table_schema,
3978new_aux_table_schema,
3979OB_DDL_TABLE_RENAME))) {
3980RS_LOG(WARN, "schema service update_table_options failed",
3981K(*aux_table_schema), K(ret));
3982}
3983}
3984}
3985return ret;
3986}
3987
3988int ObDDLOperator::update_index_status(
3989const uint64_t tenant_id,
3990const uint64_t data_table_id,
3991const uint64_t index_table_id,
3992const share::schema::ObIndexStatus status,
3993const bool in_offline_ddl_white_list,
3994common::ObMySQLTransaction &trans,
3995const common::ObString *ddl_stmt_str)
3996{
3997int ret = OB_SUCCESS;
3998int64_t new_schema_version = OB_INVALID_VERSION;
3999ObSchemaService *schema_service = schema_service_.get_schema_service();
4000ObSchemaGetterGuard schema_guard;
4001const ObTableSchema *data_table_schema = nullptr;
4002ObTableSchema copy_data_table_schema;
4003
4004if (OB_INVALID_ID == data_table_id || OB_INVALID_ID == index_table_id
4005|| status <= INDEX_STATUS_NOT_FOUND || status >= INDEX_STATUS_MAX) {
4006ret = OB_INVALID_ARGUMENT;
4007LOG_WARN("invalid argument", K(data_table_id), K(index_table_id), K(status));
4008} else if (OB_ISNULL(schema_service)) {
4009ret = OB_ERR_SYS;
4010LOG_WARN("schema service should not be NULL");
4011} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
4012LOG_WARN("get tenant schema guard failed", K(ret), K(tenant_id));
4013} else if (OB_FAIL(schema_guard.get_table_schema(tenant_id, data_table_id, data_table_schema))) {
4014LOG_WARN("get table schema failed", K(ret));
4015} else if (nullptr == data_table_schema) {
4016ret = OB_ERR_UNEXPECTED;
4017LOG_WARN("error unexpected, table schema must not be nullptr", K(ret));
4018} else if (OB_FAIL(copy_data_table_schema.assign(*data_table_schema))) {
4019LOG_WARN("assign data table schema failed", K(ret));
4020} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
4021LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
4022} else if (FALSE_IT(copy_data_table_schema.set_in_offline_ddl_white_list(in_offline_ddl_white_list))) {
4023} else if (OB_FAIL(schema_service->get_table_sql_service().update_index_status(
4024copy_data_table_schema, index_table_id, status, new_schema_version, trans, ddl_stmt_str))) {
4025LOG_WARN("update index status failed",
4026K(ret), K(data_table_id), K(index_table_id), K(status));
4027}
4028return ret;
4029}
4030
4031int ObDDLOperator::update_table_attribute(ObTableSchema &new_table_schema,
4032common::ObMySQLTransaction &trans,
4033const ObSchemaOperationType operation_type,
4034const ObString *ddl_stmt_str/*=NULL*/)
4035{
4036int ret = OB_SUCCESS;
4037const uint64_t tenant_id = new_table_schema.get_tenant_id();
4038int64_t new_schema_version = OB_INVALID_VERSION;
4039ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
4040const bool update_object_status_ignore_version = false;
4041if (OB_ISNULL(schema_service_impl)) {
4042ret = OB_ERR_SYS;
4043RS_LOG(WARN, "schema_service_impl must not null", K(ret));
4044} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
4045LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
4046} else {
4047new_table_schema.set_schema_version(new_schema_version);
4048if (OB_FAIL(schema_service_impl->get_table_sql_service().update_table_attribute(
4049trans,
4050new_table_schema,
4051operation_type,
4052update_object_status_ignore_version,
4053ddl_stmt_str))) {
4054RS_LOG(WARN, "failed to update table attribute!" ,K(ret));
4055}
4056}
4057return ret;
4058}
4059
4060int ObDDLOperator::update_single_column(common::ObMySQLTransaction &trans,
4061const ObTableSchema &origin_table_schema,
4062const ObTableSchema &new_table_schema,
4063ObColumnSchemaV2 &column_schema)
4064{
4065int ret = OB_SUCCESS;
4066const uint64_t tenant_id = origin_table_schema.get_tenant_id();
4067int64_t new_schema_version = OB_INVALID_VERSION;
4068ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
4069if (OB_ISNULL(schema_service_impl)) {
4070ret = OB_ERR_SYS;
4071RS_LOG(WARN, "schema_service_impl must not null", K(ret));
4072} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
4073LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
4074} else {
4075column_schema.set_schema_version(new_schema_version);
4076const ObColumnSchemaV2 *orig_column_schema = origin_table_schema.get_column_schema(column_schema.get_column_id());
4077if (OB_FAIL(schema_service_impl->get_table_sql_service().update_single_column(
4078trans, origin_table_schema, new_table_schema, column_schema,
4079true /* record_ddl_operation */))) {
4080RS_LOG(WARN, "failed to update single column", K(ret));
4081}
4082}
4083return ret;
4084}
4085
4086int ObDDLOperator::update_single_column_group(common::ObMySQLTransaction &trans,
4087const ObTableSchema &origin_table_schema,
4088const ObColumnSchemaV2 &column_schema)
4089{
4090int ret = OB_SUCCESS;
4091bool is_each_cg_exist = false;
4092const ObColumnSchemaV2 *orig_column_schema = nullptr;
4093char cg_name[OB_MAX_COLUMN_GROUP_NAME_LENGTH] = {'\0'};
4094ObString cg_name_str(OB_MAX_COLUMN_GROUP_NAME_LENGTH, 0, cg_name);
4095const uint64_t tenant_id = origin_table_schema.get_tenant_id();
4096ObColumnGroupSchema *ori_cg = nullptr;
4097ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
4098orig_column_schema = origin_table_schema.get_column_schema(column_schema.get_column_id());
4099if (!origin_table_schema.is_valid() || !column_schema.is_valid()) {
4100ret = OB_INVALID_ARGUMENT;
4101RS_LOG(WARN, "Invalid arguemnt", K(ret), K(origin_table_schema), K(column_schema));
4102} else if (OB_ISNULL(orig_column_schema)) {
4103ret = OB_ERR_UNEXPECTED;
4104RS_LOG(WARN, "column should not be null", K(ret), K(column_schema), K(origin_table_schema));
4105} else if (orig_column_schema->get_column_name_str() == column_schema.get_column_name_str()) {
4106/* now only rename column will use this func, other skip*/
4107} else if (!origin_table_schema.is_column_store_supported()) {
4108/* only support table need column group*/
4109} else if (OB_FAIL(origin_table_schema.is_column_group_exist(OB_EACH_COLUMN_GROUP_NAME, is_each_cg_exist))) {
4110RS_LOG(WARN, "fail check whether each cg exist", K(ret));
4111} else if (!is_each_cg_exist) {
4112/* if each cg not exist skip*/
4113} else if (column_schema.is_virtual_generated_column()) {
4114/* skip virtual generated_column*/
4115} else if (OB_FAIL(orig_column_schema->get_each_column_group_name(cg_name_str))) {
4116RS_LOG(WARN, "fail to get each column group name", K(ret));
4117} else if (OB_FAIL(origin_table_schema.get_column_group_by_name(cg_name_str, ori_cg))) {
4118RS_LOG(WARN, "column group cannot get", K(cg_name_str), K(origin_table_schema));
4119} else if (OB_ISNULL(ori_cg)) {
4120ret = OB_ERR_UNEXPECTED;
4121RS_LOG(WARN, "column group should not be null", K(ret), K(cg_name_str),
4122KPC(orig_column_schema), K(origin_table_schema));
4123} else {
4124ObColumnGroupSchema new_cg;
4125if (OB_FAIL(new_cg.assign(*ori_cg))) {
4126RS_LOG(WARN, "fail to assign column group", K(ret), K(ori_cg));
4127} else {
4128new_cg.set_schema_version(column_schema.get_schema_version());
4129cg_name_str.set_length(0);
4130if (OB_FAIL(column_schema.get_each_column_group_name(cg_name_str))) {
4131RS_LOG(WARN, "fail to gen column group related column group name", K(ret), K(column_schema));
4132} else if (OB_FAIL(new_cg.set_column_group_name(cg_name_str))) {
4133RS_LOG(WARN, "fail to set column group name", K(ret), K(new_cg), K(cg_name_str));
4134} else if (OB_FAIL(schema_service_impl->get_table_sql_service().update_single_column_group(trans,
4135origin_table_schema,
4136*ori_cg,
4137new_cg))) {
4138RS_LOG(WARN,"fail to update single column_group", K(ret));
4139}
4140}
4141}
4142return ret;
4143}
4144
4145int ObDDLOperator::batch_update_system_table_columns(
4146common::ObMySQLTransaction &trans,
4147const share::schema::ObTableSchema &orig_table_schema,
4148share::schema::ObTableSchema &new_table_schema,
4149const common::ObIArray<uint64_t> &add_column_ids,
4150const common::ObIArray<uint64_t> &alter_column_ids,
4151const common::ObString *ddl_stmt_str/*=NULL*/)
4152{
4153int ret = OB_SUCCESS;
4154const uint64_t tenant_id = new_table_schema.get_tenant_id();
4155const uint64_t table_id = new_table_schema.get_table_id();
4156int64_t new_schema_version = OB_INVALID_VERSION;
4157ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
4158const bool update_object_status_ignore_version = false;
4159if (OB_ISNULL(schema_service_impl)) {
4160ret = OB_ERR_SYS;
4161LOG_WARN("schema_service_impl must not null", KR(ret));
4162} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
4163LOG_WARN("fail to gen new schema_version", KR(ret), K(tenant_id));
4164} else {
4165(void) new_table_schema.set_schema_version(new_schema_version);
4166ObColumnSchemaV2 *new_column = NULL;
4167for (int64_t i = 0; OB_SUCC(ret) && i < add_column_ids.count(); i++) {
4168const uint64_t column_id = add_column_ids.at(i);
4169if (OB_ISNULL(new_column = new_table_schema.get_column_schema(column_id))) {
4170ret = OB_ERR_UNEXPECTED;
4171LOG_WARN("fail to get column", KR(ret), K(tenant_id), K(table_id), K(column_id));
4172} else if (FALSE_IT(new_column->set_schema_version(new_schema_version))) {
4173} else if (OB_FAIL(schema_service_impl->get_table_sql_service().insert_single_column(
4174trans, new_table_schema, *new_column, false))) {
4175LOG_WARN("fail to insert column", KR(ret), K(tenant_id), K(table_id), K(column_id));
4176}
4177} // end for
4178
4179for (int64_t i = 0; OB_SUCC(ret) && i < alter_column_ids.count(); i++) {
4180const uint64_t column_id = alter_column_ids.at(i);
4181if (OB_ISNULL(new_column = new_table_schema.get_column_schema(column_id))) {
4182ret = OB_ERR_UNEXPECTED;
4183LOG_WARN("fail to get column", KR(ret), K(tenant_id), K(table_id), K(column_id));
4184} else if (FALSE_IT(new_column->set_schema_version(new_schema_version))) {
4185} else if (OB_FAIL(schema_service_impl->get_table_sql_service().update_single_column(
4186trans, orig_table_schema, new_table_schema, *new_column, false))) {
4187LOG_WARN("fail to insert column", KR(ret), K(tenant_id), K(table_id), K(column_id));
4188}
4189} // end for
4190
4191if (FAILEDx(schema_service_impl->get_table_sql_service().update_table_attribute(
4192trans, new_table_schema, OB_DDL_ALTER_TABLE, update_object_status_ignore_version, ddl_stmt_str))) {
4193LOG_WARN("failed to update table attribute", KR(ret), K(tenant_id), K(table_id));
4194}
4195}
4196return ret;
4197}
4198
4199int ObDDLOperator::update_partition_option(common::ObMySQLTransaction &trans,
4200ObTableSchema &table_schema)
4201{
4202int ret = OB_SUCCESS;
4203const uint64_t tenant_id = table_schema.get_tenant_id();
4204int64_t new_schema_version = OB_INVALID_VERSION;
4205ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
4206if (OB_ISNULL(schema_service_impl)) {
4207ret = OB_ERR_SYS;
4208RS_LOG(WARN, "schema_service_impl must not null", K(ret));
4209} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
4210LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
4211} else {
4212if (OB_FAIL(schema_service_impl->get_table_sql_service().update_partition_option(
4213trans, table_schema, new_schema_version))) {
4214RS_LOG(WARN, "failed to update partition option", K(table_schema), K(ret));
4215}
4216}
4217return ret;
4218}
4219
4220int ObDDLOperator::update_check_constraint_state(common::ObMySQLTransaction &trans,
4221const ObTableSchema &table_schema,
4222ObConstraint &cst)
4223{
4224int ret = OB_SUCCESS;
4225const uint64_t tenant_id = table_schema.get_tenant_id();
4226int64_t new_schema_version = OB_INVALID_VERSION;
4227ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
4228if (OB_ISNULL(schema_service_impl)) {
4229ret = OB_ERR_SYS;
4230RS_LOG(WARN, "schema_service_impl must not null", K(ret));
4231} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
4232LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
4233} else {
4234cst.set_schema_version(new_schema_version);
4235if (OB_FAIL(schema_service_impl->get_table_sql_service().update_check_constraint_state(trans,
4236table_schema, cst))) {
4237RS_LOG(WARN, "failed to update check constraint state", K(table_schema), K(ret));
4238}
4239}
4240return ret;
4241}
4242
4243int ObDDLOperator::sync_aux_schema_version_for_history(common::ObMySQLTransaction &trans,
4244const ObTableSchema &index_schema)
4245{
4246int ret = OB_SUCCESS;
4247const uint64_t tenant_id = index_schema.get_tenant_id();
4248int64_t new_schema_version = OB_INVALID_VERSION;
4249ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
4250if (OB_ISNULL(schema_service_impl)) {
4251ret = OB_ERR_SYS;
4252RS_LOG(WARN, "schema_service_impl must not null", K(ret));
4253} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
4254LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
4255} else {
4256if (OB_FAIL(schema_service_impl->get_table_sql_service().sync_aux_schema_version_for_history(
4257trans, index_schema, new_schema_version))) {
4258RS_LOG(WARN, "failed to update check constraint state", K(index_schema), K(ret));
4259}
4260}
4261return ret;
4262}
4263
4264int ObDDLOperator::drop_obj_privs(
4265const uint64_t tenant_id,
4266const uint64_t obj_id,
4267const uint64_t obj_type,
4268ObMySQLTransaction &trans,
4269ObMultiVersionSchemaService &schema_service,
4270ObSchemaGetterGuard &schema_guard)
4271{
4272int ret = OB_SUCCESS;
4273ObSchemaService *schema_sql_service = schema_service.get_schema_service();
4274ObArray<const ObObjPriv *> obj_privs;
4275
4276CK (OB_NOT_NULL(schema_sql_service));
4277OZ (schema_guard.get_obj_priv_with_obj_id(tenant_id, obj_id, obj_type, obj_privs, true));
4278for (int64_t i = 0; OB_SUCC(ret) && i < obj_privs.count(); ++i) {
4279const ObObjPriv *obj_priv = obj_privs.at(i);
4280int64_t new_schema_version = OB_INVALID_VERSION;
4281
4282if (OB_ISNULL(obj_priv)) {
4283LOG_WARN("obj_priv priv is NULL", K(ret), K(obj_priv));
4284} else {
4285OZ (schema_service.gen_new_schema_version(tenant_id, new_schema_version));
4286OZ (schema_sql_service->get_priv_sql_service().delete_obj_priv(
4287*obj_priv, new_schema_version, trans));
4288// In order to prevent being deleted, but there is no time to refresh the schema.
4289// for example, obj priv has deleted, but obj schema unrefresh
4290if (ret == OB_SEARCH_NOT_FOUND) {
4291ret = OB_SUCCESS;
4292}
4293}
4294}
4295return ret;
4296}
4297
4298
4299int ObDDLOperator::drop_obj_privs(
4300const uint64_t tenant_id,
4301const uint64_t obj_id,
4302const uint64_t obj_type,
4303ObMySQLTransaction &trans)
4304{
4305int ret = OB_SUCCESS;
4306ObSchemaGetterGuard schema_guard;
4307
4308OZ (schema_service_.get_tenant_schema_guard(tenant_id, schema_guard));
4309OZ (drop_obj_privs(tenant_id, obj_id, obj_type, trans, schema_service_, schema_guard));
4310
4311return ret;
4312}
4313
4314int ObDDLOperator::drop_tablet_of_table(
4315const ObTableSchema &table_schema,
4316ObMySQLTransaction &trans)
4317{
4318int ret = OB_SUCCESS;
4319if (OB_ISNULL(GCTX.srv_rpc_proxy_) || OB_ISNULL(GCTX.lst_operator_)) {
4320ret = OB_ERR_UNEXPECTED;
4321LOG_WARN("global variable is null", KR(ret), K(GCTX.srv_rpc_proxy_), K(GCTX.lst_operator_));
4322} else {
4323const uint64_t tenant_id = table_schema.get_tenant_id();
4324int64_t new_schema_version = OB_INVALID_VERSION;
4325ObSEArray<const ObTableSchema*, 1> schemas;
4326if (table_schema.is_vir_table()
4327|| table_schema.is_view_table()
4328|| is_inner_table(table_schema.get_table_id())) {
4329// skip
4330} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
4331LOG_WARN("fail to gen new schema_version", KR(ret), K(tenant_id));
4332} else {
4333ObTabletDrop tablet_drop(tenant_id, trans, new_schema_version);
4334if (OB_FAIL(schemas.push_back(&table_schema))) {
4335LOG_WARN("failed to push_back", KR(ret), K(table_schema));
4336} else if (OB_FAIL(tablet_drop.init())) {
4337LOG_WARN("fail to init tablet drop", KR(ret), K(table_schema));
4338} else if (OB_FAIL(tablet_drop.add_drop_tablets_of_table_arg(schemas))) {
4339LOG_WARN("failed to add drop tablets", KR(ret), K(tenant_id), K(table_schema));
4340} else if (OB_FAIL(tablet_drop.execute())) {
4341LOG_WARN("failed to execute", KR(ret), K(schemas), K(table_schema));
4342}
4343}
4344}
4345return ret;
4346}
4347
4348int ObDDLOperator::drop_table(
4349const ObTableSchema &table_schema,
4350ObMySQLTransaction &trans,
4351const ObString *ddl_stmt_str/*=NULL*/,
4352const bool is_truncate_table/*false*/,
4353DropTableIdHashSet *drop_table_set/*=NULL*/,
4354const bool is_drop_db/*false*/)
4355{
4356int ret = OB_SUCCESS;
4357bool tmp = false;
4358const uint64_t tenant_id = table_schema.get_tenant_id();
4359if (OB_FAIL(ObDependencyInfo::modify_dep_obj_status(trans, tenant_id, table_schema.get_table_id(),
4360*this, schema_service_))) {
4361LOG_WARN("failed to modify obj status", K(ret));
4362} else if (OB_FAIL(drop_table_for_not_dropped_schema(
4363table_schema, trans, ddl_stmt_str, is_truncate_table,
4364drop_table_set, is_drop_db))) {
4365LOG_WARN("drop table for not dropped shema failed", K(ret));
4366} else if (table_schema.is_view_table()
4367&& OB_FAIL(ObDependencyInfo::delete_schema_object_dependency(
4368trans,
4369tenant_id,
4370table_schema.get_table_id(),
4371table_schema.get_schema_version(),
4372ObObjectType::VIEW))) {
4373LOG_WARN("failed to delete_schema_object_dependency", K(ret), K(tenant_id),
4374K(table_schema.get_table_id()));
4375}
4376
4377if (OB_FAIL(ret)) {
4378} else if ((table_schema.is_aux_table() || table_schema.is_mlog_table())
4379&& !is_inner_table(table_schema.get_table_id())) {
4380ObSnapshotInfoManager snapshot_mgr;
4381ObArray<ObTabletID> tablet_ids;
4382SCN invalid_scn;
4383if (OB_FAIL(snapshot_mgr.init(GCTX.self_addr()))) {
4384LOG_WARN("fail to init snapshot mgr", K(ret));
4385} else if (OB_FAIL(table_schema.get_tablet_ids(tablet_ids))) {
4386LOG_WARN("fail to get tablet ids", K(ret));
4387// when a index or lob is dropped, it should release all snapshots acquired, otherwise
4388// if a building index is dropped in another session, the index build task cannot release snapshots
4389// because the task needs schema to know tablet ids.
4390} else if (OB_FAIL(snapshot_mgr.batch_release_snapshot_in_trans(
4391trans, SNAPSHOT_FOR_DDL, tenant_id, -1/*schema_version*/, invalid_scn/*snapshot_scn*/, tablet_ids))) {
4392LOG_WARN("fail to release ddl snapshot acquired by this table", K(ret));
4393}
4394}
4395
4396if (OB_FAIL(ret)) {
4397} else if (table_schema.is_external_table()) {
4398if (OB_FAIL(ObExternalTableFileManager::get_instance().clear_inner_table_files(
4399table_schema.get_tenant_id(), table_schema.get_table_id(), trans))) {
4400LOG_WARN("delete external table file list failed", K(ret));
4401}
4402} else {
4403if (OB_FAIL(drop_tablet_of_table(table_schema, trans))) {
4404LOG_WARN("fail to drop tablet", K(table_schema), KR(ret));
4405}
4406}
4407
4408if (OB_SUCC(ret)) {
4409const uint64_t table_id = table_schema.get_table_id();
4410if (table_schema.is_materialized_view()) {
4411if (OB_FAIL(ObMViewSchedJobUtils::remove_mview_refresh_job(
4412trans, tenant_id, table_id))) {
4413LOG_WARN("failed to remove mview refresh job",
4414KR(ret), K(tenant_id), K(table_id));
4415}
4416} else if (table_schema.is_mlog_table()) {
4417if (OB_FAIL(ObMViewSchedJobUtils::remove_mlog_purge_job(
4418trans, tenant_id, table_id))) {
4419LOG_WARN("failed to remove mlog purge job",
4420KR(ret), K(tenant_id), K(table_id));
4421}
4422}
4423}
4424
4425return ret;
4426}
4427
4428int ObDDLOperator::drop_table_for_not_dropped_schema(
4429const ObTableSchema &table_schema,
4430ObMySQLTransaction &trans,
4431const ObString *ddl_stmt_str/*=NULL*/,
4432const bool is_truncate_table/*false*/,
4433DropTableIdHashSet *drop_table_set/*=NULL*/,
4434const bool is_drop_db/*false*/)
4435{
4436int ret = OB_SUCCESS;
4437const uint64_t tenant_id = table_schema.get_tenant_id();
4438int64_t new_schema_version = OB_INVALID_VERSION;
4439ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
4440ObSchemaGetterGuard schema_guard;
4441if (OB_ISNULL(schema_service_impl)) {
4442ret = OB_ERR_SYS;
4443LOG_ERROR("schema_service_impl must not null", K(ret));
4444} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
4445LOG_WARN("failed to get schema guard", K(ret));
4446}
4447//delete all object privileges granted on the object
4448uint64_t obj_type = static_cast<uint64_t>(ObObjectType::TABLE);
4449uint64_t table_id = table_schema.get_table_id();
4450if (OB_SUCC(ret) && !is_drop_db) {
4451OZ (drop_obj_privs(tenant_id, table_id, obj_type, trans),tenant_id, table_id, obj_type);
4452}
4453if (OB_FAIL(ret)) {
4454} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
4455LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
4456} else if (OB_FAIL(cleanup_autoinc_cache(table_schema))) {
4457LOG_WARN("fail cleanup auto inc global cache", K(ret));
4458} else if (OB_FAIL(drop_sequence_in_drop_table(table_schema, trans, schema_guard))) {
4459LOG_WARN("drop sequence in drop table fail", K(ret));
4460} else if (OB_FAIL(drop_rls_object_in_drop_table(table_schema, trans, schema_guard))) {
4461LOG_WARN("fail to drop rls object in drop table", K(ret));
4462} else if (OB_FAIL(schema_service_impl->get_table_sql_service().drop_table(
4463table_schema,
4464new_schema_version,
4465trans,
4466ddl_stmt_str,
4467is_truncate_table,
4468is_drop_db,
4469&schema_guard,
4470drop_table_set))) {
4471LOG_WARN("schema_service_impl drop_table failed", K(table_schema), K(ret));
4472} else if (OB_FAIL(sync_version_for_cascade_table(tenant_id, table_schema.get_base_table_ids(), trans))
4473|| OB_FAIL(sync_version_for_cascade_table(tenant_id, table_schema.get_depend_table_ids(), trans))) {
4474LOG_WARN("fail to sync versin for cascade tables", K(ret), K(tenant_id),
4475K(table_schema.get_base_table_ids()), K(table_schema.get_depend_table_ids()));
4476} else if (OB_FAIL(sync_version_for_cascade_mock_fk_parent_table(table_schema.get_tenant_id(), table_schema.get_depend_mock_fk_parent_table_ids(), trans))) {
4477LOG_WARN("fail to sync cascade depend_mock_fk_parent_table_ids table", K(ret));
4478}
4479
4480// delete audit in table
4481if (OB_SUCC(ret) && (table_schema.is_user_table() || table_schema.is_external_table())) {
4482ObArray<const ObSAuditSchema *> audits;
4483if (OB_FAIL(schema_guard.get_audit_schema_in_owner(tenant_id,
4484AUDIT_TABLE,
4485table_schema.get_table_id(),
4486audits))) {
4487LOG_WARN("get get_audit_schema_in_owner failed", K(tenant_id), K(table_schema), K(ret));
4488} else {
4489common::ObSqlString public_sql_string;
4490for (int64_t i = 0; OB_SUCC(ret) && i < audits.count(); ++i) {
4491const ObSAuditSchema *audit_schema = audits.at(i);
4492if (OB_ISNULL(audit_schema)) {
4493ret = OB_ERR_UNEXPECTED;
4494LOG_WARN("audit_schema is NULL", K(ret));
4495} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
4496LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
4497} else if (OB_FAIL(schema_service_impl->get_audit_sql_service().handle_audit_metainfo(
4498*audit_schema,
4499AUDIT_MT_DEL,
4500false,
4501new_schema_version,
4502NULL,
4503trans,
4504public_sql_string))) {
4505LOG_WARN("drop audit_schema failed", KPC(audit_schema), K(ret));
4506}
4507}
4508}
4509}
4510return ret;
4511}
4512
4513// ref
4514// When tables with auto-increment columns are frequently created or deleted, if the auto-increment column cache is not cleared, the memory will grow slowly.
4515// so every time when you drop table, if you bring auto-increment columns, clean up the corresponding cache.
4516int ObDDLOperator::cleanup_autoinc_cache(const ObTableSchema &table_schema)
4517{
4518int ret = OB_SUCCESS;
4519ObAutoincrementService &autoinc_service = share::ObAutoincrementService::get_instance();
4520uint64_t tenant_id = table_schema.get_tenant_id();
4521bool is_restore = false;
4522if (OB_FAIL(schema_service_.check_tenant_is_restore(NULL, tenant_id, is_restore))) {
4523LOG_WARN("fail to check if tenant is restore", KR(ret), K(tenant_id));
4524} else if (is_restore) {
4525// bugfix:
4526// skip
4527} else if (0 != table_schema.get_autoinc_column_id()) {
4528uint64_t table_id = table_schema.get_table_id();
4529uint64_t autoinc_column_id = table_schema.get_autoinc_column_id();
4530LOG_INFO("begin to clear all auto-increment cache",
4531K(tenant_id), K(table_id), K(autoinc_column_id));
4532if (OB_FAIL(autoinc_service.clear_autoinc_cache_all(tenant_id,
4533table_id,
4534autoinc_column_id,
4535table_schema.is_order_auto_increment_mode()))) {
4536LOG_WARN("failed to clear auto-increment cache",
4537K(tenant_id), K(table_id));
4538}
4539}
4540return ret;
4541}
4542
4543bool ObDDLOperator::is_aux_object(const ObDatabaseSchema &schema)
4544{
4545UNUSED(schema);
4546return false;
4547}
4548
4549bool ObDDLOperator::is_aux_object(const ObTableSchema &schema)
4550{
4551return schema.is_aux_table();
4552}
4553
4554bool ObDDLOperator::is_aux_object(const ObTriggerInfo &schema)
4555{
4556UNUSED(schema);
4557return false;
4558}
4559
4560bool ObDDLOperator::is_aux_object(const ObTenantSchema &schema)
4561{
4562UNUSED(schema);
4563return false;
4564}
4565
4566bool ObDDLOperator::is_global_index_object(const ObDatabaseSchema &schema)
4567{
4568UNUSED(schema);
4569return false;
4570}
4571
4572bool ObDDLOperator::is_global_index_object(const ObTableSchema &schema)
4573{
4574// For global local storage, local indexes are still seen, liboblog does not need to be synchronized
4575return schema.is_global_index_table() && (!schema.is_index_local_storage());
4576}
4577
4578bool ObDDLOperator::is_global_index_object(const ObTriggerInfo &schema)
4579{
4580UNUSED(schema);
4581return false;
4582}
4583
4584bool ObDDLOperator::is_global_index_object(const ObTenantSchema &schema)
4585{
4586UNUSED(schema);
4587return false;
4588}
4589
4590int ObDDLOperator::drop_table_to_recyclebin(const ObTableSchema &table_schema,
4591ObSchemaGetterGuard &schema_guard,
4592ObMySQLTransaction &trans,
4593const ObString *ddl_stmt_str,/*= NULL*/
4594const bool is_truncate_table)
4595{
4596int ret = OB_SUCCESS;
4597ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
4598const uint64_t tenant_id = table_schema.get_tenant_id();
4599int64_t new_schema_version = OB_INVALID_VERSION;
4600bool recycle_db_exist = false;
4601// materialized view will not be dropped into recyclebin
4602if (table_schema.get_table_type() == MATERIALIZED_VIEW) {
4603LOG_WARN("bypass recyclebin for materialized view");
4604} else if (OB_UNLIKELY(table_schema.has_mlog_table())) {
4605ret = OB_ERR_UNEXPECTED;
4606LOG_WARN("table with materialized view log should not come to recyclebin", KR(ret));
4607} else if (OB_UNLIKELY(table_schema.get_table_type() == MATERIALIZED_VIEW_LOG)) {
4608ret = OB_ERR_UNEXPECTED;
4609LOG_WARN("materialized view log should not come to recyclebin", KR(ret));
4610} else if (OB_ISNULL(schema_service_impl)) {
4611ret = OB_ERR_UNEXPECTED;
4612LOG_ERROR("schema_service_impl must not null", K(ret));
4613} else if (OB_INVALID_ID == tenant_id) {
4614ret = OB_INVALID_ARGUMENT;
4615LOG_WARN("tenant_id is invalid", K(ret));
4616} else if (OB_FAIL(schema_guard.check_database_exist(tenant_id,
4617OB_RECYCLEBIN_SCHEMA_ID,
4618recycle_db_exist))) {
4619LOG_WARN("check database exist failed", K(ret), K(tenant_id));
4620} else if (!recycle_db_exist) {
4621ret = OB_ERR_UNEXPECTED;
4622LOG_WARN("__recyclebin db not exist", K(ret));
4623} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
4624LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
4625} else if (OB_FAIL(cleanup_autoinc_cache(table_schema))) {
4626LOG_WARN("fail cleanup auto inc global cache", K(ret));
4627} else if (OB_FAIL(ObDependencyInfo::modify_dep_obj_status(trans, tenant_id, table_schema.get_table_id(),
4628*this, schema_service_))) {
4629LOG_WARN("failed to modify dep obj status", K(ret));
4630} else if (table_schema.is_view_table()
4631&& OB_FAIL(ObDependencyInfo::delete_schema_object_dependency(
4632trans,
4633tenant_id,
4634table_schema.get_table_id(),
4635table_schema.get_schema_version(),
4636ObObjectType::VIEW))) {
4637LOG_WARN("failed to delete_schema_object_dependency", K(ret), K(tenant_id),
4638K(table_schema.get_table_id()));
4639} else {
4640ObTableSchema new_table_schema;
4641if (OB_FAIL(new_table_schema.assign(table_schema))) {
4642LOG_WARN("fail to assign schema", K(ret));
4643} else {
4644ObSqlString new_table_name;
4645//move to the recyclebin db
4646new_table_schema.set_database_id(OB_RECYCLEBIN_SCHEMA_ID);
4647uint64_t tablegroup_id = table_schema.get_tablegroup_id();
4648if (OB_INVALID_ID != tablegroup_id) {
4649const ObTablegroupSchema *tablegroup_schema = nullptr;
4650if (OB_FAIL(schema_guard.get_tablegroup_schema(
4651tenant_id,
4652tablegroup_id,
4653tablegroup_schema))) {
4654LOG_WARN("get tablegroup schema failed", K(ret), K(tenant_id));
4655} else {
4656new_table_schema.set_tablegroup_id(OB_INVALID_ID);
4657}
4658}
4659new_table_schema.set_schema_version(new_schema_version);
4660ObSchemaOperationType op_type = OB_INVALID_DDL_OP;
4661if (OB_FAIL(ret)) {
4662} else if (OB_FAIL(construct_new_name_for_recyclebin(new_table_schema, new_table_name))) {
4663LOG_WARN("failed to construct new name for table", K(ret));
4664} else if (OB_FAIL(new_table_schema.set_table_name(new_table_name.string()))) {
4665LOG_WARN("failed to set new table name!", K(new_table_name), K(table_schema), K(ret));
4666} else {
4667ObRecycleObject recycle_object;
4668recycle_object.set_object_name(new_table_name.string());
4669recycle_object.set_original_name(table_schema.get_table_name_str());
4670recycle_object.set_tenant_id(table_schema.get_tenant_id());
4671recycle_object.set_database_id(table_schema.get_database_id());
4672recycle_object.set_table_id(table_schema.get_table_id());
4673recycle_object.set_tablegroup_id(table_schema.get_tablegroup_id());
4674op_type = table_schema.is_view_table()
4675? OB_DDL_DROP_VIEW_TO_RECYCLEBIN : OB_DDL_DROP_TABLE_TO_RECYCLEBIN;
4676if (is_truncate_table) {
4677op_type = OB_DDL_TRUNCATE_DROP_TABLE_TO_RECYCLEBIN;
4678}
4679if (OB_FAIL(recycle_object.set_type_by_table_schema(table_schema))) {
4680LOG_WARN("set type by table schema failed", K(ret));
4681} else if (OB_FAIL(schema_service_impl->insert_recyclebin_object(recycle_object,
4682trans))) {
4683LOG_WARN("insert recycle object failed", K(ret));
4684}
4685}
4686if (OB_SUCC(ret)) {
4687if (OB_FAIL(schema_service_impl->get_table_sql_service().update_table_options(
4688trans,
4689table_schema,
4690new_table_schema,
4691op_type,
4692ddl_stmt_str))) {
4693LOG_WARN("failed to alter table option!", K(ret));
4694}
4695}
4696}
4697}
4698return ret;
4699}
4700
4701int ObDDLOperator::flashback_table_from_recyclebin(const ObTableSchema &table_schema,
4702ObTableSchema &new_table_schema,
4703ObMySQLTransaction &trans,
4704const uint64_t new_db_id,
4705const ObString &new_table_name,
4706const ObString *ddl_stmt_str,
4707ObSchemaGetterGuard &guard)
4708{
4709int ret = OB_SUCCESS;
4710ObSchemaService *schema_service = schema_service_.get_schema_service();
4711ObArray<ObRecycleObject> recycle_objs;
4712const uint64_t tenant_id = table_schema.get_tenant_id();
4713int64_t new_schema_version = OB_INVALID_VERSION;
4714ObRecycleObject::RecycleObjType recycle_type = ObRecycleObject::get_type_by_table_schema(table_schema);
4715ObArenaAllocator allocator(ObModIds::OB_SCHEMA);
4716if (OB_ISNULL(schema_service)) {
4717ret = OB_ERR_UNEXPECTED;
4718LOG_WARN("schema_service should not be null", K(ret));
4719} else if (OB_INVALID_ID == table_schema.get_tenant_id()) {
4720ret = OB_INVALID_ARGUMENT;
4721LOG_WARN("tenant_id is invalid", K(ret));
4722} else if (OB_FAIL(schema_service->fetch_recycle_object(
4723tenant_id,
4724table_schema.get_table_name_str(),
4725recycle_type,
4726trans,
4727recycle_objs))) {
4728LOG_WARN("get_recycle_object failed", K(tenant_id), K(ret));
4729} else if (recycle_objs.size() != 1) {
4730ret = OB_ERR_UNEXPECTED;
4731LOG_WARN("unexpected recycle object num", K(ret),
4732"table_name", table_schema.get_table_name_str(),
4733"size", recycle_objs.size());
4734} else {
4735const ObRecycleObject &recycle_obj = recycle_objs.at(0);
4736if (OB_FAIL(new_table_schema.assign(table_schema))) {
4737LOG_WARN("fail to assign schema", K(ret));
4738} else if (new_db_id != OB_INVALID_ID) {//flashback to new db
4739new_table_schema.set_database_id(new_db_id);
4740if (new_table_schema.is_aux_table()) {
4741// should set the old name
4742// When flashback table to new db, distinguish between empty index name and renaming flashback index
4743if (!new_table_name.empty() && OB_FAIL(new_table_schema.set_table_name(new_table_name))) {
4744LOG_WARN("set new table name failed", K(ret));
4745} else if (new_table_name.empty() && OB_FAIL(new_table_schema.set_table_name(recycle_obj.get_original_name()))) {
4746LOG_WARN("set new table name failed", K(ret));
4747} else if (new_table_schema.is_index_table()) {
4748const int VISIBLE = 0;
4749const uint64_t DROPINDEX = 0;
4750new_table_schema.set_drop_index(DROPINDEX);
4751if (!table_schema.is_invisible_before()) {
4752new_table_schema.set_index_visibility(VISIBLE);
4753}
4754new_table_schema.set_invisible_before(0);
4755}
4756if (OB_SUCC(ret) && new_table_schema.is_index_table()) {
4757bool is_oracle_mode = false;
4758if (OB_FAIL(new_table_schema.check_if_oracle_compat_mode(is_oracle_mode))) {
4759LOG_WARN("fail check if oracle mode", K(ret));
4760} else if (is_oracle_mode) {
4761ObString new_idx_name;
4762if (OB_FAIL(ObTableSchema::create_new_idx_name_after_flashback(new_table_schema,
4763new_idx_name,
4764allocator,
4765guard))) {
4766} else if (OB_FAIL(new_table_schema.set_table_name(new_idx_name))) {
4767LOG_WARN("set new table name failed", K(ret));
4768}
4769}
4770}
4771} else {
4772if (!new_table_name.empty()) {
4773if (OB_FAIL(new_table_schema.set_table_name(new_table_name))) {
4774LOG_WARN("set new table name failed", K(ret));
4775}
4776} else {
4777ret = OB_ERR_UNEXPECTED;
4778LOG_WARN("database is valid, but no table name for data table",
4779K(ret), K(new_table_name));
4780}
4781}
4782} else {
4783//set original db_id
4784const ObDatabaseSchema *db_schema = NULL;
4785if (OB_FAIL(guard.get_database_schema(tenant_id,
4786recycle_obj.get_database_id(),
4787db_schema))) {
4788LOG_WARN("get database schema failed", K(ret));
4789} else if (NULL == db_schema) {
4790ret = OB_ERR_UNEXPECTED;
4791LOG_WARN("database not exist", K(recycle_obj), K(ret));
4792} else if (db_schema->is_in_recyclebin()) {
4793ret = OB_OP_NOT_ALLOW;
4794LOG_WARN("flashback table to __recyclebin database is not allowed",
4795K(recycle_obj), K(*db_schema), K(ret));
4796} else if (OB_FAIL(new_table_schema.set_table_name(recycle_obj.get_original_name()))) {
4797LOG_WARN("set table name failed", K(ret), K(recycle_obj));
4798} else {
4799new_table_schema.set_database_id(recycle_obj.get_database_id());
4800}
4801
4802if (OB_SUCC(ret) && new_table_schema.is_index_table()) {
4803const int VISIBLE = 0;
4804const uint64_t DROPINDEX = 0;
4805new_table_schema.set_drop_index(DROPINDEX);
4806if (!table_schema.is_invisible_before()) {
4807new_table_schema.set_index_visibility(VISIBLE);
4808}
4809new_table_schema.set_invisible_before(0);
4810}
4811if (OB_SUCC(ret) && new_table_schema.is_index_table()) {
4812bool is_oracle_mode = false;
4813if (OB_FAIL(new_table_schema.check_if_oracle_compat_mode(is_oracle_mode))) {
4814LOG_WARN("fail check if oracle mode", K(ret));
4815} else if (is_oracle_mode) {
4816ObString new_idx_name;
4817if (OB_FAIL(ObTableSchema::create_new_idx_name_after_flashback(new_table_schema,
4818new_idx_name,
4819allocator,
4820guard))) {
4821} else if (OB_FAIL(new_table_schema.set_table_name(new_idx_name))) {
4822LOG_WARN("set new table name failed", K(ret));
4823}
4824}
4825}
4826}
4827if (OB_SUCC(ret)) {
4828bool is_table_exist = true;
4829bool object_exist = false;
4830uint64_t synonym_id = OB_INVALID_ID;
4831const int64_t table_schema_version = OB_INVALID_VERSION; // Take the latest local schema_guard
4832ObSchemaOperationType op_type = new_table_schema.is_view_table()
4833? OB_DDL_FLASHBACK_VIEW : OB_DDL_FLASHBACK_TABLE;
4834if (new_table_schema.is_index_table()) {
4835op_type = OB_DDL_FLASHBACK_INDEX;
4836}
4837if (OB_FAIL(schema_service_.check_synonym_exist(tenant_id,
4838new_table_schema.get_database_id(),
4839new_table_schema.get_table_name_str(),
4840object_exist,
4841synonym_id))) {
4842LOG_WARN("fail to check synonym exist", K(new_table_schema), K(ret));
4843} else if (object_exist) {
4844ret = OB_ERR_EXIST_OBJECT;
4845LOG_WARN("Name is already used by an existing object", K(new_table_schema), K(ret));
4846} else if (OB_FAIL(schema_service_.check_table_exist(tenant_id,
4847new_table_schema.get_database_id(),
4848new_table_schema.get_table_name_str(),
4849new_table_schema.is_index_table(),
4850table_schema_version,
4851is_table_exist))) {
4852LOG_WARN("check_table exist failed", K(ret));
4853} else if (is_table_exist) {
4854ret = OB_ERR_TABLE_EXIST;
4855LOG_USER_ERROR(OB_ERR_TABLE_EXIST, recycle_obj.get_original_name().length(),
4856recycle_obj.get_original_name().ptr());
4857} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
4858LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
4859} else if (FALSE_IT(new_table_schema.set_schema_version(new_schema_version))) {
4860} else if (OB_FAIL(schema_service->get_table_sql_service().update_table_options(
4861trans,
4862table_schema,
4863new_table_schema,
4864op_type,
4865ddl_stmt_str))) {
4866LOG_WARN("update_table_options failed", K(ret));
4867} else if (OB_FAIL(schema_service->delete_recycle_object(tenant_id,
4868recycle_obj,
4869trans))) {
4870LOG_WARN("delete_recycle_object failed", K(tenant_id), K(ret));
4871}
4872}
4873}
4874return ret;
4875}
4876
4877int ObDDLOperator::purge_table_with_aux_table(
4878const ObTableSchema &table_schema,
4879ObSchemaGetterGuard &schema_guard,
4880ObMySQLTransaction &trans,
4881const ObString *ddl_stmt_str)
4882{
4883int ret = OB_SUCCESS;
4884if (!table_schema.is_aux_table()) {
4885if (OB_FAIL(purge_aux_table(table_schema, schema_guard, trans, USER_INDEX))) {
4886LOG_WARN("purge_aux_table failed", K(ret), K(table_schema));
4887} else if (OB_FAIL(purge_aux_table(table_schema, schema_guard, trans,
4888AUX_VERTIAL_PARTITION_TABLE))) {
4889LOG_WARN("purge_aux_table failed", K(ret), K(table_schema));
4890} else if (OB_FAIL(purge_aux_table(table_schema, schema_guard, trans,
4891AUX_LOB_META))) {
4892LOG_WARN("purge_aux_lob_meta_table failed", K(ret), K(table_schema));
4893} else if (OB_FAIL(purge_aux_table(table_schema, schema_guard, trans,
4894AUX_LOB_PIECE))) {
4895LOG_WARN("purge_aux_lob_piece_table failed", K(ret), K(table_schema));
4896} else if (OB_FAIL(purge_table_trigger(table_schema, schema_guard, trans))) {
4897LOG_WARN("purge_trigger failed", K(ret), K(table_schema));
4898}
4899}
4900if (OB_SUCC(ret)) {
4901if (OB_FAIL(purge_table_in_recyclebin(table_schema,
4902trans,
4903ddl_stmt_str))) {
4904LOG_WARN("purge table failed", K(ret));
4905}
4906}
4907return ret;
4908}
4909
4910int ObDDLOperator::purge_aux_table(
4911const ObTableSchema &table_schema,
4912ObSchemaGetterGuard &schema_guard,
4913ObMySQLTransaction &trans,
4914const ObTableType table_type)
4915{
4916int ret = OB_SUCCESS;
4917const uint64_t tenant_id = table_schema.get_tenant_id();
4918ObSEArray<uint64_t, 16> aux_tid_array; // for aux_vp or aux_lob
4919ObSEArray<ObAuxTableMetaInfo, 16> simple_index_infos;
4920bool is_index = false;
4921if (USER_INDEX == table_type) {
4922is_index = true;
4923if (OB_FAIL(table_schema.get_simple_index_infos(simple_index_infos))) {
4924LOG_WARN("get_simple_index_infos failed", K(ret), K(table_schema));
4925}
4926} else if (AUX_LOB_META == table_type) {
4927const uint64_t aux_lob_meta_tid = table_schema.get_aux_lob_meta_tid();
4928if (OB_INVALID_ID != aux_lob_meta_tid && OB_FAIL(aux_tid_array.push_back(aux_lob_meta_tid))) {
4929LOG_WARN("push back aux_lob_meta_tid failed", K(ret));
4930}
4931} else if (AUX_LOB_PIECE == table_type) {
4932const uint64_t aux_lob_piece_tid = table_schema.get_aux_lob_piece_tid();
4933if (OB_INVALID_ID != aux_lob_piece_tid && OB_FAIL(aux_tid_array.push_back(aux_lob_piece_tid))) {
4934LOG_WARN("push back aux_lob_piece_tid failed", K(ret));
4935}
4936} else if (AUX_VERTIAL_PARTITION_TABLE == table_type) {
4937if (OB_FAIL(table_schema.get_aux_vp_tid_array(aux_tid_array))) {
4938LOG_WARN("get_aux_vp_tid_array failed", K(ret), K(table_schema));
4939}
4940} else {
4941ret = OB_INVALID_ARGUMENT;
4942LOG_WARN("invalid table type", K(ret), K(table_type));
4943}
4944
4945int64_t N = is_index ? simple_index_infos.count() : aux_tid_array.count();
4946for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) {
4947const ObTableSchema *aux_table_schema = NULL;
4948uint64_t tid = is_index ? simple_index_infos.at(i).table_id_ : aux_tid_array.at(i);
4949if (OB_FAIL(schema_guard.get_table_schema(tenant_id, tid, aux_table_schema))) {
4950LOG_WARN("get_table_schema failed", K(tenant_id), "table id", tid, K(ret));
4951} else if (OB_ISNULL(aux_table_schema)) {
4952ret = OB_ERR_UNEXPECTED;
4953LOG_WARN("table schema should not be null", K(ret));
4954} else if (OB_FAIL(purge_table_in_recyclebin(*aux_table_schema,
4955trans,
4956NULL /*ddl_stmt_str*/))) {
4957LOG_WARN("ddl_operator drop_table failed", K(*aux_table_schema), K(ret));
4958}
4959}
4960
4961return ret;
4962}
4963
4964int ObDDLOperator::purge_table_in_recyclebin(const ObTableSchema &table_schema,
4965ObMySQLTransaction &trans,
4966const ObString *ddl_stmt_str/*=NULL*/)
4967{
4968int ret = OB_SUCCESS;
4969ObSchemaService *schema_service = schema_service_.get_schema_service();
4970ObArray<ObRecycleObject> recycle_objs;
4971ObRecycleObject::RecycleObjType recycle_type = ObRecycleObject::get_type_by_table_schema(table_schema);
4972
4973if (OB_ISNULL(schema_service)) {
4974ret = OB_ERR_UNEXPECTED;
4975LOG_WARN("schema_service should not be null", K(ret));
4976} else if (OB_INVALID_ID == table_schema.get_tenant_id()) {
4977ret = OB_INVALID_ARGUMENT;
4978LOG_WARN("tenant_id is invalid", K(ret));
4979} else if (OB_FAIL(schema_service->fetch_recycle_object(
4980table_schema.get_tenant_id(),
4981table_schema.get_table_name_str(),
4982recycle_type,
4983trans,
4984recycle_objs))) {
4985LOG_WARN("get_recycle_object failed", K(recycle_type), K(ret));
4986} else if (recycle_objs.size() != 1) {
4987ret = OB_ERR_UNEXPECTED;
4988LOG_WARN("unexpected recycle object num", K(ret), K(recycle_objs.size()));
4989} else if (OB_FAIL(schema_service->delete_recycle_object(
4990table_schema.get_tenant_id(),
4991recycle_objs.at(0),
4992trans))) {
4993LOG_WARN("delete_recycle_object failed", K(ret), "ObRecycleObject", recycle_objs.at(0));
4994} else if (OB_FAIL(drop_table(table_schema, trans, ddl_stmt_str, false))) {
4995LOG_WARN("drop table failed", K(ret));
4996}
4997return ret;
4998}
4999
5000int ObDDLOperator::create_index_in_recyclebin(ObTableSchema &table_schema,
5001ObSchemaGetterGuard &schema_guard,
5002ObMySQLTransaction &trans,
5003const ObString *ddl_stmt_str) {
5004int ret = OB_SUCCESS;
5005
5006if (table_schema.get_table_type() != USER_INDEX) {
5007ret = OB_ERR_UNEXPECTED;
5008LOG_WARN("table_schema type is not index", K(ret));
5009} else {
5010ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
5011uint64_t tenant_id = table_schema.get_tenant_id();
5012int64_t new_schema_version = OB_INVALID_VERSION;
5013bool recycle_db_exist = false;
5014if (OB_ISNULL(schema_service_impl)) {
5015ret = OB_ERR_UNEXPECTED;
5016LOG_ERROR("schema_service_impl must not be null", K(ret));
5017} else if (OB_FAIL(schema_guard.check_database_exist(tenant_id,
5018OB_RECYCLEBIN_SCHEMA_ID, recycle_db_exist))) {
5019LOG_WARN("check database exist failed", K(ret));
5020} else if (!recycle_db_exist) {
5021ret = OB_ERR_UNEXPECTED;
5022LOG_WARN("__recyclebin db not exist", K(ret));
5023} else {
5024ObSqlString new_table_name;
5025ObTableSchema new_table_schema;
5026if (OB_FAIL(new_table_schema.assign(table_schema))) {
5027LOG_WARN("fail to assign schema", K(ret));
5028} else {
5029new_table_schema.set_database_id(OB_RECYCLEBIN_SCHEMA_ID);
5030new_table_schema.set_tablegroup_id(OB_INVALID_ID);
5031}
5032if (OB_FAIL(ret)) {
5033} else if (OB_FAIL(construct_new_name_for_recyclebin(table_schema, new_table_name))) {
5034LOG_WARN("failed to construct new name for table", K(ret));
5035} else if (OB_FAIL(new_table_schema.set_table_name(new_table_name.string()))) {
5036LOG_WARN("failed to set new table name!", K(new_table_name), K(table_schema), K(ret));
5037} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
5038LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
5039} else {
5040new_table_schema.set_schema_version(new_schema_version);
5041ObRecycleObject recycle_object;
5042recycle_object.set_object_name(new_table_name.string());
5043recycle_object.set_original_name(table_schema.get_table_name_str());
5044recycle_object.set_tenant_id(tenant_id);
5045recycle_object.set_database_id(table_schema.get_database_id());
5046recycle_object.set_table_id(table_schema.get_table_id());
5047recycle_object.set_tablegroup_id(table_schema.get_tablegroup_id());
5048if (OB_FAIL(recycle_object.set_type_by_table_schema(table_schema))) {
5049LOG_WARN("set type by table schema failed", K(ret));
5050} else if (OB_FAIL(schema_service_impl->insert_recyclebin_object(recycle_object,
5051trans))) {
5052LOG_WARN("insert recycle object failed", K(ret));
5053} else if (OB_FAIL(schema_service_impl->get_table_sql_service().create_table(
5054new_table_schema,
5055trans,
5056ddl_stmt_str,
5057true,
5058true))) {
5059LOG_WARN("failed to create table in recyclebin", K(ret));
5060} else if (OB_FAIL(sync_version_for_cascade_table(
5061tenant_id, new_table_schema.get_depend_table_ids(), trans))) {
5062LOG_WARN("fail to sync cascade depend table", K(ret));
5063}
5064}
5065}
5066}
5067return ret;
5068}
5069
5070int ObDDLOperator::update_tablegroup_id_of_tables(const ObDatabaseSchema &database_schema,
5071ObMySQLTransaction &trans,
5072ObSchemaGetterGuard &schema_guard)
5073{
5074int ret = OB_SUCCESS;
5075const uint64_t tenant_id = database_schema.get_tenant_id();
5076int64_t new_schema_version = OB_INVALID_VERSION;
5077ObArray<const ObTableSchema*> table_schemas;
5078ObSchemaService *schema_service = schema_service_.get_schema_service();
5079if (OB_FAIL(schema_guard.get_table_schemas_in_database(tenant_id,
5080database_schema.get_database_id(),
5081table_schemas))) {
5082LOG_WARN("get_table_schemas_in_database failed", K(ret), K(tenant_id));
5083}
5084bool tg_exist = false;
5085for (int64_t idx = 0; OB_SUCC(ret) && idx < table_schemas.count(); ++idx) {
5086const ObTableSchema *table = table_schemas.at(idx);
5087if (OB_ISNULL(table)) {
5088ret = OB_ERR_UNEXPECTED;
5089LOG_WARN("table schema should not be null", K(ret));
5090} else if (table->is_index_table()) {
5091continue;
5092} else if (OB_INVALID_ID != table->get_tablegroup_id() &&
5093OB_FAIL(schema_guard.check_tablegroup_exist(table->get_tenant_id(),
5094table->get_tablegroup_id(), tg_exist))) {
5095LOG_WARN("check_tablegroup_exist failed", K(ret), KPC(table));
5096} else {
5097ObSEArray<ObAuxTableMetaInfo, 16> simple_index_infos;
5098if (OB_FAIL(table->get_simple_index_infos(simple_index_infos))) {
5099LOG_WARN("get_index_tid_array failed", K(ret));
5100}
5101for (int64_t i = 0; OB_SUCC(ret) && i < simple_index_infos.count(); ++i) {
5102const ObTableSchema *index_table_schema = NULL;
5103if (OB_FAIL(schema_guard.get_table_schema(tenant_id,
5104simple_index_infos.at(i).table_id_, index_table_schema))) {
5105LOG_WARN("get_table_schema failed", K(tenant_id),
5106"table id", simple_index_infos.at(i).table_id_, K(ret));
5107} else if (OB_ISNULL(index_table_schema)) {
5108ret = OB_ERR_UNEXPECTED;
5109LOG_WARN("table schema should not be null", K(ret));
5110} else {
5111ObTableSchema new_index_schema;
5112if (OB_FAIL(new_index_schema.assign(*index_table_schema))) {
5113LOG_WARN("fail to assign schema", K(ret));
5114} else {
5115if (!tg_exist) {
5116new_index_schema.set_tablegroup_id(OB_INVALID_ID);
5117}
5118if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
5119LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
5120} else if (FALSE_IT(new_index_schema.set_schema_version(new_schema_version))) {
5121} else if (OB_FAIL(schema_service->get_table_sql_service().update_table_options(
5122trans, *index_table_schema, new_index_schema,
5123OB_DDL_FLASHBACK_TABLE, NULL))) {
5124LOG_WARN("update_table_option failed", K(ret));
5125}
5126}
5127}
5128}
5129if (OB_SUCC(ret)) {
5130HEAP_VAR(ObTableSchema, new_ts) {
5131if (OB_FAIL(new_ts.assign(*table))) {
5132LOG_WARN("fail to assign schema", K(ret));
5133} else {
5134if (!tg_exist) {
5135new_ts.set_tablegroup_id(OB_INVALID_ID);
5136}
5137const ObSchemaOperationType op_type = new_ts.is_view_table()
5138? OB_DDL_FLASHBACK_VIEW : OB_DDL_FLASHBACK_TABLE;
5139if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
5140LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
5141} else if (FALSE_IT(new_ts.set_schema_version(new_schema_version))) {
5142} else if (OB_FAIL(schema_service->get_table_sql_service().update_table_options(
5143trans, *table, new_ts, op_type, NULL))) {
5144LOG_WARN("update_table_option failed", K(ret));
5145}
5146}
5147}
5148}
5149}
5150}
5151return ret;
5152}
5153
5154int ObDDLOperator::flashback_database_from_recyclebin(const ObDatabaseSchema &database_schema,
5155ObMySQLTransaction &trans,
5156const ObString &new_db_name,
5157ObSchemaGetterGuard &schema_guard,
5158const ObString &ddl_stmt_str)
5159{
5160int ret = OB_SUCCESS;
5161ObSchemaService *schema_service = schema_service_.get_schema_service();
5162ObArray<ObRecycleObject> recycle_objs;
5163if (OB_ISNULL(schema_service)) {
5164ret = OB_ERR_UNEXPECTED;
5165LOG_WARN("schema_service should not be null", K(ret));
5166} else if (OB_INVALID_ID == database_schema.get_tenant_id()) {
5167ret = OB_INVALID_ARGUMENT;
5168LOG_WARN("tenant_id is invalid", K(ret));
5169} else if (OB_FAIL(schema_service->fetch_recycle_object(
5170database_schema.get_tenant_id(),
5171database_schema.get_database_name(),
5172ObRecycleObject::DATABASE,
5173trans,
5174recycle_objs))) {
5175LOG_WARN("get_recycle_object failed", K(ret));
5176} else if (recycle_objs.size() != 1) {
5177ret = OB_ERR_UNEXPECTED;
5178LOG_WARN("unexpected recycle object num", K(ret));
5179} else {
5180const ObRecycleObject &recycle_obj = recycle_objs.at(0);
5181uint64_t tg_id = OB_INVALID_ID;
5182if (OB_INVALID_ID != recycle_obj.get_tablegroup_id()) {
5183bool tg_exist = false;
5184if (OB_FAIL(schema_guard.check_tablegroup_exist(recycle_obj.get_tenant_id(),
5185recycle_obj.get_tablegroup_id(),
5186tg_exist))) {
5187LOG_WARN("check_tablegroup_exist failed", K(ret), K(recycle_obj));
5188} else if (tg_exist) {
5189tg_id = recycle_obj.get_tablegroup_id();
5190}
5191}
5192if (OB_SUCC(ret)) {
5193ObDatabaseSchema new_db_schema = database_schema;
5194new_db_schema.set_in_recyclebin(false);
5195new_db_schema.set_default_tablegroup_id(tg_id);
5196if (!new_db_name.empty()) {
5197if (OB_FAIL(new_db_schema.set_database_name(new_db_name))) {
5198LOG_WARN("set database name failed", K(new_db_name));
5199}
5200} else {
5201//set original db_id
5202if (OB_FAIL(new_db_schema.set_database_name(recycle_obj.get_original_name()))) {
5203LOG_WARN("set database name failed", K(recycle_obj));
5204}
5205}
5206if (OB_SUCC(ret)) {
5207bool is_database_exist = true;
5208const uint64_t tenant_id = database_schema.get_tenant_id();
5209int64_t new_schema_version = OB_INVALID_VERSION;
5210if (OB_FAIL(schema_guard.check_database_exist(database_schema.get_tenant_id(),
5211new_db_schema.get_database_name_str(),
5212is_database_exist))) {
5213LOG_WARN("check database exist failed", K(ret), K(new_db_schema));
5214} else if (is_database_exist) {
5215ret = OB_DATABASE_EXIST;
5216LOG_USER_ERROR(OB_DATABASE_EXIST, new_db_schema.get_database_name_str().length(),
5217new_db_schema.get_database_name_str().ptr());
5218} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
5219LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
5220} else if (FALSE_IT(new_db_schema.set_schema_version(new_schema_version))) {
5221} else if (OB_FAIL(schema_service->get_database_sql_service().update_database(
5222new_db_schema,
5223trans,
5224OB_DDL_FLASHBACK_DATABASE,
5225&ddl_stmt_str))) {
5226LOG_WARN("update_database failed", K(ret), K(new_db_schema));
5227} else if (OB_FAIL(schema_service->delete_recycle_object(
5228database_schema.get_tenant_id(),
5229recycle_obj,
5230trans))) {
5231LOG_WARN("delete_recycle_object failed", K(ret), K(recycle_obj));
5232}
5233}
5234}
5235if (OB_SUCC(ret)) {
5236if (OB_FAIL(update_tablegroup_id_of_tables(database_schema, trans, schema_guard))) {
5237LOG_WARN("update tablegroup_id of tables failed", K(database_schema), K(ret));
5238}
5239}
5240}
5241return ret;
5242}
5243
5244int ObDDLOperator::purge_table_of_database(const ObDatabaseSchema &db_schema,
5245ObMySQLTransaction &trans)
5246{
5247int ret = OB_SUCCESS;
5248ObSchemaGetterGuard schema_guard;
5249const uint64_t tenant_id = db_schema.get_tenant_id();
5250const uint64_t database_id = db_schema.get_database_id();
5251ObSchemaService *schema_service = schema_service_.get_schema_service();
5252if (OB_ISNULL(schema_service)) {
5253ret = OB_ERR_UNEXPECTED;
5254LOG_WARN("schema_service should not be null", K(ret));
5255} else {
5256ObArray<ObRecycleObject> recycle_objs;
5257if (OB_FAIL(schema_service->fetch_recycle_objects_of_db(tenant_id,
5258database_id,
5259trans,
5260recycle_objs))) {
5261LOG_WARN("fetch recycle objects of db failed", K(ret));
5262} else {
5263for (int i = 0; OB_SUCC(ret) && i < recycle_objs.count(); ++i) {
5264const ObRecycleObject &recycle_obj = recycle_objs.at(i);
5265const ObTableSchema* table_schema = NULL;
5266if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
5267LOG_WARN("failed to get schema guard", K(ret));
5268} else if (OB_FAIL(schema_guard.get_table_schema(recycle_obj.get_tenant_id(),
5269recycle_obj.get_table_id(),
5270table_schema))) {
5271LOG_WARN("fail to get table_schema", KR(ret), K(recycle_obj));
5272} else if (OB_ISNULL(table_schema)) {
5273ret = OB_TABLE_NOT_EXIST;
5274LOG_WARN("table is not exist", K(ret), K(recycle_obj));
5275LOG_USER_ERROR(OB_TABLE_NOT_EXIST, to_cstring(db_schema.get_database_name_str()),
5276to_cstring(recycle_obj.get_object_name()));
5277} else if (OB_FAIL(purge_table_with_aux_table(*table_schema,
5278schema_guard,
5279trans,
5280NULL /*ddl_stmt_str */))) {
5281LOG_WARN("purge table with index failed", K(ret), K(recycle_obj));
5282}
5283}
5284}
5285}
5286return ret;
5287}
5288
5289int ObDDLOperator::purge_database_in_recyclebin(const ObDatabaseSchema &database_schema,
5290ObMySQLTransaction &trans,
5291const ObString *ddl_stmt_str)
5292{
5293int ret = OB_SUCCESS;
5294ObSchemaService *schema_service = schema_service_.get_schema_service();
5295if (OB_ISNULL(schema_service)) {
5296ret = OB_ERR_UNEXPECTED;
5297LOG_WARN("schema_service should not be null", K(ret));
5298} else if (OB_INVALID_ID == database_schema.get_tenant_id()) {
5299ret = OB_INVALID_ARGUMENT;
5300LOG_WARN("tenant_id is invalid", K(ret));
5301} else {
5302ObArray<ObRecycleObject> recycle_objs;
5303if (OB_FAIL(schema_service->fetch_recycle_object(
5304database_schema.get_tenant_id(),
5305database_schema.get_database_name_str(),
5306ObRecycleObject::DATABASE,
5307trans,
5308recycle_objs))) {
5309LOG_WARN("get_recycle_object failed", K(ret));
5310} else if (1 != recycle_objs.size()) {
5311ret = OB_ERR_UNEXPECTED;
5312LOG_WARN("unexpected recycle object num", K(ret));
5313} else if (OB_FAIL(drop_database(database_schema,
5314trans,
5315ddl_stmt_str))) {
5316LOG_WARN("drop_table failed", K(ret));
5317} else if (OB_FAIL(schema_service->delete_recycle_object(
5318database_schema.get_tenant_id(),
5319recycle_objs.at(0),
5320trans))) {
5321LOG_WARN("delete_recycle_object failed", K(ret));
5322}
5323}
5324return ret;
5325}
5326
5327int ObDDLOperator::fetch_expire_recycle_objects(
5328const uint64_t tenant_id,
5329const int64_t expire_time,
5330ObIArray<ObRecycleObject> &recycle_objs)
5331{
5332int ret = OB_SUCCESS;
5333ObSchemaService *schema_service = schema_service_.get_schema_service();
5334if (OB_ISNULL(schema_service)) {
5335ret = OB_ERR_UNEXPECTED;
5336LOG_WARN("schema_service should not be null", K(ret));
5337} else if (OB_FAIL(schema_service->fetch_expire_recycle_objects(tenant_id,
5338expire_time,
5339sql_proxy_,
5340recycle_objs))) {
5341LOG_WARN("fetch expire recycle objects failed", K(ret),
5342K(expire_time), K(tenant_id));
5343}
5344return ret;
5345}
5346
5347int ObDDLOperator::init_tenant_env(
5348const ObTenantSchema &tenant_schema,
5349const ObSysVariableSchema &sys_variable,
5350const share::ObTenantRole &tenant_role,
5351const SCN &recovery_until_scn,
5352const common::ObIArray<common::ObConfigPairs> &init_configs,
5353ObMySQLTransaction &trans)
5354{
5355int ret = OB_SUCCESS;
5356const uint64_t tenant_id = tenant_schema.get_tenant_id();
5357
5358if (OB_UNLIKELY(!recovery_until_scn.is_valid_and_not_min())) {
5359ret = OB_INVALID_ARGUMENT;
5360LOG_WARN("invalid recovery_until_scn", KR(ret), K(recovery_until_scn));
5361} else if (OB_FAIL(init_tenant_tablegroup(tenant_id, trans))) {
5362LOG_WARN("insert default tablegroup failed", K(tenant_id), K(ret));
5363} else if (OB_FAIL(init_tenant_databases(tenant_schema, sys_variable, trans))) {
5364LOG_WARN("insert default databases failed,", K(tenant_id), K(ret));
5365} else if (OB_FAIL(init_tenant_profile(tenant_id, sys_variable, trans))) {
5366LOG_WARN("fail to init tenant profile", K(tenant_id), K(ret));
5367} else if (OB_FAIL(init_tenant_users(tenant_schema, sys_variable, trans))) {
5368LOG_WARN("insert default user failed", K(tenant_id), K(ret));
5369} else if (OB_FAIL(init_tenant_keystore(tenant_id, sys_variable, trans))) {
5370LOG_WARN("fail to init tenant keystore", K(ret));
5371} else if (OB_FAIL(init_tenant_sys_stats(tenant_id, trans))) {
5372LOG_WARN("insert default sys stats failed", K(tenant_id), K(ret));
5373} else if (OB_FAIL(init_freeze_info(tenant_id, trans))) {
5374LOG_WARN("insert freeze info failed", K(tenant_id), KR(ret));
5375} else if (OB_FAIL(init_tenant_srs(tenant_id, trans))) {
5376LOG_WARN("insert tenant srs failed", K(tenant_id), K(ret));
5377} else if (OB_SYS_TENANT_ID == tenant_id) {
5378if (OB_FAIL(init_sys_tenant_charset(trans))) {
5379LOG_WARN("insert charset failed", K(tenant_id), K(ret));
5380} else if (OB_FAIL(init_sys_tenant_collation(trans))) {
5381LOG_WARN("insert collation failed", K(tenant_id), K(ret));
5382} else if (OB_FAIL(init_sys_tenant_privilege(trans))) {
5383LOG_WARN("insert privilege failed", K(tenant_id), K(ret));
5384}
5385//TODO [profile]
5386}
5387if (OB_SUCC(ret) && !is_user_tenant(tenant_id)) {
5388uint64_t user_tenant_id = gen_user_tenant_id(tenant_id);
5389if (OB_FAIL(init_tenant_config(tenant_id, init_configs, trans))) {
5390LOG_WARN("insert tenant config failed", KR(ret), K(tenant_id));
5391} else if (is_meta_tenant(tenant_id)
5392&& OB_FAIL(init_tenant_config(user_tenant_id, init_configs, trans))) {
5393LOG_WARN("insert tenant config failed", KR(ret), K(user_tenant_id));
5394}
5395}
5396
5397if (OB_SUCC(ret) && is_meta_tenant(tenant_id)) {
5398const uint64_t user_tenant_id = gen_user_tenant_id(tenant_id);
5399ObAllTenantInfo tenant_info;
5400if (OB_FAIL(tenant_info.init(user_tenant_id, tenant_role, NORMAL_SWITCHOVER_STATUS, 0,
5401SCN::base_scn(), SCN::base_scn(), SCN::base_scn(), recovery_until_scn))) {
5402LOG_WARN("failed to init tenant info", KR(ret), K(tenant_id), K(tenant_role));
5403} else if (OB_FAIL(ObAllTenantInfoProxy::init_tenant_info(tenant_info, &trans))) {
5404LOG_WARN("failed to init tenant info", KR(ret), K(tenant_info));
5405}
5406}
5407
5408return ret;
5409}
5410
5411int ObDDLOperator::init_tenant_tablegroup(const uint64_t tenant_id,
5412ObMySQLTransaction &trans)
5413{
5414int ret = OB_SUCCESS;
5415int64_t start = ObTimeUtility::current_time();
5416int64_t new_schema_version = OB_INVALID_VERSION;
5417ObSchemaService *schema_service = schema_service_.get_schema_service();
5418
5419if (OB_ISNULL(schema_service)) {
5420ret = OB_ERR_SYS;
5421LOG_ERROR("schema_service must not null");
5422} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
5423LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
5424} else {
5425ObTablegroupSchema tg_schema;
5426tg_schema.set_tenant_id(tenant_id);
5427tg_schema.set_tablegroup_id(OB_SYS_TABLEGROUP_ID);
5428tg_schema.set_tablegroup_name(OB_SYS_TABLEGROUP_NAME);
5429tg_schema.set_comment("system tablegroup");
5430tg_schema.set_schema_version(OB_CORE_SCHEMA_VERSION);
5431tg_schema.set_part_level(PARTITION_LEVEL_ZERO);
5432tg_schema.set_schema_version(new_schema_version);
5433if (OB_FAIL(tg_schema.set_sharding(OB_PARTITION_SHARDING_ADAPTIVE))) {
5434LOG_WARN("set sharding failed", K(ret), K(tg_schema));
5435} else if (OB_FAIL(schema_service->get_tablegroup_sql_service().insert_tablegroup(tg_schema, trans))) {
5436LOG_WARN("insert_tablegroup failed", K(tg_schema), K(ret));
5437}
5438}
5439LOG_INFO("init tenant tablegroup", K(ret), K(tenant_id),
5440"cost", ObTimeUtility::current_time() - start);
5441return ret;
5442}
5443
5444int ObDDLOperator::init_tenant_database(const ObTenantSchema &tenant_schema,
5445const ObString &db_name,
5446const uint64_t pure_db_id,
5447const ObString &db_comment,
5448ObMySQLTransaction &trans,
5449const bool is_oracle_mode)
5450{
5451int ret = OB_SUCCESS;
5452int64_t start = ObTimeUtility::current_time();
5453const uint64_t tenant_id = tenant_schema.get_tenant_id();
5454int64_t new_schema_version = OB_INVALID_VERSION;
5455if (db_name.empty() || OB_INVALID_ID == pure_db_id || db_comment.empty()) {
5456ret = OB_INVALID_ARGUMENT;
5457RS_LOG(WARN, "invalid argument", K(db_name), K(pure_db_id), K(db_comment), K(ret));
5458} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
5459LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
5460} else {
5461ObSchemaService *schema_service = schema_service_.get_schema_service();
5462ObDatabaseSchema db_schema;
5463db_schema.set_tenant_id(tenant_id);
5464db_schema.set_database_id(pure_db_id);
5465db_schema.set_database_name(db_name);
5466db_schema.set_comment(db_comment);
5467db_schema.set_schema_version(new_schema_version);
5468if (db_name == OB_RECYCLEBIN_SCHEMA_NAME
5469|| db_name == OB_PUBLIC_SCHEMA_NAME) {
5470db_schema.set_read_only(true);
5471}
5472
5473if (OB_ISNULL(schema_service)) {
5474ret = OB_ERR_SYS;
5475RS_LOG(ERROR, "schema_service must not null");
5476} else if (OB_FAIL(ObSchema::set_charset_and_collation_options(tenant_schema.get_charset_type(),
5477tenant_schema.get_collation_type(),
5478db_schema))) {
5479RS_LOG(WARN, "set charset and collation options failed", K(ret));
5480} else if (OB_FAIL(schema_service->get_database_sql_service().insert_database(db_schema, trans))) {
5481RS_LOG(WARN, "insert_database failed", K(db_schema), K(ret));
5482}
5483}
5484
5485// init database priv
5486if (OB_SUCC(ret)) {
5487const uint64_t tenant_id = tenant_schema.get_tenant_id();
5488ObOriginalDBKey db_key;
5489db_key.tenant_id_ = tenant_id;
5490db_key.user_id_ = is_oracle_mode ? OB_ORA_SYS_USER_ID : OB_SYS_USER_ID;
5491db_key.db_ = db_name;
5492
5493ObSchemaService *schema_service = schema_service_.get_schema_service();
5494if (OB_ISNULL(schema_service)) {
5495ret = OB_ERR_SYS;
5496RS_LOG(ERROR, "schema_service must not null");
5497} else {
5498ObSqlString ddl_stmt_str;
5499ObString ddl_sql;
5500ObNeedPriv need_priv;
5501need_priv.db_ = db_name;
5502need_priv.priv_set_ = OB_PRIV_DB_ACC;//is collect?
5503need_priv.priv_level_ = OB_PRIV_DB_LEVEL;
5504if (OB_FAIL(ObDDLSqlGenerator::gen_db_priv_sql(ObAccountArg(is_oracle_mode ? OB_ORA_SYS_USER_NAME : OB_SYS_USER_NAME,
5505OB_SYS_HOST_NAME),
5506need_priv,
5507true, /*is_grant*/
5508ddl_stmt_str))) {
5509LOG_WARN("gen db priv sql failed", K(ret));
5510} else if (FALSE_IT(ddl_sql = ddl_stmt_str.string())) {
5511} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
5512LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
5513} else if (OB_FAIL(schema_service->get_priv_sql_service().grant_database(
5514db_key, OB_PRIV_DB_ACC, new_schema_version, &ddl_sql, trans))) {
5515RS_LOG(WARN, "insert database privilege failed, ", K(ret));
5516}
5517}
5518}
5519LOG_INFO("init tenant database", K(ret),
5520"tenant_id", tenant_schema.get_tenant_id(),
5521"database_name", db_name,
5522"cost", ObTimeUtility::current_time() - start);
5523return ret;
5524}
5525
5526int ObDDLOperator::init_tenant_databases(const ObTenantSchema &tenant_schema,
5527const ObSysVariableSchema &sys_variable,
5528ObMySQLTransaction &trans)
5529{
5530int ret = OB_SUCCESS;
5531const uint64_t tenant_id = tenant_schema.get_tenant_id();
5532const bool is_sys = OB_SYS_TENANT_ID == tenant_id;
5533ObString oceanbase_schema(OB_SYS_DATABASE_NAME);
5534ObString mysql_schema(OB_MYSQL_SCHEMA_NAME);
5535ObString information_schema(OB_INFORMATION_SCHEMA_NAME);
5536ObString recyclebin_schema(OB_RECYCLEBIN_SCHEMA_NAME);
5537ObString public_schema(OB_PUBLIC_SCHEMA_NAME);
5538ObString test_schema(OB_TEST_SCHEMA_NAME);
5539ObString ora_sys_schema(OB_ORA_SYS_USER_NAME);
5540ObString ora_lbacsys_schema(OB_ORA_LBACSYS_NAME);
5541ObString ora_auditor_schema(OB_ORA_AUDITOR_NAME);
5542bool is_oracle_mode = false;
5543if (OB_FAIL(sys_variable.get_oracle_mode(is_oracle_mode))) {
5544LOG_WARN("failed to get oracle mode", K(ret));
5545} else if (OB_FAIL(init_tenant_database(tenant_schema, oceanbase_schema,
5546OB_SYS_DATABASE_ID, "system database",
5547trans, is_oracle_mode))) {
5548RS_LOG(WARN, "insert default database failed", K(tenant_id), K(ret));
5549} else if (OB_FAIL(init_tenant_database(tenant_schema, recyclebin_schema,
5550OB_RECYCLEBIN_SCHEMA_ID, "recyclebin schema",
5551trans, is_oracle_mode))) {
5552RS_LOG(WARN, "insert recyclebin schema failed", K(tenant_id), K(ret));
5553} else if (OB_FAIL(init_tenant_database(tenant_schema, public_schema,
5554OB_PUBLIC_SCHEMA_ID, "public schema",
5555trans, is_oracle_mode))) {
5556RS_LOG(WARN, "insert public schema failed", K(tenant_id), K(ret));
5557} else if (OB_FAIL(init_tenant_optimizer_stats_info(sys_variable, tenant_id, trans))) {
5558RS_LOG(WARN, "init tenant tenant optimizer control table", K(tenant_id), K(ret));
5559} else if (OB_FAIL(init_tenant_spm_configure(tenant_id, trans))) {
5560RS_LOG(WARN, "init tenant spm configure failed", K(tenant_id), K(ret));
5561} else {
5562if (!is_oracle_mode) {
5563if (OB_FAIL(init_tenant_database(tenant_schema, mysql_schema,
5564OB_MYSQL_SCHEMA_ID, "MySql schema",
5565trans, false))) {
5566RS_LOG(WARN, "insert information_schema failed", K(tenant_id), K(ret));
5567} else if (OB_FAIL(init_tenant_database(tenant_schema, information_schema,
5568OB_INFORMATION_SCHEMA_ID, "information_schema",
5569trans, false))) {
5570RS_LOG(WARN, "insert mysql schema failed", K(tenant_id), K(ret));
5571} else if (OB_FAIL(init_tenant_database(tenant_schema, test_schema,
5572OB_INITIAL_TEST_DATABASE_ID, "test schema",
5573trans, is_oracle_mode))) {
5574RS_LOG(WARN, "insert test schema failed", K(tenant_id), K(ret));
5575}
5576}
5577if (OB_FAIL(ret)) {
5578// do nothing
5579} else if (is_oracle_mode || is_sys) {
5580if (OB_FAIL(init_tenant_database(tenant_schema, ora_sys_schema,
5581OB_ORA_SYS_DATABASE_ID, "oracle sys schema", trans, is_oracle_mode))) {
5582RS_LOG(WARN, "insert oracle sys schema failed", K(ret), K(tenant_id));
5583} else if (OB_FAIL(init_tenant_database(tenant_schema, ora_lbacsys_schema,
5584OB_ORA_LBACSYS_DATABASE_ID, "oracle sys schema", trans, is_oracle_mode))) {
5585RS_LOG(WARN, "insert oracle lbacsys schema failed", K(ret), K(tenant_id));
5586} else if (OB_FAIL(init_tenant_database(tenant_schema, ora_auditor_schema,
5587OB_ORA_AUDITOR_DATABASE_ID, "oracle sys schema", trans, is_oracle_mode))) {
5588RS_LOG(WARN, "insert oracle lbacsys schema failed", K(ret), K(tenant_id));
5589}
5590}
5591}
5592
5593return ret;
5594}
5595
5596int ObDDLOperator::init_tenant_optimizer_stats_info(const ObSysVariableSchema &sys_variable,
5597uint64_t tenant_id,
5598ObMySQLTransaction &trans)
5599{
5600int ret = OB_SUCCESS;
5601ObSqlString prefs_sql;
5602ObSqlString jobs_sql;
5603const uint64_t exec_tenant_id = ObSchemaUtils::get_exec_tenant_id(tenant_id);
5604int64_t expected_affected_rows1 = 0;
5605int64_t expected_affected_rows2 = 0;
5606int64_t affected_rows1 = 0;
5607int64_t affected_rows2 = 0;
5608if (OB_FAIL(ObDbmsStatsPreferences::gen_init_global_prefs_sql(prefs_sql,
5609false,
5610&expected_affected_rows1))) {
5611LOG_WARN("failed gen init global prefs sql", K(ret), K(prefs_sql));
5612} else if (OB_FAIL(ObDbmsStatsMaintenanceWindow::get_stats_maintenance_window_jobs_sql(
5613sys_variable,
5614tenant_id,
5615jobs_sql,
5616expected_affected_rows2))) {
5617LOG_WARN("failed tto get stats maintenance window jobs sql", K(ret), K(jobs_sql));
5618} else if (OB_UNLIKELY(prefs_sql.empty() || jobs_sql.empty())) {
5619ret = OB_ERR_UNEXPECTED;
5620RS_LOG(WARN, "get unexpected empty", K(ret), K(prefs_sql), K(jobs_sql));
5621} else if (OB_FAIL(trans.write(exec_tenant_id, prefs_sql.ptr(), affected_rows1)) ||
5622OB_FAIL(trans.write(exec_tenant_id, jobs_sql.ptr(), affected_rows2))) {
5623RS_LOG(WARN, "execute sql failed", K(ret), K(prefs_sql), K(jobs_sql));
5624} else if (OB_UNLIKELY(affected_rows1 != expected_affected_rows1 ||
5625affected_rows2 != expected_affected_rows2)) {
5626ret = OB_ERR_UNEXPECTED;
5627RS_LOG(WARN, "get unexpected affected_rows", K(ret), K(affected_rows1), K(affected_rows2),
5628K(expected_affected_rows1), K(expected_affected_rows2));
5629} else {/*do nothing*/}
5630return ret;
5631}
5632
5633int ObDDLOperator::init_tenant_spm_configure(uint64_t tenant_id,
5634ObMySQLTransaction &trans)
5635{
5636int ret = OB_SUCCESS;
5637const uint64_t exec_tenant_id = ObSchemaUtils::get_exec_tenant_id(tenant_id);
5638const uint64_t extract_tenant_id = ObSchemaUtils::get_extract_tenant_id(exec_tenant_id, tenant_id);
5639int64_t affected_rows = 0;
5640ObSqlString sql;
5641if (OB_FAIL(sql.assign_fmt("INSERT INTO %s (tenant_id, name, value) VALUES ",
5642OB_ALL_SPM_CONFIG_TNAME))) {
5643RS_LOG(WARN, "sql assign failed", K(ret));
5644} else if (OB_FAIL(sql.append_fmt("(%lu, \"%s\", NULL),", extract_tenant_id, "AUTO_CAPTURE_ACTION"))) {
5645RS_LOG(WARN, "sql append failed", K(ret));
5646} else if (OB_FAIL(sql.append_fmt("(%lu, \"%s\", NULL),", extract_tenant_id, "AUTO_CAPTURE_MODULE"))) {
5647RS_LOG(WARN, "sql append failed", K(ret));
5648} else if (OB_FAIL(sql.append_fmt("(%lu, \"%s\", NULL),", extract_tenant_id, "AUTO_CAPTURE_PARSING_SCHEMA_NAME"))) {
5649RS_LOG(WARN, "sql append failed", K(ret));
5650} else if (OB_FAIL(sql.append_fmt("(%lu, \"%s\", NULL),", extract_tenant_id, "AUTO_CAPTURE_SQL_TEXT"))) {
5651RS_LOG(WARN, "sql append failed", K(ret));
5652} else if (OB_FAIL(sql.append_fmt("(%lu, \"%s\", \"%s\"),", extract_tenant_id, "AUTO_SPM_EVOLVE_TASK", "OFF"))) {
5653RS_LOG(WARN, "sql append failed", K(ret));
5654} else if (OB_FAIL(sql.append_fmt("(%lu, \"%s\", \"%s\"),", extract_tenant_id, "AUTO_SPM_EVOLVE_TASK_INTERVAL", "3600"))) {
5655RS_LOG(WARN, "sql append failed", K(ret));
5656} else if (OB_FAIL(sql.append_fmt("(%lu, \"%s\", \"%s\"),", extract_tenant_id, "AUTO_SPM_EVOLVE_TASK_MAX_RUNTIME", "1800"))) {
5657RS_LOG(WARN, "sql append failed", K(ret));
5658} else if (OB_FAIL(sql.append_fmt("(%lu, \"%s\", \"%s\"),", extract_tenant_id, "SPACE_BUDGET_PERCENT", "10"))) {
5659RS_LOG(WARN, "sql append failed", K(ret));
5660} else if (OB_FAIL(sql.append_fmt("(%lu, \"%s\", \"%s\");", extract_tenant_id, "PLAN_RETENTION_WEEKS", "53"))) {
5661RS_LOG(WARN, "sql append failed", K(ret));
5662} else if (OB_FAIL(trans.write(exec_tenant_id, sql.ptr(), affected_rows))) {
5663RS_LOG(WARN, "execute sql failed", K(ret), K(sql));
5664} else {/*do nothing*/}
5665return ret;
5666}
5667
5668/*
5669* The following system permissions are not granted to dba and need to be extracted from the complete set of permissions
5670-----------------------------------------
5671ADMINISTER KEY MANAGEMENT
5672ALTER DATABASE LINK
5673ALTER PUBLIC DATABASE LINK
5674EXEMPT ACCESS POLICY
5675EXEMPT IDENTITY POLICY
5676EXEMPT REDACTION POLICY
5677INHERIT ANY PRIVILEGES
5678INHERIT ANY REMOTE PRIVILEGES
5679KEEP DATE TIME
5680KEEP SYSGUID
5681PURGE DBA_RECYCLEBIN
5682SYSBACKUP
5683SYSDBA
5684SYSDG
5685SYSKM
5686SYSOPER
5687SYSRAC
5688TRANSLATE ANY SQL
5689UNLIMITED TABLESPACE
5690------------------------------------------
5691resource role, pre define sys priv;
5692RESOURCE CREATE TABLE NO YES YES
5693RESOURCE CREATE OPERATOR NO YES YES
5694RESOURCE CREATE TYPE NO YES YES
5695RESOURCE CREATE CLUSTER NO YES YES
5696RESOURCE CREATE TRIGGER NO YES YES
5697RESOURCE CREATE INDEXTYPE NO YES YES
5698RESOURCE CREATE PROCEDURE NO YES YES
5699RESOURCE CREATE SEQUENCE NO YES YES*/
5700int ObDDLOperator::build_raw_priv_info_inner_user(
5701uint64_t grantee_id,
5702ObRawPrivArray &raw_priv_array,
5703uint64_t &option)
5704{
5705int ret = OB_SUCCESS;
5706if (is_ora_dba_role(grantee_id) || is_ora_sys_user(grantee_id)) {
5707option = ADMIN_OPTION;
5708for (int i = PRIV_ID_NONE + 1; i < PRIV_ID_MAX && OB_SUCC(ret); i++) {
5709if (i != PRIV_ID_EXEMPT_RED_PLY
5710&& i != PRIV_ID_SYSDBA
5711&& i != PRIV_ID_SYSOPER
5712&& i != PRIV_ID_SYSBACKUP
5713&& i != PRIV_ID_EXEMPT_ACCESS_POLICY) {
5714OZ (raw_priv_array.push_back(i));
5715}
5716}
5717} else if (is_ora_resource_role(grantee_id)) {
5718option = NO_OPTION;
5719OZ (raw_priv_array.push_back(PRIV_ID_CREATE_TABLE));
5720OZ (raw_priv_array.push_back(PRIV_ID_CREATE_TYPE));
5721OZ (raw_priv_array.push_back(PRIV_ID_CREATE_TRIG));
5722OZ (raw_priv_array.push_back(PRIV_ID_CREATE_PROC));
5723OZ (raw_priv_array.push_back(PRIV_ID_CREATE_SEQ));
5724} else if (is_ora_connect_role(grantee_id)) {
5725option = NO_OPTION;
5726OZ (raw_priv_array.push_back(PRIV_ID_CREATE_SESSION));
5727} else if (is_ora_public_role(grantee_id)) {
5728} else {
5729ret = OB_INVALID_ARGUMENT;
5730LOG_WARN("grantee_id error", K(ret), K(grantee_id));
5731}
5732return ret;
5733}
5734
5735int ObDDLOperator::init_inner_user_privs(
5736const uint64_t tenant_id,
5737ObUserInfo &user,
5738ObMySQLTransaction &trans,
5739const bool is_oracle_mode)
5740{
5741int ret = OB_SUCCESS;
5742int64_t new_schema_version = OB_INVALID_VERSION;
5743uint64_t grantee_id = user.get_user_id();
5744uint64_t option = NO_OPTION;
5745ObRawPrivArray raw_priv_array;
5746ObSchemaService *schema_service = schema_service_.get_schema_service();
5747
5748if (OB_ISNULL(schema_service)) {
5749ret = OB_ERR_SYS;
5750LOG_ERROR("schema_service must not null", K(ret));
5751}
5752if (OB_SUCC(ret) && is_oracle_mode) {
5753ObString empty_str;
5754OZ (build_raw_priv_info_inner_user(grantee_id, raw_priv_array, option), grantee_id);
5755OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version), tenant_id);
5756OZ (schema_service->get_priv_sql_service().grant_sys_priv_to_ur(tenant_id,
5757grantee_id,
5758option,
5759raw_priv_array,
5760new_schema_version,
5761&empty_str,
5762trans,
5763true /*is_grant*/,
5764false),
5765tenant_id, grantee_id, ADMIN_OPTION, raw_priv_array);
5766}
5767return ret;
5768}
5769
5770int ObDDLOperator::init_tenant_user(const uint64_t tenant_id,
5771const ObString &user_name,
5772const ObString &pwd_raw,
5773const uint64_t pure_user_id,
5774const ObString &user_comment,
5775ObMySQLTransaction &trans,
5776const bool set_locked,
5777const bool is_user,
5778const bool is_oracle_mode)
5779{
5780int ret = OB_SUCCESS;
5781ObString pwd_enc;
5782char enc_buf[ENC_BUF_LEN] = {0};
5783int64_t new_schema_version = OB_INVALID_VERSION;
5784ObSchemaService *schema_service = schema_service_.get_schema_service();
5785ObUserInfo user;
5786user.set_tenant_id(tenant_id);
5787pwd_enc.assign_ptr(enc_buf, ENC_BUF_LEN);
5788if (OB_ISNULL(schema_service)) {
5789ret = OB_ERR_SYS;
5790LOG_ERROR("schema_service must not null");
5791} else if (pwd_raw.length() > 0
5792&& OB_FAIL(ObEncryptedHelper::encrypt_passwd_to_stage2(pwd_raw, pwd_enc))) {
5793LOG_WARN("Encrypt password failed", K(ret), K(pwd_raw));
5794} else if (OB_FAIL(user.set_user_name(user_name))) {
5795LOG_WARN("set user name failed", K(ret));
5796} else if (OB_FAIL(user.set_host(OB_SYS_HOST_NAME))) {
5797LOG_WARN("set host name failed", K(ret));
5798} else if (OB_FAIL(user.set_passwd(pwd_enc))) {
5799LOG_WARN("set user password failed", K(ret));
5800} else if (OB_FAIL(user.set_info(user_comment))) {
5801LOG_WARN("set user info failed", K(ret));
5802} else {
5803user.set_is_locked(set_locked);
5804user.set_user_id(pure_user_id);
5805if ((!is_oracle_mode || is_user) &&
5806pure_user_id != OB_ORA_LBACSYS_USER_ID &&
5807pure_user_id != OB_ORA_AUDITOR_USER_ID) {
5808user.set_priv_set(OB_PRIV_ALL | OB_PRIV_GRANT);
5809}
5810user.set_schema_version(OB_CORE_SCHEMA_VERSION);
5811user.set_profile_id(OB_INVALID_ID);
5812user.set_type((is_user) ? OB_USER : OB_ROLE);
5813}
5814if (OB_SUCC(ret)) {
5815ObSqlString ddl_stmt_str;
5816ObString ddl_sql;
5817if (OB_FAIL(ObDDLSqlGenerator::gen_create_user_sql(ObAccountArg(user.get_user_name_str(),
5818user.get_host_name_str(),
5819user.is_role()),
5820user.get_passwd_str(),
5821ddl_stmt_str))) {
5822LOG_WARN("gen create user sql failed", K(user), K(ret));
5823} else if (FALSE_IT(ddl_sql = ddl_stmt_str.string())) {
5824} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
5825LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
5826} else if (OB_FAIL(schema_service->get_user_sql_service().create_user(
5827user, new_schema_version, &ddl_sql, trans))) {
5828LOG_WARN("insert user failed", K(user), K(ret));
5829} else if ((!is_user || is_ora_sys_user(user.get_user_id()))
5830&& OB_FAIL(init_inner_user_privs(tenant_id, user, trans, is_oracle_mode))) {
5831LOG_WARN("init user privs failed", K(user), K(ret));
5832}
5833}
5834return ret;
5835}
5836
5837int ObDDLOperator::init_tenant_users(const ObTenantSchema &tenant_schema,
5838const ObSysVariableSchema &sys_variable,
5839ObMySQLTransaction &trans)
5840{
5841int ret = OB_SUCCESS;
5842const uint64_t tenant_id = tenant_schema.get_tenant_id();
5843ObString sys_user_name(OB_SYS_USER_NAME);
5844ObString ora_sys_user_name(OB_ORA_SYS_USER_NAME);
5845ObString ora_lbacsys_user_name(OB_ORA_LBACSYS_NAME);
5846ObString ora_auditor_user_name(OB_ORA_AUDITOR_NAME);
5847ObString ora_connect_role_name(OB_ORA_CONNECT_ROLE_NAME);
5848ObString ora_resource_role_name(OB_ORA_RESOURCE_ROLE_NAME);
5849ObString ora_dba_role_name(OB_ORA_DBA_ROLE_NAME);
5850ObString ora_public_role_name(OB_ORA_PUBLIC_ROLE_NAME);
5851ObString sys_standby_name(OB_STANDBY_USER_NAME);
5852char ora_lbacsys_password[ENCRYPT_KEY_LENGTH];
5853char ora_auditor_password[ENCRYPT_KEY_LENGTH];
5854bool is_oracle_mode = false;
5855if (OB_FAIL(sys_variable.get_oracle_mode(is_oracle_mode))) {
5856RS_LOG(WARN, "failed to get oracle mode", K(ret), K(tenant_id));
5857} else if (is_oracle_mode) {
5858// Only retain the three users that is SYS/LBACSYS/ORAAUDITOR in Oracle mode.
5859if (OB_FAIL(share::ObKeyGenerator::generate_encrypt_key(ora_lbacsys_password, ENCRYPT_KEY_LENGTH))) {
5860RS_LOG(WARN, "failed to generate lbacsys's password", K(ret), K(tenant_id));
5861} else if (OB_FAIL(share::ObKeyGenerator::generate_encrypt_key(ora_auditor_password, ENCRYPT_KEY_LENGTH))) {
5862RS_LOG(WARN, "failed to generate auditor's password", K(ret), K(tenant_id));
5863} else if (OB_FAIL(init_tenant_user(tenant_id, ora_sys_user_name, ObString(""),
5864OB_ORA_SYS_USER_ID, "oracle system administrator", trans, false, true, true))) {
5865RS_LOG(WARN, "failed to init oracle sys user", K(ret), K(tenant_id));
5866} else if (OB_FAIL(init_tenant_user(tenant_id, ora_lbacsys_user_name,
5867ObString(ENCRYPT_KEY_LENGTH, ora_lbacsys_password),
5868OB_ORA_LBACSYS_USER_ID, "oracle system administrator", trans, true, true, true))) {
5869RS_LOG(WARN, "failed to init oracle sys user", K(ret), K(tenant_id));
5870} else if (OB_FAIL(init_tenant_user(tenant_id, ora_auditor_user_name,
5871ObString(ENCRYPT_KEY_LENGTH, ora_auditor_password),
5872OB_ORA_AUDITOR_USER_ID, "oracle system administrator", trans, true, true, true))) {
5873RS_LOG(WARN, "failed to init oracle sys user", K(ret), K(tenant_id));
5874} else if (OB_FAIL(init_tenant_user(tenant_id, ora_connect_role_name, ObString(""),
5875OB_ORA_CONNECT_ROLE_ID, "oracle connect role", trans, false, false, true))) {
5876RS_LOG(WARN, "fail to init oracle connect role", K(ret), K(tenant_id));
5877} else if (OB_FAIL(init_tenant_user(tenant_id, ora_resource_role_name, ObString(""),
5878OB_ORA_RESOURCE_ROLE_ID, "oracle resource role", trans, false, false, true))) {
5879RS_LOG(WARN, "fail to init oracle resource role", K(ret), K(tenant_id));
5880} else if (OB_FAIL(init_tenant_user(tenant_id, ora_dba_role_name, ObString(""),
5881OB_ORA_DBA_ROLE_ID, "oracle dba role", trans, false, false, true))) {
5882RS_LOG(WARN, "fail to init oracle dba role", K(ret), K(tenant_id));
5883} else if (OB_FAIL(init_tenant_user(tenant_id, ora_public_role_name, ObString(""),
5884OB_ORA_PUBLIC_ROLE_ID, "oracle public role", trans, false, false, true))) {
5885RS_LOG(WARN, "fail to init oracle public role", K(ret), K(tenant_id));
5886}
5887} else {
5888if (OB_FAIL(init_tenant_user(tenant_id, sys_user_name, ObString(""), OB_SYS_USER_ID,
5889"system administrator", trans))) {
5890RS_LOG(WARN, "failed to init sys user", K(ret), K(tenant_id));
5891}
5892#ifdef OB_BUILD_TDE_SECURITY
5893if (OB_SUCC(ret)) {
5894if (OB_FAIL(share::ObKeyGenerator::generate_encrypt_key(ora_auditor_password,
5895ENCRYPT_KEY_LENGTH))) {
5896RS_LOG(WARN, "failed to generate auditor's password", K(ret), K(tenant_id));
5897} else if (OB_FAIL(init_tenant_user(tenant_id, ora_auditor_user_name,
5898ObString(ENCRYPT_KEY_LENGTH, ora_auditor_password),
5899OB_ORA_AUDITOR_USER_ID, "system administrator", trans, true))) {
5900RS_LOG(WARN, "failed to init mysql audit user", K(ret), K(tenant_id));
5901}
5902}
5903#endif
5904}
5905
5906//TODO in standby cluster, temp logical, will be deleted after inner sql ready
5907// if (OB_SUCC(ret) && is_sys_tenant(tenant_id)) {
5908// const uint64_t user_id = 100;
5909// if (OB_FAIL(init_tenant_user(tenant_id, sys_standby_name, "", user_id,
5910// "system administrator", trans))) {
5911// RS_LOG(WARN, "failed to init sys user", K(ret), K(tenant_id));
5912// }
5913// }
5914return ret;
5915}
5916
5917int ObDDLOperator::init_tenant_config(
5918const uint64_t tenant_id,
5919const common::ObIArray<common::ObConfigPairs> &init_configs,
5920ObMySQLTransaction &trans)
5921{
5922int ret = OB_SUCCESS;
5923int64_t tenant_idx = !is_user_tenant(tenant_id) ? 0 : 1;
5924if (is_user_tenant(tenant_id) && init_configs.count() == 1) {
5925ret = OB_SUCCESS;
5926LOG_WARN("no user config", KR(ret), K(tenant_idx), K(tenant_id), K(init_configs));
5927} else if (OB_UNLIKELY(
5928init_configs.count() < tenant_idx + 1
5929|| tenant_id != init_configs.at(tenant_idx).get_tenant_id())) {
5930ret = OB_INVALID_ARGUMENT;
5931LOG_WARN("invalid init_configs", KR(ret), K(tenant_idx), K(tenant_id), K(init_configs));
5932} else if (OB_FAIL(init_tenant_config_(tenant_id, init_configs.at(tenant_idx), trans))) {
5933LOG_WARN("fail to init tenant config", KR(ret), K(tenant_id));
5934} else if (OB_FAIL(init_tenant_config_from_seed_(tenant_id, trans))) {
5935LOG_WARN("fail to init tenant config from seed", KR(ret), K(tenant_id));
5936}
5937return ret;
5938}
5939
5940int ObDDLOperator::init_tenant_config_(
5941const uint64_t tenant_id,
5942const common::ObConfigPairs &tenant_config,
5943ObMySQLTransaction &trans)
5944{
5945int ret = OB_SUCCESS;
5946omt::ObTenantConfigGuard hard_code_config(TENANT_CONF(OB_SYS_TENANT_ID));
5947int64_t config_cnt = tenant_config.get_configs().count();
5948if (OB_UNLIKELY(tenant_id != tenant_config.get_tenant_id() || config_cnt <= 0)) {
5949ret = OB_INVALID_ARGUMENT;
5950LOG_WARN("invalid tenant config", KR(ret), K(tenant_id), K(tenant_config));
5951} else if (!hard_code_config.is_valid()) {
5952ret = OB_ERR_UNEXPECTED;
5953LOG_WARN("failed to get hard code config", KR(ret), K(tenant_id));
5954} else {
5955ObDMLSqlSplicer dml;
5956ObConfigItem *item = NULL;
5957char svr_ip[OB_MAX_SERVER_ADDR_SIZE] = "ANY";
5958int64_t svr_port = 0;
5959int64_t config_version = omt::ObTenantConfig::INITIAL_TENANT_CONF_VERSION + 1;
5960FOREACH_X(config, tenant_config.get_configs(), OB_SUCC(ret)) {
5961const ObConfigStringKey key(config->key_.ptr());
5962if (OB_ISNULL(hard_code_config->get_container().get(key))
5963|| OB_ISNULL(item = *(hard_code_config->get_container().get(key)))) {
5964ret = OB_ENTRY_NOT_EXIST;
5965LOG_WARN("config not exist", KR(ret), KPC(config));
5966} else if (OB_FAIL(dml.add_pk_column("tenant_id", tenant_id))
5967|| OB_FAIL(dml.add_pk_column("zone", ""))
5968|| OB_FAIL(dml.add_pk_column("svr_type", print_server_role(OB_SERVER)))
5969|| OB_FAIL(dml.add_pk_column(K(svr_ip)))
5970|| OB_FAIL(dml.add_pk_column(K(svr_port)))
5971|| OB_FAIL(dml.add_pk_column("name", config->key_.ptr()))
5972|| OB_FAIL(dml.add_column("data_type", item->data_type()))
5973|| OB_FAIL(dml.add_column("value", config->value_.ptr()))
5974|| OB_FAIL(dml.add_column("info", ""))
5975|| OB_FAIL(dml.add_column("config_version", config_version))
5976|| OB_FAIL(dml.add_column("section", item->section()))
5977|| OB_FAIL(dml.add_column("scope", item->scope()))
5978|| OB_FAIL(dml.add_column("source", item->source()))
5979|| OB_FAIL(dml.add_column("edit_level", item->edit_level()))) {
5980LOG_WARN("fail to add column", KR(ret), K(tenant_id), KPC(config));
5981} else if (OB_FAIL(dml.finish_row())) {
5982LOG_WARN("fail to finish row", KR(ret), K(tenant_id), KPC(config));
5983}
5984} // end foreach
5985ObSqlString sql;
5986int64_t affected_rows = 0;
5987const uint64_t exec_tenant_id = gen_meta_tenant_id(tenant_id);
5988if (FAILEDx(dml.splice_batch_insert_sql(OB_TENANT_PARAMETER_TNAME, sql))) {
5989LOG_WARN("fail to generate sql", KR(ret), K(tenant_id));
5990} else if (OB_FAIL(trans.write(exec_tenant_id, sql.ptr(), affected_rows))) {
5991LOG_WARN("fail to execute sql", KR(ret), K(tenant_id), K(exec_tenant_id), K(sql));
5992} else if (config_cnt != affected_rows) {
5993ret = OB_ERR_UNEXPECTED;
5994LOG_WARN("affected_rows not match", KR(ret), K(tenant_id), K(config_cnt), K(affected_rows));
5995}
5996}
5997return ret;
5998}
5999
6000int ObDDLOperator::init_tenant_config_from_seed_(
6001const uint64_t tenant_id,
6002ObMySQLTransaction &trans)
6003{
6004int ret = OB_SUCCESS;
6005int64_t start = ObTimeUtility::current_time();
6006ObSqlString sql;
6007const static char *from_seed = "select config_version, zone, svr_type, svr_ip, svr_port, name, "
6008"data_type, value, info, section, scope, source, edit_level "
6009"from __all_seed_parameter";
6010ObSQLClientRetryWeak sql_client_retry_weak(&sql_proxy_);
6011SMART_VAR(ObMySQLProxy::MySQLResult, result) {
6012int64_t expected_rows = 0;
6013int64_t config_version = omt::ObTenantConfig::INITIAL_TENANT_CONF_VERSION + 1;
6014bool is_first = true;
6015if (OB_FAIL(sql_client_retry_weak.read(result, OB_SYS_TENANT_ID, from_seed))) {
6016LOG_WARN("read config from __all_seed_parameter failed", K(from_seed), K(ret));
6017} else {
6018sql.reset();
6019if (OB_FAIL(sql.assign_fmt("INSERT IGNORE INTO %s "
6020"(TENANT_ID, ZONE, SVR_TYPE, SVR_IP, SVR_PORT, NAME, DATA_TYPE, VALUE, INFO, "
6021"SECTION, SCOPE, SOURCE, EDIT_LEVEL, CONFIG_VERSION) VALUES",
6022OB_TENANT_PARAMETER_TNAME))) {
6023LOG_WARN("sql assign failed", K(ret));
6024}
6025
6026while (OB_SUCC(ret) && OB_SUCC(result.get_result()->next())) {
6027common::sqlclient::ObMySQLResult *rs = result.get_result();
6028if (OB_ISNULL(rs)) {
6029ret = OB_ERR_UNEXPECTED;
6030LOG_WARN("system config result is null", K(ret));
6031} else {
6032ObString var_zone, var_svr_type, var_svr_ip, var_name, var_data_type;
6033ObString var_value, var_info, var_section, var_scope, var_source, var_edit_level;
6034int64_t var_svr_port = 0;
6035EXTRACT_VARCHAR_FIELD_MYSQL(*rs, "zone", var_zone);
6036EXTRACT_VARCHAR_FIELD_MYSQL(*rs, "svr_type", var_svr_type);
6037EXTRACT_VARCHAR_FIELD_MYSQL(*rs, "svr_ip", var_svr_ip);
6038EXTRACT_VARCHAR_FIELD_MYSQL(*rs, "name", var_name);
6039EXTRACT_VARCHAR_FIELD_MYSQL(*rs, "data_type", var_data_type);
6040EXTRACT_VARCHAR_FIELD_MYSQL(*rs, "value", var_value);
6041EXTRACT_VARCHAR_FIELD_MYSQL(*rs, "info", var_info);
6042EXTRACT_VARCHAR_FIELD_MYSQL(*rs, "section", var_section);
6043EXTRACT_VARCHAR_FIELD_MYSQL(*rs, "scope", var_scope);
6044EXTRACT_VARCHAR_FIELD_MYSQL(*rs, "source", var_source);
6045EXTRACT_VARCHAR_FIELD_MYSQL(*rs, "edit_level", var_edit_level);
6046EXTRACT_INT_FIELD_MYSQL(*rs, "svr_port", var_svr_port, int64_t);
6047if (FAILEDx(sql.append_fmt("%s('%lu', '%.*s', '%.*s', '%.*s', %ld, '%.*s', '%.*s', '%.*s',"
6048"'%.*s', '%.*s', '%.*s', '%.*s', '%.*s', %ld)",
6049is_first ? " " : ", ",
6050tenant_id,
6051var_zone.length(), var_zone.ptr(),
6052var_svr_type.length(), var_svr_type.ptr(),
6053var_svr_ip.length(), var_svr_ip.ptr(), var_svr_port,
6054var_name.length(), var_name.ptr(),
6055var_data_type.length(), var_data_type.ptr(),
6056var_value.length(), var_value.ptr(),
6057var_info.length(), var_info.ptr(),
6058var_section.length(), var_section.ptr(),
6059var_scope.length(), var_scope.ptr(),
6060var_source.length(), var_source.ptr(),
6061var_edit_level.length(), var_edit_level.ptr(), config_version))) {
6062LOG_WARN("sql append failed", K(ret));
6063}
6064}
6065expected_rows++;
6066is_first = false;
6067} // while
6068
6069if (OB_ITER_END == ret) {
6070ret = OB_SUCCESS;
6071uint64_t exec_tenant_id = gen_meta_tenant_id(tenant_id);
6072if (expected_rows > 0) {
6073int64_t affected_rows = 0;
6074if (OB_FAIL(trans.write(exec_tenant_id, sql.ptr(), affected_rows))) {
6075LOG_WARN("execute sql failed", KR(ret), K(tenant_id), K(exec_tenant_id), K(sql));
6076} else if (OB_UNLIKELY(affected_rows < 0
6077|| expected_rows < affected_rows)) {
6078ret = OB_ERR_UNEXPECTED;
6079LOG_WARN("unexpected affected_rows", KR(ret), K(expected_rows), K(affected_rows));
6080}
6081}
6082} else {
6083LOG_WARN("failed to get result from result set", K(ret));
6084}
6085} // else
6086LOG_INFO("init tenant config", K(ret), K(tenant_id),
6087"cost", ObTimeUtility::current_time() - start);
6088}
6089return ret;
6090
6091}
6092
6093int ObDDLOperator::init_freeze_info(const uint64_t tenant_id,
6094ObMySQLTransaction &trans)
6095{
6096int ret = OB_SUCCESS;
6097int64_t start = ObTimeUtility::current_time();
6098ObFreezeInfoProxy freeze_info_proxy(tenant_id);
6099ObFreezeInfo frozen_status;
6100frozen_status.set_initial_value(DATA_CURRENT_VERSION);
6101// init freeze_info in __all_freeze_info
6102if (OB_FAIL(freeze_info_proxy.set_freeze_info(trans, frozen_status))) {
6103LOG_WARN("fail to set freeze info", KR(ret), K(frozen_status), K(tenant_id));
6104}
6105
6106LOG_INFO("init freeze info", K(ret), K(tenant_id),
6107"cost", ObTimeUtility::current_time() - start);
6108return ret;
6109}
6110
6111int ObDDLOperator::init_tenant_srs(const uint64_t tenant_id,
6112ObMySQLTransaction &trans)
6113{
6114// todo : import srs_id 0 in srs mgr init
6115int ret = OB_SUCCESS;
6116ObSqlString sql;
6117int64_t start = ObTimeUtility::current_time();
6118int64_t expected_rows = 1;
6119uint64_t tenant_data_version = 0;
6120if (OB_FAIL(GET_MIN_DATA_VERSION(tenant_id, tenant_data_version))) {
6121LOG_WARN("get tenant data version failed", K(ret));
6122} else if (tenant_data_version < DATA_VERSION_4_1_0_0) {
6123ret = OB_NOT_SUPPORTED;
6124LOG_USER_ERROR(OB_NOT_SUPPORTED, "tenant version is less than 4.1, spatial reference system");
6125} else {
6126if (OB_FAIL(sql.assign_fmt("INSERT INTO %s "
6127"(SRS_VERSION, SRS_ID, SRS_NAME, ORGANIZATION, ORGANIZATION_COORDSYS_ID, DEFINITION, minX, maxX, minY, maxY, proj4text, DESCRIPTION) VALUES"
6128R"((1, 0, '', NULL, NULL, '', -2147483648,2147483647,-2147483648,2147483647,'', NULL))",
6129OB_ALL_SPATIAL_REFERENCE_SYSTEMS_TNAME))) {
6130LOG_WARN("sql assign failed", K(ret));
6131}
6132
6133if (OB_SUCC(ret)) {
6134int64_t affected_rows = 0;
6135if (OB_FAIL(trans.write(tenant_id, sql.ptr(), affected_rows))) {
6136LOG_WARN("execute sql failed", K(ret), K(sql));
6137} else if (expected_rows != affected_rows) {
6138ret = OB_ERR_UNEXPECTED;
6139LOG_WARN("unexpected affected_rows", K(expected_rows), K(affected_rows));
6140}
6141}
6142}
6143
6144LOG_INFO("init tenant srs", K(ret), K(tenant_id),
6145"cost", ObTimeUtility::current_time() - start);
6146return ret;
6147}
6148
6149int ObDDLOperator::init_tenant_sys_stats(const uint64_t tenant_id,
6150ObMySQLTransaction &trans)
6151{
6152int ret = OB_SUCCESS;
6153int64_t start = ObTimeUtility::current_time();
6154ObSysStat sys_stat;
6155if (OB_FAIL(sys_stat.set_initial_values(tenant_id))) {
6156LOG_WARN("set initial values failed", K(ret));
6157} else if (sys_stat.item_list_.is_empty()) {
6158ret = OB_INVALID_ARGUMENT;
6159LOG_WARN("not system stat item", KR(ret), K(tenant_id));
6160} else if (OB_FAIL(replace_sys_stat(tenant_id, sys_stat, trans))) {
6161LOG_WARN("replace system stat failed", K(ret));
6162}
6163LOG_INFO("init sys stat", K(ret), K(tenant_id),
6164"cost", ObTimeUtility::current_time() - start);
6165return ret;
6166}
6167
6168int ObDDLOperator::replace_sys_stat(const uint64_t tenant_id,
6169ObSysStat &sys_stat,
6170ObISQLClient &trans)
6171{
6172int ret = OB_SUCCESS;
6173ObSqlString sql;
6174const uint64_t exec_tenant_id = ObSchemaUtils::get_exec_tenant_id(tenant_id);
6175if (sys_stat.item_list_.is_empty()) {
6176// skip
6177} else if (OB_FAIL(sql.assign_fmt("INSERT INTO %s "
6178"(TENANT_ID, ZONE, NAME, DATA_TYPE, VALUE, INFO, gmt_modified) VALUES ",
6179OB_ALL_SYS_STAT_TNAME))) {
6180LOG_WARN("sql append failed", K(ret));
6181} else {
6182DLIST_FOREACH_X(it, sys_stat.item_list_, OB_SUCC(ret)) {
6183if (OB_ISNULL(it)) {
6184ret = OB_ERR_UNEXPECTED;
6185LOG_WARN("it is null", K(ret));
6186} else {
6187char buf[2L<<10] = "";
6188int64_t pos = 0;
6189if (OB_FAIL(it->value_.print_sql_literal(
6190buf, sizeof(buf), pos))) {
6191LOG_WARN("print obj failed", K(ret), "obj", it->value_);
6192} else {
6193ObString value(pos, buf);
6194uint64_t schema_id = OB_INVALID_ID;
6195if (OB_FAIL(ObMaxIdFetcher::str_to_uint(value, schema_id))) {
6196LOG_WARN("fail to convert str to uint", K(ret), K(value));
6197} else if (FALSE_IT(schema_id = ObSchemaUtils::get_extract_schema_id(exec_tenant_id, schema_id))) {
6198} else if (OB_FAIL(sql.append_fmt("%s(%lu, '', '%s', %d, '%ld', '%s', now())",
6199(it == sys_stat.item_list_.get_first()) ? "" : ", ",
6200ObSchemaUtils::get_extract_tenant_id(exec_tenant_id, tenant_id),
6201it->name_, it->value_.get_type(),
6202static_cast<int64_t>(schema_id),
6203it->info_))) {
6204LOG_WARN("sql append failed", K(ret));
6205}
6206}
6207}
6208}
6209if (OB_SUCC(ret)) {
6210LOG_INFO("create system stat sql", K(sql));
6211int64_t affected_rows = 0;
6212if (OB_FAIL(trans.write(exec_tenant_id, sql.ptr(), affected_rows))) {
6213LOG_WARN("execute sql failed", K(ret), K(sql));
6214} else if (sys_stat.item_list_.get_size() != affected_rows
6215&& sys_stat.item_list_.get_size() != affected_rows / 2) {
6216ret = OB_ERR_UNEXPECTED;
6217LOG_WARN("unexpected affected_rows", K(affected_rows),
6218"expected", sys_stat.item_list_.get_size());
6219}
6220}
6221}
6222return ret;
6223}
6224
6225int ObDDLOperator::init_sys_tenant_charset(ObMySQLTransaction &trans)
6226{
6227int ret = OB_SUCCESS;
6228const ObCharsetWrapper *charset_wrap_arr = NULL;
6229int64_t charset_wrap_arr_len = 0;
6230ObSqlString sql;
6231if (OB_FAIL(sql.assign_fmt("insert into %s "
6232"(charset, description, default_collation, max_length) values ",
6233OB_ALL_CHARSET_TNAME))) {
6234LOG_WARN("sql append failed", K(ret));
6235} else {
6236ObCharset::get_charset_wrap_arr(charset_wrap_arr, charset_wrap_arr_len);
6237if (OB_ISNULL(charset_wrap_arr) ||
6238OB_UNLIKELY(ObCharset::VALID_CHARSET_TYPES != charset_wrap_arr_len)) {
6239ret = OB_ERR_UNEXPECTED;
6240LOG_ERROR("charset wrap array is NULL or charset_wrap_arr_len is not CHARSET_WRAPPER_COUNT",
6241K(ret), K(charset_wrap_arr), K(charset_wrap_arr_len));
6242} else {
6243for (int64_t i = 0; OB_SUCC(ret) && i < charset_wrap_arr_len; ++i) {
6244ObCharsetWrapper charset_wrap = charset_wrap_arr[i];
6245if (OB_FAIL(sql.append_fmt("%s('%s', '%s', '%s', %ld)",
6246(0 == i) ? "" : ", ", ObCharset::charset_name(charset_wrap.charset_),
6247charset_wrap.description_,
6248ObCharset::collation_name(ObCharset::get_default_collation(charset_wrap.charset_)),
6249charset_wrap.maxlen_))) {
6250LOG_WARN("sql append failed", K(ret));
6251}
6252}
6253}
6254}
6255
6256if (OB_SUCC(ret)) {
6257LOG_INFO("create charset sql", K(sql));
6258int64_t affected_rows = 0;
6259if (OB_FAIL(trans.write(sql.ptr(), affected_rows))) {
6260LOG_WARN("execute sql failed", K(ret), K(sql));
6261} else if (charset_wrap_arr_len != affected_rows) {
6262ret = OB_ERR_UNEXPECTED;
6263LOG_ERROR("unexpected affected_rows", K(affected_rows),
6264"expected", (charset_wrap_arr_len));
6265}
6266}
6267return ret;
6268}
6269
6270int ObDDLOperator::init_sys_tenant_collation(ObMySQLTransaction &trans)
6271{
6272int ret = OB_SUCCESS;
6273const ObCollationWrapper *collation_wrap_arr = NULL;
6274int64_t collation_wrap_arr_len = 0;
6275ObSqlString sql;
6276int64_t total_valid_collations = 0;
6277if (OB_FAIL(sql.assign_fmt("insert into %s "
6278"(collation, charset, id, `is_default`, is_compiled, sortlen) values ",
6279OB_ALL_COLLATION_TNAME))) {
6280LOG_WARN("sql append failed", K(ret));
6281} else {
6282ObCharset::get_collation_wrap_arr(collation_wrap_arr, collation_wrap_arr_len);
6283if (OB_ISNULL(collation_wrap_arr) ||
6284OB_UNLIKELY(ObCharset::VALID_COLLATION_TYPES != collation_wrap_arr_len)) {
6285ret = OB_ERR_UNEXPECTED;
6286LOG_ERROR("collation wrap array is NULL or collation_wrap_arr_len is not COLLATION_WRAPPER_COUNT",
6287K(ret), K(collation_wrap_arr), K(collation_wrap_arr_len));
6288} else {
6289for (int64_t i = 0; OB_SUCC(ret) && i < collation_wrap_arr_len; ++i) {
6290ObCollationWrapper collation_wrap = collation_wrap_arr[i];
6291if (CS_TYPE_INVALID != collation_wrap.collation_) {
6292if (OB_FAIL(sql.append_fmt("%s('%s', '%s', %ld, '%s', '%s', %ld)",
6293(0 == total_valid_collations) ? "" : ", ",
6294ObCharset::collation_name(collation_wrap.collation_),
6295ObCharset::charset_name(collation_wrap.charset_),
6296collation_wrap.id_,
6297(true == collation_wrap.default_) ? "Yes" : "",
6298(true == collation_wrap.compiled_) ? "Yes" : "",
6299collation_wrap.sortlen_))) {
6300LOG_WARN("sql append failed", K(ret));
6301}
6302total_valid_collations++;
6303}
6304}
6305}
6306}
6307
6308if (OB_SUCC(ret)) {
6309LOG_INFO("create collation sql", K(sql));
6310int64_t affected_rows = 0;
6311if (OB_FAIL(trans.write(sql.ptr(), affected_rows))) {
6312LOG_WARN("execute sql failed", K(ret), K(sql));
6313} else if (total_valid_collations != affected_rows) {
6314ret = OB_ERR_UNEXPECTED;
6315LOG_ERROR("unexpected affected_rows", K(affected_rows),
6316"expected", (collation_wrap_arr_len));
6317}
6318}
6319return ret;
6320}
6321
6322int ObDDLOperator::init_sys_tenant_privilege(ObMySQLTransaction &trans)
6323{
6324int ret = OB_SUCCESS;
6325
6326ObSqlString sql;
6327int64_t row_count = 0;
6328if (OB_FAIL(sql.assign_fmt("INSERT /*+ use_plan_cache(none) */ INTO %s "
6329"(Privilege, Context, Comment) values ",
6330OB_ALL_PRIVILEGE_TNAME))) {
6331LOG_WARN("sql append failed", K(ret));
6332} else {
6333const PrivilegeRow *current_row = all_privileges;
6334if (OB_ISNULL(current_row)) {
6335ret = OB_ERR_UNEXPECTED;
6336LOG_WARN("current_row is null", K(ret));
6337} else {
6338bool is_first_row = true;
6339for (; OB_SUCC(ret) && NULL != current_row && NULL != current_row->privilege_;
6340++current_row) {
6341if (OB_FAIL(sql.append_fmt("%s('%s', '%s', '%s')",
6342is_first_row ? "" : ", ",
6343current_row->privilege_,
6344current_row->context_,
6345current_row->comment_))) {
6346LOG_WARN("sql append failed", K(ret));
6347} else {
6348++row_count;
6349is_first_row = false;
6350}
6351}
6352}
6353}
6354
6355if (OB_SUCC(ret)) {
6356LOG_INFO("create privileges sql", K(sql));
6357int64_t affected_rows = 0;
6358if (OB_FAIL(trans.write(sql.ptr(), affected_rows))) {
6359LOG_WARN("execute sql failed", K(ret), K(sql));
6360} else if (row_count != affected_rows) {
6361ret = OB_ERR_UNEXPECTED;
6362LOG_ERROR("unexpected affected_rows", K(affected_rows));
6363}
6364}
6365
6366return ret;
6367}
6368
6369//----Functions for managing privileges----
6370int ObDDLOperator::create_user(
6371const share::schema::ObUserInfo &user_info,
6372const ObString *ddl_stmt_str,
6373common::ObMySQLTransaction &trans)
6374{
6375int ret = OB_SUCCESS;
6376const uint64_t tenant_id = user_info.get_tenant_id();
6377int64_t new_schema_version = OB_INVALID_VERSION;
6378ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
6379if (OB_ISNULL(schema_sql_service)) {
6380ret = OB_ERR_SYS;
6381LOG_ERROR("schema_sql_service must not be null", K(ret));
6382} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
6383LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
6384} else if (OB_FAIL(schema_sql_service->get_user_sql_service().create_user(
6385user_info, new_schema_version, ddl_stmt_str, trans))) {
6386LOG_WARN("Failed to create user", K(user_info), K(ret));
6387}
6388return ret;
6389}
6390
6391int ObDDLOperator::drop_user(
6392const uint64_t tenant_id,
6393const uint64_t user_id,
6394const common::ObString *ddl_stmt_str,
6395common::ObMySQLTransaction &trans)
6396{
6397int ret = OB_SUCCESS;
6398ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
6399if (OB_INVALID_ID == tenant_id || OB_INVALID_ID == user_id) {
6400ret = OB_INVALID_ARGUMENT;
6401LOG_WARN("Tenant_id and user_id must not be null", K(tenant_id), K(user_id), K(ret));
6402} else if (OB_ISNULL(schema_sql_service)) {
6403ret = OB_ERR_SYS;
6404LOG_ERROR("schama sql service and schema manager must not be null",
6405K(schema_sql_service), K(ret));
6406}
6407//delete user
6408if (OB_SUCC(ret)) {
6409int64_t new_schema_version = OB_INVALID_VERSION;
6410ObSchemaGetterGuard schema_guard;
6411if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
6412LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
6413} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
6414LOG_WARN("failed to get schema guard", K(ret));
6415} else if (OB_FAIL(schema_sql_service->get_user_sql_service().drop_user(tenant_id,
6416user_id, new_schema_version, ddl_stmt_str, trans, schema_guard))) {
6417LOG_WARN("Drop user from all user table error", K(tenant_id), K(user_id), K(ret));
6418}
6419}
6420//delete db and table privileges of this user
6421if (OB_SUCC(ret)) {
6422if (OB_FAIL(drop_db_table_privs(tenant_id, user_id, trans))) {
6423LOG_WARN("Drop db, table privileges of user error", K(tenant_id), K(user_id), K(ret));
6424}
6425}
6426
6427// oracle mode, if the user has a label security policy, the label granted to the user will be deleted synchronously.
6428if (OB_SUCC(ret)) {
6429ObSchemaGetterGuard schema_guard;
6430if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
6431LOG_WARN("failed to get schema guard", K(ret));
6432} else if (OB_FAIL(drop_all_label_se_user_components(tenant_id, user_id,
6433OB_INVALID_ID, trans,
6434ObString(), schema_guard))) {
6435LOG_WARN("fail to drop user label components cascaded", K(ret));
6436}
6437}
6438
6439
6440// delete audit in user
6441if (OB_SUCC(ret)) {
6442ObArray<const ObSAuditSchema *> audits;
6443ObSchemaGetterGuard schema_guard;
6444int64_t new_schema_version = OB_INVALID_VERSION;
6445if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
6446LOG_WARN("failed to get schema guard", K(ret));
6447} else if (OB_FAIL(schema_guard.get_audit_schema_in_owner(tenant_id,
6448AUDIT_STMT,
6449user_id,
6450audits))) {
6451LOG_WARN("get get_audit_schema_in_owner failed", K(tenant_id), K(ret));
6452} else if (!audits.empty()) {
6453common::ObSqlString public_sql_string;
6454for (int64_t i = 0; OB_SUCC(ret) && i < audits.count(); ++i) {
6455const ObSAuditSchema *audit_schema = audits.at(i);
6456if (OB_ISNULL(audit_schema)) {
6457ret = OB_ERR_UNEXPECTED;
6458LOG_WARN("audit_schema is NULL", K(ret));
6459} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
6460LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
6461} else if (OB_FAIL(schema_sql_service->get_audit_sql_service().handle_audit_metainfo(
6462*audit_schema,
6463AUDIT_MT_DEL,
6464false,
6465new_schema_version,
6466NULL,
6467trans,
6468public_sql_string))) {
6469LOG_WARN("drop audit_schema failed", KPC(audit_schema), K(ret));
6470} else {
6471LOG_INFO("succ to delete audit_schema from drop user", KPC(audit_schema));
6472}
6473}
6474} else {
6475LOG_DEBUG("no need to delete audit_schema from drop user", K(user_id));
6476}
6477}
6478
6479return ret;
6480}
6481
6482int ObDDLOperator::drop_db_table_privs(
6483const uint64_t tenant_id,
6484const uint64_t user_id,
6485common::ObMySQLTransaction &trans)
6486{
6487int ret = OB_SUCCESS;
6488ObSchemaGetterGuard schema_guard;
6489ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
6490int64_t ddl_count = 0;
6491if (OB_INVALID_ID == tenant_id || OB_INVALID_ID == user_id) {
6492ret = OB_INVALID_ARGUMENT;
6493LOG_WARN("Tenant_id and user_id must not be null", K(tenant_id), K(user_id), K(ret));
6494} else if (OB_ISNULL(schema_sql_service)) {
6495ret = OB_ERR_SYS;
6496LOG_ERROR("schama sql service and schema manager must not be null",
6497K(schema_sql_service), K(ret));
6498} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
6499LOG_WARN("failed to get schema guard", K(ret));
6500}
6501// delete database privileges of this user
6502if (OB_SUCC(ret)) {
6503ObArray<const ObDBPriv *> db_privs;
6504if (OB_FAIL(schema_guard.get_db_priv_with_user_id(
6505tenant_id, user_id, db_privs))) {
6506LOG_WARN("Get database privileges of user to be deleted error",
6507K(tenant_id), K(user_id), K(ret));
6508} else {
6509for (int64_t i = 0; OB_SUCC(ret) && i < db_privs.count(); ++i) {
6510const ObDBPriv *db_priv = db_privs.at(i);
6511int64_t new_schema_version = OB_INVALID_VERSION;
6512if (OB_ISNULL(db_priv)) {
6513LOG_WARN("db priv is NULL", K(ret), K(db_priv));
6514} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
6515LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
6516} else if (OB_FAIL(schema_sql_service->get_priv_sql_service().delete_db_priv(
6517db_priv->get_original_key(), new_schema_version, trans))) {
6518LOG_WARN("Delete database privilege failed", "DB Priv", *db_priv, K(ret));
6519}
6520}
6521ddl_count -= db_privs.count();
6522}
6523}
6524// delete table privileges of this user MYSQL
6525if (OB_SUCC(ret)) {
6526ObArray<const ObTablePriv *> table_privs;
6527if (OB_FAIL(schema_guard.get_table_priv_with_user_id(
6528tenant_id, user_id, table_privs))) {
6529LOG_WARN("Get table privileges of user to be deleted error",
6530K(tenant_id), K(user_id), K(ret));
6531} else {
6532for (int64_t i = 0; OB_SUCC(ret) && i < table_privs.count(); ++i) {
6533const ObTablePriv *table_priv = table_privs.at(i);
6534int64_t new_schema_version = OB_INVALID_VERSION;
6535if (OB_ISNULL(table_priv)) {
6536LOG_WARN("table priv is NULL", K(ret), K(table_priv));
6537} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
6538LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
6539} else if (OB_FAIL(schema_sql_service->get_priv_sql_service().delete_table_priv(
6540table_priv->get_sort_key(), new_schema_version, trans))) {
6541LOG_WARN("Delete table privilege failed", "Table Priv", *table_priv, K(ret));
6542}
6543}
6544}
6545}
6546
6547// delete oracle table privileges of this user ORACLE
6548if (OB_SUCC(ret)) {
6549ObArray<const ObObjPriv *> obj_privs;
6550
6551OZ (schema_guard.get_obj_priv_with_grantee_id(
6552tenant_id, user_id, obj_privs));
6553OZ (schema_guard.get_obj_priv_with_grantor_id(
6554tenant_id, user_id, obj_privs, false));
6555for (int64_t i = 0; OB_SUCC(ret) && i < obj_privs.count(); ++i) {
6556const ObObjPriv *obj_priv = obj_privs.at(i);
6557int64_t new_schema_version = OB_INVALID_VERSION;
6558if (OB_ISNULL(obj_priv)) {
6559LOG_WARN("obj_priv priv is NULL", K(ret), K(obj_priv));
6560} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
6561LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
6562} else if (OB_FAIL(schema_sql_service->get_priv_sql_service().delete_obj_priv(
6563*obj_priv, new_schema_version, trans))) {
6564LOG_WARN("Delete obj_priv privilege failed", "obj Priv", *obj_priv, K(ret));
6565}
6566}
6567}
6568
6569return ret;
6570}
6571
6572int ObDDLOperator::rename_user(
6573const uint64_t tenant_id,
6574const uint64_t user_id,
6575const ObAccountArg &new_account,
6576const common::ObString *ddl_stmt_str,
6577common::ObMySQLTransaction &trans)
6578{
6579int ret = OB_SUCCESS;
6580ObSchemaGetterGuard schema_guard;
6581ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
6582if (OB_INVALID_ID == tenant_id || OB_INVALID_ID == user_id) {
6583ret = OB_INVALID_ARGUMENT;
6584LOG_WARN("tenant_id and user_id must not be null", K(tenant_id), K(user_id), K(ret));
6585} else if (OB_ISNULL(schema_sql_service)) {
6586ret = OB_ERR_SYS;
6587LOG_ERROR("schama service_impl must not null",
6588"schema_service_impl", schema_sql_service, K(ret));
6589} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
6590LOG_WARN("failed to get schema guard", K(ret));
6591} else {
6592const ObUserInfo *user_info = NULL;
6593int64_t new_schema_version = OB_INVALID_VERSION;
6594if (OB_FAIL(schema_guard.get_user_info(tenant_id, user_id, user_info))) {
6595LOG_WARN("failed to get user info", K(ret), K(tenant_id), K(user_id));
6596} else if (OB_ISNULL(user_info)) {
6597ret = OB_ERR_USER_NOT_EXIST;
6598LOG_WARN("User not exist", K(ret));
6599} else {
6600ObUserInfo new_user_info = *user_info;
6601new_user_info.set_user_name(new_account.user_name_);
6602new_user_info.set_host(new_account.host_name_);
6603if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
6604LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
6605} else if (OB_FAIL(schema_sql_service->get_user_sql_service().rename_user(
6606new_user_info, new_schema_version, ddl_stmt_str, trans))) {
6607LOG_WARN("Failed to rename user", K(tenant_id), K(user_id), K(new_account), K(ret));
6608}
6609}
6610}
6611return ret;
6612}
6613
6614int ObDDLOperator::set_passwd(
6615const uint64_t tenant_id,
6616const uint64_t user_id,
6617const common::ObString &passwd,
6618const ObString *ddl_stmt_str,
6619common::ObMySQLTransaction &trans)
6620{
6621int ret = OB_SUCCESS;
6622ObSchemaGetterGuard schema_guard;
6623ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
6624if (OB_INVALID_ID == tenant_id || OB_INVALID_ID == user_id) {
6625ret = OB_INVALID_ARGUMENT;
6626LOG_WARN("tenant_id and user_id must not be null", K(tenant_id), K(user_id), K(ret));
6627} else if (OB_ISNULL(schema_sql_service)) {
6628ret = OB_ERR_SYS;
6629LOG_ERROR("schama service_impl and schema manage must not null",
6630"schema_service_impl", schema_sql_service, K(ret));
6631} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
6632LOG_WARN("failed to get schema guard", K(ret));
6633} else {
6634const ObUserInfo *user_info = NULL;
6635if (OB_FAIL(schema_guard.get_user_info(tenant_id, user_id, user_info))) {
6636LOG_WARN("failed to get user info", K(ret));
6637} else if (OB_ISNULL(user_info)) {
6638ret = OB_ERR_USER_NOT_EXIST;
6639LOG_WARN("User not exist", K(ret));
6640} else {
6641int64_t new_schema_version = OB_INVALID_VERSION;
6642ObUserInfo new_user_info = *user_info;
6643new_user_info.set_passwd(passwd);
6644new_user_info.set_password_last_changed(ObTimeUtility::current_time());
6645if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
6646LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
6647} else if (OB_FAIL(schema_sql_service->get_user_sql_service().set_passwd(
6648new_user_info, new_schema_version, ddl_stmt_str, trans))) {
6649LOG_WARN("Failed to set passwd", K(tenant_id), K(user_id), K(ret));
6650}
6651}
6652}
6653
6654return ret;
6655}
6656
6657int ObDDLOperator::set_max_connections(
6658const uint64_t tenant_id,
6659const uint64_t user_id,
6660const uint64_t max_connections_per_hour,
6661const uint64_t max_user_connections,
6662const ObString *ddl_stmt_str,
6663common::ObMySQLTransaction &trans)
6664{
6665int ret = OB_SUCCESS;
6666ObSchemaGetterGuard schema_guard;
6667ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
6668if (OB_INVALID_ID == tenant_id || OB_INVALID_ID == user_id) {
6669ret = OB_INVALID_ARGUMENT;
6670LOG_WARN("tenant_id and user_id must not be null", K(tenant_id), K(user_id), K(ret));
6671} else if (OB_ISNULL(schema_sql_service)) {
6672ret = OB_ERR_SYS;
6673LOG_ERROR("schama service_impl and schema manage must not null",
6674"schema_service_impl", schema_sql_service, K(ret));
6675} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
6676LOG_WARN("failed to get schema guard", K(ret));
6677} else {
6678const ObUserInfo *user_info = NULL;
6679if (OB_FAIL(schema_guard.get_user_info(tenant_id, user_id, user_info))) {
6680LOG_WARN("failed to get user info", K(ret));
6681} else if (OB_ISNULL(user_info)) {
6682ret = OB_ERR_USER_NOT_EXIST;
6683LOG_WARN("User not exist", K(ret));
6684} else {
6685int64_t new_schema_version = OB_INVALID_VERSION;
6686ObUserInfo new_user_info = *user_info;
6687if (OB_INVALID_ID != max_connections_per_hour) {
6688new_user_info.set_max_connections(max_connections_per_hour);
6689}
6690if (OB_INVALID_ID != max_user_connections) {
6691new_user_info.set_max_user_connections(max_user_connections);
6692}
6693if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
6694LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
6695} else if (OB_FAIL(schema_sql_service->get_user_sql_service().set_max_connections(
6696new_user_info, new_schema_version, ddl_stmt_str, trans))) {
6697LOG_WARN("Failed to set passwd", K(tenant_id), K(user_id), K(ret));
6698}
6699}
6700}
6701
6702return ret;
6703}
6704
6705int ObDDLOperator::alter_role(
6706const uint64_t tenant_id,
6707const uint64_t role_id,
6708const common::ObString &passwd,
6709const ObString *ddl_stmt_str,
6710common::ObMySQLTransaction &trans)
6711{
6712int ret = OB_SUCCESS;
6713ObSchemaGetterGuard schema_guard;
6714ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
6715if (OB_INVALID_ID == tenant_id || OB_INVALID_ID == role_id) {
6716ret = OB_INVALID_ARGUMENT;
6717LOG_WARN("tenant_id and role_id must not be null", K(tenant_id), K(role_id), K(ret));
6718} else if (OB_ISNULL(schema_sql_service)) {
6719ret = OB_ERR_SYS;
6720LOG_ERROR("schama service_impl and schema manage must not null",
6721"schema_service_impl", schema_sql_service, K(ret));
6722} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
6723LOG_WARN("failed to get schema guard", K(ret));
6724} else {
6725const ObUserInfo *role_info = NULL;
6726if (OB_FAIL(schema_guard.get_user_info(tenant_id, role_id, role_info))) {
6727LOG_WARN("failed to get role info", K(ret), K(role_id));
6728} else if (OB_ISNULL(role_info)) {
6729ret = OB_ROLE_NOT_EXIST;
6730LOG_WARN("Role not exist", K(ret));
6731} else {
6732int64_t new_schema_version = OB_INVALID_VERSION;
6733ObUserInfo new_role_info = *role_info;
6734new_role_info.set_passwd(passwd);
6735if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
6736LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
6737} else if (OB_FAIL(schema_sql_service->get_user_sql_service().alter_role(
6738new_role_info, new_schema_version, ddl_stmt_str, trans))) {
6739LOG_WARN("Failed to alter_role", K(tenant_id), K(role_id), K(ret));
6740}
6741}
6742}
6743
6744return ret;
6745}
6746
6747int ObDDLOperator::alter_user_default_role(const ObString &ddl_str,
6748const ObUserInfo &schema,
6749ObIArray<uint64_t> &role_id_array,
6750ObIArray<uint64_t> &disable_flag_array,
6751ObMySQLTransaction &trans)
6752{
6753int ret = OB_SUCCESS;
6754ObSchemaService *schema_sql_service = NULL;
6755int64_t new_schema_version = OB_INVALID_VERSION;
6756
6757if (OB_ISNULL(schema_sql_service = schema_service_.get_schema_service())) {
6758ret = OB_ERR_SYS;
6759LOG_ERROR("schema_sql_service must not null", K(ret));
6760} else if (!schema.is_valid()) {
6761ret = OB_INVALID_ARGUMENT;
6762LOG_WARN("invalid argument", K(ret), K(schema));
6763} else if (OB_FAIL(schema_service_.gen_new_schema_version(schema.get_tenant_id(),
6764new_schema_version))) {
6765LOG_WARN("fail to gen new schema_version", K(ret));
6766} else {
6767if (OB_FAIL(schema_sql_service->get_priv_sql_service().alter_user_default_role(
6768schema,
6769new_schema_version,
6770&ddl_str,
6771role_id_array,
6772disable_flag_array,
6773trans))) {
6774LOG_WARN("alter user default role failed", K(ret));
6775}
6776}
6777
6778LOG_DEBUG("alter_user_default_role", K(schema));
6779return ret;
6780}
6781
6782int ObDDLOperator::alter_user_profile(const ObString &ddl_str,
6783ObUserInfo &schema,
6784ObMySQLTransaction &trans)
6785{
6786int ret = OB_SUCCESS;
6787ObSchemaService *schema_sql_service = NULL;
6788int64_t new_schema_version = OB_INVALID_VERSION;
6789
6790if (OB_ISNULL(schema_sql_service = schema_service_.get_schema_service())) {
6791ret = OB_ERR_SYS;
6792LOG_ERROR("schema_sql_service must not null", K(ret));
6793} else if (!schema.is_valid()) {
6794ret = OB_INVALID_ARGUMENT;
6795LOG_WARN("invalid argument", K(ret), K(schema));
6796} else if (OB_FAIL(schema_service_.gen_new_schema_version(schema.get_tenant_id(), new_schema_version))) {
6797LOG_WARN("fail to gen new schema_version", K(ret));
6798} else {
6799schema.set_schema_version(new_schema_version);
6800if (OB_FAIL(schema_sql_service->get_user_sql_service().alter_user_profile(schema, &ddl_str, trans))) {
6801LOG_WARN("alter user profile failed", K(ret));
6802}
6803}
6804
6805LOG_DEBUG("alter_user_profile", K(schema));
6806return ret;
6807}
6808
6809int ObDDLOperator::alter_user_require(const uint64_t tenant_id,
6810const uint64_t user_id,
6811const obrpc::ObSetPasswdArg &arg,
6812const common::ObString *ddl_stmt_str,
6813common::ObMySQLTransaction &trans)
6814{
6815int ret = OB_SUCCESS;
6816ObSchemaGetterGuard schema_guard;
6817ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
6818if (OB_INVALID_ID == tenant_id || OB_INVALID_ID == user_id) {
6819ret = OB_INVALID_ARGUMENT;
6820LOG_WARN("tenant_id and user_id must not be null", K(tenant_id), K(user_id), K(ret));
6821} else if (OB_ISNULL(schema_sql_service)) {
6822ret = OB_ERR_SYS;
6823LOG_ERROR("schama service_impl and schema manage must not null",
6824"schema_service_impl", schema_sql_service, K(ret));
6825} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
6826LOG_WARN("failed to get schema guard", K(ret));
6827} else {
6828const ObUserInfo *user_info = NULL;
6829if (OB_FAIL(schema_guard.get_user_info(tenant_id, user_id, user_info))) {
6830LOG_WARN("failed to get user info", K(ret));
6831} else if (OB_ISNULL(user_info)) {
6832ret = OB_ERR_USER_NOT_EXIST;
6833LOG_WARN("User not exist", K(ret));
6834} else {
6835int64_t new_schema_version = OB_INVALID_VERSION;
6836ObUserInfo new_user_info = *user_info;
6837new_user_info.set_ssl_type(arg.ssl_type_);
6838new_user_info.set_ssl_cipher(arg.ssl_cipher_);
6839new_user_info.set_x509_issuer(arg.x509_issuer_);
6840new_user_info.set_x509_subject(arg.x509_subject_);
6841if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
6842LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
6843} else if (OB_FAIL(schema_sql_service->get_user_sql_service().alter_user_require(
6844new_user_info, new_schema_version, ddl_stmt_str, trans))) {
6845LOG_WARN("Failed to alter_user_require", K(tenant_id), K(user_id), K(ret));
6846}
6847}
6848}
6849
6850return ret;
6851}
6852
6853int ObDDLOperator::grant_revoke_user(
6854const uint64_t tenant_id,
6855const uint64_t user_id,
6856const ObPrivSet priv_set,
6857const bool grant,
6858const bool is_from_inner_sql,
6859const ObString *ddl_stmt_str,
6860common::ObMySQLTransaction &trans)
6861{
6862int ret = OB_SUCCESS;
6863ObSchemaGetterGuard schema_guard;
6864ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
6865if (OB_INVALID_ID == tenant_id || OB_INVALID_ID == user_id) {
6866ret = OB_INVALID_ARGUMENT;
6867LOG_WARN("tenant_id and user_id must not be null", K(tenant_id), K(user_id), K(ret));
6868} else if (OB_ISNULL(schema_sql_service)) {
6869ret = OB_ERR_SYS;
6870LOG_ERROR("schama service_impl and schema manage must not null",
6871"schema_service_impl", schema_sql_service, K(ret));
6872} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
6873LOG_WARN("failed to get schema guard", K(ret));
6874} else {
6875ObPrivSet new_priv = priv_set;
6876
6877const ObUserInfo *user_info = NULL;
6878if (OB_FAIL(schema_guard.get_user_info(tenant_id, user_id, user_info)) ||
6879NULL == user_info) {
6880ret = OB_ERR_USER_NOT_EXIST;
6881LOG_WARN("User not exist", K(ret));
6882} else {
6883if (grant) {
6884new_priv = priv_set | user_info->get_priv_set();
6885} else {
6886new_priv = (~priv_set) & user_info->get_priv_set();
6887}
6888//no matter privilege change or not, write a sql
6889int64_t new_schema_version = OB_INVALID_VERSION;
6890ObUserInfo new_user_info = *user_info;
6891new_user_info.set_priv_set(new_priv);
6892if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
6893LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
6894} else if (OB_FAIL(schema_sql_service->get_user_sql_service().grant_revoke_user(
6895new_user_info, new_schema_version, ddl_stmt_str, trans, is_from_inner_sql))) {
6896LOG_WARN("Failed to grant or revoke user", K(tenant_id), K(user_id), K(grant), K(ret));
6897}
6898}
6899}
6900
6901return ret;
6902}
6903
6904int ObDDLOperator::lock_user(
6905const uint64_t tenant_id,
6906const uint64_t user_id,
6907const bool locked,
6908const ObString *ddl_stmt_str,
6909common::ObMySQLTransaction &trans)
6910{
6911int ret = OB_SUCCESS;
6912ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
6913ObSchemaGetterGuard schema_guard;
6914if (OB_INVALID_ID == tenant_id || OB_INVALID_ID == user_id) {
6915ret = OB_INVALID_ARGUMENT;
6916LOG_WARN("tenant_id and user_id is invalid", K(tenant_id), K(user_id), K(ret));
6917} else if (OB_ISNULL(schema_sql_service)) {
6918ret = OB_ERR_SYS;
6919LOG_ERROR("schama service_impl and schema manage must not null",
6920"schema_service_impl", schema_sql_service, K(ret));
6921} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
6922LOG_WARN("failed to get schema guard", K(ret));
6923} else {
6924const ObUserInfo *user_info = NULL;
6925if (OB_FAIL(schema_guard.get_user_info(tenant_id, user_id, user_info)) ||
6926NULL == user_info) {
6927ret = OB_ERR_USER_NOT_EXIST;
6928LOG_WARN("User not exist", K(ret));
6929} else if (locked != user_info->get_is_locked()) {
6930int64_t new_schema_version = OB_INVALID_VERSION;
6931ObUserInfo new_user_info = *user_info;
6932new_user_info.set_is_locked(locked);
6933if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
6934LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
6935} else if (OB_FAIL(schema_sql_service->get_user_sql_service().lock_user(
6936new_user_info, new_schema_version, ddl_stmt_str, trans))) {
6937LOG_WARN("Failed to lock user", K(tenant_id), K(user_id), K(locked), K(ret));
6938}
6939}
6940}
6941return ret;
6942}
6943
6944int ObDDLOperator::grant_database(
6945const ObOriginalDBKey &db_priv_key,
6946const ObPrivSet priv_set,
6947const ObString *ddl_stmt_str,
6948common::ObMySQLTransaction &trans)
6949{
6950int ret = OB_SUCCESS;
6951const uint64_t tenant_id = db_priv_key.tenant_id_;
6952ObSchemaGetterGuard schema_guard;
6953ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
6954if (OB_ISNULL(schema_sql_service)) {
6955ret = OB_ERR_SYS;
6956LOG_ERROR("schama service_impl and schema manage must not null",
6957"schema_service_impl", schema_sql_service, K(ret));
6958} else if (!db_priv_key.is_valid()) {
6959ret = OB_INVALID_ARGUMENT;
6960LOG_WARN("db_priv_key is invalid", K(db_priv_key), K(ret));
6961} else if (0 == priv_set) {
6962//do nothing
6963} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
6964LOG_WARN("failed to get schema guard", K(ret));
6965} else {
6966ObPrivSet new_priv = priv_set;
6967bool need_flush = true;
6968ObPrivSet db_priv_set = OB_PRIV_SET_EMPTY;
6969if (OB_FAIL(schema_guard.get_db_priv_set(db_priv_key, db_priv_set, true))) {
6970LOG_WARN("get db priv set failed", K(ret));
6971} else {
6972new_priv |= db_priv_set;
6973need_flush = (new_priv != db_priv_set);
6974if (need_flush) {
6975int64_t new_schema_version = OB_INVALID_VERSION;
6976if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
6977LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
6978} else if (OB_FAIL(schema_sql_service->get_priv_sql_service().grant_database(db_priv_key,
6979new_priv,
6980new_schema_version,
6981ddl_stmt_str,
6982trans))) {
6983LOG_WARN("Failed to grant database", K(db_priv_key), K(ret));
6984}
6985}
6986}
6987}
6988return ret;
6989}
6990
6991int ObDDLOperator::revoke_database(
6992const ObOriginalDBKey &db_priv_key,
6993const ObPrivSet priv_set,
6994common::ObMySQLTransaction &trans)
6995{
6996int ret = OB_SUCCESS;
6997const uint64_t tenant_id = db_priv_key.tenant_id_;
6998ObSchemaGetterGuard schema_guard;
6999ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
7000if (OB_ISNULL(schema_sql_service)) {
7001ret = OB_ERR_SYS;
7002LOG_ERROR("schama service_impl and schema manage must not null",
7003"schema_service_impl", schema_sql_service, K(ret));
7004} else if (!db_priv_key.is_valid()) {
7005ret = OB_INVALID_ARGUMENT;
7006LOG_WARN("db_priv_key is invalid", K(db_priv_key), K(ret));
7007} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
7008LOG_WARN("failed to get schema guard", K(ret));
7009} else {
7010ObPrivSet db_priv_set = OB_PRIV_SET_EMPTY;
7011if (OB_FAIL(schema_guard.get_db_priv_set(db_priv_key, db_priv_set, true))) {
7012LOG_WARN("get db priv set failed", K(ret));
7013} else if (OB_PRIV_SET_EMPTY == db_priv_set) {
7014ret = OB_ERR_NO_GRANT;
7015LOG_WARN("No such grant to revoke", K(db_priv_key), K(ret));
7016} else if (0 == priv_set) {
7017//do nothing
7018} else {
7019ObPrivSet new_priv = db_priv_set & (~priv_set);
7020if (db_priv_set & priv_set) {
7021ObSqlString ddl_stmt_str;
7022ObString ddl_sql;
7023const ObUserInfo *user_info = NULL;
7024ObNeedPriv need_priv;
7025need_priv.db_ = db_priv_key.db_;
7026need_priv.priv_level_ = OB_PRIV_DB_LEVEL;
7027need_priv.priv_set_ = db_priv_set & priv_set; //priv to revoke
7028int64_t new_schema_version = OB_INVALID_VERSION;
7029if (OB_FAIL(schema_guard.get_user_info(tenant_id, db_priv_key.user_id_, user_info))) {
7030LOG_WARN("get user info failed", K(tenant_id), K(db_priv_key), K(ret));
7031} else if (OB_ISNULL(user_info)) {
7032ret = OB_ERR_UNEXPECTED;
7033LOG_WARN("user not exist", K(db_priv_key), K(ret));
7034} else if (OB_FAIL(ObDDLSqlGenerator::gen_db_priv_sql(ObAccountArg(user_info->get_user_name_str(), user_info->get_host_name_str()),
7035need_priv,
7036false, /*is_grant*/
7037ddl_stmt_str))) {
7038LOG_WARN("gen_db_priv_sql failed", K(ret), K(need_priv));
7039} else if (FALSE_IT(ddl_sql = ddl_stmt_str.string())) {
7040} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
7041LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
7042} else if (OB_FAIL(schema_sql_service->get_priv_sql_service().revoke_database(
7043db_priv_key, new_priv, new_schema_version, &ddl_sql, trans))) {
7044LOG_WARN("Failed to revoke database", K(db_priv_key), K(ret));
7045}
7046}
7047}
7048}
7049return ret;
7050}
7051
7052/* According to the current obj priv, check if the user has all the permissions listed in obj_priv_array */
7053int ObDDLOperator::check_obj_privs_exists(
7054ObSchemaGetterGuard &schema_guard,
7055const share::schema::ObObjPrivSortKey &obj_priv_key, /* in: obj priv key */
7056const ObRawObjPrivArray &obj_priv_array, /* in: privs to be deleted */
7057ObRawObjPrivArray &option_priv_array, /* out: privs to be deleted cascade */
7058bool &is_all) /* out: obj priv array is all privs existed */
7059{
7060int ret = OB_SUCCESS;
7061ObPackedObjPriv obj_privs = 0;
7062ObRawObjPriv raw_obj_priv = 0;
7063bool exists = false;
7064uint64_t option_out = false;
7065is_all = false;
7066int org_n = 0;
7067OZ (schema_guard.get_obj_privs(obj_priv_key, obj_privs));
7068for (int i = 0; i < obj_priv_array.count() && OB_SUCC(ret); i++) {
7069raw_obj_priv = obj_priv_array.at(i);
7070OZ (ObOraPrivCheck::raw_obj_priv_exists_with_info(raw_obj_priv,
7071obj_privs,
7072exists,
7073option_out),
7074raw_obj_priv, obj_privs, ret);
7075if (OB_SUCC(ret)) {
7076if (!exists) {
7077ret = OB_ERR_CANNOT_REVOKE_PRIVILEGES_YOU_DID_NOT_GRANT;
7078} else if (option_out == GRANT_OPTION) {
7079OZ (option_priv_array.push_back(raw_obj_priv));
7080}
7081}
7082}
7083OZ (ObPrivPacker::get_total_obj_privs(obj_privs, org_n));
7084OX (is_all = org_n == obj_priv_array.count());
7085return ret;
7086}
7087
7088/** According to the current obj priv, check if the user has all the permissions listed in obj_priv_array, including the column permissions on the table.
7089* check_obj_privs_exists_including_col_privs
7090* is used to check the existence of object permissions
7091* @param {ObSchemaGetterGuard &} schema_guard : schema_guard
7092* @param {const schema::ObObjPrivSortKey &} obj_priv_key : object for checking, accurate to the table
7093* @param {const ObRawObjPrivArray &} obj_priv_array : permission for revoking
7094* @param {ObIArray<schema::ObObjPrivSortKey> &} new_key_array : There may be multiple columns on the obj_priv_key
7095* table object with independent column permissions, so we regenerate all the keys,
7096* which are accurate to the column
7097* @param {ObIArray<ObPackedObjPriv> &} new_packed_privs_array : Corresponds to new_key_array,
7098* permission to revoke
7099* @param {ObIArray<bool> &} is_all : Corresponds to new_key_array,
7100* indicating whether the permission to revoke is all the permissions owned by the key
7101* @return {int} : ret
7102*/
7103int ObDDLOperator::check_obj_privs_exists_including_col_privs(
7104ObSchemaGetterGuard &schema_guard,
7105const share::schema::ObObjPrivSortKey &obj_priv_key,
7106const ObRawObjPrivArray &obj_priv_array,
7107ObIArray<share::schema::ObObjPrivSortKey> &new_key_array,
7108ObIArray<ObPackedObjPriv> &new_packed_privs_array,
7109ObIArray<bool> &is_all)
7110{
7111int ret = OB_SUCCESS;
7112ObRawObjPriv raw_obj_priv_to_be_revoked = 0;
7113ObPackedObjPriv packed_table_privs = 0;
7114ObPackedObjPriv packed_table_privs_to_be_revoked = 0;
7115ObSEArray<uint64_t, 4> col_id_array;
7116ObSEArray<ObPackedObjPriv, 4> packed_col_privs_array;
7117ObPackedObjPriv packed_total_matched_privs = 0;
7118ObObjPrivSortKey new_col_key = obj_priv_key;
7119ObPackedObjPriv packed_col_privs = 0;
7120ObPackedObjPriv packed_col_privs_to_be_revoked = 0;
7121bool exists = false;
7122int org_n = 0;
7123int own_priv_count = 0;
7124int revoked_priv_count = 0;
7125uint64_t option_out = false;
7126bool is_all_single = false;
7127new_key_array.reset();
7128new_packed_privs_array.reset();
7129is_all.reset();
7130// 1. Find all object permissions based on grantee_id, grantor_id, obj_type, obj_id.
7131OZ (build_table_and_col_priv_array_for_revoke_all(schema_guard,
7132obj_priv_key,
7133packed_table_privs,
7134col_id_array,
7135packed_col_privs_array));
7136CK (col_id_array.count() == packed_col_privs_array.count());
7137// 2. check permissions of table level.
7138for (int i = 0; OB_SUCC(ret) && i < obj_priv_array.count(); ++i) {
7139raw_obj_priv_to_be_revoked = obj_priv_array.at(i);
7140// Check whether the table-level permission exists on the table
7141OZ (ObOraPrivCheck::raw_obj_priv_exists_with_info(raw_obj_priv_to_be_revoked,
7142packed_table_privs,
7143exists,
7144option_out),
7145raw_obj_priv_to_be_revoked, packed_table_privs, ret);
7146if (OB_SUCC(ret)) {
7147// If it exists, add the permission together with option to packed_table_privs_to_be_revoked,
7148// and added in packed_total_matched_privs means that the permission was found
7149// The permission may not exist as a table-level permission on the table
7150if (exists) {
7151OZ (ObPrivPacker::append_raw_obj_priv(option_out,
7152raw_obj_priv_to_be_revoked,
7153packed_table_privs_to_be_revoked));
7154OZ (ObPrivPacker::append_raw_obj_priv(option_out,
7155raw_obj_priv_to_be_revoked,
7156packed_total_matched_privs));
7157}
7158}
7159}
7160// Record the table key and its permission to be revoke in the return value
7161if (packed_table_privs_to_be_revoked) {
7162OZ (new_key_array.push_back(obj_priv_key));
7163OZ (new_packed_privs_array.push_back(packed_table_privs_to_be_revoked));
7164OZ (ObPrivPacker::get_total_obj_privs(packed_table_privs, own_priv_count));
7165OZ (ObPrivPacker::get_total_obj_privs(packed_table_privs_to_be_revoked, revoked_priv_count));
7166OX (is_all_single = own_priv_count == revoked_priv_count);
7167OZ (is_all.push_back(is_all_single));
7168}
7169// 3. Check column permissions
7170for (int i = 0; OB_SUCC(ret) && i < col_id_array.count(); ++i) {
7171// each column
7172new_col_key.col_id_ = col_id_array.at(i);
7173packed_col_privs = packed_col_privs_array.at(i);
7174packed_col_privs_to_be_revoked = 0;
7175for (int i = 0; OB_SUCC(ret) && i < obj_priv_array.count(); ++i) {
7176raw_obj_priv_to_be_revoked = obj_priv_array.at(i);
7177// Check only if the permission may be a column permission
7178if (ObOraPrivCheck::raw_priv_can_be_granted_to_column(raw_obj_priv_to_be_revoked)) {
7179// Check if the permission exists on the column
7180OZ (ObOraPrivCheck::raw_obj_priv_exists_with_info(raw_obj_priv_to_be_revoked,
7181packed_col_privs,
7182exists,
7183option_out),
7184raw_obj_priv_to_be_revoked, packed_col_privs, ret);
7185if (OB_SUCC(ret)) {
7186if (exists) {
7187OZ (ObPrivPacker::append_raw_obj_priv(option_out,
7188raw_obj_priv_to_be_revoked,
7189packed_col_privs_to_be_revoked));
7190OZ (ObPrivPacker::append_raw_obj_priv(option_out,
7191raw_obj_priv_to_be_revoked,
7192packed_total_matched_privs));
7193}
7194}
7195}
7196}
7197// According to whether there are permissions in packed_col_privs_to_be_revoked, decide whether to keep the key
7198if (OB_SUCC(ret)) {
7199if (packed_col_privs_to_be_revoked) {
7200OZ (new_key_array.push_back(new_col_key));
7201OZ (new_packed_privs_array.push_back(packed_col_privs_to_be_revoked));
7202OZ (ObPrivPacker::get_total_obj_privs(packed_col_privs, own_priv_count));
7203OZ (ObPrivPacker::get_total_obj_privs(packed_col_privs_to_be_revoked, revoked_priv_count));
7204OX (is_all_single = own_priv_count == revoked_priv_count);
7205OZ (is_all.push_back(is_all_single));
7206}
7207}
7208}
7209// The three arrays should be the same size after processing.
7210CK (new_key_array.count() == new_packed_privs_array.count());
7211CK (new_key_array.count() == is_all.count());
7212// According to the number of packed_total_matched_privs, determine whether to try to revoke a permission that does not exist
7213OZ (ObPrivPacker::get_total_obj_privs(packed_total_matched_privs, org_n));
7214if (OB_SUCC(ret)) {
7215if (org_n < obj_priv_array.count()) {
7216ret = OB_ERR_CANNOT_REVOKE_PRIVILEGES_YOU_DID_NOT_GRANT;
7217LOG_WARN("try to revoke non exists privs", K(ret), K(org_n), K(obj_priv_array.count()));
7218}
7219}
7220return ret;
7221}
7222
7223/* According to the current obj priv, determine which ones need to be newly added priv array */
7224int ObDDLOperator::set_need_flush_ora(
7225ObSchemaGetterGuard &schema_guard,
7226const share::schema::ObObjPrivSortKey &obj_priv_key, /* in: obj priv key*/
7227const uint64_t option, /* in: new option */
7228const ObRawObjPrivArray &obj_priv_array, /* in: new privs used want to add */
7229ObRawObjPrivArray &new_obj_priv_array) /* out: new privs actually to be added */
7230{
7231int ret = OB_SUCCESS;
7232ObPackedObjPriv obj_privs = 0;
7233ObRawObjPriv raw_obj_priv = 0;
7234bool exists = false;
7235OZ (schema_guard.get_obj_privs(obj_priv_key, obj_privs));
7236for (int i = 0; i < obj_priv_array.count() && OB_SUCC(ret); i++) {
7237raw_obj_priv = obj_priv_array.at(i);
7238OZ (ObOraPrivCheck::raw_obj_priv_exists(raw_obj_priv,
7239option,
7240obj_privs,
7241exists),
7242raw_obj_priv, option, obj_privs, ret);
7243if (OB_SUCC(ret) && !exists) {
7244OZ (new_obj_priv_array.push_back(raw_obj_priv));
7245}
7246}
7247return ret;
7248}
7249
7250/* Only handle authorization for one object, for example, one table, one column */
7251int ObDDLOperator::grant_table(
7252const ObTablePrivSortKey &table_priv_key,
7253const ObPrivSet priv_set,
7254const ObString *ddl_stmt_str,
7255common::ObMySQLTransaction &trans,
7256const share::ObRawObjPrivArray &obj_priv_array,
7257const uint64_t option,
7258const share::schema::ObObjPrivSortKey &obj_priv_key)
7259{
7260int ret = OB_SUCCESS;
7261ObRawObjPrivArray new_obj_priv_array;
7262const uint64_t tenant_id = table_priv_key.tenant_id_;
7263ObSchemaGetterGuard schema_guard;
7264ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
7265if (OB_ISNULL(schema_sql_service)) {
7266ret = OB_ERR_SYS;
7267LOG_ERROR("schama service_impl and schema manage must not null",
7268"schema_service_impl", schema_sql_service, K(ret));
7269} else if (!table_priv_key.is_valid()) {
7270ret = OB_INVALID_ARGUMENT;
7271LOG_WARN("table_priv_key is invalid", K(table_priv_key), K(ret));
7272} else if (0 == priv_set && obj_priv_array.count() == 0) {
7273//do nothing
7274} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
7275LOG_WARN("failed to get schema guard", K(ret));
7276} else {
7277ObPrivSet new_priv = priv_set;
7278ObPrivSet table_priv_set = OB_PRIV_SET_EMPTY;
7279if (OB_FAIL(schema_guard.get_table_priv_set(table_priv_key, table_priv_set))) {
7280LOG_WARN("get table priv set failed", K(ret));
7281} else {
7282bool need_flush = true;
7283new_priv |= table_priv_set;
7284need_flush = (new_priv != table_priv_set);
7285bool is_directory = false;
7286if (obj_priv_array.count() > 0
7287&& static_cast<uint64_t>(ObObjectType::DIRECTORY) == obj_priv_key.obj_type_) {
7288is_directory = true;
7289}
7290
7291if (need_flush && !is_directory) {
7292int64_t new_schema_version = OB_INVALID_VERSION;
7293int64_t new_schema_version_ora = OB_INVALID_VERSION;
7294if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
7295LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
7296} else if (obj_priv_array.count() > 0) {
7297OZ (set_need_flush_ora(schema_guard, obj_priv_key, option, obj_priv_array,
7298new_obj_priv_array));
7299if (new_obj_priv_array.count() > 0) {
7300OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version_ora));
7301}
7302}
7303OZ (schema_sql_service->get_priv_sql_service().grant_table(
7304table_priv_key, new_priv, new_schema_version, ddl_stmt_str, trans,
7305new_obj_priv_array, option, obj_priv_key, new_schema_version_ora, true, false),
7306table_priv_key, ret, false);
7307} else if (obj_priv_array.count() > 0) {
7308OZ (set_need_flush_ora(schema_guard, obj_priv_key, option, obj_priv_array,
7309new_obj_priv_array));
7310if (new_obj_priv_array.count() > 0) {
7311int64_t new_schema_version_ora = OB_INVALID_VERSION;
7312OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version_ora));
7313OZ (schema_sql_service->get_priv_sql_service().grant_table_ora_only(
7314ddl_stmt_str, trans, new_obj_priv_array, option, obj_priv_key,
7315new_schema_version_ora, false, false),table_priv_key, ret);
7316}
7317}
7318}
7319}
7320
7321return ret;
7322}
7323
7324/* in: grantor, grantee, obj_type, obj_id
7325out: table_packed_privs
7326array of col_id which has col privs
7327array of col_packed_privs */
7328int ObDDLOperator::build_table_and_col_priv_array_for_revoke_all(
7329ObSchemaGetterGuard &schema_guard,
7330const ObObjPrivSortKey &obj_priv_key,
7331ObPackedObjPriv &packed_table_priv,
7332ObSEArray<uint64_t, 4> &col_id_array,
7333ObSEArray<ObPackedObjPriv, 4> &packed_privs_array)
7334{
7335int ret = OB_SUCCESS;
7336ObSEArray<const ObObjPriv *, 4> obj_priv_array;
7337uint64_t col_id = 0;
7338CK (obj_priv_key.is_valid());
7339OZ (schema_guard.get_obj_privs_in_grantor_ur_obj_id(obj_priv_key.tenant_id_,
7340obj_priv_key,
7341obj_priv_array));
7342for (int i = 0; i < obj_priv_array.count() && OB_SUCC(ret); i++) {
7343const ObObjPriv *obj_priv = obj_priv_array.at(i);
7344if (obj_priv != NULL) {
7345col_id = obj_priv->get_col_id();
7346if (col_id == OBJ_LEVEL_FOR_TAB_PRIV) {
7347packed_table_priv = obj_priv->get_obj_privs();
7348} else {
7349OZ (col_id_array.push_back(col_id));
7350OZ (packed_privs_array.push_back(obj_priv->get_obj_privs()));
7351}
7352}
7353}
7354
7355return ret;
7356}
7357
7358int ObDDLOperator::revoke_table_all(
7359ObSchemaGetterGuard &schema_guard,
7360const uint64_t tenant_id,
7361const ObObjPrivSortKey &obj_priv_key,
7362ObString &ddl_sql,
7363common::ObMySQLTransaction &trans)
7364{
7365int ret = OB_SUCCESS;
7366int64_t new_schema_version = OB_INVALID_VERSION;
7367share::ObPackedObjPriv packed_table_privs = 0;
7368ObRawObjPrivArray raw_priv_array;
7369ObRawObjPrivArray option_raw_array;
7370ObSEArray<uint64_t, 4> col_id_array;
7371ObSEArray<ObPackedObjPriv, 4> packed_privs_array;
7372ObObjPrivSortKey new_key = obj_priv_key;
7373ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
7374if (OB_ISNULL(schema_sql_service)) {
7375ret = OB_ERR_SYS;
7376LOG_ERROR("schama service_impl and schema manage must not null",
7377"schema_service_impl", schema_sql_service,
7378K(ret));
7379}
7380OZ (build_table_and_col_priv_array_for_revoke_all(schema_guard,
7381obj_priv_key,
7382packed_table_privs,
7383col_id_array,
7384packed_privs_array));
7385if (OB_SUCC(ret)) {
7386// 1. table-level permissions
7387if (packed_table_privs > 0) {
7388OZ (ObPrivPacker::raw_obj_priv_from_pack(packed_table_privs, raw_priv_array));
7389OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version));
7390OZ (schema_sql_service->get_priv_sql_service().revoke_table_ora(
7391new_key, raw_priv_array, new_schema_version, &ddl_sql, trans, true));
7392OZ (ObPrivPacker::raw_option_obj_priv_from_pack(packed_table_privs, option_raw_array));
7393OZ (revoke_obj_cascade(schema_guard, new_key.grantee_id_,
7394trans, new_key, option_raw_array));
7395}
7396// 2. column-level permissions
7397for (int i = 0; i < col_id_array.count() && OB_SUCC(ret); i++) {
7398new_key.col_id_ = col_id_array.at(i);
7399OZ (ObPrivPacker::raw_obj_priv_from_pack(packed_privs_array.at(i), raw_priv_array));
7400OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version));
7401OZ (schema_sql_service->get_priv_sql_service().revoke_table_ora(
7402new_key, raw_priv_array, new_schema_version, &ddl_sql, trans, true));
7403OZ (ObPrivPacker::raw_option_obj_priv_from_pack(packed_privs_array.at(i),
7404option_raw_array));
7405OZ (revoke_obj_cascade(schema_guard, new_key.grantee_id_,
7406trans, new_key, option_raw_array));
7407}
7408}
7409return ret;
7410}
7411
7412int ObDDLOperator::build_next_level_revoke_obj(
7413ObSchemaGetterGuard &schema_guard,
7414const ObObjPrivSortKey &old_key,
7415ObObjPrivSortKey &new_key,
7416ObIArray<const ObObjPriv *> &obj_privs)
7417{
7418int ret = OB_SUCCESS;
7419new_key = old_key;
7420new_key.grantor_id_ = new_key.grantee_id_;
7421OZ (schema_guard.get_obj_privs_in_grantor_obj_id(new_key.tenant_id_,
7422new_key,
7423obj_privs));
7424return ret;
7425}
7426
7427/* After processing the top-level revoke obj, then call this function to process revoke recursively.
74281. According to the obj key of the upper layer, change grantee to grantor and find new permissions that need to be reclaimed
74292. If there are permissions that need to be reclaimed, call revoke obj ora, if not, end.
74303. calling self. If a new grantee is found back to the original grantee, the end */
7431int ObDDLOperator::revoke_obj_cascade(
7432ObSchemaGetterGuard &schema_guard,
7433const uint64_t start_grantee_id, /* in: check circle */
7434common::ObMySQLTransaction &trans,
7435const ObObjPrivSortKey &old_key, /* in: old key */
7436ObRawObjPrivArray &old_array) /* in: privs that have grantable option */
7437{
7438int ret = OB_SUCCESS;
7439uint64_t tenant_id = old_key.tenant_id_;
7440int64_t new_schema_version = OB_INVALID_VERSION;
7441ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
7442ObObjPrivSortKey new_key;
7443ObRawObjPrivArray grantable_array;
7444ObRawObjPrivArray new_array;
7445ObSEArray<const ObObjPriv *, 4> obj_privs;
7446bool is_all = false;
7447if (old_array.count() > 0) {
7448OZ (build_next_level_revoke_obj(schema_guard, old_key, new_key, obj_privs));
7449/* If there are multiple, it means that this user has delegated to multiple other users */
7450if (obj_privs.count() > 0) {
7451ObPackedObjPriv old_p_list;
7452ObPackedObjPriv old_opt_p_list;
7453ObPackedObjPriv privs_revoke_this_level;
7454ObPackedObjPriv privs_revoke_next_level;
7455
7456OZ (ObPrivPacker::pack_raw_obj_priv_list(NO_OPTION, old_array, old_p_list));
7457OZ (ObPrivPacker::pack_raw_obj_priv_list(GRANT_OPTION, old_array, old_opt_p_list));
7458
7459for (int i = 0; OB_SUCC(ret) && i < obj_privs.count(); i++) {
7460const ObObjPriv* obj_priv = obj_privs.at(i);
7461if (obj_priv != NULL) {
7462/* 1. cross join grantee privs and grantor privs without option */
7463privs_revoke_this_level = old_p_list & obj_priv->get_obj_privs();
7464
7465if (privs_revoke_this_level > 0) {
7466/* 2. build new_Key */
7467new_key.grantee_id_ = obj_priv->get_grantee_id();
7468/* 2. build new array */
7469OZ (ObPrivPacker::raw_obj_priv_from_pack(privs_revoke_this_level, new_array));
7470OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version));
7471OX (is_all = (new_array.count() == old_array.count()));
7472OZ (schema_sql_service->get_priv_sql_service().revoke_table_ora(
7473new_key, new_array, new_schema_version, NULL, trans, is_all));
7474/* 3. new grantee is equ org grantee. end */
7475if (OB_SUCC(ret)) {
7476if (new_key.grantee_id_ == start_grantee_id) {
7477} else {
7478/* 3. decide privs to be revoked recursively */
7479privs_revoke_next_level = old_opt_p_list & obj_priv->get_obj_privs();
7480if (privs_revoke_next_level > 0) {
7481OZ (ObPrivPacker::raw_obj_priv_from_pack(privs_revoke_next_level, new_array));
7482OZ (revoke_obj_cascade(schema_guard, start_grantee_id, trans,
7483new_key, new_array));
7484}
7485}
7486}
7487}
7488}
7489}
7490}
7491}
7492return ret;
7493}
7494
7495/* Get all foreign keys of a user referencing the specified parent table */
7496int ObDDLOperator::build_fk_array_by_parent_table(
7497uint64_t tenant_id,
7498ObSchemaGetterGuard &schema_guard,
7499const ObString &grantee_name,
7500const ObString &db_name,
7501const ObString &tab_name,
7502ObIArray<ObDropForeignKeyArg> &drop_fk_array,
7503ObIArray<uint64_t> &ref_tab_id_array)
7504{
7505int ret = OB_SUCCESS;
7506const ObTableSchema *table_schema = NULL;
7507
7508OZ (schema_guard.get_table_schema(tenant_id, db_name, tab_name, false, table_schema));
7509if (OB_SUCC(ret)) {
7510if (NULL == table_schema) {
7511ret = OB_TABLE_NOT_EXIST;
7512} else {
7513uint64_t db_id = OB_INVALID_ID;
7514OZ (schema_guard.get_database_id(tenant_id, grantee_name, db_id));
7515/* Traverse all child tables referencing the parent table, if the owner of the child table is grantee, add drop fk array */
7516const ObIArray<ObForeignKeyInfo> &fk_array = table_schema->get_foreign_key_infos();
7517for (int i = 0; OB_SUCC(ret) && i < fk_array.count(); i++) {
7518const ObForeignKeyInfo &fk_info = fk_array.at(i);
7519const ObSimpleTableSchemaV2 *ref_table = NULL;
7520OZ (schema_guard.get_simple_table_schema(tenant_id, fk_info.child_table_id_, ref_table));
7521if (OB_SUCC(ret)) {
7522if (ref_table == NULL) {
7523ret = OB_TABLE_NOT_EXIST;
7524} else if (ref_table->get_database_id() == db_id) {
7525ObDropForeignKeyArg fk_arg;
7526fk_arg.foreign_key_name_ = fk_info.foreign_key_name_;
7527OZ (drop_fk_array.push_back(fk_arg));
7528OZ (ref_tab_id_array.push_back(ref_table->get_table_id()));
7529}
7530}
7531}
7532}
7533}
7534
7535return ret;
7536}
7537
7538int ObDDLOperator::drop_fk_cascade(
7539uint64_t tenant_id,
7540ObSchemaGetterGuard &schema_guard,
7541bool has_ref_priv,
7542bool has_no_cascade,
7543const ObString &grantee_name,
7544const ObString &parent_db_name,
7545const ObString &parent_tab_name,
7546ObMySQLTransaction &trans)
7547{
7548int ret = OB_SUCCESS;
7549if (has_ref_priv) {
7550ObSEArray<ObDropForeignKeyArg, 4> drop_fk_array;
7551ObSEArray<uint64_t, 4> ref_tab_id_array;
7552OZ (build_fk_array_by_parent_table(tenant_id,
7553schema_guard,
7554grantee_name,
7555parent_db_name,
7556parent_tab_name,
7557drop_fk_array,
7558ref_tab_id_array));
7559if (OB_SUCC(ret)) {
7560if (drop_fk_array.count() > 0) {
7561if (has_no_cascade) {
7562ret = OB_ERR_CASCADE_CONSTRAINTS_MUST_BE_SPECIFIED_TO_PERFORM_THIS_REVOKE;
7563} else {
7564for (int i = 0; OB_SUCC(ret) && i < drop_fk_array.count(); i++) {
7565const ObTableSchema *ref_tab = NULL;
7566const ObDropForeignKeyArg &drop_fk = drop_fk_array.at(i);
7567
7568OZ (schema_guard.get_table_schema(tenant_id,
7569ref_tab_id_array.at(i), ref_tab));
7570if (OB_SUCC(ret)) {
7571if (ref_tab == NULL) {
7572ret = OB_TABLE_NOT_EXIST;
7573} else {
7574const ObForeignKeyInfo *parent_table_mock_foreign_key_info = NULL;
7575OZ (alter_table_drop_foreign_key(*ref_tab, drop_fk, trans, parent_table_mock_foreign_key_info, ref_tab->get_in_offline_ddl_white_list()));
7576if (OB_SUCC(ret) && NULL != parent_table_mock_foreign_key_info) {
7577ret = OB_ERR_UNEXPECTED;
7578LOG_WARN("parent_table_mock_foreign_key_info in oracle mode is unexpected", K(ret));
7579}
7580}
7581}
7582}
7583}
7584}
7585}
7586}
7587
7588return ret;
7589}
7590
7591int ObDDLOperator::revoke_table(
7592const ObTablePrivSortKey &table_priv_key,
7593const ObPrivSet priv_set,
7594common::ObMySQLTransaction &trans,
7595const ObObjPrivSortKey &obj_priv_key,
7596const share::ObRawObjPrivArray &obj_priv_array,
7597const bool revoke_all_ora)
7598{
7599int ret = OB_SUCCESS;
7600const uint64_t tenant_id = table_priv_key.tenant_id_;
7601ObSchemaGetterGuard schema_guard;
7602ObSchemaService *schema_sql_service = schema_service_.get_schema_service();
7603if (OB_ISNULL(schema_sql_service)) {
7604ret = OB_ERR_SYS;
7605LOG_ERROR("schama service_impl and schema manage must not null",
7606"schema_service_impl", schema_sql_service,
7607K(ret));
7608} else if (!table_priv_key.is_valid()) {
7609ret = OB_INVALID_ARGUMENT;
7610LOG_WARN("db_priv_key is invalid", K(table_priv_key), K(ret));
7611} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
7612LOG_WARN("failed to get schema guard", K(ret));
7613} else {
7614ObPrivSet table_priv_set = OB_PRIV_SET_EMPTY;
7615if (OB_FAIL(schema_guard.get_table_priv_set(table_priv_key, table_priv_set))) {
7616LOG_WARN("get table priv set failed", K(ret));
7617} else if (OB_PRIV_SET_EMPTY == table_priv_set
7618&& !revoke_all_ora
7619&& obj_priv_array.count() == 0) {
7620ret = OB_ERR_CANNOT_REVOKE_PRIVILEGES_YOU_DID_NOT_GRANT;
7621LOG_WARN("No such grant to revoke", K(table_priv_key), K(ret));
7622} else if (0 == priv_set && obj_priv_array.count() == 0) {
7623// do-nothing
7624} else {
7625ObPrivSet new_priv = table_priv_set & (~priv_set);
7626/* If there is an intersection between the existing permissions and the permissions that require revoke */
7627if (table_priv_set & priv_set) {
7628ObSqlString ddl_stmt_str;
7629ObString ddl_sql;
7630const ObUserInfo *user_info = NULL;
7631ObNeedPriv need_priv;
7632share::ObRawObjPrivArray option_priv_array;
7633
7634need_priv.db_ = table_priv_key.db_;
7635need_priv.table_ = table_priv_key.table_;
7636need_priv.priv_level_ = OB_PRIV_TABLE_LEVEL;
7637need_priv.priv_set_ = table_priv_set & priv_set; //priv to revoke
7638int64_t new_schema_version = OB_INVALID_VERSION;
7639int64_t new_schema_version_ora = OB_INVALID_VERSION;
7640bool is_all = false;
7641bool has_ref_priv = false;
7642if (OB_FAIL(check_obj_privs_exists(schema_guard, obj_priv_key,
7643obj_priv_array, option_priv_array, is_all))) {
7644LOG_WARN("priv not exits", K(obj_priv_array), K(ret));
7645} else if (OB_FAIL(schema_guard.get_user_info(tenant_id, table_priv_key.user_id_, user_info))) {
7646LOG_WARN("get user info failed", K(table_priv_key), K(ret));
7647} else if (OB_ISNULL(user_info)) {
7648ret = OB_ERR_UNEXPECTED;
7649LOG_WARN("user not exist", K(table_priv_key), K(ret));
7650} else if (OB_FAIL(drop_fk_cascade(tenant_id,
7651schema_guard,
7652has_ref_priv,
7653true, /* has no cascade */
7654user_info->get_user_name_str(), /* grantee name */
7655table_priv_key.db_,
7656table_priv_key.table_,
7657trans))) {
7658LOG_WARN("drop fk cascase failed", K(table_priv_key), K(ret));
7659} else if (OB_FAIL(ObDDLSqlGenerator::gen_table_priv_sql(
7660ObAccountArg(user_info->get_user_name_str(), user_info->get_host_name_str()),
7661need_priv,
7662false, /*is_grant*/
7663ddl_stmt_str))) {
7664LOG_WARN("gen_table_priv_sql failed", K(ret), K(need_priv));
7665} else if (FALSE_IT(ddl_sql = ddl_stmt_str.string())) {
7666} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id,
7667new_schema_version))) {
7668LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
7669} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id,
7670new_schema_version_ora))) {
7671LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
7672} else if (OB_FAIL(schema_sql_service->get_priv_sql_service().revoke_table(
7673table_priv_key, new_priv, new_schema_version, &ddl_sql, trans,
7674new_schema_version_ora, obj_priv_key, obj_priv_array, is_all))) {
7675LOG_WARN("Failed to revoke table", K(table_priv_key), K(ret));
7676} else {
7677OZ (revoke_obj_cascade(schema_guard, obj_priv_key.grantee_id_,
7678trans, obj_priv_key, option_priv_array));
7679}
7680// In revoke all statement, if you have permission, it will come here, and the content of mysql will be processed first.
7681if (OB_SUCC(ret) && revoke_all_ora) {
7682OZ (revoke_table_all(schema_guard, tenant_id, obj_priv_key, ddl_sql, trans));
7683}
7684} else {
7685ObSqlString ddl_stmt_str;
7686ObString ddl_sql;
7687const ObUserInfo *user_info = NULL;
7688int64_t new_schema_version = OB_INVALID_VERSION;
7689share::ObRawObjPrivArray option_priv_array;
7690ObRawObjPrivArray raw_priv_array;
7691ObArray<bool> is_all;
7692ObArray<ObObjPrivSortKey> priv_key_array;
7693ObArray<ObPackedObjPriv> packed_privs_array;
7694ObRawObjPrivArray option_raw_array;
7695// In oracle mode, need to check revoke permission exists, only need to reclaim oracle permission
7696// Due to the existence of column permissions, one obj_priv_key is expanded into multiple,
7697// and each key represents a column
7698OZ (check_obj_privs_exists_including_col_privs(schema_guard, obj_priv_key,
7699obj_priv_array, priv_key_array, packed_privs_array, is_all));
7700OZ (schema_guard.get_user_info(tenant_id, table_priv_key.user_id_, user_info));
7701if (OB_SUCC(ret) && user_info == NULL) {
7702ret = OB_ERR_UNEXPECTED;
7703LOG_WARN("user not exist", K(table_priv_key), K(ret));
7704}
7705
7706OZ (ObDDLSqlGenerator::gen_table_priv_sql_ora(
7707ObAccountArg(user_info->get_user_name_str(), user_info->get_host_name_str()),
7708table_priv_key,
7709revoke_all_ora,
7710obj_priv_array,
7711false,
7712ddl_stmt_str));
7713OX (ddl_sql = ddl_stmt_str.string());
7714if (OB_SUCC(ret)) {
7715if (revoke_all_ora) {
7716OZ (revoke_table_all(schema_guard, tenant_id, obj_priv_key, ddl_sql, trans));
7717} else if (obj_priv_array.count() > 0) {
7718// Revoke table permissions and column permissions one by one
7719for (int i = 0; OB_SUCC(ret) && i < priv_key_array.count(); ++i) {
7720const ObObjPrivSortKey &priv_key = priv_key_array.at(i);
7721OZ (ObPrivPacker::raw_obj_priv_from_pack(packed_privs_array.at(i), raw_priv_array));
7722OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version));
7723OZ (schema_sql_service->get_priv_sql_service().revoke_table_ora(
7724priv_key, raw_priv_array, new_schema_version,
7725&ddl_sql, trans, is_all.at(i)));
7726OZ (ObPrivPacker::raw_option_obj_priv_from_pack(packed_privs_array.at(i),
7727option_raw_array));
7728OZ (revoke_obj_cascade(schema_guard, priv_key.grantee_id_,
7729trans, priv_key, option_raw_array));
7730}
7731}
7732}
7733}
7734}
7735}
7736return ret;
7737}
7738
7739int ObDDLOperator::get_flush_role_array(
7740const uint64_t option,
7741const common::ObIArray<uint64_t> &org_role_ids,
7742bool &need_flush,
7743bool is_grant,
7744const ObUserInfo &user_info,
7745common::ObIArray<uint64_t> &role_ids)
7746{
7747int ret = OB_SUCCESS;
7748need_flush = false;
7749if (org_role_ids.count() > 0) {
7750if (is_grant) {
7751for (int64_t i = 0; OB_SUCC(ret) && i < org_role_ids.count(); ++i) {
7752const uint64_t role_id = org_role_ids.at(i);
7753if (!user_info.role_exists(role_id, option)) {
7754need_flush = true;
7755OZ (role_ids.push_back(role_id));
7756}
7757}
7758} else {
7759need_flush = true;
7760OZ (role_ids.assign(org_role_ids));
7761}
7762}
7763return ret;
7764}
7765
7766int ObDDLOperator::grant_revoke_role(
7767const uint64_t tenant_id,
7768const ObUserInfo &user_info,
7769const common::ObIArray<uint64_t> &org_role_ids,
7770// When specified_role_info is not empty, use it as role_info instead of reading it in the schema.
7771const ObUserInfo *specified_role_info,
7772common::ObMySQLTransaction &trans,
7773const bool log_operation,
7774const bool is_grant,
7775const uint64_t option)
7776{
7777int ret = OB_SUCCESS;
7778ObSchemaService *schema_service = schema_service_.get_schema_service();
7779ObSchemaGetterGuard schema_guard;
7780int64_t new_schema_version = OB_INVALID_VERSION;
7781ObString ddl_sql;
7782
7783if (OB_ISNULL(schema_service)) {
7784ret = OB_ERR_SYS;
7785LOG_ERROR("schema_service must not null", K(ret));
7786} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
7787LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
7788} else if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
7789LOG_WARN("failed to get schema guard", K(ret));
7790} else {
7791common::ObSEArray<uint64_t, 8> role_ids;
7792bool need_flush = false;
7793OZ (get_flush_role_array(option,
7794org_role_ids,
7795need_flush,
7796is_grant,
7797user_info,
7798role_ids));
7799if (OB_SUCC(ret) && need_flush) {
7800common::ObSqlString sql_string;
7801if (OB_FAIL(sql_string.append_fmt(is_grant ? "GRANT ": "REVOKE "))) {
7802LOG_WARN("append sql failed", K(ret));
7803} else if (OB_NOT_NULL(specified_role_info)) {
7804// Use single specified role info
7805if (OB_FAIL(sql_string.append_fmt("%s", specified_role_info->get_user_name()))) {
7806LOG_WARN("append sql failed", K(ret));
7807}
7808} else {
7809// Use role info obtained from schema
7810for (int64_t i = 0; OB_SUCC(ret) && i < role_ids.count(); ++i) {
7811const uint64_t role_id = role_ids.at(i);
7812const ObUserInfo *role_info = NULL;
7813if (0 != i) {
7814if (OB_FAIL(sql_string.append_fmt(","))) {
7815LOG_WARN("append sql failed", K(ret));
7816}
7817}
7818if (FAILEDx(schema_guard.get_user_info(tenant_id, role_id, role_info))) {
7819LOG_WARN("Failed to get role info", K(ret), K(tenant_id), K(role_id));
7820} else if (NULL == role_info) {
7821ret = OB_ERR_UNEXPECTED;
7822LOG_WARN("role doesn't exist", K(ret), K(role_id));
7823} else if (OB_FAIL(sql_string.append_fmt("%s", role_info->get_user_name()))) {
7824LOG_WARN("append sql failed", K(ret));
7825}
7826}
7827}
7828if (OB_SUCC(ret)) {
7829if (OB_FAIL(sql_string.append_fmt(is_grant ? " TO %s": " FROM %s", user_info.get_user_name()))) {
7830LOG_WARN("append sql failed", K(ret));
7831} else if (is_grant && option != NO_OPTION && OB_FAIL(sql_string.append_fmt(
7832" WITH ADMIN OPTION"))) {
7833LOG_WARN("append sql failed", K(ret));
7834} else {
7835ddl_sql = sql_string.string();
7836LOG_WARN("wang sql ", K(ddl_sql), K(sql_string));
7837}
7838}
7839if (OB_SUCC(ret)) {
7840if (OB_FAIL(schema_service->get_priv_sql_service().grant_revoke_role(tenant_id,
7841user_info,
7842role_ids,
7843specified_role_info,
7844new_schema_version,
7845log_operation ? &ddl_sql : NULL,
7846trans,
7847is_grant,
7848schema_guard,
7849option))) {
7850LOG_WARN("Failed to revoke role", K(user_info), K(ret));
7851}
7852}
7853}
7854}
7855
7856return ret;
7857}
7858
7859int ObDDLOperator::get_flush_priv_array(
7860const uint64_t option,
7861const share::ObRawPrivArray &priv_array,
7862const ObSysPriv *sys_priv,
7863share::ObRawPrivArray &new_priv_array,
7864bool &need_flush,
7865const bool is_grant,
7866const ObUserInfo &user_info)
7867{
7868int ret = OB_SUCCESS;
7869int64_t raw_priv = 0;
7870bool exists = false;
7871
7872need_flush = FALSE;
7873if (is_grant) {
7874if (sys_priv == NULL) {
7875need_flush = true;
7876OZ (new_priv_array.assign(priv_array));
7877} else {
7878ARRAY_FOREACH(priv_array, idx) {
7879raw_priv = priv_array.at(idx);
7880OZ (ObOraPrivCheck::raw_sys_priv_exists(option,
7881raw_priv,
7882sys_priv->get_priv_array(),
7883exists));
7884if (OB_SUCC(ret) && !exists) {
7885need_flush = true;
7886OZ (new_priv_array.push_back(raw_priv));
7887}
7888}
7889}
7890}
7891else {
7892need_flush = true;
7893if (sys_priv == NULL) {
7894ret = OB_ERR_SYSTEM_PRIVILEGES_NOT_GRANTED_TO;
7895LOG_USER_ERROR(OB_ERR_SYSTEM_PRIVILEGES_NOT_GRANTED_TO,
7896user_info.get_user_name_str().length(),
7897user_info.get_user_name_str().ptr());
7898LOG_WARN("revoke sys priv fail, sys priv not exists", K(priv_array), K(ret));
7899} else {
7900ARRAY_FOREACH(priv_array, idx) {
7901raw_priv = priv_array.at(idx);
7902OZ (ObOraPrivCheck::raw_sys_priv_exists(option,
7903raw_priv,
7904sys_priv->get_priv_array(),
7905exists));
7906if (OB_SUCC(ret) && !exists) {
7907ret = OB_ERR_SYSTEM_PRIVILEGES_NOT_GRANTED_TO;
7908LOG_USER_ERROR(OB_ERR_SYSTEM_PRIVILEGES_NOT_GRANTED_TO,
7909user_info.get_user_name_str().length(),
7910user_info.get_user_name_str().ptr());
7911LOG_WARN("revoke sys priv fail, sys priv not exists", K(priv_array), K(ret));
7912}
7913OZ (new_priv_array.push_back(raw_priv));
7914}
7915}
7916}
7917return ret;
7918}
7919
7920int ObDDLOperator::grant_sys_priv_to_ur(
7921const uint64_t tenant_id,
7922const uint64_t grantee_id,
7923const ObSysPriv* sys_priv,
7924const uint64_t option,
7925const ObRawPrivArray priv_array,
7926common::ObMySQLTransaction &trans,
7927const bool is_grant,
7928const common::ObString *ddl_stmt_str,
7929ObSchemaGetterGuard &schema_guard)
7930{
7931int ret = OB_SUCCESS;
7932ObSchemaService *schema_service = schema_service_.get_schema_service();
7933int64_t new_schema_version = OB_INVALID_VERSION;
7934ObRawPrivArray new_priv_array;
7935bool need_flush;
7936const ObUserInfo *user_info = NULL;
7937if (OB_ISNULL(schema_service)) {
7938ret = OB_ERR_SYS;
7939LOG_ERROR("schema_service must not null", K(ret));
7940}
7941OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version), tenant_id);
7942OZ (schema_guard.get_user_info(tenant_id, grantee_id, user_info));
7943OZ (get_flush_priv_array(option,
7944priv_array,
7945sys_priv,
7946new_priv_array,
7947need_flush,
7948is_grant,
7949*user_info));
7950if (OB_SUCC(ret) && need_flush) {
7951if (is_grant) {
7952CK (new_priv_array.count() > 0);
7953OZ (schema_service->get_priv_sql_service().grant_sys_priv_to_ur(tenant_id,
7954grantee_id,
7955option,
7956new_priv_array,
7957new_schema_version,
7958ddl_stmt_str,
7959trans,
7960is_grant,
7961false));
7962} else {
7963int n_cnt = 0;
7964bool revoke_all_flag = false;
7965/* revoke */
7966CK(sys_priv != NULL);
7967OZ (ObPrivPacker::get_total_privs(sys_priv->get_priv_array(), n_cnt));
7968revoke_all_flag = (n_cnt == new_priv_array.count());
7969/* revoke all */
7970OZ (schema_service->get_priv_sql_service().grant_sys_priv_to_ur(tenant_id,
7971grantee_id,
7972option,
7973new_priv_array,
7974new_schema_version,
7975ddl_stmt_str,
7976trans,
7977is_grant,
7978revoke_all_flag),
7979tenant_id, grantee_id, new_priv_array, is_grant, revoke_all_flag);
7980}
7981}
7982return ret;
7983}
7984
7985//----End of functions for managing privileges----
7986
7987//----Functions for managing outlines----
7988int ObDDLOperator::create_outline(ObOutlineInfo &outline_info,
7989ObMySQLTransaction &trans,
7990const ObString *ddl_stmt_str/*=NULL*/)
7991{
7992int ret = OB_SUCCESS;
7993uint64_t new_outline_id = OB_INVALID_ID;
7994const uint64_t tenant_id = outline_info.get_tenant_id();
7995int64_t new_schema_version = OB_INVALID_VERSION;
7996ObSchemaService *schema_service = schema_service_.get_schema_service();
7997
7998if (OB_ISNULL(schema_service)) {
7999ret = OB_ERR_SYS;
8000LOG_ERROR("schema_service must not null", K(ret));
8001} // else if (!outline_info.is_valid()) {
8002// ret = OB_INVALID_ARGUMENT;
8003// LOG_ERROR("outline is invalid", K(outline_info), K(ret));
8004// }
8005else if (OB_FAIL(schema_service->fetch_new_outline_id(tenant_id, new_outline_id))) {
8006LOG_WARN("failed to fetch new_outline_id", K(tenant_id), K(ret));
8007} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8008LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8009} else {
8010outline_info.set_outline_id(new_outline_id);
8011outline_info.set_schema_version(new_schema_version);
8012if (OB_FAIL(schema_service->get_outline_sql_service().insert_outline(
8013outline_info, trans, ddl_stmt_str))) {
8014LOG_WARN("insert outline info failed", K(outline_info), K(ret));
8015}
8016}
8017return ret;
8018}
8019
8020int ObDDLOperator::replace_outline(ObOutlineInfo &outline_info,
8021ObMySQLTransaction &trans,
8022const ObString *ddl_stmt_str/*=NULL*/)
8023{
8024int ret = OB_SUCCESS;
8025const uint64_t tenant_id = outline_info.get_tenant_id();
8026int64_t new_schema_version = OB_INVALID_VERSION;
8027ObSchemaService *schema_service = schema_service_.get_schema_service();
8028if (OB_ISNULL(schema_service)) {
8029ret = OB_ERR_SYS;
8030LOG_ERROR("schema_service is NULL", K(ret));
8031} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8032LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8033} else {
8034outline_info.set_schema_version(new_schema_version);
8035if (OB_FAIL(schema_service->get_outline_sql_service().replace_outline(
8036outline_info, trans, ddl_stmt_str))) {
8037LOG_WARN("replace outline info failed", K(outline_info), K(ret));
8038} else {/*do nothing*/}
8039}
8040return ret;
8041}
8042
8043int ObDDLOperator::alter_outline(ObOutlineInfo &outline_info,
8044ObMySQLTransaction &trans,
8045const ObString *ddl_stmt_str/*=NULL*/)
8046{
8047int ret = OB_SUCCESS;
8048const uint64_t tenant_id = outline_info.get_tenant_id();
8049int64_t new_schema_version = OB_INVALID_VERSION;
8050ObSchemaService *schema_service = schema_service_.get_schema_service();
8051if (OB_ISNULL(schema_service)) {
8052ret = OB_ERR_SYS;
8053LOG_ERROR("schema_service is NULL", K(ret));
8054} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8055LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8056} else {
8057outline_info.set_schema_version(new_schema_version);
8058if (OB_FAIL(schema_service->get_outline_sql_service().alter_outline(
8059outline_info, trans, ddl_stmt_str))) {
8060LOG_WARN("alter outline failed", K(outline_info), K(ret));
8061} else {/*do nothing*/}
8062}
8063return ret;
8064}
8065
8066int ObDDLOperator::drop_outline(const uint64_t tenant_id,
8067const uint64_t database_id,
8068const uint64_t outline_id,
8069ObMySQLTransaction &trans,
8070const ObString *ddl_stmt_str/*=NULL*/)
8071{
8072int ret = OB_SUCCESS;
8073int64_t new_schema_version = OB_INVALID_VERSION;
8074ObSchemaService *schema_service = schema_service_.get_schema_service();
8075if (OB_UNLIKELY(OB_INVALID_ID == tenant_id || OB_INVALID_ID == database_id
8076|| OB_INVALID_ID == outline_id)) {
8077ret = OB_INVALID_ARGUMENT;
8078LOG_WARN("invalid arguments", K(tenant_id), K(database_id), K(outline_id), K(ret));
8079} else if (OB_ISNULL(schema_service)) {
8080ret = OB_ERR_SYS;
8081LOG_ERROR("schema_service must not null", K(ret));
8082} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8083LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8084} else if (OB_FAIL(schema_service->get_outline_sql_service().delete_outline(
8085tenant_id,
8086database_id,
8087outline_id,
8088new_schema_version,
8089trans,
8090ddl_stmt_str))) {
8091LOG_WARN("drop outline failed", K(tenant_id), K(outline_id), KT(outline_id), K(ret));
8092} else {/*do nothing*/}
8093return ret;
8094}
8095
8096//----Functions for managing dblinks----
8097int ObDDLOperator::create_dblink(ObDbLinkBaseInfo &dblink_info,
8098ObMySQLTransaction &trans,
8099const ObString *ddl_stmt_str/*=NULL*/)
8100{
8101int ret = OB_SUCCESS;
8102ObSchemaService *schema_service = schema_service_.get_schema_service();
8103uint64_t tenant_id = dblink_info.get_tenant_id();
8104uint64_t new_dblink_id = OB_INVALID_ID;
8105int64_t schema_version = -1;
8106int64_t is_deleted = 0;
8107if (OB_ISNULL(schema_service)) {
8108ret = OB_ERR_SYS;
8109LOG_ERROR("schema_service must not null", K(ret));
8110} else if (OB_FAIL(schema_service->fetch_new_dblink_id(tenant_id, new_dblink_id))) {
8111LOG_WARN("failed to fetch new_dblink_id", K(tenant_id), K(ret));
8112} else if (FALSE_IT(dblink_info.set_dblink_id(new_dblink_id))) {
8113// nothing.
8114} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, schema_version))) {
8115LOG_WARN("failed to gen new_schema_version", K(tenant_id), K(ret));
8116} else if (FALSE_IT(dblink_info.set_schema_version(schema_version))) {
8117// nothing.
8118} else if (OB_FAIL(schema_service->get_dblink_sql_service().insert_dblink(
8119dblink_info, is_deleted, trans, ddl_stmt_str))) {
8120LOG_WARN("failed to insert dblink", K(dblink_info.get_dblink_name()), K(ret));
8121}
8122return ret;
8123}
8124
8125int ObDDLOperator::drop_dblink(ObDbLinkBaseInfo &dblink_info,
8126ObMySQLTransaction &trans,
8127const common::ObString *ddl_stmt_str/*=NULL*/)
8128{
8129int ret = OB_SUCCESS;
8130ObSchemaService *schema_service = schema_service_.get_schema_service();
8131uint64_t tenant_id = dblink_info.get_tenant_id();
8132uint64_t dblink_id = dblink_info.get_dblink_id();
8133int64_t schema_version = -1;
8134int64_t is_deleted = 1;
8135if (OB_ISNULL(schema_service)) {
8136ret = OB_ERR_SYS;
8137LOG_ERROR("schema_service must not null", K(ret));
8138} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, schema_version))) {
8139LOG_WARN("failed to gen new_schema_version", K(tenant_id), K(ret));
8140} else if (FALSE_IT(dblink_info.set_schema_version(schema_version))) {
8141// nothing.
8142} else if (OB_FAIL(schema_service->get_dblink_sql_service().insert_dblink(
8143dblink_info, is_deleted, trans, ddl_stmt_str))) {
8144LOG_WARN("failed to insert dblink", K(dblink_info.get_dblink_name()), K(ret));
8145} else if (OB_FAIL(schema_service->get_dblink_sql_service().delete_dblink(
8146tenant_id, dblink_id, trans))) {
8147LOG_WARN("failed to delete dblink", K(dblink_info.get_dblink_name()), K(ret));
8148}
8149return ret;
8150}
8151//----End of functions for managing dblinks----
8152
8153//----Functions for managing synonym----
8154int ObDDLOperator::create_synonym(ObSynonymInfo &synonym_info,
8155ObMySQLTransaction &trans,
8156const ObString *ddl_stmt_str/*=NULL*/)
8157{
8158int ret = OB_SUCCESS;
8159uint64_t new_synonym_id = OB_INVALID_ID;
8160const uint64_t tenant_id = synonym_info.get_tenant_id();
8161int64_t new_schema_version = OB_INVALID_VERSION;
8162ObSchemaService *schema_service = schema_service_.get_schema_service();
8163if (OB_ISNULL(schema_service)) {
8164ret = OB_ERR_SYS;
8165LOG_ERROR("schema_service must not null", K(ret));
8166} else if (OB_FAIL(schema_service->fetch_new_synonym_id(tenant_id, new_synonym_id))) {
8167LOG_WARN("failed to fetch new_synonym_id", K(tenant_id), K(ret));
8168} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8169LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8170} else {
8171synonym_info.set_synonym_id(new_synonym_id);
8172synonym_info.set_schema_version(new_schema_version);
8173if (OB_FAIL(schema_service->get_synonym_sql_service().insert_synonym(
8174synonym_info, &trans, ddl_stmt_str))) {
8175LOG_WARN("insert synonym info failed", K(synonym_info.get_synonym_name_str()), K(ret));
8176}
8177}
8178return ret;
8179}
8180
8181int ObDDLOperator::replace_synonym(ObSynonymInfo &synonym_info,
8182ObMySQLTransaction &trans,
8183const ObString *ddl_stmt_str/*=NULL*/)
8184{
8185int ret = OB_SUCCESS;
8186const uint64_t tenant_id = synonym_info.get_tenant_id();
8187int64_t new_schema_version = OB_INVALID_VERSION;
8188ObSchemaService *schema_service = schema_service_.get_schema_service();
8189if (OB_ISNULL(schema_service)) {
8190ret = OB_ERR_SYS;
8191LOG_ERROR("schema_service is NULL", K(ret));
8192} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8193LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8194} else {
8195synonym_info.set_schema_version(new_schema_version);
8196if (OB_FAIL(schema_service->get_synonym_sql_service().replace_synonym(
8197synonym_info, &trans, ddl_stmt_str))) {
8198LOG_WARN("replace synonym info failed", K(synonym_info.get_synonym_name_str()), K(ret));
8199} else {/*do nothing*/}
8200}
8201return ret;
8202}
8203
8204int ObDDLOperator::drop_synonym(const uint64_t tenant_id,
8205const uint64_t database_id,
8206const uint64_t synonym_id,
8207ObMySQLTransaction &trans,
8208const ObString *ddl_stmt_str/*=NULL*/)
8209{
8210int ret = OB_SUCCESS;
8211ObSchemaService *schema_service = schema_service_.get_schema_service();
8212int64_t new_schema_version = OB_INVALID_VERSION;
8213if (OB_UNLIKELY(OB_INVALID_ID == tenant_id
8214|| OB_INVALID_ID == synonym_id)) {
8215ret = OB_INVALID_ARGUMENT;
8216LOG_WARN("invalid arguments", K(tenant_id), K(database_id), K(synonym_id), K(ret));
8217} else if (OB_ISNULL(schema_service)) {
8218ret = OB_ERR_SYS;
8219LOG_ERROR("schema_service must not null", K(ret));
8220} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8221LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8222} else if (OB_FAIL(schema_service->get_synonym_sql_service().delete_synonym(
8223tenant_id,
8224database_id,
8225synonym_id,
8226new_schema_version,
8227&trans,
8228ddl_stmt_str))) {
8229LOG_WARN("drop synonym failed", K(tenant_id), K(synonym_id), KT(synonym_id), K(ret));
8230} else {/*do nothing*/}
8231return ret;
8232}
8233
8234
8235//----End of functions for managing outlines----
8236
8237int ObDDLOperator::create_routine(ObRoutineInfo &routine_info,
8238ObMySQLTransaction &trans,
8239ObErrorInfo &error_info,
8240ObIArray<ObDependencyInfo> &dep_infos,
8241const ObString *ddl_stmt_str)
8242{
8243int ret = OB_SUCCESS;
8244uint64_t new_routine_id = OB_INVALID_ID;
8245const uint64_t tenant_id = routine_info.get_tenant_id();
8246int64_t new_schema_version = OB_INVALID_VERSION;
8247ObSchemaService *schema_service = schema_service_.get_schema_service();
8248
8249if (OB_ISNULL(schema_service)) {
8250ret = OB_ERR_SYS;
8251LOG_ERROR("schema_service must not null", K(ret));
8252} else if (OB_SYS_TENANT_ID == tenant_id
8253&& OB_FAIL(schema_service->fetch_new_sys_pl_object_id(tenant_id, new_routine_id))) {
8254LOG_WARN("failed to fetch new_routine_id", K(tenant_id), K(ret));
8255} else if (OB_SYS_TENANT_ID != tenant_id
8256&& OB_FAIL(schema_service->fetch_new_routine_id(tenant_id, new_routine_id))) {
8257LOG_WARN("failed to fetch new_routine_id", K(tenant_id), K(ret));
8258} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8259LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8260} else {
8261routine_info.set_routine_id(new_routine_id);
8262routine_info.set_schema_version(new_schema_version);
8263if (OB_FAIL(schema_service->get_routine_sql_service().create_routine(routine_info,
8264&trans,
8265ddl_stmt_str))) {
8266LOG_WARN("insert routine info failed", K(routine_info), K(ret));
8267}
8268}
8269OZ (insert_dependency_infos(trans, dep_infos, tenant_id, routine_info.get_routine_id(),
8270routine_info.get_schema_version(),
8271routine_info.get_owner_id()));
8272
8273// add audit in routine if necessary
8274if (OB_SUCC(ret)) {
8275int64_t new_schema_version = OB_INVALID_VERSION;
8276ObArray<const ObSAuditSchema *> audits;
8277ObSchemaGetterGuard schema_guard;
8278if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
8279LOG_WARN("failed to get schema guard", K(ret));
8280} else if (OB_FAIL(schema_guard.get_audit_schema_in_owner(tenant_id,
8281AUDIT_OBJ_DEFAULT,
8282OB_AUDIT_MOCK_USER_ID,
8283audits))) {
8284LOG_WARN("get get_audit_schema_in_owner failed", K(tenant_id), K(ret));
8285} else if (!audits.empty()) {
8286ObSchemaService *schema_service = schema_service_.get_schema_service();
8287common::ObSqlString public_sql_string;
8288ObSAuditSchema new_audit_schema;
8289for (int64_t i = 0; OB_SUCC(ret) && i < audits.count(); ++i) {
8290uint64_t new_audit_id = common::OB_INVALID_ID;
8291const ObSAuditSchema *audit_schema = audits.at(i);
8292if (OB_ISNULL(audit_schema)) {
8293ret = OB_ERR_UNEXPECTED;
8294LOG_WARN("audit_schema is NULL", K(ret));
8295} else if (!audit_schema->is_access_operation_for_procedure()) {
8296continue;
8297} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))){
8298LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8299} else if (OB_FAIL(schema_service->fetch_new_audit_id(tenant_id, new_audit_id))) {
8300LOG_WARN("Failed to fetch new_audit_id", K(ret));
8301} else if (OB_FAIL(new_audit_schema.assign(*audit_schema))) {
8302LOG_WARN("fail to assign audit schema", KR(ret));
8303} else {
8304new_audit_schema.set_schema_version(new_schema_version);
8305new_audit_schema.set_audit_id(new_audit_id);
8306new_audit_schema.set_audit_type(AUDIT_PROCEDURE);
8307new_audit_schema.set_owner_id(routine_info.get_routine_id());
8308if (OB_FAIL(schema_service->get_audit_sql_service().handle_audit_metainfo(
8309new_audit_schema,
8310AUDIT_MT_ADD,
8311false,
8312new_schema_version,
8313NULL,
8314trans,
8315public_sql_string))) {
8316LOG_WARN("add audit_schema failed", K(new_audit_schema), K(ret));
8317} else {
8318LOG_INFO("succ to add audit_schema from routine", K(new_audit_schema));
8319}
8320}
8321}
8322}
8323}
8324if (OB_SUCC(ret)) {
8325if (OB_FAIL(error_info.handle_error_info(trans, &routine_info))) {
8326LOG_WARN("insert create routine error info failed.", K(ret), K(error_info));
8327}
8328}
8329return ret;
8330}
8331
8332int ObDDLOperator::replace_routine(ObRoutineInfo &routine_info,
8333const ObRoutineInfo *old_routine_info,
8334ObMySQLTransaction &trans,
8335ObErrorInfo &error_info,
8336ObIArray<ObDependencyInfo> &dep_infos,
8337const ObString *ddl_stmt_str)
8338{
8339int ret = OB_SUCCESS;
8340ObSchemaService *schema_service = schema_service_.get_schema_service();
8341CK(OB_NOT_NULL(schema_service));
8342CK(OB_NOT_NULL(old_routine_info));
8343const uint64_t tenant_id = routine_info.get_tenant_id();
8344int64_t del_param_schema_version = OB_INVALID_VERSION;
8345int64_t new_schema_version = OB_INVALID_VERSION;
8346if (old_routine_info->get_routine_params().count() > 0) {
8347if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, del_param_schema_version))) {
8348LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8349}
8350}
8351
8352if (OB_FAIL(ret)) {
8353} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8354LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8355} else {
8356routine_info.set_routine_id(old_routine_info->get_routine_id());
8357routine_info.set_schema_version(new_schema_version);
8358}
8359OZ (ObDependencyInfo::delete_schema_object_dependency(trans, old_routine_info->get_tenant_id(),
8360old_routine_info->get_routine_id(),
8361new_schema_version,
8362old_routine_info->get_object_type()));
8363if (OB_SUCC(ret)
8364&& OB_FAIL(schema_service->get_routine_sql_service().replace_routine(routine_info,
8365old_routine_info,
8366del_param_schema_version,
8367&trans,
8368ddl_stmt_str))) {
8369LOG_WARN("replace routine info failed", K(routine_info), K(ret));
8370}
8371
8372OZ (insert_dependency_infos(trans, dep_infos, tenant_id, routine_info.get_routine_id(),
8373routine_info.get_schema_version(),
8374routine_info.get_owner_id()));
8375if (OB_SUCC(ret)) {
8376if (OB_FAIL(error_info.handle_error_info(trans, &routine_info))) {
8377LOG_WARN("replace routine error info failed.", K(ret), K(error_info));
8378}
8379}
8380return ret;
8381}
8382
8383int ObDDLOperator::alter_routine(const ObRoutineInfo &routine_info,
8384ObMySQLTransaction &trans,
8385ObErrorInfo &error_info,
8386const ObString *ddl_stmt_str)
8387{
8388int ret = OB_SUCCESS;
8389UNUSEDx(ddl_stmt_str);
8390if (OB_FAIL(error_info.handle_error_info(trans, &routine_info))) {
8391LOG_WARN("drop routine error info failed.", K(ret), K(error_info));
8392}
8393return ret;
8394}
8395
8396int ObDDLOperator::drop_routine(const ObRoutineInfo &routine_info,
8397ObMySQLTransaction &trans,
8398ObErrorInfo &error_info,
8399const ObString *ddl_stmt_str)
8400{
8401int ret = OB_SUCCESS;
8402const uint64_t tenant_id = routine_info.get_tenant_id();
8403int64_t new_schema_version = OB_INVALID_VERSION;
8404ObSchemaService *schema_service = schema_service_.get_schema_service();
8405
8406if (OB_ISNULL(schema_service)) {
8407ret = OB_ERR_SYS;
8408LOG_ERROR("schema_service must not null", K(ret));
8409} else if (OB_FAIL(drop_obj_privs(tenant_id, routine_info.get_routine_id(),
8410static_cast<uint64_t>(routine_info.get_routine_type()),
8411trans))) {
8412LOG_WARN("fail to drop_obj_privs", K(ret), K(tenant_id));
8413} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8414LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8415} else if (OB_FAIL(schema_service->get_routine_sql_service().drop_routine(
8416routine_info, new_schema_version, trans, ddl_stmt_str))) {
8417LOG_WARN("drop routine info failed", K(routine_info), K(ret));
8418}
8419OZ (ObDependencyInfo::delete_schema_object_dependency(trans, routine_info.get_tenant_id(),
8420routine_info.get_routine_id(),
8421new_schema_version,
8422routine_info.get_object_type()));
8423if (OB_SUCC(ret)) {
8424if (OB_FAIL(error_info.handle_error_info(trans, &routine_info))) {
8425LOG_WARN("drop routine error info failed.", K(ret), K(error_info));
8426}
8427}
8428
8429// delete audit in procedure
8430if (OB_SUCC(ret)) {
8431ObArray<const ObSAuditSchema *> audits;
8432ObSchemaGetterGuard schema_guard;
8433ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
8434if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
8435LOG_WARN("failed to get schema guard", K(ret));
8436} else if (OB_FAIL(schema_guard.get_audit_schema_in_owner(tenant_id,
8437AUDIT_PROCEDURE,
8438routine_info.get_routine_id(),
8439audits))) {
8440LOG_WARN("get get_audit_schema_in_owner failed", K(tenant_id), K(ret));
8441} else if (!audits.empty()) {
8442common::ObSqlString public_sql_string;
8443for (int64_t i = 0; OB_SUCC(ret) && i < audits.count(); ++i) {
8444const ObSAuditSchema *audit_schema = audits.at(i);
8445if (OB_ISNULL(audit_schema)) {
8446ret = OB_ERR_UNEXPECTED;
8447LOG_WARN("audit_schema is NULL", K(ret));
8448} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8449LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8450} else if (OB_FAIL(schema_service_impl->get_audit_sql_service().handle_audit_metainfo(
8451*audit_schema,
8452AUDIT_MT_DEL,
8453false,
8454new_schema_version,
8455NULL,
8456trans,
8457public_sql_string))) {
8458LOG_WARN("drop audit_schema failed", KPC(audit_schema), K(ret));
8459} else {
8460LOG_INFO("succ to delete audit_schema from drop procedure", KPC(audit_schema));
8461}
8462}
8463} else {
8464LOG_DEBUG("no need to delete audit_schema from drop procedure", K(audits), K(routine_info));
8465}
8466}
8467return ret;
8468}
8469
8470int ObDDLOperator::update_routine_info(ObRoutineInfo &routine_info,
8471int64_t tenant_id,
8472int64_t parent_id,
8473int64_t owner_id,
8474int64_t database_id,
8475int64_t routine_id = OB_INVALID_ID)
8476{
8477int ret = OB_SUCCESS;
8478uint64_t new_routine_id = routine_id;
8479int64_t new_schema_version = OB_INVALID_VERSION;
8480ObSchemaService *schema_service = schema_service_.get_schema_service();
8481lib::Worker::CompatMode compat_mode = lib::Worker::CompatMode::INVALID;
8482if (OB_ISNULL(schema_service)) {
8483ret = OB_ERR_SYS;
8484LOG_ERROR("schema_service must not null", K(ret));
8485} else if (OB_INVALID_ID == new_routine_id
8486&& OB_SYS_TENANT_ID == tenant_id
8487&& OB_FAIL(schema_service->fetch_new_sys_pl_object_id(tenant_id, new_routine_id))) {
8488LOG_WARN("failed to fetch new_routine_id", K(tenant_id), K(ret));
8489} else if (OB_INVALID_ID == new_routine_id
8490&& OB_SYS_TENANT_ID != tenant_id
8491&& OB_FAIL(schema_service->fetch_new_routine_id(tenant_id, new_routine_id))) {
8492LOG_WARN("failed to fetch new_routine_id", K(tenant_id), K(ret));
8493} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8494LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8495} else if (OB_FAIL(ObCompatModeGetter::get_tenant_mode(tenant_id, compat_mode))) {
8496LOG_WARN("fail to get tenant mode", K(ret));
8497} else {
8498routine_info.set_database_id(database_id);
8499if (lib::Worker::CompatMode::ORACLE == compat_mode) {
8500routine_info.set_owner_id(owner_id);
8501}
8502routine_info.set_package_id(parent_id);
8503routine_info.set_routine_id(new_routine_id);
8504routine_info.set_schema_version(new_schema_version);
8505}
8506return ret;
8507}
8508
8509int ObDDLOperator::create_package(const ObPackageInfo *old_package_info,
8510ObPackageInfo &new_package_info,
8511ObMySQLTransaction &trans,
8512ObSchemaGetterGuard &schema_guard,
8513ObIArray<ObRoutineInfo> &public_routine_infos,
8514ObErrorInfo &error_info,
8515ObIArray<ObDependencyInfo> &dep_infos,
8516const ObString *ddl_stmt_str)
8517{
8518int ret = OB_SUCCESS;
8519uint64_t new_package_id = OB_INVALID_ID;
8520const uint64_t tenant_id = new_package_info.get_tenant_id();
8521int64_t new_schema_version = OB_INVALID_VERSION;
8522bool is_replace = false;
8523ObSchemaService *schema_service = schema_service_.get_schema_service();
8524if (OB_ISNULL(schema_service)) {
8525ret = OB_ERR_SYS;
8526LOG_ERROR("schema_service must not null", K(ret));
8527} else {
8528if (OB_NOT_NULL(old_package_info)) {
8529is_replace = true;
8530new_package_id = old_package_info->get_package_id();
8531if (old_package_info->is_package() && OB_FAIL(del_routines_in_package(*old_package_info, trans, schema_guard))) {
8532LOG_WARN("del routines in package failed", K(ret), K(old_package_info));
8533} else {}
8534} else {
8535if (OB_SYS_TENANT_ID == tenant_id) {
8536if (OB_FAIL(schema_service->fetch_new_sys_pl_object_id(tenant_id, new_package_id))) {
8537LOG_WARN("failed to fetch new_package_id", K(tenant_id), K(ret));
8538}
8539} else {
8540if (OB_FAIL(schema_service->fetch_new_package_id(tenant_id, new_package_id))) {
8541LOG_WARN("failed to fetch new_package_id", K(tenant_id), K(ret));
8542}
8543}
8544}
8545if (OB_SUCC(ret)) {
8546new_package_info.set_package_id(new_package_id);
8547if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8548LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8549} else if (FALSE_IT(new_package_info.set_schema_version(new_schema_version))) {
8550} else if (OB_FAIL(schema_service->get_routine_sql_service().create_package(new_package_info,
8551&trans, is_replace, ddl_stmt_str))) {
8552LOG_WARN("insert package info failed", K(new_package_info), K(ret));
8553} else {
8554ARRAY_FOREACH(public_routine_infos, routine_idx) {
8555ObRoutineInfo &routine_info = public_routine_infos.at(routine_idx);
8556if (new_package_info.is_package()) {
8557if (OB_FAIL(update_routine_info(routine_info,
8558tenant_id,
8559new_package_info.get_package_id(),
8560new_package_info.get_owner_id(),
8561new_package_info.get_database_id(),
8562routine_info.get_routine_id()))) {
8563LOG_WARN("failed to update routine info", K(ret));
8564} else if (OB_FAIL(schema_service->get_routine_sql_service().create_routine(routine_info,
8565&trans, NULL))) {
8566LOG_WARN("insert routine info failed", K(routine_info), K(ret));
8567}
8568} else if (OB_INVALID_ID == routine_info.get_routine_id()) {
8569OZ (update_routine_info(routine_info,
8570tenant_id,
8571routine_info.get_package_id(),
8572routine_info.get_owner_id(),
8573routine_info.get_database_id()));
8574OZ (schema_service->get_routine_sql_service().create_routine(routine_info, &trans, NULL));
8575} else {
8576// update routine route sql
8577int64_t new_schema_version = OB_INVALID_VERSION;
8578OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version));
8579OX (routine_info.set_schema_version(new_schema_version));
8580OZ (schema_service->get_routine_sql_service().update_routine(routine_info, &trans));
8581}
8582}
8583}
8584}
8585
8586// add audit in package if necessary
8587if (OB_SUCC(ret) && new_package_info.is_package()) {
8588int64_t new_schema_version = OB_INVALID_VERSION;
8589ObArray<const ObSAuditSchema *> audits;
8590if (OB_FAIL(schema_guard.get_audit_schema_in_owner(tenant_id,
8591AUDIT_OBJ_DEFAULT,
8592OB_AUDIT_MOCK_USER_ID,
8593audits))) {
8594LOG_WARN("get get_audit_schema_in_owner failed", K(tenant_id), K(ret));
8595} else if (!audits.empty()) {
8596ObSchemaService *schema_service = schema_service_.get_schema_service();
8597common::ObSqlString public_sql_string;
8598ObSAuditSchema new_audit_schema;
8599for (int64_t i = 0; OB_SUCC(ret) && i < audits.count(); ++i) {
8600uint64_t new_audit_id = common::OB_INVALID_ID;
8601const ObSAuditSchema *audit_schema = audits.at(i);
8602if (OB_ISNULL(audit_schema)) {
8603ret = OB_ERR_UNEXPECTED;
8604LOG_WARN("audit_schema is NULL", K(ret));
8605} else if (!audit_schema->is_access_operation_for_package()) {
8606continue;
8607} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))){
8608LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8609} else if (OB_FAIL(schema_service->fetch_new_audit_id(tenant_id, new_audit_id))) {
8610LOG_WARN("Failed to fetch new_audit_id", K(ret));
8611} else if (OB_FAIL(new_audit_schema.assign(*audit_schema))) {
8612LOG_WARN("fail to assign audit schema", KR(ret));
8613} else {
8614new_audit_schema.set_schema_version(new_schema_version);
8615new_audit_schema.set_audit_id(new_audit_id);
8616new_audit_schema.set_audit_type(AUDIT_PACKAGE);
8617new_audit_schema.set_owner_id(new_package_info.get_package_id());
8618if (OB_FAIL(schema_service->get_audit_sql_service().handle_audit_metainfo(
8619new_audit_schema,
8620AUDIT_MT_ADD,
8621false,
8622new_schema_version,
8623NULL,
8624trans,
8625public_sql_string))) {
8626LOG_WARN("add audit_schema failed", K(new_audit_schema), K(ret));
8627} else {
8628LOG_INFO("succ to add audit_schema from package", K(new_audit_schema));
8629}
8630}
8631}
8632}
8633}
8634if (is_replace) {
8635OZ (ObDependencyInfo::delete_schema_object_dependency(trans, tenant_id,
8636old_package_info->get_package_id(),
8637new_schema_version,
8638old_package_info->get_object_type()));
8639}
8640OZ (insert_dependency_infos(trans, dep_infos, tenant_id, new_package_info.get_package_id(),
8641new_package_info.get_schema_version(),
8642new_package_info.get_owner_id()));
8643if (OB_SUCC(ret)) {
8644if (OB_FAIL(error_info.handle_error_info(trans, &new_package_info))) {
8645LOG_WARN("insert create package error info failed.", K(ret), K(error_info));
8646}
8647}
8648}
8649return ret;
8650}
8651
8652int ObDDLOperator::alter_package(const ObPackageInfo &package_info,
8653ObSchemaGetterGuard &schema_guard,
8654ObMySQLTransaction &trans,
8655ObIArray<ObRoutineInfo> &public_routine_infos,
8656ObErrorInfo &error_info,
8657const ObString *ddl_stmt_str)
8658{
8659int ret = OB_SUCCESS;
8660UNUSEDx(ddl_stmt_str);
8661ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
8662const uint64_t tenant_id = package_info.get_tenant_id();
8663int64_t new_schema_version = OB_INVALID_VERSION;
8664uint64_t package_id = package_info.get_package_id();
8665ObPackageInfo new_package_info;
8666OV (OB_NOT_NULL(schema_service_impl), OB_ERR_SYS);
8667OV (OB_INVALID_ID != tenant_id && OB_INVALID_ID != package_id, OB_INVALID_ARGUMENT);
8668if (OB_SUCC(ret)) {
8669if (public_routine_infos.count() > 0) {
8670bool need_create = false;
8671ObArray<const ObRoutineInfo *> routine_infos;
8672OZ (schema_guard.get_routine_infos_in_package(tenant_id,
8673public_routine_infos.at(0).get_package_id(),
8674routine_infos));
8675OX (need_create = (0 == routine_infos.count()));
8676// update routine route sql
8677ARRAY_FOREACH(public_routine_infos, routine_idx) {
8678ObRoutineInfo &routine_info = public_routine_infos.at(routine_idx);
8679if (need_create) {
8680CK (OB_INVALID_ID == routine_info.get_routine_id());
8681OZ (update_routine_info(routine_info,
8682tenant_id,
8683routine_info.get_package_id(),
8684routine_info.get_owner_id(),
8685routine_info.get_database_id()));
8686OZ (schema_service_impl->get_routine_sql_service().create_routine(routine_info, &trans, NULL));
8687} else {
8688OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version));
8689OX (routine_info.set_schema_version(new_schema_version));
8690OZ (schema_service_impl->get_routine_sql_service().update_routine(routine_info, &trans));
8691}
8692}
8693OZ (new_package_info.assign(package_info));
8694OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version));
8695OX (new_package_info.set_schema_version(new_schema_version));
8696OZ (schema_service_impl->get_routine_sql_service().alter_package(new_package_info, &trans, ddl_stmt_str));
8697}
8698}
8699OZ (error_info.handle_error_info(trans, &package_info));
8700return ret;
8701}
8702
8703int ObDDLOperator::drop_package(const ObPackageInfo &package_info,
8704ObMySQLTransaction &trans,
8705ObSchemaGetterGuard &schema_guard,
8706ObErrorInfo &error_info,
8707const ObString *ddl_stmt_str)
8708{
8709int ret = OB_SUCCESS;
8710ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
8711const uint64_t tenant_id = package_info.get_tenant_id();
8712int64_t new_schema_version = OB_INVALID_VERSION;
8713uint64_t package_id = package_info.get_package_id();
8714if (OB_ISNULL(schema_service_impl)) {
8715ret = OB_ERR_SYS;
8716LOG_ERROR("schema_service must not null", K(ret));
8717} else if (OB_INVALID_ID == tenant_id || OB_INVALID_ID == package_id) {
8718ret = OB_INVALID_ARGUMENT;
8719LOG_WARN("invalid argument", K(ret));
8720}
8721if (OB_SUCC(ret)) {
8722if (package_info.is_package()
8723&& OB_FAIL(del_routines_in_package(package_info, trans, schema_guard))) {
8724LOG_WARN("del routines in package failed", K(ret));
8725}
8726}
8727
8728if (OB_SUCC(ret)) {
8729if (PACKAGE_TYPE == package_info.get_type()) {
8730// delete privs on package
8731OZ (drop_obj_privs(tenant_id,
8732package_info.get_package_id(),
8733static_cast<uint64_t>(ObObjectType::PACKAGE),
8734trans));
8735uint64_t database_id = package_info.get_database_id();
8736int64_t compatible_mode = package_info.get_compatibility_mode();
8737const ObString &package_name = package_info.get_package_name();
8738const ObPackageInfo *package_body_info = NULL;
8739if (OB_FAIL(schema_guard.get_package_info(tenant_id, database_id, package_name, PACKAGE_BODY_TYPE,
8740compatible_mode, package_body_info))) {
8741LOG_WARN("get package body info failed", K(tenant_id), K(database_id), K(package_name), K(ret));
8742} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8743LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8744} else if (NULL != package_body_info) {
8745if (OB_FAIL(schema_service_impl->get_routine_sql_service().drop_package(package_body_info->get_tenant_id(),
8746package_body_info->get_database_id(),
8747package_body_info->get_package_id(),
8748new_schema_version, trans))) {
8749LOG_WARN("drop package body info failed", K(package_body_info), K(ret));
8750}
8751} else {
8752// package spec
8753OZ (ObDependencyInfo::delete_schema_object_dependency(trans, tenant_id,
8754package_info.get_package_id(),
8755new_schema_version,
8756package_info.get_object_type()));
8757if (OB_NOT_NULL(package_body_info)) {
8758OZ (ObDependencyInfo::delete_schema_object_dependency(trans, tenant_id,
8759package_body_info->get_package_id(),
8760new_schema_version,
8761package_body_info->get_object_type()));
8762}
8763}
8764}
8765}
8766if (OB_SUCC(ret)) {
8767if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8768LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8769} else if (OB_FAIL(schema_service_impl->get_routine_sql_service().drop_package(package_info.get_tenant_id(),
8770package_info.get_database_id(),
8771package_info.get_package_id(),
8772new_schema_version, trans, ddl_stmt_str))) {
8773LOG_WARN("drop package info failed", K(package_info), K(ret));
8774}
8775}
8776if (OB_SUCC(ret)) {
8777if (OB_FAIL(error_info.handle_error_info(trans, &package_info))) {
8778LOG_WARN("delete drop package error info failed", K(ret), K(error_info));
8779}
8780}
8781
8782// delete audit in package
8783if (OB_SUCC(ret)) {
8784ObArray<const ObSAuditSchema *> audits;
8785if (OB_FAIL(schema_guard.get_audit_schema_in_owner(tenant_id,
8786AUDIT_PACKAGE,
8787package_info.get_package_id(),
8788audits))) {
8789LOG_WARN("get get_audit_schema_in_owner failed", K(tenant_id), K(ret));
8790} else if (!audits.empty()) {
8791common::ObSqlString public_sql_string;
8792for (int64_t i = 0; OB_SUCC(ret) && i < audits.count(); ++i) {
8793const ObSAuditSchema *audit_schema = audits.at(i);
8794if (OB_ISNULL(audit_schema)) {
8795ret = OB_ERR_UNEXPECTED;
8796LOG_WARN("audit_schema is NULL", K(ret));
8797} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8798LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8799} else if (OB_FAIL(schema_service_impl->get_audit_sql_service().handle_audit_metainfo(
8800*audit_schema,
8801AUDIT_MT_DEL,
8802false,
8803new_schema_version,
8804NULL,
8805trans,
8806public_sql_string))) {
8807LOG_WARN("drop audit_schema failed", KPC(audit_schema), K(ret));
8808} else {
8809LOG_INFO("succ to delete audit_schema from drop package", KPC(audit_schema));
8810}
8811}
8812} else {
8813LOG_DEBUG("no need to delete audit_schema from drop package", K(audits), K(package_info));
8814}
8815}
8816return ret;
8817}
8818
8819int ObDDLOperator::del_routines_in_package(const ObPackageInfo &package_info,
8820ObMySQLTransaction &trans,
8821ObSchemaGetterGuard &schema_guard)
8822{
8823int ret = OB_SUCCESS;
8824ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
8825const uint64_t tenant_id = package_info.get_tenant_id();
8826uint64_t package_id = package_info.get_package_id();
8827if (OB_INVALID_ID == tenant_id
8828|| OB_INVALID_ID == package_id) {
8829ret = OB_INVALID_ARGUMENT;
8830LOG_WARN("invalid argument", K(ret));
8831} else {
8832ObArray<const ObRoutineInfo *> routines;
8833if (OB_FAIL(schema_guard.get_routine_infos_in_package(tenant_id, package_id, routines))) {
8834LOG_WARN("get routines in package failed", K(tenant_id), K(package_id), K(ret));
8835} else {
8836for (int64_t i = 0; OB_SUCC(ret) && i < routines.count(); ++i) {
8837const ObRoutineInfo *routine_info = routines.at(i);
8838int64_t new_schema_version = OB_INVALID_VERSION;
8839if (OB_ISNULL(routine_info)) {
8840ret = OB_ERR_UNEXPECTED;
8841LOG_WARN("routine info is NULL", K(ret));
8842} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
8843LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
8844} else if (OB_FAIL(schema_service_impl->get_routine_sql_service().drop_routine(
8845*routine_info, new_schema_version, trans))) {
8846LOG_WARN("drop routine failed", "routine_id", routine_info->get_routine_id(), K(ret));
8847}
8848}
8849}
8850}
8851return ret;
8852}
8853
8854// functions for managing trigger
8855
8856int ObDDLOperator::create_trigger(ObTriggerInfo &trigger_info,
8857ObMySQLTransaction &trans,
8858ObErrorInfo &error_info,
8859ObIArray<ObDependencyInfo> &dep_infos,
8860int64_t &table_schema_version,
8861const ObString *ddl_stmt_str,
8862bool is_update_table_schema_version,
8863bool is_for_truncate_table)
8864{
8865int ret = OB_SUCCESS;
8866table_schema_version = OB_INVALID_VERSION;
8867ObSchemaService *schema_service = schema_service_.get_schema_service();
8868const uint64_t tenant_id = trigger_info.get_tenant_id();
8869int64_t new_schema_version = OB_INVALID_VERSION;
8870bool is_replace = false;
8871OV (OB_NOT_NULL(schema_service));
8872if (!is_for_truncate_table) {
8873// If create_trigger through truncate table, trigger_info already has its own trigger_id.
8874// but there is no such trigger in the internal table at this time, so is_replace must be false
8875OX (is_replace = (OB_INVALID_ID != trigger_info.get_trigger_id()));
8876if (!is_replace) {
8877OZ (fill_trigger_id(*schema_service, trigger_info), trigger_info.get_trigger_name());
8878}
8879}
8880OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version),
8881tenant_id, trigger_info.get_trigger_name());
8882OX (trigger_info.set_schema_version(new_schema_version));
8883OZ (schema_service->get_trigger_sql_service().create_trigger(trigger_info, is_replace,
8884trans, ddl_stmt_str),
8885trigger_info.get_trigger_name(), is_replace);
8886if (!trigger_info.is_system_type() && is_update_table_schema_version) {
8887uint64_t base_table_id = trigger_info.get_base_object_id();
8888OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version));
8889OX (table_schema_version = new_schema_version);
8890OZ (schema_service->get_table_sql_service().update_data_table_schema_version(
8891trans, tenant_id, base_table_id, false/*in offline ddl white list*/, new_schema_version),
8892base_table_id, trigger_info.get_trigger_name());
8893}
8894if (OB_FAIL(ret)) {
8895} else if (0 == dep_infos.count()) {
8896// create trigger in mysql mode or create trigger when truncate table, dep_infos.count() is 0,
8897// no need to deal with dependencies.
8898} else {
8899OZ (ObDependencyInfo::delete_schema_object_dependency(trans, trigger_info.get_tenant_id(),
8900trigger_info.get_trigger_id(),
8901trigger_info.get_schema_version(),
8902trigger_info.get_object_type()));
8903OZ (insert_dependency_infos(trans, dep_infos, trigger_info.get_tenant_id(), trigger_info.get_trigger_id(),
8904trigger_info.get_schema_version(), trigger_info.get_owner_id()));
8905}
8906if (OB_SUCC(ret)) {
8907if (OB_FAIL(error_info.handle_error_info(trans, &trigger_info))) {
8908LOG_WARN("insert trigger error info failed.", K(ret), K(error_info));
8909}
8910}
8911return ret;
8912}
8913
8914int ObDDLOperator::drop_trigger(const ObTriggerInfo &trigger_info,
8915ObMySQLTransaction &trans,
8916const ObString *ddl_stmt_str,
8917bool is_update_table_schema_version,
8918const bool in_offline_ddl_white_list)
8919{
8920int ret = OB_SUCCESS;
8921ObSchemaService *schema_service = schema_service_.get_schema_service();
8922const uint64_t tenant_id = trigger_info.get_tenant_id();
8923int64_t new_schema_version = OB_INVALID_VERSION;
8924OV (OB_NOT_NULL(schema_service));
8925OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version),
8926tenant_id, trigger_info.get_trigger_name());
8927OZ (schema_service->get_trigger_sql_service().drop_trigger(trigger_info, false,
8928new_schema_version,
8929trans, ddl_stmt_str),
8930trigger_info.get_trigger_name());
8931OZ (ObDependencyInfo::delete_schema_object_dependency(trans, tenant_id,
8932trigger_info.get_trigger_id(),
8933new_schema_version,
8934trigger_info.get_object_type()));
8935if (OB_SUCC(ret) && !trigger_info.is_system_type() && is_update_table_schema_version) {
8936uint64_t base_table_id = trigger_info.get_base_object_id();
8937OZ (schema_service->get_table_sql_service().update_data_table_schema_version(trans,
8938tenant_id, base_table_id, in_offline_ddl_white_list),
8939base_table_id, trigger_info.get_trigger_name());
8940}
8941if (OB_SUCC(ret)) {
8942ObErrorInfo error_info;
8943if (OB_FAIL(error_info.handle_error_info(trans, &trigger_info))) {
8944LOG_WARN("delete trigger error info failed.", K(ret), K(error_info));
8945}
8946}
8947return ret;
8948}
8949
8950int ObDDLOperator::alter_trigger(ObTriggerInfo &trigger_info,
8951ObMySQLTransaction &trans,
8952const ObString *ddl_stmt_str,
8953bool is_update_table_schema_version)
8954{
8955int ret = OB_SUCCESS;
8956ObSchemaService *schema_service = schema_service_.get_schema_service();
8957const uint64_t tenant_id = trigger_info.get_tenant_id();
8958int64_t new_schema_version = OB_INVALID_VERSION;
8959OV (OB_NOT_NULL(schema_service));
8960OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version), tenant_id,
8961trigger_info.get_trigger_name());
8962OX (trigger_info.set_schema_version(new_schema_version));
8963OZ (schema_service->get_trigger_sql_service().alter_trigger(trigger_info, new_schema_version,
8964trans, ddl_stmt_str),
8965trigger_info.get_trigger_name());
8966if (OB_SUCC(ret) && !trigger_info.is_system_type() && is_update_table_schema_version) {
8967uint64_t base_table_id = trigger_info.get_base_object_id();
8968OZ (schema_service->get_table_sql_service().update_data_table_schema_version(
8969trans, tenant_id, base_table_id, false/*in offline ddl white list*/),
8970base_table_id, trigger_info.get_trigger_name());
8971}
8972ObErrorInfo error_info;
8973OZ (error_info.handle_error_info(trans, &trigger_info), error_info);
8974return ret;
8975}
8976
8977int ObDDLOperator::drop_trigger_to_recyclebin(const ObTriggerInfo &trigger_info,
8978ObSchemaGetterGuard &schema_guard,
8979ObMySQLTransaction &trans)
8980{
8981int ret = OB_SUCCESS;
8982ObSchemaService *schema_service = schema_service_.get_schema_service();
8983uint64_t tenant_id = trigger_info.get_tenant_id();
8984uint64_t recyclebin_id = OB_RECYCLEBIN_SCHEMA_ID;
8985uint64_t base_database_id = OB_INVALID_ID;
8986bool recyclebin_exist = false;
8987int64_t new_schema_version = OB_INVALID_VERSION;
8988const ObSimpleTableSchemaV2 *base_table_schema = NULL;
8989ObTriggerInfo new_trigger_info;
8990ObSqlString new_trigger_name;
8991ObRecycleObject recyclebin_object;
8992OV (OB_NOT_NULL(schema_service));
8993OV (OB_INVALID_TENANT_ID != tenant_id, OB_INVALID_ARGUMENT);
8994OZ (schema_guard.check_database_exist(tenant_id, recyclebin_id, recyclebin_exist));
8995OV (recyclebin_exist);
8996OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version));
8997OZ (new_trigger_info.deep_copy(trigger_info));
8998OX (new_trigger_info.set_database_id(recyclebin_id));
8999OX (new_trigger_info.set_schema_version(new_schema_version));
9000OZ (construct_new_name_for_recyclebin(new_trigger_info, new_trigger_name));
9001OZ (new_trigger_info.set_trigger_name(new_trigger_name.string()));
9002OZ (schema_guard.get_simple_table_schema(tenant_id, trigger_info.get_base_object_id(), base_table_schema),
9003trigger_info.get_base_object_id());
9004OV (OB_NOT_NULL(base_table_schema), trigger_info.get_base_object_id());
9005OX (base_database_id = base_table_schema->get_database_id());
9006OX (recyclebin_object.set_tenant_id(tenant_id));
9007OX (recyclebin_object.set_database_id(base_database_id));
9008OX (recyclebin_object.set_table_id(trigger_info.get_trigger_id()));
9009OX (recyclebin_object.set_tablegroup_id(OB_INVALID_ID));
9010OZ (recyclebin_object.set_object_name(new_trigger_name.string()));
9011OZ (recyclebin_object.set_original_name(trigger_info.get_trigger_name()));
9012OX (recyclebin_object.set_type(ObRecycleObject::TRIGGER));
9013OZ (schema_service->insert_recyclebin_object(recyclebin_object, trans));
9014OZ (schema_service->get_trigger_sql_service().drop_trigger(new_trigger_info, true,
9015new_schema_version, trans),
9016trigger_info.get_trigger_name());
9017return ret;
9018}
9019
9020int ObDDLOperator::flashback_trigger(const ObTriggerInfo &trigger_info,
9021uint64_t new_database_id,
9022const ObString &new_table_name,
9023ObSchemaGetterGuard &schema_guard,
9024ObMySQLTransaction &trans)
9025{
9026int ret = OB_SUCCESS;
9027ObSchemaService *schema_service = schema_service_.get_schema_service();
9028uint64_t tenant_id = trigger_info.get_tenant_id();
9029const ObDatabaseSchema *database_schema = NULL;
9030ObSEArray<ObRecycleObject, 1> recycle_objects;
9031ObArenaAllocator allocator(ObModIds::OB_SCHEMA);
9032ObString new_trigger_name;
9033int64_t new_schema_version = OB_INVALID_VERSION;
9034ObTriggerInfo new_trigger_info;
9035OV (OB_NOT_NULL(schema_service));
9036OZ (schema_service->fetch_recycle_object(tenant_id, trigger_info.get_trigger_name(),
9037ObRecycleObject::TRIGGER, trans, recycle_objects));
9038OV (1 == recycle_objects.count(), OB_ERR_UNEXPECTED, recycle_objects.count());
9039OZ (new_trigger_info.deep_copy(trigger_info));
9040// database id.
9041if (OB_INVALID_ID == new_database_id) {
9042OX (new_database_id = recycle_objects.at(0).get_database_id());
9043}
9044OZ (schema_guard.get_database_schema(tenant_id, new_database_id, database_schema), new_database_id);
9045OV (OB_NOT_NULL(database_schema), new_database_id);
9046OV (!database_schema->is_in_recyclebin(), OB_OP_NOT_ALLOW, new_database_id);
9047OX (new_trigger_info.set_database_id(new_database_id));
9048// trigger name.
9049OZ (build_flashback_object_name(new_trigger_info, NULL, "OBTRG",
9050schema_guard, allocator, new_trigger_name));
9051OX (new_trigger_info.set_trigger_name(new_trigger_name));
9052// other operation.
9053OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version), tenant_id);
9054OZ (schema_service->delete_recycle_object(tenant_id, recycle_objects.at(0), trans));
9055if (OB_FAIL(ret)) {
9056//do nothing
9057} else if (new_table_name.empty()) {
9058// If new_table_name is empty, it means that the table does not have a rename, and rebuild_trigger_package is not required.
9059// Otherwise, table rename is required, and rebuild_trigger_package is required.
9060OZ (schema_service->get_trigger_sql_service().flashback_trigger(new_trigger_info,
9061new_schema_version, trans),
9062new_trigger_info.get_trigger_id());
9063} else {
9064OZ (schema_service->get_trigger_sql_service()
9065.rebuild_trigger_package(new_trigger_info,
9066database_schema->get_database_name(),
9067new_table_name, new_schema_version,
9068trans, OB_DDL_FLASHBACK_TRIGGER),
9069database_schema->get_database_name(), new_table_name);
9070}
9071return ret;
9072}
9073
9074int ObDDLOperator::purge_table_trigger(const ObTableSchema &table_schema,
9075ObSchemaGetterGuard &schema_guard,
9076ObMySQLTransaction &trans)
9077{
9078int ret = OB_SUCCESS;
9079const uint64_t tenant_id = table_schema.get_tenant_id();
9080const ObIArray<uint64_t> &trigger_id_list = table_schema.get_trigger_list();
9081const ObTriggerInfo *trigger_info = NULL;
9082for (int i = 0; OB_SUCC(ret) && i < trigger_id_list.count(); i++) {
9083OZ (schema_guard.get_trigger_info(tenant_id, trigger_id_list.at(i), trigger_info), trigger_id_list.at(i));
9084OV (OB_NOT_NULL(trigger_info), OB_ERR_UNEXPECTED, trigger_id_list.at(i));
9085OZ (purge_trigger(*trigger_info, trans), trigger_id_list.at(i));
9086}
9087return ret;
9088}
9089
9090int ObDDLOperator::purge_trigger(const ObTriggerInfo &trigger_info, ObMySQLTransaction &trans)
9091{
9092int ret = OB_SUCCESS;
9093ObSchemaService *schema_service = schema_service_.get_schema_service();
9094ObRecycleObject::RecycleObjType recycle_type = ObRecycleObject::TRIGGER;
9095ObArray<ObRecycleObject> recycle_objects;
9096OV (OB_NOT_NULL(schema_service));
9097OZ (schema_service->fetch_recycle_object(trigger_info.get_tenant_id(),
9098trigger_info.get_trigger_name(),
9099recycle_type, trans, recycle_objects));
9100OV (1 == recycle_objects.count(), OB_ERR_UNEXPECTED, recycle_objects.count());
9101OZ (schema_service->delete_recycle_object(trigger_info.get_tenant_id(),
9102recycle_objects.at(0), trans));
9103OZ (drop_trigger(trigger_info, trans, NULL));
9104return ret;
9105}
9106
9107int ObDDLOperator::rebuild_trigger_package(const ObTriggerInfo &trigger_info,
9108const ObString &database_name,
9109const ObString &table_name,
9110ObMySQLTransaction &trans)
9111{
9112int ret = OB_SUCCESS;
9113ObSchemaService *schema_service = schema_service_.get_schema_service();
9114const uint64_t tenant_id = trigger_info.get_tenant_id();
9115int64_t new_schema_version = OB_INVALID_VERSION;
9116OV (OB_NOT_NULL(schema_service));
9117OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version),
9118tenant_id, trigger_info.get_trigger_name());
9119OZ (schema_service->get_trigger_sql_service().rebuild_trigger_package(trigger_info,
9120database_name, table_name,
9121new_schema_version, trans),
9122trigger_info.get_trigger_name());
9123return ret;
9124}
9125
9126int ObDDLOperator::fill_trigger_id(ObSchemaService &schema_service, ObTriggerInfo &trigger_info)
9127{
9128int ret = OB_SUCCESS;
9129uint64_t new_trigger_id = OB_INVALID_ID;
9130OV (OB_INVALID_ID == trigger_info.get_trigger_id());
9131OZ (schema_service.fetch_new_trigger_id(trigger_info.get_tenant_id(), new_trigger_id),
9132trigger_info.get_trigger_name());
9133OX (trigger_info.set_trigger_id(new_trigger_id));
9134return ret;
9135}
9136
9137/* [data_table_prefix]_[data_table_id]_RECYCLE_[object_type_prefix]_[timestamp].
9138* data_table_prefix: if not null, use '[data_table_prefix]_[data_table_id]' as prefix,
9139* otherwise, skip this part.
9140* object_type_prefix: OBIDX / OBCHECK / OBTRG ...
9141*/
9142template <typename SchemaType>
9143int ObDDLOperator::build_flashback_object_name(const SchemaType &object_schema,
9144const char *data_table_prefix,
9145const char *object_type_prefix,
9146ObSchemaGetterGuard &schema_guard,
9147ObIAllocator &allocator,
9148ObString &object_name)
9149{
9150int ret = OB_SUCCESS;
9151char *buf = NULL;
9152int64_t buf_len = 0;
9153int64_t pos = 0;
9154int64_t saved_pos = 0;
9155bool object_exist = true;
9156static const char *RECYCLE = "RECYCLE";
9157static const int64_t INT64_STR_LEN = 20;
9158OV (OB_NOT_NULL(object_type_prefix));
9159if (OB_NOT_NULL(data_table_prefix)) {
9160OX (buf_len += (STRLEN(data_table_prefix) + 1 + // [data_table_prefix]_
9161INT64_STR_LEN + 1)); // [data_table_id]_
9162}
9163OX (buf_len += (STRLEN(RECYCLE) + 1 + // RECYCLE_
9164STRLEN(object_type_prefix) + 1 + // [object_type_prefix]_
9165INT64_STR_LEN)); // [timestamp]
9166OV (OB_NOT_NULL(buf = static_cast<char *>(allocator.alloc(buf_len))),
9167OB_ALLOCATE_MEMORY_FAILED, buf_len);
9168if (OB_NOT_NULL(data_table_prefix)) {
9169OZ (BUF_PRINTF("%s_%lu_", data_table_prefix, object_schema.get_data_table_id()));
9170}
9171OZ (BUF_PRINTF("%s_%s_", RECYCLE, object_type_prefix));
9172OX (saved_pos = pos);
9173while (OB_SUCC(ret) && object_exist) {
9174OX (pos = saved_pos);
9175OZ (BUF_PRINTF("%ld", ObTimeUtility::current_time()));
9176OX (object_name.assign(buf, static_cast<int32_t>(pos)));
9177OZ (schema_guard.check_flashback_object_exist(object_schema, object_name, object_exist));
9178}
9179return ret;
9180}
9181
9182// functions for managing udt
9183
9184int ObDDLOperator::create_udt(ObUDTTypeInfo &udt_info,
9185ObMySQLTransaction &trans,
9186ObErrorInfo &error_info,
9187ObIArray<share::schema::ObRoutineInfo> &public_routine_infos,
9188ObSchemaGetterGuard &schema_guard,
9189ObIArray<ObDependencyInfo> &dep_infos,
9190const ObString *ddl_stmt_str)
9191{
9192UNUSED(schema_guard);
9193int ret = OB_SUCCESS;
9194uint64_t new_udt_id = OB_INVALID_ID;
9195const uint64_t tenant_id = udt_info.get_tenant_id();
9196int64_t new_schema_version = OB_INVALID_VERSION;
9197ObSchemaService *schema_service = schema_service_.get_schema_service();
9198ObUDTObjectType *obj_info = NULL;
9199
9200if (OB_ISNULL(schema_service)) {
9201ret = OB_ERR_SYS;
9202LOG_ERROR("schema_service must not null", K(ret));
9203} else if (OB_SYS_TENANT_ID == tenant_id
9204&& !is_inner_pl_udt_id(new_udt_id = udt_info.get_type_id())) {
9205ret = OB_ERR_UNEXPECTED;
9206LOG_WARN("failed to get new system udt id", K(tenant_id), K(ret));
9207} else if (OB_SYS_TENANT_ID != tenant_id
9208&& OB_FAIL(schema_service->fetch_new_udt_id(tenant_id, new_udt_id))) {
9209LOG_WARN("failed to fetch new_udt_id", K(tenant_id), K(ret));
9210} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
9211LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
9212} else {
9213// object spec and collection
9214if (udt_info.is_object_spec_ddl() || !udt_info.is_object_type()) {
9215udt_info.set_type_id(new_udt_id);
9216if (udt_info.is_object_spec_ddl()
9217&& 0 < udt_info.get_object_type_infos().count()) {
9218OX (obj_info = udt_info.get_object_type_infos().at(0));
9219CK (OB_NOT_NULL(obj_info));
9220// set object spec id
9221uint64_t new_obj_id = OB_INVALID_ID;
9222if (OB_SYS_TENANT_ID == tenant_id) {
9223OZ (schema_service->fetch_new_sys_pl_object_id(tenant_id, new_obj_id));
9224} else {
9225OZ (schema_service->fetch_new_udt_id(tenant_id, new_obj_id));
9226}
9227OX (obj_info->set_coll_type(new_obj_id));
9228}
9229} else { // object body
9230CK (2 == udt_info.get_object_type_infos().count());
9231OX (obj_info = udt_info.get_object_type_infos().at(1));
9232CK (OB_NOT_NULL(obj_info));
9233// set object body id
9234OX (obj_info->set_coll_type(new_udt_id));
9235// udt_info.set_type_id(new_udt_id);
9236}
9237udt_info.set_schema_version(new_schema_version);
9238if (FAILEDx(schema_service->get_udt_sql_service().create_udt(udt_info,
9239&trans,
9240ddl_stmt_str))) {
9241LOG_WARN("insert udt info failed", K(udt_info), K(ret));
9242} else {
9243ARRAY_FOREACH(public_routine_infos, routine_idx) {
9244ObRoutineInfo &routine_info = public_routine_infos.at(routine_idx);
9245if (udt_info.is_object_spec_ddl()) {
9246if (OB_FAIL(update_routine_info(routine_info,
9247tenant_id,
9248udt_info.get_type_id(),
9249udt_info.get_database_id(),
9250udt_info.get_database_id()))) {
9251LOG_WARN("failed to update routine info", K(ret));
9252} else if (OB_FAIL(schema_service->get_routine_sql_service().create_routine(routine_info,
9253&trans, NULL))) {
9254LOG_WARN("insert routine info failed", K(routine_info), K(ret));
9255}
9256} else {
9257// update routine route sql
9258int64_t new_schema_version = OB_INVALID_VERSION;
9259OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version));
9260OX (routine_info.set_schema_version(new_schema_version));
9261OZ (schema_service->get_routine_sql_service().update_routine(routine_info, &trans));
9262}
9263}
9264}
9265}
9266if (OB_SUCC(ret) && !is_inner_pl_udt_id(new_udt_id)) {
9267OZ (insert_dependency_infos(trans, dep_infos, tenant_id, new_udt_id,
9268new_schema_version,
9269udt_info.get_database_id()));
9270}
9271if (OB_SUCC(ret)) {
9272if (OB_FAIL(error_info.handle_error_info(trans, &udt_info))) {
9273LOG_WARN("insert trigger error info failed.", K(ret), K(error_info));
9274}
9275}
9276return ret;
9277}
9278
9279int ObDDLOperator::replace_udt(ObUDTTypeInfo &udt_info,
9280const ObUDTTypeInfo *old_udt_info,
9281ObMySQLTransaction &trans,
9282ObErrorInfo &error_info,
9283ObIArray<share::schema::ObRoutineInfo> &public_routine_infos,
9284ObSchemaGetterGuard &schema_guard,
9285ObIArray<ObDependencyInfo> &dep_infos,
9286const ObString *ddl_stmt_str)
9287{
9288int ret = OB_SUCCESS;
9289const uint64_t tenant_id = udt_info.get_tenant_id();
9290int64_t del_param_schema_version = OB_INVALID_VERSION;
9291int64_t new_schema_version = OB_INVALID_VERSION;
9292ObSchemaService *schema_service = schema_service_.get_schema_service();
9293CK(OB_NOT_NULL(schema_service));
9294CK(OB_NOT_NULL(old_udt_info));
9295if (old_udt_info->get_attrs().count() > 0 || OB_NOT_NULL(old_udt_info->get_coll_info())) {
9296if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, del_param_schema_version))) {
9297LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
9298}
9299}
9300
9301if (OB_FAIL(ret)) {
9302} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
9303LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
9304} else {
9305udt_info.set_schema_version(new_schema_version);
9306udt_info.set_type_id(old_udt_info->get_type_id());
9307}
9308
9309if (OB_SUCC(ret)) {
9310ObUDTObjectType *old_obj_info = NULL, *new_obj_info = NULL;
9311if (udt_info.is_object_spec_ddl()) {
9312// create or replace xxx is object(a number), no object type info
9313// CK (0 < old_udt_info->get_object_type_infos().count());
9314// CK (0 < udt_info.get_object_type_infos().count());
9315if (udt_info.get_object_type_infos().count() > 0) {
9316OX (new_obj_info = udt_info.get_object_type_infos().at(0));
9317if (old_udt_info->get_object_type_infos().count() > 0) {
9318OX (old_obj_info = old_udt_info->get_object_type_infos().at(0));
9319OX (new_obj_info->set_coll_type(old_obj_info->get_coll_type()));
9320} else {
9321uint64_t new_obj_id = OB_INVALID_ID;
9322if (OB_SYS_TENANT_ID == tenant_id) {
9323OZ (schema_service->fetch_new_sys_pl_object_id(tenant_id, new_obj_id));
9324} else {
9325OZ (schema_service->fetch_new_udt_id(tenant_id, new_obj_id));
9326}
9327OX (new_obj_info->set_coll_type(new_obj_id));
9328}
9329}
9330} else if (udt_info.is_object_body_ddl()) {
9331CK (2 == old_udt_info->get_object_type_infos().count());
9332CK (2 == udt_info.get_object_type_infos().count());
9333OX (old_obj_info = old_udt_info->get_object_type_infos().at(1));
9334OX (new_obj_info = udt_info.get_object_type_infos().at(1));
9335OX (new_obj_info->set_coll_type(old_obj_info->get_coll_type()));
9336} else {
9337// do nothing
9338}
9339}
9340
9341OZ (ObDependencyInfo::delete_schema_object_dependency(trans, tenant_id,
9342old_udt_info->get_type_id(),
9343new_schema_version,
9344old_udt_info->get_object_type()));
9345OZ (ObDependencyInfo::delete_schema_object_dependency(trans, tenant_id,
9346udt_info.get_type_id(),
9347new_schema_version,
9348udt_info.get_object_type()));
9349if (OB_SUCC(ret)) {
9350// Discuss whether the built-in functions of the object type need to be deleted and then inserted in several situations
9351// 1. spec spec replace , need delete, Because the definition of the function may have changed
9352// 2. spec body replace, need delete, Although the function definition cannot be changed,
9353// the parameters may change, because some member functions will add a self parameter,
9354// which needs to be added to the __all_routine_param table.
9355// 3. body body replace, need delete, Because the function body may have changed. type_id has also changed
9356// bool need_del_routines = true;
9357// if ((udt_info.is_object_type() && udt_info.is_object_body_ddl()
9358// && old_udt_info->is_object_type() && old_udt_info->is_object_body_ddl())) {
9359// need_del_routines = false;
9360// }
9361if (udt_info.is_object_spec_ddl() && OB_FAIL(del_routines_in_udt(udt_info, trans, schema_guard))) {
9362LOG_WARN("failed to delete functions", K(ret));
9363} else if (OB_FAIL(schema_service->get_udt_sql_service().replace_udt(udt_info,
9364old_udt_info,
9365del_param_schema_version,
9366&trans,
9367ddl_stmt_str))) {
9368LOG_WARN("replace udt info failed", K(udt_info), K(ret));
9369} else {
9370ARRAY_FOREACH(public_routine_infos, routine_idx) {
9371ObRoutineInfo &routine_info = public_routine_infos.at(routine_idx);
9372if (udt_info.is_object_spec_ddl()) {
9373if (OB_FAIL(update_routine_info(routine_info,
9374tenant_id,
9375udt_info.get_type_id(),
9376udt_info.get_database_id(),
9377udt_info.get_database_id()))) {
9378LOG_WARN("failed to update routine info", K(ret));
9379} else if (OB_FAIL(schema_service->get_routine_sql_service().create_routine(routine_info,
9380&trans, NULL))) {
9381LOG_WARN("insert routine info failed", K(routine_info), K(ret));
9382}
9383} else {
9384// update routine route sql
9385int64_t new_schema_version = OB_INVALID_VERSION;
9386OZ (schema_service_.gen_new_schema_version(tenant_id, new_schema_version));
9387OX (routine_info.set_schema_version(new_schema_version));
9388OZ (schema_service->get_routine_sql_service().update_routine(routine_info, &trans));
9389}
9390}
9391}
9392}
9393OZ (insert_dependency_infos(trans, dep_infos, tenant_id, udt_info.get_type_id(),
9394udt_info.get_schema_version(),
9395udt_info.get_database_id()));
9396if (OB_SUCC(ret)) {
9397if (OB_FAIL(error_info.handle_error_info(trans, &udt_info))) {
9398LOG_WARN("insert trigger error info failed.", K(ret), K(error_info));
9399}
9400}
9401return ret;
9402}
9403
9404int ObDDLOperator::drop_udt(const ObUDTTypeInfo &udt_info,
9405ObMySQLTransaction &trans,
9406ObSchemaGetterGuard &schema_guard,
9407const ObString *ddl_stmt_str)
9408{
9409int ret = OB_SUCCESS;
9410const uint64_t tenant_id = udt_info.get_tenant_id();
9411int64_t new_schema_version = OB_INVALID_VERSION;
9412ObSchemaService *schema_service = schema_service_.get_schema_service();
9413
9414if (OB_ISNULL(schema_service)) {
9415ret = OB_ERR_SYS;
9416LOG_ERROR("schema_service must not null", K(ret));
9417} else if (OB_FAIL(drop_obj_privs(tenant_id, udt_info.get_type_id(),
9418static_cast<uint64_t>(ObObjectType::TYPE),
9419trans))) {
9420LOG_WARN("fail to drop_obj_privs", K(ret), K(udt_info), K(tenant_id));
9421} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
9422LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
9423} else if (udt_info.is_object_spec_ddl() && OB_FAIL(del_routines_in_udt(udt_info,
9424trans, schema_guard))) {
9425LOG_WARN("faile to drop routines in udt", K(ret));
9426} else if (OB_FAIL(schema_service->get_udt_sql_service().drop_udt(udt_info,
9427new_schema_version,
9428trans, ddl_stmt_str))) {
9429LOG_WARN("drop udt info failed", K(udt_info), K(ret));
9430}
9431OZ (ObDependencyInfo::delete_schema_object_dependency(trans, tenant_id,
9432udt_info.get_type_id(),
9433new_schema_version,
9434udt_info.get_object_type()));
9435if (OB_SUCC(ret)) {
9436ObErrorInfo error_info;
9437if (OB_FAIL(error_info.handle_error_info(trans, &udt_info))) {
9438LOG_WARN("insert trigger error info failed.", K(ret), K(error_info));
9439}
9440}
9441return ret;
9442}
9443
9444int ObDDLOperator::del_routines_in_udt(const ObUDTTypeInfo &udt_info,
9445ObMySQLTransaction &trans,
9446ObSchemaGetterGuard &schema_guard)
9447{
9448int ret = OB_SUCCESS;
9449ObSchemaService *schema_service_impl = schema_service_.get_schema_service();
9450const uint64_t tenant_id = udt_info.get_tenant_id();
9451uint64_t udt_id = udt_info.get_type_id();
9452if (OB_INVALID_ID == tenant_id
9453|| OB_INVALID_ID == udt_id) {
9454ret = OB_INVALID_ARGUMENT;
9455LOG_WARN("invalid argument", K(ret));
9456} else {
9457ObArray<const ObRoutineInfo *> routines;
9458if (OB_FAIL(schema_guard.get_routine_infos_in_udt(tenant_id, udt_id, routines))) {
9459LOG_WARN("get routines in udt failed", K(tenant_id), K(udt_id), K(ret));
9460} else {
9461for (int64_t i = 0; OB_SUCC(ret) && i < routines.count(); ++i) {
9462const ObRoutineInfo *routine_info = routines.at(i);
9463int64_t new_schema_version = OB_INVALID_VERSION;
9464if (OB_ISNULL(routine_info)) {
9465ret = OB_ERR_UNEXPECTED;
9466LOG_WARN("routine info is NULL", K(ret));
9467} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
9468LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
9469} else if (OB_FAIL(schema_service_impl->get_routine_sql_service().drop_routine(
9470*routine_info, new_schema_version, trans))) {
9471LOG_WARN("drop routine failed", "routine_id", routine_info->get_routine_id(), K(ret));
9472}
9473}
9474}
9475}
9476return ret;
9477}
9478
9479//----Functions for managing UDF----
9480int ObDDLOperator::create_user_defined_function(share::schema::ObUDF &udf_info,
9481common::ObMySQLTransaction &trans,
9482const common::ObString *ddl_stmt_str/*=NULL*/)
9483{
9484int ret = OB_SUCCESS;
9485uint64_t new_udf_id = OB_INVALID_ID;
9486const uint64_t tenant_id = udf_info.get_tenant_id();
9487int64_t new_schema_version = OB_INVALID_VERSION;
9488ObSchemaService *schema_service = schema_service_.get_schema_service();
9489if (OB_ISNULL(schema_service)) {
9490ret = OB_ERR_SYS;
9491LOG_ERROR("schema_service must exist", K(ret));
9492} else if (OB_FAIL(schema_service->fetch_new_udf_id(tenant_id, new_udf_id))) {
9493LOG_WARN("failed to fetch new_udf_id", K(tenant_id), K(ret));
9494} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
9495LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
9496} else {
9497udf_info.set_udf_id(new_udf_id);
9498udf_info.set_schema_version(new_schema_version);
9499if (OB_FAIL(schema_service->get_udf_sql_service().insert_udf(udf_info, &trans, ddl_stmt_str))) {
9500LOG_WARN("insert udf info failed", K(udf_info.get_name_str()), K(ret));
9501}
9502}
9503return ret;
9504}
9505
9506int ObDDLOperator::drop_user_defined_function(const uint64_t tenant_id,
9507const common::ObString &name,
9508common::ObMySQLTransaction &trans,
9509const common::ObString *ddl_stmt_str/*=NULL*/)
9510{
9511int ret = OB_SUCCESS;
9512int64_t new_schema_version = OB_INVALID_VERSION;
9513ObSchemaService *schema_service = schema_service_.get_schema_service();
9514if (OB_UNLIKELY(OB_INVALID_ID == tenant_id)) {
9515ret = OB_INVALID_ARGUMENT;
9516LOG_WARN("invalid arguments", K(tenant_id), K(ret));
9517} else if (OB_ISNULL(schema_service)) {
9518ret = OB_ERR_SYS;
9519LOG_ERROR("schema_service must exist", K(ret));
9520} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
9521LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
9522} else if (OB_FAIL(schema_service->get_udf_sql_service().delete_udf(
9523tenant_id,
9524name,
9525new_schema_version,
9526&trans,
9527ddl_stmt_str))) {
9528LOG_WARN("drop udf failed", K(tenant_id), K(name), K(ret));
9529} else {/*do nothing*/}
9530return ret;
9531}
9532//----End of functions for managing UDF----
9533
9534int ObDDLOperator::insert_ori_schema_version(
9535ObMySQLTransaction &trans,
9536const uint64_t tenant_id,
9537const uint64_t table_id,
9538const int64_t &ori_schema_version)
9539{
9540int ret = OB_SUCCESS;
9541ObSchemaService *schema_service = schema_service_.get_schema_service();
9542if (OB_INVALID_VERSION == ori_schema_version
9543|| !is_valid_tenant_id(tenant_id)) {
9544ret = OB_ERR_UNEXPECTED;
9545LOG_WARN("invalid schema version" , K(ret), K(tenant_id), K(table_id), K(ori_schema_version));
9546} else if (OB_FAIL(schema_service->get_table_sql_service().insert_ori_schema_version(
9547trans, tenant_id, table_id, ori_schema_version))) {
9548LOG_WARN("insert_ori_schema_version failed", K(ret), K(tenant_id), K(table_id), K(ori_schema_version));
9549}
9550return ret;
9551}
9552
9553int ObDDLOperator::insert_temp_table_info(ObMySQLTransaction &trans, const ObTableSchema &table_schema)
9554{
9555int ret = OB_SUCCESS;
9556ObSchemaService *schema_service = schema_service_.get_schema_service();
9557if (OB_ISNULL(schema_service)) {
9558ret = OB_ERR_UNEXPECTED;
9559LOG_WARN("get invalid schema service", K(ret));
9560} else if (table_schema.is_ctas_tmp_table() || table_schema.is_tmp_table()) {
9561if (is_inner_table(table_schema.get_table_id())) {
9562ret = OB_OP_NOT_ALLOW;
9563LOG_WARN("create tmp sys table not allowed", K(ret), "table_id", table_schema.get_table_id());
9564} else if (OB_FAIL(schema_service->get_table_sql_service().insert_temp_table_info(trans, table_schema))) {
9565LOG_WARN("insert_temp_table_info failed", K(ret));
9566}
9567}
9568return ret;
9569}
9570
9571int ObDDLOperator::delete_temp_table_info(ObMySQLTransaction &trans, const ObTableSchema &table_schema)
9572{
9573int ret = OB_SUCCESS;
9574ObSchemaService *schema_service = schema_service_.get_schema_service();
9575if (OB_ISNULL(schema_service)) {
9576ret = OB_ERR_UNEXPECTED;
9577LOG_WARN("get invalid schema service", K(ret));
9578} else if (is_inner_table(table_schema.get_table_id())) {
9579ret = OB_OP_NOT_ALLOW;
9580LOG_WARN("create tmp sys table not allowed", K(ret), "table_id", table_schema.get_table_id());
9581} else if (OB_FAIL(schema_service->get_table_sql_service().delete_from_all_temp_table(
9582trans, table_schema.get_tenant_id(), table_schema.get_table_id()))) {
9583LOG_WARN("insert_temp_table_info failed", K(ret));
9584}
9585return ret;
9586}
9587
9588int ObDDLOperator::drop_trigger_cascade(const ObTableSchema &table_schema,
9589ObMySQLTransaction &trans)
9590{
9591int ret = OB_SUCCESS;
9592ObSchemaGetterGuard schema_guard;
9593const ObIArray<uint64_t> &trigger_list = table_schema.get_trigger_list();
9594const ObTriggerInfo *trigger_info = NULL;
9595uint64_t tenant_id = table_schema.get_tenant_id();
9596uint64_t trigger_id = OB_INVALID_ID;
9597OZ (schema_service_.get_tenant_schema_guard(tenant_id, schema_guard), tenant_id);
9598for (int64_t i = 0; OB_SUCC(ret) && i < trigger_list.count(); i++) {
9599OX (trigger_id = trigger_list.at(i));
9600OZ (schema_guard.get_trigger_info(tenant_id, trigger_id, trigger_info));
9601OV (OB_NOT_NULL(trigger_info));
9602OZ (drop_trigger(*trigger_info, trans, NULL), trigger_info);
9603}
9604return ret;
9605}
9606
9607int ObDDLOperator::drop_inner_generated_index_column(ObMySQLTransaction &trans,
9608ObSchemaGetterGuard &schema_guard,
9609const ObTableSchema &index_schema,
9610ObTableSchema &new_data_table_schema)
9611{
9612int ret = OB_SUCCESS;
9613const ObTableSchema *data_table = NULL;
9614const ObColumnSchemaV2 *index_col = NULL;
9615const uint64_t tenant_id = index_schema.get_tenant_id();
9616uint64_t data_table_id = index_schema.get_data_table_id();
9617const ObIndexInfo &index_info = index_schema.get_index_info();
9618if (OB_FAIL(schema_guard.get_table_schema(tenant_id, data_table_id, data_table))) {
9619LOG_WARN("get table schema failed", KR(ret), K(tenant_id), K(data_table_id));
9620} else if (OB_ISNULL(data_table)) {
9621ret = OB_ERR_UNEXPECTED;
9622LOG_WARN("data table schema is unknown", K(data_table_id));
9623} else if (!new_data_table_schema.is_valid()) {
9624if (OB_FAIL(new_data_table_schema.assign(*data_table))) {
9625LOG_WARN("fail to assign schema", K(ret));
9626}
9627}
9628ObSEArray<ObAuxTableMetaInfo, 16> simple_index_infos;
9629if (OB_FAIL(ret)) {
9630} else if (OB_FAIL(new_data_table_schema.get_simple_index_infos(simple_index_infos))) {
9631LOG_WARN("get simple_index_infos failed", K(ret));
9632} else {
9633new_data_table_schema.set_in_offline_ddl_white_list(index_schema.get_in_offline_ddl_white_list());
9634}
9635for (int64_t i = 0; OB_SUCC(ret) && i < index_info.get_size(); ++i) {
9636// Generated columns on index table are converted to normal column,
9637// we need to get column schema from data table here.
9638if (OB_ISNULL(index_col = data_table->get_column_schema(
9639tenant_id, index_info.get_column(i)->column_id_))) {
9640ret = OB_ERR_UNEXPECTED;
9641LOG_WARN("get index column schema failed", K(ret), K(tenant_id), K(index_info));
9642} else if (index_col->is_hidden() && index_col->is_generated_column()) {
9643// delete the generated column generated internally when the index is created,
9644// This kind of generated column is hidden.
9645// delete generated column in data table for spatial index
9646bool exist_index = false;
9647for (int64_t j = 0; OB_SUCC(ret) && !exist_index && j < simple_index_infos.count(); ++j) {
9648const ObColumnSchemaV2 *tmp_col = NULL;
9649if (simple_index_infos.at(j).table_id_ != index_schema.get_table_id()) {
9650// If there are other indexes on the hidden column, they cannot be deleted.
9651if (OB_FAIL(schema_guard.get_column_schema(tenant_id,
9652simple_index_infos.at(j).table_id_, index_col->get_column_id(), tmp_col))) {
9653LOG_WARN("get column schema from schema guard failed", KR(ret), K(tenant_id),
9654K(simple_index_infos.at(j).table_id_), K(index_col->get_column_id()));
9655} else if (tmp_col != NULL) {
9656exist_index = true;
9657}
9658}
9659}
9660// There are no other indexes, delete the hidden column.
9661if (OB_SUCC(ret) && !exist_index) {
9662// if generate column is not the last column // 1. update prev_column_id // 2. update inner table
9663if (OB_FAIL(update_prev_id_for_delete_column(*data_table, new_data_table_schema, *index_col, trans))) {
9664LOG_WARN("failed to update column previous id for delete column", K(ret));
9665} else if (OB_FAIL(delete_single_column(trans, new_data_table_schema, index_col->get_column_name_str()))) {
9666LOG_WARN("delete index inner generated column failed", K(new_data_table_schema), K(*index_col));
9667}
9668}
9669}
9670}
9671if (OB_SUCC(ret)) {
9672if (OB_FAIL(alter_table_options(schema_guard,
9673new_data_table_schema,
9674*data_table,
9675false,
9676trans))) {
9677LOG_WARN("alter table options failed", K(ret), K(new_data_table_schema));
9678} else {
9679for (int64_t j = 0; OB_SUCC(ret) && j < simple_index_infos.count(); ++j) {
9680if (simple_index_infos.at(j).table_id_ == index_schema.get_table_id()) {
9681simple_index_infos.remove(j);
9682if (OB_FAIL(new_data_table_schema.set_simple_index_infos(simple_index_infos))) {
9683LOG_WARN("fail to set simple index infos", K(ret));
9684}
9685break;
9686}
9687}
9688}
9689}
9690
9691return ret;
9692}
9693
9694int ObDDLOperator::create_tablespace(ObTablespaceSchema &tablespace_schema,
9695common::ObMySQLTransaction &trans,
9696share::schema::ObSchemaGetterGuard &schema_guard,
9697const common::ObString *ddl_stmt_str)
9698{
9699int ret = OB_SUCCESS;
9700UNUSED(schema_guard);
9701uint64_t new_ts_id = OB_INVALID_ID;
9702ObSchemaService *schema_service = schema_service_.get_schema_service();
9703const uint64_t tenant_id = tablespace_schema.get_tenant_id();
9704int64_t new_schema_version = OB_INVALID_VERSION;
9705if (OB_ISNULL(schema_service)) {
9706ret = OB_ERR_UNEXPECTED;
9707LOG_WARN("get invalid schema service", K(ret));
9708} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
9709LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
9710} else {
9711tablespace_schema.set_schema_version(new_schema_version);
9712}
9713if (OB_FAIL(ret)) {
9714/*do nothing*/
9715} else if (OB_FAIL(schema_service->fetch_new_tablespace_id(
9716tablespace_schema.get_tenant_id(), new_ts_id))) {
9717LOG_WARN("failed to fetch_new_table_id", K(ret));
9718} else if (OB_INVALID_ID == new_ts_id) {
9719ret = OB_ERR_UNEXPECTED;
9720LOG_WARN("get invalid schema id", K(ret));
9721} else if (FALSE_IT(tablespace_schema.set_tablespace_id(new_ts_id))) {
9722} else if(OB_FAIL(schema_service->get_tablespace_sql_service().create_tablespace(
9723tablespace_schema, trans, ddl_stmt_str))) {
9724LOG_WARN("create tablespace failed", K(ret));
9725}
9726
9727return ret;
9728}
9729
9730int ObDDLOperator::alter_tablespace(ObTablespaceSchema &tablespace_schema,
9731common::ObMySQLTransaction &trans,
9732share::schema::ObSchemaGetterGuard &schema_guard,
9733const common::ObString *ddl_stmt_str)
9734{
9735int ret = OB_SUCCESS;
9736UNUSED(schema_guard);
9737ObSchemaService *schema_service = schema_service_.get_schema_service();
9738const uint64_t tenant_id = tablespace_schema.get_tenant_id();
9739int64_t new_schema_version = OB_INVALID_VERSION;
9740if (OB_ISNULL(schema_service)) {
9741ret = OB_ERR_UNEXPECTED;
9742LOG_WARN("get invalid schema service", K(ret));
9743} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
9744LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
9745} else {
9746tablespace_schema.set_schema_version(new_schema_version);
9747}
9748if (OB_FAIL(ret)) {
9749/*do nothing*/
9750} else if(OB_FAIL(schema_service->get_tablespace_sql_service().alter_tablespace(
9751tablespace_schema, trans, ddl_stmt_str))) {
9752LOG_WARN("alter tablespace failed", K(ret));
9753} else {
9754// Change of tablespace state, change the cascading table_schema attribute under tablespace
9755// Currently only supports encryption, it needs to be changed, so there is no more judgment here
9756common::ObArray<const share::schema::ObTableSchema *> table_schemas;
9757if (OB_FAIL(schema_guard.get_table_schemas_in_tenant(tenant_id, table_schemas))) {
9758LOG_WARN("fail to get table schemas in tenant", K(ret));
9759} else {
9760for (int i = 0; i < table_schemas.count() && OB_SUCC(ret); ++i) {
9761if (table_schemas.at(i)->get_tablespace_id() != OB_INVALID_ID &&
9762table_schemas.at(i)->get_tablespace_id() == tablespace_schema.get_tablespace_id()) {
9763if (OB_FAIL(update_tablespace_table(table_schemas.at(i), trans, tablespace_schema))) {
9764LOG_WARN("fail to push back table schema", K(ret));
9765}
9766}
9767}
9768}
9769}
9770return ret;
9771}
9772
9773int ObDDLOperator::update_tablespace_table(
9774const share::schema::ObTableSchema *table_schema,
9775common::ObMySQLTransaction &trans,
9776ObTablespaceSchema &tablespace_schema)
9777{
9778int ret = OB_SUCCESS;
9779// Currently this interface only supports cascading update encryption
9780share::schema::ObTableSchema new_table_schema;
9781ObSchemaService *schema_service = schema_service_.get_schema_service();
9782int64_t tenant_id = table_schema->get_tenant_id();
9783int64_t new_schema_version = OB_INVALID_VERSION;
9784if (OB_ISNULL(schema_service)) {
9785ret = OB_ERR_UNEXPECTED;
9786LOG_WARN("get invalid schema service", K(ret));
9787} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
9788LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
9789} else if (OB_FAIL(new_table_schema.assign(*table_schema))) {
9790LOG_WARN("fail to assign table schema", K(ret));
9791} else {
9792new_table_schema.set_schema_version(new_schema_version);
9793new_table_schema.set_encryption_str(tablespace_schema.get_encryption_name());
9794if (OB_FAIL(schema_service->get_table_sql_service().update_table_options(trans,
9795*table_schema, new_table_schema, OB_DDL_ALTER_TABLE, NULL))) {
9796LOG_WARN("fail to update data table schema version", K(ret),
9797K(new_table_schema.get_table_id()));
9798}
9799}
9800return ret;
9801}
9802
9803int ObDDLOperator::drop_tablespace(ObTablespaceSchema &tablespace_schema,
9804common::ObMySQLTransaction &trans,
9805share::schema::ObSchemaGetterGuard &schema_guard,
9806const common::ObString *ddl_stmt_str)
9807{
9808int ret = OB_SUCCESS;
9809ObArray<const ObTableSchema *> tables;
9810ObSchemaService *schema_service = schema_service_.get_schema_service();
9811uint64_t tablespace_id = tablespace_schema.get_tablespace_id();
9812const uint64_t tenant_id = tablespace_schema.get_tenant_id();
9813int64_t new_schema_version = OB_INVALID_VERSION;
9814if (OB_ISNULL(schema_service)) {
9815ret = OB_ERR_UNEXPECTED;
9816LOG_WARN("get invalid schema service", K(ret));
9817} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
9818LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
9819} else {
9820tablespace_schema.set_schema_version(new_schema_version);
9821}
9822if (OB_FAIL(ret)) {
9823/*do nothing*/
9824} else if (OB_FAIL(schema_guard.get_table_schemas_in_tablespace(tenant_id, tablespace_id, tables))) {
9825LOG_WARN("get tables in tablespace failed", K(tenant_id), KT(tablespace_id), K(ret));
9826} else if (tables.count() > 0) {
9827ret = OB_TABLESPACE_DELETE_NOT_EMPTY;
9828LOG_WARN("can not delete a tablespace which is not empty", K(ret));
9829} else if (OB_FAIL(schema_service->get_tablespace_sql_service().drop_tablespace(
9830tablespace_schema, trans, ddl_stmt_str))) {
9831LOG_WARN("drop tablespace failed", K(ret));
9832}
9833return ret;
9834}
9835
9836// revise column info of check constraints
9837int ObDDLOperator::revise_constraint_column_info(
9838obrpc::ObSchemaReviseArg arg,
9839common::ObMySQLTransaction &trans)
9840{
9841int ret = OB_SUCCESS;
9842const uint64_t tenant_id = arg.tenant_id_;
9843int64_t new_schema_version = OB_INVALID_VERSION;
9844ObSchemaService *schema_service = schema_service_.get_schema_service();
9845ObSchemaGetterGuard schema_guard;
9846const ObTableSchema *table_schema = nullptr;
9847if (OB_ISNULL(schema_service)) {
9848ret = OB_ERR_UNEXPECTED;
9849LOG_WARN("schema_service is NULL", K(ret));
9850} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
9851LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
9852} else {
9853for (int64_t i = 0; OB_SUCC(ret) && i < arg.csts_array_.count(); ++i) {
9854arg.csts_array_.at(i).set_schema_version(new_schema_version);
9855}
9856}
9857if (OB_SUCC(ret)) {
9858if (OB_FAIL(schema_service_.get_tenant_schema_guard(tenant_id, schema_guard))) {
9859LOG_WARN("get tenant schema guard failed", K(ret), K(tenant_id));
9860} else if (OB_FAIL(schema_guard.get_table_schema(tenant_id, arg.table_id_, table_schema))) {
9861LOG_WARN("get table schema failed", K(ret), K(tenant_id), K(arg.table_id_));
9862} else if (nullptr == table_schema) {
9863ret = OB_ERR_UNEXPECTED;
9864LOG_WARN("error unexpected, table schema must not be nullptr", K(ret));
9865} else if (OB_FAIL(schema_service->get_table_sql_service().revise_check_cst_column_info(
9866trans, *table_schema, arg.csts_array_))) {
9867LOG_WARN("insert single constraint failed", K(ret), K(arg.csts_array_));
9868} else if (OB_FAIL(schema_service->get_table_sql_service().update_data_table_schema_version(
9869trans, tenant_id, arg.table_id_, table_schema->get_in_offline_ddl_white_list()))) {
9870LOG_WARN("update data_table_schema version failed", K(ret), K(arg.table_id_));
9871}
9872}
9873return ret;
9874}
9875
9876// revise info of not null constraints
9877int ObDDLOperator::revise_not_null_constraint_info(
9878obrpc::ObSchemaReviseArg arg,
9879share::schema::ObSchemaGetterGuard &schema_guard,
9880common::ObMySQLTransaction &trans)
9881{
9882int ret = OB_SUCCESS;
9883const uint64_t tenant_id = arg.tenant_id_;
9884const uint64_t table_id = arg.table_id_;
9885int64_t new_schema_version = OB_INVALID_VERSION;
9886ObSchemaService *schema_service = schema_service_.get_schema_service();
9887const ObTableSchema *ori_table_schema = NULL;
9888ObSEArray<const ObColumnSchemaV2 *, 16> not_null_cols;
9889const bool update_object_status_ignore_version = false;
9890if (OB_ISNULL(schema_service)) {
9891ret = OB_ERR_UNEXPECTED;
9892LOG_WARN("schema_service is NULL", K(ret));
9893} else if (OB_FAIL(schema_guard.get_table_schema(tenant_id, table_id, ori_table_schema))) {
9894LOG_WARN("get table schema faield", K(ret), K(tenant_id), K(table_id));
9895} else if (OB_ISNULL(ori_table_schema)) {
9896ret = OB_ERR_UNEXPECTED;
9897LOG_WARN("table schema is null", K(ret), K(table_id), K(tenant_id));
9898} else {
9899bool is_heap_table = ori_table_schema->is_heap_table();
9900ObTableSchema::const_column_iterator col_iter = ori_table_schema->column_begin();
9901ObTableSchema::const_column_iterator col_iter_end = ori_table_schema->column_end();
9902for (; col_iter != col_iter_end && OB_SUCC(ret); col_iter++) {
9903if (OB_ISNULL(*col_iter)) {
9904ret = OB_ERR_UNEXPECTED;
9905LOG_WARN("column schema is null", K(ret), KPC(ori_table_schema));
9906} else if (!(*col_iter)->is_nullable() && !(*col_iter)->is_hidden()) {
9907if (!is_heap_table && (*col_iter)->is_rowkey_column()) {
9908// do nothing for rowkey columns.
9909// not filter rowkey column of no_pk_table since it may be partition key and can be null.
9910} else if (OB_UNLIKELY((*col_iter)->has_not_null_constraint())) {
9911ret = OB_ERR_UNEXPECTED;
9912LOG_WARN("column with not null attr should not have not null constraint", K(ret),
9913KPC(*col_iter));
9914} else if (OB_FAIL(not_null_cols.push_back(*col_iter))) {
9915LOG_WARN("push back failed", K(ret));
9916}
9917}
9918}
9919}
9920
9921if (OB_FAIL(ret) || 0 == not_null_cols.count()) {
9922// do nothing
9923} else if (OB_FAIL(schema_service_.gen_new_schema_version(arg.tenant_id_, new_schema_version))) {
9924LOG_WARN("fail to gen new schema_version", K(ret), K(arg.tenant_id_));
9925} else {
9926bool is_oracle_mode = false;
9927uint64_t new_cst_id = OB_INVALID_ID;
9928if (OB_FAIL(ori_table_schema->check_if_oracle_compat_mode(is_oracle_mode))) {
9929LOG_WARN("fail check if oracle mode", K(ret));
9930}
9931for (int64_t i = 0; OB_SUCC(ret) && i < not_null_cols.count(); ++i) {
9932ObArenaAllocator allocator("ReviseNotNulCst");
9933ObString cst_name;
9934ObString check_expr_str;
9935ObConstraint cst;
9936const ObColumnSchemaV2 *col_schema = not_null_cols.at(i);
9937uint64_t column_id = col_schema->get_column_id();
9938bool cst_name_generated = false;
9939ObColumnSchemaV2 new_col_schema;
9940if (OB_FAIL(ObTableSchema::create_cons_name_automatically_with_dup_check(cst_name,
9941ori_table_schema->get_table_name_str(),
9942allocator,
9943CONSTRAINT_TYPE_NOT_NULL,
9944schema_guard,
9945tenant_id,
9946ori_table_schema->get_database_id(),
994710, /* retry_times */
9948cst_name_generated,
9949true))) {
9950LOG_WARN("create cons name automatically failed", K(ret));
9951} else if (OB_UNLIKELY(!cst_name_generated)) {
9952ret = OB_ERR_UNEXPECTED;
9953LOG_WARN("duplicate name constraint already exists", K(ret), KPC(ori_table_schema));
9954} else if (OB_FAIL(ObResolverUtils::create_not_null_expr_str(
9955col_schema->get_column_name_str(), allocator, check_expr_str, is_oracle_mode))) {
9956LOG_WARN("create not null expr str failed", K(ret));
9957} else if (OB_FAIL(schema_service->fetch_new_constraint_id(tenant_id, new_cst_id))) {
9958LOG_WARN("failed to fetch new constraint id", K(ret));
9959} else if (OB_FAIL(new_col_schema.assign(*col_schema))) {
9960LOG_WARN("fail to assign column schema", KR(ret));
9961} else {
9962const bool only_history = false;
9963const bool need_to_deal_with_cst_cols = true;
9964const bool do_cst_revise = true;
9965cst.set_tenant_id(tenant_id);
9966cst.set_table_id(table_id);
9967cst.set_constraint_id(new_cst_id);
9968cst.set_schema_version(new_schema_version);
9969cst.set_constraint_name(cst_name);
9970cst.set_name_generated_type(GENERATED_TYPE_SYSTEM);
9971cst.set_check_expr(check_expr_str);
9972cst.set_constraint_type(CONSTRAINT_TYPE_NOT_NULL);
9973cst.set_rely_flag(false);
9974cst.set_enable_flag(true);
9975cst.set_validate_flag(CST_FK_VALIDATED);
9976
9977new_col_schema.set_schema_version(new_schema_version);
9978new_col_schema.add_not_null_cst();
9979new_col_schema.set_nullable(true);
9980if (OB_FAIL(cst.assign_not_null_cst_column_id(column_id))) {
9981LOG_WARN("assign not null constraint column id failed", K(ret));
9982} else if (OB_FAIL(schema_service->get_table_sql_service().add_single_constraint(
9983trans, cst, only_history,need_to_deal_with_cst_cols, do_cst_revise))) {
9984LOG_WARN("add single constraint failed", K(ret), K(cst));
9985} else if (OB_FAIL(schema_service->get_table_sql_service().update_single_column(
9986trans, *ori_table_schema, *ori_table_schema, new_col_schema, false))) {
9987LOG_WARN("update single column failed", K(ret));
9988}
9989}
9990}
9991if (OB_SUCC(ret)) {
9992ObTableSchema new_table_schema;
9993if (OB_FAIL(new_table_schema.assign(*ori_table_schema))) {
9994LOG_WARN("assign table schema failed", K(ret));
9995} else {
9996new_table_schema.set_schema_version(new_schema_version);
9997if (OB_FAIL(schema_service->get_table_sql_service().update_table_attribute(
9998trans, new_table_schema, OB_DDL_ADD_CONSTRAINT, update_object_status_ignore_version))) {
9999LOG_WARN("update table attribute faield", K(ret));
10000}
10001}
10002}
10003}
10004LOG_INFO("revise not null constraint info", K(ret), K(arg));
10005return ret;
10006}
10007
10008int ObDDLOperator::create_keystore(ObKeystoreSchema &keystore_schema,
10009common::ObMySQLTransaction &trans,
10010share::schema::ObSchemaGetterGuard &schema_guard,
10011const common::ObString *ddl_stmt_str)
10012{
10013int ret = OB_SUCCESS;
10014UNUSED(schema_guard);
10015uint64_t new_ts_id = OB_INVALID_ID;
10016const uint64_t tenant_id = keystore_schema.get_tenant_id();
10017ObSchemaService *schema_service = schema_service_.get_schema_service();
10018int64_t new_schema_version = OB_INVALID_VERSION;
10019if (OB_ISNULL(schema_service)) {
10020ret = OB_ERR_UNEXPECTED;
10021LOG_WARN("get invalid schema service", K(ret));
10022} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
10023LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
10024} else {
10025keystore_schema.set_schema_version(new_schema_version);
10026}
10027if (OB_FAIL(ret)) {
10028/*do nothing*/
10029} else if (is_mysql_inner_keystore_id(keystore_schema.get_keystore_id())) {
10030/* If the id is equal to the internal keystore id, no fetch is required */
10031} else if (OB_FAIL(schema_service->fetch_new_keystore_id(
10032keystore_schema.get_tenant_id(), new_ts_id))) {
10033LOG_WARN("failed to fetch_new_table_id", K(ret));
10034} else if (OB_INVALID_ID == new_ts_id) {
10035ret = OB_ERR_UNEXPECTED;
10036LOG_WARN("get invalid schema id", K(ret));
10037} else if (FALSE_IT(keystore_schema.set_keystore_id(new_ts_id))) {
10038}
10039
10040if (OB_SUCC(ret)) {
10041if (OB_FAIL(schema_service->get_keystore_sql_service().create_keystore(
10042keystore_schema, trans, ddl_stmt_str))) {
10043LOG_WARN("create keystore failed", K(ret));
10044}
10045}
10046
10047return ret;
10048}
10049
10050int ObDDLOperator::alter_keystore(ObKeystoreSchema &keystore_schema,
10051common::ObMySQLTransaction &trans,
10052share::schema::ObSchemaGetterGuard &schema_guard,
10053const common::ObString *ddl_stmt_str,
10054bool &is_set_key,
10055bool is_kms)
10056{
10057int ret = OB_SUCCESS;
10058uint64_t new_ms_id = OB_INVALID_ID;
10059ObSchemaService *schema_service = schema_service_.get_schema_service();
10060const uint64_t tenant_id = keystore_schema.get_tenant_id();
10061int64_t new_schema_version = OB_INVALID_VERSION;
10062if (OB_ISNULL(schema_service)) {
10063ret = OB_ERR_UNEXPECTED;
10064LOG_WARN("get invalid schema service", K(ret));
10065} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
10066LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
10067} else {
10068keystore_schema.set_schema_version(new_schema_version);
10069}
10070if (OB_FAIL(ret)) {
10071/*do nothing*/
10072} else if (is_set_key && !is_kms) {
10073if (OB_FAIL(schema_service->fetch_new_master_key_id(
10074keystore_schema.get_tenant_id(), new_ms_id))) {
10075LOG_WARN("failed to fetch_new_master_id", K(ret));
10076} else if (OB_INVALID_ID == new_ms_id) {
10077ret = OB_ERR_UNEXPECTED;
10078LOG_WARN("get invalid master id", K(ret));
10079} else if (FALSE_IT(keystore_schema.set_master_key_id(new_ms_id))) {
10080}
10081}
10082
10083if (OB_FAIL(ret)) {
10084/*do nothing*/
10085} else if(OB_FAIL(schema_service->get_keystore_sql_service().alter_keystore(
10086keystore_schema, trans, ddl_stmt_str))) {
10087LOG_WARN("alter keystore failed", K(ret));
10088} else {
10089// The keystore state changes, the table_schema version needs to be pushed up.
10090common::ObArray<const share::schema::ObSimpleTableSchemaV2 *> table_schemas;
10091if (OB_FAIL(schema_guard.get_table_schemas_in_tenant(tenant_id, table_schemas))) {
10092LOG_WARN("fail to get table schemas in tenant", K(ret));
10093}
10094common::ObArray<const share::schema::ObSimpleTableSchemaV2 *> need_encrypt_table_schemas;
10095for (int i = 0; i < table_schemas.count() && OB_SUCC(ret); ++i) {
10096if (table_schemas.at(i)->need_encrypt()) {
10097ret = need_encrypt_table_schemas.push_back(table_schemas.at(i));
10098}
10099}
10100for (int i = 0; i < need_encrypt_table_schemas.count() && OB_SUCC(ret); ++i) {
10101const ObSimpleTableSchemaV2 *table_schema = need_encrypt_table_schemas.at(i);
10102if (OB_FAIL(schema_service->get_table_sql_service().update_data_table_schema_version(trans,
10103tenant_id, table_schema->get_table_id(), table_schema->get_in_offline_ddl_white_list()))) {
10104LOG_WARN("fail to update data table schema version", K(ret), "id", table_schema->get_table_id());
10105}
10106}
10107}
10108return ret;
10109}
10110
10111int ObDDLOperator::handle_label_se_policy_function(ObSchemaOperationType ddl_type,
10112const ObString &ddl_stmt_str,
10113ObSchemaGetterGuard &schema_guard,
10114ObLabelSePolicySchema &schema,
10115ObMySQLTransaction &trans)
10116{
10117int ret = OB_SUCCESS;
10118uint64_t tenant_id = schema.get_tenant_id();
10119ObSchemaService *schema_sql_service = NULL;
10120
10121if (OB_ISNULL(schema_sql_service = schema_service_.get_schema_service())) {
10122ret = OB_ERR_SYS;
10123LOG_ERROR("schema_sql_service must not null", K(ret));
10124}
10125
10126if (OB_SUCC(ret)) {
10127int64_t new_schema_version = OB_INVALID_VERSION;
10128
10129if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
10130LOG_WARN("fail to gen new schema_version", K(ret));
10131} else {
10132schema.set_schema_version(new_schema_version);
10133}
10134}
10135
10136if (OB_SUCC(ret)) {
10137bool is_policy_name_exist = false;
10138bool is_column_name_exist = false;
10139const ObLabelSePolicySchema *old_schema = NULL;
10140const ObLabelSePolicySchema *temp_schema = NULL;
10141
10142//check if the schema is already existed
10143if (OB_SUCC(ret)) {
10144if (OB_FAIL(schema_guard.get_label_se_policy_schema_by_name(
10145tenant_id, schema.get_policy_name_str(), old_schema))) {
10146LOG_WARN("fail to check schema exist", K(ret));
10147} else if (FALSE_IT(is_policy_name_exist = OB_NOT_NULL(old_schema))) {
10148} else if (!schema.get_column_name_str().empty()
10149&& OB_FAIL(schema_guard.get_label_se_policy_schema_by_column_name(
10150tenant_id, schema.get_column_name_str(), temp_schema))) {
10151LOG_WARN("fail to check schema exist", K(ret));
10152} else if (FALSE_IT(is_column_name_exist = OB_NOT_NULL(temp_schema))) {
10153}
10154}
10155
10156//other stuff case by case
10157if (OB_SUCC(ret)) {
10158uint64_t label_se_policy_id = OB_INVALID_ID;
10159
10160switch (ddl_type) {
10161case OB_DDL_CREATE_LABEL_SE_POLICY:
10162if (OB_UNLIKELY(is_policy_name_exist | is_column_name_exist)) {
10163ret = is_policy_name_exist ? OB_OBJECT_NAME_EXIST : OB_ERR_COLUMN_DUPLICATE;
10164LOG_WARN("policy already existed", K(ret), K(tenant_id));
10165} else if (OB_FAIL(schema_sql_service->fetch_new_label_se_policy_id(tenant_id, label_se_policy_id))) {
10166LOG_WARN("fail to fetch new label se policy id", K(tenant_id), K(ret));
10167} else {
10168schema.set_label_se_policy_id(label_se_policy_id);
10169}
10170break;
10171case OB_DDL_ALTER_LABEL_SE_POLICY:
10172case OB_DDL_DROP_LABEL_SE_POLICY:
10173if (OB_UNLIKELY(!is_policy_name_exist)) {
10174ret = OB_OBJECT_NAME_NOT_EXIST;
10175LOG_WARN("policy not existed", K(ret), K(tenant_id));
10176} else {
10177schema.set_label_se_policy_id(old_schema->get_label_se_policy_id());
10178schema.set_column_name(old_schema->get_column_name_str());
10179if (schema.get_default_options() < 0) {
10180schema.set_default_options(old_schema->get_default_options());
10181}
10182if (schema.get_flag() < 0) {
10183schema.set_flag(old_schema->get_flag());
10184}
10185}
10186break;
10187default:
10188ret = OB_ERR_UNEXPECTED;
10189LOG_WARN("unknown ddl type", K(ret), K(ddl_type));
10190}
10191}
10192}
10193
10194if (OB_SUCC(ret)) {
10195if (OB_FAIL(schema_sql_service->get_label_se_policy_sql_service()
10196.apply_new_schema(schema, trans, ddl_type, ddl_stmt_str))) {
10197LOG_WARN("create policy info failed", K(ret));
10198}
10199}
10200
10201LOG_DEBUG("ddl operator create plan label security policy", K(schema));
10202return ret;
10203}
10204
10205int ObDDLOperator::handle_label_se_component_function(ObSchemaOperationType ddl_type,
10206const ObString &ddl_stmt_str,
10207const ObString &policy_name,
10208ObSchemaGetterGuard &schema_guard,
10209ObLabelSeComponentSchema &schema,
10210ObMySQLTransaction &trans)
10211{
10212int ret = OB_SUCCESS;
10213uint64_t tenant_id = schema.get_tenant_id();
10214ObSchemaService *schema_sql_service = NULL;
10215
10216if (OB_ISNULL(schema_sql_service = schema_service_.get_schema_service())) {
10217ret = OB_ERR_SYS;
10218LOG_ERROR("schema_sql_service must not null", K(ret));
10219}
10220
10221if (OB_SUCC(ret)) {
10222int64_t new_schema_version = OB_INVALID_VERSION;
10223
10224if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
10225LOG_WARN("fail to gen new schema_version", K(ret));
10226} else {
10227schema.set_schema_version(new_schema_version);
10228}
10229}
10230
10231if (OB_SUCC(ret)) {
10232
10233//check and get policy id of the policy_name
10234uint64_t policy_id = OB_INVALID_ID;
10235if (OB_SUCC(ret)) {
10236const ObLabelSePolicySchema *policy_schema = NULL;
10237if (OB_FAIL(schema_guard.get_label_se_policy_schema_by_name(tenant_id,
10238policy_name,
10239policy_schema))) {
10240LOG_WARN("fail to check schema exist", K(ret));
10241} else if (OB_ISNULL(policy_schema)) {
10242ret = OB_ERR_POLICY_STRING_NOT_FOUND;
10243LOG_WARN("the schema exists, but the schema pointer is NULL", K(ret));
10244} else {
10245policy_id = policy_schema->get_label_se_policy_id();
10246}
10247}
10248
10249bool is_schema_exist = false;
10250const ObLabelSeComponentSchema *old_schema = NULL;
10251if (OB_SUCC(ret)) {
10252if (OB_FAIL(schema_guard.get_label_se_component_schema_by_comp_num(tenant_id,
10253policy_id,
10254schema.get_comp_type(),
10255schema.get_comp_num(),
10256old_schema))) {
10257LOG_WARN("fail to check schema exist", K(ret));
10258} else {
10259is_schema_exist = (old_schema != NULL);
10260}
10261}
10262
10263//check short name unique
10264if (OB_SUCC(ret) && !schema.get_short_name_str().empty()
10265&& (!is_schema_exist || old_schema->get_short_name_str() != schema.get_short_name_str())) {
10266bool is_exist = false;
10267if (OB_FAIL(schema_service_.check_label_se_component_short_name_exist(tenant_id,
10268policy_id,
10269schema.get_comp_type(),
10270schema.get_short_name_str(),
10271is_exist))) {
10272LOG_WARN("check component short name failed", K(ret));
10273} else if (is_exist) {
10274ret = OB_OBJECT_NAME_EXIST; //TODO [label]
10275}
10276}
10277//check the long name unique
10278if (OB_SUCC(ret) && !schema.get_long_name_str().empty()
10279&& (!is_schema_exist || old_schema->get_long_name_str() != schema.get_long_name_str())) {
10280bool is_exist = false;
10281if (OB_FAIL(schema_service_.check_label_se_component_long_name_exist(tenant_id,
10282policy_id,
10283schema.get_comp_type(),
10284schema.get_long_name_str(),
10285is_exist))) {
10286LOG_WARN("check component long name failed", K(ret));
10287} else if (is_exist) {
10288ret = OB_OBJECT_NAME_EXIST; //TODO [label]
10289}
10290}
10291
10292//other stuff case by case
10293if (OB_SUCC(ret)) {
10294uint64_t label_se_component_id = OB_INVALID_ID;
10295
10296schema.set_label_se_policy_id(policy_id);
10297
10298switch (ddl_type) {
10299case OB_DDL_CREATE_LABEL_SE_LEVEL:
10300if (OB_UNLIKELY(is_schema_exist)) {
10301ret = OB_OBJECT_NAME_EXIST;
10302LOG_WARN("level already existed", K(ret), K(tenant_id));
10303} else if (OB_FAIL(schema_sql_service->fetch_new_label_se_component_id(tenant_id, label_se_component_id))) {
10304LOG_WARN("fail to fetch new label se policy id", K(tenant_id), K(ret));
10305} else {
10306schema.set_label_se_component_id(label_se_component_id);
10307}
10308break;
10309case OB_DDL_ALTER_LABEL_SE_LEVEL:
10310case OB_DDL_DROP_LABEL_SE_LEVEL:
10311if (OB_UNLIKELY(!is_schema_exist)) {
10312ret = OB_OBJECT_NAME_NOT_EXIST;
10313LOG_WARN("level not exist", K(ret), K(schema));
10314} else {
10315schema.set_label_se_component_id(old_schema->get_label_se_component_id());
10316if (schema.get_short_name_str().empty()) {
10317schema.set_short_name(old_schema->get_short_name_str());
10318}
10319if (schema.get_long_name_str().empty()) {
10320schema.set_long_name(old_schema->get_long_name_str());
10321}
10322if (schema.get_parent_name_str().empty()) {
10323schema.set_parent_name(old_schema->get_parent_name_str());
10324}
10325}
10326break;
10327default:
10328ret = OB_ERR_UNEXPECTED;
10329LOG_WARN("unknown ddl type", K(ret), K(ddl_type));
10330}
10331}
10332
10333// When deleting component, it need to check whether the component is used in the label
10334if (OB_SUCC(ret) && OB_DDL_DROP_LABEL_SE_LEVEL == ddl_type) {
10335ObSEArray<const ObLabelSeLabelSchema *, 4> label_schemas;
10336if (OB_FAIL(schema_guard.get_label_se_label_infos_in_tenant(tenant_id, label_schemas))) {
10337LOG_WARN("fail to get label se schemas", K(ret));
10338}
10339
10340const ObLabelSeLabelSchema *label_schema = NULL;
10341ObLabelSeDecomposedLabel label_comps;
10342ObLabelSeLabelCompNums label_nums;
10343
10344for (int64_t i = 0; OB_SUCC(ret) && i < label_schemas.count(); ++i) {
10345label_comps.reset();
10346if (OB_ISNULL(label_schema = label_schemas[i])) {
10347ret = OB_ERR_UNEXPECTED;
10348LOG_WARN("label schema is NULL", K(ret));
10349} else if (OB_FAIL(ObLabelSeResolver::resolve_label_text(label_schema->get_label_str(),
10350label_comps))) {
10351LOG_WARN("fail to resolve label text", K(ret));
10352} else if (OB_FAIL(ObLabelSeUtil::convert_label_comps_name_to_num(tenant_id,
10353policy_id,
10354schema_guard,
10355label_comps,
10356label_nums))) {
10357LOG_WARN("fail to convert comp names to num", K(ret));
10358} else if (label_nums.level_num_ == schema.get_comp_num()) {
10359ret = OB_ERR_LBAC_ERROR;
10360LOG_USER_ERROR(OB_ERR_LBAC_ERROR, "level in use by existing labels");
10361}
10362}
10363}
10364}
10365
10366if (OB_SUCC(ret)) {
10367if (OB_FAIL(schema_sql_service->get_label_se_policy_sql_service()
10368.apply_new_schema(schema, trans, ddl_type, ddl_stmt_str))) {
10369LOG_WARN("create policy info failed", K(ret));
10370}
10371}
10372LOG_DEBUG("ddl operator create plan label security policy", K(schema));
10373return ret;
10374}
10375
10376
10377int ObDDLOperator::handle_label_se_label_function(ObSchemaOperationType ddl_type,
10378const ObString &ddl_stmt_str,
10379const ObString &policy_name,
10380ObSchemaGetterGuard &schema_guard,
10381ObLabelSeLabelSchema &schema,
10382ObMySQLTransaction &trans)
10383{
10384int ret = OB_SUCCESS;
10385uint64_t tenant_id = schema.get_tenant_id();
10386ObSchemaService *schema_sql_service = NULL;
10387
10388if (OB_ISNULL(schema_sql_service = schema_service_.get_schema_service())) {
10389ret = OB_ERR_SYS;
10390LOG_ERROR("schema_sql_service must not null", K(ret));
10391}
10392
10393if (OB_SUCC(ret)) {
10394int64_t new_schema_version = OB_INVALID_VERSION;
10395
10396if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
10397LOG_WARN("fail to gen new schema_version", K(ret));
10398} else {
10399schema.set_schema_version(new_schema_version);
10400}
10401}
10402
10403if (OB_SUCC(ret)) {
10404//check and get policy id of the policy_name
10405uint64_t policy_id = OB_INVALID_ID;
10406if (OB_SUCC(ret)) {
10407const ObLabelSePolicySchema *policy_schema = NULL;
10408if (OB_FAIL(schema_guard.get_label_se_policy_schema_by_name(tenant_id,
10409policy_name,
10410policy_schema))) {
10411LOG_WARN("fail to check schema exist", K(ret));
10412} else if (OB_ISNULL(policy_schema)) {
10413ret = OB_ERR_POLICY_STRING_NOT_FOUND;
10414LOG_WARN("the schema exists, but the schema pointer is NULL", K(ret));
10415} else {
10416policy_id = policy_schema->get_label_se_policy_id();
10417}
10418}
10419
10420//check label schema secondly
10421const ObLabelSeLabelSchema *old_schema = NULL;
10422const ObLabelSeLabelSchema *temp_schema = NULL;
10423bool is_label_tag_exist = false;
10424bool is_label_name_exist = false;
10425if (OB_SUCC(ret)) {
10426if (OB_FAIL(schema_guard.get_label_se_label_by_label_tag(tenant_id,
10427schema.get_label_tag(),
10428old_schema))) {
10429LOG_WARN("fail to check schema exist", K(ret));
10430} else if (FALSE_IT(is_label_tag_exist = OB_NOT_NULL(old_schema))) {
10431} else if (!schema.get_label_str().empty()
10432&& OB_FAIL(schema_guard.get_label_se_label_schema_by_name(tenant_id,
10433schema.get_label_str(),
10434temp_schema))) {
10435LOG_WARN("fail to check schema exist", K(ret));
10436} else if (FALSE_IT(is_label_name_exist = OB_NOT_NULL(temp_schema))) {
10437}
10438}
10439
10440//other stuff case by case
10441if (OB_SUCC(ret)) {
10442uint64_t label_se_label_id = OB_INVALID_ID;
10443
10444schema.set_label_se_policy_id(policy_id);
10445
10446switch (ddl_type) {
10447case OB_DDL_CREATE_LABEL_SE_LABEL:
10448if (OB_UNLIKELY(is_label_tag_exist | is_label_name_exist)) {
10449ret = OB_OBJECT_NAME_EXIST;
10450LOG_WARN("policy already existed", K(ret), K(schema));
10451} else if (OB_FAIL(schema_sql_service->fetch_new_label_se_label_id(tenant_id, label_se_label_id))) {
10452LOG_WARN("fail to fetch new label se policy id", K(schema), K(ret));
10453} else {
10454schema.set_label_se_label_id(label_se_label_id);
10455}
10456break;
10457case OB_DDL_ALTER_LABEL_SE_LABEL:
10458case OB_DDL_DROP_LABEL_SE_LABEL:
10459if (OB_UNLIKELY(!is_label_tag_exist)) {
10460ret = OB_OBJECT_NAME_NOT_EXIST;
10461LOG_WARN("policy not existed", K(ret), K(schema));
10462} else {
10463schema.set_label_se_label_id(old_schema->get_label_se_label_id());
10464if (schema.get_label_str().empty()) {
10465schema.set_label(old_schema->get_label());
10466} else if (old_schema->get_label_str() == schema.get_label_str()) {
10467//do nothing
10468} else if (OB_UNLIKELY(is_label_name_exist)) {
10469ret = OB_OBJECT_NAME_EXIST;
10470LOG_WARN("policy existed", K(ret), K(schema));
10471}
10472if (schema.get_flag() < 0) {
10473schema.set_flag(old_schema->get_flag());
10474} else {
10475if (ddl_type == OB_DDL_ALTER_LABEL_SE_LABEL
10476&& old_schema->get_flag() != 0 && schema.get_flag() == 0) {
10477ret = OB_ERR_LBAC_ERROR;
10478LOG_USER_ERROR(OB_ERR_LBAC_ERROR, "cannot change data label to user label");
10479}
10480}
10481}
10482break;
10483default:
10484ret = OB_ERR_UNEXPECTED;
10485LOG_WARN("unknown ddl type", K(ret), K(ddl_type));
10486}
10487}
10488}
10489
10490if (OB_SUCC(ret)) {
10491if (OB_FAIL(schema_sql_service->get_label_se_policy_sql_service()
10492.apply_new_schema(schema, trans, ddl_type, ddl_stmt_str))) {
10493LOG_WARN("create policy info failed", K(ret));
10494}
10495}
10496
10497LOG_DEBUG("ddl operator handle label schema", K(schema));
10498return ret;
10499}
10500
10501int ObDDLOperator::drop_all_label_se_table_column(uint64_t tenant_id,
10502uint64_t policy_id,
10503ObMySQLTransaction &trans,
10504ObSchemaGetterGuard &schema_guard)
10505{
10506int ret = OB_SUCCESS;
10507ObSEArray<const ObTableSchema *, 32> tables;
10508const ObLabelSePolicySchema *policy = NULL;
10509ObString policy_column_name;
10510ObSchemaService *schema_service = NULL;
10511
10512if (OB_ISNULL(schema_service = schema_service_.get_schema_service())) {
10513ret = OB_ERR_UNEXPECTED;
10514LOG_WARN("schema service is null", K(ret));
10515} else if (OB_FAIL(schema_guard.get_table_schemas_in_tenant(tenant_id, tables))) {
10516LOG_WARN("fail to get all table schemas", K(ret));
10517} else if (OB_FAIL(schema_guard.get_label_se_policy_schema_by_id(tenant_id, policy_id, policy))) {
10518LOG_WARN("fail to get policy schema", K(ret));
10519} else if (OB_ISNULL(policy)) {
10520ret = OB_ERR_UNEXPECTED;
10521LOG_WARN("fail to get policy schema", K(ret));
10522} else {
10523policy_column_name = policy->get_column_name_str();
10524}
10525for (int64_t t_i = 0; OB_SUCC(ret) && t_i < tables.count(); ++t_i) {
10526const ObTableSchema *table = NULL;
10527ObTableSchema new_table_schema;
10528if (OB_ISNULL(table = tables.at(t_i))) {
10529ret = OB_ERR_UNEXPECTED;
10530LOG_WARN("table is NULL", K(ret));
10531} else if (OB_FAIL(new_table_schema.assign(*table))) {
10532LOG_WARN("fail to assign table schema", K(ret));
10533}
10534for (int64_t c_j = 0; OB_SUCC(ret) && c_j < table->get_label_se_column_ids().count(); ++c_j) {
10535const ObColumnSchemaV2 *column = NULL;
10536column = schema_guard.get_column_schema(table->get_tenant_id(),
10537table->get_table_id(),
10538table->get_label_se_column_ids().at(c_j));
10539if (OB_ISNULL(column)) {
10540ret = OB_ERR_UNEXPECTED;
10541LOG_WARN("fail to get column schema", K(ret));
10542} else if (0 == column->get_column_name_str().compare(policy_column_name)) {
10543ret = OB_NOT_SUPPORTED;
10544LOG_WARN("drop policy with table column", K(ret), K(table->get_table_name_str()));
10545LOG_USER_ERROR(OB_NOT_SUPPORTED, "drop policy with table column");
10546/*
10547int64_t new_schema_version = OB_INVALID_SCHEMA_VERSION;
10548if (table->is_index_table()) {
10549ret = OB_ERR_ALTER_INDEX_COLUMN;
10550LOG_WARN("can't not drop index column", K(ret));
10551} else if (OB_FAIL(update_prev_id_for_delete_column(*table, new_table_schema, *column, trans))) {
10552LOG_WARN("fail to update prev id for delete column", K(ret));
10553} else if (OB_FAIL(new_table_schema.delete_column(column->get_column_name_str()))) {
10554LOG_WARN("fail to delete column", K(ret));
10555} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
10556LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
10557} else if (OB_FAIL(schema_service->get_table_sql_service().delete_single_column(
10558new_schema_version, trans, *table, *column))) {
10559LOG_WARN("fail to delete column", K(ret));
10560} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
10561LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
10562} else if (OB_FAIL(schema_service->get_table_sql_service()
10563.sync_aux_schema_version_for_history(
10564trans, *table, new_schema_version ))) {
10565LOG_WARN("fail to sync aux schema version column", K(ret));
10566}
10567*/
10568}
10569}
10570}
10571return ret;
10572}
10573
10574int ObDDLOperator::drop_all_label_se_labels_in_policy(uint64_t tenant_id,
10575uint64_t policy_id,
10576ObMySQLTransaction &trans,
10577const ObString &ddl_stmt_str,
10578ObSchemaGetterGuard &schema_guard)
10579{
10580int ret = OB_SUCCESS;
10581
10582ObSchemaService *schema_sql_service = NULL;
10583
10584if (OB_ISNULL(schema_sql_service = schema_service_.get_schema_service())) {
10585ret = OB_ERR_SYS;
10586LOG_ERROR("schema_sql_service must not null", K(ret));
10587}
10588
10589ObSEArray<const ObLabelSeLabelSchema*, 16> labels;
10590if (OB_SUCC(ret)) {
10591if (OB_FAIL(schema_guard.get_label_se_label_infos_in_tenant(tenant_id, labels))) {
10592LOG_WARN("fail to get user level infos in tenant", K(ret), K(tenant_id));
10593}
10594}
10595
10596const ObLabelSeLabelSchema *label_schema = NULL;
10597int64_t new_schema_version = OB_INVALID_VERSION;
10598
10599for (int64_t i = 0; OB_SUCC(ret) && i < labels.count(); ++i) {
10600
10601if (OB_ISNULL(label_schema = labels.at(i))) {
10602ret = OB_ERR_UNEXPECTED;
10603LOG_WARN("schema is NULL", K(ret));
10604}
10605
10606if (OB_SUCC(ret) && label_schema->get_label_se_policy_id() == policy_id) {
10607ObLabelSeLabelSchema schema;
10608if (OB_FAIL(schema.assign(*label_schema))) {
10609LOG_WARN("fail to assign label schema", KR(ret));
10610} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
10611LOG_WARN("fail to gen new schema_version", K(ret));
10612} else {
10613schema.set_schema_version(new_schema_version);
10614}
10615if (OB_SUCC(ret)) {
10616if (OB_FAIL(schema_sql_service->get_label_se_policy_sql_service()
10617.apply_new_schema(schema, trans, OB_DDL_DROP_LABEL_SE_LABEL, ddl_stmt_str))) {
10618LOG_WARN("apply schema failed", K(ret));
10619}
10620}
10621}
10622
10623}
10624
10625return ret;
10626}
10627
10628int ObDDLOperator::drop_all_label_se_components_in_policy(uint64_t tenant_id,
10629uint64_t policy_id,
10630ObMySQLTransaction &trans,
10631const ObString &ddl_stmt_str,
10632ObSchemaGetterGuard &schema_guard)
10633{
10634int ret = OB_SUCCESS;
10635
10636ObSchemaService *schema_sql_service = NULL;
10637
10638if (OB_ISNULL(schema_sql_service = schema_service_.get_schema_service())) {
10639ret = OB_ERR_SYS;
10640LOG_ERROR("schema_sql_service must not null", K(ret));
10641}
10642
10643ObSEArray<const ObLabelSeComponentSchema*, 16> components;
10644if (OB_SUCC(ret)) {
10645if (OB_FAIL(schema_guard.get_label_se_component_infos_in_tenant(tenant_id, components))) {
10646LOG_WARN("fail to get user level infos in tenant", K(ret), K(tenant_id));
10647}
10648}
10649
10650const ObLabelSeComponentSchema *component_schema = NULL;
10651int64_t new_schema_version = OB_INVALID_VERSION;
10652
10653for (int64_t i = 0; OB_SUCC(ret) && i < components.count(); ++i) {
10654
10655if (OB_ISNULL(component_schema = components.at(i))) {
10656ret = OB_ERR_UNEXPECTED;
10657LOG_WARN("schema is NULL", K(ret));
10658}
10659
10660if (OB_SUCC(ret) && component_schema->get_label_se_policy_id() == policy_id) {
10661ObLabelSeComponentSchema schema;
10662ObSchemaOperationType ddl_type = OB_INVALID_DDL_OP;
10663if (OB_FAIL(schema.assign(*component_schema))) {
10664LOG_WARN("fail to assign component_schema", KR(ret));
10665} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
10666LOG_WARN("fail to gen new schema_version", K(ret));
10667} else {
10668schema.set_schema_version(new_schema_version);
10669}
10670
10671if (OB_SUCC(ret)) {
10672switch (component_schema->get_comp_type()) {
10673case static_cast<int64_t>(ObLabelSeComponentSchema::CompType::LEVEL):
10674ddl_type = OB_DDL_DROP_LABEL_SE_LEVEL;
10675break;
10676default:
10677ret = OB_NOT_SUPPORTED;
10678LOG_WARN("compartment and group not supported", K(ret), K(component_schema->get_comp_type()));
10679}
10680}
10681
10682if (OB_SUCC(ret)) {
10683if (OB_FAIL(schema_sql_service->get_label_se_policy_sql_service()
10684.apply_new_schema(schema, trans, ddl_type, ddl_stmt_str))) {
10685LOG_WARN("apply schema failed", K(ret));
10686}
10687}
10688}
10689}
10690return ret;
10691}
10692
10693int ObDDLOperator::drop_all_label_se_user_components(uint64_t tenant_id,
10694uint64_t user_id,
10695uint64_t policy_id,
10696ObMySQLTransaction &trans,
10697const ObString &ddl_stmt_str,
10698ObSchemaGetterGuard &schema_guard)
10699{
10700int ret = OB_SUCCESS;
10701
10702ObSchemaService *schema_sql_service = NULL;
10703
10704if (OB_ISNULL(schema_sql_service = schema_service_.get_schema_service())) {
10705ret = OB_ERR_SYS;
10706LOG_ERROR("schema_sql_service must not null", K(ret));
10707}
10708
10709ObSEArray<const ObLabelSeUserLevelSchema*, 4> user_levels;
10710if (OB_SUCC(ret)) {
10711if (OB_FAIL(schema_guard.get_label_se_user_level_infos_in_tenant(tenant_id, user_levels))) {
10712LOG_WARN("fail to get user level infos in tenant", K(ret), K(tenant_id));
10713}
10714}
10715
10716const ObLabelSeUserLevelSchema *user_level_schema = NULL;
10717int64_t new_schema_version = OB_INVALID_VERSION;
10718
10719for (int64_t i = 0; OB_SUCC(ret) && i < user_levels.count(); ++i) {
10720
10721if (OB_ISNULL(user_level_schema = user_levels.at(i))) {
10722ret = OB_ERR_UNEXPECTED;
10723LOG_WARN("schema is NULL", K(ret));
10724}
10725
10726if (OB_SUCC(ret) && (user_level_schema->get_user_id() == user_id
10727|| user_level_schema->get_label_se_policy_id() == policy_id)) {
10728ObLabelSeUserLevelSchema schema;
10729if (OB_FAIL(schema.assign(*user_level_schema))) {
10730LOG_WARN("fail to assign ObLabelSeUserLevelSchema", KR(ret));
10731} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
10732LOG_WARN("fail to gen new schema_version", K(ret));
10733} else {
10734schema.set_schema_version(new_schema_version);
10735}
10736
10737if (OB_SUCC(ret)) {
10738if (OB_FAIL(schema_sql_service->get_label_se_policy_sql_service()
10739.apply_new_schema(schema, trans, OB_DDL_DROP_LABEL_SE_USER_LEVELS, ddl_stmt_str))) {
10740LOG_WARN("apply schema failed", K(ret));
10741}
10742}
10743}
10744}
10745return ret;
10746}
10747
10748int ObDDLOperator::handle_label_se_user_level_function(ObSchemaOperationType ddl_type,
10749const ObString &ddl_stmt_str,
10750const ObString &policy_name,
10751ObSchemaGetterGuard &schema_guard,
10752ObLabelSeUserLevelSchema &schema,
10753ObMySQLTransaction &trans)
10754{
10755int ret = OB_SUCCESS;
10756uint64_t tenant_id = schema.get_tenant_id();
10757ObSchemaService *schema_sql_service = NULL;
10758
10759if (OB_ISNULL(schema_sql_service = schema_service_.get_schema_service())) {
10760ret = OB_ERR_SYS;
10761LOG_ERROR("schema_sql_service must not null", K(ret));
10762}
10763
10764if (OB_SUCC(ret)) {
10765int64_t new_schema_version = OB_INVALID_VERSION;
10766
10767if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
10768LOG_WARN("fail to gen new schema_version", K(ret));
10769} else {
10770schema.set_schema_version(new_schema_version);
10771}
10772}
10773
10774uint64_t policy_id = OB_INVALID_ID;
10775
10776if (OB_SUCC(ret)) {
10777if (OB_FAIL(ObLabelSeResolver::resolve_policy_name(tenant_id,
10778policy_name,
10779schema_guard,
10780policy_id))) {
10781LOG_WARN("fail to resovle policy name", K(ret));
10782}
10783}
10784
10785if (OB_SUCC(ret)) {
10786bool is_usera_exist = false;
10787if (OB_FAIL(schema_service_.check_user_exist(tenant_id, schema.get_user_id(), is_usera_exist))) {
10788LOG_WARN("fail to check schema exist", K(ret));
10789} else if (OB_UNLIKELY(!is_usera_exist)) {
10790ret = OB_ERR_USER_NOT_EXIST;
10791LOG_WARN("the schema exists, but the schema pointer is NULL", K(ret));
10792}
10793}
10794
10795if (OB_SUCC(ret)) {
10796const ObLabelSeUserLevelSchema *old_schema = NULL;
10797bool is_schema_exist = false;
10798
10799//check if the schema is already existed
10800if (OB_SUCC(ret)) {
10801if (OB_FAIL(schema_guard.get_label_se_user_level_by_id(tenant_id,
10802schema.get_user_id(),
10803policy_id,
10804old_schema))) {
10805LOG_WARN("fail to check schema exist", K(ret));
10806} else {
10807is_schema_exist = (old_schema != NULL);
10808}
10809}
10810
10811if (OB_SUCC(ret)) {
10812schema.set_label_se_policy_id(policy_id);
10813
10814if (OB_DDL_CREATE_LABEL_SE_USER_LEVELS != ddl_type) {
10815ret = OB_INVALID_ARGUMENT;
10816LOG_WARN("invalid ddl type", K(ret), K(ddl_type));
10817} else {
10818if (!is_schema_exist) {
10819//new schema id
10820uint64_t label_se_user_level_id = OB_INVALID_ID;
10821
10822if (OB_FAIL(schema_sql_service->fetch_new_label_se_user_level_id(tenant_id, label_se_user_level_id))) {
10823LOG_WARN("fail to fetch new label se policy id", K(tenant_id), K(ret));
10824} else {
10825schema.set_label_se_user_level_id(label_se_user_level_id);
10826}
10827} else {
10828//use old schema id
10829ddl_type = OB_DDL_ALTER_LABEL_SE_USER_LEVELS;
10830schema.set_label_se_user_level_id(old_schema->get_label_se_user_level_id());
10831if (schema.get_maximum_level() < 0) {
10832schema.set_maximum_level(old_schema->get_maximum_level());
10833}
10834if (schema.get_minimum_level() < 0) {
10835schema.set_minimum_level(old_schema->get_minimum_level());
10836}
10837if (schema.get_default_level() < 0) {
10838schema.set_default_level(old_schema->get_default_level());
10839}
10840if (schema.get_row_level() < 0) {
10841schema.set_row_level(old_schema->get_row_level());
10842}
10843}
10844}
10845}
10846}
10847
10848if (OB_SUCC(ret)) {
10849if (OB_FAIL(schema_sql_service->get_label_se_policy_sql_service()
10850.apply_new_schema(schema, trans, ddl_type, ddl_stmt_str))) {
10851LOG_WARN("create policy info failed", K(ret));
10852}
10853}
10854
10855LOG_DEBUG("ddl operator handle user level schema", K(schema));
10856return ret;
10857}
10858
10859int ObDDLOperator::handle_profile_function(
10860ObProfileSchema &schema,
10861ObMySQLTransaction &trans,
10862share::schema::ObSchemaOperationType ddl_type,
10863const ObString &ddl_stmt_str,
10864ObSchemaGetterGuard &schema_guard
10865)
10866{
10867int ret = OB_SUCCESS;
10868uint64_t tenant_id = schema.get_tenant_id();
10869ObSchemaService *schema_sql_service = NULL;
10870if (OB_ISNULL(schema_sql_service = schema_service_.get_schema_service())) {
10871ret = OB_ERR_SYS;
10872LOG_ERROR("schema_sql_service must not null", K(ret));
10873}
10874if (OB_SUCC(ret)) {
10875int64_t new_schema_version = OB_INVALID_VERSION;
10876if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
10877LOG_WARN("fail to gen new schema_version", K(ret));
10878} else {
10879schema.set_schema_version(new_schema_version);
10880}
10881}
10882if (OB_SUCC(ret)) {
10883bool is_schema_exist = false;
10884const ObProfileSchema *old_schema = NULL;
10885//check if the schema is already existed
10886if (OB_SUCC(ret)) {
10887if (OB_FAIL(schema_guard.get_profile_schema_by_name(tenant_id,
10888schema.get_profile_name_str(),
10889old_schema))) {
10890LOG_WARN("fail to check schema exist", K(ret));
10891} else {
10892is_schema_exist = (old_schema != NULL);
10893}
10894}
10895//other stuff case by case
10896if (OB_SUCC(ret)) {
10897uint64_t profile_id = OB_INVALID_ID;
10898switch (ddl_type) {
10899case OB_DDL_CREATE_PROFILE:
10900if (OB_UNLIKELY(is_schema_exist)) {
10901ret = OB_ERR_PROFILE_STRING_ALREADY_EXISTS;
10902LOG_USER_ERROR(OB_ERR_PROFILE_STRING_ALREADY_EXISTS, schema.get_profile_name_str().length(), schema.get_profile_name_str().ptr());
10903} else if (is_oracle_inner_profile_id(schema.get_profile_id())) {
10904/*do nothing*/
10905} else if (OB_FAIL(schema_sql_service->fetch_new_profile_id(tenant_id, profile_id))) {
10906LOG_WARN("fail to fetch new profile id", K(tenant_id), K(ret));
10907} else {
10908schema.set_profile_id(profile_id);
10909}
10910break;
10911case OB_DDL_ALTER_PROFILE:
10912case OB_DDL_DROP_PROFILE:
10913if (OB_UNLIKELY(!is_schema_exist)) {
10914ret = OB_ERR_PROFILE_STRING_DOES_NOT_EXIST;
10915LOG_USER_ERROR(OB_ERR_PROFILE_STRING_DOES_NOT_EXIST, schema.get_profile_name_str().length(), schema.get_profile_name_str().ptr());
10916} else {
10917schema.set_profile_id(old_schema->get_profile_id());
10918if (schema.get_failed_login_attempts() == ObProfileSchema::INVALID_VALUE) {
10919schema.set_failed_login_attempts(old_schema->get_failed_login_attempts());
10920}
10921if (schema.get_password_lock_time() == ObProfileSchema::INVALID_VALUE) {
10922schema.set_password_lock_time(old_schema->get_password_lock_time());
10923}
10924if (schema.get_password_life_time() == ObProfileSchema::INVALID_VALUE) {
10925schema.set_password_life_time(old_schema->get_password_life_time());
10926}
10927if (schema.get_password_grace_time() == ObProfileSchema::INVALID_VALUE) {
10928schema.set_password_grace_time(old_schema->get_password_grace_time());
10929}
10930}
10931break;
10932default:
10933ret = OB_NOT_SUPPORTED;
10934LOG_WARN("unknown ddl type", K(ret), K(ddl_type));
10935}
10936}
10937}
10938
10939if (OB_SUCC(ret)) {
10940if (OB_FAIL(schema_sql_service->get_profile_sql_service()
10941.apply_new_schema(schema, trans, ddl_type, ddl_stmt_str))) {
10942LOG_WARN("apply profile failed", K(ret));
10943}
10944}
10945
10946LOG_DEBUG("ddl operator", K(schema));
10947return ret;
10948}
10949
10950//----Functions for directory object----
10951int ObDDLOperator::create_directory(const ObString &ddl_str,
10952const uint64_t user_id,
10953share::schema::ObDirectorySchema &schema,
10954common::ObMySQLTransaction &trans)
10955{
10956int ret = OB_SUCCESS;
10957ObSchemaService *schema_service = schema_service_.get_schema_service();
10958const uint64_t tenant_id = schema.get_tenant_id();
10959uint64_t new_directory_id = OB_INVALID_ID;
10960int64_t schema_version = OB_INVALID_VERSION;
10961if (OB_ISNULL(schema_service)) {
10962ret = OB_ERR_SYS;
10963LOG_ERROR("schema_service should not be null", K(ret));
10964} else if (OB_FAIL(schema_service->fetch_new_directory_id(tenant_id, new_directory_id))) {
10965LOG_WARN("failed to fetch new_directory_id", K(tenant_id), K(ret));
10966} else if (FALSE_IT(schema.set_directory_id(new_directory_id))) {
10967// do nothing
10968} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, schema_version))) {
10969LOG_WARN("failed to gen new_schema_version", K(tenant_id), K(ret));
10970} else if (FALSE_IT(schema.set_schema_version(schema_version))) {
10971// do nothing
10972} else if (OB_FAIL(schema_service->get_directory_sql_service().apply_new_schema(
10973schema, trans, ObSchemaOperationType::OB_DDL_CREATE_DIRECTORY, ddl_str))) {
10974LOG_WARN("failed to create directory", K(schema.get_directory_name()), K(ret));
10975} else {
10976// after directory created, we should grant read/write/execute privilege to user
10977ObTablePrivSortKey table_priv_key;
10978table_priv_key.tenant_id_ = tenant_id;
10979table_priv_key.user_id_ = user_id;
10980
10981ObPrivSet priv_set;
10982priv_set = OB_PRIV_READ | OB_PRIV_WRITE | OB_PRIV_EXECUTE;
10983
10984ObObjPrivSortKey obj_priv_key;
10985obj_priv_key.tenant_id_ = tenant_id;
10986obj_priv_key.obj_id_ = new_directory_id;
10987obj_priv_key.obj_type_ = static_cast<uint64_t>(ObObjectType::DIRECTORY);
10988obj_priv_key.col_id_ = OB_COMPACT_COLUMN_INVALID_ID;
10989obj_priv_key.grantor_id_ = OB_ORA_SYS_USER_ID;
10990obj_priv_key.grantee_id_ = user_id;
10991
10992share::ObRawObjPrivArray priv_array;
10993priv_array.push_back(OBJ_PRIV_ID_READ);
10994priv_array.push_back(OBJ_PRIV_ID_WRITE);
10995priv_array.push_back(OBJ_PRIV_ID_EXECUTE);
10996if (OB_FAIL(this->grant_table(table_priv_key, priv_set, NULL, trans,
10997priv_array, 0, obj_priv_key))) {
10998LOG_WARN("fail to grant table", K(ret), K(table_priv_key), K(priv_set), K(obj_priv_key));
10999}
11000}
11001return ret;
11002}
11003
11004int ObDDLOperator::alter_directory(const ObString &ddl_str,
11005share::schema::ObDirectorySchema &schema,
11006common::ObMySQLTransaction &trans)
11007{
11008int ret = OB_SUCCESS;
11009ObSchemaService *schema_service = schema_service_.get_schema_service();
11010const uint64_t tenant_id = schema.get_tenant_id();
11011int64_t new_schema_version = OB_INVALID_VERSION;
11012if (OB_ISNULL(schema_service)) {
11013ret = OB_ERR_SYS;
11014LOG_ERROR("schema_service should not be null", K(ret));
11015} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
11016LOG_WARN("failed to gen new schema_version", K(ret), K(tenant_id));
11017} else if (FALSE_IT(schema.set_schema_version(new_schema_version))) {
11018// do nothing
11019} else if (OB_FAIL(schema_service->get_directory_sql_service().apply_new_schema(
11020schema, trans, ObSchemaOperationType::OB_DDL_ALTER_DIRECTORY, ddl_str))) {
11021LOG_WARN("failed to alter directory", K(schema), K(ret));
11022}
11023return ret;
11024}
11025
11026int ObDDLOperator::drop_directory(const ObString &ddl_str,
11027share::schema::ObDirectorySchema &schema,
11028common::ObMySQLTransaction &trans)
11029{
11030int ret = OB_SUCCESS;
11031ObSchemaService *schema_service = schema_service_.get_schema_service();
11032const uint64_t tenant_id = schema.get_tenant_id();
11033const uint64_t directory_id = schema.get_directory_id();
11034const uint64_t directory_type = static_cast<uint64_t>(ObObjectType::DIRECTORY);
11035int64_t new_schema_version = OB_INVALID_VERSION;
11036if (OB_ISNULL(schema_service)) {
11037ret = OB_ERR_SYS;
11038LOG_ERROR("schema_service must not null", K(ret));
11039} else if (OB_FAIL(this->drop_obj_privs(tenant_id, directory_id, directory_type, trans))) {
11040LOG_WARN("failed to drop obj privs for directory", K(ret),
11041K(tenant_id), K(directory_id), K(directory_type));
11042} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
11043LOG_WARN("failed to gen new schema_version", K(ret), K(tenant_id));
11044} else if (FALSE_IT(schema.set_schema_version(new_schema_version))) {
11045// do nothing
11046} else if (OB_FAIL(schema_service->get_directory_sql_service().apply_new_schema(
11047schema, trans, ObSchemaOperationType::OB_DDL_DROP_DIRECTORY, ddl_str))) {
11048LOG_WARN("failed to drop directory", K(schema), K(ret));
11049}
11050return ret;
11051}
11052//----End of functions for directory object----
11053
11054//----Functions for rls object----
11055int ObDDLOperator::create_rls_policy(ObRlsPolicySchema &schema,
11056ObMySQLTransaction &trans,
11057const ObString &ddl_stmt_str,
11058bool is_update_table_schema,
11059const ObTableSchema *table_schema)
11060{
11061int ret = OB_SUCCESS;
11062uint64_t tenant_id = schema.get_tenant_id();
11063uint64_t new_rls_policy_id = OB_INVALID_ID;
11064int64_t new_schema_version = OB_INVALID_VERSION;
11065ObSchemaService *schema_service = schema_service_.get_schema_service();
11066if (OB_ISNULL(schema_service)) {
11067ret = OB_ERR_SYS;
11068LOG_ERROR("schema_service must not null", K(ret));
11069} else if (OB_FAIL(schema_service->fetch_new_rls_policy_id(tenant_id, new_rls_policy_id))) {
11070LOG_WARN("failed to fetch new_rls_policy_id", K(tenant_id), K(ret));
11071} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
11072LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
11073} else {
11074schema.set_rls_policy_id(new_rls_policy_id);
11075if (OB_FAIL(schema.set_ids_cascade())) {
11076LOG_WARN("fail to set_ids_cascade", K(schema), K(ret));
11077} else if (OB_FAIL(schema_service->get_rls_sql_service().apply_new_schema(
11078schema, new_schema_version, trans, OB_DDL_CREATE_RLS_POLICY, ddl_stmt_str))) {
11079LOG_WARN("fail to create rls policy", K(schema), K(ret));
11080} else if (!is_update_table_schema) {
11081// do nothing
11082} else if (OB_ISNULL(table_schema)) {
11083ret = OB_ERR_UNEXPECTED;
11084LOG_WARN("table schema is null", K(ret));
11085} else if (OB_FAIL(update_rls_table_schema(*table_schema, OB_DDL_CREATE_RLS_POLICY, trans))) {
11086LOG_WARN("fail to update table schema", KR(ret));
11087}
11088}
11089return ret;
11090}
11091
11092int ObDDLOperator::drop_rls_policy(const ObRlsPolicySchema &schema,
11093ObMySQLTransaction &trans,
11094const ObString &ddl_stmt_str,
11095bool is_update_table_schema,
11096const ObTableSchema *table_schema)
11097{
11098int ret = OB_SUCCESS;
11099uint64_t tenant_id = schema.get_tenant_id();
11100int64_t new_schema_version = OB_INVALID_VERSION;
11101ObSchemaService *schema_service = schema_service_.get_schema_service();
11102if (OB_ISNULL(schema_service)) {
11103ret = OB_ERR_SYS;
11104LOG_ERROR("schema_service must not null", K(ret));
11105} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
11106LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
11107} else if (OB_FAIL(schema_service->get_rls_sql_service().apply_new_schema(
11108schema, new_schema_version, trans, OB_DDL_DROP_RLS_POLICY, ddl_stmt_str))) {
11109LOG_WARN("fail to drop rls policy", K(schema), K(ret));
11110} else if (!is_update_table_schema) {
11111// do nothing
11112} else if (OB_ISNULL(table_schema)) {
11113ret = OB_ERR_UNEXPECTED;
11114LOG_WARN("table schema is null", K(ret));
11115} else if (OB_FAIL(update_rls_table_schema(*table_schema, OB_DDL_DROP_RLS_POLICY, trans))) {
11116LOG_WARN("fail to update table schema", KR(ret));
11117}
11118return ret;
11119}
11120
11121int ObDDLOperator::alter_rls_policy(const ObRlsPolicySchema &schema,
11122ObMySQLTransaction &trans,
11123const ObString &ddl_stmt_str)
11124{
11125int ret = OB_SUCCESS;
11126uint64_t tenant_id = schema.get_tenant_id();
11127int64_t new_schema_version = OB_INVALID_VERSION;
11128ObSchemaService *schema_service = schema_service_.get_schema_service();
11129if (OB_ISNULL(schema_service)) {
11130ret = OB_ERR_SYS;
11131LOG_ERROR("schema_service must not null", K(ret));
11132} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
11133LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
11134} else if (OB_FAIL(schema_service->get_rls_sql_service().apply_new_schema(
11135schema, new_schema_version, trans, OB_DDL_ALTER_RLS_POLICY, ddl_stmt_str))) {
11136LOG_WARN("fail to alter rls policy", K(schema), K(ret));
11137} else if (OB_FAIL(schema_service->get_table_sql_service().update_data_table_schema_version(
11138trans, tenant_id, schema.get_table_id(), false/*in offline ddl white list*/))) {
11139LOG_WARN("fail to update table schema", KR(ret));
11140}
11141return ret;
11142}
11143
11144int ObDDLOperator::create_rls_group(ObRlsGroupSchema &schema,
11145ObMySQLTransaction &trans,
11146const ObString &ddl_stmt_str,
11147bool is_update_table_schema,
11148const ObTableSchema *table_schema)
11149{
11150int ret = OB_SUCCESS;
11151uint64_t tenant_id = schema.get_tenant_id();
11152uint64_t new_rls_group_id = OB_INVALID_ID;
11153int64_t new_schema_version = OB_INVALID_VERSION;
11154ObSchemaService *schema_service = schema_service_.get_schema_service();
11155if (OB_ISNULL(schema_service)) {
11156ret = OB_ERR_SYS;
11157LOG_ERROR("schema_service must not null", K(ret));
11158} else if (OB_FAIL(schema_service->fetch_new_rls_group_id(tenant_id, new_rls_group_id))) {
11159LOG_WARN("failed to fetch new_rls_group_id", K(tenant_id), K(ret));
11160} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
11161LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
11162} else {
11163schema.set_rls_group_id(new_rls_group_id);
11164if (OB_FAIL(schema_service->get_rls_sql_service().apply_new_schema(
11165schema, new_schema_version, trans, OB_DDL_CREATE_RLS_GROUP, ddl_stmt_str))) {
11166LOG_WARN("fail to create rls group", K(schema), K(ret));
11167} else if (!is_update_table_schema) {
11168// do nothing
11169} else if (OB_ISNULL(table_schema)) {
11170ret = OB_ERR_UNEXPECTED;
11171LOG_WARN("table schema is null", K(ret));
11172} else if (OB_FAIL(update_rls_table_schema(*table_schema, OB_DDL_CREATE_RLS_GROUP, trans))) {
11173LOG_WARN("fail to update table schema", KR(ret));
11174}
11175}
11176return ret;
11177}
11178
11179int ObDDLOperator::drop_rls_group(const ObRlsGroupSchema &schema,
11180ObMySQLTransaction &trans,
11181const ObString &ddl_stmt_str,
11182bool is_update_table_schema,
11183const ObTableSchema *table_schema)
11184{
11185int ret = OB_SUCCESS;
11186uint64_t tenant_id = schema.get_tenant_id();
11187int64_t new_schema_version = OB_INVALID_VERSION;
11188ObSchemaService *schema_service = schema_service_.get_schema_service();
11189if (OB_ISNULL(schema_service)) {
11190ret = OB_ERR_SYS;
11191LOG_ERROR("schema_service must not null", K(ret));
11192} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
11193LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
11194} else if (OB_FAIL(schema_service->get_rls_sql_service().apply_new_schema(
11195schema, new_schema_version, trans, OB_DDL_DROP_RLS_GROUP, ddl_stmt_str))) {
11196LOG_WARN("fail to drop rls group", K(schema), K(ret));
11197} else if (!is_update_table_schema) {
11198// do nothing
11199} else if (OB_ISNULL(table_schema)) {
11200ret = OB_ERR_UNEXPECTED;
11201LOG_WARN("table schema is null", K(ret));
11202} else if (OB_FAIL(update_rls_table_schema(*table_schema, OB_DDL_DROP_RLS_GROUP, trans))) {
11203LOG_WARN("fail to update table schema", KR(ret));
11204}
11205return ret;
11206}
11207
11208int ObDDLOperator::create_rls_context(ObRlsContextSchema &schema,
11209ObMySQLTransaction &trans,
11210const ObString &ddl_stmt_str,
11211bool is_update_table_schema,
11212const ObTableSchema *table_schema)
11213{
11214int ret = OB_SUCCESS;
11215uint64_t tenant_id = schema.get_tenant_id();
11216uint64_t new_rls_context_id = OB_INVALID_ID;
11217int64_t new_schema_version = OB_INVALID_VERSION;
11218ObSchemaService *schema_service = schema_service_.get_schema_service();
11219if (OB_ISNULL(schema_service)) {
11220ret = OB_ERR_SYS;
11221LOG_ERROR("schema_service must not null", K(ret));
11222} else if (OB_FAIL(schema_service->fetch_new_rls_context_id(tenant_id, new_rls_context_id))) {
11223LOG_WARN("failed to fetch new_rls_context_id", K(tenant_id), K(ret));
11224} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
11225LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
11226} else {
11227schema.set_rls_context_id(new_rls_context_id);
11228if (OB_FAIL(schema_service->get_rls_sql_service().apply_new_schema(
11229schema, new_schema_version, trans, OB_DDL_CREATE_RLS_CONTEXT, ddl_stmt_str))) {
11230LOG_WARN("fail to create rls context", K(schema), K(ret));
11231} else if (!is_update_table_schema) {
11232// do nothing
11233} else if (OB_ISNULL(table_schema)) {
11234ret = OB_ERR_UNEXPECTED;
11235LOG_WARN("table schema is null", K(ret));
11236} else if (OB_FAIL(update_rls_table_schema(*table_schema, OB_DDL_CREATE_RLS_CONTEXT, trans))) {
11237LOG_WARN("fail to update table schema", KR(ret));
11238}
11239}
11240return ret;
11241}
11242
11243int ObDDLOperator::drop_rls_context(const ObRlsContextSchema &schema,
11244ObMySQLTransaction &trans,
11245const ObString &ddl_stmt_str,
11246bool is_update_table_schema,
11247const ObTableSchema *table_schema)
11248{
11249int ret = OB_SUCCESS;
11250uint64_t tenant_id = schema.get_tenant_id();
11251int64_t new_schema_version = OB_INVALID_VERSION;
11252ObSchemaService *schema_service = schema_service_.get_schema_service();
11253if (OB_ISNULL(schema_service)) {
11254ret = OB_ERR_SYS;
11255LOG_ERROR("schema_service must not null", K(ret));
11256} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
11257LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
11258} else if (OB_FAIL(schema_service->get_rls_sql_service().apply_new_schema(
11259schema, new_schema_version, trans, OB_DDL_DROP_RLS_CONTEXT, ddl_stmt_str))) {
11260LOG_WARN("fail to drop rls context", K(schema), K(ret));
11261} else if (!is_update_table_schema) {
11262// do nothing
11263} else if (OB_ISNULL(table_schema)) {
11264ret = OB_ERR_UNEXPECTED;
11265LOG_WARN("table schema is null", K(ret));
11266} else if (OB_FAIL(update_rls_table_schema(*table_schema, OB_DDL_DROP_RLS_CONTEXT, trans))) {
11267LOG_WARN("fail to update table schema", KR(ret));
11268}
11269return ret;
11270}
11271
11272int ObDDLOperator::drop_rls_sec_column(const ObRlsPolicySchema &schema,
11273const ObRlsSecColumnSchema &column_schema,
11274ObMySQLTransaction &trans,
11275const ObString &ddl_stmt_str)
11276{
11277int ret = OB_SUCCESS;
11278uint64_t tenant_id = schema.get_tenant_id();
11279int64_t new_schema_version = OB_INVALID_VERSION;
11280ObSchemaService *schema_service = schema_service_.get_schema_service();
11281if (OB_ISNULL(schema_service)) {
11282ret = OB_ERR_SYS;
11283LOG_ERROR("schema_service must not null", K(ret));
11284} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
11285LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
11286} else if (OB_FAIL(schema_service->get_rls_sql_service().drop_rls_sec_column(
11287schema, column_schema, new_schema_version, trans, ddl_stmt_str))) {
11288LOG_WARN("fail to drop rls policy", K(schema), K(ret));
11289}
11290return ret;
11291}
11292
11293int ObDDLOperator::update_rls_table_schema(const ObTableSchema &table_schema,
11294const ObSchemaOperationType ddl_type,
11295ObMySQLTransaction &trans)
11296{
11297int ret = OB_SUCCESS;
11298ObSchemaService *schema_service = schema_service_.get_schema_service();
11299bool need_add_flag = false;
11300bool need_del_flag = false;
11301if (OB_ISNULL(schema_service)) {
11302ret = OB_ERR_SYS;
11303LOG_ERROR("schema_service must not null", K(ret));
11304} else {
11305switch(ddl_type) {
11306case OB_DDL_CREATE_RLS_POLICY:
11307case OB_DDL_CREATE_RLS_GROUP:
11308case OB_DDL_CREATE_RLS_CONTEXT:
11309if (!table_schema.has_table_flag(CASCADE_RLS_OBJECT_FLAG)) {
11310need_add_flag = true;
11311}
11312break;
11313case OB_DDL_DROP_RLS_POLICY:
11314if (!table_schema.get_rls_group_ids().empty() ||
11315!table_schema.get_rls_context_ids().empty()) {
11316// do nothing
11317} else if (1 == table_schema.get_rls_policy_ids().count()) {
11318need_del_flag = true;
11319}
11320break;
11321case OB_DDL_DROP_RLS_GROUP:
11322if (!table_schema.get_rls_policy_ids().empty() ||
11323!table_schema.get_rls_context_ids().empty()) {
11324// do nothing
11325} else if (1 == table_schema.get_rls_group_ids().count()) {
11326need_del_flag = true;
11327}
11328break;
11329case OB_DDL_DROP_RLS_CONTEXT:
11330if (!table_schema.get_rls_policy_ids().empty() ||
11331!table_schema.get_rls_group_ids().empty()) {
11332// do nothing
11333} else if (1 == table_schema.get_rls_context_ids().count()) {
11334need_del_flag = true;
11335}
11336break;
11337default:
11338ret = OB_ERR_UNEXPECTED;
11339LOG_WARN("unknown ddl type", KR(ret), K(ddl_type));
11340}
11341}
11342if (OB_FAIL(ret)) {
11343} else if (OB_UNLIKELY(need_add_flag && need_del_flag)) {
11344ret = OB_ERR_UNEXPECTED;
11345LOG_WARN("unexpeted statue", KR(ret), K(ddl_type), K(table_schema));
11346} else if (need_add_flag || need_del_flag) {
11347share::schema::ObTableSchema new_table_schema;
11348if (OB_FAIL(new_table_schema.assign(table_schema))) {
11349LOG_WARN("failed to assign schema", K(ret));
11350} else if (FALSE_IT(new_table_schema.add_or_del_table_flag(CASCADE_RLS_OBJECT_FLAG,
11351need_add_flag))) {
11352} else if (OB_FAIL(update_table_attribute(new_table_schema, trans, OB_DDL_ALTER_TABLE))) {
11353LOG_WARN("failed to update table attribute", K(ret));
11354}
11355} else {
11356if (OB_FAIL(schema_service->get_table_sql_service().update_data_table_schema_version(
11357trans, table_schema.get_tenant_id(), table_schema.get_table_id(), false))) {
11358LOG_WARN("fail to update table schema", K(ret));
11359}
11360}
11361return ret;
11362}
11363
11364int ObDDLOperator::drop_rls_object_in_drop_table(const ObTableSchema &table_schema,
11365ObMySQLTransaction &trans,
11366ObSchemaGetterGuard &schema_guard)
11367{
11368int ret = OB_SUCCESS;
11369uint64_t tenant_id = table_schema.get_tenant_id();
11370uint64_t table_id = table_schema.get_table_id();
11371ObString empty_str;
11372for (int64_t i = 0; OB_SUCC(ret) && i < table_schema.get_rls_policy_ids().count(); ++i) {
11373const ObRlsPolicySchema *policy_schema = NULL;
11374uint64_t policy_id = table_schema.get_rls_policy_ids().at(i);
11375OZ (schema_guard.get_rls_policy_schema_by_id(tenant_id, policy_id, policy_schema));
11376CK (OB_NOT_NULL(policy_schema));
11377OZ (drop_rls_policy(*policy_schema, trans, empty_str, false, NULL));
11378}
11379for (int64_t i = 0; OB_SUCC(ret) && i < table_schema.get_rls_group_ids().count(); ++i) {
11380const ObRlsGroupSchema *group_schema = NULL;
11381uint64_t group_id = table_schema.get_rls_group_ids().at(i);
11382OZ (schema_guard.get_rls_group_schema_by_id(tenant_id, group_id, group_schema));
11383CK (OB_NOT_NULL(group_schema));
11384OZ (drop_rls_group(*group_schema, trans, empty_str, false, NULL));
11385}
11386for (int64_t i = 0; OB_SUCC(ret) && i < table_schema.get_rls_context_ids().count(); ++i) {
11387const ObRlsContextSchema *context_schema = NULL;
11388uint64_t context_id = table_schema.get_rls_context_ids().at(i);
11389OZ (schema_guard.get_rls_context_schema_by_id(tenant_id, context_id, context_schema));
11390CK (OB_NOT_NULL(context_schema));
11391OZ (drop_rls_context(*context_schema, trans, empty_str, false, NULL));
11392}
11393return ret;
11394}
11395
11396//----End of functions for rls object----
11397
11398int ObDDLOperator::init_tenant_profile(int64_t tenant_id,
11399const share::schema::ObSysVariableSchema &sys_variable,
11400common::ObMySQLTransaction &trans)
11401{
11402int ret = OB_SUCCESS;
11403bool is_oracle_mode = false;
11404if (OB_FAIL(sys_variable.get_oracle_mode(is_oracle_mode))) {
11405LOG_WARN("failed to get oracle mode", K(ret), K(tenant_id));
11406} else if (!is_oracle_mode) {
11407/*do nothing*/
11408} else {
11409ObProfileSchema profile_schema;
11410profile_schema.set_tenant_id(tenant_id);
11411profile_schema.set_profile_id(OB_ORACLE_TENANT_INNER_PROFILE_ID);
11412profile_schema.set_password_lock_time(USECS_PER_DAY);
11413profile_schema.set_failed_login_attempts(ObProfileSchema::UNLIMITED_VALUE);
11414profile_schema.set_password_life_time(ObProfileSchema::UNLIMITED_VALUE);
11415profile_schema.set_password_grace_time(ObProfileSchema::UNLIMITED_VALUE);
11416profile_schema.set_password_verify_function("NULL");
11417profile_schema.set_profile_name("DEFAULT");
11418ObSchemaService *schema_service = schema_service_.get_schema_service();
11419int64_t new_schema_version = OB_INVALID_VERSION;
11420if (OB_ISNULL(schema_service)) {
11421ret = OB_ERR_UNEXPECTED;
11422LOG_WARN("get invalid schema service", K(ret));
11423} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
11424LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
11425} else if (FALSE_IT(profile_schema.set_schema_version(new_schema_version))) {
11426} else if (OB_FAIL(schema_service->get_profile_sql_service().apply_new_schema(
11427profile_schema, trans, ObSchemaOperationType::OB_DDL_CREATE_PROFILE, NULL))) {
11428LOG_WARN("create default profile failed", K(ret));
11429}
11430}
11431return ret;
11432}
11433
11434int ObDDLOperator::init_tenant_keystore(int64_t tenant_id,
11435const share::schema::ObSysVariableSchema &sys_variable,
11436common::ObMySQLTransaction &trans)
11437{
11438int ret = OB_SUCCESS;
11439bool is_oracle_mode = false;
11440if (OB_FAIL(sys_variable.get_oracle_mode(is_oracle_mode))) {
11441LOG_WARN("failed to get oracle mode", K(ret), K(tenant_id));
11442} else if (is_oracle_mode) {
11443// nothing
11444} else {
11445ObSchemaService *schema_service = schema_service_.get_schema_service();
11446ObKeystoreSchema keystore_schema;
11447keystore_schema.set_keystore_id(OB_MYSQL_TENANT_INNER_KEYSTORE_ID);
11448keystore_schema.set_tenant_id(tenant_id);
11449keystore_schema.set_status(2);
11450keystore_schema.set_keystore_name("mysql_keystore");
11451int64_t new_schema_version = OB_INVALID_VERSION;
11452if (OB_ISNULL(schema_service)) {
11453ret = OB_ERR_UNEXPECTED;
11454LOG_WARN("get invalid schema service", K(ret));
11455} else if (OB_FAIL(schema_service_.gen_new_schema_version(tenant_id, new_schema_version))) {
11456LOG_WARN("fail to gen new schema_version", K(ret), K(tenant_id));
11457} else if (FALSE_IT(keystore_schema.set_schema_version(new_schema_version))) {
11458} else if (OB_FAIL(schema_service->get_keystore_sql_service().create_keystore(
11459keystore_schema, trans, NULL))) {
11460LOG_WARN("create keystore failed", K(ret));
11461}
11462}
11463return ret;
11464}
11465
11466int ObDDLOperator::insert_dependency_infos(common::ObMySQLTransaction &trans,
11467ObIArray<ObDependencyInfo> &dep_infos,
11468uint64_t tenant_id,
11469uint64_t dep_obj_id,
11470uint64_t schema_version, uint64_t owner_id)
11471{
11472int ret = OB_SUCCESS;
11473if (OB_INVALID_ID == owner_id
11474|| OB_INVALID_ID == dep_obj_id
11475|| OB_INVALID_ID == tenant_id
11476|| OB_INVALID_SCHEMA_VERSION == schema_version) {
11477ret = OB_ERR_UNEXPECTED;
11478LOG_WARN("illegal schema version or owner id", K(ret), K(schema_version),
11479K(owner_id), K(dep_obj_id));
11480} else {
11481for (int64_t i = 0 ; OB_SUCC(ret) && i < dep_infos.count(); ++i) {
11482ObDependencyInfo & dep = dep_infos.at(i);
11483dep.set_tenant_id(tenant_id);
11484dep.set_dep_obj_id(dep_obj_id);
11485dep.set_dep_obj_owner_id(owner_id);
11486dep.set_schema_version(schema_version);
11487OZ (dep.insert_schema_object_dependency(trans));
11488}
11489}
11490
11491return ret;
11492}
11493
11494int ObDDLOperator::update_table_status(const ObTableSchema &orig_table_schema,
11495const int64_t schema_version,
11496const ObObjectStatus new_status,
11497const bool update_object_status_ignore_version,
11498ObMySQLTransaction &trans)
11499{
11500int ret = OB_SUCCESS;
11501ObSchemaService *schema_service = schema_service_.get_schema_service();
11502uint64_t data_version = 0;
11503ObTableSchema new_schema;
11504const ObSchemaOperationType op = OB_DDL_ALTER_TABLE;
11505if (OB_ISNULL(schema_service)) {
11506ret = OB_ERR_UNEXPECTED;
11507LOG_WARN("schema_service is NULL", K(ret));
11508} else if (schema_version <= 0) {
11509ret = OB_INVALID_ARGUMENT;
11510LOG_WARN("schema_version is invalid", K(ret), K(schema_version));
11511} else if (!update_object_status_ignore_version && OB_FAIL(GET_MIN_DATA_VERSION(orig_table_schema.get_tenant_id(), data_version))) {
11512LOG_WARN("failed to get data version", K(ret));
11513} else if (!update_object_status_ignore_version && data_version < DATA_VERSION_4_1_0_0) {
11514ret = OB_NOT_SUPPORTED;
11515LOG_WARN("version and feature mismatch", K(ret));
11516} else if (OB_FAIL(new_schema.assign(orig_table_schema))) {
11517LOG_WARN("failed to assign table schema", K(ret));
11518} else if (FALSE_IT(new_schema.set_object_status(new_status))) {
11519} else if (FALSE_IT(new_schema.set_schema_version(schema_version))) {
11520} else if (new_schema.get_column_count() > 0
11521&& FALSE_IT(new_schema.set_view_column_filled_flag(ObViewColumnFilledFlag::FILLED))) {
11522/*
11523*Except for drop view, there is no way to reduce the column count,
11524*and there is no need to consider the table mode of this view before
11525*/
11526} else if (OB_FAIL(schema_service->get_table_sql_service().update_table_attribute(trans, new_schema, op, update_object_status_ignore_version) )) {
11527LOG_WARN("update table status failed", K(ret));
11528}
11529return ret;
11530}
11531
11532int ObDDLOperator::update_view_columns(const ObTableSchema &view_schema,
11533common::ObMySQLTransaction &trans)
11534{
11535int ret = OB_SUCCESS;
11536ObSchemaService *schema_service = schema_service_.get_schema_service();
11537uint64_t data_version = 0;
11538if (OB_ISNULL(schema_service)) {
11539ret = OB_ERR_UNEXPECTED;
11540LOG_WARN("schema_service is NULL", K(ret));
11541} else if (OB_FAIL(GET_MIN_DATA_VERSION(view_schema.get_tenant_id(), data_version))) {
11542LOG_WARN("failed to get data version", K(ret));
11543} else if (data_version < DATA_VERSION_4_1_0_0) {
11544ret = OB_NOT_SUPPORTED;
11545LOG_WARN("version and feature mismatch", K(ret));
11546} else if (OB_FAIL(schema_service->get_table_sql_service().update_view_columns(trans, view_schema))) {
11547LOG_WARN("failed to add columns", K(ret));
11548}
11549return ret;
11550}
11551
11552// only used in upgrading
11553int ObDDLOperator::reset_view_status(common::ObMySQLTransaction &trans,
11554const uint64_t tenant_id,
11555const ObTableSchema *table)
11556{
11557int ret = OB_SUCCESS;
11558ObObjectStatus new_status = ObObjectStatus::INVALID;
11559ObSchemaService *schema_service = schema_service_.get_schema_service();
11560int64_t schema_version = OB_INVALID_VERSION;
11561const bool update_object_status_ignore_version = true;
11562if (OB_ISNULL(schema_service)) {
11563ret = OB_ERR_UNEXPECTED;
11564LOG_WARN("schema_service is NULL", K(ret));
11565} else {
11566if (OB_ISNULL(table) || !table->is_view_table()) {
11567ret = OB_ERR_UNEXPECTED;
11568LOG_WARN("get wrong schema", K(ret), KP(table));
11569} else if (OB_FAIL(schema_service->gen_new_schema_version(tenant_id, schema_version, schema_version))) {
11570LOG_WARN("failed to gen new schema version", K(ret));
11571} else if (OB_FAIL(update_table_status(*table,
11572schema_version,
11573new_status,
11574update_object_status_ignore_version,
11575trans))) {
11576LOG_WARN("failed to update table status", K(ret));
11577}
11578}
11579return ret;
11580}
11581
11582
11583// only used in upgrading
11584int ObDDLOperator::try_add_dep_info_for_synonym(const ObSimpleSynonymSchema *synonym_info,
11585common::ObMySQLTransaction &trans)
11586{
11587int ret = OB_SUCCESS;
11588//add def obj info if exists
11589bool ref_exists = false;
11590ObObjectType ref_type = ObObjectType::INVALID;
11591uint64_t ref_obj_id = OB_INVALID_ID;
11592uint64_t ref_schema_version = share::OB_INVALID_SCHEMA_VERSION;
11593if (OB_ISNULL(synonym_info)) {
11594ret = OB_ERR_UNEXPECTED;
11595LOG_WARN("get unecpected synonym info", K(ret));
11596} else if (OB_FAIL(ObSQLUtils::find_synonym_ref_obj(synonym_info->get_object_database_id(),
11597synonym_info->get_object_name_str(),
11598synonym_info->get_tenant_id(),
11599ref_exists,
11600ref_obj_id,
11601ref_type,
11602ref_schema_version))) {
11603LOG_WARN("failed to find synonym ref obj", K(ret));
11604} else {
11605if (ref_exists) {
11606ObDependencyInfo dep;
11607dep.set_dep_obj_id(synonym_info->get_synonym_id());
11608dep.set_dep_obj_type(ObObjectType::SYNONYM);
11609dep.set_ref_obj_id(ref_obj_id);
11610dep.set_ref_obj_type(ref_type);
11611dep.set_dep_timestamp(-1);
11612dep.set_ref_timestamp(ref_schema_version);
11613dep.set_tenant_id(synonym_info->get_tenant_id());
11614if (OB_FAIL(dep.insert_schema_object_dependency(trans))) {
11615if (OB_ERR_PRIMARY_KEY_DUPLICATE == ret) {
11616ret = OB_SUCCESS;
11617LOG_TRACE("synonym have dep info before", K(*synonym_info));
11618}
11619}
11620}
11621}
11622return ret;
11623}
11624
11625
11626}//end namespace rootserver
11627}//end namespace oceanbase
11628