From 0fa70fd5ff1b380c120d7192cbe199fb992e6a91 Mon Sep 17 00:00:00 2001 From: leftgeek <1094669802@qq.com> Date: Mon, 5 Aug 2024 09:47:42 +0000 Subject: [PATCH] allow forced non-sorting for direct insert into select --- .../table_load/ob_table_load_coordinator.cpp | 2 +- .../code_generator/ob_static_engine_cg.cpp | 1 + .../engine/cmd/ob_table_direct_insert_ctx.cpp | 23 ++++++++++++++++++- .../engine/cmd/ob_table_direct_insert_ctx.h | 4 ++++ src/sql/engine/ob_physical_plan.cpp | 7 ++++-- src/sql/engine/ob_physical_plan.h | 6 +++++ src/sql/resolver/dml/ob_hint.cpp | 12 ++++++++++ src/sql/resolver/dml/ob_hint.h | 1 + 8 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/observer/table_load/ob_table_load_coordinator.cpp b/src/observer/table_load/ob_table_load_coordinator.cpp index 514c50c1a4..816b1febd1 100644 --- a/src/observer/table_load/ob_table_load_coordinator.cpp +++ b/src/observer/table_load/ob_table_load_coordinator.cpp @@ -363,7 +363,7 @@ int ObTableLoadCoordinator::gen_apply_arg(ObDirectLoadResourceApplyArg &apply_ar need_sort = false; unit.memory_size_ = min_unsort_memory; } else { - need_sort = true; + need_sort = ctx_->param_.need_sort_; // allow forced non-sorting unit.memory_size_ = MIN(ObTableLoadAssignedMemoryManager::MIN_SORT_MEMORY_PER_TASK, memory_limit); } } else { diff --git a/src/sql/code_generator/ob_static_engine_cg.cpp b/src/sql/code_generator/ob_static_engine_cg.cpp index 696a35db88..40d4a8e02c 100644 --- a/src/sql/code_generator/ob_static_engine_cg.cpp +++ b/src/sql/code_generator/ob_static_engine_cg.cpp @@ -6766,6 +6766,7 @@ int ObStaticEngineCG::generate_spec(ObLogInsert &op, spec.plan_->set_online_sample_percent(op.get_plan()->get_optimizer_context() .get_exec_ctx()->get_table_direct_insert_ctx() .get_online_sample_percent()); + spec.plan_->set_direct_load_need_sort(global_hint.get_direct_load_need_sort()); // check is insert overwrite bool is_insert_overwrite = false; ObExecContext *exec_ctx = NULL; diff --git a/src/sql/engine/cmd/ob_table_direct_insert_ctx.cpp b/src/sql/engine/cmd/ob_table_direct_insert_ctx.cpp index 6dc691f311..a32fcb0c02 100644 --- a/src/sql/engine/cmd/ob_table_direct_insert_ctx.cpp +++ b/src/sql/engine/cmd/ob_table_direct_insert_ctx.cpp @@ -92,6 +92,7 @@ int ObTableDirectInsertCtx::init( insert_mode = ObDirectLoadInsertMode::NORMAL; } ObDirectLoadMode::Type load_mode = is_insert_overwrite ? ObDirectLoadMode::INSERT_OVERWRITE : ObDirectLoadMode::INSERT_INTO; + bool is_heap_table = false; if (OB_FAIL(GCTX.omt_->get_tenant(MTL_ID(), tenant))) { LOG_WARN("fail to get tenant handle", KR(ret), K(MTL_ID())); } else if (OB_FAIL(ObTableLoadService::check_support_direct_load(*schema_guard, @@ -107,6 +108,8 @@ int ObTableDirectInsertCtx::init( table_id, column_ids))) { LOG_WARN("failed to init store column idxs", KR(ret)); + } else if(OB_FAIL(get_is_heap_table(*schema_guard, tenant_id, table_id, is_heap_table))) { + LOG_WARN("failed to get is heap table", KR(ret), K(tenant_id), K(table_id)); } else { ObTableLoadParam param; param.tenant_id_ = MTL_ID(); @@ -117,7 +120,7 @@ int ObTableDirectInsertCtx::init( param.column_count_ = column_ids.count(); param.px_mode_ = true; param.online_opt_stat_gather_ = is_online_gather_statistics_; - param.need_sort_ = true; + param.need_sort_ = is_heap_table ? phy_plan.get_direct_load_need_sort() : true; param.max_error_row_count_ = 0; param.dup_action_ = (enable_inc_replace ? sql::ObLoadDupActionType::LOAD_REPLACE : sql::ObLoadDupActionType::LOAD_STOP_ON_DUP); @@ -205,5 +208,23 @@ int ObTableDirectInsertCtx::get_compressor_type(const uint64_t tenant_id, return ret; } +int ObTableDirectInsertCtx::get_is_heap_table( + ObSchemaGetterGuard &schema_guard, + const uint64_t tenant_id, + const uint64_t table_id, + bool &is_heap_table) +{ + int ret = OB_SUCCESS; + const ObTableSchema *table_schema = nullptr; + if (OB_FAIL(schema_guard.get_table_schema(tenant_id, table_id, table_schema))) { + LOG_WARN("fail to get table schema", KR(ret), K(tenant_id), K(table_id)); + } else if (OB_ISNULL(table_schema)) { + ret = OB_TABLE_NOT_EXIST; + LOG_WARN("table schema is null", KR(ret)); + } else { + is_heap_table = table_schema->is_heap_table(); + } + return ret; +} } // namespace sql } // namespace oceanbase diff --git a/src/sql/engine/cmd/ob_table_direct_insert_ctx.h b/src/sql/engine/cmd/ob_table_direct_insert_ctx.h index 3bb581145b..d1f24912fd 100644 --- a/src/sql/engine/cmd/ob_table_direct_insert_ctx.h +++ b/src/sql/engine/cmd/ob_table_direct_insert_ctx.h @@ -73,6 +73,10 @@ public: private: int get_compressor_type(const uint64_t tenant_id, const uint64_t table_id, const int64_t parallel, ObCompressorType &compressor_type); + int get_is_heap_table(share::schema::ObSchemaGetterGuard &schema_guard, + const uint64_t tenant_id, + const uint64_t table_id, + bool &is_heap_table); private: observer::ObTableLoadExecCtx *load_exec_ctx_; observer::ObTableLoadInstance *table_load_instance_; diff --git a/src/sql/engine/ob_physical_plan.cpp b/src/sql/engine/ob_physical_plan.cpp index 35795be3ae..95dae8e173 100644 --- a/src/sql/engine/ob_physical_plan.cpp +++ b/src/sql/engine/ob_physical_plan.cpp @@ -146,7 +146,8 @@ ObPhysicalPlan::ObPhysicalPlan(MemoryContext &mem_context /* = CURRENT_CONTEXT * can_set_feedback_info_(true), need_switch_to_table_lock_worker_(false), data_complement_gen_doc_id_(false), - dml_table_ids_() + dml_table_ids_(), + direct_load_need_sort_(false) { } @@ -252,6 +253,7 @@ void ObPhysicalPlan::reset() need_switch_to_table_lock_worker_ = false; data_complement_gen_doc_id_ = false; dml_table_ids_.reset(); + direct_load_need_sort_ = false; } void ObPhysicalPlan::destroy() { @@ -828,7 +830,8 @@ OB_SERIALIZE_MEMBER(ObPhysicalPlan, insert_overwrite_, online_sample_percent_, need_switch_to_table_lock_worker_, - data_complement_gen_doc_id_); + data_complement_gen_doc_id_, + direct_load_need_sort_); int ObPhysicalPlan::set_table_locations(const ObTablePartitionInfoArray &infos, ObSchemaGetterGuard &schema_guard) diff --git a/src/sql/engine/ob_physical_plan.h b/src/sql/engine/ob_physical_plan.h index 7ba0986e92..9cf0b4db71 100644 --- a/src/sql/engine/ob_physical_plan.h +++ b/src/sql/engine/ob_physical_plan.h @@ -523,6 +523,11 @@ public: int set_mview_ids(const ObIArray &mview_ids) { return mview_ids_.assign(mview_ids); } ObFixedArray &get_dml_table_ids() { return dml_table_ids_; } const ObIArray &get_dml_table_ids() const { return dml_table_ids_; } + void set_direct_load_need_sort(const bool direct_load_need_sort) + { + direct_load_need_sort_ = direct_load_need_sort; + } + bool get_direct_load_need_sort() const { return direct_load_need_sort_; } public: static const int64_t MAX_PRINTABLE_SIZE = 2 * 1024 * 1024; private: @@ -720,6 +725,7 @@ private: // further cursor stmt will check agains // to decide whether it read uncommitted data common::ObFixedArray dml_table_ids_; + bool direct_load_need_sort_; }; inline void ObPhysicalPlan::set_affected_last_insert_id(bool affected_last_insert_id) diff --git a/src/sql/resolver/dml/ob_hint.cpp b/src/sql/resolver/dml/ob_hint.cpp index ccb43c9ae0..d4a395e5f1 100644 --- a/src/sql/resolver/dml/ob_hint.cpp +++ b/src/sql/resolver/dml/ob_hint.cpp @@ -597,6 +597,18 @@ void ObGlobalHint::merge_osg_hint(int8_t flag) { osg_hint_.flags_ |= flag; } +bool ObGlobalHint::get_direct_load_need_sort() const +{ + bool need_sort = false; + if (has_direct_load()) { + // if direct(need_sort, max_allowed_error_rows) hint is provided, + // use its need_sort param, otherwise need_sort = true + need_sort = direct_load_hint_.is_enable() ? + direct_load_hint_.need_sort() : true; + } + return need_sort; +} + int ObOptimizerStatisticsGatheringHint::print_osg_hint(PlanText &plan_text) const { int ret = OB_SUCCESS; diff --git a/src/sql/resolver/dml/ob_hint.h b/src/sql/resolver/dml/ob_hint.h index c4d2d47fc3..135c0df638 100644 --- a/src/sql/resolver/dml/ob_hint.h +++ b/src/sql/resolver/dml/ob_hint.h @@ -305,6 +305,7 @@ struct ObGlobalHint { { return (direct_load_hint_.is_enable() && direct_load_hint_.is_inc_replace_load_method()); } + bool get_direct_load_need_sort() const; // wether should generate optimizer_statistics_operator. bool should_generate_osg_operator () const {