{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/41325055","id":41325055,"diff_hunk":"@@ -88,6 +88,276 @@ case class Average(child: Expression) extends AlgebraicAggregate {\n   }\n }\n \n+abstract class CentralMomentAgg(child: Expression) extends AlgebraicAggregate {\n+\n+  // specify the maximum order moment needed for the computation\n+  def maxMoment: Int\n+\n+  override def children: Seq[Expression] = child :: Nil\n+\n+  override def nullable: Boolean = true\n+\n+  // Return data type.\n+  override def dataType: DataType = resultType\n+\n+  // Expected input data type.\n+  // TODO: Right now, we replace old aggregate functions (based on AggregateExpression1) to the\n+  // new version at planning time (after analysis phase). For now, NullType is added at here\n+  // to make it resolved when we have cases like `select avg(null)`.\n+  // We can use our analyzer to cast NullType to the default data type of the NumericType once\n+  // we remove the old aggregate functions. Then, we will not need NullType at here.\n+  override def inputTypes: Seq[AbstractDataType] = Seq(TypeCollection(NumericType, NullType))\n+\n+  protected val resultType = DoubleType\n+\n+  protected val currentM4 = AttributeReference(\"currentM4\", resultType)()\n+  protected val currentM3 = AttributeReference(\"currentM3\", resultType)()\n+  protected val currentM2 = AttributeReference(\"currentM2\", resultType)()\n+  protected val currentM1 = AttributeReference(\"currentM1\", resultType)()\n+  protected val currentM0 = AttributeReference(\"currentM0\", resultType)()\n+\n+  override val bufferAttributes = List(currentM4, currentM3, currentM2, currentM1, currentM0)\n+\n+  override val initialValues = Seq(\n+    /* currentM4 = */ Cast(Literal(0), resultType),\n+    /* currentM3 = */ Cast(Literal(0), resultType),\n+    /* currentM2 = */ Cast(Literal(0), resultType),\n+    /* currentM1 = */ Cast(Literal(0), resultType),\n+    /* currentM0 = */ Cast(Literal(0), resultType)\n+  )\n+\n+  override val updateExpressions = {\n+    lazy val updateM0: Expression = {\n+      Add(currentM0, Cast(Literal(1), resultType))\n+    }\n+    lazy val delta = Subtract(Cast(child, resultType), currentM1)\n+    lazy val deltaN = Divide(delta, updateM0)\n+\n+    lazy val updateM1: Expression = if (maxMoment >= 1) {\n+      Add(currentM1, Divide(delta, updateM0))\n+    } else {\n+      Cast(Literal(0), resultType)\n+    }\n+\n+    lazy val updateM2: Expression = if (maxMoment >= 2) {\n+      Add(currentM2, Multiply(deltaN * delta, Subtract(updateM0, Cast(Literal(1), resultType))))\n+    } else {\n+      Cast(Literal(0), resultType)\n+    }\n+\n+    lazy val updateM3: Expression = if (maxMoment >= 3) {\n+      currentM3 + deltaN * deltaN * delta * (updateM0 - Cast(Literal(1), resultType)) *\n+        (updateM0 - Cast(Literal(2), resultType)) -\n+        deltaN * currentM2 * Cast(Literal(3), resultType)\n+    } else {\n+      Cast(Literal(0), resultType)\n+    }\n+\n+    lazy val updateM4: Expression = if (maxMoment >= 4) {\n+      currentM4 + deltaN * deltaN * deltaN * delta * (updateM0 - Cast(Literal(1), resultType)) *\n+        (updateM0 * updateM0 - updateM0 * Cast(Literal(3), resultType) +\n+          Cast(Literal(3), resultType)) + deltaN * deltaN * currentM2 *\n+        Cast(Literal(6), resultType) - deltaN * currentM3 * Cast(Literal(4), resultType)\n+    } else {\n+      Cast(Literal(0), resultType)\n+    }\n+\n+    Seq(\n+      /* currentM4 = */ If(IsNull(child), currentM4, updateM4),\n+      /* currentM3 = */ If(IsNull(child), currentM3, updateM3),\n+      /* currentM2 = */ If(IsNull(child), currentM2, updateM2),\n+      /* currentM1 = */ If(IsNull(child), currentM1, updateM1),\n+      /* currentM0 = */ If(IsNull(child), currentM0, updateM0)\n+    )\n+  }\n+\n+  override val mergeExpressions = {\n+\n+\n+    lazy val updateM0 = currentM0.left + currentM0.right\n+    lazy val delta = currentM1.right - currentM1.left\n+    lazy val deltaN = delta / updateM0\n+\n+    lazy val updateM1 = if (maxMoment >= 1) {\n+      currentM1.left + delta * (currentM0.right / updateM0)\n+    } else {\n+      Cast(Literal(0), resultType)\n+    }\n+\n+    lazy val updateM2 = if (maxMoment >= 2) {\n+      currentM2.left + currentM2.right + delta * deltaN * currentM0.left * currentM0.right\n+    } else {\n+      Cast(Literal(0), resultType)\n+    }\n+\n+    lazy val updateM3 = if (maxMoment >= 3) {","path":"sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/functions.scala","position":106,"original_position":106,"commit_id":"e6c8d277d01ecffab7473bdd47de2e5069692ac6","original_commit_id":"e6c8d277d01ecffab7473bdd47de2e5069692ac6","user":{"login":"sethah","id":7275795,"avatar_url":"https://avatars.githubusercontent.com/u/7275795?v=3","gravatar_id":"","url":"https://api.github.com/users/sethah","html_url":"https://github.com/sethah","followers_url":"https://api.github.com/users/sethah/followers","following_url":"https://api.github.com/users/sethah/following{/other_user}","gists_url":"https://api.github.com/users/sethah/gists{/gist_id}","starred_url":"https://api.github.com/users/sethah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sethah/subscriptions","organizations_url":"https://api.github.com/users/sethah/orgs","repos_url":"https://api.github.com/users/sethah/repos","events_url":"https://api.github.com/users/sethah/events{/privacy}","received_events_url":"https://api.github.com/users/sethah/received_events","type":"User","site_admin":false},"body":"maxMoment variable is implemented with intent to avoid extra computation. For instance, if variance function calls `CentralMomentAgg` then the 3rd and 4th moments do not need to be computed. Would appreciate feedback/criticism of this implementation.","created_at":"2015-10-06T21:29:01Z","updated_at":"2015-10-06T21:29:01Z","html_url":"https://github.com/apache/spark/pull/9003#discussion_r41325055","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/9003","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/41325055"},"html":{"href":"https://github.com/apache/spark/pull/9003#discussion_r41325055"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/9003"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/9003","id":46958902,"html_url":"https://github.com/apache/spark/pull/9003","diff_url":"https://github.com/apache/spark/pull/9003.diff","patch_url":"https://github.com/apache/spark/pull/9003.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/9003","number":9003,"state":"open","locked":false,"title":"[SPARK-10641][WIP][SQL] Add Skewness and Kurtosis Support","user":{"login":"sethah","id":7275795,"avatar_url":"https://avatars.githubusercontent.com/u/7275795?v=3","gravatar_id":"","url":"https://api.github.com/users/sethah","html_url":"https://github.com/sethah","followers_url":"https://api.github.com/users/sethah/followers","following_url":"https://api.github.com/users/sethah/following{/other_user}","gists_url":"https://api.github.com/users/sethah/gists{/gist_id}","starred_url":"https://api.github.com/users/sethah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sethah/subscriptions","organizations_url":"https://api.github.com/users/sethah/orgs","repos_url":"https://api.github.com/users/sethah/repos","events_url":"https://api.github.com/users/sethah/events{/privacy}","received_events_url":"https://api.github.com/users/sethah/received_events","type":"User","site_admin":false},"body":"Implementing skewness and kurtosis support based on following algorithm:\r\nhttps://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Higher-order_statistics\r\n","created_at":"2015-10-06T21:15:56Z","updated_at":"2015-10-06T21:29:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"fdc4553135d97cdfbfaf18d627f02a177eb42a70","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/9003/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/9003/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/9003/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/e6c8d277d01ecffab7473bdd47de2e5069692ac6","head":{"label":"sethah:SPARK-10641","ref":"SPARK-10641","sha":"e6c8d277d01ecffab7473bdd47de2e5069692ac6","user":{"login":"sethah","id":7275795,"avatar_url":"https://avatars.githubusercontent.com/u/7275795?v=3","gravatar_id":"","url":"https://api.github.com/users/sethah","html_url":"https://github.com/sethah","followers_url":"https://api.github.com/users/sethah/followers","following_url":"https://api.github.com/users/sethah/following{/other_user}","gists_url":"https://api.github.com/users/sethah/gists{/gist_id}","starred_url":"https://api.github.com/users/sethah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sethah/subscriptions","organizations_url":"https://api.github.com/users/sethah/orgs","repos_url":"https://api.github.com/users/sethah/repos","events_url":"https://api.github.com/users/sethah/events{/privacy}","received_events_url":"https://api.github.com/users/sethah/received_events","type":"User","site_admin":false},"repo":{"id":38016887,"name":"spark","full_name":"sethah/spark","owner":{"login":"sethah","id":7275795,"avatar_url":"https://avatars.githubusercontent.com/u/7275795?v=3","gravatar_id":"","url":"https://api.github.com/users/sethah","html_url":"https://github.com/sethah","followers_url":"https://api.github.com/users/sethah/followers","following_url":"https://api.github.com/users/sethah/following{/other_user}","gists_url":"https://api.github.com/users/sethah/gists{/gist_id}","starred_url":"https://api.github.com/users/sethah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sethah/subscriptions","organizations_url":"https://api.github.com/users/sethah/orgs","repos_url":"https://api.github.com/users/sethah/repos","events_url":"https://api.github.com/users/sethah/events{/privacy}","received_events_url":"https://api.github.com/users/sethah/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/sethah/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/sethah/spark","forks_url":"https://api.github.com/repos/sethah/spark/forks","keys_url":"https://api.github.com/repos/sethah/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sethah/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sethah/spark/teams","hooks_url":"https://api.github.com/repos/sethah/spark/hooks","issue_events_url":"https://api.github.com/repos/sethah/spark/issues/events{/number}","events_url":"https://api.github.com/repos/sethah/spark/events","assignees_url":"https://api.github.com/repos/sethah/spark/assignees{/user}","branches_url":"https://api.github.com/repos/sethah/spark/branches{/branch}","tags_url":"https://api.github.com/repos/sethah/spark/tags","blobs_url":"https://api.github.com/repos/sethah/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sethah/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sethah/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/sethah/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sethah/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/sethah/spark/languages","stargazers_url":"https://api.github.com/repos/sethah/spark/stargazers","contributors_url":"https://api.github.com/repos/sethah/spark/contributors","subscribers_url":"https://api.github.com/repos/sethah/spark/subscribers","subscription_url":"https://api.github.com/repos/sethah/spark/subscription","commits_url":"https://api.github.com/repos/sethah/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/sethah/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/sethah/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/sethah/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/sethah/spark/contents/{+path}","compare_url":"https://api.github.com/repos/sethah/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sethah/spark/merges","archive_url":"https://api.github.com/repos/sethah/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sethah/spark/downloads","issues_url":"https://api.github.com/repos/sethah/spark/issues{/number}","pulls_url":"https://api.github.com/repos/sethah/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/sethah/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/sethah/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sethah/spark/labels{/name}","releases_url":"https://api.github.com/repos/sethah/spark/releases{/id}","created_at":"2015-06-24T23:36:53Z","updated_at":"2015-06-24T23:37:41Z","pushed_at":"2015-10-06T20:56:42Z","git_url":"git://github.com/sethah/spark.git","ssh_url":"git@github.com:sethah/spark.git","clone_url":"https://github.com/sethah/spark.git","svn_url":"https://github.com/sethah/spark","homepage":null,"size":124809,"stargazers_count":0,"watchers_count":0,"language":"Scala","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":"5952bdb7df20d007d59f82261095faca3822c6f6","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":17165658,"name":"spark","full_name":"apache/spark","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/spark","description":"Mirror of Apache Spark","fork":false,"url":"https://api.github.com/repos/apache/spark","forks_url":"https://api.github.com/repos/apache/spark/forks","keys_url":"https://api.github.com/repos/apache/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/spark/teams","hooks_url":"https://api.github.com/repos/apache/spark/hooks","issue_events_url":"https://api.github.com/repos/apache/spark/issues/events{/number}","events_url":"https://api.github.com/repos/apache/spark/events","assignees_url":"https://api.github.com/repos/apache/spark/assignees{/user}","branches_url":"https://api.github.com/repos/apache/spark/branches{/branch}","tags_url":"https://api.github.com/repos/apache/spark/tags","blobs_url":"https://api.github.com/repos/apache/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/spark/languages","stargazers_url":"https://api.github.com/repos/apache/spark/stargazers","contributors_url":"https://api.github.com/repos/apache/spark/contributors","subscribers_url":"https://api.github.com/repos/apache/spark/subscribers","subscription_url":"https://api.github.com/repos/apache/spark/subscription","commits_url":"https://api.github.com/repos/apache/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/spark/contents/{+path}","compare_url":"https://api.github.com/repos/apache/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/spark/merges","archive_url":"https://api.github.com/repos/apache/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/spark/downloads","issues_url":"https://api.github.com/repos/apache/spark/issues{/number}","pulls_url":"https://api.github.com/repos/apache/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/spark/labels{/name}","releases_url":"https://api.github.com/repos/apache/spark/releases{/id}","created_at":"2014-02-25T08:00:08Z","updated_at":"2015-10-06T21:16:39Z","pushed_at":"2015-10-06T21:15:56Z","git_url":"git://github.com/apache/spark.git","ssh_url":"git@github.com:apache/spark.git","clone_url":"https://github.com/apache/spark.git","svn_url":"https://github.com/apache/spark","homepage":null,"size":1628016,"stargazers_count":5677,"watchers_count":5677,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":5247,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":358,"forks":5247,"open_issues":358,"watchers":5677,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/9003"},"html":{"href":"https://github.com/apache/spark/pull/9003"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/9003"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/9003/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/9003/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/9003/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/e6c8d277d01ecffab7473bdd47de2e5069692ac6"}}}}