{"action":"created","comment":{"url":"https://api.github.com/repos/apache/incubator-madlib/pulls/comments/67399048","id":67399048,"diff_hunk":"@@ -0,0 +1,200 @@\n+# coding=utf-8\n+#\n+# Licensed to the Apache Software Foundation (ASF) under one\n+# or more contributor license agreements.  See the NOTICE file\n+# distributed with this work for additional information\n+# regarding copyright ownership.  The ASF licenses this file\n+# to you under the Apache License, Version 2.0 (the\n+# \"License\"); you may not use this file except in compliance\n+# with 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,\n+# software distributed under the License is distributed on an\n+# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n+# KIND, either express or implied.  See the License for the\n+# specific language governing permissions and limitations\n+# under the License.\n+\n+# Pivoting\n+# The goal of the MADlib pivot function is to provide a data summarization tool\n+# that can do basic OLAP type operations on data stored in one table and output\n+# the summarized data to a second table.  Typical operations are count, average,\n+# min, max and standard deviation, however user defined aggregates (UDAs) are\n+# also be allowed.\n+\n+# Please refer to the pivot.sql_in file for the documentation\n+\n+\"\"\"\n+@file pivot.py_in\n+\n+\"\"\"\n+import plpy\n+from utilities import _assert\n+from utilities import split_quoted_delimited_str\n+from utilities import strip_end_quotes\n+from validate_args import table_exists\n+from validate_args import columns_exist_in_table\n+from validate_args import table_is_empty\n+from validate_args import _get_table_schema_names\n+from validate_args import get_first_schema\n+\n+m4_changequote(`<!', `!>')\n+\n+def pivot(schema_madlib, source_table, out_table,\n+                 index, pivot_cols, pivot_values,\n+                 aggregate_func, **kwargs):\n+    \"\"\"\n+    Helper function that can be used to pivot tables\n+    Args:\n+        @param source_table     The original data table\n+        @param out_table        The output table that contains the dummy\n+                                variable columns\n+        @param index            The index columns to group by the records by\n+        @param pivot_cols       The columns to pivot the table\n+        @param pivot_values     The value columns to be summarized in the\n+                                pivoted table\n+        @param aggregate_func   The aggregate function to be applied to the\n+                                values\n+\n+    \"\"\"\n+    indices = split_quoted_delimited_str(index)\n+    cols = split_quoted_delimited_str(pivot_cols)\n+    vals = split_quoted_delimited_str(pivot_values)\n+    # aggregate_func = \"sum\"\n+    validate_pivot_coding(source_table, out_table, indices, cols, vals)\n+    new_col_names =[]\n+    sql_list = [\"CREATE TABLE \" + out_table + \" AS (SELECT \" + index]\n+    # Preperation for multiple index, pivot, etc.\n+    for col in cols:\n+        for val in vals:\n+            col_no_quotes = strip_end_quotes(col.strip())\n+            val_no_quotes = strip_end_quotes(val.strip())\n+            distinct_values = plpy.execute(\n+                \"SELECT {col} AS value FROM {source_table} \"\n+                \"GROUP BY {col} ORDER BY {col}\".\n+                format(col=col, source_table=source_table))\n+            distinct_values = [strip_end_quotes(item['value']) for item in distinct_values]\n+            null_wrap_case_str = \"\"\n+            null_wrap_end_str = \"\"\n+            if None in distinct_values:\n+                null_wrap_case_str = \"CASE WHEN \\\"{col}\\\" is NULL THEN NULL ELSE \"\n+                null_wrap_end_str = \"END\"\n+            case_str = (\"{agg}({wrap_case} \"\n+                        \"CASE WHEN \\\"{{col}}\\\" = '{{value}}' THEN {val} ELSE 0 END \"","path":"src/ports/postgres/modules/utilities/pivot.py_in","position":85,"original_position":85,"commit_id":"d24df8923d74e9a2d2883c5e564875f05502d68e","original_commit_id":"d24df8923d74e9a2d2883c5e564875f05502d68e","user":{"login":"orhankislal","id":9490520,"avatar_url":"https://avatars.githubusercontent.com/u/9490520?v=3","gravatar_id":"","url":"https://api.github.com/users/orhankislal","html_url":"https://github.com/orhankislal","followers_url":"https://api.github.com/users/orhankislal/followers","following_url":"https://api.github.com/users/orhankislal/following{/other_user}","gists_url":"https://api.github.com/users/orhankislal/gists{/gist_id}","starred_url":"https://api.github.com/users/orhankislal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/orhankislal/subscriptions","organizations_url":"https://api.github.com/users/orhankislal/orgs","repos_url":"https://api.github.com/users/orhankislal/repos","events_url":"https://api.github.com/users/orhankislal/events{/privacy}","received_events_url":"https://api.github.com/users/orhankislal/received_events","type":"User","site_admin":false},"body":"Not a bad idea, it'll get even more confusing when we add multiple value columns. I'll wait for any other comments and make one more commit with all of the changes.","created_at":"2016-06-16T18:28:35Z","updated_at":"2016-06-16T18:28:35Z","html_url":"https://github.com/apache/incubator-madlib/pull/47#discussion_r67399048","pull_request_url":"https://api.github.com/repos/apache/incubator-madlib/pulls/47","_links":{"self":{"href":"https://api.github.com/repos/apache/incubator-madlib/pulls/comments/67399048"},"html":{"href":"https://github.com/apache/incubator-madlib/pull/47#discussion_r67399048"},"pull_request":{"href":"https://api.github.com/repos/apache/incubator-madlib/pulls/47"}}},"pull_request":{"url":"https://api.github.com/repos/apache/incubator-madlib/pulls/47","id":73996946,"html_url":"https://github.com/apache/incubator-madlib/pull/47","diff_url":"https://github.com/apache/incubator-madlib/pull/47.diff","patch_url":"https://github.com/apache/incubator-madlib/pull/47.patch","issue_url":"https://api.github.com/repos/apache/incubator-madlib/issues/47","number":47,"state":"open","locked":false,"title":"Feature: Pivot Function","user":{"login":"orhankislal","id":9490520,"avatar_url":"https://avatars.githubusercontent.com/u/9490520?v=3","gravatar_id":"","url":"https://api.github.com/users/orhankislal","html_url":"https://github.com/orhankislal","followers_url":"https://api.github.com/users/orhankislal/followers","following_url":"https://api.github.com/users/orhankislal/following{/other_user}","gists_url":"https://api.github.com/users/orhankislal/gists{/gist_id}","starred_url":"https://api.github.com/users/orhankislal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/orhankislal/subscriptions","organizations_url":"https://api.github.com/users/orhankislal/orgs","repos_url":"https://api.github.com/users/orhankislal/repos","events_url":"https://api.github.com/users/orhankislal/events{/privacy}","received_events_url":"https://api.github.com/users/orhankislal/received_events","type":"User","site_admin":false},"body":"JIRA: MADLIB-908\r\n\r\nThe goal of the MADlib pivot function is to provide a data summarization tool\r\nthat can do basic OLAP type operations on data stored in one table and output\r\nthe summarized data to a second table.","created_at":"2016-06-15T22:45:41Z","updated_at":"2016-06-16T18:28:35Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e8739bfcaeaed34dd8455b0726099210f621962a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/incubator-madlib/pulls/47/commits","review_comments_url":"https://api.github.com/repos/apache/incubator-madlib/pulls/47/comments","review_comment_url":"https://api.github.com/repos/apache/incubator-madlib/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/incubator-madlib/issues/47/comments","statuses_url":"https://api.github.com/repos/apache/incubator-madlib/statuses/d24df8923d74e9a2d2883c5e564875f05502d68e","head":{"label":"orhankislal:feature/pivoting_take2","ref":"feature/pivoting_take2","sha":"d24df8923d74e9a2d2883c5e564875f05502d68e","user":{"login":"orhankislal","id":9490520,"avatar_url":"https://avatars.githubusercontent.com/u/9490520?v=3","gravatar_id":"","url":"https://api.github.com/users/orhankislal","html_url":"https://github.com/orhankislal","followers_url":"https://api.github.com/users/orhankislal/followers","following_url":"https://api.github.com/users/orhankislal/following{/other_user}","gists_url":"https://api.github.com/users/orhankislal/gists{/gist_id}","starred_url":"https://api.github.com/users/orhankislal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/orhankislal/subscriptions","organizations_url":"https://api.github.com/users/orhankislal/orgs","repos_url":"https://api.github.com/users/orhankislal/repos","events_url":"https://api.github.com/users/orhankislal/events{/privacy}","received_events_url":"https://api.github.com/users/orhankislal/received_events","type":"User","site_admin":false},"repo":{"id":49608798,"name":"incubator-madlib","full_name":"orhankislal/incubator-madlib","owner":{"login":"orhankislal","id":9490520,"avatar_url":"https://avatars.githubusercontent.com/u/9490520?v=3","gravatar_id":"","url":"https://api.github.com/users/orhankislal","html_url":"https://github.com/orhankislal","followers_url":"https://api.github.com/users/orhankislal/followers","following_url":"https://api.github.com/users/orhankislal/following{/other_user}","gists_url":"https://api.github.com/users/orhankislal/gists{/gist_id}","starred_url":"https://api.github.com/users/orhankislal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/orhankislal/subscriptions","organizations_url":"https://api.github.com/users/orhankislal/orgs","repos_url":"https://api.github.com/users/orhankislal/repos","events_url":"https://api.github.com/users/orhankislal/events{/privacy}","received_events_url":"https://api.github.com/users/orhankislal/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/orhankislal/incubator-madlib","description":"Mirror of Apache MADlib (Incubating)","fork":true,"url":"https://api.github.com/repos/orhankislal/incubator-madlib","forks_url":"https://api.github.com/repos/orhankislal/incubator-madlib/forks","keys_url":"https://api.github.com/repos/orhankislal/incubator-madlib/keys{/key_id}","collaborators_url":"https://api.github.com/repos/orhankislal/incubator-madlib/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/orhankislal/incubator-madlib/teams","hooks_url":"https://api.github.com/repos/orhankislal/incubator-madlib/hooks","issue_events_url":"https://api.github.com/repos/orhankislal/incubator-madlib/issues/events{/number}","events_url":"https://api.github.com/repos/orhankislal/incubator-madlib/events","assignees_url":"https://api.github.com/repos/orhankislal/incubator-madlib/assignees{/user}","branches_url":"https://api.github.com/repos/orhankislal/incubator-madlib/branches{/branch}","tags_url":"https://api.github.com/repos/orhankislal/incubator-madlib/tags","blobs_url":"https://api.github.com/repos/orhankislal/incubator-madlib/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/orhankislal/incubator-madlib/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/orhankislal/incubator-madlib/git/refs{/sha}","trees_url":"https://api.github.com/repos/orhankislal/incubator-madlib/git/trees{/sha}","statuses_url":"https://api.github.com/repos/orhankislal/incubator-madlib/statuses/{sha}","languages_url":"https://api.github.com/repos/orhankislal/incubator-madlib/languages","stargazers_url":"https://api.github.com/repos/orhankislal/incubator-madlib/stargazers","contributors_url":"https://api.github.com/repos/orhankislal/incubator-madlib/contributors","subscribers_url":"https://api.github.com/repos/orhankislal/incubator-madlib/subscribers","subscription_url":"https://api.github.com/repos/orhankislal/incubator-madlib/subscription","commits_url":"https://api.github.com/repos/orhankislal/incubator-madlib/commits{/sha}","git_commits_url":"https://api.github.com/repos/orhankislal/incubator-madlib/git/commits{/sha}","comments_url":"https://api.github.com/repos/orhankislal/incubator-madlib/comments{/number}","issue_comment_url":"https://api.github.com/repos/orhankislal/incubator-madlib/issues/comments{/number}","contents_url":"https://api.github.com/repos/orhankislal/incubator-madlib/contents/{+path}","compare_url":"https://api.github.com/repos/orhankislal/incubator-madlib/compare/{base}...{head}","merges_url":"https://api.github.com/repos/orhankislal/incubator-madlib/merges","archive_url":"https://api.github.com/repos/orhankislal/incubator-madlib/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/orhankislal/incubator-madlib/downloads","issues_url":"https://api.github.com/repos/orhankislal/incubator-madlib/issues{/number}","pulls_url":"https://api.github.com/repos/orhankislal/incubator-madlib/pulls{/number}","milestones_url":"https://api.github.com/repos/orhankislal/incubator-madlib/milestones{/number}","notifications_url":"https://api.github.com/repos/orhankislal/incubator-madlib/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/orhankislal/incubator-madlib/labels{/name}","releases_url":"https://api.github.com/repos/orhankislal/incubator-madlib/releases{/id}","deployments_url":"https://api.github.com/repos/orhankislal/incubator-madlib/deployments","created_at":"2016-01-13T23:23:39Z","updated_at":"2016-01-13T23:23:43Z","pushed_at":"2016-06-15T22:43:55Z","git_url":"git://github.com/orhankislal/incubator-madlib.git","ssh_url":"git@github.com:orhankislal/incubator-madlib.git","clone_url":"https://github.com/orhankislal/incubator-madlib.git","svn_url":"https://github.com/orhankislal/incubator-madlib","homepage":null,"size":15794,"stargazers_count":0,"watchers_count":0,"language":"C","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":"45ef7753bfe6f1883b408ff9a8baa3d8aba5b86c","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":42763345,"name":"incubator-madlib","full_name":"apache/incubator-madlib","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-madlib","description":"Mirror of Apache MADlib (Incubating)","fork":false,"url":"https://api.github.com/repos/apache/incubator-madlib","forks_url":"https://api.github.com/repos/apache/incubator-madlib/forks","keys_url":"https://api.github.com/repos/apache/incubator-madlib/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/incubator-madlib/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/incubator-madlib/teams","hooks_url":"https://api.github.com/repos/apache/incubator-madlib/hooks","issue_events_url":"https://api.github.com/repos/apache/incubator-madlib/issues/events{/number}","events_url":"https://api.github.com/repos/apache/incubator-madlib/events","assignees_url":"https://api.github.com/repos/apache/incubator-madlib/assignees{/user}","branches_url":"https://api.github.com/repos/apache/incubator-madlib/branches{/branch}","tags_url":"https://api.github.com/repos/apache/incubator-madlib/tags","blobs_url":"https://api.github.com/repos/apache/incubator-madlib/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/incubator-madlib/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/incubator-madlib/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/incubator-madlib/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/incubator-madlib/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/incubator-madlib/languages","stargazers_url":"https://api.github.com/repos/apache/incubator-madlib/stargazers","contributors_url":"https://api.github.com/repos/apache/incubator-madlib/contributors","subscribers_url":"https://api.github.com/repos/apache/incubator-madlib/subscribers","subscription_url":"https://api.github.com/repos/apache/incubator-madlib/subscription","commits_url":"https://api.github.com/repos/apache/incubator-madlib/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/incubator-madlib/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/incubator-madlib/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/incubator-madlib/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/incubator-madlib/contents/{+path}","compare_url":"https://api.github.com/repos/apache/incubator-madlib/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/incubator-madlib/merges","archive_url":"https://api.github.com/repos/apache/incubator-madlib/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/incubator-madlib/downloads","issues_url":"https://api.github.com/repos/apache/incubator-madlib/issues{/number}","pulls_url":"https://api.github.com/repos/apache/incubator-madlib/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/incubator-madlib/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/incubator-madlib/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/incubator-madlib/labels{/name}","releases_url":"https://api.github.com/repos/apache/incubator-madlib/releases{/id}","deployments_url":"https://api.github.com/repos/apache/incubator-madlib/deployments","created_at":"2015-09-19T07:00:06Z","updated_at":"2016-06-16T02:23:42Z","pushed_at":"2016-06-15T22:45:41Z","git_url":"git://github.com/apache/incubator-madlib.git","ssh_url":"git@github.com:apache/incubator-madlib.git","clone_url":"https://github.com/apache/incubator-madlib.git","svn_url":"https://github.com/apache/incubator-madlib","homepage":null,"size":15847,"stargazers_count":55,"watchers_count":55,"language":"C","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":31,"mirror_url":"git://git.apache.org/incubator-madlib.git","open_issues_count":2,"forks":31,"open_issues":2,"watchers":55,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/incubator-madlib/pulls/47"},"html":{"href":"https://github.com/apache/incubator-madlib/pull/47"},"issue":{"href":"https://api.github.com/repos/apache/incubator-madlib/issues/47"},"comments":{"href":"https://api.github.com/repos/apache/incubator-madlib/issues/47/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/incubator-madlib/pulls/47/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/incubator-madlib/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/incubator-madlib/pulls/47/commits"},"statuses":{"href":"https://api.github.com/repos/apache/incubator-madlib/statuses/d24df8923d74e9a2d2883c5e564875f05502d68e"}}}}
{"actor":{"display_login":"orhankislal"}}