Code review comment: >          List<ScalarOperator> partitions = new ArrayList<>();             for (Expr partitionExpression : windowOperator.getPartitionExprs()) {                 ScalarOperator operator = SqlToScalarOperatorTranslator                         .translate(partitionExpression, subOpt.getExpressionMapping(), columnRefFactory);                 partitions.add(operator);             }              List<Ordering> orderings = new ArrayList<>();             for (OrderByElement orderByElement : windowOperator.getOrderByElements()) {                 ColumnRefOperator col =                         (ColumnRefOperator) SqlToScalarOperatorTranslator                                 .translate(orderByElement.getExpr(), subOpt.getExpressionMapping(), columnRefFactory);                 orderings.add(new Ordering(col, orderByElement.getIsAsc(),                         OrderByElement.nullsFirst(orderByElement.getNullsFirstParam())));             }  Shortcut characteristic is one of optimization, but the main purpose is to reduce SqlToScalarOperatorTranslator.translate method call  in reorderWindowOperator method  @packy92 @LiShuMing     

Code before review:
private OptExprBuilder window(OptExprBuilder subOpt, List<AnalyticExpr> window) {
if (window.isEmpty()) {
return subOpt;
}
/*
* Build ProjectOperator of partition expression and order by expression in window function.
*/
List<Expr> projectExpressions = new ArrayList<>();
for (AnalyticExpr analyticExpr : window) {
projectExpressions.addAll(analyticExpr.getPartitionExprs());
projectExpressions.addAll(analyticExpr.getOrderByElements()
.stream().map(OrderByElement::getExpr).collect(Collectors.toList()));
}
final ExpressionMapping expressionMapping = subOpt.getExpressionMapping();
boolean allColumnRef = true;
Map<Expr, ColumnRefOperator> tempMapping = new HashMap<>();
for (Expr expression : projectExpressions) {
ScalarOperator operator = SqlToScalarOperatorTranslator.translate(expression, expressionMapping,
columnRefFactory);
if (!operator.isColumnRef()) {
allColumnRef = false;
tempMapping.clear();
break;
} else {
tempMapping.put(expression, (ColumnRefOperator) operator);
}
}
if (allColumnRef) {
expressionMapping.getExpressionToColumns().putAll(tempMapping);
}
/*
* If there is no expression calculate in partition and order by,
* there is no need to add ProjectOperator here
*/
if (!allColumnRef) {
ExpressionMapping outputTranslations = new ExpressionMapping(subOpt.getScope());
List<ColumnRefOperator> fieldMappings = new ArrayList<>();
Map<ColumnRefOperator, ScalarOperator> projections = Maps.newHashMap();
for (ColumnRefOperator expression : subOpt.getFieldMappings()) {
ColumnRefOperator variable =
columnRefFactory.create(expression, expression.getType(), expression.isNullable());
projections.put(variable, expression);
fieldMappings.add(variable);
}
outputTranslations.setFieldMappings(fieldMappings);
for (Expr expression : subOpt.getExpressionMapping().getAllExpressions()) {
ColumnRefOperator columnRef = findOrCreateColumnRefForExpr(expression,
subOpt.getExpressionMapping(), projections, columnRefFactory);
outputTranslations.put(expression, columnRef);
}
for (Expr expression : projectExpressions) {
ColumnRefOperator columnRef = findOrCreateColumnRefForExpr(expression,
subOpt.getExpressionMapping(), projections, columnRefFactory);
outputTranslations.put(expression, columnRef);
}
LogicalProjectOperator projectOperator = new LogicalProjectOperator(projections);
subOpt.setExpressionMapping(outputTranslations);
subOpt = subOpt.withNewRoot(projectOperator);
}
/*
* If necessary, rewrites the analytic function, window, and/or order-by elements
* into a standard format for the purpose of simpler backend execution
*/
List<WindowTransformer.WindowOperator> windowOperators = new ArrayList<>();
for (AnalyticExpr analyticExpr : window) {
WindowTransformer.WindowOperator rewriteOperator = WindowTransformer.standardize(analyticExpr);
if (windowOperators.contains(rewriteOperator)) {
WindowTransformer.WindowOperator windowOperator =
windowOperators.get(windowOperators.indexOf(rewriteOperator));
if (rewriteOperator.isSkewed()) {
windowOperator.setSkewed();
}
windowOperator.addFunction(analyticExpr);
} else {
windowOperators.add(rewriteOperator);
}
}
List<LogicalWindowOperator> logicalWindowOperators =
WindowTransformer.reorderWindowOperator(windowOperators, columnRefFactory, subOpt);
for (LogicalWindowOperator logicalWindowOperator : logicalWindowOperators) {
subOpt = subOpt.withNewRoot(logicalWindowOperator);
}
return subOpt;
}

Line under review: }

Code after addressing the review:
private OptExprBuilder window(OptExprBuilder subOpt, List<AnalyticExpr> window) {
if (window.isEmpty()) {
return subOpt;
}
/*
* Build ProjectOperator of partition expression and order by expression in window function.
*/
List<Expr> projectExpressions = new ArrayList<>();
for (AnalyticExpr analyticExpr : window) {
projectExpressions.addAll(analyticExpr.getPartitionExprs());
projectExpressions.addAll(analyticExpr.getOrderByElements()
.stream().map(OrderByElement::getExpr).collect(Collectors.toList()));
}
final ExpressionMapping expressionMapping = subOpt.getExpressionMapping();
boolean allColumnRef = true;
Map<Expr, ColumnRefOperator> tempMapping = new HashMap<>();
for (Expr expression : projectExpressions) {
ScalarOperator operator = SqlToScalarOperatorTranslator.translate(expression, expressionMapping,
columnRefFactory);
if (!operator.isColumnRef()) {
allColumnRef = false;
tempMapping.clear();
break;
} else {
tempMapping.put(expression, (ColumnRefOperator) operator);
}
}
if (allColumnRef) {
expressionMapping.getExpressionToColumns().putAll(tempMapping);
}
/*
* If there is no expression calculate in partition and order by,
* there is no need to add ProjectOperator here
*/
if (!allColumnRef) {
ExpressionMapping outputTranslations = new ExpressionMapping(subOpt.getScope());
List<ColumnRefOperator> fieldMappings = new ArrayList<>();
Map<ColumnRefOperator, ScalarOperator> projections = Maps.newHashMap();
for (ColumnRefOperator expression : subOpt.getFieldMappings()) {
ColumnRefOperator variable =
columnRefFactory.create(expression, expression.getType(), expression.isNullable());
projections.put(variable, expression);
fieldMappings.add(variable);
}
outputTranslations.setFieldMappings(fieldMappings);
for (Expr expression : subOpt.getExpressionMapping().getAllExpressions()) {
ColumnRefOperator columnRef = findOrCreateColumnRefForExpr(expression,
subOpt.getExpressionMapping(), projections, columnRefFactory);
outputTranslations.put(expression, columnRef);
}
for (Expr expression : projectExpressions) {
ColumnRefOperator columnRef = findOrCreateColumnRefForExpr(expression,
subOpt.getExpressionMapping(), projections, columnRefFactory);
outputTranslations.put(expression, columnRef);
}
LogicalProjectOperator projectOperator = new LogicalProjectOperator(projections);
subOpt.setExpressionMapping(outputTranslations);
subOpt = subOpt.withNewRoot(projectOperator);
}
/*
* If necessary, rewrites the analytic function, window, and/or order-by elements
* into a standard format for the purpose of simpler backend execution
*/
List<WindowTransformer.WindowOperator> windowOperators = new ArrayList<>();
for (AnalyticExpr analyticExpr : window) {
WindowTransformer.WindowOperator rewriteOperator = WindowTransformer.standardize(analyticExpr);
if (windowOperators.contains(rewriteOperator)) {
WindowTransformer.WindowOperator windowOperator =
windowOperators.get(windowOperators.indexOf(rewriteOperator));
if (rewriteOperator.isSkewed()) {
windowOperator.setSkewed();
}
windowOperator.addFunction(analyticExpr);
} else {
windowOperators.add(rewriteOperator);
}
}
List<LogicalWindowOperator> logicalWindowOperators =
WindowTransformer.reorderWindowOperator(windowOperators, columnRefFactory, subOpt);
for (LogicalWindowOperator logicalWindowOperator : logicalWindowOperators) {
subOpt = subOpt.withNewRoot(logicalWindowOperator);
}
return subOpt;
}