|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql |
| 19 | + |
| 20 | +import java.beans.Introspector |
| 21 | +import java.lang.{Iterable => JIterable} |
| 22 | +import java.util.{Iterator => JIterator, Map => JMap} |
| 23 | + |
| 24 | +import com.google.common.reflect.TypeToken |
| 25 | + |
| 26 | +import org.apache.spark.sql.types._ |
| 27 | + |
| 28 | +import scala.language.existentials |
| 29 | + |
| 30 | +/** |
| 31 | + * Type-inference utilities for POJOs and Java collections. |
| 32 | + */ |
| 33 | +private [sql] object JavaTypeInference { |
| 34 | + |
| 35 | + private val iterableType = TypeToken.of(classOf[JIterable[_]]) |
| 36 | + private val mapType = TypeToken.of(classOf[JMap[_, _]]) |
| 37 | + private val iteratorReturnType = classOf[JIterable[_]].getMethod("iterator").getGenericReturnType |
| 38 | + private val nextReturnType = classOf[JIterator[_]].getMethod("next").getGenericReturnType |
| 39 | + private val keySetReturnType = classOf[JMap[_, _]].getMethod("keySet").getGenericReturnType |
| 40 | + private val valuesReturnType = classOf[JMap[_, _]].getMethod("values").getGenericReturnType |
| 41 | + |
| 42 | + /** |
| 43 | + * Infers the corresponding SQL data type of a Java type. |
| 44 | + * @param typeToken Java type |
| 45 | + * @return (SQL data type, nullable) |
| 46 | + */ |
| 47 | + private [sql] def inferDataType(typeToken: TypeToken[_]): (DataType, Boolean) = { |
| 48 | + // TODO: All of this could probably be moved to Catalyst as it is mostly not Spark specific. |
| 49 | + typeToken.getRawType match { |
| 50 | + case c: Class[_] if c.isAnnotationPresent(classOf[SQLUserDefinedType]) => |
| 51 | + (c.getAnnotation(classOf[SQLUserDefinedType]).udt().newInstance(), true) |
| 52 | + |
| 53 | + case c: Class[_] if c == classOf[java.lang.String] => (StringType, true) |
| 54 | + case c: Class[_] if c == java.lang.Short.TYPE => (ShortType, false) |
| 55 | + case c: Class[_] if c == java.lang.Integer.TYPE => (IntegerType, false) |
| 56 | + case c: Class[_] if c == java.lang.Long.TYPE => (LongType, false) |
| 57 | + case c: Class[_] if c == java.lang.Double.TYPE => (DoubleType, false) |
| 58 | + case c: Class[_] if c == java.lang.Byte.TYPE => (ByteType, false) |
| 59 | + case c: Class[_] if c == java.lang.Float.TYPE => (FloatType, false) |
| 60 | + case c: Class[_] if c == java.lang.Boolean.TYPE => (BooleanType, false) |
| 61 | + |
| 62 | + case c: Class[_] if c == classOf[java.lang.Short] => (ShortType, true) |
| 63 | + case c: Class[_] if c == classOf[java.lang.Integer] => (IntegerType, true) |
| 64 | + case c: Class[_] if c == classOf[java.lang.Long] => (LongType, true) |
| 65 | + case c: Class[_] if c == classOf[java.lang.Double] => (DoubleType, true) |
| 66 | + case c: Class[_] if c == classOf[java.lang.Byte] => (ByteType, true) |
| 67 | + case c: Class[_] if c == classOf[java.lang.Float] => (FloatType, true) |
| 68 | + case c: Class[_] if c == classOf[java.lang.Boolean] => (BooleanType, true) |
| 69 | + |
| 70 | + case c: Class[_] if c == classOf[java.math.BigDecimal] => (DecimalType(), true) |
| 71 | + case c: Class[_] if c == classOf[java.sql.Date] => (DateType, true) |
| 72 | + case c: Class[_] if c == classOf[java.sql.Timestamp] => (TimestampType, true) |
| 73 | + |
| 74 | + case _ if typeToken.isArray => |
| 75 | + val (dataType, nullable) = inferDataType(typeToken.getComponentType) |
| 76 | + (ArrayType(dataType, nullable), true) |
| 77 | + |
| 78 | + case _ if mapType.isAssignableFrom(typeToken) => |
| 79 | + val typeToken2 = typeToken.asInstanceOf[TypeToken[_ <: JMap[_, _]]] |
| 80 | + val mapSupertype = typeToken2.getSupertype(classOf[JMap[_, _]]) |
| 81 | + val keyType = elementType(mapSupertype.resolveType(keySetReturnType)) |
| 82 | + val valueType = elementType(mapSupertype.resolveType(valuesReturnType)) |
| 83 | + val (keyDataType, _) = inferDataType(keyType) |
| 84 | + val (valueDataType, nullable) = inferDataType(valueType) |
| 85 | + (MapType(keyDataType, valueDataType, nullable), true) |
| 86 | + |
| 87 | + case _ => |
| 88 | + val beanInfo = Introspector.getBeanInfo(typeToken.getRawType) |
| 89 | + val properties = beanInfo.getPropertyDescriptors.filterNot(_.getName == "class") |
| 90 | + val fields = properties.map { property => |
| 91 | + val returnType = typeToken.method(property.getReadMethod).getReturnType |
| 92 | + val (dataType, nullable) = inferDataType(returnType) |
| 93 | + new StructField(property.getName, dataType, nullable) |
| 94 | + } |
| 95 | + (new StructType(fields), true) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private def elementType(typeToken: TypeToken[_]): TypeToken[_] = { |
| 100 | + val typeToken2 = typeToken.asInstanceOf[TypeToken[_ <: JIterable[_]]] |
| 101 | + val iterableSupertype = typeToken2.getSupertype(classOf[JIterable[_]]) |
| 102 | + val iteratorType = iterableSupertype.resolveType(iteratorReturnType) |
| 103 | + val itemType = iteratorType.resolveType(nextReturnType) |
| 104 | + itemType |
| 105 | + } |
| 106 | +} |
0 commit comments