Skip to content

Commit fee890e

Browse files
committed
Enable for UTC
1 parent b360af5 commit fee890e

2 files changed

Lines changed: 44 additions & 23 deletions

File tree

spark/src/main/scala/org/apache/comet/expressions/CometCast.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,8 @@ object CometCast extends CometExpressionSerde[Cast] with CometExprShim {
217217
Compatible(Some("Only supports years between 262143 BC and 262142 AD"))
218218
case DataTypes.TimestampType if timeZoneId.exists(tz => tz != "UTC") =>
219219
Incompatible(Some(s"Cast will use UTC instead of $timeZoneId"))
220-
case DataTypes.TimestampType if evalMode == CometEvalMode.ANSI =>
221-
Incompatible(Some("ANSI mode not supported"))
222220
case DataTypes.TimestampType =>
223-
// https://github.com/apache/datafusion-comet/issues/328
224-
Incompatible(Some("Not all valid formats are supported"))
221+
Compatible(Some("Only supports years between 262143 BC and 262142 AD"))
225222
case _ =>
226223
unsupported(DataTypes.StringType, toType)
227224
}

spark/src/test/scala/org/apache/comet/CometCastSuite.scala

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -954,19 +954,40 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper {
954954
}
955955
}
956956

957-
test("cast StringType to TimestampType disabled by default") {
958-
withSQLConf((SQLConf.SESSION_LOCAL_TIMEZONE.key, "UTC")) {
959-
val values = Seq("2020-01-01T12:34:56.123456", "T2").toDF("a")
960-
castFallbackTest(
961-
values.toDF("a"),
962-
DataTypes.TimestampType,
963-
"Not all valid formats are supported")
957+
test("cast StringType to TimestampType - UTC") {
958+
withSQLConf(SQLConf.SESSION_LOCAL_TIMEZONE.key -> "UTC") {
959+
val values = Seq(
960+
"2020",
961+
"2020-01",
962+
"2020-01-01",
963+
"2020-01-01T12",
964+
"2020-01-01T12:34",
965+
"2020-01-01T12:34:56",
966+
"2020-01-01T12:34:56.123456",
967+
"T2",
968+
"-9?",
969+
"0100",
970+
"0100-01",
971+
"0100-01-01",
972+
"0100-01-01T12",
973+
"0100-01-01T12:34",
974+
"0100-01-01T12:34:56",
975+
"0100-01-01T12:34:56.123456",
976+
"10000",
977+
"10000-01",
978+
"10000-01-01",
979+
"10000-01-01T12",
980+
"10000-01-01T12:34",
981+
"10000-01-01T12:34:56",
982+
"10000-01-01T12:34:56.123456")
983+
castTimestampTest(values.toDF("a"), DataTypes.TimestampType, assertNative = true)
964984
}
965985
}
966986

967987
ignore("cast StringType to TimestampType") {
968-
// https://github.com/apache/datafusion-comet/issues/328
969-
withSQLConf((CometConf.getExprAllowIncompatConfigKey(classOf[Cast]), "true")) {
988+
// TODO: enable once all Spark timestamp formats are supported natively
989+
// (e.g. time-only formats like "T12:34" are accepted by Spark but not yet by Comet)
990+
withSQLConf((SQLConf.SESSION_LOCAL_TIMEZONE.key, "UTC")) {
970991
val values = Seq("2020-01-01T12:34:56.123456", "T2") ++ gen.generateStrings(
971992
dataSize,
972993
timestampPattern,
@@ -1021,12 +1042,10 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper {
10211042
castTimestampTest(values.toDF("a"), DataTypes.TimestampType)
10221043
}
10231044

1024-
// test for invalid inputs
1025-
withSQLConf(
1026-
SQLConf.SESSION_LOCAL_TIMEZONE.key -> "UTC",
1027-
CometConf.getExprAllowIncompatConfigKey(classOf[Cast]) -> "true") {
1045+
// test for invalid inputs with UTC - asserts native execution
1046+
withSQLConf(SQLConf.SESSION_LOCAL_TIMEZONE.key -> "UTC") {
10281047
val values = Seq("-9?", "1-", "0.5")
1029-
castTimestampTest(values.toDF("a"), DataTypes.TimestampType)
1048+
castTimestampTest(values.toDF("a"), DataTypes.TimestampType, assertNative = true)
10301049
}
10311050
}
10321051

@@ -1492,25 +1511,30 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper {
14921511
}
14931512
}
14941513

1495-
private def castTimestampTest(input: DataFrame, toType: DataType) = {
1514+
private def castTimestampTest(
1515+
input: DataFrame,
1516+
toType: DataType,
1517+
assertNative: Boolean = false) = {
14961518
withTempPath { dir =>
14971519
val data = roundtripParquet(input, dir).coalesce(1)
14981520
data.createOrReplaceTempView("t")
14991521

15001522
withSQLConf((SQLConf.ANSI_ENABLED.key, "false")) {
15011523
// cast() should return null for invalid inputs when ansi mode is disabled
15021524
val df = data.withColumn("converted", col("a").cast(toType))
1503-
checkSparkAnswer(df)
1525+
if (assertNative) {
1526+
checkSparkAnswerAndOperator(df)
1527+
} else {
1528+
checkSparkAnswer(df)
1529+
}
15041530

15051531
// try_cast() should always return null for invalid inputs
15061532
val df2 = spark.sql(s"select try_cast(a as ${toType.sql}) from t")
15071533
checkSparkAnswer(df2)
15081534
}
15091535

15101536
// with ANSI enabled, we should produce the same exception as Spark
1511-
withSQLConf(
1512-
SQLConf.ANSI_ENABLED.key -> "true",
1513-
CometConf.getExprAllowIncompatConfigKey(classOf[Cast]) -> "true") {
1537+
withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
15141538
val df = data.withColumn("converted", col("a").cast(toType))
15151539
checkSparkAnswerMaybeThrows(df) match {
15161540
case (None, None) =>

0 commit comments

Comments
 (0)