You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by we...@apache.org on 2024/01/05 14:57:43 UTC

(spark) branch master updated: [SPARK-46602][SQL] Propagate `allowExisting` in view creation when the view/table does not exists

This is an automated email from the ASF dual-hosted git repository.

wenchen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new 9b3c70f6094c [SPARK-46602][SQL] Propagate `allowExisting` in view creation when the view/table does not exists
9b3c70f6094c is described below

commit 9b3c70f6094c97ed61018d9fca8a50320574ab49
Author: Xinyi Yu <xi...@databricks.com>
AuthorDate: Fri Jan 5 22:57:28 2024 +0800

    [SPARK-46602][SQL] Propagate `allowExisting` in view creation when the view/table does not exists
    
    ### What changes were proposed in this pull request?
    This PR fixes the undesired behavior that concurrent `CREATE VIEW IF NOT EXISTS` queries could throw `TABLE_OR_VIEW_ALREADY_EXISTS` exceptions. It's because the current implementation did not propagate the 'IF NOT EXISTS' when the detecting view/table does not exists.
    
    ### Why are the changes needed?
    Fix the above issue.
    
    ### Does this PR introduce _any_ user-facing change?
    Yes in the sense that if fixes an issue in concurrent case.
    
    ### How was this patch tested?
    Without the fix the following test failed while with this PR if passed. But following the [comment](https://github.com/apache/spark/pull/44603#discussion_r1442515458), I removed the test from this PR.
    ```scala
      test("CREATE VIEW IF NOT EXISTS never throws TABLE_OR_VIEW_ALREADY_EXISTS") {
        // Concurrently create a view with the same name, so that some of the queries may all
        // get that the view does not exist and try to create it. But with IF NOT EXISTS, the
        // queries should not fail.
        import ExecutionContext.Implicits.global
        val concurrency = 10
        val tableName = "table_name"
        val viewName = "view_name"
        withTable(tableName) {
          sql(s"CREATE TABLE $tableName (id int) USING parquet")
          withView("view_name") {
            val futures = (0 to concurrency).map { _ =>
              Future {
                Try {
                  sql(s"CREATE VIEW IF NOT EXISTS $viewName AS SELECT * FROM $tableName")
                }
              }
            }
            futures.map { future =>
               val res = ThreadUtils.awaitResult(future, 5.seconds)
               assert(
                 res.isSuccess,
                 s"Failed to create view: ${if (res.isFailure) res.failed.get.getMessage}"
               )
            }
          }
        }
      }
    ```
    
    ### Was this patch authored or co-authored using generative AI tooling?
    No.
    
    Closes #44603 from anchovYu/create-view-if-not-exist-fix.
    
    Authored-by: Xinyi Yu <xi...@databricks.com>
    Signed-off-by: Wenchen Fan <we...@databricks.com>
---
 .../src/main/scala/org/apache/spark/sql/execution/command/views.scala   | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/command/views.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/views.scala
index 6b0a1c34ed2b..3a761541a00e 100644
--- a/sql/core/src/main/scala/org/apache/spark/sql/execution/command/views.scala
+++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/views.scala
@@ -168,7 +168,7 @@ case class CreateViewCommand(
       }
     } else {
       // Create the view if it doesn't exist.
-      catalog.createTable(prepareTable(sparkSession, analyzedPlan), ignoreIfExists = false)
+      catalog.createTable(prepareTable(sparkSession, analyzedPlan), ignoreIfExists = allowExisting)
     }
     Seq.empty[Row]
   }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org