cloud-fan commented on a change in pull request #28572:
URL: https://github.com/apache/spark/pull/28572#discussion_r427022894
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
##########
@@ -3071,15 +3071,31 @@ class Analyzer(
case p => p transformExpressions {
case u @ UpCast(child, _, _) if !child.resolved => u
- case UpCast(child, dt: AtomicType, _)
+ case u @ UpCast(child, _, _)
if SQLConf.get.getConf(SQLConf.LEGACY_LOOSE_UPCAST) &&
+ u.dataType.isInstanceOf[AtomicType] &&
child.dataType == StringType =>
- Cast(child, dt.asNullable)
-
- case UpCast(child, dataType, walkedTypePath) if
!Cast.canUpCast(child.dataType, dataType) =>
- fail(child, dataType, walkedTypePath)
-
- case UpCast(child, dataType, _) => Cast(child, dataType.asNullable)
+ Cast(child, u.dataType.asNullable)
+
+ case u @ UpCast(child, _, walkedTypePath)
+ if child.dataType.isInstanceOf[DecimalType]
+ && u.dataType.isInstanceOf[DecimalType]
+ && walkedTypePath.nonEmpty =>
+ // SPARK-31750: there are two cases: 1. for local BigDecimal
collection,
+ // e.g. Seq(BigDecimal(12.34), BigDecimal(1)).toDF("a"), we will have
+ // UpCast(child, Decimal(38, 18)) where child's data type is always
Decimal(38, 18).
+ // 2. for other cases where data type is explicitly known, e.g,
spark.read
+ // .parquet("/tmp/file").as[BigDecimal]. We will have UpCast(child,
Decimal(38, 18)),
+ // where child's data type can be, e.g. Decimal(38, 0). In this
case, we actually
+ // should not do cast otherwise there will be precision lost.
+ // Thus, we eliminate the UpCast here to avoid precision lost for
case 2 and do
+ // no hurt for case 1.
Review comment:
We can simplify the comment to only mention case 2
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]