public RexNode visitOver(RexOver over) {
      if (over.op.getName().equals(LAST_VALUE_FUNC) && over.getWindow().getLowerBound().isUnbounded()
        && over.getWindow().getUpperBound().isUnbounded()) {
        ImmutableList<RexFieldCollation> orderKeys = over.getWindow().orderKeys;
        if (CollectionUtils.isEmpty(orderKeys)) {
          return over;
        }
        ImmutableList.Builder<RexFieldCollation> newOrderKeys = ImmutableList.builder();
        for (RexFieldCollation orderKey : orderKeys) {
          Set<SqlKind> flags = new HashSet<>();
          if (orderKey.getDirection() == RelFieldCollation.Direction.ASCENDING) {
            flags.add(SqlKind.DESCENDING);
          }
          if (orderKey.right.contains(SqlKind.NULLS_FIRST)) {
            flags.add(SqlKind.NULLS_LAST);
          } else {
            flags.add(SqlKind.NULLS_FIRST);
          }
          newOrderKeys.add(new RexFieldCollation(orderKey.left, flags));
        }
        SqlAggFunction s = (SqlAggFunction) over.op;
        SqlFunctionConverter.CalciteUDAF newSqlAggFunction = new SqlFunctionConverter.CalciteUDAF(
            over.isDistinct(), FIRST_VALUE_FUNC, s.getReturnTypeInference(), s.getOperandTypeInference(),
            s.getOperandTypeChecker());
        List<RexNode> clonedOperands = visitList(over.operands, new boolean[] {false});
        RexWindow window = visitWindow(over.getWindow());
        return rexBuilder.makeOver(over.type, newSqlAggFunction, clonedOperands,
            window.partitionKeys, newOrderKeys.build(),
            window.getLowerBound(), window.getUpperBound(),
            window.isRows(), true, false, over.isDistinct(), over.ignoreNulls());
      }
      return over;
    }
Rule to rewrite a window function containing a last value clause.
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveWindowingLastValueRewrite.java