{"action":"created","comment":{"url":"https://api.github.com/repos/apache/drill/pulls/comments/63799682","id":63799682,"diff_hunk":"@@ -0,0 +1,161 @@\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+ * <p/>\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ * <p/>\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.drill.exec.planner.common;\n+\n+import java.util.Map;\n+\n+import com.google.common.base.Preconditions;\n+import com.google.common.collect.Maps;\n+import org.apache.calcite.rel.RelNode;\n+import org.apache.calcite.rel.RelVisitor;\n+import org.apache.calcite.rel.core.TableScan;\n+import org.apache.drill.exec.client.DrillClient;\n+import org.apache.drill.exec.ops.QueryContext;\n+import org.apache.drill.exec.planner.logical.DrillTable;\n+import org.apache.drill.exec.proto.UserBitShared;\n+import org.apache.drill.exec.server.DrillbitContext;\n+import org.apache.drill.exec.server.rest.QueryWrapper.Listener;\n+\n+/**\n+ * Wraps the stats table info including schema and tableName. Also materializes stats from storage and keeps them in\n+ * memory.\n+ */\n+public class DrillStatsTable {\n+  private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(DrillStatsTable.class);\n+\n+  /**\n+   * List of columns in stats table.\n+   */\n+  public static final String COL_COLUMN = \"column\";\n+  public static final String COL_COMPUTED = \"computed\";\n+  public static final String COL_STATCOUNT = \"statcount\";\n+  public static final String COL_NDV = \"ndv\";\n+\n+  private final String schemaName;\n+  private final String tableName;\n+\n+  private final Map<String, Long> ndv = Maps.newHashMap();\n+  private double rowCount = -1;\n+\n+  private boolean materialized = false;\n+\n+  public DrillStatsTable(String schemaName, String tableName) {\n+    this.schemaName = schemaName;\n+    this.tableName = tableName;\n+  }\n+\n+  /**\n+   * Get number of distinct values of given column. If stats are not present for the given column, a null is returned.\n+   *\n+   * Note: returned data may not be accurate. Accuracy depends on whether the table data has changed after the\n+   * stats are computed.\n+   *\n+   * @param col\n+   * @return\n+   */\n+  public Double getNdv(String col) {\n+    Preconditions.checkState(materialized, \"Stats are not yet materialized.\");\n+\n+    final String upperCol = col.toUpperCase();\n+    final Long ndvCol = ndv.get(upperCol);\n+    if (ndvCol != null) {\n+      return Math.min(ndvCol, rowCount);\n+    }\n+\n+    return null;\n+  }\n+\n+  /**\n+   * Get row count of the table. Returns null if stats are not present.\n+   *\n+   * Note: returned data may not be accurate. Accuracy depends on whether the table data has changed after the\n+   * stats are computed.\n+   *\n+   * @return\n+   */\n+  public Double getRowCount() {\n+    Preconditions.checkState(materialized, \"Stats are not yet materialized.\");\n+    return rowCount > 0 ? rowCount : null;\n+  }\n+\n+  /**\n+   * Read the stats from storage and keep them in memory.\n+   * @param context\n+   * @throws Exception\n+   */\n+  public void materialize(final QueryContext context) throws Exception {\n+    if (materialized) {\n+      return;\n+    }\n+\n+    final String fullTableName = \"`\" + schemaName + \"`.`\" + tableName + \"`\";\n+    final String sql = \"SELECT a.* FROM \" + fullTableName + \" AS a INNER JOIN \" +\n+        \"(SELECT `\" + COL_COLUMN + \"`, max(`\" + COL_COMPUTED +\"`) AS `\" + COL_COMPUTED + \"` \" +\n+        \"FROM \" + fullTableName + \" GROUP BY `\" + COL_COLUMN + \"`) AS b \" +\n+        \"ON a.`\" + COL_COLUMN + \"` = b.`\" + COL_COLUMN +\"` and a.`\" + COL_COMPUTED + \"` = b.`\" + COL_COMPUTED + \"`\";\n+\n+    final DrillbitContext dc = context.getDrillbitContext();\n+    try(final DrillClient client = new DrillClient(dc.getConfig(), dc.getClusterCoordinator(), dc.getAllocator())) {","path":"exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillStatsTable.java","position":113,"original_position":113,"commit_id":"8ef7fd75a7cc997b4c5dfd9d4d9e278dde165cab","original_commit_id":"079d109ee40d1be0ce6f5cfed5a091da41a477dc","user":{"login":"sudheeshkatkam","id":3272600,"avatar_url":"https://avatars.githubusercontent.com/u/3272600?v=3","gravatar_id":"","url":"https://api.github.com/users/sudheeshkatkam","html_url":"https://github.com/sudheeshkatkam","followers_url":"https://api.github.com/users/sudheeshkatkam/followers","following_url":"https://api.github.com/users/sudheeshkatkam/following{/other_user}","gists_url":"https://api.github.com/users/sudheeshkatkam/gists{/gist_id}","starred_url":"https://api.github.com/users/sudheeshkatkam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sudheeshkatkam/subscriptions","organizations_url":"https://api.github.com/users/sudheeshkatkam/orgs","repos_url":"https://api.github.com/users/sudheeshkatkam/repos","events_url":"https://api.github.com/users/sudheeshkatkam/events{/privacy}","received_events_url":"https://api.github.com/users/sudheeshkatkam/received_events","type":"User","site_admin":false},"body":"Few questions here:\r\n(1) How does this work when authentication is enabled?\r\n(2) Why does this use a listener from the rest package?\r\n(3) Are there alternative way to run this query?","created_at":"2016-05-18T23:16:35Z","updated_at":"2016-05-18T23:16:35Z","html_url":"https://github.com/apache/drill/pull/425#discussion_r63799682","pull_request_url":"https://api.github.com/repos/apache/drill/pulls/425","_links":{"self":{"href":"https://api.github.com/repos/apache/drill/pulls/comments/63799682"},"html":{"href":"https://github.com/apache/drill/pull/425#discussion_r63799682"},"pull_request":{"href":"https://api.github.com/repos/apache/drill/pulls/425"}}},"pull_request":{"url":"https://api.github.com/repos/apache/drill/pulls/425","id":62584355,"html_url":"https://github.com/apache/drill/pull/425","diff_url":"https://github.com/apache/drill/pull/425.diff","patch_url":"https://github.com/apache/drill/pull/425.patch","issue_url":"https://api.github.com/repos/apache/drill/issues/425","number":425,"state":"open","locked":false,"title":"DRILL-1328: Support table statistics","user":{"login":"vkorukanti","id":1719945,"avatar_url":"https://avatars.githubusercontent.com/u/1719945?v=3","gravatar_id":"","url":"https://api.github.com/users/vkorukanti","html_url":"https://github.com/vkorukanti","followers_url":"https://api.github.com/users/vkorukanti/followers","following_url":"https://api.github.com/users/vkorukanti/following{/other_user}","gists_url":"https://api.github.com/users/vkorukanti/gists{/gist_id}","starred_url":"https://api.github.com/users/vkorukanti/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkorukanti/subscriptions","organizations_url":"https://api.github.com/users/vkorukanti/orgs","repos_url":"https://api.github.com/users/vkorukanti/repos","events_url":"https://api.github.com/users/vkorukanti/events{/privacy}","received_events_url":"https://api.github.com/users/vkorukanti/received_events","type":"User","site_admin":false},"body":"Patch attached to the JIRA is seems to be useful for generating table stats and using them for query planning. I rebased the patch to latest master, fixed few issues and added few tests.\r\n\r\nIt still needs work to make it a full fledged feature, but I think the current state of the patch is good enough to commit and make improvements/fixes later.\r\n\r\n@jinfengni and @amansinha100 : Could you please review the patch?","created_at":"2016-03-11T18:39:35Z","updated_at":"2016-05-18T23:16:35Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4ec5a8d5f1368d00df26e1de74241bd37f6c0feb","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/drill/pulls/425/commits","review_comments_url":"https://api.github.com/repos/apache/drill/pulls/425/comments","review_comment_url":"https://api.github.com/repos/apache/drill/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/drill/issues/425/comments","statuses_url":"https://api.github.com/repos/apache/drill/statuses/8ef7fd75a7cc997b4c5dfd9d4d9e278dde165cab","head":{"label":"vkorukanti:DRILL-1328-r1","ref":"DRILL-1328-r1","sha":"8ef7fd75a7cc997b4c5dfd9d4d9e278dde165cab","user":{"login":"vkorukanti","id":1719945,"avatar_url":"https://avatars.githubusercontent.com/u/1719945?v=3","gravatar_id":"","url":"https://api.github.com/users/vkorukanti","html_url":"https://github.com/vkorukanti","followers_url":"https://api.github.com/users/vkorukanti/followers","following_url":"https://api.github.com/users/vkorukanti/following{/other_user}","gists_url":"https://api.github.com/users/vkorukanti/gists{/gist_id}","starred_url":"https://api.github.com/users/vkorukanti/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkorukanti/subscriptions","organizations_url":"https://api.github.com/users/vkorukanti/orgs","repos_url":"https://api.github.com/users/vkorukanti/repos","events_url":"https://api.github.com/users/vkorukanti/events{/privacy}","received_events_url":"https://api.github.com/users/vkorukanti/received_events","type":"User","site_admin":false},"repo":{"id":14540979,"name":"drill","full_name":"vkorukanti/drill","owner":{"login":"vkorukanti","id":1719945,"avatar_url":"https://avatars.githubusercontent.com/u/1719945?v=3","gravatar_id":"","url":"https://api.github.com/users/vkorukanti","html_url":"https://github.com/vkorukanti","followers_url":"https://api.github.com/users/vkorukanti/followers","following_url":"https://api.github.com/users/vkorukanti/following{/other_user}","gists_url":"https://api.github.com/users/vkorukanti/gists{/gist_id}","starred_url":"https://api.github.com/users/vkorukanti/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkorukanti/subscriptions","organizations_url":"https://api.github.com/users/vkorukanti/orgs","repos_url":"https://api.github.com/users/vkorukanti/repos","events_url":"https://api.github.com/users/vkorukanti/events{/privacy}","received_events_url":"https://api.github.com/users/vkorukanti/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/vkorukanti/drill","description":"Mirror of Apache Drill (Incubating)","fork":true,"url":"https://api.github.com/repos/vkorukanti/drill","forks_url":"https://api.github.com/repos/vkorukanti/drill/forks","keys_url":"https://api.github.com/repos/vkorukanti/drill/keys{/key_id}","collaborators_url":"https://api.github.com/repos/vkorukanti/drill/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/vkorukanti/drill/teams","hooks_url":"https://api.github.com/repos/vkorukanti/drill/hooks","issue_events_url":"https://api.github.com/repos/vkorukanti/drill/issues/events{/number}","events_url":"https://api.github.com/repos/vkorukanti/drill/events","assignees_url":"https://api.github.com/repos/vkorukanti/drill/assignees{/user}","branches_url":"https://api.github.com/repos/vkorukanti/drill/branches{/branch}","tags_url":"https://api.github.com/repos/vkorukanti/drill/tags","blobs_url":"https://api.github.com/repos/vkorukanti/drill/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/vkorukanti/drill/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/vkorukanti/drill/git/refs{/sha}","trees_url":"https://api.github.com/repos/vkorukanti/drill/git/trees{/sha}","statuses_url":"https://api.github.com/repos/vkorukanti/drill/statuses/{sha}","languages_url":"https://api.github.com/repos/vkorukanti/drill/languages","stargazers_url":"https://api.github.com/repos/vkorukanti/drill/stargazers","contributors_url":"https://api.github.com/repos/vkorukanti/drill/contributors","subscribers_url":"https://api.github.com/repos/vkorukanti/drill/subscribers","subscription_url":"https://api.github.com/repos/vkorukanti/drill/subscription","commits_url":"https://api.github.com/repos/vkorukanti/drill/commits{/sha}","git_commits_url":"https://api.github.com/repos/vkorukanti/drill/git/commits{/sha}","comments_url":"https://api.github.com/repos/vkorukanti/drill/comments{/number}","issue_comment_url":"https://api.github.com/repos/vkorukanti/drill/issues/comments{/number}","contents_url":"https://api.github.com/repos/vkorukanti/drill/contents/{+path}","compare_url":"https://api.github.com/repos/vkorukanti/drill/compare/{base}...{head}","merges_url":"https://api.github.com/repos/vkorukanti/drill/merges","archive_url":"https://api.github.com/repos/vkorukanti/drill/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/vkorukanti/drill/downloads","issues_url":"https://api.github.com/repos/vkorukanti/drill/issues{/number}","pulls_url":"https://api.github.com/repos/vkorukanti/drill/pulls{/number}","milestones_url":"https://api.github.com/repos/vkorukanti/drill/milestones{/number}","notifications_url":"https://api.github.com/repos/vkorukanti/drill/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/vkorukanti/drill/labels{/name}","releases_url":"https://api.github.com/repos/vkorukanti/drill/releases{/id}","deployments_url":"https://api.github.com/repos/vkorukanti/drill/deployments","created_at":"2013-11-19T23:19:09Z","updated_at":"2015-04-23T17:45:35Z","pushed_at":"2016-05-18T18:55:56Z","git_url":"git://github.com/vkorukanti/drill.git","ssh_url":"git@github.com:vkorukanti/drill.git","clone_url":"https://github.com/vkorukanti/drill.git","svn_url":"https://github.com/vkorukanti/drill","homepage":null,"size":22586,"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":"b075bf6102e1561ee3755b483e1d1e6018d7e505","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":5683653,"name":"drill","full_name":"apache/drill","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/drill","description":"Mirror of Apache Drill","fork":false,"url":"https://api.github.com/repos/apache/drill","forks_url":"https://api.github.com/repos/apache/drill/forks","keys_url":"https://api.github.com/repos/apache/drill/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/drill/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/drill/teams","hooks_url":"https://api.github.com/repos/apache/drill/hooks","issue_events_url":"https://api.github.com/repos/apache/drill/issues/events{/number}","events_url":"https://api.github.com/repos/apache/drill/events","assignees_url":"https://api.github.com/repos/apache/drill/assignees{/user}","branches_url":"https://api.github.com/repos/apache/drill/branches{/branch}","tags_url":"https://api.github.com/repos/apache/drill/tags","blobs_url":"https://api.github.com/repos/apache/drill/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/drill/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/drill/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/drill/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/drill/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/drill/languages","stargazers_url":"https://api.github.com/repos/apache/drill/stargazers","contributors_url":"https://api.github.com/repos/apache/drill/contributors","subscribers_url":"https://api.github.com/repos/apache/drill/subscribers","subscription_url":"https://api.github.com/repos/apache/drill/subscription","commits_url":"https://api.github.com/repos/apache/drill/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/drill/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/drill/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/drill/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/drill/contents/{+path}","compare_url":"https://api.github.com/repos/apache/drill/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/drill/merges","archive_url":"https://api.github.com/repos/apache/drill/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/drill/downloads","issues_url":"https://api.github.com/repos/apache/drill/issues{/number}","pulls_url":"https://api.github.com/repos/apache/drill/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/drill/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/drill/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/drill/labels{/name}","releases_url":"https://api.github.com/repos/apache/drill/releases{/id}","deployments_url":"https://api.github.com/repos/apache/drill/deployments","created_at":"2012-09-05T07:00:26Z","updated_at":"2016-05-17T16:22:08Z","pushed_at":"2016-05-18T18:55:56Z","git_url":"git://github.com/apache/drill.git","ssh_url":"git@github.com:apache/drill.git","clone_url":"https://github.com/apache/drill.git","svn_url":"https://github.com/apache/drill","homepage":"","size":48814,"stargazers_count":617,"watchers_count":617,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":433,"mirror_url":"git://git.apache.org/drill.git","open_issues_count":111,"forks":433,"open_issues":111,"watchers":617,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/drill/pulls/425"},"html":{"href":"https://github.com/apache/drill/pull/425"},"issue":{"href":"https://api.github.com/repos/apache/drill/issues/425"},"comments":{"href":"https://api.github.com/repos/apache/drill/issues/425/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/drill/pulls/425/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/drill/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/drill/pulls/425/commits"},"statuses":{"href":"https://api.github.com/repos/apache/drill/statuses/8ef7fd75a7cc997b4c5dfd9d4d9e278dde165cab"}}}}