|
| 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.types |
| 19 | + |
| 20 | +import scala.reflect.ClassTag |
| 21 | +import scala.reflect.runtime.universe.{TypeTag, runtimeMirror} |
| 22 | + |
| 23 | +import org.apache.spark.sql.catalyst.ScalaReflectionLock |
| 24 | +import org.apache.spark.sql.catalyst.expressions.Expression |
| 25 | +import org.apache.spark.util.Utils |
| 26 | + |
| 27 | +/** |
| 28 | + * A non-concrete data type, reserved for internal uses. |
| 29 | + */ |
| 30 | +private[sql] abstract class AbstractDataType { |
| 31 | + def defaultConcreteType: DataType |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +/** |
| 36 | + * An internal type used to represent everything that is not null, UDTs, arrays, structs, and maps. |
| 37 | + */ |
| 38 | +protected[sql] abstract class AtomicType extends DataType { |
| 39 | + private[sql] type InternalType |
| 40 | + @transient private[sql] val tag: TypeTag[InternalType] |
| 41 | + private[sql] val ordering: Ordering[InternalType] |
| 42 | + |
| 43 | + @transient private[sql] val classTag = ScalaReflectionLock.synchronized { |
| 44 | + val mirror = runtimeMirror(Utils.getSparkClassLoader) |
| 45 | + ClassTag[InternalType](mirror.runtimeClass(tag.tpe)) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +/** |
| 51 | + * :: DeveloperApi :: |
| 52 | + * Numeric data types. |
| 53 | + * |
| 54 | + * @group dataType |
| 55 | + */ |
| 56 | +abstract class NumericType extends AtomicType { |
| 57 | + // Unfortunately we can't get this implicitly as that breaks Spark Serialization. In order for |
| 58 | + // implicitly[Numeric[JvmType]] to be valid, we have to change JvmType from a type variable to a |
| 59 | + // type parameter and add a numeric annotation (i.e., [JvmType : Numeric]). This gets |
| 60 | + // desugared by the compiler into an argument to the objects constructor. This means there is no |
| 61 | + // longer an no argument constructor and thus the JVM cannot serialize the object anymore. |
| 62 | + private[sql] val numeric: Numeric[InternalType] |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +private[sql] object NumericType extends AbstractDataType { |
| 67 | + /** |
| 68 | + * Enables matching against NumericType for expressions: |
| 69 | + * {{{ |
| 70 | + * case Cast(child @ NumericType(), StringType) => |
| 71 | + * ... |
| 72 | + * }}} |
| 73 | + */ |
| 74 | + def unapply(e: Expression): Boolean = e.dataType.isInstanceOf[NumericType] |
| 75 | + |
| 76 | + override def defaultConcreteType: DataType = IntegerType |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +private[sql] object IntegralType extends AbstractDataType { |
| 81 | + /** |
| 82 | + * Enables matching against IntegralType for expressions: |
| 83 | + * {{{ |
| 84 | + * case Cast(child @ IntegralType(), StringType) => |
| 85 | + * ... |
| 86 | + * }}} |
| 87 | + */ |
| 88 | + def unapply(e: Expression): Boolean = e.dataType.isInstanceOf[IntegralType] |
| 89 | + |
| 90 | + override def defaultConcreteType: DataType = IntegerType |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +private[sql] abstract class IntegralType extends NumericType { |
| 95 | + private[sql] val integral: Integral[InternalType] |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +private[sql] object FractionalType extends AbstractDataType { |
| 100 | + /** |
| 101 | + * Enables matching against FractionalType for expressions: |
| 102 | + * {{{ |
| 103 | + * case Cast(child @ FractionalType(), StringType) => |
| 104 | + * ... |
| 105 | + * }}} |
| 106 | + */ |
| 107 | + def unapply(e: Expression): Boolean = e.dataType.isInstanceOf[FractionalType] |
| 108 | + |
| 109 | + override def defaultConcreteType: DataType = DoubleType |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +private[sql] abstract class FractionalType extends NumericType { |
| 114 | + private[sql] val fractional: Fractional[InternalType] |
| 115 | + private[sql] val asIntegral: Integral[InternalType] |
| 116 | +} |
0 commit comments