0lai0 commented on code in PR #3804:
URL: https://github.com/apache/datafusion-comet/pull/3804#discussion_r3063429146
##########
spark/src/test/scala/org/apache/comet/CometTemporalExpressionSuite.scala:
##########
@@ -489,4 +489,58 @@ class CometTemporalExpressionSuite extends CometTestBase
with AdaptiveSparkPlanH
dummyDF.selectExpr("unix_date(cast(NULL as date))"))
}
}
+
+ /**
+ * Checks that the Comet-evaluated DataFrame produces the same results as
the baseline DataFrame
+ * evaluated by native Spark JVM, and that Comet native operators are used.
This is needed
+ * because Hours is a PartitionTransformExpression that extends Unevaluable.
+ */
+ private def checkHours(cometDF: DataFrame, baselineDF: DataFrame): Unit = {
+ // Ensure the expected answer is evaluated solely by native Spark JVM
(Comet off)
+ var expected: Array[Row] = Array.empty
+ withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
+ expected = baselineDF.collect()
+ }
+ checkAnswer(cometDF, expected.toSeq)
+ checkCometOperators(stripAQEPlan(cometDF.queryExecution.executedPlan))
+ }
+
+ test("hours - timestamp input") {
+ import org.apache.spark.sql.catalyst.expressions.Hours
+ val r = new Random(42)
+ val tsSchema = StructType(Seq(StructField("ts", DataTypes.TimestampType,
true)))
+ val tsDF = FuzzDataGenerator.generateDataFrame(r, spark, tsSchema, 1000,
DataGenOptions())
+
+ for (timezone <- Seq("UTC", "America/Los_Angeles", "Asia/Tokyo")) {
+ withSQLConf(SQLConf.SESSION_LOCAL_TIMEZONE.key -> timezone) {
+ checkHours(
+ tsDF.select(col("ts"),
getColumnFromExpression(Hours(UnresolvedAttribute("ts")))),
+ tsDF.selectExpr("ts", "cast(floor(unix_micros(ts) / 3600000000D) as
int)"))
+ }
+ }
+ }
+
+ test("hours - timestamp_ntz input") {
+ import org.apache.spark.sql.catalyst.expressions.Hours
+ val r = new Random(42)
+ val ntzSchema = StructType(Seq(StructField("ts",
DataTypes.TimestampNTZType, true)))
+ val ntzDF = FuzzDataGenerator.generateDataFrame(r, spark, ntzSchema, 1000,
DataGenOptions())
+
+ val _spark = spark
+ import _spark.implicits._
+ val expectedDF = ntzDF
+ .map { row =>
+ val ts = row.getAs[java.time.LocalDateTime]("ts")
+ val micros = if (ts != null) {
+
org.apache.spark.sql.catalyst.util.DateTimeUtils.localDateTimeToMicros(ts)
+ } else 0L // assuming safe non-null
Review Comment:
Fixed. It now properly handles null values and returns null instead of 0.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]