Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GLUTEN-8581][VL] Fix Spark legacy date formatter under case insensitive configuration #8583

Merged
merged 4 commits into from
Jan 25, 2025
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
3 changes: 2 additions & 1 deletion cpp/velox/compute/WholeStageResultIterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,8 @@ std::unordered_map<std::string, std::string> WholeStageResultIterator::getQueryC

configs[velox::core::QueryConfig::kSparkPartitionId] = std::to_string(taskInfo_.partitionId);

// Enable Spark legacy date formatter if spark.sql.legacy.timeParserPolicy is set to 'LEGACY'.
// Enable Spark legacy date formatter if spark.sql.legacy.timeParserPolicy is set to 'LEGACY'
// or 'legacy'
if (veloxCfg_->get<std::string>(kSparkLegacyTimeParserPolicy, "") == "LEGACY") {
configs[velox::core::QueryConfig::kSparkLegacyDateFormatter] = "true";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,6 @@ class VeloxTestSettings extends BackendTestSettings {
.exclude("to_unix_timestamp")
// Unsupported datetime format: specifier X is not supported by velox.
.exclude("to_timestamp with microseconds precision")
// Replaced by another test.
.exclude("to_timestamp")
// Legacy mode is not supported, assuming this mode is not commonly used.
.exclude("SPARK-30668: use legacy timestamp parser in to_timestamp")
// Legacy mode is not supported and velox getTimestamp function does not throw
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,9 @@ class GlutenDateFunctionsSuite extends DateFunctionsSuite with GlutenSQLTestsTra
df1.selectExpr(s"unix_timestamp(x, 'yyyy-MM-dd mm:HH:ss')"),
Seq(Row(secs(ts4.getTime)), Row(null), Row(secs(ts3.getTime)), Row(null)))

// legacyParserPolicy is not respected by Gluten.
// invalid format
// val invalid = df1.selectExpr(s"unix_timestamp(x, 'yyyy-MM-dd aa:HH:ss')")
// if (legacyParserPolicy == "legacy") {
// checkAnswer(invalid,
// Seq(Row(null), Row(null), Row(null), Row(null)))
// } else {
// val e = intercept[SparkUpgradeException](invalid.collect())
// assert(e.getCause.isInstanceOf[IllegalArgumentException])
// assert(e.getMessage.contains(
// "You may get a different result due to the upgrading to Spark"))
// }

val invalid = df1.selectExpr(s"unix_timestamp(x, 'yyyy-MM-dd aa:HH:ss')")
checkAnswer(invalid, Seq(Row(null), Row(null), Row(null), Row(null)))
// February
val y1 = "2016-02-29"
val y2 = "2017-02-29"
Expand Down Expand Up @@ -198,53 +188,8 @@ class GlutenDateFunctionsSuite extends DateFunctionsSuite with GlutenSQLTestsTra
df2.select(unix_timestamp(col("y"), "yyyy-MM-dd")),
Seq(Row(secs(ts5.getTime)), Row(null)))

// Not consistent behavior with gluten + velox.
// invalid format
// val invalid = df1.selectExpr(s"to_unix_timestamp(x, 'yyyy-MM-dd bb:HH:ss')")
// val e = intercept[IllegalArgumentException](invalid.collect())
// assert(e.getMessage.contains('b'))
}
}
}

// Ported from spark with a test case for legacy mode removed.
testGluten("to_timestamp") {
Seq("legacy", "corrected").foreach {
legacyParserPolicy =>
withSQLConf(SQLConf.LEGACY_TIME_PARSER_POLICY.key -> legacyParserPolicy) {
val date1 = Date.valueOf("2015-07-24")
val date2 = Date.valueOf("2015-07-25")
val ts_date1 = Timestamp.valueOf("2015-07-24 00:00:00")
val ts_date2 = Timestamp.valueOf("2015-07-25 00:00:00")
val ts1 = Timestamp.valueOf("2015-07-24 10:00:00")
val ts2 = Timestamp.valueOf("2015-07-25 02:02:02")
val s1 = "2015/07/24 10:00:00.5"
val s2 = "2015/07/25 02:02:02.6"
val ts1m = Timestamp.valueOf("2015-07-24 10:00:00.5")
val ts2m = Timestamp.valueOf("2015-07-25 02:02:02.6")
val ss1 = "2015-07-24 10:00:00"
val ss2 = "2015-07-25 02:02:02"
val fmt = "yyyy/MM/dd HH:mm:ss.S"
val df = Seq((date1, ts1, s1, ss1), (date2, ts2, s2, ss2)).toDF("d", "ts", "s", "ss")

checkAnswer(
df.select(to_timestamp(col("ss"))),
df.select(timestamp_seconds(unix_timestamp(col("ss")))))
checkAnswer(df.select(to_timestamp(col("ss"))), Seq(Row(ts1), Row(ts2)))
if (legacyParserPolicy == "legacy") {
// In Spark 2.4 and earlier, to_timestamp() parses in seconds precision and cuts off
// the fractional part of seconds. The behavior was changed by SPARK-27438.
// Ignore this test case. Velox returns null for such case.
// val legacyFmt = "yyyy/MM/dd HH:mm:ss"
// checkAnswer(df.select(to_timestamp(col("s"), legacyFmt)), Seq(
// Row(ts1), Row(ts2)))
} else {
checkAnswer(df.select(to_timestamp(col("s"), fmt)), Seq(Row(ts1m), Row(ts2m)))
}
checkAnswer(df.select(to_timestamp(col("ts"), fmt)), Seq(Row(ts1), Row(ts2)))
checkAnswer(
df.select(to_timestamp(col("d"), "yyyy-MM-dd")),
Seq(Row(ts_date1), Row(ts_date2)))
val invalid = df1.selectExpr(s"to_unix_timestamp(x, 'yyyy-MM-dd bb:HH:ss')")
checkAnswer(invalid, Seq(Row(null), Row(null), Row(null), Row(null)))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1070,8 +1070,6 @@ class VeloxTestSettings extends BackendTestSettings {
.exclude("to_unix_timestamp")
// Unsupported datetime format: specifier X is not supported by velox.
.exclude("to_timestamp with microseconds precision")
// Replaced by another test.
.exclude("to_timestamp")
// Legacy mode is not supported, assuming this mode is not commonly used.
.exclude("SPARK-30668: use legacy timestamp parser in to_timestamp")
// Legacy mode is not supported and velox getTimestamp function does not throw
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,9 @@ class GlutenDateFunctionsSuite extends DateFunctionsSuite with GlutenSQLTestsTra
df1.selectExpr(s"unix_timestamp(x, 'yyyy-MM-dd mm:HH:ss')"),
Seq(Row(secs(ts4.getTime)), Row(null), Row(secs(ts3.getTime)), Row(null)))

// legacyParserPolicy is not respected by Gluten.
// invalid format
// val invalid = df1.selectExpr(s"unix_timestamp(x, 'yyyy-MM-dd aa:HH:ss')")
// if (legacyParserPolicy == "legacy") {
// checkAnswer(invalid,
// Seq(Row(null), Row(null), Row(null), Row(null)))
// } else {
// val e = intercept[SparkUpgradeException](invalid.collect())
// assert(e.getCause.isInstanceOf[IllegalArgumentException])
// assert( e.getMessage.contains(
// "You may get a different result due to the upgrading to Spark"))
// }
val invalid = df1.selectExpr(s"unix_timestamp(x, 'yyyy-MM-dd aa:HH:ss')")
checkAnswer(invalid, Seq(Row(null), Row(null), Row(null), Row(null)))

// February
val y1 = "2016-02-29"
Expand Down Expand Up @@ -196,53 +187,8 @@ class GlutenDateFunctionsSuite extends DateFunctionsSuite with GlutenSQLTestsTra
df2.select(unix_timestamp(col("y"), "yyyy-MM-dd")),
Seq(Row(secs(ts5.getTime)), Row(null)))

// Not consistent behavior with gluten + velox.
// invalid format
// val invalid = df1.selectExpr(s"to_unix_timestamp(x, 'yyyy-MM-dd bb:HH:ss')")
// val e = intercept[IllegalArgumentException](invalid.collect())
// assert(e.getMessage.contains('b'))
}
}
}

// Ported from spark with a test case for legacy mode removed.
testGluten("to_timestamp") {
Seq("legacy", "corrected").foreach {
legacyParserPolicy =>
withSQLConf(SQLConf.LEGACY_TIME_PARSER_POLICY.key -> legacyParserPolicy) {
val date1 = Date.valueOf("2015-07-24")
val date2 = Date.valueOf("2015-07-25")
val ts_date1 = Timestamp.valueOf("2015-07-24 00:00:00")
val ts_date2 = Timestamp.valueOf("2015-07-25 00:00:00")
val ts1 = Timestamp.valueOf("2015-07-24 10:00:00")
val ts2 = Timestamp.valueOf("2015-07-25 02:02:02")
val s1 = "2015/07/24 10:00:00.5"
val s2 = "2015/07/25 02:02:02.6"
val ts1m = Timestamp.valueOf("2015-07-24 10:00:00.5")
val ts2m = Timestamp.valueOf("2015-07-25 02:02:02.6")
val ss1 = "2015-07-24 10:00:00"
val ss2 = "2015-07-25 02:02:02"
val fmt = "yyyy/MM/dd HH:mm:ss.S"
val df = Seq((date1, ts1, s1, ss1), (date2, ts2, s2, ss2)).toDF("d", "ts", "s", "ss")

checkAnswer(
df.select(to_timestamp(col("ss"))),
df.select(timestamp_seconds(unix_timestamp(col("ss")))))
checkAnswer(df.select(to_timestamp(col("ss"))), Seq(Row(ts1), Row(ts2)))
if (legacyParserPolicy == "legacy") {
// In Spark 2.4 and earlier, to_timestamp() parses in seconds precision and cuts off
// the fractional part of seconds. The behavior was changed by SPARK-27438.
// Ignore this test case. Velox returns null for such case.
// val legacyFmt = "yyyy/MM/dd HH:mm:ss"
// checkAnswer(df.select(to_timestamp(col("s"), legacyFmt)), Seq(
// Row(ts1), Row(ts2)))
} else {
checkAnswer(df.select(to_timestamp(col("s"), fmt)), Seq(Row(ts1m), Row(ts2m)))
}
checkAnswer(df.select(to_timestamp(col("ts"), fmt)), Seq(Row(ts1), Row(ts2)))
checkAnswer(
df.select(to_timestamp(col("d"), "yyyy-MM-dd")),
Seq(Row(ts_date1), Row(ts_date2)))
val invalid = df1.selectExpr(s"to_unix_timestamp(x, 'yyyy-MM-dd bb:HH:ss')")
checkAnswer(invalid, Seq(Row(null), Row(null), Row(null), Row(null)))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1100,8 +1100,6 @@ class VeloxTestSettings extends BackendTestSettings {
.exclude("to_unix_timestamp")
// Unsupported datetime format: specifier X is not supported by velox.
.exclude("to_timestamp with microseconds precision")
// Replaced by another test.
.exclude("to_timestamp")
// Legacy mode is not supported, assuming this mode is not commonly used.
.exclude("SPARK-30668: use legacy timestamp parser in to_timestamp")
// Legacy mode is not supported and velox getTimestamp function does not throw
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,9 @@ class GlutenDateFunctionsSuite extends DateFunctionsSuite with GlutenSQLTestsTra
df1.selectExpr(s"unix_timestamp(x, 'yyyy-MM-dd mm:HH:ss')"),
Seq(Row(secs(ts4.getTime)), Row(null), Row(secs(ts3.getTime)), Row(null)))

// legacyParserPolicy is not respected by Gluten.
// invalid format
// val invalid = df1.selectExpr(s"unix_timestamp(x, 'yyyy-MM-dd aa:HH:ss')")
// if (legacyParserPolicy == "legacy") {
// checkAnswer(invalid,
// Seq(Row(null), Row(null), Row(null), Row(null)))
// } else {
// val e = intercept[SparkUpgradeException](invalid.collect())
// assert(e.getCause.isInstanceOf[IllegalArgumentException])
// assert( e.getMessage.contains(
// "You may get a different result due to the upgrading to Spark"))
// }
val invalid = df1.selectExpr(s"unix_timestamp(x, 'yyyy-MM-dd aa:HH:ss')")
checkAnswer(invalid, Seq(Row(null), Row(null), Row(null), Row(null)))

// February
val y1 = "2016-02-29"
Expand Down Expand Up @@ -196,53 +187,9 @@ class GlutenDateFunctionsSuite extends DateFunctionsSuite with GlutenSQLTestsTra
df2.select(unix_timestamp(col("y"), "yyyy-MM-dd")),
Seq(Row(secs(ts5.getTime)), Row(null)))

// Not consistent behavior with gluten + velox.
// invalid format
// val invalid = df1.selectExpr(s"to_unix_timestamp(x, 'yyyy-MM-dd bb:HH:ss')")
// val e = intercept[IllegalArgumentException](invalid.collect())
// assert(e.getMessage.contains('b'))
}
}
}

// Ported from spark with a test case for legacy mode removed.
testGluten("to_timestamp") {
Seq("legacy", "corrected").foreach {
legacyParserPolicy =>
withSQLConf(SQLConf.LEGACY_TIME_PARSER_POLICY.key -> legacyParserPolicy) {
val date1 = Date.valueOf("2015-07-24")
val date2 = Date.valueOf("2015-07-25")
val ts_date1 = Timestamp.valueOf("2015-07-24 00:00:00")
val ts_date2 = Timestamp.valueOf("2015-07-25 00:00:00")
val ts1 = Timestamp.valueOf("2015-07-24 10:00:00")
val ts2 = Timestamp.valueOf("2015-07-25 02:02:02")
val s1 = "2015/07/24 10:00:00.5"
val s2 = "2015/07/25 02:02:02.6"
val ts1m = Timestamp.valueOf("2015-07-24 10:00:00.5")
val ts2m = Timestamp.valueOf("2015-07-25 02:02:02.6")
val ss1 = "2015-07-24 10:00:00"
val ss2 = "2015-07-25 02:02:02"
val fmt = "yyyy/MM/dd HH:mm:ss.S"
val df = Seq((date1, ts1, s1, ss1), (date2, ts2, s2, ss2)).toDF("d", "ts", "s", "ss")

checkAnswer(
df.select(to_timestamp(col("ss"))),
df.select(timestamp_seconds(unix_timestamp(col("ss")))))
checkAnswer(df.select(to_timestamp(col("ss"))), Seq(Row(ts1), Row(ts2)))
if (legacyParserPolicy == "legacy") {
// In Spark 2.4 and earlier, to_timestamp() parses in seconds precision and cuts off
// the fractional part of seconds. The behavior was changed by SPARK-27438.
// Ignore this test case. Velox returns null for such case.
// val legacyFmt = "yyyy/MM/dd HH:mm:ss"
// checkAnswer(df.select(to_timestamp(col("s"), legacyFmt)), Seq(
// Row(ts1), Row(ts2)))
} else {
checkAnswer(df.select(to_timestamp(col("s"), fmt)), Seq(Row(ts1m), Row(ts2m)))
}
checkAnswer(df.select(to_timestamp(col("ts"), fmt)), Seq(Row(ts1), Row(ts2)))
checkAnswer(
df.select(to_timestamp(col("d"), "yyyy-MM-dd")),
Seq(Row(ts_date1), Row(ts_date2)))
val invalid = df1.selectExpr(s"to_unix_timestamp(x, 'yyyy-MM-dd bb:HH:ss')")
checkAnswer(invalid, Seq(Row(null), Row(null), Row(null), Row(null)))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1122,8 +1122,6 @@ class VeloxTestSettings extends BackendTestSettings {
.exclude("to_unix_timestamp")
// Unsupported datetime format: specifier X is not supported by velox.
.exclude("to_timestamp with microseconds precision")
// Replaced by another test.
.exclude("to_timestamp")
// Legacy mode is not supported, assuming this mode is not commonly used.
.exclude("SPARK-30668: use legacy timestamp parser in to_timestamp")
// Legacy mode is not supported and velox getTimestamp function does not throw
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,9 @@ class GlutenDateFunctionsSuite extends DateFunctionsSuite with GlutenSQLTestsTra
df1.selectExpr(s"unix_timestamp(x, 'yyyy-MM-dd mm:HH:ss')"),
Seq(Row(secs(ts4.getTime)), Row(null), Row(secs(ts3.getTime)), Row(null)))

// legacyParserPolicy is not respected by Gluten.
// invalid format
// val invalid = df1.selectExpr(s"unix_timestamp(x, 'yyyy-MM-dd aa:HH:ss')")
// if (legacyParserPolicy == "legacy") {
// checkAnswer(invalid,
// Seq(Row(null), Row(null), Row(null), Row(null)))
// } else {
// val e = intercept[SparkUpgradeException](invalid.collect())
// assert(e.getCause.isInstanceOf[IllegalArgumentException])
// assert( e.getMessage.contains(
// "You may get a different result due to the upgrading to Spark"))
// }
val invalid = df1.selectExpr(s"unix_timestamp(x, 'yyyy-MM-dd aa:HH:ss')")
checkAnswer(invalid, Seq(Row(null), Row(null), Row(null), Row(null)))

// February
val y1 = "2016-02-29"
Expand Down Expand Up @@ -196,53 +187,8 @@ class GlutenDateFunctionsSuite extends DateFunctionsSuite with GlutenSQLTestsTra
df2.select(unix_timestamp(col("y"), "yyyy-MM-dd")),
Seq(Row(secs(ts5.getTime)), Row(null)))

// Not consistent behavior with gluten + velox.
// invalid format
// val invalid = df1.selectExpr(s"to_unix_timestamp(x, 'yyyy-MM-dd bb:HH:ss')")
// val e = intercept[IllegalArgumentException](invalid.collect())
// assert(e.getMessage.contains('b'))
}
}
}

// Ported from spark with a test case for legacy mode removed.
testGluten("to_timestamp") {
Seq("legacy", "corrected").foreach {
legacyParserPolicy =>
withSQLConf(SQLConf.LEGACY_TIME_PARSER_POLICY.key -> legacyParserPolicy) {
val date1 = Date.valueOf("2015-07-24")
val date2 = Date.valueOf("2015-07-25")
val ts_date1 = Timestamp.valueOf("2015-07-24 00:00:00")
val ts_date2 = Timestamp.valueOf("2015-07-25 00:00:00")
val ts1 = Timestamp.valueOf("2015-07-24 10:00:00")
val ts2 = Timestamp.valueOf("2015-07-25 02:02:02")
val s1 = "2015/07/24 10:00:00.5"
val s2 = "2015/07/25 02:02:02.6"
val ts1m = Timestamp.valueOf("2015-07-24 10:00:00.5")
val ts2m = Timestamp.valueOf("2015-07-25 02:02:02.6")
val ss1 = "2015-07-24 10:00:00"
val ss2 = "2015-07-25 02:02:02"
val fmt = "yyyy/MM/dd HH:mm:ss.S"
val df = Seq((date1, ts1, s1, ss1), (date2, ts2, s2, ss2)).toDF("d", "ts", "s", "ss")

checkAnswer(
df.select(to_timestamp(col("ss"))),
df.select(timestamp_seconds(unix_timestamp(col("ss")))))
checkAnswer(df.select(to_timestamp(col("ss"))), Seq(Row(ts1), Row(ts2)))
if (legacyParserPolicy == "legacy") {
// In Spark 2.4 and earlier, to_timestamp() parses in seconds precision and cuts off
// the fractional part of seconds. The behavior was changed by SPARK-27438.
// Ignore this test case. Velox returns null for such case.
// val legacyFmt = "yyyy/MM/dd HH:mm:ss"
// checkAnswer(df.select(to_timestamp(col("s"), legacyFmt)), Seq(
// Row(ts1), Row(ts2)))
} else {
checkAnswer(df.select(to_timestamp(col("s"), fmt)), Seq(Row(ts1m), Row(ts2m)))
}
checkAnswer(df.select(to_timestamp(col("ts"), fmt)), Seq(Row(ts1), Row(ts2)))
checkAnswer(
df.select(to_timestamp(col("d"), "yyyy-MM-dd")),
Seq(Row(ts_date1), Row(ts_date2)))
val invalid = df1.selectExpr(s"to_unix_timestamp(x, 'yyyy-MM-dd bb:HH:ss')")
checkAnswer(invalid, Seq(Row(null), Row(null), Row(null), Row(null)))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,13 @@ object GlutenConfig {
SPARK_SHUFFLE_FILE_BUFFER,
(JavaUtils.byteStringAs(v, ByteUnit.KiB) * 1024).toString))

conf
.get(LEGACY_TIME_PARSER_POLICY.key)
.foreach(
v =>
nativeConfMap
.put(LEGACY_TIME_PARSER_POLICY.key, v.toUpperCase(Locale.ROOT)))

// Backend's dynamic session conf only.
val confPrefix = prefixOf(backendName)
conf
Expand Down
Loading