@@ -432,36 +432,20 @@ object ResolveDefaultColumns extends QueryErrorsBase
432
432
targetType : DataType ,
433
433
colName : String ): Option [Expression ] = {
434
434
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 {
452
436
case _ : BooleanType | _ : ArrayType | _ : StructType | _ : MapType => true
453
437
case _ => false
454
438
}) =>
455
439
val casted = Cast (e, targetType, Some (conf.sessionLocalTimeZone), evalMode = EvalMode .TRY )
456
440
try {
457
441
Option (casted.eval(EmptyRow )).map(Literal (_, targetType))
458
442
} catch {
459
- case e @ ( _ : SparkThrowable | _ : RuntimeException ) =>
443
+ case ex @ ( _ : SparkThrowable | _ : RuntimeException ) =>
460
444
logWarning(log " Failed to cast default value ' ${MDC (COLUMN_DEFAULT_VALUE , e)}' " +
461
445
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)} " +
463
447
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 )
465
449
None
466
450
}
467
451
case _ => None
@@ -477,8 +461,7 @@ object ResolveDefaultColumns extends QueryErrorsBase
477
461
dataType : DataType ,
478
462
statementType : String ,
479
463
colName : String ,
480
- defaultSQL : String ,
481
- castWiderOnlyLiterals : Boolean = true ): Expression = {
464
+ defaultSQL : String ): Expression = {
482
465
val supplanted = CharVarcharUtils .replaceCharVarcharWithString(dataType)
483
466
// Perform implicit coercion from the provided expression type to the required column type.
484
467
val ret = analyzed match {
@@ -487,12 +470,7 @@ object ResolveDefaultColumns extends QueryErrorsBase
487
470
case canUpCast if Cast .canUpCast(canUpCast.dataType, supplanted) =>
488
471
Cast (analyzed, supplanted, Some (conf.sessionLocalTimeZone))
489
472
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(
496
474
throw QueryCompilationErrors .defaultValuesDataTypeError(
497
475
statementType, colName, defaultSQL, dataType, other.dataType))
498
476
}
0 commit comments