Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-34076][SQL] SQLContext.dropTempTable fails if cache is non-empty #31136

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,9 @@ class CatalogImpl(sparkSession: SparkSession) extends Catalog {
* @since 2.0.0
*/
override def dropTempView(viewName: String): Boolean = {
sparkSession.sessionState.catalog.getTempView(viewName).exists { viewDef =>
sparkSession.sessionState.catalog.getTempView(viewName).exists { _ =>
sparkSession.sharedState.cacheManager.uncacheQuery(
sparkSession, viewDef, cascade = false)
sparkSession.table(viewName), cascade = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we add an assert into uncacheQuery to make sure all passed in logical plans are analyzed? e.g.,

def uncacheQuery(
      spark: SparkSession,
      plan: LogicalPlan,
      cascade: Boolean,
      blocking: Boolean = false): Unit = {
    assert(plan.analyzed, "the plan input to `uncacheQuery` should be analyzed")
   ...
}

Copy link
Member Author

@sunchao sunchao Jan 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not totally sure about this. It seems common that a temporary view is dropped after its dependent view/table is already gone, as demonstrated by the test failures, so this assertion will fail in those cases.

Another approach is to ignore the possible non-fatal errors during query analysis and uncache, similar to what's done in DropTableCommand.

sessionCatalog.dropTempView(viewName)
}
}
Expand All @@ -411,9 +411,11 @@ class CatalogImpl(sparkSession: SparkSession) extends Catalog {
* @since 2.1.0
*/
override def dropGlobalTempView(viewName: String): Boolean = {
sparkSession.sessionState.catalog.getGlobalTempView(viewName).exists { viewDef =>
sparkSession.sessionState.catalog.getGlobalTempView(viewName).exists { _ =>
val db = sparkSession.sessionState.conf.getConf(StaticSQLConf.GLOBAL_TEMP_DATABASE)
val ident = TableIdentifier(viewName, Some(db))
sparkSession.sharedState.cacheManager.uncacheQuery(
sparkSession, viewDef, cascade = false)
sparkSession.table(ident), cascade = false)
sessionCatalog.dropGlobalTempView(viewName)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,28 @@ class CachedTableSuite extends QueryTest with SQLTestUtils with TestHiveSingleto
}
}
}

test("SPARK-34076: should be able to drop temp view with cached tables") {
val t = "cachedTable"
val v = "tempView"
withTable(t) {
withTempView(v) {
sql(s"CREATE TEMPORARY VIEW $v AS SELECT key FROM src LIMIT 10")
sql(s"CREATE TABLE $t AS SELECT * FROM src")
sql(s"CACHE TABLE $t")
}
}
}

test("SPARK-34076: should be able to drop global temp view with cached tables") {
val t = "cachedTable"
val v = "globalTempView"
withTable(t) {
withGlobalTempView(v) {
sql(s"CREATE GLOBAL TEMPORARY VIEW $v AS SELECT key FROM src LIMIT 10")
sql(s"CREATE TABLE $t AS SELECT * FROM src")
sql(s"CACHE TABLE $t")
}
}
}
}