diff --git a/src/backend/gpopt/config/CConfigParamMapping.cpp b/src/backend/gpopt/config/CConfigParamMapping.cpp index 54bcc0698a3..8d490444e84 100644 --- a/src/backend/gpopt/config/CConfigParamMapping.cpp +++ b/src/backend/gpopt/config/CConfigParamMapping.cpp @@ -290,7 +290,11 @@ CConfigParamMapping::SConfigMappingElem CConfigParamMapping::m_elements[] = { false, // m_negate_param GPOS_WSZ_LIT( "Explore a nested loop join even if a hash join is possible")}, - + {EopttraceEnableUseDistributionInDQA, + &optimizer_enable_use_distribution_in_dqa, + false, // m_negate_param + GPOS_WSZ_LIT( + "Enable use the distribution key in DQA")}, }; //--------------------------------------------------------------------------- diff --git a/src/backend/gporca/libgpopt/src/operators/CPhysicalAgg.cpp b/src/backend/gporca/libgpopt/src/operators/CPhysicalAgg.cpp index 1100b56e522..c20d68cc42f 100644 --- a/src/backend/gporca/libgpopt/src/operators/CPhysicalAgg.cpp +++ b/src/backend/gporca/libgpopt/src/operators/CPhysicalAgg.cpp @@ -115,12 +115,17 @@ CPhysicalAgg::CPhysicalAgg( ulDistrReqs = 2; } - // Split DQA generates a 2-stage aggregate to handle the case where - // hash aggregate has a distinct agg func. Here we need to be careful - // not to prohibit distribution property enforcement. - m_should_enforce_distribution &= !( - isAggFromSplitDQA && aggStage == CLogicalGbAgg::EasTwoStageScalarDQA && - colref_array->Size() > 0); + // Force enable distribution property in DQA + if (GPOS_FTRACE(EopttraceEnableUseDistributionInDQA)) { + m_should_enforce_distribution = false; + } else { + // Split DQA generates a 2-stage aggregate to handle the case where + // hash aggregate has a distinct agg func. Here we need to be careful + // not to prohibit distribution property enforcement. + m_should_enforce_distribution &= !( + isAggFromSplitDQA && aggStage == CLogicalGbAgg::EasTwoStageScalarDQA && + colref_array->Size() > 0); + } SetDistrRequests(ulDistrReqs); } diff --git a/src/backend/gporca/libnaucrates/include/naucrates/traceflags/traceflags.h b/src/backend/gporca/libnaucrates/include/naucrates/traceflags/traceflags.h index 042869c1f5c..d1bc807e8d7 100644 --- a/src/backend/gporca/libnaucrates/include/naucrates/traceflags/traceflags.h +++ b/src/backend/gporca/libnaucrates/include/naucrates/traceflags/traceflags.h @@ -210,11 +210,14 @@ enum EOptTraceFlag // Use legacy (cdbhash) opfamilies for compatibility EopttraceUseLegacyOpfamilies = 103039, - // enable NL Left Join plan alternatives where inner child is redistributed if possible + // Enable NL Left Join plan alternatives where inner child is redistributed if possible EopttraceEnableRedistributeNLLOJInnerChild = 103040, EopttraceForceComprehensiveJoinImplementation = 103041, + // Enable use the distribution key in DQA + EopttraceEnableUseDistributionInDQA = 103042, + /////////////////////////////////////////////////////// ///////////////////// statistics flags //////////////// ////////////////////////////////////////////////////// diff --git a/src/backend/utils/misc/guc_gp.c b/src/backend/utils/misc/guc_gp.c index 35e7078ff9b..c1ef312f756 100644 --- a/src/backend/utils/misc/guc_gp.c +++ b/src/backend/utils/misc/guc_gp.c @@ -405,6 +405,7 @@ bool optimizer_enable_space_pruning; bool optimizer_enable_associativity; bool optimizer_enable_eageragg; bool optimizer_enable_range_predicate_dpe; +bool optimizer_enable_use_distribution_in_dqa; /* Analyze related GUCs for Optimizer */ bool optimizer_analyze_root_partition; @@ -2952,6 +2953,17 @@ struct config_bool ConfigureNamesBool_gp[] = NULL, NULL, NULL }, + { + {"optimizer_enable_use_distribution_in_dqa", PGC_USERSET, DEVELOPER_OPTIONS, + gettext_noop("Enable use the distribution key in DQA"), + NULL, + GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE + }, + &optimizer_enable_use_distribution_in_dqa, + false, + NULL, NULL, NULL + }, + { {"gp_pause_on_restore_point_replay", PGC_SIGHUP, DEVELOPER_OPTIONS, gettext_noop("Pause recovery when a restore point is replayed."), diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h index f8a715632c3..512b43b065a 100644 --- a/src/include/utils/guc.h +++ b/src/include/utils/guc.h @@ -584,6 +584,7 @@ extern bool optimizer_cte_inlining; extern bool optimizer_enable_space_pruning; extern bool optimizer_enable_associativity; extern bool optimizer_enable_range_predicate_dpe; +extern bool optimizer_enable_use_distribution_in_dqa; /* Analyze related GUCs for Optimizer */ extern bool optimizer_analyze_root_partition; diff --git a/src/include/utils/unsync_guc_name.h b/src/include/utils/unsync_guc_name.h index 2e74fc61eae..4f23d394e10 100644 --- a/src/include/utils/unsync_guc_name.h +++ b/src/include/utils/unsync_guc_name.h @@ -423,6 +423,7 @@ "optimizer_enable_partition_propagation", "optimizer_enable_partition_selection", "optimizer_enable_range_predicate_dpe", + "optimizer_enable_use_distribution_in_dqa", "optimizer_enable_redistribute_nestloop_loj_inner_child", "optimizer_enable_replicated_table", "optimizer_enable_sort", diff --git a/src/test/regress/expected/gp_dqa.out b/src/test/regress/expected/gp_dqa.out index 6fc8baf7614..9090a1547a8 100644 --- a/src/test/regress/expected/gp_dqa.out +++ b/src/test/regress/expected/gp_dqa.out @@ -2377,6 +2377,10 @@ select count(distinct a) from t_issue_659; (1 row) set gp_eager_distinct_dedup = on; +-- for ORCA +set optimizer_force_three_stage_scalar_dqa to on; +set optimizer_force_multistage_agg to on; +set optimizer_enable_use_distribution_in_dqa to on; explain(costs off) select count(distinct a) from t_issue_659; QUERY PLAN @@ -2397,4 +2401,7 @@ select count(distinct a) from t_issue_659; (1 row) reset gp_eager_distinct_dedup; +reset optimizer_force_three_stage_scalar_dqa; +reset optimizer_force_multistage_agg; +reset optimizer_enable_use_distribution_in_dqa; drop table t_issue_659; diff --git a/src/test/regress/expected/gp_dqa_optimizer.out b/src/test/regress/expected/gp_dqa_optimizer.out index 12ee39cadd2..43e023d1006 100644 --- a/src/test/regress/expected/gp_dqa_optimizer.out +++ b/src/test/regress/expected/gp_dqa_optimizer.out @@ -2524,16 +2524,23 @@ select count(distinct a) from t_issue_659; (1 row) set gp_eager_distinct_dedup = on; +-- for ORCA +set optimizer_force_three_stage_scalar_dqa to on; +set optimizer_force_multistage_agg to on; +set optimizer_enable_use_distribution_in_dqa to on; explain(costs off) select count(distinct a) from t_issue_659; - QUERY PLAN ------------------------------------------------- - Finalize Aggregate + QUERY PLAN +------------------------------------------------- + Aggregate -> Gather Motion 3:1 (slice1; segments: 3) - -> Partial Aggregate - -> Seq Scan on t_issue_659 + -> HashAggregate + Group Key: a + -> Streaming HashAggregate + Group Key: a + -> Seq Scan on t_issue_659 Optimizer: Pivotal Optimizer (GPORCA) -(5 rows) +(8 rows) select count(distinct a) from t_issue_659; count @@ -2542,4 +2549,7 @@ select count(distinct a) from t_issue_659; (1 row) reset gp_eager_distinct_dedup; +reset optimizer_force_three_stage_scalar_dqa; +reset optimizer_force_multistage_agg; +reset optimizer_enable_use_distribution_in_dqa; drop table t_issue_659; diff --git a/src/test/regress/sql/gp_dqa.sql b/src/test/regress/sql/gp_dqa.sql index a4b8a099102..65f52cd7436 100644 --- a/src/test/regress/sql/gp_dqa.sql +++ b/src/test/regress/sql/gp_dqa.sql @@ -419,8 +419,15 @@ explain(costs off) select count(distinct a) from t_issue_659; select count(distinct a) from t_issue_659; set gp_eager_distinct_dedup = on; +-- for ORCA +set optimizer_force_three_stage_scalar_dqa to on; +set optimizer_force_multistage_agg to on; +set optimizer_enable_use_distribution_in_dqa to on; explain(costs off) select count(distinct a) from t_issue_659; select count(distinct a) from t_issue_659; reset gp_eager_distinct_dedup; +reset optimizer_force_three_stage_scalar_dqa; +reset optimizer_force_multistage_agg; +reset optimizer_enable_use_distribution_in_dqa; drop table t_issue_659;