{"action":"created","comment":{"url":"https://api.github.com/repos/prestodb/presto/pulls/comments/111556256","pull_request_review_id":32803354,"id":111556256,"diff_hunk":"@@ -0,0 +1,101 @@\n+/*\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * 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 com.facebook.presto.sql.planner;\n+\n+import com.facebook.presto.metadata.Metadata;\n+import com.facebook.presto.sql.analyzer.Analysis;\n+import com.facebook.presto.sql.analyzer.TypeSignatureProvider;\n+import com.facebook.presto.sql.tree.ArrayConstructor;\n+import com.facebook.presto.sql.tree.Cast;\n+import com.facebook.presto.sql.tree.Expression;\n+import com.facebook.presto.sql.tree.FunctionCall;\n+import com.facebook.presto.sql.tree.GroupingOperation;\n+import com.facebook.presto.sql.tree.LongLiteral;\n+import com.facebook.presto.sql.tree.QualifiedName;\n+import com.facebook.presto.sql.tree.QuerySpecification;\n+import com.facebook.presto.sql.tree.SimpleGroupBy;\n+import com.facebook.presto.type.ListLiteralType;\n+import com.google.common.collect.ImmutableList;\n+\n+import java.util.Arrays;\n+import java.util.List;\n+import java.util.Optional;\n+\n+import static com.facebook.presto.operator.scalar.GroupingOperationFunction.GROUPING;\n+import static com.facebook.presto.spi.type.BigintType.BIGINT;\n+import static com.facebook.presto.sql.analyzer.ExpressionAnalyzer.resolveFunction;\n+import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;\n+import static com.google.common.base.Preconditions.checkState;\n+import static java.util.Objects.requireNonNull;\n+\n+public final class GroupingOperationRewriter\n+{\n+    private GroupingOperationRewriter() {}\n+\n+    public static Expression rewriteGroupingOperation(GroupingOperation expression, QuerySpecification queryNode, Analysis analysis, Metadata metadata, Optional<Symbol> groupIdSymbol)\n+    {\n+        requireNonNull(queryNode, \"node is null\");\n+        requireNonNull(analysis, \"analysis is null\");\n+        requireNonNull(metadata, \"metadata is null\");\n+        requireNonNull(groupIdSymbol, \"groupIdSymbol is null\");\n+\n+        checkState(queryNode.getGroupBy().isPresent(), \"GroupBy node must be present\");\n+\n+        // No GroupIdNode and a GROUPING() operation imply a single grouping, which\n+        // means that any columns specified as arguments to GROUPING() will be included\n+        // in the group and none of them will be aggregated over. Hence, re-write the\n+        // GroupingOperation to a constant literal of 0.\n+        // See SQL:2011:4.16.2 and SQL:2011:6.9.10.\n+        boolean isSimpleGroupByPresent = queryNode.getGroupBy().get().getGroupingElements().stream().anyMatch(SimpleGroupBy.class::isInstance);\n+        if (isSimpleGroupByPresent) {\n+            return new LongLiteral(\"0\");\n+        }\n+        else {\n+            checkState(groupIdSymbol.isPresent(), \"groupId symbol is missing\");\n+\n+            List<Expression> columnReferences = ImmutableList.copyOf(analysis.getColumnReferences());\n+            List<Expression> groupingOrdinals = expression.getGroupingColumns().stream()\n+                    .map(columnReferences::indexOf)\n+                    .map(columnOrdinal -> new LongLiteral(Integer.toString(columnOrdinal)))\n+                    .collect(toImmutableList());\n+\n+            List<List<Expression>> groupingSetOrdinals;\n+            ImmutableList.Builder<List<Expression>> groupingSetOrdinalsBuilder = ImmutableList.builder();\n+            for (List<Expression> groupingSet : analysis.getGroupingSets(queryNode)) {\n+                groupingSetOrdinalsBuilder.add(groupingSet.stream()\n+                        .map(columnReferences::indexOf)\n+                        .map(columnOrdinal -> new LongLiteral(Integer.toString(columnOrdinal)))\n+                        .collect(toImmutableList()));\n+            }\n+            groupingSetOrdinals = groupingSetOrdinalsBuilder.build();\n+\n+            Expression firstGroupingArgument = groupIdSymbol.get().toSymbolReference();\n+            List<Expression> newGroupingArguments = ImmutableList.of(\n+                    firstGroupingArgument,\n+                    new Cast(new ArrayConstructor(groupingOrdinals), ListLiteralType.NAME),\n+                    new Cast(new ArrayConstructor(groupingSetOrdinals.stream().map(ArrayConstructor::new).collect(toImmutableList())), ListLiteralType.NAME)\n+            );\n+\n+            FunctionCall rewritten = new FunctionCall(expression.getLocation().get(), QualifiedName.of(GROUPING), newGroupingArguments);\n+            List<TypeSignatureProvider> functionTypes = Arrays.asList(\n+                    new TypeSignatureProvider(BIGINT.getTypeSignature()),\n+                    new TypeSignatureProvider(ListLiteralType.LIST_LITERAL.getTypeSignature()),\n+                    new TypeSignatureProvider(ListLiteralType.LIST_LITERAL.getTypeSignature())\n+            );\n+            resolveFunction(rewritten, functionTypes, metadata.getFunctionRegistry());\n+\n+            return rewritten;","path":"presto-main/src/main/java/com/facebook/presto/sql/planner/GroupingOperationRewriter.java","position":98,"original_position":98,"commit_id":"36069c8afd91f2da7b079e39ea71ef4f8c070efa","original_commit_id":"52c76af9b13686f52ab4426087957438e8b3d967","user":{"login":"sopel39","id":5989165,"avatar_url":"https://avatars2.githubusercontent.com/u/5989165?v=3","gravatar_id":"","url":"https://api.github.com/users/sopel39","html_url":"https://github.com/sopel39","followers_url":"https://api.github.com/users/sopel39/followers","following_url":"https://api.github.com/users/sopel39/following{/other_user}","gists_url":"https://api.github.com/users/sopel39/gists{/gist_id}","starred_url":"https://api.github.com/users/sopel39/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sopel39/subscriptions","organizations_url":"https://api.github.com/users/sopel39/orgs","repos_url":"https://api.github.com/users/sopel39/repos","events_url":"https://api.github.com/users/sopel39/events{/privacy}","received_events_url":"https://api.github.com/users/sopel39/received_events","type":"User","site_admin":false},"body":"much nicer and readable. nice!","created_at":"2017-04-14T10:00:06Z","updated_at":"2017-04-14T11:03:58Z","html_url":"https://github.com/prestodb/presto/pull/7712#discussion_r111556256","pull_request_url":"https://api.github.com/repos/prestodb/presto/pulls/7712","_links":{"self":{"href":"https://api.github.com/repos/prestodb/presto/pulls/comments/111556256"},"html":{"href":"https://github.com/prestodb/presto/pull/7712#discussion_r111556256"},"pull_request":{"href":"https://api.github.com/repos/prestodb/presto/pulls/7712"}}},"pull_request":{"url":"https://api.github.com/repos/prestodb/presto/pulls/7712","id":113677580,"html_url":"https://github.com/prestodb/presto/pull/7712","diff_url":"https://github.com/prestodb/presto/pull/7712.diff","patch_url":"https://github.com/prestodb/presto/pull/7712.patch","issue_url":"https://api.github.com/repos/prestodb/presto/issues/7712","number":7712,"state":"open","locked":false,"title":"Feature grouping","user":{"login":"petroav","id":665991,"avatar_url":"https://avatars2.githubusercontent.com/u/665991?v=3","gravatar_id":"","url":"https://api.github.com/users/petroav","html_url":"https://github.com/petroav","followers_url":"https://api.github.com/users/petroav/followers","following_url":"https://api.github.com/users/petroav/following{/other_user}","gists_url":"https://api.github.com/users/petroav/gists{/gist_id}","starred_url":"https://api.github.com/users/petroav/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/petroav/subscriptions","organizations_url":"https://api.github.com/users/petroav/orgs","repos_url":"https://api.github.com/users/petroav/repos","events_url":"https://api.github.com/users/petroav/events{/privacy}","received_events_url":"https://api.github.com/users/petroav/received_events","type":"User","site_admin":false},"body":"","created_at":"2017-03-31T15:43:09Z","updated_at":"2017-04-14T11:03:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5a72e73b50335f6ff537b52c0b3b1d791ffb97c5","assignee":{"login":"sopel39","id":5989165,"avatar_url":"https://avatars2.githubusercontent.com/u/5989165?v=3","gravatar_id":"","url":"https://api.github.com/users/sopel39","html_url":"https://github.com/sopel39","followers_url":"https://api.github.com/users/sopel39/followers","following_url":"https://api.github.com/users/sopel39/following{/other_user}","gists_url":"https://api.github.com/users/sopel39/gists{/gist_id}","starred_url":"https://api.github.com/users/sopel39/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sopel39/subscriptions","organizations_url":"https://api.github.com/users/sopel39/orgs","repos_url":"https://api.github.com/users/sopel39/repos","events_url":"https://api.github.com/users/sopel39/events{/privacy}","received_events_url":"https://api.github.com/users/sopel39/received_events","type":"User","site_admin":false},"assignees":[{"login":"sopel39","id":5989165,"avatar_url":"https://avatars2.githubusercontent.com/u/5989165?v=3","gravatar_id":"","url":"https://api.github.com/users/sopel39","html_url":"https://github.com/sopel39","followers_url":"https://api.github.com/users/sopel39/followers","following_url":"https://api.github.com/users/sopel39/following{/other_user}","gists_url":"https://api.github.com/users/sopel39/gists{/gist_id}","starred_url":"https://api.github.com/users/sopel39/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sopel39/subscriptions","organizations_url":"https://api.github.com/users/sopel39/orgs","repos_url":"https://api.github.com/users/sopel39/repos","events_url":"https://api.github.com/users/sopel39/events{/privacy}","received_events_url":"https://api.github.com/users/sopel39/received_events","type":"User","site_admin":false}],"milestone":null,"commits_url":"https://api.github.com/repos/prestodb/presto/pulls/7712/commits","review_comments_url":"https://api.github.com/repos/prestodb/presto/pulls/7712/comments","review_comment_url":"https://api.github.com/repos/prestodb/presto/pulls/comments{/number}","comments_url":"https://api.github.com/repos/prestodb/presto/issues/7712/comments","statuses_url":"https://api.github.com/repos/prestodb/presto/statuses/36069c8afd91f2da7b079e39ea71ef4f8c070efa","head":{"label":"Teradata:feature-grouping","ref":"feature-grouping","sha":"36069c8afd91f2da7b079e39ea71ef4f8c070efa","user":{"login":"Teradata","id":1615979,"avatar_url":"https://avatars2.githubusercontent.com/u/1615979?v=3","gravatar_id":"","url":"https://api.github.com/users/Teradata","html_url":"https://github.com/Teradata","followers_url":"https://api.github.com/users/Teradata/followers","following_url":"https://api.github.com/users/Teradata/following{/other_user}","gists_url":"https://api.github.com/users/Teradata/gists{/gist_id}","starred_url":"https://api.github.com/users/Teradata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Teradata/subscriptions","organizations_url":"https://api.github.com/users/Teradata/orgs","repos_url":"https://api.github.com/users/Teradata/repos","events_url":"https://api.github.com/users/Teradata/events{/privacy}","received_events_url":"https://api.github.com/users/Teradata/received_events","type":"Organization","site_admin":false},"repo":{"id":36903368,"name":"presto","full_name":"Teradata/presto","owner":{"login":"Teradata","id":1615979,"avatar_url":"https://avatars2.githubusercontent.com/u/1615979?v=3","gravatar_id":"","url":"https://api.github.com/users/Teradata","html_url":"https://github.com/Teradata","followers_url":"https://api.github.com/users/Teradata/followers","following_url":"https://api.github.com/users/Teradata/following{/other_user}","gists_url":"https://api.github.com/users/Teradata/gists{/gist_id}","starred_url":"https://api.github.com/users/Teradata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Teradata/subscriptions","organizations_url":"https://api.github.com/users/Teradata/orgs","repos_url":"https://api.github.com/users/Teradata/repos","events_url":"https://api.github.com/users/Teradata/events{/privacy}","received_events_url":"https://api.github.com/users/Teradata/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Teradata/presto","description":"Teradata Distribution of Presto -- A Distributed SQL Query Engine for Big Data","fork":true,"url":"https://api.github.com/repos/Teradata/presto","forks_url":"https://api.github.com/repos/Teradata/presto/forks","keys_url":"https://api.github.com/repos/Teradata/presto/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Teradata/presto/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Teradata/presto/teams","hooks_url":"https://api.github.com/repos/Teradata/presto/hooks","issue_events_url":"https://api.github.com/repos/Teradata/presto/issues/events{/number}","events_url":"https://api.github.com/repos/Teradata/presto/events","assignees_url":"https://api.github.com/repos/Teradata/presto/assignees{/user}","branches_url":"https://api.github.com/repos/Teradata/presto/branches{/branch}","tags_url":"https://api.github.com/repos/Teradata/presto/tags","blobs_url":"https://api.github.com/repos/Teradata/presto/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Teradata/presto/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Teradata/presto/git/refs{/sha}","trees_url":"https://api.github.com/repos/Teradata/presto/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Teradata/presto/statuses/{sha}","languages_url":"https://api.github.com/repos/Teradata/presto/languages","stargazers_url":"https://api.github.com/repos/Teradata/presto/stargazers","contributors_url":"https://api.github.com/repos/Teradata/presto/contributors","subscribers_url":"https://api.github.com/repos/Teradata/presto/subscribers","subscription_url":"https://api.github.com/repos/Teradata/presto/subscription","commits_url":"https://api.github.com/repos/Teradata/presto/commits{/sha}","git_commits_url":"https://api.github.com/repos/Teradata/presto/git/commits{/sha}","comments_url":"https://api.github.com/repos/Teradata/presto/comments{/number}","issue_comment_url":"https://api.github.com/repos/Teradata/presto/issues/comments{/number}","contents_url":"https://api.github.com/repos/Teradata/presto/contents/{+path}","compare_url":"https://api.github.com/repos/Teradata/presto/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Teradata/presto/merges","archive_url":"https://api.github.com/repos/Teradata/presto/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Teradata/presto/downloads","issues_url":"https://api.github.com/repos/Teradata/presto/issues{/number}","pulls_url":"https://api.github.com/repos/Teradata/presto/pulls{/number}","milestones_url":"https://api.github.com/repos/Teradata/presto/milestones{/number}","notifications_url":"https://api.github.com/repos/Teradata/presto/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Teradata/presto/labels{/name}","releases_url":"https://api.github.com/repos/Teradata/presto/releases{/id}","deployments_url":"https://api.github.com/repos/Teradata/presto/deployments","created_at":"2015-06-05T00:38:37Z","updated_at":"2017-04-07T00:48:05Z","pushed_at":"2017-04-14T10:33:30Z","git_url":"git://github.com/Teradata/presto.git","ssh_url":"git@github.com:Teradata/presto.git","clone_url":"https://github.com/Teradata/presto.git","svn_url":"https://github.com/Teradata/presto","homepage":"http://www.teradata.com/presto","size":181833,"stargazers_count":72,"watchers_count":72,"language":"Java","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":19,"mirror_url":null,"open_issues_count":21,"forks":19,"open_issues":21,"watchers":72,"default_branch":"master"}},"base":{"label":"prestodb:master","ref":"master","sha":"0a53e60b459f9ea97e77316600352358d8c9ab66","user":{"login":"prestodb","id":6882181,"avatar_url":"https://avatars3.githubusercontent.com/u/6882181?v=3","gravatar_id":"","url":"https://api.github.com/users/prestodb","html_url":"https://github.com/prestodb","followers_url":"https://api.github.com/users/prestodb/followers","following_url":"https://api.github.com/users/prestodb/following{/other_user}","gists_url":"https://api.github.com/users/prestodb/gists{/gist_id}","starred_url":"https://api.github.com/users/prestodb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/prestodb/subscriptions","organizations_url":"https://api.github.com/users/prestodb/orgs","repos_url":"https://api.github.com/users/prestodb/repos","events_url":"https://api.github.com/users/prestodb/events{/privacy}","received_events_url":"https://api.github.com/users/prestodb/received_events","type":"Organization","site_admin":false},"repo":{"id":5349565,"name":"presto","full_name":"prestodb/presto","owner":{"login":"prestodb","id":6882181,"avatar_url":"https://avatars3.githubusercontent.com/u/6882181?v=3","gravatar_id":"","url":"https://api.github.com/users/prestodb","html_url":"https://github.com/prestodb","followers_url":"https://api.github.com/users/prestodb/followers","following_url":"https://api.github.com/users/prestodb/following{/other_user}","gists_url":"https://api.github.com/users/prestodb/gists{/gist_id}","starred_url":"https://api.github.com/users/prestodb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/prestodb/subscriptions","organizations_url":"https://api.github.com/users/prestodb/orgs","repos_url":"https://api.github.com/users/prestodb/repos","events_url":"https://api.github.com/users/prestodb/events{/privacy}","received_events_url":"https://api.github.com/users/prestodb/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/prestodb/presto","description":"Distributed SQL query engine for big data","fork":false,"url":"https://api.github.com/repos/prestodb/presto","forks_url":"https://api.github.com/repos/prestodb/presto/forks","keys_url":"https://api.github.com/repos/prestodb/presto/keys{/key_id}","collaborators_url":"https://api.github.com/repos/prestodb/presto/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/prestodb/presto/teams","hooks_url":"https://api.github.com/repos/prestodb/presto/hooks","issue_events_url":"https://api.github.com/repos/prestodb/presto/issues/events{/number}","events_url":"https://api.github.com/repos/prestodb/presto/events","assignees_url":"https://api.github.com/repos/prestodb/presto/assignees{/user}","branches_url":"https://api.github.com/repos/prestodb/presto/branches{/branch}","tags_url":"https://api.github.com/repos/prestodb/presto/tags","blobs_url":"https://api.github.com/repos/prestodb/presto/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/prestodb/presto/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/prestodb/presto/git/refs{/sha}","trees_url":"https://api.github.com/repos/prestodb/presto/git/trees{/sha}","statuses_url":"https://api.github.com/repos/prestodb/presto/statuses/{sha}","languages_url":"https://api.github.com/repos/prestodb/presto/languages","stargazers_url":"https://api.github.com/repos/prestodb/presto/stargazers","contributors_url":"https://api.github.com/repos/prestodb/presto/contributors","subscribers_url":"https://api.github.com/repos/prestodb/presto/subscribers","subscription_url":"https://api.github.com/repos/prestodb/presto/subscription","commits_url":"https://api.github.com/repos/prestodb/presto/commits{/sha}","git_commits_url":"https://api.github.com/repos/prestodb/presto/git/commits{/sha}","comments_url":"https://api.github.com/repos/prestodb/presto/comments{/number}","issue_comment_url":"https://api.github.com/repos/prestodb/presto/issues/comments{/number}","contents_url":"https://api.github.com/repos/prestodb/presto/contents/{+path}","compare_url":"https://api.github.com/repos/prestodb/presto/compare/{base}...{head}","merges_url":"https://api.github.com/repos/prestodb/presto/merges","archive_url":"https://api.github.com/repos/prestodb/presto/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/prestodb/presto/downloads","issues_url":"https://api.github.com/repos/prestodb/presto/issues{/number}","pulls_url":"https://api.github.com/repos/prestodb/presto/pulls{/number}","milestones_url":"https://api.github.com/repos/prestodb/presto/milestones{/number}","notifications_url":"https://api.github.com/repos/prestodb/presto/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/prestodb/presto/labels{/name}","releases_url":"https://api.github.com/repos/prestodb/presto/releases{/id}","deployments_url":"https://api.github.com/repos/prestodb/presto/deployments","created_at":"2012-08-09T01:03:37Z","updated_at":"2017-04-14T07:21:57Z","pushed_at":"2017-04-14T09:02:01Z","git_url":"git://github.com/prestodb/presto.git","ssh_url":"git@github.com:prestodb/presto.git","clone_url":"https://github.com/prestodb/presto.git","svn_url":"https://github.com/prestodb/presto","homepage":"https://prestodb.io","size":70537,"stargazers_count":5862,"watchers_count":5862,"language":"Java","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1886,"mirror_url":null,"open_issues_count":894,"forks":1886,"open_issues":894,"watchers":5862,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/prestodb/presto/pulls/7712"},"html":{"href":"https://github.com/prestodb/presto/pull/7712"},"issue":{"href":"https://api.github.com/repos/prestodb/presto/issues/7712"},"comments":{"href":"https://api.github.com/repos/prestodb/presto/issues/7712/comments"},"review_comments":{"href":"https://api.github.com/repos/prestodb/presto/pulls/7712/comments"},"review_comment":{"href":"https://api.github.com/repos/prestodb/presto/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/prestodb/presto/pulls/7712/commits"},"statuses":{"href":"https://api.github.com/repos/prestodb/presto/statuses/36069c8afd91f2da7b079e39ea71ef4f8c070efa"}}}}
{"actor":{"display_login":"sopel39"}}