hsyuan commented on a change in pull request #1500: [CALCITE-3405] Prune 
columns for ProjectableFilterableTable when Proj…
URL: https://github.com/apache/calcite/pull/1500#discussion_r341911689
 
 

 ##########
 File path: 
core/src/main/java/org/apache/calcite/rel/rules/ProjectTableScanRule.java
 ##########
 @@ -103,30 +107,48 @@ protected static boolean test(TableScan scan) {
   protected void apply(RelOptRuleCall call, Project project, TableScan scan) {
     final RelOptTable table = scan.getTable();
     assert table.unwrap(ProjectableFilterableTable.class) != null;
-    if (!project.isMapping()) {
-      return;
-    }
-    final Mappings.TargetMapping mapping = Project.getPartialMapping(
-            project.getInput().getRowType().getFieldCount(),
-            project.getProjects());
 
-    final ImmutableIntList projects;
-    final ImmutableList<RexNode> filters;
+    final List<Integer> selectedColumns = new ArrayList<>();
+    project.getProjects().forEach(proj -> {
+      proj.accept(new RexVisitorImpl<Void>(true) {
+        public Void visitInputRef(RexInputRef inputRef) {
+          if (!selectedColumns.contains(inputRef.getIndex())) {
+            selectedColumns.add(inputRef.getIndex());
+          }
+          return null;
+        }
+      });
+    });
+
+    final List<RexNode> filtersPushDown;
+    final List<Integer> projectsPushDown;
     if (scan instanceof Bindables.BindableTableScan) {
       final Bindables.BindableTableScan bindableScan =
           (Bindables.BindableTableScan) scan;
-      filters = bindableScan.filters;
-      projects = bindableScan.projects;
+      filtersPushDown = bindableScan.filters;
+      projectsPushDown = selectedColumns.stream()
+          .map(col -> bindableScan.projects.get(col))
+          .collect(Collectors.toList());
     } else {
-      filters = ImmutableList.of();
-      projects = scan.identity();
+      filtersPushDown = ImmutableList.of();
+      projectsPushDown = selectedColumns;
     }
+    Bindables.BindableTableScan newScan = Bindables.BindableTableScan.create(
+        scan.getCluster(), scan.getTable(), filtersPushDown, projectsPushDown);
+    Mapping mapping =
+        Mappings.target(selectedColumns, scan.getRowType().getFieldCount());
+    final List<RexNode> newProjectRexNodes =
+        ImmutableList.copyOf(RexUtil.apply(mapping, project.getProjects()));
 
 Review comment:
   Remove `ImmutableList.copyOf`

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to