{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/82517080","pull_request_review_id":3421097,"id":82517080,"diff_hunk":"@@ -0,0 +1,121 @@\n+/*\n+ * Licensed to the Apache Software Foundation (ASF) under one or more\n+ * contributor license agreements.  See the NOTICE file distributed with\n+ * this work for additional information regarding copyright ownership.\n+ * The ASF licenses this file to You under the Apache License, Version 2.0\n+ * (the \"License\"); you may not use this file except in compliance with\n+ * 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, 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+\n+package org.apache.spark.sql.catalyst.catalog\n+\n+import javax.annotation.concurrent.GuardedBy\n+\n+import scala.collection.mutable\n+\n+import org.apache.spark.sql.AnalysisException\n+import org.apache.spark.sql.catalyst.analysis.TempTableAlreadyExistsException\n+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan\n+import org.apache.spark.sql.catalyst.util.StringUtils\n+\n+\n+/**\n+ * A thread-safe manager for global temporary views, providing atomic operations to manage them,\n+ * e.g. create, update, remove, etc.\n+ *\n+ * Note that, the view name is always case-sensitive here, callers are responsible to format the\n+ * view name w.r.t. case-sensitive config.\n+ *\n+ * @param database The system preserved virtual database that keeps all the global temporary views.\n+ */\n+class GlobalTempViewManager(val database: String) {\n+\n+  /** List of view definitions, mapping from view name to logical plan. */\n+  @GuardedBy(\"this\")\n+  private val viewDefinitions = new mutable.HashMap[String, LogicalPlan]\n+\n+  /**\n+   * Returns the global view definition which matches the given name, or None if not found.\n+   */\n+  def get(name: String): Option[LogicalPlan] = synchronized {\n+    viewDefinitions.get(name)\n+  }\n+\n+  /**\n+   * Creates a global temp view, or issue an exception if the view already exists and\n+   * `overrideIfExists` is false.\n+   */\n+  def create(\n+      name: String,\n+      viewDefinition: LogicalPlan,\n+      overrideIfExists: Boolean): Unit = synchronized {\n+    if (!overrideIfExists && viewDefinitions.contains(name)) {\n+      throw new TempTableAlreadyExistsException(name)\n+    }\n+    viewDefinitions.put(name, viewDefinition)\n+  }\n+\n+  /**\n+   * Updates the global temp view if it exists, returns true if updated, false otherwise.\n+   */\n+  def update(\n+      name: String,\n+      viewDefinition: LogicalPlan): Boolean = synchronized {\n+    if (viewDefinitions.contains(name)) {\n+      viewDefinitions.put(name, viewDefinition)\n+      true\n+    } else {\n+      false\n+    }\n+  }\n+\n+  /**\n+   * Removes the global temp view if it exists, returns true if removed, false otherwise.\n+   */\n+  def remove(name: String): Boolean = synchronized {\n+    viewDefinitions.remove(name).isDefined\n+  }\n+\n+  /**\n+   * Renames the global temp view if the source view exists and the destination view not exists, or\n+   * issue an exception if the source view exists but the destination view already exists. Returns\n+   * true if renamed, false otherwise.\n+   */\n+  def rename(oldName: String, newName: String): Boolean = synchronized {\n+    if (viewDefinitions.contains(oldName)) {\n+      if (viewDefinitions.contains(newName)) {\n+        throw new AnalysisException(\n+          s\"rename temporary view from '$oldName' to '$newName': destination view already exists\")\n+      }\n+\n+      val viewDefinition = viewDefinitions(oldName)\n+      viewDefinitions.remove(oldName)\n+      viewDefinitions.put(newName, viewDefinition)\n+      true\n+    } else {\n+      false\n+    }","path":"sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/GlobalTempViewManager.scala","position":105,"original_position":105,"commit_id":"cbbe122299a690cba7aff6c1a320d366513d42c9","original_commit_id":"cbbe122299a690cba7aff6c1a320d366513d42c9","user":{"login":"cloud-fan","id":3182036,"avatar_url":"https://avatars.githubusercontent.com/u/3182036?v=3","gravatar_id":"","url":"https://api.github.com/users/cloud-fan","html_url":"https://github.com/cloud-fan","followers_url":"https://api.github.com/users/cloud-fan/followers","following_url":"https://api.github.com/users/cloud-fan/following{/other_user}","gists_url":"https://api.github.com/users/cloud-fan/gists{/gist_id}","starred_url":"https://api.github.com/users/cloud-fan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cloud-fan/subscriptions","organizations_url":"https://api.github.com/users/cloud-fan/orgs","repos_url":"https://api.github.com/users/cloud-fan/repos","events_url":"https://api.github.com/users/cloud-fan/events{/privacy}","received_events_url":"https://api.github.com/users/cloud-fan/received_events","type":"User","site_admin":false},"body":"This behaviour comes from the previous temp view: If the source doesn't exist, try persisted table/view, if source exists but destination already exists, throw an error.","created_at":"2016-10-09T07:47:31Z","updated_at":"2016-10-09T07:47:32Z","html_url":"https://github.com/apache/spark/pull/14897#discussion_r82517080","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/14897","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/82517080"},"html":{"href":"https://github.com/apache/spark/pull/14897#discussion_r82517080"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/14897"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/14897","id":83467564,"html_url":"https://github.com/apache/spark/pull/14897","diff_url":"https://github.com/apache/spark/pull/14897.diff","patch_url":"https://github.com/apache/spark/pull/14897.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/14897","number":14897,"state":"open","locked":false,"title":"[SPARK-17338][SQL] add global temp view","user":{"login":"cloud-fan","id":3182036,"avatar_url":"https://avatars.githubusercontent.com/u/3182036?v=3","gravatar_id":"","url":"https://api.github.com/users/cloud-fan","html_url":"https://github.com/cloud-fan","followers_url":"https://api.github.com/users/cloud-fan/followers","following_url":"https://api.github.com/users/cloud-fan/following{/other_user}","gists_url":"https://api.github.com/users/cloud-fan/gists{/gist_id}","starred_url":"https://api.github.com/users/cloud-fan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cloud-fan/subscriptions","organizations_url":"https://api.github.com/users/cloud-fan/orgs","repos_url":"https://api.github.com/users/cloud-fan/repos","events_url":"https://api.github.com/users/cloud-fan/events{/privacy}","received_events_url":"https://api.github.com/users/cloud-fan/received_events","type":"User","site_admin":false},"body":"## What changes were proposed in this pull request?\r\n\r\nGlobal temporary view is a cross-session temporary view, which means it's shared among all sessions. Its lifetime is the lifetime of the Spark application, i.e. it will be automatically dropped when the application terminates. It's tied to a system preserved database `global_temp`(configurable via SparkConf), and we must use the qualified name to refer a global temp view, e.g. SELECT * FROM global_temp.view1.\r\n\r\nchanges for `SessionCatalog`:\r\n\r\n1. add a new field `gloabalTempViews: GlobalTempViewManager`, to access the shared global temp views, and the global temp db name.\r\n2. `createDatabase` will fail if users wanna create `global_temp`, which is system preserved.\r\n3. `setCurrentDatabase` will fail if users wanna set `global_temp`, which is system preserved.\r\n4. add `createGlobalTempView`, which is used in `CreateViewCommand` to create global temp views.\r\n5. add `dropGlobalTempView`, which is used in `CatalogImpl` to drop global temp view.\r\n6. add `alterTempViewDefinition`, which is used in `AlterViewAsCommand` to update the view definition for local/global temp views.\r\n7. `renameTable`/`dropTable`/`isTemporaryTable`/`lookupRelation`/`getTempViewOrPermanentTableMetadata`/`refreshTable` will handle global temp views.\r\n\r\nchanges for SQL commands:\r\n\r\n1. `CreateViewCommand`/`AlterViewAsCommand` is updated to support global temp views\r\n2. `ShowTablesCommand` outputs a new column `database`, which is used to distinguish global and local temp views.\r\n3. other commands can also handle global temp views if they call `SessionCatalog` APIs which accepts global temp views, e.g. `DropTableCommand`, `AlterTableRenameCommand`, `ShowColumnsCommand`, etc.\r\n\r\nchanges for other public API\r\n\r\n1. add a new method `dropGlobalTempView` in `Catalog`\r\n2. `Catalog.findTable` can find global temp view\r\n3. add a new method `createGlobalTempView` in `Dataset`\r\n\r\n## How was this patch tested?\r\n\r\nnew tests in `SQLViewSuite`","created_at":"2016-08-31T14:49:29Z","updated_at":"2016-10-09T07:47:32Z","closed_at":null,"merged_at":null,"merge_commit_sha":"841c2ca7c43d18cbcfd6872c55ce1af012dda6a3","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/14897/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/14897/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/14897/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/cbbe122299a690cba7aff6c1a320d366513d42c9","head":{"label":"cloud-fan:global-temp-view","ref":"global-temp-view","sha":"cbbe122299a690cba7aff6c1a320d366513d42c9","user":{"login":"cloud-fan","id":3182036,"avatar_url":"https://avatars.githubusercontent.com/u/3182036?v=3","gravatar_id":"","url":"https://api.github.com/users/cloud-fan","html_url":"https://github.com/cloud-fan","followers_url":"https://api.github.com/users/cloud-fan/followers","following_url":"https://api.github.com/users/cloud-fan/following{/other_user}","gists_url":"https://api.github.com/users/cloud-fan/gists{/gist_id}","starred_url":"https://api.github.com/users/cloud-fan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cloud-fan/subscriptions","organizations_url":"https://api.github.com/users/cloud-fan/orgs","repos_url":"https://api.github.com/users/cloud-fan/repos","events_url":"https://api.github.com/users/cloud-fan/events{/privacy}","received_events_url":"https://api.github.com/users/cloud-fan/received_events","type":"User","site_admin":false},"repo":{"id":19689699,"name":"spark","full_name":"cloud-fan/spark","owner":{"login":"cloud-fan","id":3182036,"avatar_url":"https://avatars.githubusercontent.com/u/3182036?v=3","gravatar_id":"","url":"https://api.github.com/users/cloud-fan","html_url":"https://github.com/cloud-fan","followers_url":"https://api.github.com/users/cloud-fan/followers","following_url":"https://api.github.com/users/cloud-fan/following{/other_user}","gists_url":"https://api.github.com/users/cloud-fan/gists{/gist_id}","starred_url":"https://api.github.com/users/cloud-fan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cloud-fan/subscriptions","organizations_url":"https://api.github.com/users/cloud-fan/orgs","repos_url":"https://api.github.com/users/cloud-fan/repos","events_url":"https://api.github.com/users/cloud-fan/events{/privacy}","received_events_url":"https://api.github.com/users/cloud-fan/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/cloud-fan/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/cloud-fan/spark","forks_url":"https://api.github.com/repos/cloud-fan/spark/forks","keys_url":"https://api.github.com/repos/cloud-fan/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cloud-fan/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cloud-fan/spark/teams","hooks_url":"https://api.github.com/repos/cloud-fan/spark/hooks","issue_events_url":"https://api.github.com/repos/cloud-fan/spark/issues/events{/number}","events_url":"https://api.github.com/repos/cloud-fan/spark/events","assignees_url":"https://api.github.com/repos/cloud-fan/spark/assignees{/user}","branches_url":"https://api.github.com/repos/cloud-fan/spark/branches{/branch}","tags_url":"https://api.github.com/repos/cloud-fan/spark/tags","blobs_url":"https://api.github.com/repos/cloud-fan/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cloud-fan/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cloud-fan/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/cloud-fan/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cloud-fan/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/cloud-fan/spark/languages","stargazers_url":"https://api.github.com/repos/cloud-fan/spark/stargazers","contributors_url":"https://api.github.com/repos/cloud-fan/spark/contributors","subscribers_url":"https://api.github.com/repos/cloud-fan/spark/subscribers","subscription_url":"https://api.github.com/repos/cloud-fan/spark/subscription","commits_url":"https://api.github.com/repos/cloud-fan/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/cloud-fan/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/cloud-fan/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/cloud-fan/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/cloud-fan/spark/contents/{+path}","compare_url":"https://api.github.com/repos/cloud-fan/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cloud-fan/spark/merges","archive_url":"https://api.github.com/repos/cloud-fan/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cloud-fan/spark/downloads","issues_url":"https://api.github.com/repos/cloud-fan/spark/issues{/number}","pulls_url":"https://api.github.com/repos/cloud-fan/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/cloud-fan/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/cloud-fan/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cloud-fan/spark/labels{/name}","releases_url":"https://api.github.com/repos/cloud-fan/spark/releases{/id}","deployments_url":"https://api.github.com/repos/cloud-fan/spark/deployments","created_at":"2014-05-12T07:32:26Z","updated_at":"2016-05-26T10:38:18Z","pushed_at":"2016-10-03T15:40:30Z","git_url":"git://github.com/cloud-fan/spark.git","ssh_url":"git@github.com:cloud-fan/spark.git","clone_url":"https://github.com/cloud-fan/spark.git","svn_url":"https://github.com/cloud-fan/spark","homepage":null,"size":179351,"stargazers_count":1,"watchers_count":1,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"af6ece33d39cf305bd4a211d08a2f8e910c69bc1","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}","deployments_url":"https://api.github.com/repos/apache/spark/deployments","created_at":"2014-02-25T08:00:08Z","updated_at":"2016-10-09T06:58:37Z","pushed_at":"2016-10-09T07:02:04Z","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":531516,"stargazers_count":10282,"watchers_count":10282,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":9560,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":474,"forks":9560,"open_issues":474,"watchers":10282,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/14897"},"html":{"href":"https://github.com/apache/spark/pull/14897"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/14897"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/14897/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/14897/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/14897/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/cbbe122299a690cba7aff6c1a320d366513d42c9"}}}}
{"actor":{"display_login":"cloud-fan"}}