Skip to content
Closed
Show file tree
Hide file tree
Changes from 10 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
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,6 @@ class SessionCatalog(
requireDbExists(db)
requireTableExists(tableIdentifier)
externalCatalog.alterTableStats(db, table, newStats)
// Invalidate the table relation cache
refreshTable(identifier)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ case class AnalyzeColumnCommand(
colStats = tableMeta.stats.map(_.colStats).getOrElse(Map.empty) ++ newColStats)

sessionState.catalog.alterTableStats(tableIdentWithDB, Some(statistics))
sessionState.catalog.refreshTable(tableIdentWithDB)

Seq.empty[Row]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ case class AnalyzeTableCommand(
val newStats = CommandUtils.compareAndGetNewStats(tableMeta.stats, newTotalSize, newRowCount)
if (newStats.isDefined) {
sessionState.catalog.alterTableStats(tableIdentWithDB, newStats)
sessionState.catalog.refreshTable(tableIdentWithDB)
}

Seq.empty[Row]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ case class AlterTableAddPartitionCommand(
catalog.alterTableStats(table.identifier, None)
}
}
catalog.refreshTable(table.identifier)
Seq.empty[Row]
}

Expand Down Expand Up @@ -548,6 +549,7 @@ case class AlterTableDropPartitionCommand(
retainData = retainData)

CommandUtils.updateTableStats(sparkSession, table)
catalog.refreshTable(table.identifier)

Seq.empty[Row]
}
Expand Down Expand Up @@ -800,6 +802,7 @@ case class AlterTableSetLocationCommand(
}

CommandUtils.updateTableStats(sparkSession, table)
catalog.refreshTable(table.identifier)
Seq.empty[Row]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ case class TruncateTableCommand(
val newStats = CatalogStatistics(sizeInBytes = 0, rowCount = Some(0))
catalog.alterTableStats(tableName, Some(newStats))
}
catalog.refreshTable(table.identifier)
Seq.empty[Row]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ case class InsertIntoHadoopFsRelationCommand(
sparkSession.catalog.refreshByPath(outputPath.toString)

if (catalogTable.nonEmpty) {
sparkSession.sessionState.catalog.refreshTable(catalogTable.get.identifier)
Comment thread
wangyum marked this conversation as resolved.
Outdated
CommandUtils.updateTableStats(sparkSession, catalogTable.get)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we switch line 193 and 192?

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.

There is a issue here: It may refresh the table twice. I moved this logical to CommandUtils.updateTableStats:

// In other cases, we still need to invalidate the table relation cache.
catalog.refreshTable(table.identifier)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import org.scalatest.BeforeAndAfterEach

import org.apache.spark.internal.config
import org.apache.spark.sql.{AnalysisException, QueryTest, Row, SaveMode}
import org.apache.spark.sql.catalyst.TableIdentifier
import org.apache.spark.sql.catalyst.{QualifiedTableName, TableIdentifier}
import org.apache.spark.sql.catalyst.analysis.{FunctionRegistry, NoSuchPartitionException, NoSuchTableException, TempTableAlreadyExistsException}
import org.apache.spark.sql.catalyst.catalog._
import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec
Expand Down Expand Up @@ -167,6 +167,37 @@ class InMemoryCatalogedDDLSuite extends DDLSuite with SharedSQLContext with Befo
assert(e.message.contains("It doesn't match the specified format"))
}
}

test("SPARK-25403 refresh the table after inserting data") {
withTable("t") {
val catalog = spark.sessionState.catalog
val table = QualifiedTableName(catalog.getCurrentDatabase, "t")
sql("CREATE TABLE t (a INT) USING parquet")
sql("INSERT INTO TABLE t VALUES (1)")
assert(catalog.getCachedTable(table) === null, "Table relation should be invalidated.")
assert(spark.table("t").count() === 1)
assert(catalog.getCachedTable(table) !== null, "Table relation should be cached.")
}
}

test("SPARK-19784 refresh the table after altering the table location") {
withTable("t") {
withTempDir { dir =>
val catalog = spark.sessionState.catalog
val table = QualifiedTableName(catalog.getCurrentDatabase, "t")
val p1 = s"${dir.getCanonicalPath}/p1"
val p2 = s"${dir.getCanonicalPath}/p2"
sql(s"CREATE TABLE t (a INT) USING parquet LOCATION '$p1'")
sql("INSERT INTO TABLE t VALUES (1)")
assert(catalog.getCachedTable(table) === null, "Table relation should be invalidated.")
spark.range(5).toDF("a").write.parquet(p2)
spark.sql(s"ALTER TABLE t SET LOCATION '$p2'")
assert(catalog.getCachedTable(table) === null, "Table relation should be invalidated.")
assert(spark.table("t").count() === 5)
assert(catalog.getCachedTable(table) !== null, "Table relation should be cached.")
}
}
}
}

abstract class DDLSuite extends QueryTest with SQLTestUtils {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ class HiveParquetMetastoreSuite extends ParquetPartitioningTest {
def checkCached(tableIdentifier: TableIdentifier): Unit = {
// Converted test_parquet should be cached.
getCachedDataSourceTable(tableIdentifier) match {
case null => fail("Converted test_parquet should be cached in the cache.")
case null => fail(s"Converted ${tableIdentifier.table} should be cached in the cache.")
case LogicalRelation(_: HadoopFsRelation, _, _, _) => // OK
case other =>
fail(
Expand Down Expand Up @@ -480,7 +480,7 @@ class HiveParquetMetastoreSuite extends ParquetPartitioningTest {
|INSERT INTO TABLE test_insert_parquet
|select a, b from jt
""".stripMargin)
checkCached(tableIdentifier)
assert(getCachedDataSourceTable(tableIdentifier) === null)
// Make sure we can read the data.
checkAnswer(
sql("select * from test_insert_parquet"),
Expand Down