You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2020/05/22 02:29:17 UTC

[GitHub] [calcite] xy2953396112 opened a new pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

xy2953396112 opened a new pull request #1987:
URL: https://github.com/apache/calcite/pull/1987


   https://issues.apache.org/jira/browse/CALCITE-4020


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] rubenada commented on a change in pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
rubenada commented on a change in pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#discussion_r439257255



##########
File path: core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java
##########
@@ -350,6 +354,100 @@ public TrimResult trimFields(
         Mappings.createIdentity(rel.getRowType().getFieldCount()));
   }
 
+  /**
+   * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
+   * {@link org.apache.calcite.rel.logical.LogicalCalc}.
+   */
+  public TrimResult trimFields(
+      Calc calc,
+      ImmutableBitSet fieldsUsed,
+      Set<RelDataTypeField> extraFields) {
+    final RexProgram rexProgram = calc.getProgram();
+    final List<RexNode> projs = Lists.transform(rexProgram.getProjectList(),
+        rexProgram::expandLocalRef);
+
+    final RelDataType rowType = calc.getRowType();
+    final int fieldCount = rowType.getFieldCount();
+    final RelNode input = calc.getInput();
+
+    final Set<RelDataTypeField> inputExtraFields =
+        new LinkedHashSet<>(extraFields);
+    RelOptUtil.InputFinder inputFinder =
+        new RelOptUtil.InputFinder(inputExtraFields);
+    for (Ord<RexNode> ord : Ord.zip(projs)) {
+      if (fieldsUsed.get(ord.i)) {
+        ord.e.accept(inputFinder);
+      }
+    }
+    ImmutableBitSet inputFieldsUsed = inputFinder.inputBitSet.build();
+
+    // Create input with trimmed columns.
+    TrimResult trimResult =
+        trimChild(calc, input, inputFieldsUsed, inputExtraFields);
+    RelNode newInput = trimResult.left;
+    final Mapping inputMapping = trimResult.right;
+
+    // If the input is unchanged, and we need to project all columns,
+    // there's nothing we can do.
+    if (newInput == input
+        && fieldsUsed.cardinality() == fieldCount) {
+      return result(calc, Mappings.createIdentity(fieldCount));
+    }
+
+    // Some parts of the system can't handle rows with zero fields, so
+    // pretend that one field is used.
+    if (fieldsUsed.cardinality() == 0) {
+      return dummyProject(fieldCount, newInput);
+    }
+
+    // Build new project expressions, and populate the mapping.
+    final List<RexNode> newProjects = new ArrayList<>();
+    final RexVisitor<RexNode> shuttle =
+        new RexPermuteInputsShuttle(
+            inputMapping, newInput);
+    final Mapping mapping =
+        Mappings.create(
+            MappingType.INVERSE_SURJECTION,
+            fieldCount,
+            fieldsUsed.cardinality());
+    for (Ord<RexNode> ord : Ord.zip(projs)) {
+      if (fieldsUsed.get(ord.i)) {
+        mapping.set(ord.i, newProjects.size());
+        RexNode newProjectExpr = ord.e.accept(shuttle);
+        newProjects.add(newProjectExpr);
+      }
+    }
+
+    final RelDataType newRowType =
+        RelOptUtil.permute(calc.getCluster().getTypeFactory(), rowType,
+            mapping);
+
+    final RelNode newInputRelNode = relBuilder.push(newInput).build();
+    if (rexProgram.getCondition() != null) {
+      final List<RexNode> filter = Lists.transform(
+          ImmutableList.of(
+          rexProgram.getCondition()), rexProgram::expandLocalRef);
+      assert filter.size() == 1;
+      final RexNode conditionExpr = filter.get(0);
+
+      final RexNode newConditionExpr =
+          conditionExpr.accept(shuttle);
+      final RexProgram newRexProgram = RexProgram.create(newInputRelNode.getRowType(),
+          newProjects, newConditionExpr, newRowType.getFieldNames(),
+          newInputRelNode.getCluster().getRexBuilder());
+      LogicalCalc logicalCalc = LogicalCalc.create(newInputRelNode, newRexProgram);
+
+      return result(logicalCalc, mapping);
+    } else {
+      final RexProgram newRexProgram = RexProgram
+          .create(newInputRelNode.getRowType(), newProjects, null,
+              newRowType.getFieldNames(), newInputRelNode.getCluster().getRexBuilder());
+
+      LogicalCalc logicalCalc = LogicalCalc.create(newInputRelNode, newRexProgram);
+      return result(logicalCalc, mapping);

Review comment:
       Hints need to be transferred from the original Calc to the new Calc (see CALCITE-4055).
   Also, IMHO this piece of code could be refactored, to avoid duplicating instructions at the end of the "if" and "else" block, something like:
   ```
   RexNode newConditionExpr = null;
   if (rexProgram.getCondition() != null) {
     ...
     newConditionExpr = conditionExpr.accept(shuttle);
   }
   final RexProgram newRexProgram = ...
   final LogicalCalc logicalCalc = LogicalCalc.create(newInputRelNode, newRexProgram);
   logicalCalc.withHints(calc.getHints()); // transfer hints
   return result(logicalCalc, mapping);
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] rubenada commented on a change in pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
rubenada commented on a change in pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#discussion_r439724173



##########
File path: core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java
##########
@@ -350,6 +354,100 @@ public TrimResult trimFields(
         Mappings.createIdentity(rel.getRowType().getFieldCount()));
   }
 
+  /**
+   * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
+   * {@link org.apache.calcite.rel.logical.LogicalCalc}.
+   */
+  public TrimResult trimFields(
+      Calc calc,
+      ImmutableBitSet fieldsUsed,
+      Set<RelDataTypeField> extraFields) {
+    final RexProgram rexProgram = calc.getProgram();
+    final List<RexNode> projs = Lists.transform(rexProgram.getProjectList(),
+        rexProgram::expandLocalRef);
+
+    final RelDataType rowType = calc.getRowType();
+    final int fieldCount = rowType.getFieldCount();
+    final RelNode input = calc.getInput();
+
+    final Set<RelDataTypeField> inputExtraFields =
+        new LinkedHashSet<>(extraFields);
+    RelOptUtil.InputFinder inputFinder =
+        new RelOptUtil.InputFinder(inputExtraFields);
+    for (Ord<RexNode> ord : Ord.zip(projs)) {
+      if (fieldsUsed.get(ord.i)) {
+        ord.e.accept(inputFinder);
+      }
+    }
+    ImmutableBitSet inputFieldsUsed = inputFinder.inputBitSet.build();
+
+    // Create input with trimmed columns.
+    TrimResult trimResult =
+        trimChild(calc, input, inputFieldsUsed, inputExtraFields);
+    RelNode newInput = trimResult.left;
+    final Mapping inputMapping = trimResult.right;
+
+    // If the input is unchanged, and we need to project all columns,
+    // there's nothing we can do.
+    if (newInput == input
+        && fieldsUsed.cardinality() == fieldCount) {
+      return result(calc, Mappings.createIdentity(fieldCount));
+    }
+
+    // Some parts of the system can't handle rows with zero fields, so
+    // pretend that one field is used.
+    if (fieldsUsed.cardinality() == 0) {
+      return dummyProject(fieldCount, newInput);
+    }
+
+    // Build new project expressions, and populate the mapping.
+    final List<RexNode> newProjects = new ArrayList<>();
+    final RexVisitor<RexNode> shuttle =
+        new RexPermuteInputsShuttle(
+            inputMapping, newInput);
+    final Mapping mapping =
+        Mappings.create(
+            MappingType.INVERSE_SURJECTION,
+            fieldCount,
+            fieldsUsed.cardinality());
+    for (Ord<RexNode> ord : Ord.zip(projs)) {
+      if (fieldsUsed.get(ord.i)) {
+        mapping.set(ord.i, newProjects.size());
+        RexNode newProjectExpr = ord.e.accept(shuttle);
+        newProjects.add(newProjectExpr);
+      }
+    }
+
+    final RelDataType newRowType =
+        RelOptUtil.permute(calc.getCluster().getTypeFactory(), rowType,
+            mapping);
+
+    final RelNode newInputRelNode = relBuilder.push(newInput).build();
+    if (rexProgram.getCondition() != null) {
+      final List<RexNode> filter = Lists.transform(
+          ImmutableList.of(
+          rexProgram.getCondition()), rexProgram::expandLocalRef);
+      assert filter.size() == 1;
+      final RexNode conditionExpr = filter.get(0);
+
+      final RexNode newConditionExpr =
+          conditionExpr.accept(shuttle);
+      final RexProgram newRexProgram = RexProgram.create(newInputRelNode.getRowType(),
+          newProjects, newConditionExpr, newRowType.getFieldNames(),
+          newInputRelNode.getCluster().getRexBuilder());
+      LogicalCalc logicalCalc = LogicalCalc.create(newInputRelNode, newRexProgram);
+
+      return result(logicalCalc, mapping);
+    } else {
+      final RexProgram newRexProgram = RexProgram
+          .create(newInputRelNode.getRowType(), newProjects, null,
+              newRowType.getFieldNames(), newInputRelNode.getCluster().getRexBuilder());
+
+      LogicalCalc logicalCalc = LogicalCalc.create(newInputRelNode, newRexProgram);
+      return result(logicalCalc, mapping);

Review comment:
       There is still duplicated code in the if + else block, I think the refactoring I proposed in my comment above would make the code cleaner.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] xy2953396112 commented on a change in pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
xy2953396112 commented on a change in pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#discussion_r439955731



##########
File path: core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java
##########
@@ -350,6 +354,92 @@ public TrimResult trimFields(
         Mappings.createIdentity(rel.getRowType().getFieldCount()));
   }
 
+  /**
+   * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
+   * {@link org.apache.calcite.rel.logical.LogicalCalc}.
+   */
+  public TrimResult trimFields(
+      Calc calc,
+      ImmutableBitSet fieldsUsed,
+      Set<RelDataTypeField> extraFields) {
+    final RexProgram rexProgram = calc.getProgram();
+    final List<RexNode> projs = Lists.transform(rexProgram.getProjectList(),
+        rexProgram::expandLocalRef);
+
+    final RelDataType rowType = calc.getRowType();
+    final int fieldCount = rowType.getFieldCount();
+    final RelNode input = calc.getInput();
+
+    final Set<RelDataTypeField> inputExtraFields =
+        new LinkedHashSet<>(extraFields);
+    RelOptUtil.InputFinder inputFinder =
+        new RelOptUtil.InputFinder(inputExtraFields);
+    for (Ord<RexNode> ord : Ord.zip(projs)) {
+      if (fieldsUsed.get(ord.i)) {
+        ord.e.accept(inputFinder);
+      }
+    }
+    ImmutableBitSet inputFieldsUsed = inputFinder.build();
+
+    // Create input with trimmed columns.
+    TrimResult trimResult =
+        trimChild(calc, input, inputFieldsUsed, inputExtraFields);
+    RelNode newInput = trimResult.left;
+    final Mapping inputMapping = trimResult.right;
+
+    // If the input is unchanged, and we need to project all columns,
+    // there's nothing we can do.
+    if (newInput == input
+        && fieldsUsed.cardinality() == fieldCount) {
+      return result(calc, Mappings.createIdentity(fieldCount));
+    }
+
+    // Some parts of the system can't handle rows with zero fields, so
+    // pretend that one field is used.
+    if (fieldsUsed.cardinality() == 0) {
+      return dummyProject(fieldCount, newInput);
+    }
+
+    // Build new project expressions, and populate the mapping.
+    final List<RexNode> newProjects = new ArrayList<>();
+    final RexVisitor<RexNode> shuttle =
+        new RexPermuteInputsShuttle(
+            inputMapping, newInput);
+    final Mapping mapping =
+        Mappings.create(
+            MappingType.INVERSE_SURJECTION,
+            fieldCount,
+            fieldsUsed.cardinality());
+    for (Ord<RexNode> ord : Ord.zip(projs)) {
+      if (fieldsUsed.get(ord.i)) {
+        mapping.set(ord.i, newProjects.size());
+        RexNode newProjectExpr = ord.e.accept(shuttle);
+        newProjects.add(newProjectExpr);
+      }
+    }
+
+    final RelDataType newRowType =
+        RelOptUtil.permute(calc.getCluster().getTypeFactory(), rowType,
+            mapping);
+
+    final RelNode newInputRelNode = relBuilder.push(newInput).build();
+    RexNode newConditionExpr = null;
+    if (rexProgram.getCondition() != null) {
+      final List<RexNode> filter = Lists.transform(
+          ImmutableList.of(
+              rexProgram.getCondition()), rexProgram::expandLocalRef);
+      assert filter.size() == 1;
+      final RexNode conditionExpr = filter.get(0);
+      newConditionExpr = conditionExpr.accept(shuttle);
+    }
+    final RexProgram newRexProgram = RexProgram.create(newInputRelNode.getRowType(),
+        newProjects, newConditionExpr, newRowType.getFieldNames(),
+        newInputRelNode.getCluster().getRexBuilder());
+    final LogicalCalc logicalCalc = LogicalCalc.create(newInputRelNode, newRexProgram);
+    // transfer hints
+    return result(logicalCalc.withHints(calc.getHints()), mapping);
+  }

Review comment:
       `Calc#copy` is better.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] xy2953396112 commented on a change in pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
xy2953396112 commented on a change in pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#discussion_r439955628



##########
File path: core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java
##########
@@ -350,6 +354,92 @@ public TrimResult trimFields(
         Mappings.createIdentity(rel.getRowType().getFieldCount()));
   }
 
+  /**
+   * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
+   * {@link org.apache.calcite.rel.logical.LogicalCalc}.
+   */
+  public TrimResult trimFields(
+      Calc calc,
+      ImmutableBitSet fieldsUsed,
+      Set<RelDataTypeField> extraFields) {
+    final RexProgram rexProgram = calc.getProgram();
+    final List<RexNode> projs = Lists.transform(rexProgram.getProjectList(),
+        rexProgram::expandLocalRef);
+
+    final RelDataType rowType = calc.getRowType();
+    final int fieldCount = rowType.getFieldCount();
+    final RelNode input = calc.getInput();
+
+    final Set<RelDataTypeField> inputExtraFields =
+        new LinkedHashSet<>(extraFields);
+    RelOptUtil.InputFinder inputFinder =

Review comment:
       ok




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] chunweilei commented on pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
chunweilei commented on pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#issuecomment-641713202


   > @chunweilei Help review this pr when you have time, thanks a lot. Friendly ping ~
   
   Would review this week~~


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] xy2953396112 commented on a change in pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
xy2953396112 commented on a change in pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#discussion_r439726564



##########
File path: core/src/test/java/org/apache/calcite/sql2rel/RelFieldTrimmerTest.java
##########
@@ -306,4 +314,139 @@
     assertTrue(project.getHints().contains(projectHint));
   }
 
+  @Test void testCalcFieldTrimmer0() {
+    final RelBuilder builder = RelBuilder.create(config().build());
+    final RelNode root =
+        builder.scan("EMP")
+            .project(builder.field("EMPNO"), builder.field("ENAME"), builder.field("DEPTNO"))
+            .exchange(RelDistributions.SINGLETON)
+            .project(builder.field("EMPNO"), builder.field("ENAME"))
+            .build();
+
+    final HepProgram hepProgram = new HepProgramBuilder().
+        addRuleInstance(ProjectToCalcRule.INSTANCE).build();
+
+    final HepPlanner hepPlanner = new HepPlanner(hepProgram);
+    hepPlanner.setRoot(root);
+    final RelNode relNode = hepPlanner.findBestExp();
+    final RelFieldTrimmer fieldTrimmer = new RelFieldTrimmer(null, builder);
+    final RelNode trimmed = fieldTrimmer.trim(relNode);
+
+    final String expected = ""
+        + "LogicalCalc(expr#0..1=[{inputs}], proj#0..1=[{exprs}])\n"
+        + "  LogicalExchange(distribution=[single])\n"
+        + "    LogicalCalc(expr#0..1=[{inputs}], proj#0..1=[{exprs}])\n"
+        + "      LogicalProject(EMPNO=[$0], ENAME=[$1])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    assertThat(trimmed, hasTree(expected));
+  }
+
+  @Test void testCalcFieldTrimmer1() {
+    final RelBuilder builder = RelBuilder.create(config().build());
+    final RelNode root =
+        builder.scan("EMP")
+            .project(builder.field("EMPNO"), builder.field("ENAME"), builder.field("DEPTNO"))
+            .exchange(RelDistributions.SINGLETON)
+            .filter(
+                builder.call(SqlStdOperatorTable.GREATER_THAN,
+                    builder.field("EMPNO"), builder.literal(100)))
+            .build();
+
+    final HepProgram hepProgram = new HepProgramBuilder()
+        .addRuleInstance(ProjectToCalcRule.INSTANCE)
+        .addRuleInstance(FilterToCalcRule.INSTANCE)
+        .build();
+
+    final HepPlanner hepPlanner = new HepPlanner(hepProgram);
+    hepPlanner.setRoot(root);
+    final RelNode relNode = hepPlanner.findBestExp();
+    final RelFieldTrimmer fieldTrimmer = new RelFieldTrimmer(null, builder);
+    final RelNode trimmed = fieldTrimmer.trim(relNode);
+
+    final String expected = ""
+        + "LogicalCalc(expr#0..2=[{inputs}], expr#3=[100], expr#4=[>($t0, $t3)], proj#0."
+        + ".2=[{exprs}], $condition=[$t4])\n"
+        + "  LogicalExchange(distribution=[single])\n"
+        + "    LogicalCalc(expr#0..2=[{inputs}], proj#0..2=[{exprs}])\n"
+        + "      LogicalProject(EMPNO=[$0], ENAME=[$1], DEPTNO=[$7])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    assertThat(trimmed, hasTree(expected));
+  }
+
+  @Test void testCalcFieldTrimmer2() {
+    final RelBuilder builder = RelBuilder.create(config().build());
+    final RelNode root =
+        builder.scan("EMP")
+            .project(builder.field("EMPNO"), builder.field("ENAME"), builder.field("DEPTNO"))
+            .exchange(RelDistributions.SINGLETON)
+            .filter(
+                builder.call(SqlStdOperatorTable.GREATER_THAN,
+                    builder.field("EMPNO"), builder.literal(100)))
+            .project(builder.field("EMPNO"), builder.field("ENAME"))
+            .build();
+
+    final HepProgram hepProgram = new HepProgramBuilder()
+        .addRuleInstance(ProjectToCalcRule.INSTANCE)
+        .addRuleInstance(FilterToCalcRule.INSTANCE)
+        .addRuleInstance(CalcMergeRule.INSTANCE).build();
+
+    final HepPlanner hepPlanner = new HepPlanner(hepProgram);
+    hepPlanner.setRoot(root);
+    final RelNode relNode = hepPlanner.findBestExp();
+    final RelFieldTrimmer fieldTrimmer = new RelFieldTrimmer(null, builder);
+    final RelNode trimmed = fieldTrimmer.trim(relNode);
+
+    final String expected = ""
+        + "LogicalCalc(expr#0..1=[{inputs}], expr#2=[100], expr#3=[>($t0, $t2)], proj#0."
+        + ".1=[{exprs}], $condition=[$t3])\n"
+        + "  LogicalExchange(distribution=[single])\n"
+        + "    LogicalCalc(expr#0..1=[{inputs}], proj#0..1=[{exprs}])\n"
+        + "      LogicalProject(EMPNO=[$0], ENAME=[$1])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    assertThat(trimmed, hasTree(expected));
+  }
+
+  @Test void testCalcWithHints() {
+    final RelHint calcHint = RelHint.builder("resource").build();
+    final RelBuilder builder = RelBuilder.create(config().build());
+    builder.getCluster().setHintStrategies(
+        HintStrategyTable.builder().hintStrategy("resource", HintPredicates.CALC).build());
+    final RelNode original =
+        builder.scan("EMP")
+            .project(
+                builder.field("EMPNO"),
+                builder.field("ENAME"),
+                builder.field("DEPTNO")
+            ).hints(calcHint)
+            .sort(builder.field("EMPNO"))
+            .project(builder.field("EMPNO"))
+            .build();
+
+    final HepProgram hepProgram = new HepProgramBuilder()
+        .addRuleInstance(ProjectToCalcRule.INSTANCE)
+        .build();
+    final HepPlanner hepPlanner = new HepPlanner(hepProgram);
+    hepPlanner.setRoot(original);
+    final RelNode relNode = hepPlanner.findBestExp();
+
+    final RelFieldTrimmer fieldTrimmer = new RelFieldTrimmer(null, builder);
+    final RelNode trimmed = fieldTrimmer.trim(relNode);
+
+    final String expected = ""
+        + "LogicalCalc(expr#0=[{inputs}], EMPNO=[$t0])\n"
+        + "  LogicalSort(sort0=[$0], dir0=[ASC])\n"
+        + "    LogicalCalc(expr#0=[{inputs}], EMPNO=[$t0])\n"
+        + "      LogicalProject(EMPNO=[$0])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    assertThat(trimmed, hasTree(expected));
+
+    assertTrue(original.getInput(0).getInput(0) instanceof Project);

Review comment:
       Thanks, Add the verification logic of the original Calc contains the hints.
   
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] xy2953396112 commented on pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
xy2953396112 commented on pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#issuecomment-641697159


   @chunweilei  Help review this pr when you have time, thanks a lot. Friendly ping ~


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] rubenada commented on pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
rubenada commented on pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#issuecomment-643116159


   @xy2953396112 Please bear in mind that [CALCITE-4055 - RelFieldTrimmer loses hints](https://issues.apache.org/jira/browse/CALCITE-4055) ([PR#2015](https://github.com/apache/calcite/pull/2015)) has been merged, which leads to some conflicts in the current PR. Sorry for the inconvenience.
   Also, since you are adding the processing of Calc, which implements Hintable, you should take into consideration the guidelines from CALCITE-4055 in order to transfer the hints from the original Calc to the trimmed Calc.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] xy2953396112 commented on a change in pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
xy2953396112 commented on a change in pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#discussion_r430136341



##########
File path: core/src/test/java/org/apache/calcite/sql2rel/RelFieldTrimmerTest.java
##########
@@ -183,4 +190,96 @@
     assertThat(trimmed, hasTree(expected));
   }
 
+  @Test public void testCalcFieldTrimmer0() {
+    final RelBuilder builder = RelBuilder.create(config().build());
+    final RelNode root =
+        builder.scan("EMP")
+            .project(builder.field("EMPNO"), builder.field("ENAME"), builder.field("DEPTNO"))
+            .exchange(RelDistributions.SINGLETON)
+            .project(builder.field("EMPNO"), builder.field("ENAME"))
+            .build();
+

Review comment:
       `testCalcFieldTrimmer0` is used for check Project operator.
   `testCalcFieldTrimmer1` is used for check Filter operator.
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] chunweilei merged pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
chunweilei merged pull request #1987:
URL: https://github.com/apache/calcite/pull/1987


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] chunweilei commented on a change in pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
chunweilei commented on a change in pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#discussion_r430128039



##########
File path: core/src/test/java/org/apache/calcite/sql2rel/RelFieldTrimmerTest.java
##########
@@ -183,4 +190,96 @@
     assertThat(trimmed, hasTree(expected));
   }
 
+  @Test public void testCalcFieldTrimmer0() {
+    final RelBuilder builder = RelBuilder.create(config().build());
+    final RelNode root =
+        builder.scan("EMP")
+            .project(builder.field("EMPNO"), builder.field("ENAME"), builder.field("DEPTNO"))
+            .exchange(RelDistributions.SINGLETON)
+            .project(builder.field("EMPNO"), builder.field("ENAME"))
+            .build();
+

Review comment:
       What's the difference between `testCalcFieldTrimmer0 ` and `testCalcFieldTrimmer1`?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] rubenada commented on a change in pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
rubenada commented on a change in pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#discussion_r439724299



##########
File path: core/src/test/java/org/apache/calcite/sql2rel/RelFieldTrimmerTest.java
##########
@@ -306,4 +314,139 @@
     assertTrue(project.getHints().contains(projectHint));
   }
 
+  @Test void testCalcFieldTrimmer0() {
+    final RelBuilder builder = RelBuilder.create(config().build());
+    final RelNode root =
+        builder.scan("EMP")
+            .project(builder.field("EMPNO"), builder.field("ENAME"), builder.field("DEPTNO"))
+            .exchange(RelDistributions.SINGLETON)
+            .project(builder.field("EMPNO"), builder.field("ENAME"))
+            .build();
+
+    final HepProgram hepProgram = new HepProgramBuilder().
+        addRuleInstance(ProjectToCalcRule.INSTANCE).build();
+
+    final HepPlanner hepPlanner = new HepPlanner(hepProgram);
+    hepPlanner.setRoot(root);
+    final RelNode relNode = hepPlanner.findBestExp();
+    final RelFieldTrimmer fieldTrimmer = new RelFieldTrimmer(null, builder);
+    final RelNode trimmed = fieldTrimmer.trim(relNode);
+
+    final String expected = ""
+        + "LogicalCalc(expr#0..1=[{inputs}], proj#0..1=[{exprs}])\n"
+        + "  LogicalExchange(distribution=[single])\n"
+        + "    LogicalCalc(expr#0..1=[{inputs}], proj#0..1=[{exprs}])\n"
+        + "      LogicalProject(EMPNO=[$0], ENAME=[$1])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    assertThat(trimmed, hasTree(expected));
+  }
+
+  @Test void testCalcFieldTrimmer1() {
+    final RelBuilder builder = RelBuilder.create(config().build());
+    final RelNode root =
+        builder.scan("EMP")
+            .project(builder.field("EMPNO"), builder.field("ENAME"), builder.field("DEPTNO"))
+            .exchange(RelDistributions.SINGLETON)
+            .filter(
+                builder.call(SqlStdOperatorTable.GREATER_THAN,
+                    builder.field("EMPNO"), builder.literal(100)))
+            .build();
+
+    final HepProgram hepProgram = new HepProgramBuilder()
+        .addRuleInstance(ProjectToCalcRule.INSTANCE)
+        .addRuleInstance(FilterToCalcRule.INSTANCE)
+        .build();
+
+    final HepPlanner hepPlanner = new HepPlanner(hepProgram);
+    hepPlanner.setRoot(root);
+    final RelNode relNode = hepPlanner.findBestExp();
+    final RelFieldTrimmer fieldTrimmer = new RelFieldTrimmer(null, builder);
+    final RelNode trimmed = fieldTrimmer.trim(relNode);
+
+    final String expected = ""
+        + "LogicalCalc(expr#0..2=[{inputs}], expr#3=[100], expr#4=[>($t0, $t3)], proj#0."
+        + ".2=[{exprs}], $condition=[$t4])\n"
+        + "  LogicalExchange(distribution=[single])\n"
+        + "    LogicalCalc(expr#0..2=[{inputs}], proj#0..2=[{exprs}])\n"
+        + "      LogicalProject(EMPNO=[$0], ENAME=[$1], DEPTNO=[$7])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    assertThat(trimmed, hasTree(expected));
+  }
+
+  @Test void testCalcFieldTrimmer2() {
+    final RelBuilder builder = RelBuilder.create(config().build());
+    final RelNode root =
+        builder.scan("EMP")
+            .project(builder.field("EMPNO"), builder.field("ENAME"), builder.field("DEPTNO"))
+            .exchange(RelDistributions.SINGLETON)
+            .filter(
+                builder.call(SqlStdOperatorTable.GREATER_THAN,
+                    builder.field("EMPNO"), builder.literal(100)))
+            .project(builder.field("EMPNO"), builder.field("ENAME"))
+            .build();
+
+    final HepProgram hepProgram = new HepProgramBuilder()
+        .addRuleInstance(ProjectToCalcRule.INSTANCE)
+        .addRuleInstance(FilterToCalcRule.INSTANCE)
+        .addRuleInstance(CalcMergeRule.INSTANCE).build();
+
+    final HepPlanner hepPlanner = new HepPlanner(hepProgram);
+    hepPlanner.setRoot(root);
+    final RelNode relNode = hepPlanner.findBestExp();
+    final RelFieldTrimmer fieldTrimmer = new RelFieldTrimmer(null, builder);
+    final RelNode trimmed = fieldTrimmer.trim(relNode);
+
+    final String expected = ""
+        + "LogicalCalc(expr#0..1=[{inputs}], expr#2=[100], expr#3=[>($t0, $t2)], proj#0."
+        + ".1=[{exprs}], $condition=[$t3])\n"
+        + "  LogicalExchange(distribution=[single])\n"
+        + "    LogicalCalc(expr#0..1=[{inputs}], proj#0..1=[{exprs}])\n"
+        + "      LogicalProject(EMPNO=[$0], ENAME=[$1])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    assertThat(trimmed, hasTree(expected));
+  }
+
+  @Test void testCalcWithHints() {
+    final RelHint calcHint = RelHint.builder("resource").build();
+    final RelBuilder builder = RelBuilder.create(config().build());
+    builder.getCluster().setHintStrategies(
+        HintStrategyTable.builder().hintStrategy("resource", HintPredicates.CALC).build());
+    final RelNode original =
+        builder.scan("EMP")
+            .project(
+                builder.field("EMPNO"),
+                builder.field("ENAME"),
+                builder.field("DEPTNO")
+            ).hints(calcHint)
+            .sort(builder.field("EMPNO"))
+            .project(builder.field("EMPNO"))
+            .build();
+
+    final HepProgram hepProgram = new HepProgramBuilder()
+        .addRuleInstance(ProjectToCalcRule.INSTANCE)
+        .build();
+    final HepPlanner hepPlanner = new HepPlanner(hepProgram);
+    hepPlanner.setRoot(original);
+    final RelNode relNode = hepPlanner.findBestExp();
+
+    final RelFieldTrimmer fieldTrimmer = new RelFieldTrimmer(null, builder);
+    final RelNode trimmed = fieldTrimmer.trim(relNode);
+
+    final String expected = ""
+        + "LogicalCalc(expr#0=[{inputs}], EMPNO=[$t0])\n"
+        + "  LogicalSort(sort0=[$0], dir0=[ASC])\n"
+        + "    LogicalCalc(expr#0=[{inputs}], EMPNO=[$t0])\n"
+        + "      LogicalProject(EMPNO=[$0])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    assertThat(trimmed, hasTree(expected));
+
+    assertTrue(original.getInput(0).getInput(0) instanceof Project);

Review comment:
       minor: I think in this test it would also make sense to verify that the original Calc (`relNode.getInput(0).getInput(0)`) contains the hints.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] chunweilei commented on a change in pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
chunweilei commented on a change in pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#discussion_r439907127



##########
File path: core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java
##########
@@ -350,6 +354,92 @@ public TrimResult trimFields(
         Mappings.createIdentity(rel.getRowType().getFieldCount()));
   }
 
+  /**
+   * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
+   * {@link org.apache.calcite.rel.logical.LogicalCalc}.
+   */
+  public TrimResult trimFields(
+      Calc calc,
+      ImmutableBitSet fieldsUsed,
+      Set<RelDataTypeField> extraFields) {
+    final RexProgram rexProgram = calc.getProgram();
+    final List<RexNode> projs = Lists.transform(rexProgram.getProjectList(),
+        rexProgram::expandLocalRef);
+
+    final RelDataType rowType = calc.getRowType();
+    final int fieldCount = rowType.getFieldCount();
+    final RelNode input = calc.getInput();
+
+    final Set<RelDataTypeField> inputExtraFields =
+        new LinkedHashSet<>(extraFields);
+    RelOptUtil.InputFinder inputFinder =

Review comment:
       Do we have to use `LinkedHashSet `? Looks like `HashSet` is enough.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] xy2953396112 commented on pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
xy2953396112 commented on pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#issuecomment-643595465


   @rubenada 
   Thank you for your reminder, I have updated the code. 
   Please help review this pr when you have time, thanks a lot.
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] rubenada commented on a change in pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
rubenada commented on a change in pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#discussion_r439258169



##########
File path: core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java
##########
@@ -350,6 +354,100 @@ public TrimResult trimFields(
         Mappings.createIdentity(rel.getRowType().getFieldCount()));
   }
 
+  /**
+   * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
+   * {@link org.apache.calcite.rel.logical.LogicalCalc}.
+   */
+  public TrimResult trimFields(
+      Calc calc,
+      ImmutableBitSet fieldsUsed,
+      Set<RelDataTypeField> extraFields) {
+    final RexProgram rexProgram = calc.getProgram();
+    final List<RexNode> projs = Lists.transform(rexProgram.getProjectList(),
+        rexProgram::expandLocalRef);
+
+    final RelDataType rowType = calc.getRowType();
+    final int fieldCount = rowType.getFieldCount();
+    final RelNode input = calc.getInput();
+
+    final Set<RelDataTypeField> inputExtraFields =
+        new LinkedHashSet<>(extraFields);
+    RelOptUtil.InputFinder inputFinder =
+        new RelOptUtil.InputFinder(inputExtraFields);
+    for (Ord<RexNode> ord : Ord.zip(projs)) {
+      if (fieldsUsed.get(ord.i)) {
+        ord.e.accept(inputFinder);
+      }
+    }
+    ImmutableBitSet inputFieldsUsed = inputFinder.inputBitSet.build();
+
+    // Create input with trimmed columns.
+    TrimResult trimResult =
+        trimChild(calc, input, inputFieldsUsed, inputExtraFields);
+    RelNode newInput = trimResult.left;
+    final Mapping inputMapping = trimResult.right;
+
+    // If the input is unchanged, and we need to project all columns,
+    // there's nothing we can do.
+    if (newInput == input
+        && fieldsUsed.cardinality() == fieldCount) {
+      return result(calc, Mappings.createIdentity(fieldCount));
+    }
+
+    // Some parts of the system can't handle rows with zero fields, so
+    // pretend that one field is used.
+    if (fieldsUsed.cardinality() == 0) {
+      return dummyProject(fieldCount, newInput);

Review comment:
       I am not sure if, in this situation, the hints from the original Calc must / can be transferred into the new "dummy Project". What do you think @danny0405 ?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] xy2953396112 commented on a change in pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
xy2953396112 commented on a change in pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#discussion_r439726414



##########
File path: core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java
##########
@@ -350,6 +354,100 @@ public TrimResult trimFields(
         Mappings.createIdentity(rel.getRowType().getFieldCount()));
   }
 
+  /**
+   * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
+   * {@link org.apache.calcite.rel.logical.LogicalCalc}.
+   */
+  public TrimResult trimFields(
+      Calc calc,
+      ImmutableBitSet fieldsUsed,
+      Set<RelDataTypeField> extraFields) {
+    final RexProgram rexProgram = calc.getProgram();
+    final List<RexNode> projs = Lists.transform(rexProgram.getProjectList(),
+        rexProgram::expandLocalRef);
+
+    final RelDataType rowType = calc.getRowType();
+    final int fieldCount = rowType.getFieldCount();
+    final RelNode input = calc.getInput();
+
+    final Set<RelDataTypeField> inputExtraFields =
+        new LinkedHashSet<>(extraFields);
+    RelOptUtil.InputFinder inputFinder =
+        new RelOptUtil.InputFinder(inputExtraFields);
+    for (Ord<RexNode> ord : Ord.zip(projs)) {
+      if (fieldsUsed.get(ord.i)) {
+        ord.e.accept(inputFinder);
+      }
+    }
+    ImmutableBitSet inputFieldsUsed = inputFinder.inputBitSet.build();
+
+    // Create input with trimmed columns.
+    TrimResult trimResult =
+        trimChild(calc, input, inputFieldsUsed, inputExtraFields);
+    RelNode newInput = trimResult.left;
+    final Mapping inputMapping = trimResult.right;
+
+    // If the input is unchanged, and we need to project all columns,
+    // there's nothing we can do.
+    if (newInput == input
+        && fieldsUsed.cardinality() == fieldCount) {
+      return result(calc, Mappings.createIdentity(fieldCount));
+    }
+
+    // Some parts of the system can't handle rows with zero fields, so
+    // pretend that one field is used.
+    if (fieldsUsed.cardinality() == 0) {
+      return dummyProject(fieldCount, newInput);
+    }
+
+    // Build new project expressions, and populate the mapping.
+    final List<RexNode> newProjects = new ArrayList<>();
+    final RexVisitor<RexNode> shuttle =
+        new RexPermuteInputsShuttle(
+            inputMapping, newInput);
+    final Mapping mapping =
+        Mappings.create(
+            MappingType.INVERSE_SURJECTION,
+            fieldCount,
+            fieldsUsed.cardinality());
+    for (Ord<RexNode> ord : Ord.zip(projs)) {
+      if (fieldsUsed.get(ord.i)) {
+        mapping.set(ord.i, newProjects.size());
+        RexNode newProjectExpr = ord.e.accept(shuttle);
+        newProjects.add(newProjectExpr);
+      }
+    }
+
+    final RelDataType newRowType =
+        RelOptUtil.permute(calc.getCluster().getTypeFactory(), rowType,
+            mapping);
+
+    final RelNode newInputRelNode = relBuilder.push(newInput).build();
+    if (rexProgram.getCondition() != null) {
+      final List<RexNode> filter = Lists.transform(
+          ImmutableList.of(
+          rexProgram.getCondition()), rexProgram::expandLocalRef);
+      assert filter.size() == 1;
+      final RexNode conditionExpr = filter.get(0);
+
+      final RexNode newConditionExpr =
+          conditionExpr.accept(shuttle);
+      final RexProgram newRexProgram = RexProgram.create(newInputRelNode.getRowType(),
+          newProjects, newConditionExpr, newRowType.getFieldNames(),
+          newInputRelNode.getCluster().getRexBuilder());
+      LogicalCalc logicalCalc = LogicalCalc.create(newInputRelNode, newRexProgram);
+
+      return result(logicalCalc, mapping);
+    } else {
+      final RexProgram newRexProgram = RexProgram
+          .create(newInputRelNode.getRowType(), newProjects, null,
+              newRowType.getFieldNames(), newInputRelNode.getCluster().getRexBuilder());
+
+      LogicalCalc logicalCalc = LogicalCalc.create(newInputRelNode, newRexProgram);
+      return result(logicalCalc, mapping);

Review comment:
       Thanks, clean up the code.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] chunweilei commented on a change in pull request #1987: [CALCITE-4020] Support Calc operator in RelFieldTrimmer

Posted by GitBox <gi...@apache.org>.
chunweilei commented on a change in pull request #1987:
URL: https://github.com/apache/calcite/pull/1987#discussion_r439908697



##########
File path: core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java
##########
@@ -350,6 +354,92 @@ public TrimResult trimFields(
         Mappings.createIdentity(rel.getRowType().getFieldCount()));
   }
 
+  /**
+   * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
+   * {@link org.apache.calcite.rel.logical.LogicalCalc}.
+   */
+  public TrimResult trimFields(
+      Calc calc,
+      ImmutableBitSet fieldsUsed,
+      Set<RelDataTypeField> extraFields) {
+    final RexProgram rexProgram = calc.getProgram();
+    final List<RexNode> projs = Lists.transform(rexProgram.getProjectList(),
+        rexProgram::expandLocalRef);
+
+    final RelDataType rowType = calc.getRowType();
+    final int fieldCount = rowType.getFieldCount();
+    final RelNode input = calc.getInput();
+
+    final Set<RelDataTypeField> inputExtraFields =
+        new LinkedHashSet<>(extraFields);
+    RelOptUtil.InputFinder inputFinder =
+        new RelOptUtil.InputFinder(inputExtraFields);
+    for (Ord<RexNode> ord : Ord.zip(projs)) {
+      if (fieldsUsed.get(ord.i)) {
+        ord.e.accept(inputFinder);
+      }
+    }
+    ImmutableBitSet inputFieldsUsed = inputFinder.build();
+
+    // Create input with trimmed columns.
+    TrimResult trimResult =
+        trimChild(calc, input, inputFieldsUsed, inputExtraFields);
+    RelNode newInput = trimResult.left;
+    final Mapping inputMapping = trimResult.right;
+
+    // If the input is unchanged, and we need to project all columns,
+    // there's nothing we can do.
+    if (newInput == input
+        && fieldsUsed.cardinality() == fieldCount) {
+      return result(calc, Mappings.createIdentity(fieldCount));
+    }
+
+    // Some parts of the system can't handle rows with zero fields, so
+    // pretend that one field is used.
+    if (fieldsUsed.cardinality() == 0) {
+      return dummyProject(fieldCount, newInput);
+    }
+
+    // Build new project expressions, and populate the mapping.
+    final List<RexNode> newProjects = new ArrayList<>();
+    final RexVisitor<RexNode> shuttle =
+        new RexPermuteInputsShuttle(
+            inputMapping, newInput);
+    final Mapping mapping =
+        Mappings.create(
+            MappingType.INVERSE_SURJECTION,
+            fieldCount,
+            fieldsUsed.cardinality());
+    for (Ord<RexNode> ord : Ord.zip(projs)) {
+      if (fieldsUsed.get(ord.i)) {
+        mapping.set(ord.i, newProjects.size());
+        RexNode newProjectExpr = ord.e.accept(shuttle);
+        newProjects.add(newProjectExpr);
+      }
+    }
+
+    final RelDataType newRowType =
+        RelOptUtil.permute(calc.getCluster().getTypeFactory(), rowType,
+            mapping);
+
+    final RelNode newInputRelNode = relBuilder.push(newInput).build();
+    RexNode newConditionExpr = null;
+    if (rexProgram.getCondition() != null) {
+      final List<RexNode> filter = Lists.transform(
+          ImmutableList.of(
+              rexProgram.getCondition()), rexProgram::expandLocalRef);
+      assert filter.size() == 1;
+      final RexNode conditionExpr = filter.get(0);
+      newConditionExpr = conditionExpr.accept(shuttle);
+    }
+    final RexProgram newRexProgram = RexProgram.create(newInputRelNode.getRowType(),
+        newProjects, newConditionExpr, newRowType.getFieldNames(),
+        newInputRelNode.getCluster().getRexBuilder());
+    final LogicalCalc logicalCalc = LogicalCalc.create(newInputRelNode, newRexProgram);
+    // transfer hints
+    return result(logicalCalc.withHints(calc.getHints()), mapping);
+  }

Review comment:
       Should we use `Calc#copy` to generate new Calc?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org