Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
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
12 changes: 12 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/SQLContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,18 @@ class SQLContext private[sql](
sparkSession.catalog.clearCache()
}

/**
* Invalidate and refresh all the cached the metadata of the given table. For performance reasons,
* Spark SQL or the external data source library it uses might cache certain metadata about a
* table, such as the location of blocks. When those change outside of Spark SQL, users should
* call this function to invalidate the cache.
*
* @since 1.3.0
*/
def refreshTable(tableName: String): Unit = {
sparkSession.catalog.refreshTable(tableName)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This method did not exist in SQLContext. It's in HiveContext.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, will remove it. Thanks!


// scalastyle:off
// Disable style checker so "implicits" object can start with lowercase i
/**
Expand Down
10 changes: 10 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/catalog/Catalog.scala
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,14 @@ abstract class Catalog {
*/
def clearCache(): Unit

/**
* Invalidate and refresh all the cached the metadata of the given table. For performance reasons,
* Spark SQL or the external data source library it uses might cache certain metadata about a
* table, such as the location of blocks. When those change outside of Spark SQL, users should
* call this function to invalidate the cache.
*
* @since 2.0.0
*/
def refreshTable(tableName: String): Unit

}
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,17 @@ class CatalogImpl(sparkSession: SparkSession) extends Catalog {
sparkSession.cacheManager.lookupCachedData(qName).nonEmpty
}

/**
* Refresh the cache entry for a metastore table, if any.
*
* @group cachemgmt
* @since 2.0.0
*/
override def refreshTable(tableName: String): Unit = {
val tableIdent = sparkSession.sessionState.sqlParser.parseTableIdentifier(tableName)
sessionCatalog.refreshTable(tableIdent)
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ class MetastoreDataSourcesSuite extends QueryTest with SQLTestUtils with TestHiv
.mode(SaveMode.Append)
.saveAsTable("arrayInParquet")

sessionState.refreshTable("arrayInParquet")
refreshTable("arrayInParquet")

checkAnswer(
sql("SELECT a FROM arrayInParquet"),
Expand Down Expand Up @@ -681,7 +681,7 @@ class MetastoreDataSourcesSuite extends QueryTest with SQLTestUtils with TestHiv
.mode(SaveMode.Append)
.saveAsTable("mapInParquet")

sessionState.refreshTable("mapInParquet")
refreshTable("mapInParquet")

checkAnswer(
sql("SELECT a FROM mapInParquet"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ class MultiDatabaseSuite extends QueryTest with SQLTestUtils with TestHiveSingle

activateDatabase(db) {
sql(
s"""CREATE EXTERNAL TABLE t (id BIGINT)
s"""
|CREATE EXTERNAL TABLE t (id BIGINT)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems these formatting changes are not necessary?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Will revert them back. Thanks!

|PARTITIONED BY (p INT)
|STORED AS PARQUET
|LOCATION '$path'
Expand All @@ -217,7 +218,7 @@ class MultiDatabaseSuite extends QueryTest with SQLTestUtils with TestHiveSingle

df.write.parquet(s"$path/p=2")
sql("ALTER TABLE t ADD PARTITION (p=2)")
hiveContext.sessionState.refreshTable("t")
hiveContext.refreshTable("t")
checkAnswer(
spark.table("t"),
df.withColumn("p", lit(1)).union(df.withColumn("p", lit(2))))
Expand All @@ -234,11 +235,12 @@ class MultiDatabaseSuite extends QueryTest with SQLTestUtils with TestHiveSingle
val path = dir.getCanonicalPath

sql(
s"""CREATE EXTERNAL TABLE $db.t (id BIGINT)
|PARTITIONED BY (p INT)
|STORED AS PARQUET
|LOCATION '$path'
""".stripMargin)
s"""
|CREATE EXTERNAL TABLE $db.t (id BIGINT)
|PARTITIONED BY (p INT)
|STORED AS PARQUET
|LOCATION '$path'
""".stripMargin)

checkAnswer(spark.table(s"$db.t"), spark.emptyDataFrame)

Expand All @@ -249,7 +251,7 @@ class MultiDatabaseSuite extends QueryTest with SQLTestUtils with TestHiveSingle

df.write.parquet(s"$path/p=2")
sql(s"ALTER TABLE $db.t ADD PARTITION (p=2)")
hiveContext.sessionState.refreshTable(s"$db.t")
hiveContext.refreshTable(s"$db.t")
checkAnswer(
spark.table(s"$db.t"),
df.withColumn("p", lit(1)).union(df.withColumn("p", lit(2))))
Expand Down Expand Up @@ -279,11 +281,11 @@ class MultiDatabaseSuite extends QueryTest with SQLTestUtils with TestHiveSingle
val message = intercept[AnalysisException] {
sql(
s"""
|CREATE TABLE `d:b`.`t:a` (a int)
|USING parquet
|OPTIONS (
| path '$path'
|)
|CREATE TABLE `d:b`.`t:a` (a int)
|USING parquet
|OPTIONS (
| path '$path'
|)
""".stripMargin)
}.getMessage
assert(message.contains("is not a valid name for metastore"))
Expand All @@ -293,12 +295,12 @@ class MultiDatabaseSuite extends QueryTest with SQLTestUtils with TestHiveSingle
val message = intercept[AnalysisException] {
sql(
s"""
|CREATE TABLE `d:b`.`table` (a int)
|USING parquet
|OPTIONS (
| path '$path'
|)
""".stripMargin)
|CREATE TABLE `d:b`.`table` (a int)
|USING parquet
|OPTIONS (
| path '$path'
|)
""".stripMargin)
}.getMessage
assert(message.contains("is not a valid name for metastore"))
}
Expand Down