Skip to content

Commit c2be46b

Browse files
committed
check to decide if milli transter to micro will out of long range
1 parent 8e19274 commit c2be46b

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,9 @@ abstract class ToTimestamp
814814
"specified format is '" + o + "'")
815815
}
816816

817+
private lazy val minRange = Long.MinValue / scaleFactor
818+
private lazy val maxRange = Long.MaxValue / scaleFactor
819+
817820
override def eval(input: InternalRow): Any = {
818821
val t = left.eval(input)
819822
if (t == null) {
@@ -856,15 +859,12 @@ abstract class ToTimestamp
856859
}
857860
case LongType =>
858861
val input = t.asInstanceOf[Long]
859-
val minRange = Long.MinValue / scaleFactor
860-
val maxRange = Long.MaxValue / scaleFactor
861862
if ( minRange < input && input < maxRange ) {
862863
Math.multiplyExact(input, scaleFactor.asInstanceOf[Long])
863864
} else {
864865
throw new IllegalArgumentException(
865-
s"""|input='$input' is not valid,
866-
|which valid range is from $minRange to $maxRange for '$right'""".stripMargin
867-
)
866+
"input [" + input + "] not from " + minRange + " to "
867+
+ maxRange + " for format " + right)
868868
}
869869
}
870870
}
@@ -949,7 +949,13 @@ abstract class ToTimestamp
949949
boolean ${ev.isNull} = ${eval1.isNull};
950950
$javaType ${ev.value} = ${CodeGenerator.defaultValue(dataType)};
951951
if (!${ev.isNull}) {
952-
${ev.value} = ${eval1.value} * $scaleFactor;
952+
if ( (${minRange}L < ${eval1.value}) && (${eval1.value} < ${maxRange}L) ) {
953+
${ev.value} = ${eval1.value} * $scaleFactor;
954+
} else {
955+
throw new java.lang.IllegalArgumentException(
956+
"input [" + ${eval1.value} + "] not from " + ${minRange}L + " to "
957+
+ ${maxRange}L + " for format " + "${right.toString}");
958+
}
953959
}""")
954960
}
955961
}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,18 +1166,22 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
11661166
Literal(-1580184371847123L),
11671167
Literal("micro")), -1580184371847123L)
11681168

1169-
// checkEvaluation(
1170-
// GetTimestamp(Literal(9223372036854776L), Literal("milli")),
1171-
// "input='9223372036854776' is not valid,\n" +
1172-
// "which valid range is from -9223372036854775 to 9223372036854775 for 'milli'"
1173-
// )
1174-
1169+
// out of range check
11751170
checkExceptionInExpression[IllegalArgumentException](
11761171
GetTimestamp(Literal(9223372036854776L), Literal("milli")),
1177-
"input='9223372036854776' is not valid,\n" +
1178-
"which valid range is from -9223372036854775 to 9223372036854775 for 'milli'"
1172+
"input [9223372036854776] not from " +
1173+
"-9223372036854775 to 9223372036854775 for format milli"
11791174
)
11801175

1176+
checkExceptionInExpression[IllegalArgumentException](
1177+
GetTimestamp(Literal(-9223372036854776L), Literal("milli")),
1178+
"input [-9223372036854776] not from " +
1179+
"-9223372036854775 to 9223372036854775 for format milli"
1180+
)
1181+
1182+
// micro range could not out of range
1183+
1184+
// illegal format check
11811185
checkExceptionInExpression[IllegalArgumentException](
11821186
GetTimestamp(
11831187
Literal(1580184371847123L),

0 commit comments

Comments
 (0)