Skip to content

Commit b1995d7

Browse files
committed
Review comments
1 parent 0719e89 commit b1995d7

File tree

1 file changed

+6
-28
lines changed

1 file changed

+6
-28
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ResolveDefaultColumnsUtil.scala

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -432,36 +432,20 @@ object ResolveDefaultColumns extends QueryErrorsBase
432432
targetType: DataType,
433433
colName: String): Option[Expression] = {
434434
expr match {
435-
case l: Literal => defaultValueFromWiderType(l, targetType, colName)
436-
case _ => None
437-
}
438-
}
439-
440-
/**
441-
* If the provided default value is a literal of a wider type than the target column,
442-
* but the literal value fits within the narrower type, just coerce it for convenience.
443-
* Exclude boolean/array/struct/map types from consideration for this type coercion to
444-
* avoid surprising behavior like interpreting "false" as integer zero.
445-
*/
446-
def defaultValueFromWiderType(
447-
expr: Expression,
448-
targetType: DataType,
449-
colName: String): Option[Expression] = {
450-
expr match {
451-
case e if !Seq(targetType, e.dataType).exists(_ match {
435+
case e if e.foldable && !Seq(targetType, e.dataType).exists(_ match {
452436
case _: BooleanType | _: ArrayType | _: StructType | _: MapType => true
453437
case _ => false
454438
}) =>
455439
val casted = Cast(e, targetType, Some(conf.sessionLocalTimeZone), evalMode = EvalMode.TRY)
456440
try {
457441
Option(casted.eval(EmptyRow)).map(Literal(_, targetType))
458442
} catch {
459-
case e @ ( _: SparkThrowable | _: RuntimeException) =>
443+
case ex @ ( _: SparkThrowable | _: RuntimeException) =>
460444
logWarning(log"Failed to cast default value '${MDC(COLUMN_DEFAULT_VALUE, e)}' " +
461445
log"for column ${MDC(COLUMN_NAME, colName)} " +
462-
log"from ${MDC(COLUMN_DATA_TYPE_SOURCE, expr.dataType)} " +
446+
log"from ${MDC(COLUMN_DATA_TYPE_SOURCE, e.dataType)} " +
463447
log"to ${MDC(COLUMN_DATA_TYPE_TARGET, targetType)} " +
464-
log"due to ${MDC(ERROR, e.getMessage)}", e)
448+
log"due to ${MDC(ERROR, ex.getMessage)}", ex)
465449
None
466450
}
467451
case _ => None
@@ -477,8 +461,7 @@ object ResolveDefaultColumns extends QueryErrorsBase
477461
dataType: DataType,
478462
statementType: String,
479463
colName: String,
480-
defaultSQL: String,
481-
castWiderOnlyLiterals: Boolean = true): Expression = {
464+
defaultSQL: String): Expression = {
482465
val supplanted = CharVarcharUtils.replaceCharVarcharWithString(dataType)
483466
// Perform implicit coercion from the provided expression type to the required column type.
484467
val ret = analyzed match {
@@ -487,12 +470,7 @@ object ResolveDefaultColumns extends QueryErrorsBase
487470
case canUpCast if Cast.canUpCast(canUpCast.dataType, supplanted) =>
488471
Cast(analyzed, supplanted, Some(conf.sessionLocalTimeZone))
489472
case other =>
490-
val casted = if (castWiderOnlyLiterals) {
491-
defaultValueFromWiderTypeLiteral(other, supplanted, colName)
492-
} else {
493-
defaultValueFromWiderType(other, supplanted, colName)
494-
}
495-
casted.getOrElse(
473+
defaultValueFromWiderTypeLiteral(other, supplanted, colName).getOrElse(
496474
throw QueryCompilationErrors.defaultValuesDataTypeError(
497475
statementType, colName, defaultSQL, dataType, other.dataType))
498476
}

0 commit comments

Comments
 (0)