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 2019/10/15 03:45:41 UTC

[GitHub] [calcite] jinxing64 commented on a change in pull request #1500: [CALCITE-3405] Prune columns for ProjectableFilterableTable when Proj…

jinxing64 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_r334741507
 
 

 ##########
 File path: core/src/main/java/org/apache/calcite/rel/rules/ProjectTableScanRule.java
 ##########
 @@ -103,30 +106,47 @@ 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);
+    final List<RexNode> newProjectRexNodes = new RexShuttle() {
+      @Override public RexNode visitInputRef(RexInputRef inputRef) {
+        int newIdx = selectedColumns.indexOf(inputRef.getIndex());
+        assert newIdx != -1;
+        return new RexInputRef(newIdx, inputRef.getType());
+      }
+    }.apply(project.getProjects());
 
 Review comment:
   Thanks ~ that would be 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


With regards,
Apache Git Services