morrySnow commented on code in PR #13046: URL: https://github.com/apache/doris/pull/13046#discussion_r1012478753
########## fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/cascades/CostAndEnforcerJob.java: ########## @@ -164,74 +167,100 @@ public void execute() { if (curTotalCost > context.getCostUpperBound()) { break; } + // the request child properties will be covered by the output properties + // that corresponding to the request properties. so if we run a costAndEnforceJob of the same + // group expression, that request child properties will be different of this. } // This mean that we successfully optimize all child groups. + // if break when running the loop above, the condition must be false. if (curChildIndex == groupExpression.arity()) { - - // to ensure distributionSpec has been added sufficiently. - ChildrenPropertiesRegulator regulator = new ChildrenPropertiesRegulator(groupExpression, - lowestCostChildren, requestChildrenProperties, requestChildrenProperties, context); - double enforceCost = regulator.adjustChildrenProperties(); - if (enforceCost < 0) { - // invalid enforce, return. - return; - } - curTotalCost += enforceCost; - - // Not need to do pruning here because it has been done when we get the - // best expr from the child group - ChildOutputPropertyDeriver childOutputPropertyDeriver - = new ChildOutputPropertyDeriver(requestChildrenProperties); - PhysicalProperties outputProperty = childOutputPropertyDeriver.getOutputProperties(groupExpression); - - // update current group statistics and re-compute costs. - if (groupExpression.children().stream().anyMatch(group -> group.getStatistics() == null)) { - // if we come here, mean that we have some error in stats calculator and should fix it. + if (!calculateEnforce(requestChildrenProperties)) { return; } - StatsCalculator.estimate(groupExpression); - curTotalCost -= curNodeCost; - curNodeCost = CostCalculator.calculateCost(groupExpression); - groupExpression.setCost(curNodeCost); - curTotalCost += curNodeCost; - - // record map { outputProperty -> outputProperty }, { ANY -> outputProperty }, - recordPropertyAndCost(groupExpression, outputProperty, PhysicalProperties.ANY, - requestChildrenProperties); - recordPropertyAndCost(groupExpression, outputProperty, outputProperty, requestChildrenProperties); - enforce(outputProperty, requestChildrenProperties); - if (curTotalCost < context.getCostUpperBound()) { context.setCostUpperBound(curTotalCost); } } - clear(); } } + /** + * calculate enforce + * @return false if error occurs, the caller will return. + */ + private boolean calculateEnforce(List<PhysicalProperties> requestChildrenProperties) { + // to ensure distributionSpec has been added sufficiently. + // it's certain that lowestCostChildren is equals to arity(). + ChildrenPropertiesRegulator regulator = new ChildrenPropertiesRegulator(groupExpression, + lowestCostChildren, requestChildrenProperties, requestChildrenProperties, context); + double enforceCost = regulator.adjustChildrenProperties(); + if (enforceCost < 0) { + // invalid enforce, return. + return false; + } + curTotalCost += enforceCost; + + // Not need to do pruning here because it has been done when we get the + // best expr from the child group + ChildOutputPropertyDeriver childOutputPropertyDeriver + = new ChildOutputPropertyDeriver(requestChildrenProperties); + // the physical properties the group expression support for its parent. + PhysicalProperties outputProperty = childOutputPropertyDeriver.getOutputProperties(groupExpression); + + // update current group statistics and re-compute costs. + if (groupExpression.children().stream().anyMatch(group -> group.getStatistics() == null)) { + // if we come here, mean that we have some error in stats calculator and should fix it. + return false; + } + StatsCalculator.estimate(groupExpression); + curTotalCost -= curNodeCost; + curNodeCost = CostCalculator.calculateCost(groupExpression); + groupExpression.setCost(curNodeCost); + curTotalCost += curNodeCost; + + // record map { outputProperty -> outputProperty }, { ANY -> outputProperty }, + recordPropertyAndCost(groupExpression, outputProperty, PhysicalProperties.ANY, + requestChildrenProperties); + recordPropertyAndCost(groupExpression, outputProperty, outputProperty, requestChildrenProperties); + enforce(outputProperty, requestChildrenProperties); + return true; + } + + /** + * add enforce node + * @param outputProperty the group expression's out property + * @param requestChildrenProperty the group expression's request to its child. + */ private void enforce(PhysicalProperties outputProperty, List<PhysicalProperties> requestChildrenProperty) { PhysicalProperties requiredProperties = context.getRequiredProperties(); - if (!outputProperty.satisfy(requiredProperties)) { - EnforceMissingPropertiesHelper enforceMissingPropertiesHelper - = new EnforceMissingPropertiesHelper(context, groupExpression, curTotalCost); - PhysicalProperties addEnforcedProperty = enforceMissingPropertiesHelper - .enforceProperty(outputProperty, requiredProperties); - curTotalCost = enforceMissingPropertiesHelper.getCurTotalCost(); - - // enforcedProperty is superset of requiredProperty - if (!addEnforcedProperty.equals(requiredProperties)) { - recordPropertyAndCost(groupExpression.getOwnerGroup().getBestPlan(addEnforcedProperty), - addEnforcedProperty, requiredProperties, Lists.newArrayList(outputProperty)); - } - } else { + if (outputProperty.satisfy(requiredProperties)) { if (!outputProperty.equals(requiredProperties)) { recordPropertyAndCost(groupExpression, outputProperty, requiredProperties, requestChildrenProperty); } + return; + } + EnforceMissingPropertiesHelper enforceMissingPropertiesHelper + = new EnforceMissingPropertiesHelper(context, groupExpression, curTotalCost); + PhysicalProperties addEnforcedProperty = enforceMissingPropertiesHelper + .enforceProperty(outputProperty, requiredProperties); + curTotalCost = enforceMissingPropertiesHelper.getCurTotalCost(); + + // enforcedProperty is superset of requiredProperty + if (!addEnforcedProperty.equals(requiredProperties)) { + recordPropertyAndCost(groupExpression.getOwnerGroup().getBestPlan(addEnforcedProperty), + addEnforcedProperty, requiredProperties, Lists.newArrayList(outputProperty)); } } + /** + * record property and cost Review Comment: should add a blank line between method doc and param docs. plz fix it in next PR -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org