Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2a59aa9

Browse files
committedApr 17, 2025·
[SPARK-51830] Exception handling for partition datatype conversion call
1 parent ed702c0 commit 2a59aa9

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed
 

‎sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/PartitioningUtils.scala

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import org.apache.spark.sql.catalyst.expressions.{Attribute, Cast, Literal}
3838
import org.apache.spark.sql.catalyst.types.DataTypeUtils
3939
import org.apache.spark.sql.catalyst.util.{CaseInsensitiveMap, DateFormatter, DateTimeUtils, TimeFormatter, TimestampFormatter}
4040
import org.apache.spark.sql.errors.{QueryCompilationErrors, QueryExecutionErrors}
41+
import org.apache.spark.sql.internal.SQLConf
4142
import org.apache.spark.sql.types._
4243
import org.apache.spark.sql.util.SchemaUtils
4344
import org.apache.spark.unsafe.types.UTF8String
@@ -361,12 +362,21 @@ object PartitioningUtils extends SQLConfHelper {
361362
}.mkString("/")
362363
}
363364

364-
def removeLeadingZerosFromNumberTypePartition(value: String, dataType: DataType): String =
365-
dataType match {
366-
case ByteType | ShortType | IntegerType | LongType | FloatType | DoubleType =>
367-
Option(castPartValueToDesiredType(dataType, value, null)).map(_.toString).orNull
368-
case _ => value
365+
def removeLeadingZerosFromNumberTypePartition(value: String, dataType: DataType): String = {
366+
try {
367+
dataType match {
368+
case ByteType | ShortType | IntegerType | LongType | FloatType | DoubleType =>
369+
Option(castPartValueToDesiredType(dataType, value, null)).map(_.toString).orNull
370+
case _ => value
371+
}
372+
} catch {
373+
case NonFatal(_) =>
374+
if (SQLConf.get.validatePartitionColumns) {
375+
throw QueryExecutionErrors.failedToCastValueToDataTypeForPartitionColumnError(
376+
value, dataType, null)
377+
} else value
369378
}
379+
}
370380

371381
def getPathFragment(spec: TablePartitionSpec, partitionColumns: Seq[Attribute]): String = {
372382
getPathFragment(spec, DataTypeUtils.fromAttributes(partitionColumns))

‎sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetPartitionDiscoverySuite.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,14 @@ class ParquetV2PartitionDiscoverySuite extends ParquetPartitionDiscoverySuite {
14051405
assert("p_int=10/p_float=1.0" === path)
14061406
}
14071407

1408+
test("SPARK-51830 handle exception and validate partition column as false") {
1409+
SQLConf.get.setConf(SQLConf.VALIDATE_PARTITION_COLUMNS, false)
1410+
val spec = Map("p_int" -> "not_a_number")
1411+
val schema = new StructType().add("p_int", "int")
1412+
val path = PartitioningUtils.getPathFragment(spec, schema)
1413+
assert("p_int=not_a_number" === path)
1414+
}
1415+
14081416
test("SPARK-39417: Null partition value") {
14091417
// null partition value is replaced by DEFAULT_PARTITION_NAME before hitting getPathFragment.
14101418
val spec = Map("p_int"-> ExternalCatalogUtils.DEFAULT_PARTITION_NAME)

0 commit comments

Comments
 (0)
Please sign in to comment.