Skip to content

Commit ed1edc7

Browse files
author
Davies Liu
committed
address comments for #7805
1 parent 0244170 commit ed1edc7

File tree

6 files changed

+22
-22
lines changed

6 files changed

+22
-22
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ case class ToDate(child: Expression) extends UnaryExpression with ImplicitCastIn
710710
override def eval(input: InternalRow): Any = child.eval(input)
711711

712712
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
713-
defineCodeGen(ctx, ev, d => d)
713+
child.genCode(ctx, ev)
714714
}
715715
}
716716

@@ -726,23 +726,23 @@ case class TruncDate(date: Expression, format: Expression)
726726
override def dataType: DataType = DateType
727727
override def prettyName: String = "trunc"
728728

729-
lazy val minItemConst = DateTimeUtils.parseTruncLevel(format.eval().asInstanceOf[UTF8String])
729+
private lazy val truncLevel = DateTimeUtils.parseTruncLevel(format.eval().asInstanceOf[UTF8String])
730730

731731
override def eval(input: InternalRow): Any = {
732-
val minItem = if (format.foldable) {
733-
minItemConst
732+
val level = if (format.foldable) {
733+
truncLevel
734734
} else {
735735
DateTimeUtils.parseTruncLevel(format.eval().asInstanceOf[UTF8String])
736736
}
737-
if (minItem == -1) {
737+
if (level == -1) {
738738
// unknown format
739739
null
740740
} else {
741741
val d = date.eval(input)
742742
if (d == null) {
743743
null
744744
} else {
745-
DateTimeUtils.truncDate(d.asInstanceOf[Int], minItem)
745+
DateTimeUtils.truncDate(d.asInstanceOf[Int], level)
746746
}
747747
}
748748
}
@@ -751,7 +751,7 @@ case class TruncDate(date: Expression, format: Expression)
751751
val dtu = DateTimeUtils.getClass.getName.stripSuffix("$")
752752

753753
if (format.foldable) {
754-
if (minItemConst == -1) {
754+
if (truncLevel == -1) {
755755
s"""
756756
boolean ${ev.isNull} = true;
757757
${ctx.javaType(dataType)} ${ev.primitive} = ${ctx.defaultValue(dataType)};
@@ -763,7 +763,7 @@ case class TruncDate(date: Expression, format: Expression)
763763
boolean ${ev.isNull} = ${d.isNull};
764764
${ctx.javaType(dataType)} ${ev.primitive} = ${ctx.defaultValue(dataType)};
765765
if (!${ev.isNull}) {
766-
${ev.primitive} = $dtu.truncDate(${d.primitive}, $minItemConst);
766+
${ev.primitive} = $dtu.truncDate(${d.primitive}, $truncLevel);
767767
}
768768
"""
769769
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,8 @@ object DateTimeUtils {
794794
} else if (level == TRUNC_TO_MONTH) {
795795
d - DateTimeUtils.getDayOfMonth(d) + 1
796796
} else {
797-
throw new Exception(s"Invalid trunc level: $level")
797+
// caller make sure that this should never be reached
798+
sys.error(s"Invalid trunc level: $level")
798799
}
799800
}
800801

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
363363
checkEvaluation(TruncDate(Literal.create(input, DateType), Literal.create(fmt, StringType)),
364364
expected)
365365
checkEvaluation(
366-
TruncDate(Literal.create(input, DateType), NonFoldableLiteral.create(fmt, StringType)),
366+
TruncDate(Literal.create(input, DateType), NonFoldableLiteral(fmt, StringType)),
367367
expected)
368368
}
369369
val date = Date.valueOf("2015-07-22")

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,4 @@ object NonFoldableLiteral {
4747
val lit = Literal(value)
4848
NonFoldableLiteral(lit.value, lit.dataType)
4949
}
50-
def create(value: Any, dataType: DataType): NonFoldableLiteral = {
51-
val lit = Literal.create(value, dataType)
52-
NonFoldableLiteral(lit.value, lit.dataType)
53-
}
5450
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,13 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
192192
checkEvaluation(Literal.create("a", StringType).like(Literal.create(null, StringType)), null)
193193
checkEvaluation(Literal.create(null, StringType).like(Literal.create(null, StringType)), null)
194194
checkEvaluation(
195-
Literal.create("a", StringType).like(NonFoldableLiteral.create("a", StringType)), true)
195+
Literal.create("a", StringType).like(NonFoldableLiteral("a", StringType)), true)
196196
checkEvaluation(
197-
Literal.create("a", StringType).like(NonFoldableLiteral.create(null, StringType)), null)
197+
Literal.create("a", StringType).like(NonFoldableLiteral(null, StringType)), null)
198198
checkEvaluation(
199-
Literal.create(null, StringType).like(NonFoldableLiteral.create("a", StringType)), null)
199+
Literal.create(null, StringType).like(NonFoldableLiteral("a", StringType)), null)
200200
checkEvaluation(
201-
Literal.create(null, StringType).like(NonFoldableLiteral.create(null, StringType)), null)
201+
Literal.create(null, StringType).like(NonFoldableLiteral(null, StringType)), null)
202202

203203
checkEvaluation("abdef" like "abdef", true)
204204
checkEvaluation("a_%b" like "a\\__b", true)
@@ -241,12 +241,12 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
241241
checkEvaluation(Literal.create(null, StringType) rlike "abdef", null)
242242
checkEvaluation("abdef" rlike Literal.create(null, StringType), null)
243243
checkEvaluation(Literal.create(null, StringType) rlike Literal.create(null, StringType), null)
244-
checkEvaluation("abdef" rlike NonFoldableLiteral.create("abdef", StringType), true)
245-
checkEvaluation("abdef" rlike NonFoldableLiteral.create(null, StringType), null)
244+
checkEvaluation("abdef" rlike NonFoldableLiteral("abdef", StringType), true)
245+
checkEvaluation("abdef" rlike NonFoldableLiteral(null, StringType), null)
246246
checkEvaluation(
247-
Literal.create(null, StringType) rlike NonFoldableLiteral.create("abdef", StringType), null)
247+
Literal.create(null, StringType) rlike NonFoldableLiteral("abdef", StringType), null)
248248
checkEvaluation(
249-
Literal.create(null, StringType) rlike NonFoldableLiteral.create(null, StringType), null)
249+
Literal.create(null, StringType) rlike NonFoldableLiteral(null, StringType), null)
250250

251251
checkEvaluation("abdef" rlike "abdef", true)
252252
checkEvaluation("abbbbc" rlike "a.*c", true)

sql/core/src/main/scala/org/apache/spark/sql/functions.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,9 @@ object functions {
21922192
/**
21932193
* Returns date truncated to the unit specified by the format.
21942194
*
2195+
* @param format: 'year', 'yyyy', 'yy' for truncate by year,
2196+
* or 'month', 'mon', 'mm' for truncate by month
2197+
*
21952198
* @group datetime_funcs
21962199
* @since 1.5.0
21972200
*/

0 commit comments

Comments
 (0)