@@ -125,39 +125,31 @@ private[sql] object JsonRDD extends Logging {
125125 * Returns the most general data type for two given data types.
126126 */
127127 private [json] def compatibleType (t1 : DataType , t2 : DataType ): DataType = {
128- // Try and find a promotion rule that contains both types in question.
129- val applicableConversion = HiveTypeCoercion .allPromotions.find(p => p.contains(t1) && p
130- .contains(t2))
131-
132- // If found return the widest common type, otherwise None
133- val returnType = applicableConversion.map(_.filter(t => t == t1 || t == t2).last)
134-
135- if (returnType.isDefined) {
136- returnType.get
137- } else {
138- // t1 or t2 is a StructType, ArrayType, BooleanType, or an unexpected type.
139- (t1, t2) match {
140- case (other : DataType , NullType ) => other
141- case (NullType , other : DataType ) => other
142- case (StructType (fields1), StructType (fields2)) => {
143- val newFields = (fields1 ++ fields2).groupBy(field => field.name).map {
144- case (name, fieldTypes) => {
145- val dataType = fieldTypes.map(field => field.dataType).reduce(
146- (type1 : DataType , type2 : DataType ) => compatibleType(type1, type2))
147- StructField (name, dataType, true )
128+ HiveTypeCoercion .findTightestCommonType(t1,t2) match {
129+ case Some (commonType) => commonType
130+ case None =>
131+ // t1 or t2 is a StructType, ArrayType, BooleanType, or an unexpected type.
132+ (t1, t2) match {
133+ case (other : DataType , NullType ) => other
134+ case (NullType , other : DataType ) => other
135+ case (StructType (fields1), StructType (fields2)) => {
136+ val newFields = (fields1 ++ fields2).groupBy(field => field.name).map {
137+ case (name, fieldTypes) => {
138+ val dataType = fieldTypes.map(field => field.dataType).reduce(
139+ (type1 : DataType , type2 : DataType ) => compatibleType(type1, type2))
140+ StructField (name, dataType, true )
141+ }
148142 }
143+ StructType (newFields.toSeq.sortBy {
144+ case StructField (name, _, _) => name
145+ })
149146 }
150- StructType (newFields.toSeq.sortBy {
151- case StructField (name, _, _) => name
152- })
147+ case (ArrayType (elementType1, containsNull1), ArrayType (elementType2, containsNull2)) =>
148+ ArrayType (compatibleType(elementType1, elementType2), containsNull1 || containsNull2)
149+ // TODO: We should use JsonObjectStringType to mark that values of field will be
150+ // strings and every string is a Json object.
151+ case (_, _) => StringType
153152 }
154- case (ArrayType (elementType1, containsNull1), ArrayType (elementType2, containsNull2)) =>
155- ArrayType (compatibleType(elementType1, elementType2), containsNull1 || containsNull2)
156- // TODO: We should use JsonObjectStringType to mark that values of field will be
157- // strings and every string is a Json object.
158- case (BooleanType , BooleanType ) => BooleanType
159- case (_, _) => StringType
160- }
161153 }
162154 }
163155
0 commit comments