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-34060][SQL] Fix Hive table caching while updating stats by ALTER TABLE .. DROP PARTITION #31112

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -510,6 +510,30 @@ object CatalogTable {
propKey == originalKey || propKey == s"$originalKey.numParts" ||
propKey.startsWith(s"$originalKey.part.")
}

def normalize(table: CatalogTable): CatalogTable = {
val nondeterministicProps = Set(
"CreateTime",
"transient_lastDdlTime",
"grantTime",
"lastUpdateTime",
"last_modified_by",
"last_modified_time",
"Owner:",
// The following are hive specific schema parameters which we do not need to match exactly.
"totalNumberFiles",
"maxFileSize",
"minFileSize"
)

table.copy(
createTime = 0L,
lastAccessTime = 0L,
properties = table.properties.filterKeys(!nondeterministicProps.contains(_)).toMap,
stats = None,
ignoredProperties = Map.empty
)
}
}

/**
Expand Down Expand Up @@ -781,10 +805,7 @@ case class HiveTableRelation(
def isPartitioned: Boolean = partitionCols.nonEmpty

override def doCanonicalize(): HiveTableRelation = copy(
tableMeta = tableMeta.copy(
storage = CatalogStorageFormat.empty,
createTime = -1
),
tableMeta = CatalogTable.normalize(tableMeta),
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the bug fix. Cleaning of storage and createTime is not enough. tableMeta can have other "temporary" fields.

dataCols = dataCols.zipWithIndex.map {
case (attr, index) => attr.withExprId(ExprId(index))
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,29 +223,6 @@ abstract class ShowCreateTableSuite extends QueryTest with SQLTestUtils {
}

protected def checkCatalogTables(expected: CatalogTable, actual: CatalogTable): Unit = {
def normalize(table: CatalogTable): CatalogTable = {
val nondeterministicProps = Set(
"CreateTime",
"transient_lastDdlTime",
"grantTime",
"lastUpdateTime",
"last_modified_by",
"last_modified_time",
"Owner:",
// The following are hive specific schema parameters which we do not need to match exactly.
"totalNumberFiles",
"maxFileSize",
"minFileSize"
)

table.copy(
createTime = 0L,
lastAccessTime = 0L,
properties = table.properties.filterKeys(!nondeterministicProps.contains(_)).toMap,
stats = None,
ignoredProperties = Map.empty
)
}
assert(normalize(actual) == normalize(expected))
assert(CatalogTable.normalize(actual) == CatalogTable.normalize(expected))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.apache.spark.sql.hive.execution.command

import org.apache.spark.sql.Row
import org.apache.spark.sql.execution.command.v1
import org.apache.spark.sql.internal.SQLConf

/**
* The class contains tests for the `ALTER TABLE .. DROP PARTITION` command to check
Expand All @@ -42,4 +44,34 @@ class AlterTableDropPartitionSuite
}
}
}

test("SPARK-34060: update stats of cached table") {
withSQLConf(SQLConf.AUTO_SIZE_UPDATE_ENABLED.key -> "true") {
withNamespaceAndTable("ns", "tbl") { t =>
def checkTableSize(expected: String): Unit = {
val stats =
sql(s"DESCRIBE TABLE EXTENDED $t")
.select("data_type")
.where("col_name = 'Statistics'")
.first()
.getString(0)
assert(stats.contains(expected))
}

sql(s"CREATE TABLE $t (id int, part int) $defaultUsing PARTITIONED BY (part)")
sql(s"INSERT INTO $t PARTITION (part=0) SELECT 0")
sql(s"INSERT INTO $t PARTITION (part=1) SELECT 1")
assert(!spark.catalog.isCached(t))
sql(s"CACHE TABLE $t")
assert(spark.catalog.isCached(t))
checkAnswer(sql(s"SELECT * FROM $t"), Seq(Row(0, 0), Row(1, 1)))
checkTableSize("4 bytes")

sql(s"ALTER TABLE $t DROP PARTITION (part=0)")
assert(spark.catalog.isCached(t))
checkTableSize("2 bytes")
checkAnswer(sql(s"SELECT * FROM $t"), Seq(Row(1, 1)))
}
}
}
}