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

[NSE-955] Support more date format in unix timestamp #1063

Merged
merged 4 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -513,7 +515,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))))
}
}
}