code: private void placeTVEsForOrderby(
              Map<AbstractExpression, Integer> aggTableIndexMap,
              Map<Integer, ParsedColInfo> indexToColumnMap) {
          // Detect the edge order by case
          detectComplexOrderby();
  
          if (isComplexOrderBy()) {
              // Case that ORDER BY is below Projection node
              boolean replaceAll = hasComplexAgg() || hasComplexGroupby();
              for (ParsedColInfo orderCol : m_orderColumns) {
                  AbstractExpression expr = orderCol.m_expression;
                  if (replaceAll ||
                          expr.hasAnySubexpressionOfClass(AggregateExpression.class)) {
                      orderCol.m_expression = expr.replaceWithTVE(aggTableIndexMap, indexToColumnMap);
                  }
              }
          }
          else if (hasAggregateOrGroupby()) {
              // Case that ORDER BY is above Projection node
  
              Map<AbstractExpression, Integer> displayIndexMap = new HashMap<>();
              Map<Integer, ParsedColInfo> displayIndexToColumnMap = new HashMap<>();
  
              int orderByIndex = 0;
              for (ParsedColInfo col : m_displayColumns) {
                  displayIndexMap.put(col.m_expression, orderByIndex);
                  assert(col.m_alias != null);
                  displayIndexToColumnMap.put(orderByIndex, col);
                  orderByIndex++;
              }
  
              // place the TVEs from Display columns in the ORDER BY expression
              for (ParsedColInfo orderCol : m_orderColumns) {
                  AbstractExpression expr = orderCol.m_expression;
                  expr = expr.replaceWithTVE(displayIndexMap, displayIndexToColumnMap);
                  orderCol.m_expression = expr;
              }
          }
          // other cases like (2) from the function comments.
      }
docs: Replace TVE for order by columns.
  Restrictions: Order by clause must operate on display column list,
  except for columns that are
  (1) GROUP BY columns list without DISTINCT,
  (2) in tables or joined tables without GROUP BY or DISTINCT clause.
  
  Display columns means exact columns/expressions
  in the select list or tag alias.
  Order by clause can be columns or expressions on the columns.