Skip to content

Commit 7c6d9a8

Browse files
committed
update view as well
1 parent a199906 commit 7c6d9a8

File tree

3 files changed

+6
-33
lines changed

3 files changed

+6
-33
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ case class AliasViewChild(conf: SQLConf) extends Rule[LogicalPlan] with CastSupp
7373
case (attr, originAttr) if !attr.semanticEquals(originAttr) =>
7474
// The dataType of the output attributes may be not the same with that of the view
7575
// output, so we should cast the attribute to the dataType of the view output attribute.
76-
// Will throw an AnalysisException if the cast can't perform or might truncate.
77-
if (Cast.mayTruncate(originAttr.dataType, attr.dataType)) {
76+
// Will throw an AnalysisException if the cast is not a up-cast.
77+
if (!Cast.canUpCast(originAttr.dataType, attr.dataType)) {
7878
throw new AnalysisException(s"Cannot up cast ${originAttr.sql} from " +
7979
s"${originAttr.dataType.catalogString} to ${attr.dataType.catalogString} as it " +
8080
s"may truncate\n")

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,6 @@ object Cast {
119119
case _ => false
120120
}
121121

122-
/**
123-
* Return true iff we may truncate during casting `from` type to `to` type. e.g. long -> int,
124-
* timestamp -> date.
125-
*/
126-
def mayTruncate(from: DataType, to: DataType): Boolean = (from, to) match {
127-
case (from: NumericType, to: DecimalType) if !to.isWiderThan(from) => true
128-
case (from: DecimalType, to: NumericType) if !from.isTighterThan(to) => true
129-
case (from, to) if illegalNumericPrecedence(from, to) => true
130-
case (TimestampType, DateType) => true
131-
case (StringType, to: NumericType) => true
132-
case _ => false
133-
}
134-
135122
private def illegalNumericPrecedence(from: DataType, to: DataType): Boolean = {
136123
val fromPrecedence = TypeCoercion.numericPrecedence.indexOf(from)
137124
val toPrecedence = TypeCoercion.numericPrecedence.indexOf(to)

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -956,37 +956,23 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
956956
checkEvaluation(ret6, "[1, [1 -> a, 2 -> b, 3 -> c]]")
957957
}
958958

959-
test("SPARK-26706: Fix Cast.mayTruncate for bytes") {
960-
assert(!Cast.mayTruncate(ByteType, ByteType))
961-
assert(!Cast.mayTruncate(DecimalType.ByteDecimal, ByteType))
962-
assert(Cast.mayTruncate(ShortType, ByteType))
963-
assert(Cast.mayTruncate(IntegerType, ByteType))
964-
assert(Cast.mayTruncate(LongType, ByteType))
965-
assert(Cast.mayTruncate(FloatType, ByteType))
966-
assert(Cast.mayTruncate(DoubleType, ByteType))
967-
assert(Cast.mayTruncate(DecimalType.IntDecimal, ByteType))
968-
}
969-
970-
test("canSafeCast and mayTruncate must be consistent for numeric types") {
971-
import DataTypeTestUtils._
972-
959+
test("up-cast") {
973960
def isCastSafe(from: NumericType, to: NumericType): Boolean = (from, to) match {
974961
case (_, dt: DecimalType) => dt.isWiderThan(from)
975962
case (dt: DecimalType, _) => dt.isTighterThan(to)
976963
case _ => numericPrecedence.indexOf(from) <= numericPrecedence.indexOf(to)
977964
}
978965

966+
import DataTypeTestUtils.numericTypes
979967
numericTypes.foreach { from =>
980968
val (safeTargetTypes, unsafeTargetTypes) = numericTypes.partition(to => isCastSafe(from, to))
981969

982970
safeTargetTypes.foreach { to =>
983-
assert(Cast.canUpCast(from, to), s"It should be possible to safely cast $from to $to")
984-
assert(!Cast.mayTruncate(from, to), s"No truncation is expected when casting $from to $to")
971+
assert(Cast.canUpCast(from, to), s"It should be possible to up-cast $from to $to")
985972
}
986973

987974
unsafeTargetTypes.foreach { to =>
988-
assert(!Cast.canUpCast(from, to), s"It shouldn't be possible to safely cast $from to $to")
989-
assert(Cast.mayTruncate(from, to), s"Truncation is expected when casting $from to $to")
975+
assert(!Cast.canUpCast(from, to), s"It shouldn't be possible to up-cast $from to $to")
990976
}
991977
}
992978
}

0 commit comments

Comments
 (0)