Skip to content
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 @@ -1043,4 +1043,13 @@ class CHSparkPlanExecApi extends SparkPlanExecApi with Logging {
extract.get.last,
original)
}

override def genMonthsBetweenTransformer(
substraitExprName: String,
date1: ExpressionTransformer,
date2: ExpressionTransformer,
roundOff: ExpressionTransformer,
original: MonthsBetween): ExpressionTransformer = {
CHMonthsBetweenTransformer(substraitExprName, date1, date2, roundOff, original)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,16 @@ case class CHTimestampAddTransformer(
Seq(LiteralTransformer(unit), left, right, LiteralTransformer(timeZoneId))
}
}

case class CHMonthsBetweenTransformer(
substraitExprName: String,
date1: ExpressionTransformer,
date2: ExpressionTransformer,
roundOff: ExpressionTransformer,
original: MonthsBetween)
extends ExpressionTransformer {
override def children: Seq[ExpressionTransformer] = {
val timeZoneId = original.timeZoneId.map(timeZoneId => LiteralTransformer(timeZoneId))
Seq(date1, date2, roundOff) ++ timeZoneId
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1064,4 +1064,13 @@ class VeloxSparkPlanExecApi extends SparkPlanExecApi {
original: Expression): ExpressionTransformer = {
ToUnixTimestampTransformer(substraitExprName, timeExp, format, original)
}

override def genMonthsBetweenTransformer(
substraitExprName: String,
date1: ExpressionTransformer,
date2: ExpressionTransformer,
roundOff: ExpressionTransformer,
original: MonthsBetween): ExpressionTransformer = {
MonthsBetweenTransformer(substraitExprName, date1, date2, roundOff, original)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,21 @@ abstract class DateFunctionsValidateSuite extends FunctionsValidateSuite {
}
}
}

test("months_between") {
withTempPath {
path =>
val t1 = Timestamp.valueOf("1997-02-28 10:30:00")
val t2 = Timestamp.valueOf("1996-10-30 00:00:00")
Seq((t1, t2)).toDF("t1", "t2").write.parquet(path.getCanonicalPath)

spark.read.parquet(path.getCanonicalPath).createOrReplaceTempView("time")
runQueryAndCompare("select months_between(t1, t2) from time") {
checkGlutenOperatorMatch[ProjectExecTransformer]
}
runQueryAndCompare("select months_between(t1, t2, false) from time") {
checkGlutenOperatorMatch[ProjectExecTransformer]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,13 @@ trait SparkPlanExecApi {
throw new GlutenNotSupportException("timestampdiff is not supported")
}

def genMonthsBetweenTransformer(
substraitExprName: String,
date1: ExpressionTransformer,
date2: ExpressionTransformer,
roundOff: ExpressionTransformer,
original: MonthsBetween): ExpressionTransformer

def isRowIndexMetadataColumn(columnName: String): Boolean = {
SparkShimLoader.getSparkShims.isRowIndexMetadataColumn(columnName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ case class MonthsBetweenTransformer(
original: MonthsBetween)
extends ExpressionTransformer {
override def children: Seq[ExpressionTransformer] = {
val timeZoneId = original.timeZoneId.map(timeZoneId => LiteralTransformer(timeZoneId))
Seq(date1, date2, roundOff) ++ timeZoneId
Seq(date1, date2, roundOff)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ object ExpressionConverter extends SQLConfHelper with Logging {
t
)
case m: MonthsBetween =>
MonthsBetweenTransformer(
BackendsApiManager.getSparkPlanExecApiInstance.genMonthsBetweenTransformer(
substraitExprName,
replaceWithExpressionTransformer0(m.date1, attributeSeq, expressionsMap),
replaceWithExpressionTransformer0(m.date2, attributeSeq, expressionsMap),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ class ClickHouseTestSettings extends BackendTestSettings {
.exclude("add_months")
.exclude("SPARK-34721: add a year-month interval to a date")
.exclude("months_between")
.excludeGlutenTest("months_between")
.exclude("next_day")
.exclude("TruncDate")
.exclude("TruncTimestamp")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ class VeloxTestSettings extends BackendTestSettings {
.exclude("to_timestamp exception mode")
// Replaced by a gluten test to pass timezone through config.
.exclude("from_unixtime")
// Replaced by a gluten test to pass timezone through config.
.exclude("months_between")
// https://github.com/facebookincubator/velox/pull/10563/files#diff-140dc50e6dac735f72d29014da44b045509df0dd1737f458de1fe8cfd33d8145
.excludeGlutenTest("from_unixtime")
enableSuite[GlutenDecimalExpressionSuite]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.apache.spark.sql.catalyst.util.DateTimeTestUtils._
import org.apache.spark.sql.catalyst.util.DateTimeUtils
import org.apache.spark.sql.catalyst.util.DateTimeUtils.{getZoneId, TimeZoneUTC}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{DateType, IntegerType, LongType, StringType, TimestampNTZType, TimestampType}
import org.apache.spark.sql.types._
import org.apache.spark.unsafe.types.UTF8String

import java.sql.{Date, Timestamp}
Expand Down Expand Up @@ -476,4 +476,83 @@ class GlutenDateExpressionsSuite extends DateExpressionsSuite with GlutenTestsTr
}
}
}

testGluten("months_between") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like this test is added for all 4 spark versions, also there is a new test for velox backend, is this intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a rewritten spark unit test, and the other is a test to ensure offload.

val sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US)
for (zid <- outstandingZoneIds) {
withSQLConf(
SQLConf.SESSION_LOCAL_TIMEZONE.key -> zid.getId
) {
val timeZoneId = Option(zid.getId)
sdf.setTimeZone(TimeZone.getTimeZone(zid))

checkEvaluation(
MonthsBetween(
Literal(new Timestamp(sdf.parse("1997-02-28 10:30:00").getTime)),
Literal(new Timestamp(sdf.parse("1996-10-30 00:00:00").getTime)),
Literal.TrueLiteral,
timeZoneId = timeZoneId
),
3.94959677
)
checkEvaluation(
MonthsBetween(
Literal(new Timestamp(sdf.parse("1997-02-28 10:30:00").getTime)),
Literal(new Timestamp(sdf.parse("1996-10-30 00:00:00").getTime)),
Literal.FalseLiteral,
timeZoneId = timeZoneId
),
3.9495967741935485
)

Seq(Literal.FalseLiteral, Literal.TrueLiteral).foreach {
roundOff =>
checkEvaluation(
MonthsBetween(
Literal(new Timestamp(sdf.parse("2015-01-30 11:52:00").getTime)),
Literal(new Timestamp(sdf.parse("2015-01-30 11:50:00").getTime)),
roundOff,
timeZoneId = timeZoneId
),
0.0
)
checkEvaluation(
MonthsBetween(
Literal(new Timestamp(sdf.parse("2015-01-31 00:00:00").getTime)),
Literal(new Timestamp(sdf.parse("2015-03-31 22:00:00").getTime)),
roundOff,
timeZoneId = timeZoneId
),
-2.0
)
checkEvaluation(
MonthsBetween(
Literal(new Timestamp(sdf.parse("2015-03-31 22:00:00").getTime)),
Literal(new Timestamp(sdf.parse("2015-02-28 00:00:00").getTime)),
roundOff,
timeZoneId = timeZoneId
),
1.0
)
}
val t = Literal(Timestamp.valueOf("2015-03-31 22:00:00"))
val tnull = Literal.create(null, TimestampType)
checkEvaluation(MonthsBetween(t, tnull, Literal.TrueLiteral, timeZoneId = timeZoneId), null)
checkEvaluation(MonthsBetween(tnull, t, Literal.TrueLiteral, timeZoneId = timeZoneId), null)
checkEvaluation(
MonthsBetween(tnull, tnull, Literal.TrueLiteral, timeZoneId = timeZoneId),
null)
checkEvaluation(
MonthsBetween(t, t, Literal.create(null, BooleanType), timeZoneId = timeZoneId),
null)
checkConsistencyBetweenInterpretedAndCodegen(
(time1: Expression, time2: Expression, roundOff: Expression) =>
MonthsBetween(time1, time2, roundOff, timeZoneId = timeZoneId),
TimestampType,
TimestampType,
BooleanType
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ class ClickHouseTestSettings extends BackendTestSettings {
.exclude("add_months")
.exclude("SPARK-34721: add a year-month interval to a date")
.exclude("months_between")
.excludeGlutenTest("months_between")
.exclude("next_day")
.exclude("TruncDate")
.exclude("TruncTimestamp")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ class VeloxTestSettings extends BackendTestSettings {
.exclude("to_timestamp exception mode")
// Replaced by a gluten test to pass timezone through config.
.exclude("from_unixtime")
// Replaced by a gluten test to pass timezone through config.
.exclude("months_between")
.exclude("test timestamp add")
// https://github.com/facebookincubator/velox/pull/10563/files#diff-140dc50e6dac735f72d29014da44b045509df0dd1737f458de1fe8cfd33d8145
.excludeGlutenTest("from_unixtime")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,83 @@ class GlutenDateExpressionsSuite extends DateExpressionsSuite with GlutenTestsTr
TimestampAdd("YEAR", Literal(1), Literal(Timestamp.valueOf("2022-02-15 12:57:00"))),
Timestamp.valueOf("2023-02-15 12:57:00"))
}

testGluten("months_between") {
val sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US)
for (zid <- outstandingZoneIds) {
withSQLConf(
SQLConf.SESSION_LOCAL_TIMEZONE.key -> zid.getId
) {
val timeZoneId = Option(zid.getId)
sdf.setTimeZone(TimeZone.getTimeZone(zid))

checkEvaluation(
MonthsBetween(
Literal(new Timestamp(sdf.parse("1997-02-28 10:30:00").getTime)),
Literal(new Timestamp(sdf.parse("1996-10-30 00:00:00").getTime)),
Literal.TrueLiteral,
timeZoneId = timeZoneId
),
3.94959677
)
checkEvaluation(
MonthsBetween(
Literal(new Timestamp(sdf.parse("1997-02-28 10:30:00").getTime)),
Literal(new Timestamp(sdf.parse("1996-10-30 00:00:00").getTime)),
Literal.FalseLiteral,
timeZoneId = timeZoneId
),
3.9495967741935485
)

Seq(Literal.FalseLiteral, Literal.TrueLiteral).foreach {
roundOff =>
checkEvaluation(
MonthsBetween(
Literal(new Timestamp(sdf.parse("2015-01-30 11:52:00").getTime)),
Literal(new Timestamp(sdf.parse("2015-01-30 11:50:00").getTime)),
roundOff,
timeZoneId = timeZoneId
),
0.0
)
checkEvaluation(
MonthsBetween(
Literal(new Timestamp(sdf.parse("2015-01-31 00:00:00").getTime)),
Literal(new Timestamp(sdf.parse("2015-03-31 22:00:00").getTime)),
roundOff,
timeZoneId = timeZoneId
),
-2.0
)
checkEvaluation(
MonthsBetween(
Literal(new Timestamp(sdf.parse("2015-03-31 22:00:00").getTime)),
Literal(new Timestamp(sdf.parse("2015-02-28 00:00:00").getTime)),
roundOff,
timeZoneId = timeZoneId
),
1.0
)
}
val t = Literal(Timestamp.valueOf("2015-03-31 22:00:00"))
val tnull = Literal.create(null, TimestampType)
checkEvaluation(MonthsBetween(t, tnull, Literal.TrueLiteral, timeZoneId = timeZoneId), null)
checkEvaluation(MonthsBetween(tnull, t, Literal.TrueLiteral, timeZoneId = timeZoneId), null)
checkEvaluation(
MonthsBetween(tnull, tnull, Literal.TrueLiteral, timeZoneId = timeZoneId),
null)
checkEvaluation(
MonthsBetween(t, t, Literal.create(null, BooleanType), timeZoneId = timeZoneId),
null)
checkConsistencyBetweenInterpretedAndCodegen(
(time1: Expression, time2: Expression, roundOff: Expression) =>
MonthsBetween(time1, time2, roundOff, timeZoneId = timeZoneId),
TimestampType,
TimestampType,
BooleanType
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ class ClickHouseTestSettings extends BackendTestSettings {
.exclude("add_months")
.exclude("SPARK-34721: add a year-month interval to a date")
.exclude("months_between")
.excludeGlutenTest("months_between")
.exclude("next_day")
.exclude("TruncDate")
.exclude("TruncTimestamp")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class VeloxTestSettings extends BackendTestSettings {
.exclude("to_timestamp exception mode")
// Replaced by a gluten test to pass timezone through config.
.exclude("from_unixtime")
// Replaced by a gluten test to pass timezone through config.
.exclude("months_between")
// Vanilla Spark does not have a unified DST Timestamp fastTime. 1320570000000L and
// 1320566400000L both represent 2011-11-06 01:00:00
.exclude("SPARK-42635: timestampadd near daylight saving transition")
Expand Down
Loading