private int genSortByKey(ASTNode ref) throws SemanticException {
      // selPair.getKey() is the operator right before OB
      // selPair.getValue() is RR which only contains columns needed in result
      // set. Extra columns needed by order by will be absent from it.
      RelNode srcRel = selPair.getKey();
      RowResolver selectOutputRR = selPair.getValue();
      RowResolver inputRR = calcitePlannerAction.relToHiveRR.get(srcRel);

      int srcRelRecordSz = srcRel.getRowType().getFieldCount();
      boolean isBothByPos = HiveConf.getBoolVar(conf, ConfVars.HIVE_GROUPBY_ORDERBY_POSITION_ALIAS);
      boolean isObyByPos = isBothByPos
              || HiveConf.getBoolVar(conf, ConfVars.HIVE_ORDERBY_POSITION_ALIAS);
      // replace each of the position alias in ORDERBY with the actual column
      if (ref != null && ref.getToken().getType() == HiveParser.Number) {
        if (isObyByPos) {
          return getFieldIndexFromColumnNumber(selectOutputRR, ref);
        } else { // if not using position alias and it is a number.
          LOG.warn("Using constant number {}" +
                  " in order by. If you try to use position alias when hive.orderby.position.alias is false, " +
                  "the position alias will be ignored.", ref.getText());
          return -1;
        }
      } else {
        // 2.2 Convert ExprNode to RexNode
        RexNode orderByExpression = getOrderByExpression(selectOutputRR, inputRR, ref);

        // 2.3 Determine the index of ob expr in child schema
        // NOTE: Calcite can not take compound exprs in OB without it being
        // present in the child (& hence we add a child Project Rel)
        if (orderByExpression instanceof RexInputRef) {
          return ((RexInputRef) orderByExpression).getIndex();
        } else {
          int fieldIndex = srcRelRecordSz + newVCLst.size();
          newVCLst.add(orderByExpression);
          vcASTTypePairs.add(new Pair<>(ref, TypeConverter.convert(orderByExpression.getType())));
          return fieldIndex;
        }
      }
    }
This method can be called at startup time to pre-register all the
additional Hive classes (compared to Calcite core classes) that may
be visited during the planning phase in the metadata providers
and the field trimmer.
ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java