{"action":"created","comment":{"url":"https://api.github.com/repos/apache/incubator-calcite/pulls/comments/38719211","id":38719211,"diff_hunk":"@@ -0,0 +1,245 @@\n+/*\n+ * Licensed to the Apache Software Foundation (ASF) under one or more\n+ * contributor license agreements.  See the NOTICE file distributed with\n+ * this work for additional information regarding copyright ownership.\n+ * The ASF licenses this file to you under the Apache License, Version 2.0\n+ * (the \"License\"); you may not use this file except in compliance with\n+ * the License.  You may obtain a copy of the License at\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+package org.apache.calcite.rel.rules;\n+\n+import org.apache.calcite.plan.RelOptRule;\n+import org.apache.calcite.plan.RelOptRuleCall;\n+import org.apache.calcite.rel.RelCollations;\n+import org.apache.calcite.rel.RelFieldCollation;\n+import org.apache.calcite.rel.core.Window;\n+import org.apache.calcite.rel.logical.LogicalProject;\n+import org.apache.calcite.rel.logical.LogicalWindow;\n+import org.apache.calcite.rel.type.RelDataTypeField;\n+import org.apache.calcite.rel.type.RelRecordType;\n+import org.apache.calcite.rex.RexCall;\n+import org.apache.calcite.rex.RexInputRef;\n+import org.apache.calcite.rex.RexNode;\n+import org.apache.calcite.rex.RexShuttle;\n+import org.apache.calcite.sql.SqlAggFunction;\n+import org.apache.calcite.util.ImmutableBitSet;\n+\n+import java.util.ArrayList;\n+import java.util.BitSet;\n+import java.util.List;\n+\n+/**\n+ * Planner rule that pushes\n+ * a {@link org.apache.calcite.rel.logical.LogicalProject}\n+ * past a {@link org.apache.calcite.rel.logical.LogicalWindow}.\n+ */\n+public class ProjectWindowTransposeRule extends RelOptRule {\n+  /** The default instance of\n+   * {@link org.apache.calcite.rel.rules.ProjectWindowTransposeRule}. */\n+  public static final ProjectWindowTransposeRule INSTANCE = new ProjectWindowTransposeRule();\n+\n+  private ProjectWindowTransposeRule() {\n+    super(\n+        operand(\n+            LogicalProject.class,\n+                operand(LogicalWindow.class,\n+                    any())));\n+  }\n+\n+  @Override\n+  public void onMatch(RelOptRuleCall call) {\n+    final LogicalProject project = call.rel(0);\n+    final LogicalWindow window = call.rel(1);\n+    final List<RelDataTypeField> rowTypeWindowInput = window.getInput().getRowType().getFieldList();\n+    final int windowInputColumn = rowTypeWindowInput.size();\n+\n+    // Record the window input columns which are actually referred\n+    // either in the LogicalProject above LogicalWindow or LogicalWindow itself\n+    final BitSet beReferred = findReference(project, window);\n+\n+    // If all the the window input columns are referred,\n+    // it is impossible to trim anyone of them out\n+    if (beReferred.cardinality() == windowInputColumn) {\n+      return;\n+    }\n+\n+    // Put a DrillProjectRel below LogicalWindow\n+    final List<RexNode> exps = new ArrayList<>();\n+    final List<RelDataTypeField> fields = new ArrayList<>();\n+\n+    // Keep only the fields which are referred\n+    for  (int index = beReferred.nextSetBit(0); index >= 0;\n+        index = beReferred.nextSetBit(index + 1)) {\n+      final RelDataTypeField relDataTypeField = rowTypeWindowInput.get(index);\n+      exps.add(new RexInputRef(index, relDataTypeField.getType()));\n+      fields.add(relDataTypeField);\n+    }\n+\n+    final LogicalProject projectBelowWindow = new LogicalProject(\n+        window.getCluster(),\n+        window.getTraitSet(),\n+        window.getInput(),\n+        exps,\n+        new RelRecordType(fields));","path":"core/src/main/java/org/apache/calcite/rel/rules/ProjectWindowTransposeRule.java","position":91,"original_position":91,"commit_id":"026caf0c7649b57a844b1ab11e3b082aa5ad25cf","original_commit_id":"026caf0c7649b57a844b1ab11e3b082aa5ad25cf","user":{"login":"hsuanyi","id":4021621,"avatar_url":"https://avatars.githubusercontent.com/u/4021621?v=3","gravatar_id":"","url":"https://api.github.com/users/hsuanyi","html_url":"https://github.com/hsuanyi","followers_url":"https://api.github.com/users/hsuanyi/followers","following_url":"https://api.github.com/users/hsuanyi/following{/other_user}","gists_url":"https://api.github.com/users/hsuanyi/gists{/gist_id}","starred_url":"https://api.github.com/users/hsuanyi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hsuanyi/subscriptions","organizations_url":"https://api.github.com/users/hsuanyi/orgs","repos_url":"https://api.github.com/users/hsuanyi/repos","events_url":"https://api.github.com/users/hsuanyi/events{/privacy}","received_events_url":"https://api.github.com/users/hsuanyi/received_events","type":"User","site_admin":false},"body":"Use below to create type:\r\nfinal RelDataType rowtype = RexUtil.createStructType(\r\n        window.getCluster().getTypeFactory(),\r\n        exps,\r\n        fieldNames);","created_at":"2015-09-04T03:35:40Z","updated_at":"2015-09-04T03:35:40Z","html_url":"https://github.com/apache/incubator-calcite/pull/123#discussion_r38719211","pull_request_url":"https://api.github.com/repos/apache/incubator-calcite/pulls/123","_links":{"self":{"href":"https://api.github.com/repos/apache/incubator-calcite/pulls/comments/38719211"},"html":{"href":"https://github.com/apache/incubator-calcite/pull/123#discussion_r38719211"},"pull_request":{"href":"https://api.github.com/repos/apache/incubator-calcite/pulls/123"}}},"pull_request":{"url":"https://api.github.com/repos/apache/incubator-calcite/pulls/123","id":43098615,"html_url":"https://github.com/apache/incubator-calcite/pull/123","diff_url":"https://github.com/apache/incubator-calcite/pull/123.diff","patch_url":"https://github.com/apache/incubator-calcite/pull/123.patch","issue_url":"https://api.github.com/repos/apache/incubator-calcite/issues/123","number":123,"state":"open","locked":false,"title":"[CALCITE-844] ProjectWindowTransposeRule added to push LogicalProject…","user":{"login":"hsuanyi","id":4021621,"avatar_url":"https://avatars.githubusercontent.com/u/4021621?v=3","gravatar_id":"","url":"https://api.github.com/users/hsuanyi","html_url":"https://github.com/hsuanyi","followers_url":"https://api.github.com/users/hsuanyi/followers","following_url":"https://api.github.com/users/hsuanyi/following{/other_user}","gists_url":"https://api.github.com/users/hsuanyi/gists{/gist_id}","starred_url":"https://api.github.com/users/hsuanyi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hsuanyi/subscriptions","organizations_url":"https://api.github.com/users/hsuanyi/orgs","repos_url":"https://api.github.com/users/hsuanyi/repos","events_url":"https://api.github.com/users/hsuanyi/events{/privacy}","received_events_url":"https://api.github.com/users/hsuanyi/received_events","type":"User","site_admin":false},"body":"… past LogicalWindow","created_at":"2015-08-22T06:24:57Z","updated_at":"2015-09-04T03:35:40Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c2e9e977c44222020ac76c1d889da930036b0388","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/incubator-calcite/pulls/123/commits","review_comments_url":"https://api.github.com/repos/apache/incubator-calcite/pulls/123/comments","review_comment_url":"https://api.github.com/repos/apache/incubator-calcite/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/incubator-calcite/issues/123/comments","statuses_url":"https://api.github.com/repos/apache/incubator-calcite/statuses/026caf0c7649b57a844b1ab11e3b082aa5ad25cf","head":{"label":"hsuanyi:CALCITE-844","ref":"CALCITE-844","sha":"026caf0c7649b57a844b1ab11e3b082aa5ad25cf","user":{"login":"hsuanyi","id":4021621,"avatar_url":"https://avatars.githubusercontent.com/u/4021621?v=3","gravatar_id":"","url":"https://api.github.com/users/hsuanyi","html_url":"https://github.com/hsuanyi","followers_url":"https://api.github.com/users/hsuanyi/followers","following_url":"https://api.github.com/users/hsuanyi/following{/other_user}","gists_url":"https://api.github.com/users/hsuanyi/gists{/gist_id}","starred_url":"https://api.github.com/users/hsuanyi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hsuanyi/subscriptions","organizations_url":"https://api.github.com/users/hsuanyi/orgs","repos_url":"https://api.github.com/users/hsuanyi/repos","events_url":"https://api.github.com/users/hsuanyi/events{/privacy}","received_events_url":"https://api.github.com/users/hsuanyi/received_events","type":"User","site_admin":false},"repo":{"id":30125719,"name":"incubator-calcite","full_name":"hsuanyi/incubator-calcite","owner":{"login":"hsuanyi","id":4021621,"avatar_url":"https://avatars.githubusercontent.com/u/4021621?v=3","gravatar_id":"","url":"https://api.github.com/users/hsuanyi","html_url":"https://github.com/hsuanyi","followers_url":"https://api.github.com/users/hsuanyi/followers","following_url":"https://api.github.com/users/hsuanyi/following{/other_user}","gists_url":"https://api.github.com/users/hsuanyi/gists{/gist_id}","starred_url":"https://api.github.com/users/hsuanyi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hsuanyi/subscriptions","organizations_url":"https://api.github.com/users/hsuanyi/orgs","repos_url":"https://api.github.com/users/hsuanyi/repos","events_url":"https://api.github.com/users/hsuanyi/events{/privacy}","received_events_url":"https://api.github.com/users/hsuanyi/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/hsuanyi/incubator-calcite","description":"Mirror of Apache Optiq","fork":true,"url":"https://api.github.com/repos/hsuanyi/incubator-calcite","forks_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/forks","keys_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/teams","hooks_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/hooks","issue_events_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/issues/events{/number}","events_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/events","assignees_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/assignees{/user}","branches_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/branches{/branch}","tags_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/tags","blobs_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/git/refs{/sha}","trees_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/statuses/{sha}","languages_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/languages","stargazers_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/stargazers","contributors_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/contributors","subscribers_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/subscribers","subscription_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/subscription","commits_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/commits{/sha}","git_commits_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/git/commits{/sha}","comments_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/comments{/number}","issue_comment_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/issues/comments{/number}","contents_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/contents/{+path}","compare_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/merges","archive_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/downloads","issues_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/issues{/number}","pulls_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/pulls{/number}","milestones_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/milestones{/number}","notifications_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/labels{/name}","releases_url":"https://api.github.com/repos/hsuanyi/incubator-calcite/releases{/id}","created_at":"2015-01-31T20:44:57Z","updated_at":"2015-05-07T15:30:05Z","pushed_at":"2015-09-03T22:56:54Z","git_url":"git://github.com/hsuanyi/incubator-calcite.git","ssh_url":"git@github.com:hsuanyi/incubator-calcite.git","clone_url":"https://github.com/hsuanyi/incubator-calcite.git","svn_url":"https://github.com/hsuanyi/incubator-calcite","homepage":null,"size":19828,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"9f1f73d392fbff4f0ac2b29cc9462cf388be645d","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":21193524,"name":"incubator-calcite","full_name":"apache/incubator-calcite","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/incubator-calcite","description":"Mirror of Apache Optiq","fork":false,"url":"https://api.github.com/repos/apache/incubator-calcite","forks_url":"https://api.github.com/repos/apache/incubator-calcite/forks","keys_url":"https://api.github.com/repos/apache/incubator-calcite/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/incubator-calcite/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/incubator-calcite/teams","hooks_url":"https://api.github.com/repos/apache/incubator-calcite/hooks","issue_events_url":"https://api.github.com/repos/apache/incubator-calcite/issues/events{/number}","events_url":"https://api.github.com/repos/apache/incubator-calcite/events","assignees_url":"https://api.github.com/repos/apache/incubator-calcite/assignees{/user}","branches_url":"https://api.github.com/repos/apache/incubator-calcite/branches{/branch}","tags_url":"https://api.github.com/repos/apache/incubator-calcite/tags","blobs_url":"https://api.github.com/repos/apache/incubator-calcite/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/incubator-calcite/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/incubator-calcite/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/incubator-calcite/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/incubator-calcite/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/incubator-calcite/languages","stargazers_url":"https://api.github.com/repos/apache/incubator-calcite/stargazers","contributors_url":"https://api.github.com/repos/apache/incubator-calcite/contributors","subscribers_url":"https://api.github.com/repos/apache/incubator-calcite/subscribers","subscription_url":"https://api.github.com/repos/apache/incubator-calcite/subscription","commits_url":"https://api.github.com/repos/apache/incubator-calcite/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/incubator-calcite/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/incubator-calcite/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/incubator-calcite/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/incubator-calcite/contents/{+path}","compare_url":"https://api.github.com/repos/apache/incubator-calcite/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/incubator-calcite/merges","archive_url":"https://api.github.com/repos/apache/incubator-calcite/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/incubator-calcite/downloads","issues_url":"https://api.github.com/repos/apache/incubator-calcite/issues{/number}","pulls_url":"https://api.github.com/repos/apache/incubator-calcite/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/incubator-calcite/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/incubator-calcite/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/incubator-calcite/labels{/name}","releases_url":"https://api.github.com/repos/apache/incubator-calcite/releases{/id}","created_at":"2014-06-25T07:00:07Z","updated_at":"2015-09-02T14:32:31Z","pushed_at":"2015-09-02T22:33:02Z","git_url":"git://github.com/apache/incubator-calcite.git","ssh_url":"git@github.com:apache/incubator-calcite.git","clone_url":"https://github.com/apache/incubator-calcite.git","svn_url":"https://github.com/apache/incubator-calcite","homepage":null,"size":94400,"stargazers_count":111,"watchers_count":111,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":93,"mirror_url":"git://git.apache.org/incubator-optiq.git","open_issues_count":8,"forks":93,"open_issues":8,"watchers":111,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/incubator-calcite/pulls/123"},"html":{"href":"https://github.com/apache/incubator-calcite/pull/123"},"issue":{"href":"https://api.github.com/repos/apache/incubator-calcite/issues/123"},"comments":{"href":"https://api.github.com/repos/apache/incubator-calcite/issues/123/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/incubator-calcite/pulls/123/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/incubator-calcite/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/incubator-calcite/pulls/123/commits"},"statuses":{"href":"https://api.github.com/repos/apache/incubator-calcite/statuses/026caf0c7649b57a844b1ab11e3b082aa5ad25cf"}}}}