diff --git a/src/share/aggregate/processor.h b/src/share/aggregate/processor.h index 4993309aa..b18032997 100644 --- a/src/share/aggregate/processor.h +++ b/src/share/aggregate/processor.h @@ -56,7 +56,8 @@ public: int finish_adding_one_row(); inline int add_one_row(const int32_t start_agg_id, const int32_t end_agg_id, AggrRowPtr row, - const int64_t batch_idx, const int64_t batch_size, ObIVector **aggr_vectors) + const int64_t batch_idx, const int64_t batch_size, ObIVector **aggr_vectors, + ObFixedArray implicit_aggr_in_3stage_indexes) { int ret = OB_SUCCESS; ObIVector *data_vec = nullptr; @@ -68,6 +69,15 @@ public: SQL_LOG(WARN, "add one row failed", K(ret)); } } + + for (int i = 0; OB_SUCC(ret) && i < implicit_aggr_in_3stage_indexes.count(); i++) { + int col_id = implicit_aggr_in_3stage_indexes.at(i); + add_one_row_fn fn = add_one_row_fns_.at(col_id); + if (OB_FAIL( + fn(aggregates_.at(col_id), agg_ctx_, col_id, row, aggr_vectors[col_id], batch_idx, batch_size))) { + SQL_LOG(WARN, "add one row failed", K(ret)); + } + } return ret; } diff --git a/src/sql/code_generator/ob_static_engine_cg.cpp b/src/sql/code_generator/ob_static_engine_cg.cpp index 8b4cb2da9..f3c8aa1d7 100644 --- a/src/sql/code_generator/ob_static_engine_cg.cpp +++ b/src/sql/code_generator/ob_static_engine_cg.cpp @@ -7623,6 +7623,49 @@ int ObStaticEngineCG::fill_aggr_infos(ObLogGroupBy &op, aggr_info.expr_ = expr; LOG_TRACE("trace all non aggr exprs", K(*expr), K(all_non_aggr_exprs.count())); } + //6.calc for implicit_aggr, if aggr_info.expr_ only in third stage, not in second stage, + // must be calc in third stage.Normally caused by implicit aggr in filter. + if (spec.aggr_stage_ == ObThreeStageAggrStage::THIRD_STAGE) { + ObOpSpec *child_spec = &spec; + bool find_second_spec = false; + while (!find_second_spec && child_spec->get_children() != NULL + && child_spec->get_child_cnt() > 0) { + if ((child_spec->type_ == PHY_VEC_HASH_GROUP_BY || + child_spec->type_ == PHY_HASH_GROUP_BY || + child_spec->type_ == PHY_VEC_MERGE_GROUP_BY || + child_spec->type_ == PHY_MERGE_GROUP_BY) && + ((ObGroupBySpec*)child_spec)->aggr_stage_ == ObThreeStageAggrStage::SECOND_STAGE) { + find_second_spec = true; + } else { + child_spec = child_spec->get_children()[0]; + } + } + if (find_second_spec) { + if (OB_FAIL(spec.implicit_aggr_in_3stage_indexes_.prepare_allocate_and_keep_count( + spec.aggr_infos_.count()))) { + OB_LOG(WARN, "fail to prepare_allocate implicit_aggr_in_3stage_indexes_", K(ret)); + } else { + ObGroupBySpec *second_stage_spec = (ObGroupBySpec*)child_spec; + for (int i = 0; i < spec.aggr_infos_.count(); i++) { + if (spec.aggr_infos_.at(i).is_implicit_first_aggr()) { + bool exist_in_second_stage = false; + for (int j = 0; !exist_in_second_stage && j < second_stage_spec->aggr_infos_.count(); j++) { + if (spec.aggr_infos_.at(i).expr_ == second_stage_spec->aggr_infos_.at(j).expr_) { + exist_in_second_stage = true; + } + } + if (!exist_in_second_stage) { + spec.implicit_aggr_in_3stage_indexes_.push_back(i); + LOG_TRACE("find implicit aggr need calc in 3stage", K(i)); + } + } + } + } + } else { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("cannot find second stage hashgroupby op", K(ret), K(find_second_spec)); + } + } return ret; } diff --git a/src/sql/engine/aggregate/ob_groupby_op.cpp b/src/sql/engine/aggregate/ob_groupby_op.cpp index 1e01b153b..fef01e3dc 100644 --- a/src/sql/engine/aggregate/ob_groupby_op.cpp +++ b/src/sql/engine/aggregate/ob_groupby_op.cpp @@ -35,7 +35,8 @@ OB_SERIALIZE_MEMBER((ObGroupBySpec, ObOpSpec), by_pass_enabled_, support_fast_single_row_agg_, skew_detection_enabled_, // FARM COMPAT WHITELIST - llc_ndv_est_enabled_); + llc_ndv_est_enabled_, + implicit_aggr_in_3stage_indexes_); DEF_TO_STRING(ObGroupBySpec) { diff --git a/src/sql/engine/aggregate/ob_groupby_op.h b/src/sql/engine/aggregate/ob_groupby_op.h index 2a2764d2f..cec4f5da1 100644 --- a/src/sql/engine/aggregate/ob_groupby_op.h +++ b/src/sql/engine/aggregate/ob_groupby_op.h @@ -37,7 +37,8 @@ public: by_pass_enabled_(false), support_fast_single_row_agg_(false), skew_detection_enabled_(false), - llc_ndv_est_enabled_(false) + llc_ndv_est_enabled_(false), + implicit_aggr_in_3stage_indexes_(alloc) { } DECLARE_VIRTUAL_TO_STRING; @@ -62,6 +63,7 @@ public: bool support_fast_single_row_agg_; bool skew_detection_enabled_; bool llc_ndv_est_enabled_; + ObFixedArray implicit_aggr_in_3stage_indexes_; }; //modifiable diff --git a/src/sql/engine/aggregate/ob_hash_groupby_vec_op.cpp b/src/sql/engine/aggregate/ob_hash_groupby_vec_op.cpp index 5fa095ab2..81f26760e 100644 --- a/src/sql/engine/aggregate/ob_hash_groupby_vec_op.cpp +++ b/src/sql/engine/aggregate/ob_hash_groupby_vec_op.cpp @@ -1398,7 +1398,9 @@ int ObHashGroupByVecOp::load_data_batch(int64_t max_row_cnt) if (OB_FAIL(ret)) { } else if (OB_FAIL(aggr_processor_.add_one_row(start_agg_id, end_agg_id, batch_new_rows_[i], i, - child_brs->size_, aggr_vectors_))) { + child_brs->size_, aggr_vectors_, + MY_SPEC.implicit_aggr_in_3stage_indexes_))) { + LOG_WARN("fail to process row", K(ret)); } } @@ -1433,7 +1435,8 @@ int ObHashGroupByVecOp::load_data_batch(int64_t max_row_cnt) if (OB_SUCC(ret)) { if (OB_FAIL(aggr_processor_.add_one_row(start_agg_id, end_agg_id, batch_old_rows_[i], i, - child_brs->size_, aggr_vectors_))) { + child_brs->size_, aggr_vectors_, + MY_SPEC.implicit_aggr_in_3stage_indexes_))) { LOG_WARN("fail to process row", K(ret)); } } @@ -2356,7 +2359,8 @@ int ObHashGroupByVecOp::by_pass_prepare_one_batch(const int64_t batch_size) if (OB_FAIL(ret)) { } else if (OB_FAIL(aggr_processor_.add_one_row(start_agg_id, end_agg_id, batch_old_rows_[i], i, - brs_.size_, aggr_vectors_))) { + brs_.size_, aggr_vectors_, + MY_SPEC.implicit_aggr_in_3stage_indexes_))) { LOG_WARN("fail to process row", K(ret)); } else { brs_.set_skip(i);