{"action":"created","comment":{"url":"https://api.github.com/repos/crate/crate/pulls/comments/78546178","id":78546178,"diff_hunk":"@@ -0,0 +1,152 @@\n+/*\n+ * Licensed to Crate under one or more contributor license agreements.\n+ * See the NOTICE file distributed with this work for additional\n+ * information regarding copyright ownership.  Crate licenses this file\n+ * to you under the Apache License, Version 2.0 (the \"License\"); you may\n+ * not use this file except in compliance with the License.  You may\n+ * 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\n+ * implied.  See the License for the specific language governing\n+ * permissions and limitations under the License.\n+ *\n+ * However, if you have executed another commercial license agreement\n+ * with Crate these terms will supersede the license and you may use the\n+ * software solely pursuant to the terms of the relevant commercial\n+ * agreement.\n+ */\n+\n+package io.crate.operation.scalar.stats;\n+\n+import com.carrotsearch.hppc.DoubleArrayList;\n+import com.google.common.collect.ImmutableList;\n+import com.google.common.collect.ImmutableSet;\n+import io.crate.metadata.FunctionIdent;\n+import io.crate.metadata.FunctionInfo;\n+import io.crate.metadata.Scalar;\n+import io.crate.operation.Input;\n+import io.crate.operation.scalar.ScalarFunctionModule;\n+import io.crate.types.ArrayType;\n+import io.crate.types.DataType;\n+import io.crate.types.DataTypes;\n+import org.apache.commons.math3.stat.descriptive.rank.Percentile;\n+\n+import java.util.Set;\n+\n+/**\n+ * @see io.crate.operation.scalar.stats.PercentileFunction function estimates percentiles based on sample data.\n+ * For the percentiles calculation, we use Percentile from (Commons Math 3.0 API).\n+ * @see org.apache.commons.math3.stat.descriptive.rank.Percentile\n+ */\n+public abstract class PercentileFunction<R, T> extends Scalar<R, T> {\n+\n+    private static final String NAME = \"percentile_cont\";\n+    private static final DataType DOUBLE_ARRAY_TYPE = new ArrayType(DataTypes.DOUBLE);\n+    private static final Set<DataType> ALLOWED_TYPES = ImmutableSet.<DataType>builder()\n+                                                           .addAll(DataTypes.NUMERIC_PRIMITIVE_TYPES)\n+                                                           .add(DataTypes.UNDEFINED)\n+                                                           .build();\n+\n+    public static void register(ScalarFunctionModule module) {\n+        PercentileContArray.register(module);\n+        PercentileCont.register(module);\n+    }\n+\n+    private final FunctionInfo info;\n+\n+    private PercentileFunction(FunctionInfo info) {\n+        this.info = info;\n+    }\n+\n+    @Override\n+    public FunctionInfo info() {\n+        return info;\n+    }\n+\n+\n+    protected static double[] toDoubleArray(Object[] array) {\n+        DoubleArrayList values = new DoubleArrayList(array.length);\n+        for (Object value : array) {\n+            if (value != null) {\n+                values.add(DataTypes.DOUBLE.value(value));\n+            }\n+        }\n+        return values.toArray();\n+    }\n+\n+    protected static boolean nullOrEmptyArgs(Object arrayValue, Object fractionValue) {\n+        return arrayValue == null || fractionValue == null || ((Object[]) arrayValue).length == 0;\n+    }\n+\n+    private static class PercentileContArray extends PercentileFunction<Double[], Number[]> {\n+\n+        public static void register(ScalarFunctionModule module) {\n+            for (DataType dataType : ALLOWED_TYPES) {\n+                FunctionIdent ident = new FunctionIdent(NAME, ImmutableList.of(DOUBLE_ARRAY_TYPE, new ArrayType(dataType)));\n+                module.register(new PercentileContArray(new FunctionInfo(ident, DOUBLE_ARRAY_TYPE)));\n+            }\n+        }\n+\n+        PercentileContArray(FunctionInfo info) {\n+            super(info);\n+        }\n+\n+        @Override\n+        public Double[] evaluate(Input<Number[]>[] args) {\n+            assert args.length == 2;\n+            Object arrayValue = args[1].value();\n+            Object fractionsValue = args[0].value();\n+\n+            if (nullOrEmptyArgs(arrayValue, fractionsValue)) {\n+                return null;\n+            }\n+\n+            return evaluatePercentiles((Object[]) arrayValue, (Object[]) fractionsValue);\n+        }\n+\n+        private Double[] evaluatePercentiles(Object[] values, Object[] fractions) {\n+            final Percentile percentile = new Percentile();\n+            percentile.setData(toDoubleArray((values)));\n+\n+            Double[] result = new Double[fractions.length];\n+            for (int idx = 0; idx < fractions.length; idx++) {\n+                result[idx] = percentile.evaluate(DataTypes.DOUBLE.value(fractions[idx]) * 100.0);\n+            }\n+            return result;\n+        }\n+    }\n+\n+\n+    private static class PercentileCont extends PercentileFunction<Double, Number[]> {\n+\n+        public static void register(ScalarFunctionModule module) {\n+            for (DataType dataType : ALLOWED_TYPES) {\n+                FunctionIdent ident\n+                    = new FunctionIdent(NAME, ImmutableList.<DataType>of(DataTypes.DOUBLE, new ArrayType(dataType)));\n+                module.register(new PercentileCont(new FunctionInfo(ident, DataTypes.DOUBLE)));\n+            }\n+        }\n+\n+        PercentileCont(FunctionInfo info) {\n+            super(info);\n+        }\n+\n+        @Override\n+        public Double evaluate(Input<Number[]>[] args) {\n+            assert args.length == 2;\n+            Object arrayValue = args[1].value();\n+            Object fractionValue = args[0].value();","path":"sql/src/main/java/io/crate/operation/scalar/stats/PercentileFunction.java","position":142,"original_position":142,"commit_id":"de21d165e0d914334b77ef9b2dc519f1e82167a4","original_commit_id":"de21d165e0d914334b77ef9b2dc519f1e82167a4","user":{"login":"mfussenegger","id":38700,"avatar_url":"https://avatars.githubusercontent.com/u/38700?v=3","gravatar_id":"","url":"https://api.github.com/users/mfussenegger","html_url":"https://github.com/mfussenegger","followers_url":"https://api.github.com/users/mfussenegger/followers","following_url":"https://api.github.com/users/mfussenegger/following{/other_user}","gists_url":"https://api.github.com/users/mfussenegger/gists{/gist_id}","starred_url":"https://api.github.com/users/mfussenegger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mfussenegger/subscriptions","organizations_url":"https://api.github.com/users/mfussenegger/orgs","repos_url":"https://api.github.com/users/mfussenegger/repos","events_url":"https://api.github.com/users/mfussenegger/events{/privacy}","received_events_url":"https://api.github.com/users/mfussenegger/received_events","type":"User","site_admin":false},"body":"I think this should be safe to cast to double because the function is only registered for  <double, array<T>>?\r\nWould avoid the DataTypes.DOUBLE.value call further down.","created_at":"2016-09-13T12:15:00Z","updated_at":"2016-09-13T12:15:00Z","html_url":"https://github.com/crate/crate/pull/4058#discussion_r78546178","pull_request_url":"https://api.github.com/repos/crate/crate/pulls/4058","_links":{"self":{"href":"https://api.github.com/repos/crate/crate/pulls/comments/78546178"},"html":{"href":"https://github.com/crate/crate/pull/4058#discussion_r78546178"},"pull_request":{"href":"https://api.github.com/repos/crate/crate/pulls/4058"}}},"pull_request":{"url":"https://api.github.com/repos/crate/crate/pulls/4058","id":85003852,"html_url":"https://github.com/crate/crate/pull/4058","diff_url":"https://github.com/crate/crate/pull/4058.diff","patch_url":"https://github.com/crate/crate/pull/4058.patch","issue_url":"https://api.github.com/repos/crate/crate/issues/4058","number":4058,"state":"open","locked":false,"title":"Implement the `percentile_cont` scalar function.","user":{"login":"kovrus","id":7442373,"avatar_url":"https://avatars.githubusercontent.com/u/7442373?v=3","gravatar_id":"","url":"https://api.github.com/users/kovrus","html_url":"https://github.com/kovrus","followers_url":"https://api.github.com/users/kovrus/followers","following_url":"https://api.github.com/users/kovrus/following{/other_user}","gists_url":"https://api.github.com/users/kovrus/gists{/gist_id}","starred_url":"https://api.github.com/users/kovrus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kovrus/subscriptions","organizations_url":"https://api.github.com/users/kovrus/orgs","repos_url":"https://api.github.com/users/kovrus/repos","events_url":"https://api.github.com/users/kovrus/events{/privacy}","received_events_url":"https://api.github.com/users/kovrus/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-09-13T08:20:06Z","updated_at":"2016-09-13T12:15:00Z","closed_at":null,"merged_at":null,"merge_commit_sha":"29316675b44011eb93e478a594d80f6aa82b99e2","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/crate/crate/pulls/4058/commits","review_comments_url":"https://api.github.com/repos/crate/crate/pulls/4058/comments","review_comment_url":"https://api.github.com/repos/crate/crate/pulls/comments{/number}","comments_url":"https://api.github.com/repos/crate/crate/issues/4058/comments","statuses_url":"https://api.github.com/repos/crate/crate/statuses/de21d165e0d914334b77ef9b2dc519f1e82167a4","head":{"label":"crate:r/percentile-cont","ref":"r/percentile-cont","sha":"de21d165e0d914334b77ef9b2dc519f1e82167a4","user":{"login":"crate","id":4048232,"avatar_url":"https://avatars.githubusercontent.com/u/4048232?v=3","gravatar_id":"","url":"https://api.github.com/users/crate","html_url":"https://github.com/crate","followers_url":"https://api.github.com/users/crate/followers","following_url":"https://api.github.com/users/crate/following{/other_user}","gists_url":"https://api.github.com/users/crate/gists{/gist_id}","starred_url":"https://api.github.com/users/crate/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crate/subscriptions","organizations_url":"https://api.github.com/users/crate/orgs","repos_url":"https://api.github.com/users/crate/repos","events_url":"https://api.github.com/users/crate/events{/privacy}","received_events_url":"https://api.github.com/users/crate/received_events","type":"Organization","site_admin":false},"repo":{"id":9342529,"name":"crate","full_name":"crate/crate","owner":{"login":"crate","id":4048232,"avatar_url":"https://avatars.githubusercontent.com/u/4048232?v=3","gravatar_id":"","url":"https://api.github.com/users/crate","html_url":"https://github.com/crate","followers_url":"https://api.github.com/users/crate/followers","following_url":"https://api.github.com/users/crate/following{/other_user}","gists_url":"https://api.github.com/users/crate/gists{/gist_id}","starred_url":"https://api.github.com/users/crate/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crate/subscriptions","organizations_url":"https://api.github.com/users/crate/orgs","repos_url":"https://api.github.com/users/crate/repos","events_url":"https://api.github.com/users/crate/events{/privacy}","received_events_url":"https://api.github.com/users/crate/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/crate/crate","description":"Crate.IO: The fast, scalable, easy to use SQL database with native full text search","fork":false,"url":"https://api.github.com/repos/crate/crate","forks_url":"https://api.github.com/repos/crate/crate/forks","keys_url":"https://api.github.com/repos/crate/crate/keys{/key_id}","collaborators_url":"https://api.github.com/repos/crate/crate/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/crate/crate/teams","hooks_url":"https://api.github.com/repos/crate/crate/hooks","issue_events_url":"https://api.github.com/repos/crate/crate/issues/events{/number}","events_url":"https://api.github.com/repos/crate/crate/events","assignees_url":"https://api.github.com/repos/crate/crate/assignees{/user}","branches_url":"https://api.github.com/repos/crate/crate/branches{/branch}","tags_url":"https://api.github.com/repos/crate/crate/tags","blobs_url":"https://api.github.com/repos/crate/crate/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/crate/crate/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/crate/crate/git/refs{/sha}","trees_url":"https://api.github.com/repos/crate/crate/git/trees{/sha}","statuses_url":"https://api.github.com/repos/crate/crate/statuses/{sha}","languages_url":"https://api.github.com/repos/crate/crate/languages","stargazers_url":"https://api.github.com/repos/crate/crate/stargazers","contributors_url":"https://api.github.com/repos/crate/crate/contributors","subscribers_url":"https://api.github.com/repos/crate/crate/subscribers","subscription_url":"https://api.github.com/repos/crate/crate/subscription","commits_url":"https://api.github.com/repos/crate/crate/commits{/sha}","git_commits_url":"https://api.github.com/repos/crate/crate/git/commits{/sha}","comments_url":"https://api.github.com/repos/crate/crate/comments{/number}","issue_comment_url":"https://api.github.com/repos/crate/crate/issues/comments{/number}","contents_url":"https://api.github.com/repos/crate/crate/contents/{+path}","compare_url":"https://api.github.com/repos/crate/crate/compare/{base}...{head}","merges_url":"https://api.github.com/repos/crate/crate/merges","archive_url":"https://api.github.com/repos/crate/crate/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/crate/crate/downloads","issues_url":"https://api.github.com/repos/crate/crate/issues{/number}","pulls_url":"https://api.github.com/repos/crate/crate/pulls{/number}","milestones_url":"https://api.github.com/repos/crate/crate/milestones{/number}","notifications_url":"https://api.github.com/repos/crate/crate/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/crate/crate/labels{/name}","releases_url":"https://api.github.com/repos/crate/crate/releases{/id}","deployments_url":"https://api.github.com/repos/crate/crate/deployments","created_at":"2013-04-10T09:17:16Z","updated_at":"2016-09-12T00:28:41Z","pushed_at":"2016-09-13T11:52:16Z","git_url":"git://github.com/crate/crate.git","ssh_url":"git@github.com:crate/crate.git","clone_url":"https://github.com/crate/crate.git","svn_url":"https://github.com/crate/crate","homepage":"https://crate.io","size":74349,"stargazers_count":1361,"watchers_count":1361,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":155,"mirror_url":null,"open_issues_count":38,"forks":155,"open_issues":38,"watchers":1361,"default_branch":"master"}},"base":{"label":"crate:percentile-aggregation","ref":"percentile-aggregation","sha":"7917e7e875367f5555a4d02a92fece0f4667efdc","user":{"login":"crate","id":4048232,"avatar_url":"https://avatars.githubusercontent.com/u/4048232?v=3","gravatar_id":"","url":"https://api.github.com/users/crate","html_url":"https://github.com/crate","followers_url":"https://api.github.com/users/crate/followers","following_url":"https://api.github.com/users/crate/following{/other_user}","gists_url":"https://api.github.com/users/crate/gists{/gist_id}","starred_url":"https://api.github.com/users/crate/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crate/subscriptions","organizations_url":"https://api.github.com/users/crate/orgs","repos_url":"https://api.github.com/users/crate/repos","events_url":"https://api.github.com/users/crate/events{/privacy}","received_events_url":"https://api.github.com/users/crate/received_events","type":"Organization","site_admin":false},"repo":{"id":9342529,"name":"crate","full_name":"crate/crate","owner":{"login":"crate","id":4048232,"avatar_url":"https://avatars.githubusercontent.com/u/4048232?v=3","gravatar_id":"","url":"https://api.github.com/users/crate","html_url":"https://github.com/crate","followers_url":"https://api.github.com/users/crate/followers","following_url":"https://api.github.com/users/crate/following{/other_user}","gists_url":"https://api.github.com/users/crate/gists{/gist_id}","starred_url":"https://api.github.com/users/crate/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crate/subscriptions","organizations_url":"https://api.github.com/users/crate/orgs","repos_url":"https://api.github.com/users/crate/repos","events_url":"https://api.github.com/users/crate/events{/privacy}","received_events_url":"https://api.github.com/users/crate/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/crate/crate","description":"Crate.IO: The fast, scalable, easy to use SQL database with native full text search","fork":false,"url":"https://api.github.com/repos/crate/crate","forks_url":"https://api.github.com/repos/crate/crate/forks","keys_url":"https://api.github.com/repos/crate/crate/keys{/key_id}","collaborators_url":"https://api.github.com/repos/crate/crate/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/crate/crate/teams","hooks_url":"https://api.github.com/repos/crate/crate/hooks","issue_events_url":"https://api.github.com/repos/crate/crate/issues/events{/number}","events_url":"https://api.github.com/repos/crate/crate/events","assignees_url":"https://api.github.com/repos/crate/crate/assignees{/user}","branches_url":"https://api.github.com/repos/crate/crate/branches{/branch}","tags_url":"https://api.github.com/repos/crate/crate/tags","blobs_url":"https://api.github.com/repos/crate/crate/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/crate/crate/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/crate/crate/git/refs{/sha}","trees_url":"https://api.github.com/repos/crate/crate/git/trees{/sha}","statuses_url":"https://api.github.com/repos/crate/crate/statuses/{sha}","languages_url":"https://api.github.com/repos/crate/crate/languages","stargazers_url":"https://api.github.com/repos/crate/crate/stargazers","contributors_url":"https://api.github.com/repos/crate/crate/contributors","subscribers_url":"https://api.github.com/repos/crate/crate/subscribers","subscription_url":"https://api.github.com/repos/crate/crate/subscription","commits_url":"https://api.github.com/repos/crate/crate/commits{/sha}","git_commits_url":"https://api.github.com/repos/crate/crate/git/commits{/sha}","comments_url":"https://api.github.com/repos/crate/crate/comments{/number}","issue_comment_url":"https://api.github.com/repos/crate/crate/issues/comments{/number}","contents_url":"https://api.github.com/repos/crate/crate/contents/{+path}","compare_url":"https://api.github.com/repos/crate/crate/compare/{base}...{head}","merges_url":"https://api.github.com/repos/crate/crate/merges","archive_url":"https://api.github.com/repos/crate/crate/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/crate/crate/downloads","issues_url":"https://api.github.com/repos/crate/crate/issues{/number}","pulls_url":"https://api.github.com/repos/crate/crate/pulls{/number}","milestones_url":"https://api.github.com/repos/crate/crate/milestones{/number}","notifications_url":"https://api.github.com/repos/crate/crate/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/crate/crate/labels{/name}","releases_url":"https://api.github.com/repos/crate/crate/releases{/id}","deployments_url":"https://api.github.com/repos/crate/crate/deployments","created_at":"2013-04-10T09:17:16Z","updated_at":"2016-09-12T00:28:41Z","pushed_at":"2016-09-13T11:52:16Z","git_url":"git://github.com/crate/crate.git","ssh_url":"git@github.com:crate/crate.git","clone_url":"https://github.com/crate/crate.git","svn_url":"https://github.com/crate/crate","homepage":"https://crate.io","size":74349,"stargazers_count":1361,"watchers_count":1361,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":155,"mirror_url":null,"open_issues_count":38,"forks":155,"open_issues":38,"watchers":1361,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/crate/crate/pulls/4058"},"html":{"href":"https://github.com/crate/crate/pull/4058"},"issue":{"href":"https://api.github.com/repos/crate/crate/issues/4058"},"comments":{"href":"https://api.github.com/repos/crate/crate/issues/4058/comments"},"review_comments":{"href":"https://api.github.com/repos/crate/crate/pulls/4058/comments"},"review_comment":{"href":"https://api.github.com/repos/crate/crate/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/crate/crate/pulls/4058/commits"},"statuses":{"href":"https://api.github.com/repos/crate/crate/statuses/de21d165e0d914334b77ef9b2dc519f1e82167a4"}}}}
{"actor":{"display_login":"mfussenegger"}}