Github user liancheng commented on a diff in the pull request:

    https://github.com/apache/spark/pull/4826#discussion_r25588714
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/types/dataTypes.scala ---
    @@ -198,6 +198,57 @@ object DataType {
           case (left, right) => left == right
         }
       }
    +
    +  /**
    +   * Compares two types, ignoring compatible nullability of ArrayType, 
MapType, StructType.
    +   *
    +   * Compatible nullability is defined as follows:
    +   *   - If `from` and `to` are ArrayTypes, `from` has a compatible 
nullability with `to`
    +   *   if and only if `from.containsNull` is false, or both of 
`from.containsNull` and
    +   *   `to.containsNull` are true.
    +   *   - If `from` and `to` are MapTypes, `from` has a compatible 
nullability with `to`
    +   *   if and only if `from.valueContainsNull` is false, or both of 
`from.valueContainsNull` and
    +   *   `to.valueContainsNull` are true.
    +   *   - If `from` and `to` are StructTypes, `from` has a compatible 
nullability with `to`
    +   *   if and only if for all every pair of fields, `fromField.nullable` 
is false, or both
    +   *   of `fromField.nullable` and `toField.nullable` are true.
    +   */
    +  private[spark] def equalsIgnoreCompatibleNullability(from: DataType, to: 
DataType): Boolean = {
    +    (from, to) match {
    +      case (ArrayType(fromElement, fn), ArrayType(toElement, tn)) =>
    +        (tn || !fn) && equalsIgnoreCompatibleNullability(fromElement, 
toElement)
    +
    +      case (MapType(fromKey, fromValue, fn), MapType(toKey, toValue, tn)) 
=>
    +        (tn || !fn) &&
    +          equalsIgnoreCompatibleNullability(fromKey, toKey) &&
    +          equalsIgnoreCompatibleNullability(fromValue, toValue)
    +
    +      case (StructType(fromFields), StructType(toFields)) =>
    +        fromFields.size == toFields.size &&
    +          fromFields.zip(toFields).forall {
    +            case (fromField, toField) =>
    +              fromField.name == toField.name &&
    +                (toField.nullable || !fromField.nullable) &&
    +                equalsIgnoreCompatibleNullability(fromField.dataType, 
toField.dataType)
    +          }
    +
    +      case (fromDataType, toDataType) => fromDataType == toDataType
    +    }
    +  }
    +
    +  /** Sets all nullable/containsNull/valueContainsNull to true. */
    +  private[spark] def alwaysNullable(dataType: DataType): DataType = 
dataType match {
    +    case ArrayType(elementType, _) =>
    +      ArrayType(alwaysNullable(elementType), containsNull = true)
    +    case MapType(keyType, valueType, _) =>
    +      MapType(alwaysNullable(keyType), alwaysNullable(valueType), true)
    --- End diff --
    
    Nit: Use named parameter for `valueContainsNull`.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to