cloud-fan commented on code in PR #53164:
URL: https://github.com/apache/spark/pull/53164#discussion_r2559749409
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -360,6 +360,35 @@ object Cast extends QueryErrorsBase {
*/
def canUpCast(from: DataType, to: DataType): Boolean =
UpCastRule.canUpCast(from, to)
+ /**
+ * Returns true iff it is safe to provide a default value of `from` type
typically defined in the
+ * data source metadata to the `to` type typically in the read schema of a
query.
+ */
+ def canAssignDefaultValue(from: DataType, to: DataType): Boolean = {
+ def isVariantStruct(dt: DataType): Boolean = {
+ dt match {
+ case s: StructType =>
+ s.fields.length > 0 &&
s.fields.forall(_.metadata.contains("__VARIANT_METADATA_KEY"))
+ case _ => false
+ }
+ }
+ (from, to) match {
+ case (s1: StructType, s2: StructType) =>
+ s1.length == s2.length && s1.fields.zip(s2.fields).forall {
+ case (f1, f2) => resolvableNullability(f1.nullable, f2.nullable) &&
+ canAssignDefaultValue(f1.dataType, f2.dataType)
+ }
+ case (ArrayType(fromType, fn), ArrayType(toType, tn)) =>
+ resolvableNullability(fn, tn) && canAssignDefaultValue(fromType,
toType)
+ case (MapType(fromKey, fromValue, fn), MapType(toKey, toValue, tn)) =>
+ resolvableNullability(fn, tn) && canAssignDefaultValue(fromKey, toKey)
&&
+ canAssignDefaultValue(fromValue, toValue)
+ // A VARIANT field can be read as StructType due to shredding.
+ case (_: VariantType, s: StructType) => isVariantStruct(s)
+ case (k, v) => canUpCast(k, v)
Review Comment:
```suggestion
case _ => canUpCast(from, to)
```
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]