Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Commit

Permalink
[NSE-955] Support more date format in unix timestamp (#1063)
Browse files Browse the repository at this point in the history
* Initial commit

* Add a UT

* Change arrow branch [will revert at last]

* Revert "Change arrow branch [will revert at last]"

This reverts commit e55df88.
  • Loading branch information
PHILO-HE authored Aug 12, 2022
1 parent 9567587 commit 875275d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ object ColumnarDateTimeExpressions {
val yearMonthDayFormat = "yyyy-MM-dd"
val yearMonthDayTimeFormat = "yyyy-MM-dd HH:mm:ss"
val yearMonthDayTimeNoSepFormat = "yyyyMMddHHmmss"
val yearMonthDayNoSepFormat = "yyyyMMdd"
var formatLiteral: String = null

buildCheck()
Expand All @@ -474,7 +475,8 @@ object ColumnarDateTimeExpressions {
// Only support yyyy-MM-dd or yyyy-MM-dd HH:mm:ss.
if (!this.formatLiteral.equals(yearMonthDayFormat) &&
!this.formatLiteral.equals(yearMonthDayTimeFormat) &&
!this.formatLiteral.equals(yearMonthDayTimeNoSepFormat)) {
!this.formatLiteral.equals(yearMonthDayTimeNoSepFormat) &&
!this.formatLiteral.equals(yearMonthDayNoSepFormat)) {
throw new UnsupportedOperationException(
s"$formatLiteral is not supported in ColumnarUnixTimestamp.")
}
Expand Down Expand Up @@ -518,7 +520,8 @@ object ColumnarDateTimeExpressions {
TreeBuilder.makeFunction("divide", Lists.newArrayList(
ConverterUtils.subtractTimestampOffset(castNode),
TreeBuilder.makeLiteral(java.lang.Long.valueOf(1000L))), outType)
} else if (this.formatLiteral.equals(yearMonthDayTimeNoSepFormat)) {
} else if (this.formatLiteral.equals(yearMonthDayTimeNoSepFormat) ||
this.formatLiteral.equals(yearMonthDayNoSepFormat)) {
val timestampType = new ArrowType.Timestamp(TimeUnit.MILLISECOND, "UTC")
val timestampNode = TreeBuilder.makeFunction("castTIMESTAMP_withCarrying_withoutSep",
Lists.newArrayList(leftNode), timestampType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,21 @@ class DateTimeSuite extends QueryTest with SharedSparkSession {
Seq(Row(java.lang.Long.valueOf(1248940800L)),
Row(java.lang.Long.valueOf(1249027200L)),
Row(java.lang.Long.valueOf(1249113600L))))

// Test date format: yyyyMMdd
val datesNoSep = Seq("20090730", "20090731", "20090801")
.map(s => Tuple1(s)).toDF("time")
datesNoSep.createOrReplaceTempView("dates_no_sep")
val frameNoSep = sql("SELECT unix_timestamp(time, 'yyyyMMdd') FROM dates_no_sep")
frameNoSep.explain()
frameNoSep.show()
assert(frameNoSep.queryExecution.executedPlan.find(p => p
.isInstanceOf[ColumnarConditionProjectExec]).isDefined)
checkAnswer(
frameNoSep,
Seq(Row(java.lang.Long.valueOf(1248940800L)),
Row(java.lang.Long.valueOf(1249027200L)),
Row(java.lang.Long.valueOf(1249113600L))))
}
}
}

0 comments on commit 875275d

Please sign in to comment.