core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java
/**
   * Trims a child relational expression, then adds back a dummy project to
   * restore the fields that were removed.
   *
   * <p>Sounds pointless? It causes unused fields to be removed
   * further down the tree (towards the leaves), but it ensure that the
   * consuming relational expression continues to see the same fields.
   *
   * @param rel        Relational expression
   * @param input      Input relational expression, whose fields to trim
   * @param fieldsUsed Bitmap of fields needed by the consumer
   * @return New relational expression and its field mapping
   */
Trims a child relational expression, then adds back a dummy project to
restore the fields that were removed.

Sounds pointless. It causes unused fields to be removed
further down the tree (towards the leaves), but it ensure that the
consuming relational expression continues to see the same fields.

@param rel        Relational expression
@param input      Input relational expression, whose fields to trim
@param fieldsUsed Bitmap of fields needed by the consumer
@return New relational expression and its field mapping
Trims a child relational expression then adds back a dummy project to restore the fields that were removed . Sounds pointless . It causes unused fields to be removed further down the tree ( towards the leaves ) but it ensure that the consuming relational expression continues to see the same fields . @param rel Relational expression @param input Input relational expression whose fields to trim @param fieldsUsed Bitmap of fields needed by the consumer @return New relational expression and its field mapping
protected TrimResult trimChildRestore(
      RelNode rel,
      RelNode input,
      ImmutableBitSet fieldsUsed,
      Set<RelDataTypeField> extraFields) {
    TrimResult trimResult = trimChild(rel, input, fieldsUsed, extraFields);
    if (trimResult.right.isIdentity()) {
      return trimResult;
    }
    final RelDataType rowType = input.getRowType();
    List<RelDataTypeField> fieldList = rowType.getFieldList();
    final List<RexNode> exprList = new ArrayList<RexNode>();
    final List<String> nameList = rowType.getFieldNames();
    RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
    assert trimResult.right.getSourceCount() == fieldList.size();
    for (int i = 0; i < fieldList.size(); i++) {
      int source = trimResult.right.getTargetOpt(i);
      RelDataTypeField field = fieldList.get(i);
      exprList.add(
          source < 0
              ? rexBuilder.makeZeroLiteral(field.getType())
              : rexBuilder.makeInputRef(field.getType(), source));
    }
    RelNode project =
        projectFactory.createProject(
            trimResult.left, exprList, nameList);
    return new TrimResult(
        project,
        Mappings.createIdentity(fieldList.size()));
  }
protected TrimResult trimChildRestore ( RelNode rel , RelNode input , ImmutableBitSet fieldsUsed , Set < RelDataTypeField > extraFields ) { TrimResult trimResult = trimChild ( rel , input , fieldsUsed , extraFields ) ; if ( trimResult . right . isIdentity ( ) ) { return trimResult ; } final RelDataType rowType = input . getRowType ( ) ; List < RelDataTypeField > fieldList = rowType . getFieldList ( ) ; final List < RexNode > exprList = new ArrayList < RexNode > ( ) ; final List < String > nameList = rowType . getFieldNames ( ) ; RexBuilder rexBuilder = rel . getCluster ( ) . getRexBuilder ( ) ; assert trimResult . right . getSourceCount ( ) == fieldList . size ( ) ; for ( int i = 0 ; i < fieldList . size ( ) ; i ++ ) { int source = trimResult . right . getTargetOpt ( i ) ; RelDataTypeField field = fieldList . get ( i ) ; exprList . add ( source < 0 ? rexBuilder . makeZeroLiteral ( field . getType ( ) ) : rexBuilder . makeInputRef ( field . getType ( ) , source ) ) ; } RelNode project = projectFactory . createProject ( trimResult . left , exprList , nameList ) ; return new TrimResult ( project , Mappings . createIdentity ( fieldList . size ( ) ) ) ; }
Trims a child relational expression, then adds back a dummy project to
 restore the fields that were removed.
Trims a child relational expression then adds back a dummy project to restore the fields that were removed .