Skip to content
Merged
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 @@ -48,7 +48,8 @@ abstract class HiveHelper {
partitionBy: Seq[String],
partitionValues: Seq[String],
databaseName: Option[String],
tableName: String): Unit
tableName: String,
location: String): Unit

def repairHiveTable(databaseName: Option[String],
tableName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package za.co.absa.pramen.core.utils.hive

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.execution.datasources.PartitioningUtils.PartitionValues
import org.apache.spark.sql.types.StructType
import org.slf4j.LoggerFactory
import za.co.absa.pramen.api.CatalogTable
Expand Down Expand Up @@ -108,7 +107,8 @@ class HiveHelperSparkCatalog(spark: SparkSession) extends HiveHelper {
partitionBy: Seq[String],
partitionValues: Seq[String],
databaseName: Option[String],
tableName: String): Unit = {
tableName: String,
location: String): Unit = {
if (partitionBy.length != partitionValues.length) {
throw new IllegalArgumentException(s"Partition columns and values must have the same length. Columns: $partitionBy, values: $partitionValues")
}
Expand All @@ -128,8 +128,22 @@ class HiveHelperSparkCatalog(spark: SparkSession) extends HiveHelper {
.replace("@partitionClause", partitionClause)
.replace("@schema", schemaDDL)

log.info(s"Executing: $sql")
spark.sql(sql).collect()
try {
log.info(s"Executing: $sql")
spark.sql(sql).collect()
} catch {
case ex: Throwable if ex.getMessage != null && ex.getMessage.toLowerCase.contains("partition not found") =>
log.info(s"Partition not found for $fullTableName, partition: $partitionClause. Adding partition...")
try {
addPartition(databaseName, tableName, partitionBy, partitionValues, location)
} catch {
case NonFatal(ex) =>
log.warn(s"Failed to add partition for $fullTableName, partition: $partitionClause", ex)
}

log.info(s"Executing: $sql")
spark.sql(sql).collect()
}
}

override def repairHiveTable(databaseName: Option[String],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import org.apache.spark.sql.types.StructType
import org.slf4j.LoggerFactory
import za.co.absa.pramen.core.utils.SparkUtils

import scala.util.control.NonFatal

class HiveHelperSql(val queryExecutor: QueryExecutor,
hiveConfig: HiveQueryTemplates,
alwaysEscapeColumnNames: Boolean) extends HiveHelper {
Expand Down Expand Up @@ -90,7 +92,8 @@ class HiveHelperSql(val queryExecutor: QueryExecutor,
partitionBy: Seq[String],
partitionValues: Seq[String],
databaseName: Option[String],
tableName: String): Unit = {
tableName: String,
location: String): Unit = {
if (partitionBy.length != partitionValues.length) {
throw new IllegalArgumentException(s"Partition columns and values must have the same length. Columns: $partitionBy, values: $partitionValues")
}
Expand All @@ -100,8 +103,22 @@ class HiveHelperSql(val queryExecutor: QueryExecutor,

log.info(s"Replacing partition schema for $fullTableName, partition: $partitionClause...")

val sql = applyPartitionTemplate(hiveConfig.replacePartitionSchemaTemplate, fullTableName, "", partitionClause, schemaDDL)
queryExecutor.execute(sql)
val sql = applyPartitionTemplate(hiveConfig.replacePartitionSchemaTemplate, fullTableName, location, partitionClause, schemaDDL)

try {
queryExecutor.execute(sql)
} catch {
case ex: Throwable if ex.getMessage != null && ex.getMessage.toLowerCase.contains("partition not found") =>
log.info(s"Partition not found for $fullTableName, partition: $partitionClause. Adding partition...")
try {
addPartition(databaseName, tableName, partitionBy, partitionValues, location)
} catch {
case NonFatal(ex) =>
log.warn(s"Failed to add partition for $fullTableName, partition: $partitionClause", ex)
}

queryExecutor.execute(sql)
}
}

override def repairHiveTable(databaseName: Option[String],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class HiveHelperSqlSuite extends AnyWordSpec with SparkTestBase with TempDirFixt
val hiveHelper = new HiveHelperSql(qe, defaultHiveConfig, true)
val schema = spark.read.parquet(path).withColumn("b", lit(1)).schema

hiveHelper.replaceHivePartitionSchema(schema, "a" :: "b" :: Nil, Seq("AA", "22"), Some("db"), "tbl")
hiveHelper.replaceHivePartitionSchema(schema, "a" :: "b" :: Nil, Seq("AA", "22"), Some("db"), "tbl", "")

val actual = qe.queries.mkString("\n")

Expand Down
Loading