Skip to content

Commit 88080a2

Browse files
committed
Clearer definition of implicit type cast.
1 parent f0ff97f commit 88080a2

21 files changed

+35
-58
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -723,26 +723,28 @@ object HiveTypeCoercion {
723723
* If the implicit cast is not allowed, return the expression itself.
724724
*/
725725
def implicitCast(e: Expression, expectedType: AbstractDataType): Expression = {
726-
(e, expectedType) match {
726+
val inType = e.dataType
727+
(inType, expectedType) match {
727728
// Cast null type (usually from null literals) into target types
728-
case (in @ NullType(), target: DataType) => Cast(in, target.defaultConcreteType)
729+
case (NullType, target: DataType) => Cast(e, target.defaultConcreteType)
729730

730731
// Implicit cast among numeric types
731-
case (in @ NumericType(), target: NumericType) if in.dataType != target =>
732-
Cast(in, target)
732+
case (_: NumericType, target: NumericType) if e.dataType != target => Cast(e, target)
733733

734734
// Implicit cast between date time types
735-
case (in @ DateType(), TimestampType) => Cast(in, TimestampType)
736-
case (in @ TimestampType(), DateType) => Cast(in, DateType)
735+
case (DateType, TimestampType) => Cast(e, TimestampType)
736+
case (TimestampType, DateType) => Cast(e, DateType)
737737

738-
// Implicit from string to atomic types, and vice versa
739-
case (in @ StringType(), target: AtomicType) if target != StringType =>
740-
Cast(in, target.defaultConcreteType)
741-
case (in, StringType) if in.dataType != StringType =>
742-
Cast(in, StringType)
738+
// Implicit cast from/to string
739+
case (StringType, NumericType) => Cast(e, DoubleType)
740+
case (StringType, target: NumericType) => Cast(e, target)
741+
case (StringType, DateType) => Cast(e, DateType)
742+
case (StringType, TimestampType) => Cast(e, TimestampType)
743+
case (StringType, BinaryType) => Cast(e, BinaryType)
744+
case (any, StringType) if any != StringType => Cast(e, StringType)
743745

744746
// Else, just return the same input expression
745-
case (in, _) => in
747+
case _ => e
746748
}
747749
}
748750
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ExpectsInputTypes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ trait ExpectsInputTypes { self: Expression =>
3737
def inputTypes: Seq[AbstractDataType]
3838

3939
override def checkInputDataTypes(): TypeCheckResult = {
40-
// We will do the type checking in `HiveTypeCoercion`, so always returning success here.
40+
// TODO: implement proper type checking.
4141
TypeCheckResult.TypeCheckSuccess
4242
}
4343
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ case class InSet(value: Expression, hset: Set[Any])
122122
case class And(left: Expression, right: Expression)
123123
extends BinaryExpression with Predicate with ExpectsInputTypes {
124124

125-
override def toString: String = s"$left && $right"
125+
override def toString: String = s"($left && $right)"
126126

127127
override def inputTypes: Seq[DataType] = Seq(BooleanType, BooleanType)
128128

@@ -171,7 +171,7 @@ case class And(left: Expression, right: Expression)
171171
case class Or(left: Expression, right: Expression)
172172
extends BinaryExpression with Predicate with ExpectsInputTypes {
173173

174-
override def toString: String = s"$left || $right"
174+
override def toString: String = s"($left || $right)"
175175

176176
override def inputTypes: Seq[DataType] = Seq(BooleanType, BooleanType)
177177

sql/catalyst/src/main/scala/org/apache/spark/sql/types/AbstractDataType.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import org.apache.spark.util.Utils
2828
* A non-concrete data type, reserved for internal uses.
2929
*/
3030
private[sql] abstract class AbstractDataType {
31-
def defaultConcreteType: DataType
31+
private[sql] def defaultConcreteType: DataType
3232
}
3333

3434

@@ -50,8 +50,6 @@ protected[sql] abstract class AtomicType extends DataType {
5050
/**
5151
* :: DeveloperApi ::
5252
* Numeric data types.
53-
*
54-
* @group dataType
5553
*/
5654
abstract class NumericType extends AtomicType {
5755
// Unfortunately we can't get this implicitly as that breaks Spark Serialization. In order for
@@ -73,7 +71,7 @@ private[sql] object NumericType extends AbstractDataType {
7371
*/
7472
def unapply(e: Expression): Boolean = e.dataType.isInstanceOf[NumericType]
7573

76-
override def defaultConcreteType: DataType = IntegerType
74+
private[sql] override def defaultConcreteType: DataType = IntegerType
7775
}
7876

7977

@@ -87,7 +85,7 @@ private[sql] object IntegralType extends AbstractDataType {
8785
*/
8886
def unapply(e: Expression): Boolean = e.dataType.isInstanceOf[IntegralType]
8987

90-
override def defaultConcreteType: DataType = IntegerType
88+
private[sql] override def defaultConcreteType: DataType = IntegerType
9189
}
9290

9391

@@ -106,7 +104,7 @@ private[sql] object FractionalType extends AbstractDataType {
106104
*/
107105
def unapply(e: Expression): Boolean = e.dataType.isInstanceOf[FractionalType]
108106

109-
override def defaultConcreteType: DataType = DoubleType
107+
private[sql] override def defaultConcreteType: DataType = DoubleType
110108
}
111109

112110

sql/catalyst/src/main/scala/org/apache/spark/sql/types/ArrayType.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import org.json4s.JsonDSL._
2222
import org.apache.spark.annotation.DeveloperApi
2323

2424

25-
object ArrayType {
25+
object ArrayType extends AbstractDataType {
2626
/** Construct a [[ArrayType]] object with the given element type. The `containsNull` is true. */
2727
def apply(elementType: DataType): ArrayType = ArrayType(elementType, containsNull = true)
28+
29+
override def defaultConcreteType: DataType = ArrayType(NullType, containsNull = true)
2830
}
2931

3032

@@ -41,8 +43,6 @@ object ArrayType {
4143
*
4244
* @param elementType The data type of values.
4345
* @param containsNull Indicates if values have `null` values
44-
*
45-
* @group dataType
4646
*/
4747
@DeveloperApi
4848
case class ArrayType(elementType: DataType, containsNull: Boolean) extends DataType {

sql/catalyst/src/main/scala/org/apache/spark/sql/types/BinaryType.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import org.apache.spark.sql.catalyst.util.TypeUtils
2929
* :: DeveloperApi ::
3030
* The data type representing `Array[Byte]` values.
3131
* Please use the singleton [[DataTypes.BinaryType]].
32-
*
33-
* @group dataType
3432
*/
3533
@DeveloperApi
3634
class BinaryType private() extends AtomicType {

sql/catalyst/src/main/scala/org/apache/spark/sql/types/BooleanType.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ import org.apache.spark.sql.catalyst.ScalaReflectionLock
2727
/**
2828
* :: DeveloperApi ::
2929
* The data type representing `Boolean` values. Please use the singleton [[DataTypes.BooleanType]].
30-
*
31-
*@group dataType
3230
*/
3331
@DeveloperApi
3432
class BooleanType private() extends AtomicType {

sql/catalyst/src/main/scala/org/apache/spark/sql/types/ByteType.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ import org.apache.spark.sql.catalyst.ScalaReflectionLock
2727
/**
2828
* :: DeveloperApi ::
2929
* The data type representing `Byte` values. Please use the singleton [[DataTypes.ByteType]].
30-
*
31-
* @group dataType
3230
*/
3331
@DeveloperApi
3432
class ByteType private() extends IntegralType {

sql/catalyst/src/main/scala/org/apache/spark/sql/types/DataType.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ import org.apache.spark.sql.catalyst.expressions.Expression
3131
/**
3232
* :: DeveloperApi ::
3333
* The base type of all Spark SQL data types.
34-
*
35-
* @group dataType
3634
*/
3735
@DeveloperApi
3836
abstract class DataType extends AbstractDataType {

sql/catalyst/src/main/scala/org/apache/spark/sql/types/DateType.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ import org.apache.spark.sql.catalyst.ScalaReflectionLock
2626

2727
/**
2828
* :: DeveloperApi ::
29-
* The data type representing `java.sql.Date` values.
29+
* A date type, supporting "0001-01-01" through "9999-12-31".
30+
*
3031
* Please use the singleton [[DataTypes.DateType]].
3132
*
32-
* @group dataType
33+
* Internally, this is represented as the number of days from epoch (1970-01-01 00:00:00 UTC).
3334
*/
3435
@DeveloperApi
3536
class DateType private() extends AtomicType {

0 commit comments

Comments
 (0)