Code review comment: To make sure the mv's plan is selected forcefully,  skip to check upper cost for the group expression which contains mv rewrite plan.

Code before review:
public void execute() {
if (groupExpression.isUnused()) {
return;
}
if (!checkCTEPropertyValid(groupExpression, context.getRequiredProperty())) {
return;
}
initRequiredProperties();
SessionVariable sessionVariable = context.getOptimizerContext().getSessionVariable();
boolean isMVPlanForceRewrite = sessionVariable.isEnableMaterializedViewForceRewrite() &&
MvUtils.hasAppliedMVRules(groupExpression);
for (; curPropertyPairIndex < childrenRequiredPropertiesList.size(); curPropertyPairIndex++) {
List<PhysicalPropertySet> childrenRequiredProperties =
childrenRequiredPropertiesList.get(curPropertyPairIndex);
if (curChildIndex == 0 && prevChildIndex == -1) {
localCost = CostModel.calculateCost(groupExpression);
curTotalCost += localCost;
}
for (; curChildIndex < groupExpression.getInputs().size(); curChildIndex++) {
PhysicalPropertySet childRequiredProperty = childrenRequiredProperties.get(curChildIndex);
Group childGroup = groupExpression.getInputs().get(curChildIndex);
GroupExpression childBestExpr = childGroup.getBestExpression(childRequiredProperty);
if (childBestExpr == null && prevChildIndex >= curChildIndex) {
break;
}
if (childBestExpr == null) {
prevChildIndex = curChildIndex;
optimizeChildGroup(childRequiredProperty, childGroup);
return;
}
childrenBestExprList.add(childBestExpr);
PhysicalPropertySet childOutputProperty = childBestExpr.getOutputProperty(childRequiredProperty);
childrenOutputProperties.add(childOutputProperty);
childrenRequiredProperties.set(curChildIndex, childOutputProperty);
if (isMVPlanForceRewrite) {
continue;
}
if (!canGenerateOneStageAgg(childBestExpr)) {
break;
}
if (!checkBroadcastRowCountLimit(childRequiredProperty, childBestExpr)) {
break;
}
curTotalCost += childBestExpr.getCost(childRequiredProperty);
if (curTotalCost > context.getUpperBoundCost()) {
break;
}
}
if (curChildIndex == groupExpression.getInputs().size()) {
ChildOutputPropertyGuarantor childOutputPropertyGuarantor = new ChildOutputPropertyGuarantor(context,
groupExpression,
context.getRequiredProperty(),
childrenBestExprList,
childrenRequiredProperties,
childrenOutputProperties,
curTotalCost);
curTotalCost = childOutputPropertyGuarantor.enforceLegalChildOutputProperty();
if (curTotalCost > context.getUpperBoundCost()) {
break;
}
if (!computeCurrentGroupStatistics()) {
return;
}
OutputPropertyDeriver outputPropertyDeriver = new OutputPropertyDeriver(groupExpression,
context.getRequiredProperty(), childrenOutputProperties);
PhysicalPropertySet outputProperty = outputPropertyDeriver.getOutputProperty();
recordCostsAndEnforce(outputProperty, childrenRequiredProperties);
}
prevChildIndex = -1;
curChildIndex = 0;
curTotalCost = 0;
childrenBestExprList.clear();
childrenOutputProperties.clear();
}
}

Line under review: if (isMVPlanForceRewrite) {

Code after addressing the review:
public void execute() {
if (groupExpression.isUnused()) {
return;
}
if (!checkCTEPropertyValid(groupExpression, context.getRequiredProperty())) {
return;
}
if (context.getOptimizerContext().getSessionVariable().isEnableMaterializedViewForceRewrite() &&
groupExpression.getGroup().hasMVGroupExpression()) {
if (!groupExpression.hasAppliedMVRules()) {
return;
} else {
groupExpression.getGroup().forceChooseMVExpression(context);
}
}
initRequiredProperties();
for (; curPropertyPairIndex < childrenRequiredPropertiesList.size(); curPropertyPairIndex++) {
List<PhysicalPropertySet> childrenRequiredProperties =
childrenRequiredPropertiesList.get(curPropertyPairIndex);
if (curChildIndex == 0 && prevChildIndex == -1) {
localCost = CostModel.calculateCost(groupExpression);
curTotalCost += localCost;
}
for (; curChildIndex < groupExpression.getInputs().size(); curChildIndex++) {
PhysicalPropertySet childRequiredProperty = childrenRequiredProperties.get(curChildIndex);
Group childGroup = groupExpression.getInputs().get(curChildIndex);
GroupExpression childBestExpr = childGroup.getBestExpression(childRequiredProperty);
if (childBestExpr == null && prevChildIndex >= curChildIndex) {
break;
}
if (childBestExpr == null) {
prevChildIndex = curChildIndex;
optimizeChildGroup(childRequiredProperty, childGroup);
return;
}
childrenBestExprList.add(childBestExpr);
PhysicalPropertySet childOutputProperty = childBestExpr.getOutputProperty(childRequiredProperty);
childrenOutputProperties.add(childOutputProperty);
childrenRequiredProperties.set(curChildIndex, childOutputProperty);
if (!canGenerateOneStageAgg(childBestExpr)) {
break;
}
if (!checkBroadcastRowCountLimit(childRequiredProperty, childBestExpr)) {
break;
}
curTotalCost += childBestExpr.getCost(childRequiredProperty);
if (curTotalCost > context.getUpperBoundCost()) {
break;
}
}
if (curChildIndex == groupExpression.getInputs().size()) {
ChildOutputPropertyGuarantor childOutputPropertyGuarantor = new ChildOutputPropertyGuarantor(context,
groupExpression,
context.getRequiredProperty(),
childrenBestExprList,
childrenRequiredProperties,
childrenOutputProperties,
curTotalCost);
curTotalCost = childOutputPropertyGuarantor.enforceLegalChildOutputProperty();
if (curTotalCost > context.getUpperBoundCost()) {
break;
}
if (!computeCurrentGroupStatistics()) {
return;
}
OutputPropertyDeriver outputPropertyDeriver = new OutputPropertyDeriver(groupExpression,
context.getRequiredProperty(), childrenOutputProperties);
PhysicalPropertySet outputProperty = outputPropertyDeriver.getOutputProperty();
recordCostsAndEnforce(outputProperty, childrenRequiredProperties);
}
prevChildIndex = -1;
curChildIndex = 0;
curTotalCost = 0;
childrenBestExprList.clear();
childrenOutputProperties.clear();
}
}