Code review comment: The most risky bug in this code is: The updated code snippet potentially modifies the `expressionMapping` for each column reference even when `allColumnRef` might later be set to false.  You can modify the code like this: ``` @@ -292,10 +292,18 @@ private OptExprBuilder window(OptExprBuilder subOpt, List<AnalyticExpr> window)          }            final ExpressionMapping expressionMapping = subOpt.getExpressionMapping(); +        Map<Expr, ColumnRefOperator> tempMapping = new HashMap<>();          boolean allColumnRef = true;          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.putAll(tempMapping);          } ```

Code under 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;
        for (Expr expression : projectExpressions) {
            ScalarOperator operator = SqlToScalarOperatorTranslator.translate(expression, expressionMapping,
                    columnRefFactory);
            if (!operator.isColumnRef()) {
                allColumnRef = false;
            } else {
                expressionMapping.put(expression, (ColumnRefOperator) operator);
            }
        }

        /*
         * 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 commented on: Map<ColumnRefOperator, ScalarOperator> projections = Maps.newHashMap();

Revised code:
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;
    }