Skip to content
Closed
Show file tree
Hide file tree
Changes from 7 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 @@ -721,19 +721,18 @@ private[spark] class HiveExternalCatalog(conf: SparkConf, hadoopConf: Configurat
table: String,
stats: Option[CatalogStatistics]): Unit = withClient {
requireTableExists(db, table)
val rawTable = getRawTable(db, table)

// convert table statistics to properties so that we can persist them through hive client
val statsProperties =
val rawHiveTable = client.getRawHiveTable(db, table)
val oldProps =
client.hiveTableProps(rawHiveTable, containsStats = false)
.filterKeys(!_.startsWith(STATISTICS_PREFIX))
val newProps =
if (stats.isDefined) {
statsToProperties(stats.get)
oldProps ++ statsToProperties(stats.get)
} else {
new mutable.HashMap[String, String]()
oldProps
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@cloud-fan

  • Get all old properties from the hive table without table stats. (HiveStatisticsProperties is defined in HiveClientImpl, so do this in HiveClientImpl)
  • Filter out spark sql table and columns properties. (STATISTICS_PREFIX is defined in HiveExternalCatalog, so do this in HiveExternalCatalog)
  • Add the new table stats and then save the new table.


val oldTableNonStatsProps = rawTable.properties.filterNot(_._1.startsWith(STATISTICS_PREFIX))
val updatedTable = rawTable.copy(properties = oldTableNonStatsProps ++ statsProperties)
client.alterTable(updatedTable)
client.alterTableProps(rawHiveTable, newProps)
}

override def getTable(db: String, table: String): CatalogTable = withClient {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ private[hive] trait HiveClient {
/** Creates a table with the given metadata. */
def createTable(table: CatalogTable, ignoreIfExists: Boolean): Unit

/** Get hive table properties. */
def hiveTableProps(rawHiveTable: RawHiveTable, containsStats: Boolean): Map[String, String]

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.

shall we add a method in RawHiveTable to do it?


/** Drop the specified table. */
def dropTable(dbName: String, tableName: String, ignoreIfNotExists: Boolean, purge: Boolean): Unit

Expand All @@ -127,6 +130,9 @@ private[hive] trait HiveClient {
*/
def alterTable(dbName: String, tableName: String, table: CatalogTable): Unit

/** Alter a table properties */
def alterTableProps(rawHiveTable: RawHiveTable, newProps: Map[String, String]): Unit

/**
* Updates the given table with a new data schema and table properties, and keep everything else
* unchanged.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import java.lang.{Iterable => JIterable}
import java.lang.reflect.InvocationTargetException
import java.net.URI
import java.nio.charset.StandardCharsets.UTF_8
import java.util.{Locale, Map => JMap}
import java.util.{HashMap => JHashMap, Locale, Map => JMap}
import java.util.concurrent.TimeUnit._

import scala.collection.JavaConverters._
Expand Down Expand Up @@ -62,7 +62,7 @@ import org.apache.spark.sql.connector.catalog.SupportsNamespaces._
import org.apache.spark.sql.errors.{QueryCompilationErrors, QueryExecutionErrors}
import org.apache.spark.sql.execution.QueryExecutionException
import org.apache.spark.sql.hive.HiveExternalCatalog
import org.apache.spark.sql.hive.HiveExternalCatalog.DATASOURCE_SCHEMA
import org.apache.spark.sql.hive.HiveExternalCatalog.{DATASOURCE_SCHEMA}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types._
import org.apache.spark.util.{CircularBuffer, Utils}
Expand Down Expand Up @@ -580,6 +580,18 @@ private[hive] class HiveClientImpl(
ignoredProperties = ignoredProperties.toMap)
}

override def hiveTableProps(
rawHiveTable: RawHiveTable,
containsStats: Boolean): Map[String, String] = {
val hiveTable = rawHiveTable.rawTable.asInstanceOf[HiveTable]
val parameters = hiveTable.getParameters.asScala.toMap
if (containsStats) {
parameters
} else {
parameters.filterKeys(!HiveStatisticsProperties.contains(_))
}
}

override def createTable(table: CatalogTable, ignoreIfExists: Boolean): Unit = withHiveState {
verifyColumnDataType(table.dataSchema)
shim.createTable(client, toHiveTable(table, Some(userName)), ignoreIfExists)
Expand Down Expand Up @@ -609,6 +621,16 @@ private[hive] class HiveClientImpl(
shim.alterTable(client, qualifiedTableName, hiveTable)
}

override def alterTableProps(
rawHiveTable: RawHiveTable,
newProps: Map[String, String]): Unit = withHiveState {
val hiveTable = rawHiveTable.rawTable.asInstanceOf[HiveTable]
val newPropsMap = new JHashMap[String, String]()
newPropsMap.putAll(newProps.asJava)
hiveTable.getTTable.setParameters(newPropsMap)
shim.alterTable(client, s"${hiveTable.getDbName}.${hiveTable.getTableName}", hiveTable)
}

override def alterTableDataSchema(
dbName: String,
tableName: String,
Expand Down